blob: 0560f55cb2715d100c1d3d744a5f9dd2f616285f [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,
Stephen Hines0e567862012-03-11 20:26:40 -070047 Cached
Logancf3e5212010-12-29 01:44:55 +080048 };
49 }
Logan3f3d31f2010-11-27 13:52:03 +080050
Zonr Chang4ea08862012-01-17 17:26:49 +080051 namespace ScriptObject {
52 enum ObjectType {
53 Unknown,
54 Relocatable,
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -080055 SharedObject,
Zonr Chang4ea08862012-01-17 17:26:49 +080056 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;
Shih-wei Liao8454a3a2012-03-03 01:50:08 -080065 // The type of the object behind this script after compilation. For
66 // example, after returning from a successful call to prepareRelocatable(),
67 // the value of mObjectType will be ScriptObject::Relocatable.
Zonr Chang4ea08862012-01-17 17:26:49 +080068 ScriptObject::ObjectType mObjectType;
Logancf3e5212010-12-29 01:44:55 +080069
70 union {
71 ScriptCompiled *mCompiled;
72 ScriptCached *mCached;
73 };
74
Logan Chien311c26f2011-07-11 14:30:34 +080075 std::string mCacheDir;
76 std::string mCacheName;
Zonr Chang4ea08862012-01-17 17:26:49 +080077
78 inline std::string getCachedObjectPath() const {
Shih-wei Liao8454a3a2012-03-03 01:50:08 -080079 return std::string(mCacheDir + mCacheName + ".o");
Zonr Chang4ea08862012-01-17 17:26:49 +080080 }
81
82 inline std::string getCacheInfoPath() const {
Zonr Chang6692ab52012-01-17 17:29:13 +080083 return getCachedObjectPath().append(".info");
Zonr Chang4ea08862012-01-17 17:26:49 +080084 }
Loganecf4cbd2011-01-06 05:34:11 +080085
Logan42598052011-01-26 22:41:13 +080086 bool mIsContextSlotNotAvail;
87
Logan474cbd22011-01-31 01:47:44 +080088 // Source List
89 SourceInfo *mSourceList[2];
90 // Note: mSourceList[0] (main source)
91 // Note: mSourceList[1] (library source)
92 // TODO(logan): Generalize this, use vector or SmallVector instead!
Logan3133c412011-01-06 06:15:40 +080093
Logan Chien7890d432011-08-03 14:55:17 +080094 // External Function List
95 std::vector<char const *> mUserDefinedExternalSymbols;
96
Loganecf4cbd2011-01-06 05:34:11 +080097 // Register Symbol Lookup Function
Logancf3e5212010-12-29 01:44:55 +080098 BCCSymbolLookupFn mpExtSymbolLookupFn;
Loganf340bf72011-01-14 17:51:40 +080099 void *mpExtSymbolLookupFnContext;
Loganeaa0cc32010-12-29 01:04:20 +0800100
Stephen Hinesb67c9e72012-03-22 11:02:48 -0700101 uint32_t mCompilerVersion;
102 uint32_t mOptimizationLevel;
103
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 Hinesb67c9e72012-03-22 11:02:48 -0700108 mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL),
109 mCompilerVersion(0), mOptimizationLevel(3) {
Loganecf4cbd2011-01-06 05:34:11 +0800110 Compiler::GlobalInitialization();
Logan474cbd22011-01-31 01:47:44 +0800111
112 mSourceList[0] = NULL;
113 mSourceList[1] = NULL;
Logan3f3d31f2010-11-27 13:52:03 +0800114 }
115
Logancf3e5212010-12-29 01:44:55 +0800116 ~Script();
Loganeaa0cc32010-12-29 01:04:20 +0800117
Logan474cbd22011-01-31 01:47:44 +0800118 int addSourceBC(size_t idx,
119 char const *resName,
120 const char *bitcode,
121 size_t bitcodeSize,
122 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800123
Logan474cbd22011-01-31 01:47:44 +0800124 int addSourceModule(size_t idx,
125 llvm::Module *module,
126 unsigned long flags);
Loganecf4cbd2011-01-06 05:34:11 +0800127
Logan474cbd22011-01-31 01:47:44 +0800128 int addSourceFile(size_t idx,
129 char const *path,
130 unsigned long flags);
Loganeaa0cc32010-12-29 01:04:20 +0800131
Logan Chien7890d432011-08-03 14:55:17 +0800132 void markExternalSymbol(char const *name) {
133 mUserDefinedExternalSymbols.push_back(name);
134 }
135
136 std::vector<char const *> const &getUserDefinedExternalSymbols() const {
137 return mUserDefinedExternalSymbols;
138 }
139
Logan Chien311c26f2011-07-11 14:30:34 +0800140 int prepareExecutable(char const *cacheDir,
141 char const *cacheName,
142 unsigned long flags);
Shih-wei Liaoa0ed34e2012-03-03 01:33:30 -0800143 int writeCache();
Loganeaa0cc32010-12-29 01:04:20 +0800144
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800145 /*
146 * Link the given bitcodes in mSourceList to shared object (.so).
147 *
148 * Currently, it requires one to provide the relocatable object files with
149 * given bitcodes to output a shared object.
150 *
151 * The usage of this function is flexible. You can have a relocatable object
152 * compiled before and pass it in objPath to generate shared object. If the
153 * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
154 * you haven't done that yet) and then link the output relocatable object
Shih-wei Liao69341742012-03-03 01:45:36 -0800155 * file to .so in dsoPath.
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800156 *
157 * TODO: Currently, we only support to link the bitcodes in mSourceList[0].
158 *
159 */
Shih-wei Liao69341742012-03-03 01:45:36 -0800160 int prepareSharedObject(char const *objPath,
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800161 char const *dsoPath,
162 unsigned long flags);
163
Shih-wei Liaod8ed6a92012-03-03 01:44:29 -0800164 int prepareRelocatable(char const *objPath,
Shih-wei Liao6a60f4e2012-01-17 02:58:40 -0800165 llvm::Reloc::Model RelocModel,
166 unsigned long flags);
Joseph Wen34c600a2011-07-25 17:59:17 -0700167
Logancf3e5212010-12-29 01:44:55 +0800168 char const *getCompilerErrorMessage();
Loganeaa0cc32010-12-29 01:04:20 +0800169
Logancf3e5212010-12-29 01:44:55 +0800170 void *lookup(const char *name);
Loganeaa0cc32010-12-29 01:04:20 +0800171
Stephen Hinesb67c9e72012-03-22 11:02:48 -0700172 uint32_t getCompilerVersion() const {
173 return mCompilerVersion;
174 }
175
176 uint32_t getOptimizationLevel() const {
177 return mOptimizationLevel;
178 }
Loganeaa0cc32010-12-29 01:04:20 +0800179
Loganbe79ada2011-01-13 01:33:45 +0800180 size_t getExportVarCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800181
Loganbe79ada2011-01-13 01:33:45 +0800182 size_t getExportFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800183
Stephen Hinescc366e52012-02-21 17:22:04 -0800184 size_t getExportForEachCount() const;
185
Loganbe79ada2011-01-13 01:33:45 +0800186 size_t getPragmaCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800187
Loganbe79ada2011-01-13 01:33:45 +0800188 size_t getFuncCount() const;
Loganeaa0cc32010-12-29 01:04:20 +0800189
Stephen Hines071288a2011-01-27 14:38:26 -0800190 size_t getObjectSlotCount() const;
Loganbe79ada2011-01-13 01:33:45 +0800191
192 void getExportVarList(size_t size, void **list);
193
194 void getExportFuncList(size_t size, void **list);
195
Stephen Hinescc366e52012-02-21 17:22:04 -0800196 void getExportForEachList(size_t size, void **list);
197
Joseph Wenf36637f2011-07-06 18:27:12 -0700198 void getExportVarNameList(std::vector<std::string> &list);
199
200 void getExportFuncNameList(std::vector<std::string> &list);
201
Stephen Hinescc366e52012-02-21 17:22:04 -0800202 void getExportForEachNameList(std::vector<std::string> &list);
203
Loganbe79ada2011-01-13 01:33:45 +0800204 void getPragmaList(size_t size,
205 char const **keyList,
206 char const **valueList);
207
Loganf340bf72011-01-14 17:51:40 +0800208 void getFuncInfoList(size_t size, FuncInfo *list);
Loganbe79ada2011-01-13 01:33:45 +0800209
Stephen Hines071288a2011-01-27 14:38:26 -0800210 void getObjectSlotList(size_t size, uint32_t *list);
211
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700212 size_t getELFSize() const;
213
214 const char *getELF() const;
Logan02286cb2011-01-07 00:30:47 +0800215
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800216 int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
Loganeaa0cc32010-12-29 01:04:20 +0800217
Shih-wei Liaoabc7f512012-01-18 00:34:07 -0800218 bool isCacheable() const;
Loganeaa0cc32010-12-29 01:04:20 +0800219
Loganf340bf72011-01-14 17:51:40 +0800220 void setError(int error) {
Logan39736412010-12-29 00:24:04 +0800221 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
222 mErrorCode = error;
Logan3f3d31f2010-11-27 13:52:03 +0800223 }
224 }
225
Loganf340bf72011-01-14 17:51:40 +0800226 int getError() {
227 int result = mErrorCode;
Logan39736412010-12-29 00:24:04 +0800228 mErrorCode = BCC_NO_ERROR;
Logan3f3d31f2010-11-27 13:52:03 +0800229 return result;
230 }
Logan033f46e2011-01-06 05:51:24 +0800231
232 private:
Zonr Chang743dd712012-01-19 10:13:52 +0800233 //
234 // It returns 0 if there's a cache hit.
235 //
Shih-wei Liao8454a3a2012-03-03 01:50:08 -0800236 // Side effect: it will set mCacheDir, mCacheName.
Zonr Chang743dd712012-01-19 10:13:52 +0800237 int internalLoadCache(char const *cacheDir, char const *cacheName,
Shih-wei Liao8454a3a2012-03-03 01:50:08 -0800238 bool checkOnly);
Stephen Hines0e567862012-03-11 20:26:40 -0700239
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800240 int internalCompile(const CompilerOption&);
Logan3f3d31f2010-11-27 13:52:03 +0800241 };
242
243} // namespace bcc
244
245#endif // BCC_SCRIPT_H