blob: b654797bb9c9d1f6bdc6d66e3e3d2d31bb6add5b [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
20#include <vector>
21#include <string>
22
23#include <stdint.h>
24#include <stddef.h>
25
26#include <llvm/ADT/SmallVector.h>
27
28#include <bcc/bcc.h>
29#include <bcc/bcc_mccache.h>
30#include "bcc_internal.h"
31
32#include "Compiler.h"
33
34namespace llvm {
35 class Module;
36 class GDBJITRegistrar;
37}
Loganeaa0cc32010-12-29 01:04:20 +080038
Logan3f3d31f2010-11-27 13:52:03 +080039namespace bcc {
Stephen Hines97c92c22012-05-03 12:30:24 -070040 class ScriptCompiled;
41 class ScriptCached;
42 class Source;
43 struct CompilerOption;
Logancf3e5212010-12-29 01:44:55 +080044
Stephen Hines97c92c22012-05-03 12:30:24 -070045 namespace ScriptStatus {
46 enum StatusType {
47 Unknown,
48 Compiled,
49 Cached
50 };
51 }
Logan3f3d31f2010-11-27 13:52:03 +080052
Stephen Hines97c92c22012-05-03 12:30:24 -070053 namespace ScriptObject {
54 enum ObjectType {
55 Unknown,
56 Relocatable,
57 SharedObject,
58 Executable,
59 };
60 }
Zonr Chang4ea08862012-01-17 17:26:49 +080061
Stephen Hines97c92c22012-05-03 12:30:24 -070062 class Script {
63 private:
64 int mErrorCode;
Logan39736412010-12-29 00:24:04 +080065
Stephen Hines97c92c22012-05-03 12:30:24 -070066 ScriptStatus::StatusType mStatus;
67 // 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.
70 ScriptObject::ObjectType mObjectType;
Logancf3e5212010-12-29 01:44:55 +080071
Stephen Hines97c92c22012-05-03 12:30:24 -070072 union {
73 ScriptCompiled *mCompiled;
74 ScriptCached *mCached;
75 };
Logancf3e5212010-12-29 01:44:55 +080076
Stephen Hines97c92c22012-05-03 12:30:24 -070077 std::string mCacheDir;
78 std::string mCacheName;
Zonr Chang4ea08862012-01-17 17:26:49 +080079
Stephen Hines97c92c22012-05-03 12:30:24 -070080 inline std::string getCachedObjectPath() const {
81 return std::string(mCacheDir + mCacheName + ".o");
82 }
Zonr Chang4ea08862012-01-17 17:26:49 +080083
Stephen Hines97c92c22012-05-03 12:30:24 -070084 inline std::string getCacheInfoPath() const {
85 return getCachedObjectPath().append(".info");
86 }
Loganecf4cbd2011-01-06 05:34:11 +080087
Stephen Hines97c92c22012-05-03 12:30:24 -070088 bool mIsContextSlotNotAvail;
Logan42598052011-01-26 22:41:13 +080089
Stephen Hines97c92c22012-05-03 12:30:24 -070090 // This is the source associated with this object and is going to be
91 // compiled.
92 Source *mSource;
93
94 class DependencyInfo {
95 private:
96 MCO_ResourceType mSourceType;
97 std::string mSourceName;
98 uint8_t mSHA1[20];
99
100 public:
101 DependencyInfo(MCO_ResourceType pSourceType,
102 const std::string &pSourceName,
103 const uint8_t *pSHA1);
104
105 inline MCO_ResourceType getSourceType() const
106 { return mSourceType; }
107
108 inline const std::string getSourceName() const
109 { return mSourceName; }
110
111 inline const uint8_t *getSHA1Checksum() const
112 { return mSHA1; }
113 };
114 llvm::SmallVector<DependencyInfo *, 2> mDependencyInfos;
115
116 // External Function List
117 std::vector<char const *> mUserDefinedExternalSymbols;
118
119 // Register Symbol Lookup Function
120 BCCSymbolLookupFn mpExtSymbolLookupFn;
121 void *mpExtSymbolLookupFnContext;
122
123 // Reset the state of this script object
124 void resetState();
125
126 public:
127 Script(Source &pSource);
128
129 ~Script();
130
131 // Reset this object with the new source supplied. Return false if this
132 // object remains unchanged after the call (e.g., the supplied source is
133 // the same with the one contain in this object.) If pPreserveCurrent is
134 // false, the current containing source will be destroyed after successfully
135 // reset.
136 bool reset(Source &pSource, bool pPreserveCurrent = false);
137
138 // Merge (or link) another source into the current source associated with
139 // this Script object. Return false on error.
140 bool mergeSource(Source &pSource, bool pPreserveSource = false);
141
142 // Add dependency information for this script given the source named
143 // pSourceName. pSHA1 is the SHA-1 checksum of the given source. Return
144 // false on error.
145 bool addSourceDependencyInfo(MCO_ResourceType pSourceType,
146 const std::string &pSourceName,
147 const uint8_t *pSHA1);
148
149 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
157 int prepareExecutable(char const *cacheDir,
158 char const *cacheName,
159 unsigned long flags);
160 int writeCache();
161
162 /*
163 * Link the given bitcodes in mSource to shared object (.so).
164 *
165 * Currently, it requires one to provide the relocatable object files with
166 * given bitcodes to output a shared object.
167 *
168 * The usage of this function is flexible. You can have a relocatable object
169 * compiled before and pass it in objPath to generate shared object. If the
170 * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
171 * you haven't done that yet) and then link the output relocatable object
172 * file to .so in dsoPath.
173 *
174 * TODO: Currently, we only support to link a bitcode (i.e., mSource.)
175 *
176 */
177 int prepareSharedObject(char const *objPath,
178 char const *dsoPath,
179 unsigned long flags);
180
181 int prepareRelocatable(char const *objPath,
182 llvm::Reloc::Model RelocModel,
183 unsigned long flags);
184
185 char const *getCompilerErrorMessage();
186
187 void *lookup(const char *name);
188
189 size_t getExportVarCount() const;
190
191 size_t getExportFuncCount() const;
192
193 size_t getExportForEachCount() const;
194
195 size_t getPragmaCount() const;
196
197 size_t getFuncCount() const;
198
199 size_t getObjectSlotCount() const;
200
201 void getExportVarList(size_t size, void **list);
202
203 void getExportFuncList(size_t size, void **list);
204
205 void getExportForEachList(size_t size, void **list);
206
207 void getExportVarNameList(std::vector<std::string> &list);
208
209 void getExportFuncNameList(std::vector<std::string> &list);
210
211 void getExportForEachNameList(std::vector<std::string> &list);
212
213 void getPragmaList(size_t size,
214 char const **keyList,
215 char const **valueList);
216
217 void getFuncInfoList(size_t size, FuncInfo *list);
218
219 void getObjectSlotList(size_t size, uint32_t *list);
220
221 size_t getELFSize() const;
222
223 const char *getELF() const;
224
225 int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
226
227 bool isCacheable() const;
228
229 void setError(int error) {
230 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
231 mErrorCode = error;
232 }
233 }
234
235 int getError() {
236 int result = mErrorCode;
237 mErrorCode = BCC_NO_ERROR;
238 return result;
239 }
240
241 private:
242 //
243 // It returns 0 if there's a cache hit.
244 //
245 // Side effect: it will set mCacheDir, mCacheName.
246 int internalLoadCache(char const *cacheDir, char const *cacheName,
247 bool checkOnly);
248
249 int internalCompile(const CompilerOption&);
250 };
251
252} // namespace bcc
253
254#endif // BCC_SCRIPT_H