blob: 4534951809bf9ed985c75683bd3fde609b204aac [file] [log] [blame]
Logan3f3d31f2010-11-27 13:52:03 +08001/*
Stephen Hinescc366e52012-02-21 17:22:04 -08002 * Copyright 2010-2012, The Android Open Source Project
Logan3f3d31f2010-11-27 13:52:03 +08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BCC_SCRIPT_H
18#define BCC_SCRIPT_H
19
20#include <bcc/bcc.h>
Loganf340bf72011-01-14 17:51:40 +080021#include "bcc_internal.h"
Logan3f3d31f2010-11-27 13:52:03 +080022
Zonr Changc09dee62012-04-12 17:15:30 +080023#include "BCCContext.h"
Loganecf4cbd2011-01-06 05:34:11 +080024#include "Compiler.h"
25
Shih-wei Liao8afed382012-01-10 15:57:24 +080026#include <llvm/Support/CodeGen.h>
27
Logan Chien7890d432011-08-03 14:55:17 +080028#include <vector>
29#include <string>
30
Loganbe79ada2011-01-13 01:33:45 +080031#include <stddef.h>
32
Loganeaa0cc32010-12-29 01:04:20 +080033namespace llvm {
34 class Module;
Daniel Malea094881f2011-12-14 17:39:16 -050035 class GDBJITRegistrar;
Loganeaa0cc32010-12-29 01:04:20 +080036}
37
Logan3f3d31f2010-11-27 13:52:03 +080038namespace bcc {
Logancf3e5212010-12-29 01:44:55 +080039 class ScriptCompiled;
40 class ScriptCached;
Logan474cbd22011-01-31 01:47:44 +080041 class SourceInfo;
Zonr Chang2fcbd022012-01-06 21:04:31 +080042 struct CompilerOption;
Logancf3e5212010-12-29 01:44:55 +080043
44 namespace ScriptStatus {
45 enum StatusType {
46 Unknown,
47 Compiled,
Stephen Hines0e567862012-03-11 20:26:40 -070048 Cached
Logancf3e5212010-12-29 01:44:55 +080049 };
50 }
Logan3f3d31f2010-11-27 13:52:03 +080051
Zonr Chang4ea08862012-01-17 17:26:49 +080052 namespace ScriptObject {
53 enum ObjectType {
54 Unknown,
55 Relocatable,
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -080056 SharedObject,
Zonr Chang4ea08862012-01-17 17:26:49 +080057 Executable,
58 };
59 }
60
Logan0647e9e2010-12-29 00:21:31 +080061 class Script {
Logan39736412010-12-29 00:24:04 +080062 private:
Zonr Changc09dee62012-04-12 17:15:30 +080063 BCCContext mContext;
64
Loganf340bf72011-01-14 17:51:40 +080065 int mErrorCode;
Logan39736412010-12-29 00:24:04 +080066
Logancf3e5212010-12-29 01:44:55 +080067 ScriptStatus::StatusType mStatus;
Shih-wei Liao8454a3a2012-03-03 01:50:08 -080068 // The type of the object behind this script after compilation. For
69 // example, after returning from a successful call to prepareRelocatable(),
70 // the value of mObjectType will be ScriptObject::Relocatable.
Zonr Chang4ea08862012-01-17 17:26:49 +080071 ScriptObject::ObjectType mObjectType;
Logancf3e5212010-12-29 01:44:55 +080072
73 union {
74 ScriptCompiled *mCompiled;
75 ScriptCached *mCached;
76 };
77
Logan Chien311c26f2011-07-11 14:30:34 +080078 std::string mCacheDir;
79 std::string mCacheName;
Zonr Chang4ea08862012-01-17 17:26:49 +080080
81 inline std::string getCachedObjectPath() const {
Shih-wei Liao8454a3a2012-03-03 01:50:08 -080082 return std::string(mCacheDir + mCacheName + ".o");
Zonr Chang4ea08862012-01-17 17:26:49 +080083 }
84
85 inline std::string getCacheInfoPath() const {
Zonr Chang6692ab52012-01-17 17:29:13 +080086 return getCachedObjectPath().append(".info");
Zonr Chang4ea08862012-01-17 17:26:49 +080087 }
Loganecf4cbd2011-01-06 05:34:11 +080088
Logan42598052011-01-26 22:41:13 +080089 bool mIsContextSlotNotAvail;
90
Logan474cbd22011-01-31 01:47:44 +080091 // Source List
92 SourceInfo *mSourceList[2];
93 // Note: mSourceList[0] (main source)
94 // Note: mSourceList[1] (library source)
95 // TODO(logan): Generalize this, use vector or SmallVector instead!
Logan3133c412011-01-06 06:15:40 +080096
Logan Chien7890d432011-08-03 14:55:17 +080097 // External Function List
98 std::vector<char const *> mUserDefinedExternalSymbols;
99
Loganecf4cbd2011-01-06 05:34:11 +0800100 // Register Symbol Lookup Function
Logancf3e5212010-12-29 01:44:55 +0800101 BCCSymbolLookupFn mpExtSymbolLookupFn;
Loganf340bf72011-01-14 17:51:40 +0800102 void *mpExtSymbolLookupFnContext;
Loganeaa0cc32010-12-29 01:04:20 +0800103
Logan3f3d31f2010-11-27 13:52:03 +0800104 public:
Logan033f46e2011-01-06 05:51:24 +0800105 Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
Stephen Hinesa7e5c8f2012-03-28 12:58:24 -0700106 mObjectType(ScriptObject::Unknown),
Logan Chien311c26f2011-07-11 14:30:34 +0800107 mIsContextSlotNotAvail(false),
Stephen Hines74785ec2012-04-02 12:26:21 -0700108 mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
Loganecf4cbd2011-01-06 05:34:11 +0800109 Compiler::GlobalInitialization();
Logan474cbd22011-01-31 01:47:44 +0800110
111 mSourceList[0] = NULL;
112 mSourceList[1] = NULL;
Logan3f3d31f2010-11-27 13:52:03 +0800113 }
114
Logancf3e5212010-12-29 01:44:55 +0800115 ~Script();
Loganeaa0cc32010-12-29 01:04:20 +0800116
Logan474cbd22011-01-31 01:47:44 +0800117 int addSourceBC(size_t idx,
118 char const *resName,
119 const char *bitcode,
120 size_t bitcodeSize,
121 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800122
Logan474cbd22011-01-31 01:47:44 +0800123 int addSourceModule(size_t idx,
124 llvm::Module *module,
125 unsigned long flags);
Loganecf4cbd2011-01-06 05:34:11 +0800126
Logan474cbd22011-01-31 01:47:44 +0800127 int addSourceFile(size_t idx,
128 char const *path,
129 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800130
Logan Chien7890d432011-08-03 14:55:17 +0800131 void markExternalSymbol(char const *name) {
132 mUserDefinedExternalSymbols.push_back(name);
133 }
134
135 std::vector<char const *> const &getUserDefinedExternalSymbols() const {
136 return mUserDefinedExternalSymbols;
137 }
138
Logan Chien311c26f2011-07-11 14:30:34 +0800139 int prepareExecutable(char const *cacheDir,
140 char const *cacheName,
141 unsigned long flags);
Shih-wei Liaoa0ed34e2012-03-03 01:33:30 -0800142 int writeCache();
Loganeaa0cc32010-12-29 01:04:20 +0800143
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800144 /*
145 * Link the given bitcodes in mSourceList to shared object (.so).
146 *
147 * Currently, it requires one to provide the relocatable object files with
148 * given bitcodes to output a shared object.
149 *
150 * The usage of this function is flexible. You can have a relocatable object
151 * compiled before and pass it in objPath to generate shared object. If the
152 * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
153 * you haven't done that yet) and then link the output relocatable object
Shih-wei Liao69341742012-03-03 01:45:36 -0800154 * file to .so in dsoPath.
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800155 *
156 * TODO: Currently, we only support to link the bitcodes in mSourceList[0].
157 *
158 */
Shih-wei Liao69341742012-03-03 01:45:36 -0800159 int prepareSharedObject(char const *objPath,
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800160 char const *dsoPath,
161 unsigned long flags);
162
Shih-wei Liaod8ed6a92012-03-03 01:44:29 -0800163 int prepareRelocatable(char const *objPath,
Shih-wei Liao6a60f4e2012-01-17 02:58:40 -0800164 llvm::Reloc::Model RelocModel,
165 unsigned long flags);
Joseph Wen34c600a2011-07-25 17:59:17 -0700166
Logancf3e5212010-12-29 01:44:55 +0800167 char const *getCompilerErrorMessage();
Loganeaa0cc32010-12-29 01:04:20 +0800168
Logancf3e5212010-12-29 01:44:55 +0800169 void *lookup(const char *name);
Loganeaa0cc32010-12-29 01:04:20 +0800170
Loganbe79ada2011-01-13 01:33:45 +0800171 size_t getExportVarCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800172
Loganbe79ada2011-01-13 01:33:45 +0800173 size_t getExportFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800174
Stephen Hinescc366e52012-02-21 17:22:04 -0800175 size_t getExportForEachCount() const;
176
Loganbe79ada2011-01-13 01:33:45 +0800177 size_t getPragmaCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800178
Loganbe79ada2011-01-13 01:33:45 +0800179 size_t getFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800180
Stephen Hines071288a2011-01-27 14:38:26 -0800181 size_t getObjectSlotCount() const;
Loganbe79ada2011-01-13 01:33:45 +0800182
183 void getExportVarList(size_t size, void **list);
184
185 void getExportFuncList(size_t size, void **list);
186
Stephen Hinescc366e52012-02-21 17:22:04 -0800187 void getExportForEachList(size_t size, void **list);
188
Joseph Wenf36637f2011-07-06 18:27:12 -0700189 void getExportVarNameList(std::vector<std::string> &list);
190
191 void getExportFuncNameList(std::vector<std::string> &list);
192
Stephen Hinescc366e52012-02-21 17:22:04 -0800193 void getExportForEachNameList(std::vector<std::string> &list);
194
Loganbe79ada2011-01-13 01:33:45 +0800195 void getPragmaList(size_t size,
196 char const **keyList,
197 char const **valueList);
198
Loganf340bf72011-01-14 17:51:40 +0800199 void getFuncInfoList(size_t size, FuncInfo *list);
Loganbe79ada2011-01-13 01:33:45 +0800200
Stephen Hines071288a2011-01-27 14:38:26 -0800201 void getObjectSlotList(size_t size, uint32_t *list);
202
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700203 size_t getELFSize() const;
204
205 const char *getELF() const;
Logan02286cb2011-01-07 00:30:47 +0800206
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800207 int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
Loganeaa0cc32010-12-29 01:04:20 +0800208
Shih-wei Liaoabc7f512012-01-18 00:34:07 -0800209 bool isCacheable() const;
Loganeaa0cc32010-12-29 01:04:20 +0800210
Loganf340bf72011-01-14 17:51:40 +0800211 void setError(int error) {
Logan39736412010-12-29 00:24:04 +0800212 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
213 mErrorCode = error;
Logan3f3d31f2010-11-27 13:52:03 +0800214 }
215 }
216
Loganf340bf72011-01-14 17:51:40 +0800217 int getError() {
218 int result = mErrorCode;
Logan39736412010-12-29 00:24:04 +0800219 mErrorCode = BCC_NO_ERROR;
Logan3f3d31f2010-11-27 13:52:03 +0800220 return result;
221 }
Logan033f46e2011-01-06 05:51:24 +0800222
223 private:
Zonr Chang743dd712012-01-19 10:13:52 +0800224 //
225 // It returns 0 if there's a cache hit.
226 //
Shih-wei Liao8454a3a2012-03-03 01:50:08 -0800227 // Side effect: it will set mCacheDir, mCacheName.
Zonr Chang743dd712012-01-19 10:13:52 +0800228 int internalLoadCache(char const *cacheDir, char const *cacheName,
Shih-wei Liao8454a3a2012-03-03 01:50:08 -0800229 bool checkOnly);
Stephen Hines0e567862012-03-11 20:26:40 -0700230
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800231 int internalCompile(const CompilerOption&);
Logan3f3d31f2010-11-27 13:52:03 +0800232 };
233
234} // namespace bcc
235
236#endif // BCC_SCRIPT_H