blob: 27931e68e711cb61b37924d8c053b3fe41ff6546 [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
Loganecf4cbd2011-01-06 05:34:11 +080023#include "Compiler.h"
24
Shih-wei Liao8afed382012-01-10 15:57:24 +080025#include <llvm/Support/CodeGen.h>
26
Logan Chien7890d432011-08-03 14:55:17 +080027#include <vector>
28#include <string>
29
Loganbe79ada2011-01-13 01:33:45 +080030#include <stddef.h>
31
Loganeaa0cc32010-12-29 01:04:20 +080032namespace llvm {
33 class Module;
34}
35
Logan3f3d31f2010-11-27 13:52:03 +080036namespace bcc {
Logancf3e5212010-12-29 01:44:55 +080037 class ScriptCompiled;
38 class ScriptCached;
Logan474cbd22011-01-31 01:47:44 +080039 class SourceInfo;
Zonr Chang2fcbd022012-01-06 21:04:31 +080040 struct CompilerOption;
Logancf3e5212010-12-29 01:44:55 +080041
42 namespace ScriptStatus {
43 enum StatusType {
44 Unknown,
45 Compiled,
Logan35849002011-01-15 07:30:43 +080046#if USE_CACHE
Loganf7f0ac52011-01-07 03:53:43 +080047 Cached,
Logan35849002011-01-15 07:30:43 +080048#endif
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:
Loganf340bf72011-01-14 17:51:40 +080063 int mErrorCode;
Logan39736412010-12-29 00:24:04 +080064
Logancf3e5212010-12-29 01:44:55 +080065 ScriptStatus::StatusType mStatus;
Zonr Chang4ea08862012-01-17 17:26:49 +080066 ScriptObject::ObjectType mObjectType;
Logancf3e5212010-12-29 01:44:55 +080067
68 union {
69 ScriptCompiled *mCompiled;
Logan35849002011-01-15 07:30:43 +080070#if USE_CACHE
Logancf3e5212010-12-29 01:44:55 +080071 ScriptCached *mCached;
Logan35849002011-01-15 07:30:43 +080072#endif
Logancf3e5212010-12-29 01:44:55 +080073 };
74
Logan Chien311c26f2011-07-11 14:30:34 +080075#if USE_CACHE
76 std::string mCacheDir;
77 std::string mCacheName;
Zonr Chang4ea08862012-01-17 17:26:49 +080078
79 inline std::string getCachedObjectPath() const {
80#if USE_OLD_JIT
81 return std::string(mCacheDir + mCacheName + ".jit-image");
82#elif USE_MCJIT
83 std::string objPath(mCacheDir + mCacheName);
84
85 // Append suffix depends on the object type
86 switch (mObjectType) {
87 case ScriptObject::Relocatable:
88 case ScriptObject::Executable: {
89 objPath.append(".o");
90 break;
91 }
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -080092
93 case ScriptObject::SharedObject: {
94 objPath.append(".so");
95 break;
96 }
97
Zonr Chang4ea08862012-01-17 17:26:49 +080098 default: {
Shih-wei Liao2b7db0e2012-01-17 17:49:46 -080099 assert(false && "Unknown object type!");
Zonr Chang4ea08862012-01-17 17:26:49 +0800100 }
101 }
102 return objPath;
103#endif
104 }
105
106 inline std::string getCacheInfoPath() const {
107#if USE_OLD_JIT
Zonr Chang6692ab52012-01-17 17:29:13 +0800108 return getCachedObjectPath().append(".oBCC");
Zonr Chang4ea08862012-01-17 17:26:49 +0800109#elif USE_MCJIT
Zonr Chang6692ab52012-01-17 17:29:13 +0800110 return getCachedObjectPath().append(".info");
Zonr Chang4ea08862012-01-17 17:26:49 +0800111#endif
112 }
Logan Chien311c26f2011-07-11 14:30:34 +0800113#endif
Loganecf4cbd2011-01-06 05:34:11 +0800114
Logan42598052011-01-26 22:41:13 +0800115 bool mIsContextSlotNotAvail;
116
Logan474cbd22011-01-31 01:47:44 +0800117 // Source List
118 SourceInfo *mSourceList[2];
119 // Note: mSourceList[0] (main source)
120 // Note: mSourceList[1] (library source)
121 // TODO(logan): Generalize this, use vector or SmallVector instead!
Logan3133c412011-01-06 06:15:40 +0800122
Logan Chien7890d432011-08-03 14:55:17 +0800123 // External Function List
124 std::vector<char const *> mUserDefinedExternalSymbols;
125
Loganecf4cbd2011-01-06 05:34:11 +0800126 // Register Symbol Lookup Function
Logancf3e5212010-12-29 01:44:55 +0800127 BCCSymbolLookupFn mpExtSymbolLookupFn;
Loganf340bf72011-01-14 17:51:40 +0800128 void *mpExtSymbolLookupFnContext;
Loganeaa0cc32010-12-29 01:04:20 +0800129
Logan3f3d31f2010-11-27 13:52:03 +0800130 public:
Logan033f46e2011-01-06 05:51:24 +0800131 Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
Zonr Chang4ea08862012-01-17 17:26:49 +0800132 mObjectType(ScriptObject::Unknown), mIsContextSlotNotAvail(false),
Logan65719812011-01-07 11:17:14 +0800133 mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
Loganecf4cbd2011-01-06 05:34:11 +0800134 Compiler::GlobalInitialization();
Logan474cbd22011-01-31 01:47:44 +0800135
136 mSourceList[0] = NULL;
137 mSourceList[1] = NULL;
Logan3f3d31f2010-11-27 13:52:03 +0800138 }
139
Logancf3e5212010-12-29 01:44:55 +0800140 ~Script();
Loganeaa0cc32010-12-29 01:04:20 +0800141
Logan474cbd22011-01-31 01:47:44 +0800142 int addSourceBC(size_t idx,
143 char const *resName,
144 const char *bitcode,
145 size_t bitcodeSize,
146 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800147
Logan474cbd22011-01-31 01:47:44 +0800148 int addSourceModule(size_t idx,
149 llvm::Module *module,
150 unsigned long flags);
Loganecf4cbd2011-01-06 05:34:11 +0800151
Logan474cbd22011-01-31 01:47:44 +0800152 int addSourceFile(size_t idx,
153 char const *path,
154 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800155
Logan Chien7890d432011-08-03 14:55:17 +0800156 void markExternalSymbol(char const *name) {
157 mUserDefinedExternalSymbols.push_back(name);
158 }
159
160 std::vector<char const *> const &getUserDefinedExternalSymbols() const {
161 return mUserDefinedExternalSymbols;
162 }
163
Logan Chien311c26f2011-07-11 14:30:34 +0800164 int prepareExecutable(char const *cacheDir,
165 char const *cacheName,
166 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800167
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800168 /*
169 * Link the given bitcodes in mSourceList to shared object (.so).
170 *
171 * Currently, it requires one to provide the relocatable object files with
172 * given bitcodes to output a shared object.
173 *
174 * The usage of this function is flexible. You can have a relocatable object
175 * compiled before and pass it in objPath to generate shared object. If the
176 * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
177 * you haven't done that yet) and then link the output relocatable object
178 * file. The latter case will have libbcc compile with USE_CACHE enabled.
179 *
180 * TODO: Currently, we only support to link the bitcodes in mSourceList[0].
181 *
182 */
183 int prepareSharedObject(char const *cacheDir,
184 char const *cacheName,
185 char const *objPath,
186 char const *dsoPath,
187 unsigned long flags);
188
Shih-wei Liao6a60f4e2012-01-17 02:58:40 -0800189 int prepareRelocatable(char const *cacheDir,
190 char const *cacheName,
191 llvm::Reloc::Model RelocModel,
192 unsigned long flags);
Joseph Wen34c600a2011-07-25 17:59:17 -0700193
Logancf3e5212010-12-29 01:44:55 +0800194 char const *getCompilerErrorMessage();
Loganeaa0cc32010-12-29 01:04:20 +0800195
Logancf3e5212010-12-29 01:44:55 +0800196 void *lookup(const char *name);
Loganeaa0cc32010-12-29 01:04:20 +0800197
Loganeaa0cc32010-12-29 01:04:20 +0800198
Loganbe79ada2011-01-13 01:33:45 +0800199 size_t getExportVarCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800200
Loganbe79ada2011-01-13 01:33:45 +0800201 size_t getExportFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800202
Stephen Hinescc366e52012-02-21 17:22:04 -0800203 size_t getExportForEachCount() const;
204
Loganbe79ada2011-01-13 01:33:45 +0800205 size_t getPragmaCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800206
Loganbe79ada2011-01-13 01:33:45 +0800207 size_t getFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800208
Stephen Hines071288a2011-01-27 14:38:26 -0800209 size_t getObjectSlotCount() const;
Loganbe79ada2011-01-13 01:33:45 +0800210
211 void getExportVarList(size_t size, void **list);
212
213 void getExportFuncList(size_t size, void **list);
214
Stephen Hinescc366e52012-02-21 17:22:04 -0800215 void getExportForEachList(size_t size, void **list);
216
Joseph Wenf36637f2011-07-06 18:27:12 -0700217 void getExportVarNameList(std::vector<std::string> &list);
218
219 void getExportFuncNameList(std::vector<std::string> &list);
220
Stephen Hinescc366e52012-02-21 17:22:04 -0800221 void getExportForEachNameList(std::vector<std::string> &list);
222
Loganbe79ada2011-01-13 01:33:45 +0800223 void getPragmaList(size_t size,
224 char const **keyList,
225 char const **valueList);
226
Loganf340bf72011-01-14 17:51:40 +0800227 void getFuncInfoList(size_t size, FuncInfo *list);
Loganbe79ada2011-01-13 01:33:45 +0800228
Stephen Hines071288a2011-01-27 14:38:26 -0800229 void getObjectSlotList(size_t size, uint32_t *list);
230
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700231 size_t getELFSize() const;
232
233 const char *getELF() const;
Logan02286cb2011-01-07 00:30:47 +0800234
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800235 int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
Loganeaa0cc32010-12-29 01:04:20 +0800236
Logan Chiend2a5f302011-07-19 20:32:25 +0800237#if USE_OLD_JIT
Loganbe79ada2011-01-13 01:33:45 +0800238 char *getContext();
Logan Chiend2a5f302011-07-19 20:32:25 +0800239#endif
Loganbe79ada2011-01-13 01:33:45 +0800240
Shih-wei Liaoabc7f512012-01-18 00:34:07 -0800241 bool isCacheable() const;
Loganeaa0cc32010-12-29 01:04:20 +0800242
Loganf340bf72011-01-14 17:51:40 +0800243 void setError(int error) {
Logan39736412010-12-29 00:24:04 +0800244 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
245 mErrorCode = error;
Logan3f3d31f2010-11-27 13:52:03 +0800246 }
247 }
248
Loganf340bf72011-01-14 17:51:40 +0800249 int getError() {
250 int result = mErrorCode;
Logan39736412010-12-29 00:24:04 +0800251 mErrorCode = BCC_NO_ERROR;
Logan3f3d31f2010-11-27 13:52:03 +0800252 return result;
253 }
Logan033f46e2011-01-06 05:51:24 +0800254
255 private:
Logan35849002011-01-15 07:30:43 +0800256#if USE_CACHE
Zonr Chang743dd712012-01-19 10:13:52 +0800257 //
258 // It returns 0 if there's a cache hit.
259 //
260 // Side effect: it will set mCacheDir, mCacheName and mObjectType.
261 int internalLoadCache(char const *cacheDir, char const *cacheName,
262 ScriptObject::ObjectType objectType, bool checkOnly);
Logan35849002011-01-15 07:30:43 +0800263#endif
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800264 int internalCompile(const CompilerOption&);
Logan033f46e2011-01-06 05:51:24 +0800265
Logan3f3d31f2010-11-27 13:52:03 +0800266 };
267
268} // namespace bcc
269
270#endif // BCC_SCRIPT_H