blob: b05fc07c8461f3e8053e79f3483ed6d8d71a6994 [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;
Daniel Malea094881f2011-12-14 17:39:16 -050034 class GDBJITRegistrar;
Loganeaa0cc32010-12-29 01:04:20 +080035}
36
Logan3f3d31f2010-11-27 13:52:03 +080037namespace bcc {
Logancf3e5212010-12-29 01:44:55 +080038 class ScriptCompiled;
39 class ScriptCached;
Logan474cbd22011-01-31 01:47:44 +080040 class SourceInfo;
Zonr Chang2fcbd022012-01-06 21:04:31 +080041 struct CompilerOption;
Logancf3e5212010-12-29 01:44:55 +080042
43 namespace ScriptStatus {
44 enum StatusType {
45 Unknown,
46 Compiled,
Logan35849002011-01-15 07:30:43 +080047#if USE_CACHE
Loganf7f0ac52011-01-07 03:53:43 +080048 Cached,
Logan35849002011-01-15 07:30:43 +080049#endif
Logancf3e5212010-12-29 01:44:55 +080050 };
51 }
Logan3f3d31f2010-11-27 13:52:03 +080052
Zonr Chang4ea08862012-01-17 17:26:49 +080053 namespace ScriptObject {
54 enum ObjectType {
55 Unknown,
56 Relocatable,
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -080057 SharedObject,
Zonr Chang4ea08862012-01-17 17:26:49 +080058 Executable,
59 };
60 }
61
Logan0647e9e2010-12-29 00:21:31 +080062 class Script {
Logan39736412010-12-29 00:24:04 +080063 private:
Loganf340bf72011-01-14 17:51:40 +080064 int mErrorCode;
Logan39736412010-12-29 00:24:04 +080065
Logancf3e5212010-12-29 01:44:55 +080066 ScriptStatus::StatusType mStatus;
Shih-wei Liao8454a3a2012-03-03 01:50:08 -080067 // The type of the object behind this script after compilation. For
68 // example, after returning from a successful call to prepareRelocatable(),
69 // the value of mObjectType will be ScriptObject::Relocatable.
Zonr Chang4ea08862012-01-17 17:26:49 +080070 ScriptObject::ObjectType mObjectType;
Logancf3e5212010-12-29 01:44:55 +080071
72 union {
73 ScriptCompiled *mCompiled;
Logan35849002011-01-15 07:30:43 +080074#if USE_CACHE
Logancf3e5212010-12-29 01:44:55 +080075 ScriptCached *mCached;
Logan35849002011-01-15 07:30:43 +080076#endif
Logancf3e5212010-12-29 01:44:55 +080077 };
78
Logan Chien311c26f2011-07-11 14:30:34 +080079#if USE_CACHE
80 std::string mCacheDir;
81 std::string mCacheName;
Zonr Chang4ea08862012-01-17 17:26:49 +080082
83 inline std::string getCachedObjectPath() const {
84#if USE_OLD_JIT
85 return std::string(mCacheDir + mCacheName + ".jit-image");
86#elif USE_MCJIT
Shih-wei Liao8454a3a2012-03-03 01:50:08 -080087 return std::string(mCacheDir + mCacheName + ".o");
Zonr Chang4ea08862012-01-17 17:26:49 +080088#endif
89 }
90
91 inline std::string getCacheInfoPath() const {
92#if USE_OLD_JIT
Zonr Chang6692ab52012-01-17 17:29:13 +080093 return getCachedObjectPath().append(".oBCC");
Zonr Chang4ea08862012-01-17 17:26:49 +080094#elif USE_MCJIT
Zonr Chang6692ab52012-01-17 17:29:13 +080095 return getCachedObjectPath().append(".info");
Zonr Chang4ea08862012-01-17 17:26:49 +080096#endif
97 }
Logan Chien311c26f2011-07-11 14:30:34 +080098#endif
Loganecf4cbd2011-01-06 05:34:11 +080099
Logan42598052011-01-26 22:41:13 +0800100 bool mIsContextSlotNotAvail;
101
Logan474cbd22011-01-31 01:47:44 +0800102 // Source List
103 SourceInfo *mSourceList[2];
104 // Note: mSourceList[0] (main source)
105 // Note: mSourceList[1] (library source)
106 // TODO(logan): Generalize this, use vector or SmallVector instead!
Logan3133c412011-01-06 06:15:40 +0800107
Logan Chien7890d432011-08-03 14:55:17 +0800108 // External Function List
109 std::vector<char const *> mUserDefinedExternalSymbols;
110
Loganecf4cbd2011-01-06 05:34:11 +0800111 // Register Symbol Lookup Function
Logancf3e5212010-12-29 01:44:55 +0800112 BCCSymbolLookupFn mpExtSymbolLookupFn;
Loganf340bf72011-01-14 17:51:40 +0800113 void *mpExtSymbolLookupFnContext;
Loganeaa0cc32010-12-29 01:04:20 +0800114
Logan3f3d31f2010-11-27 13:52:03 +0800115 public:
Logan033f46e2011-01-06 05:51:24 +0800116 Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
Zonr Chang4ea08862012-01-17 17:26:49 +0800117 mObjectType(ScriptObject::Unknown), mIsContextSlotNotAvail(false),
Logan65719812011-01-07 11:17:14 +0800118 mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
Loganecf4cbd2011-01-06 05:34:11 +0800119 Compiler::GlobalInitialization();
Logan474cbd22011-01-31 01:47:44 +0800120
121 mSourceList[0] = NULL;
122 mSourceList[1] = NULL;
Logan3f3d31f2010-11-27 13:52:03 +0800123 }
124
Logancf3e5212010-12-29 01:44:55 +0800125 ~Script();
Loganeaa0cc32010-12-29 01:04:20 +0800126
Logan474cbd22011-01-31 01:47:44 +0800127 int addSourceBC(size_t idx,
128 char const *resName,
129 const char *bitcode,
130 size_t bitcodeSize,
131 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800132
Logan474cbd22011-01-31 01:47:44 +0800133 int addSourceModule(size_t idx,
134 llvm::Module *module,
135 unsigned long flags);
Loganecf4cbd2011-01-06 05:34:11 +0800136
Logan474cbd22011-01-31 01:47:44 +0800137 int addSourceFile(size_t idx,
138 char const *path,
139 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800140
Logan Chien7890d432011-08-03 14:55:17 +0800141 void markExternalSymbol(char const *name) {
142 mUserDefinedExternalSymbols.push_back(name);
143 }
144
145 std::vector<char const *> const &getUserDefinedExternalSymbols() const {
146 return mUserDefinedExternalSymbols;
147 }
148
Logan Chien311c26f2011-07-11 14:30:34 +0800149 int prepareExecutable(char const *cacheDir,
150 char const *cacheName,
151 unsigned long flags);
Shih-wei Liaoa0ed34e2012-03-03 01:33:30 -0800152 int writeCache();
Loganeaa0cc32010-12-29 01:04:20 +0800153
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800154 /*
155 * Link the given bitcodes in mSourceList to shared object (.so).
156 *
157 * Currently, it requires one to provide the relocatable object files with
158 * given bitcodes to output a shared object.
159 *
160 * The usage of this function is flexible. You can have a relocatable object
161 * compiled before and pass it in objPath to generate shared object. If the
162 * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
163 * you haven't done that yet) and then link the output relocatable object
Shih-wei Liao69341742012-03-03 01:45:36 -0800164 * file to .so in dsoPath.
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800165 *
166 * TODO: Currently, we only support to link the bitcodes in mSourceList[0].
167 *
168 */
Shih-wei Liao69341742012-03-03 01:45:36 -0800169 int prepareSharedObject(char const *objPath,
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800170 char const *dsoPath,
171 unsigned long flags);
172
Shih-wei Liaod8ed6a92012-03-03 01:44:29 -0800173 int prepareRelocatable(char const *objPath,
Shih-wei Liao6a60f4e2012-01-17 02:58:40 -0800174 llvm::Reloc::Model RelocModel,
175 unsigned long flags);
Joseph Wen34c600a2011-07-25 17:59:17 -0700176
Logancf3e5212010-12-29 01:44:55 +0800177 char const *getCompilerErrorMessage();
Loganeaa0cc32010-12-29 01:04:20 +0800178
Logancf3e5212010-12-29 01:44:55 +0800179 void *lookup(const char *name);
Loganeaa0cc32010-12-29 01:04:20 +0800180
Loganeaa0cc32010-12-29 01:04:20 +0800181
Loganbe79ada2011-01-13 01:33:45 +0800182 size_t getExportVarCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800183
Loganbe79ada2011-01-13 01:33:45 +0800184 size_t getExportFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800185
Stephen Hinescc366e52012-02-21 17:22:04 -0800186 size_t getExportForEachCount() const;
187
Loganbe79ada2011-01-13 01:33:45 +0800188 size_t getPragmaCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800189
Loganbe79ada2011-01-13 01:33:45 +0800190 size_t getFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800191
Stephen Hines071288a2011-01-27 14:38:26 -0800192 size_t getObjectSlotCount() const;
Loganbe79ada2011-01-13 01:33:45 +0800193
194 void getExportVarList(size_t size, void **list);
195
196 void getExportFuncList(size_t size, void **list);
197
Stephen Hinescc366e52012-02-21 17:22:04 -0800198 void getExportForEachList(size_t size, void **list);
199
Joseph Wenf36637f2011-07-06 18:27:12 -0700200 void getExportVarNameList(std::vector<std::string> &list);
201
202 void getExportFuncNameList(std::vector<std::string> &list);
203
Stephen Hinescc366e52012-02-21 17:22:04 -0800204 void getExportForEachNameList(std::vector<std::string> &list);
205
Loganbe79ada2011-01-13 01:33:45 +0800206 void getPragmaList(size_t size,
207 char const **keyList,
208 char const **valueList);
209
Loganf340bf72011-01-14 17:51:40 +0800210 void getFuncInfoList(size_t size, FuncInfo *list);
Loganbe79ada2011-01-13 01:33:45 +0800211
Stephen Hines071288a2011-01-27 14:38:26 -0800212 void getObjectSlotList(size_t size, uint32_t *list);
213
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700214 size_t getELFSize() const;
215
216 const char *getELF() const;
Logan02286cb2011-01-07 00:30:47 +0800217
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800218 int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
Loganeaa0cc32010-12-29 01:04:20 +0800219
Logan Chiend2a5f302011-07-19 20:32:25 +0800220#if USE_OLD_JIT
Loganbe79ada2011-01-13 01:33:45 +0800221 char *getContext();
Logan Chiend2a5f302011-07-19 20:32:25 +0800222#endif
Loganbe79ada2011-01-13 01:33:45 +0800223
Shih-wei Liaoabc7f512012-01-18 00:34:07 -0800224 bool isCacheable() const;
Loganeaa0cc32010-12-29 01:04:20 +0800225
Loganf340bf72011-01-14 17:51:40 +0800226 void setError(int error) {
Logan39736412010-12-29 00:24:04 +0800227 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
228 mErrorCode = error;
Logan3f3d31f2010-11-27 13:52:03 +0800229 }
230 }
231
Loganf340bf72011-01-14 17:51:40 +0800232 int getError() {
233 int result = mErrorCode;
Logan39736412010-12-29 00:24:04 +0800234 mErrorCode = BCC_NO_ERROR;
Logan3f3d31f2010-11-27 13:52:03 +0800235 return result;
236 }
Logan033f46e2011-01-06 05:51:24 +0800237
238 private:
Logan35849002011-01-15 07:30:43 +0800239#if USE_CACHE
Zonr Chang743dd712012-01-19 10:13:52 +0800240 //
241 // It returns 0 if there's a cache hit.
242 //
Shih-wei Liao8454a3a2012-03-03 01:50:08 -0800243 // Side effect: it will set mCacheDir, mCacheName.
Zonr Chang743dd712012-01-19 10:13:52 +0800244 int internalLoadCache(char const *cacheDir, char const *cacheName,
Shih-wei Liao8454a3a2012-03-03 01:50:08 -0800245 bool checkOnly);
Logan35849002011-01-15 07:30:43 +0800246#endif
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800247 int internalCompile(const CompilerOption&);
Logan3f3d31f2010-11-27 13:52:03 +0800248 };
249
250} // namespace bcc
251
252#endif // BCC_SCRIPT_H