blob: 42f7a04b03a5ae225a95ff87cd2131da3afd1341 [file] [log] [blame]
Logancf3e5212010-12-29 01:44:55 +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#define LOG_TAG "bcc"
18#include <cutils/log.h>
19
20#include "Script.h"
21
Logan04329712011-01-06 06:10:57 +080022//#include "CacheReader.h"
23//#include "CacheWriter.h"
24#include "FileHandle.h"
Logancf3e5212010-12-29 01:44:55 +080025#include "ScriptCompiled.h"
Logan04329712011-01-06 06:10:57 +080026//#include "ScriptCached.h"
Logancf3e5212010-12-29 01:44:55 +080027
28#include <new>
29
Logan033f46e2011-01-06 05:51:24 +080030#include <cutils/properties.h>
31
Loganecf4cbd2011-01-06 05:34:11 +080032namespace {
33
34// Input: cacheDir
35// Input: resName
36// Input: extName
37//
38// Note: cacheFile = resName + extName
39//
40// Output: Returns cachePath == cacheDir + cacheFile
41char *genCacheFileName(const char *cacheDir,
42 const char *resName,
43 const char *extName) {
44 char cachePath[512];
45 char cacheFile[sizeof(cachePath)];
46 const size_t kBufLen = sizeof(cachePath) - 1;
47
48 cacheFile[0] = '\0';
49 // Note: resName today is usually something like
50 // "/com.android.fountain:raw/fountain"
51 if (resName[0] != '/') {
52 // Get the absolute path of the raw/***.bc file.
53
54 // Generate the absolute path. This doesn't do everything it
55 // should, e.g. if resName is "./out/whatever" it doesn't crunch
56 // the leading "./" out because this if-block is not triggered,
57 // but it'll make do.
58 //
59 if (getcwd(cacheFile, kBufLen) == NULL) {
60 LOGE("Can't get CWD while opening raw/***.bc file\n");
61 return NULL;
62 }
63 // Append "/" at the end of cacheFile so far.
64 strncat(cacheFile, "/", kBufLen);
65 }
66
67 // cacheFile = resName + extName
68 //
69 strncat(cacheFile, resName, kBufLen);
70 if (extName != NULL) {
71 // TODO(srhines): strncat() is a bit dangerous
72 strncat(cacheFile, extName, kBufLen);
73 }
74
75 // Turn the path into a flat filename by replacing
76 // any slashes after the first one with '@' characters.
77 char *cp = cacheFile + 1;
78 while (*cp != '\0') {
79 if (*cp == '/') {
80 *cp = '@';
81 }
82 cp++;
83 }
84
85 // Tack on the file name for the actual cache file path.
86 strncpy(cachePath, cacheDir, kBufLen);
87 strncat(cachePath, cacheFile, kBufLen);
88
89 LOGV("Cache file for '%s' '%s' is '%s'\n", resName, extName, cachePath);
90 return strdup(cachePath);
91}
92
Logan033f46e2011-01-06 05:51:24 +080093bool getBooleanProp(const char *str) {
94 char buf[PROPERTY_VALUE_MAX];
95 property_get(str, buf, "0");
96 return strcmp(buf, "0") != 0;
97}
98
Loganecf4cbd2011-01-06 05:34:11 +080099} // namespace anonymous
100
Logancf3e5212010-12-29 01:44:55 +0800101namespace bcc {
102
103Script::~Script() {
104 if (mStatus == ScriptStatus::Compiled) {
105 delete mCompiled;
106 }
107}
108
109
110int Script::readBC(const char *bitcode,
111 size_t bitcodeSize,
Logancf3e5212010-12-29 01:44:55 +0800112 const BCCchar *resName,
113 const BCCchar *cacheDir) {
Loganecf4cbd2011-01-06 05:34:11 +0800114 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800115 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800116 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800117 return 1;
118 }
119
Loganecf4cbd2011-01-06 05:34:11 +0800120 sourceBC = bitcode;
121 sourceResName = resName;
122 sourceSize = bitcodeSize;
123
Logan033f46e2011-01-06 05:51:24 +0800124 if (cacheDir && resName) {
125 cacheFile = genCacheFileName(cacheDir, resName, ".oBCC");
126 }
127
Loganecf4cbd2011-01-06 05:34:11 +0800128 return 0;
Logancf3e5212010-12-29 01:44:55 +0800129}
130
131
132int Script::readModule(llvm::Module *module) {
133 if (mStatus != ScriptStatus::Unknown) {
134 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800135 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800136 return 1;
137 }
138
Loganecf4cbd2011-01-06 05:34:11 +0800139 sourceModule = module;
140 return 0;
Logancf3e5212010-12-29 01:44:55 +0800141}
142
143
144int Script::linkBC(const char *bitcode, size_t bitcodeSize) {
Logan3133c412011-01-06 06:15:40 +0800145 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800146 mErrorCode = BCC_INVALID_OPERATION;
Logan3133c412011-01-06 06:15:40 +0800147 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800148 return 1;
149 }
150
Logan3133c412011-01-06 06:15:40 +0800151 libraryBC = bitcode;
152 librarySize = bitcodeSize;
153 return 0;
Logancf3e5212010-12-29 01:44:55 +0800154}
155
156
Logancf3e5212010-12-29 01:44:55 +0800157int Script::compile() {
Loganecf4cbd2011-01-06 05:34:11 +0800158 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800159 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800160 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800161 return 1;
162 }
163
Loganecf4cbd2011-01-06 05:34:11 +0800164 // Load Cache File
Logan033f46e2011-01-06 05:51:24 +0800165 if (cacheFile && internalLoadCache() == 0) {
166 return 0;
167 }
Loganecf4cbd2011-01-06 05:34:11 +0800168
Logan033f46e2011-01-06 05:51:24 +0800169 return internalCompile();
170}
171
172
173int Script::internalLoadCache() {
174 if (getBooleanProp("debug.bcc.nocache")) {
175 // Android system environment property disable the cache mechanism by
176 // setting "debug.bcc.nocache". So we will not load the cache file any
177 // way.
178 return 1;
179 }
180
Logan04329712011-01-06 06:10:57 +0800181 if (!cacheFile) {
182 // The application developer has not specify resName or cacheDir, so
183 // we don't know where to open the cache file.
184 return 1;
Logan033f46e2011-01-06 05:51:24 +0800185 }
Logan04329712011-01-06 06:10:57 +0800186
187 FileHandle file;
188
189 if (file.open(cacheFile, OpenMode::READ) < 0) {
190 // Unable to open the cache file in read mode.
191 return 1;
192 }
193
194#if 0
195 CacheReader reader;
196
197 ScriptCached *cached = reader.readCacheFile(&file);
198 if (!cached) {
199 return 1;
200 }
201
202 mCached = cached;
203 mStatus = ScriptStatus::Cached;
Logan033f46e2011-01-06 05:51:24 +0800204#endif
Logan033f46e2011-01-06 05:51:24 +0800205
206 return 1;
207}
208
209
210int Script::internalCompile() {
211 // Create the ScriptCompiled object
Loganecf4cbd2011-01-06 05:34:11 +0800212 mCompiled = new (nothrow) ScriptCompiled(this);
213
214 if (!mCompiled) {
215 mErrorCode = BCC_OUT_OF_MEMORY;
216 LOGE("Out of memory: %s %d\n", __FILE__, __LINE__);
217 return 1;
218 }
219
220 mStatus = ScriptStatus::Compiled;
221
Logan033f46e2011-01-06 05:51:24 +0800222 // Register symbol lookup function
Loganecf4cbd2011-01-06 05:34:11 +0800223 if (mpExtSymbolLookupFn) {
224 mCompiled->registerSymbolCallback(mpExtSymbolLookupFn,
225 mpExtSymbolLookupFnContext);
226 }
227
Logan033f46e2011-01-06 05:51:24 +0800228 // Setup the source bitcode / module
Loganecf4cbd2011-01-06 05:34:11 +0800229 if (sourceBC) {
Loganf30a6712011-01-06 05:55:34 +0800230 if (mCompiled->readBC(sourceBC, sourceSize, sourceResName, 0) != 0) {
Logan033f46e2011-01-06 05:51:24 +0800231 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800232 }
233 } else if (sourceModule) {
Logan033f46e2011-01-06 05:51:24 +0800234 if (mCompiled->readModule(sourceModule) != 0) {
235 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800236 }
237 }
238
Logan3133c412011-01-06 06:15:40 +0800239 // Link the source module with the library module
Logan04329712011-01-06 06:10:57 +0800240 if (libraryBC) {
241 if (mCompiled->linkBC(libraryBC, librarySize) != 0) {
242 return 1;
243 }
244 }
Loganecf4cbd2011-01-06 05:34:11 +0800245
Logan3133c412011-01-06 06:15:40 +0800246 // Compile and JIT the code
Logan04329712011-01-06 06:10:57 +0800247 if (mCompiled->compile() != 0) {
248 return 1;
249 }
250
251 // TODO(logan): Write the cache out
252#if 0
253 if (cacheFile && !getBooleanProp("debug.bcc.nocache")) {
254 FileHandler file;
255
256 if (file.open(cacheFile, OpenMode::WRITE) >= 0) {
257 CacheWriter writer;
258 writer.writeCacheFile(&file);
259 }
260 }
261#endif
262
263 return 0;
Logancf3e5212010-12-29 01:44:55 +0800264}
265
266
267char const *Script::getCompilerErrorMessage() {
268 if (mStatus != ScriptStatus::Compiled) {
269 mErrorCode = BCC_INVALID_OPERATION;
270 return NULL;
271 }
272
273 return mCompiled->getCompilerErrorMessage();
274}
275
276
277void *Script::lookup(const char *name) {
278 if (mStatus != ScriptStatus::Compiled) {
279 mErrorCode = BCC_INVALID_OPERATION;
280 return NULL;
281 }
282
283 return mCompiled->lookup(name);
284}
285
286
287void Script::getExportVars(BCCsizei *actualVarCount,
288 BCCsizei maxVarCount,
289 BCCvoid **vars) {
290 if (mStatus != ScriptStatus::Compiled) {
291 mErrorCode = BCC_INVALID_OPERATION;
292 return;
293 }
294
295 mCompiled->getExportVars(actualVarCount, maxVarCount, vars);
296}
297
298
299void Script::getExportFuncs(BCCsizei *actualFuncCount,
300 BCCsizei maxFuncCount,
301 BCCvoid **funcs) {
302 if (mStatus != ScriptStatus::Compiled) {
303 mErrorCode = BCC_INVALID_OPERATION;
304 return;
305 }
306
307 mCompiled->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
308}
309
310
311void Script::getPragmas(BCCsizei *actualStringCount,
312 BCCsizei maxStringCount,
313 BCCchar **strings) {
314 if (mStatus != ScriptStatus::Compiled) {
315 mErrorCode = BCC_INVALID_OPERATION;
316 return;
317 }
318
319 mCompiled->getPragmas(actualStringCount, maxStringCount, strings);
320}
321
322
323void Script::getFunctions(BCCsizei *actualFunctionCount,
324 BCCsizei maxFunctionCount,
325 BCCchar **functions) {
326 if (mStatus != ScriptStatus::Compiled) {
327 mErrorCode = BCC_INVALID_OPERATION;
328 return;
329 }
330
331 mCompiled->getFunctions(actualFunctionCount, maxFunctionCount, functions);
332}
333
334
335void Script::getFunctionBinary(BCCchar *function,
336 BCCvoid **base,
337 BCCsizei *length) {
338 if (mStatus != ScriptStatus::Compiled) {
339 mErrorCode = BCC_INVALID_OPERATION;
340 return;
341 }
342
343 mCompiled->getFunctionBinary(function, base, length);
344}
345
346
347void Script::registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
348 mpExtSymbolLookupFn = pFn;
349 mpExtSymbolLookupFnContext = pContext;
350
351 if (mStatus != ScriptStatus::Compiled) {
352 mErrorCode = BCC_INVALID_OPERATION;
353 return;
354 }
355
356 mCompiled->registerSymbolCallback(pFn, pContext);
357}
358
359} // namespace bcc