blob: 4534951809bf9ed985c75683bd3fde609b204aac [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
23#include "BCCContext.h"
24#include "Compiler.h"
25
26#include <llvm/Support/CodeGen.h>
27
Stephen Hines97c92c22012-05-03 12:30:24 -070028#include <vector>
29#include <string>
30
Stephen Hines97c92c22012-05-03 12:30:24 -070031#include <stddef.h>
32
Stephen Hines97c92c22012-05-03 12:30:24 -070033namespace llvm {
34 class Module;
35 class GDBJITRegistrar;
36}
Loganeaa0cc32010-12-29 01:04:20 +080037
Logan3f3d31f2010-11-27 13:52:03 +080038namespace bcc {
Stephen Hines97c92c22012-05-03 12:30:24 -070039 class ScriptCompiled;
40 class ScriptCached;
Stephen Hinesead5ccb2012-05-03 12:30:38 -070041 class SourceInfo;
Stephen Hines97c92c22012-05-03 12:30:24 -070042 struct CompilerOption;
Logancf3e5212010-12-29 01:44:55 +080043
Stephen Hines97c92c22012-05-03 12:30:24 -070044 namespace ScriptStatus {
45 enum StatusType {
46 Unknown,
47 Compiled,
48 Cached
49 };
50 }
Logan3f3d31f2010-11-27 13:52:03 +080051
Stephen Hines97c92c22012-05-03 12:30:24 -070052 namespace ScriptObject {
53 enum ObjectType {
54 Unknown,
55 Relocatable,
56 SharedObject,
57 Executable,
58 };
59 }
Zonr Chang4ea08862012-01-17 17:26:49 +080060
Stephen Hines97c92c22012-05-03 12:30:24 -070061 class Script {
62 private:
Stephen Hinesead5ccb2012-05-03 12:30:38 -070063 BCCContext mContext;
64
Stephen Hines97c92c22012-05-03 12:30:24 -070065 int mErrorCode;
Logan39736412010-12-29 00:24:04 +080066
Stephen Hines97c92c22012-05-03 12:30:24 -070067 ScriptStatus::StatusType mStatus;
68 // The type of the object behind this script after compilation. For
69 // example, after returning from a successful call to prepareRelocatable(),
70 // the value of mObjectType will be ScriptObject::Relocatable.
71 ScriptObject::ObjectType mObjectType;
Logancf3e5212010-12-29 01:44:55 +080072
Stephen Hines97c92c22012-05-03 12:30:24 -070073 union {
74 ScriptCompiled *mCompiled;
75 ScriptCached *mCached;
76 };
Logancf3e5212010-12-29 01:44:55 +080077
Stephen Hines97c92c22012-05-03 12:30:24 -070078 std::string mCacheDir;
79 std::string mCacheName;
Zonr Chang4ea08862012-01-17 17:26:49 +080080
Stephen Hines97c92c22012-05-03 12:30:24 -070081 inline std::string getCachedObjectPath() const {
82 return std::string(mCacheDir + mCacheName + ".o");
83 }
Zonr Chang4ea08862012-01-17 17:26:49 +080084
Stephen Hines97c92c22012-05-03 12:30:24 -070085 inline std::string getCacheInfoPath() const {
86 return getCachedObjectPath().append(".info");
87 }
Loganecf4cbd2011-01-06 05:34:11 +080088
Stephen Hines97c92c22012-05-03 12:30:24 -070089 bool mIsContextSlotNotAvail;
Logan42598052011-01-26 22:41:13 +080090
Stephen Hinesead5ccb2012-05-03 12:30:38 -070091 // Source List
92 SourceInfo *mSourceList[2];
93 // Note: mSourceList[0] (main source)
94 // Note: mSourceList[1] (library source)
95 // TODO(logan): Generalize this, use vector or SmallVector instead!
Stephen Hines97c92c22012-05-03 12:30:24 -070096
97 // External Function List
98 std::vector<char const *> mUserDefinedExternalSymbols;
99
100 // Register Symbol Lookup Function
101 BCCSymbolLookupFn mpExtSymbolLookupFn;
102 void *mpExtSymbolLookupFnContext;
103
Stephen Hines97c92c22012-05-03 12:30:24 -0700104 public:
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700105 Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
106 mObjectType(ScriptObject::Unknown),
107 mIsContextSlotNotAvail(false),
108 mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
109 Compiler::GlobalInitialization();
110
111 mSourceList[0] = NULL;
112 mSourceList[1] = NULL;
113 }
Stephen Hines97c92c22012-05-03 12:30:24 -0700114
115 ~Script();
116
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700117 int addSourceBC(size_t idx,
118 char const *resName,
119 const char *bitcode,
120 size_t bitcodeSize,
121 unsigned long flags);
Stephen Hines97c92c22012-05-03 12:30:24 -0700122
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700123 int addSourceModule(size_t idx,
124 llvm::Module *module,
125 unsigned long flags);
Stephen Hines97c92c22012-05-03 12:30:24 -0700126
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700127 int addSourceFile(size_t idx,
128 char const *path,
129 unsigned long flags);
Stephen Hines97c92c22012-05-03 12:30:24 -0700130
131 void markExternalSymbol(char const *name) {
132 mUserDefinedExternalSymbols.push_back(name);
133 }
134
135 std::vector<char const *> const &getUserDefinedExternalSymbols() const {
136 return mUserDefinedExternalSymbols;
137 }
138
139 int prepareExecutable(char const *cacheDir,
140 char const *cacheName,
141 unsigned long flags);
142 int writeCache();
143
144 /*
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700145 * Link the given bitcodes in mSourceList to shared object (.so).
Stephen Hines97c92c22012-05-03 12:30:24 -0700146 *
147 * Currently, it requires one to provide the relocatable object files with
148 * given bitcodes to output a shared object.
149 *
150 * The usage of this function is flexible. You can have a relocatable object
151 * compiled before and pass it in objPath to generate shared object. If the
152 * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
153 * you haven't done that yet) and then link the output relocatable object
154 * file to .so in dsoPath.
155 *
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700156 * TODO: Currently, we only support to link the bitcodes in mSourceList[0].
Stephen Hines97c92c22012-05-03 12:30:24 -0700157 *
158 */
159 int prepareSharedObject(char const *objPath,
160 char const *dsoPath,
161 unsigned long flags);
162
163 int prepareRelocatable(char const *objPath,
164 llvm::Reloc::Model RelocModel,
165 unsigned long flags);
166
167 char const *getCompilerErrorMessage();
168
169 void *lookup(const char *name);
170
171 size_t getExportVarCount() const;
172
173 size_t getExportFuncCount() const;
174
175 size_t getExportForEachCount() const;
176
177 size_t getPragmaCount() const;
178
179 size_t getFuncCount() const;
180
181 size_t getObjectSlotCount() const;
182
183 void getExportVarList(size_t size, void **list);
184
185 void getExportFuncList(size_t size, void **list);
186
187 void getExportForEachList(size_t size, void **list);
188
189 void getExportVarNameList(std::vector<std::string> &list);
190
191 void getExportFuncNameList(std::vector<std::string> &list);
192
193 void getExportForEachNameList(std::vector<std::string> &list);
194
195 void getPragmaList(size_t size,
196 char const **keyList,
197 char const **valueList);
198
199 void getFuncInfoList(size_t size, FuncInfo *list);
200
201 void getObjectSlotList(size_t size, uint32_t *list);
202
203 size_t getELFSize() const;
204
205 const char *getELF() const;
206
207 int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
208
209 bool isCacheable() const;
210
211 void setError(int error) {
212 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
213 mErrorCode = error;
214 }
215 }
216
217 int getError() {
218 int result = mErrorCode;
219 mErrorCode = BCC_NO_ERROR;
220 return result;
221 }
222
223 private:
224 //
225 // It returns 0 if there's a cache hit.
226 //
227 // Side effect: it will set mCacheDir, mCacheName.
228 int internalLoadCache(char const *cacheDir, char const *cacheName,
229 bool checkOnly);
230
231 int internalCompile(const CompilerOption&);
232 };
233
234} // namespace bcc
235
236#endif // BCC_SCRIPT_H