blob: e95cf8a823c629e5f497ea6e4c29bf3a187d8f3c [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
Stephen Hines97c92c22012-05-03 12:30:24 -070017#ifndef BCC_SCRIPT_H
18#define BCC_SCRIPT_H
19
Stephen Hinesead5ccb2012-05-03 12:30:38 -070020#include <bcc/bcc.h>
21#include "bcc_internal.h"
22
Stephen Hinesead5ccb2012-05-03 12:30:38 -070023#include "Compiler.h"
24
25#include <llvm/Support/CodeGen.h>
26
Stephen Hines97c92c22012-05-03 12:30:24 -070027#include <vector>
28#include <string>
29
Stephen Hines97c92c22012-05-03 12:30:24 -070030#include <stddef.h>
31
Stephen Hines97c92c22012-05-03 12:30:24 -070032namespace llvm {
33 class Module;
34 class GDBJITRegistrar;
35}
Loganeaa0cc32010-12-29 01:04:20 +080036
Logan3f3d31f2010-11-27 13:52:03 +080037namespace bcc {
Stephen Hines97c92c22012-05-03 12:30:24 -070038 class ScriptCompiled;
39 class ScriptCached;
Stephen Hinesead5ccb2012-05-03 12:30:38 -070040 class SourceInfo;
Stephen Hines97c92c22012-05-03 12:30:24 -070041 struct CompilerOption;
Logancf3e5212010-12-29 01:44:55 +080042
Stephen Hines97c92c22012-05-03 12:30:24 -070043 namespace ScriptStatus {
44 enum StatusType {
45 Unknown,
46 Compiled,
47 Cached
48 };
49 }
Logan3f3d31f2010-11-27 13:52:03 +080050
Stephen Hines97c92c22012-05-03 12:30:24 -070051 namespace ScriptObject {
52 enum ObjectType {
53 Unknown,
54 Relocatable,
55 SharedObject,
56 Executable,
57 };
58 }
Zonr Chang4ea08862012-01-17 17:26:49 +080059
Stephen Hines97c92c22012-05-03 12:30:24 -070060 class Script {
61 private:
62 int mErrorCode;
Logan39736412010-12-29 00:24:04 +080063
Stephen Hines97c92c22012-05-03 12:30:24 -070064 ScriptStatus::StatusType mStatus;
65 // 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.
68 ScriptObject::ObjectType mObjectType;
Logancf3e5212010-12-29 01:44:55 +080069
Stephen Hines97c92c22012-05-03 12:30:24 -070070 union {
71 ScriptCompiled *mCompiled;
72 ScriptCached *mCached;
73 };
Logancf3e5212010-12-29 01:44:55 +080074
Stephen Hines97c92c22012-05-03 12:30:24 -070075 std::string mCacheDir;
76 std::string mCacheName;
Zonr Chang4ea08862012-01-17 17:26:49 +080077
Stephen Hines97c92c22012-05-03 12:30:24 -070078 inline std::string getCachedObjectPath() const {
79 return std::string(mCacheDir + mCacheName + ".o");
80 }
Zonr Chang4ea08862012-01-17 17:26:49 +080081
Stephen Hines97c92c22012-05-03 12:30:24 -070082 inline std::string getCacheInfoPath() const {
83 return getCachedObjectPath().append(".info");
84 }
Loganecf4cbd2011-01-06 05:34:11 +080085
Stephen Hines97c92c22012-05-03 12:30:24 -070086 bool mIsContextSlotNotAvail;
Logan42598052011-01-26 22:41:13 +080087
Stephen Hinesead5ccb2012-05-03 12:30:38 -070088 // 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!
Stephen Hines97c92c22012-05-03 12:30:24 -070093
94 // External Function List
95 std::vector<char const *> mUserDefinedExternalSymbols;
96
97 // Register Symbol Lookup Function
98 BCCSymbolLookupFn mpExtSymbolLookupFn;
99 void *mpExtSymbolLookupFnContext;
100
Stephen Hines97c92c22012-05-03 12:30:24 -0700101 public:
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700102 Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
103 mObjectType(ScriptObject::Unknown),
104 mIsContextSlotNotAvail(false),
105 mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
106 Compiler::GlobalInitialization();
107
108 mSourceList[0] = NULL;
109 mSourceList[1] = NULL;
110 }
Stephen Hines97c92c22012-05-03 12:30:24 -0700111
112 ~Script();
113
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700114 int addSourceBC(size_t idx,
115 char const *resName,
116 const char *bitcode,
117 size_t bitcodeSize,
118 unsigned long flags);
Stephen Hines97c92c22012-05-03 12:30:24 -0700119
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700120 int addSourceModule(size_t idx,
121 llvm::Module *module,
122 unsigned long flags);
Stephen Hines97c92c22012-05-03 12:30:24 -0700123
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700124 int addSourceFile(size_t idx,
125 char const *path,
126 unsigned long flags);
Stephen Hines97c92c22012-05-03 12:30:24 -0700127
128 void markExternalSymbol(char const *name) {
129 mUserDefinedExternalSymbols.push_back(name);
130 }
131
132 std::vector<char const *> const &getUserDefinedExternalSymbols() const {
133 return mUserDefinedExternalSymbols;
134 }
135
136 int prepareExecutable(char const *cacheDir,
137 char const *cacheName,
138 unsigned long flags);
139 int writeCache();
140
141 /*
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700142 * Link the given bitcodes in mSourceList to shared object (.so).
Stephen Hines97c92c22012-05-03 12:30:24 -0700143 *
144 * Currently, it requires one to provide the relocatable object files with
145 * given bitcodes to output a shared object.
146 *
147 * The usage of this function is flexible. You can have a relocatable object
148 * compiled before and pass it in objPath to generate shared object. If the
149 * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
150 * you haven't done that yet) and then link the output relocatable object
151 * file to .so in dsoPath.
152 *
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700153 * TODO: Currently, we only support to link the bitcodes in mSourceList[0].
Stephen Hines97c92c22012-05-03 12:30:24 -0700154 *
155 */
156 int prepareSharedObject(char const *objPath,
157 char const *dsoPath,
158 unsigned long flags);
159
160 int prepareRelocatable(char const *objPath,
161 llvm::Reloc::Model RelocModel,
162 unsigned long flags);
163
164 char const *getCompilerErrorMessage();
165
166 void *lookup(const char *name);
167
168 size_t getExportVarCount() const;
169
170 size_t getExportFuncCount() const;
171
172 size_t getExportForEachCount() const;
173
174 size_t getPragmaCount() const;
175
176 size_t getFuncCount() const;
177
178 size_t getObjectSlotCount() const;
179
180 void getExportVarList(size_t size, void **list);
181
182 void getExportFuncList(size_t size, void **list);
183
184 void getExportForEachList(size_t size, void **list);
185
186 void getExportVarNameList(std::vector<std::string> &list);
187
188 void getExportFuncNameList(std::vector<std::string> &list);
189
190 void getExportForEachNameList(std::vector<std::string> &list);
191
192 void getPragmaList(size_t size,
193 char const **keyList,
194 char const **valueList);
195
196 void getFuncInfoList(size_t size, FuncInfo *list);
197
198 void getObjectSlotList(size_t size, uint32_t *list);
199
200 size_t getELFSize() const;
201
202 const char *getELF() const;
203
204 int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
205
206 bool isCacheable() const;
207
208 void setError(int error) {
209 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
210 mErrorCode = error;
211 }
212 }
213
214 int getError() {
215 int result = mErrorCode;
216 mErrorCode = BCC_NO_ERROR;
217 return result;
218 }
219
220 private:
221 //
222 // It returns 0 if there's a cache hit.
223 //
224 // Side effect: it will set mCacheDir, mCacheName.
225 int internalLoadCache(char const *cacheDir, char const *cacheName,
226 bool checkOnly);
227
228 int internalCompile(const CompilerOption&);
229 };
230
231} // namespace bcc
232
233#endif // BCC_SCRIPT_H