blob: fbe49569e27e6e70b8982f0eb48de170c5430aa5 [file] [log] [blame]
Logan3f3d31f2010-11-27 13:52:03 +08001/*
2 * Copyright 2010, The Android Open Source Project
3 *
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,
56 Executable,
57 };
58 }
59
Logan0647e9e2010-12-29 00:21:31 +080060 class Script {
Logan39736412010-12-29 00:24:04 +080061 private:
Loganf340bf72011-01-14 17:51:40 +080062 int mErrorCode;
Logan39736412010-12-29 00:24:04 +080063
Logancf3e5212010-12-29 01:44:55 +080064 ScriptStatus::StatusType mStatus;
Zonr Chang4ea08862012-01-17 17:26:49 +080065 ScriptObject::ObjectType mObjectType;
Logancf3e5212010-12-29 01:44:55 +080066
67 union {
68 ScriptCompiled *mCompiled;
Logan35849002011-01-15 07:30:43 +080069#if USE_CACHE
Logancf3e5212010-12-29 01:44:55 +080070 ScriptCached *mCached;
Logan35849002011-01-15 07:30:43 +080071#endif
Logancf3e5212010-12-29 01:44:55 +080072 };
73
Logan Chien311c26f2011-07-11 14:30:34 +080074#if USE_CACHE
75 std::string mCacheDir;
76 std::string mCacheName;
Zonr Chang4ea08862012-01-17 17:26:49 +080077
78 inline std::string getCachedObjectPath() const {
79#if USE_OLD_JIT
80 return std::string(mCacheDir + mCacheName + ".jit-image");
81#elif USE_MCJIT
82 std::string objPath(mCacheDir + mCacheName);
83
84 // Append suffix depends on the object type
85 switch (mObjectType) {
86 case ScriptObject::Relocatable:
87 case ScriptObject::Executable: {
88 objPath.append(".o");
89 break;
90 }
91 default: {
92 assert(false && "Unknown onject type!");
93 }
94 }
95 return objPath;
96#endif
97 }
98
99 inline std::string getCacheInfoPath() const {
100#if USE_OLD_JIT
Zonr Chang6692ab52012-01-17 17:29:13 +0800101 return getCachedObjectPath().append(".oBCC");
Zonr Chang4ea08862012-01-17 17:26:49 +0800102#elif USE_MCJIT
Zonr Chang6692ab52012-01-17 17:29:13 +0800103 return getCachedObjectPath().append(".info");
Zonr Chang4ea08862012-01-17 17:26:49 +0800104#endif
105 }
Logan Chien311c26f2011-07-11 14:30:34 +0800106#endif
Loganecf4cbd2011-01-06 05:34:11 +0800107
Logan42598052011-01-26 22:41:13 +0800108 bool mIsContextSlotNotAvail;
109
Logan474cbd22011-01-31 01:47:44 +0800110 // Source List
111 SourceInfo *mSourceList[2];
112 // Note: mSourceList[0] (main source)
113 // Note: mSourceList[1] (library source)
114 // TODO(logan): Generalize this, use vector or SmallVector instead!
Logan3133c412011-01-06 06:15:40 +0800115
Logan Chien7890d432011-08-03 14:55:17 +0800116 // External Function List
117 std::vector<char const *> mUserDefinedExternalSymbols;
118
Loganecf4cbd2011-01-06 05:34:11 +0800119 // Register Symbol Lookup Function
Logancf3e5212010-12-29 01:44:55 +0800120 BCCSymbolLookupFn mpExtSymbolLookupFn;
Loganf340bf72011-01-14 17:51:40 +0800121 void *mpExtSymbolLookupFnContext;
Loganeaa0cc32010-12-29 01:04:20 +0800122
Logan3f3d31f2010-11-27 13:52:03 +0800123 public:
Logan033f46e2011-01-06 05:51:24 +0800124 Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
Zonr Chang4ea08862012-01-17 17:26:49 +0800125 mObjectType(ScriptObject::Unknown), mIsContextSlotNotAvail(false),
Logan65719812011-01-07 11:17:14 +0800126 mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
Loganecf4cbd2011-01-06 05:34:11 +0800127 Compiler::GlobalInitialization();
Logan474cbd22011-01-31 01:47:44 +0800128
129 mSourceList[0] = NULL;
130 mSourceList[1] = NULL;
Logan3f3d31f2010-11-27 13:52:03 +0800131 }
132
Logancf3e5212010-12-29 01:44:55 +0800133 ~Script();
Loganeaa0cc32010-12-29 01:04:20 +0800134
Logan474cbd22011-01-31 01:47:44 +0800135 int addSourceBC(size_t idx,
136 char const *resName,
137 const char *bitcode,
138 size_t bitcodeSize,
139 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800140
Logan474cbd22011-01-31 01:47:44 +0800141 int addSourceModule(size_t idx,
142 llvm::Module *module,
143 unsigned long flags);
Loganecf4cbd2011-01-06 05:34:11 +0800144
Logan474cbd22011-01-31 01:47:44 +0800145 int addSourceFile(size_t idx,
146 char const *path,
147 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800148
Logan Chien7890d432011-08-03 14:55:17 +0800149 void markExternalSymbol(char const *name) {
150 mUserDefinedExternalSymbols.push_back(name);
151 }
152
153 std::vector<char const *> const &getUserDefinedExternalSymbols() const {
154 return mUserDefinedExternalSymbols;
155 }
156
Logan Chien311c26f2011-07-11 14:30:34 +0800157 int prepareExecutable(char const *cacheDir,
158 char const *cacheName,
159 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800160
Shih-wei Liao6a60f4e2012-01-17 02:58:40 -0800161 int prepareRelocatable(char const *cacheDir,
162 char const *cacheName,
163 llvm::Reloc::Model RelocModel,
164 unsigned long flags);
Joseph Wen34c600a2011-07-25 17:59:17 -0700165
Logancf3e5212010-12-29 01:44:55 +0800166 char const *getCompilerErrorMessage();
Loganeaa0cc32010-12-29 01:04:20 +0800167
Logancf3e5212010-12-29 01:44:55 +0800168 void *lookup(const char *name);
Loganeaa0cc32010-12-29 01:04:20 +0800169
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
Loganbe79ada2011-01-13 01:33:45 +0800175 size_t getPragmaCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800176
Loganbe79ada2011-01-13 01:33:45 +0800177 size_t getFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800178
Stephen Hines071288a2011-01-27 14:38:26 -0800179 size_t getObjectSlotCount() const;
Loganbe79ada2011-01-13 01:33:45 +0800180
181 void getExportVarList(size_t size, void **list);
182
183 void getExportFuncList(size_t size, void **list);
184
Joseph Wenf36637f2011-07-06 18:27:12 -0700185 void getExportVarNameList(std::vector<std::string> &list);
186
187 void getExportFuncNameList(std::vector<std::string> &list);
188
Loganbe79ada2011-01-13 01:33:45 +0800189 void getPragmaList(size_t size,
190 char const **keyList,
191 char const **valueList);
192
Loganf340bf72011-01-14 17:51:40 +0800193 void getFuncInfoList(size_t size, FuncInfo *list);
Loganbe79ada2011-01-13 01:33:45 +0800194
Stephen Hines071288a2011-01-27 14:38:26 -0800195 void getObjectSlotList(size_t size, uint32_t *list);
196
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700197 size_t getELFSize() const;
198
199 const char *getELF() const;
Logan02286cb2011-01-07 00:30:47 +0800200
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800201 int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
Loganeaa0cc32010-12-29 01:04:20 +0800202
Logan Chiend2a5f302011-07-19 20:32:25 +0800203#if USE_OLD_JIT
Loganbe79ada2011-01-13 01:33:45 +0800204 char *getContext();
Logan Chiend2a5f302011-07-19 20:32:25 +0800205#endif
Loganbe79ada2011-01-13 01:33:45 +0800206
Loganeaa0cc32010-12-29 01:04:20 +0800207
Loganf340bf72011-01-14 17:51:40 +0800208 void setError(int error) {
Logan39736412010-12-29 00:24:04 +0800209 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
210 mErrorCode = error;
Logan3f3d31f2010-11-27 13:52:03 +0800211 }
212 }
213
Loganf340bf72011-01-14 17:51:40 +0800214 int getError() {
215 int result = mErrorCode;
Logan39736412010-12-29 00:24:04 +0800216 mErrorCode = BCC_NO_ERROR;
Logan3f3d31f2010-11-27 13:52:03 +0800217 return result;
218 }
Logan033f46e2011-01-06 05:51:24 +0800219
220 private:
Logan35849002011-01-15 07:30:43 +0800221#if USE_CACHE
Joseph Wen34c600a2011-07-25 17:59:17 -0700222 int internalLoadCache(bool checkOnly);
Logan35849002011-01-15 07:30:43 +0800223#endif
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800224 int internalCompile(const CompilerOption&);
Logan033f46e2011-01-06 05:51:24 +0800225
Logan3f3d31f2010-11-27 13:52:03 +0800226 };
227
228} // namespace bcc
229
230#endif // BCC_SCRIPT_H