blob: 51e6d8a7e9e21d6a1cd14dc93f028c3706ff28db [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) {
145 if (mStatus != ScriptStatus::Compiled) {
146 mErrorCode = BCC_INVALID_OPERATION;
147 return 1;
148 }
149
150 return mCompiled->linkBC(bitcode, bitcodeSize);
151}
152
153
Logancf3e5212010-12-29 01:44:55 +0800154int Script::compile() {
Loganecf4cbd2011-01-06 05:34:11 +0800155 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800156 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800157 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800158 return 1;
159 }
160
Loganecf4cbd2011-01-06 05:34:11 +0800161 // Load Cache File
Logan033f46e2011-01-06 05:51:24 +0800162 if (cacheFile && internalLoadCache() == 0) {
163 return 0;
164 }
Loganecf4cbd2011-01-06 05:34:11 +0800165
Logan033f46e2011-01-06 05:51:24 +0800166 return internalCompile();
167}
168
169
170int Script::internalLoadCache() {
171 if (getBooleanProp("debug.bcc.nocache")) {
172 // Android system environment property disable the cache mechanism by
173 // setting "debug.bcc.nocache". So we will not load the cache file any
174 // way.
175 return 1;
176 }
177
Logan04329712011-01-06 06:10:57 +0800178 if (!cacheFile) {
179 // The application developer has not specify resName or cacheDir, so
180 // we don't know where to open the cache file.
181 return 1;
Logan033f46e2011-01-06 05:51:24 +0800182 }
Logan04329712011-01-06 06:10:57 +0800183
184 FileHandle file;
185
186 if (file.open(cacheFile, OpenMode::READ) < 0) {
187 // Unable to open the cache file in read mode.
188 return 1;
189 }
190
191#if 0
192 CacheReader reader;
193
194 ScriptCached *cached = reader.readCacheFile(&file);
195 if (!cached) {
196 return 1;
197 }
198
199 mCached = cached;
200 mStatus = ScriptStatus::Cached;
Logan033f46e2011-01-06 05:51:24 +0800201#endif
Logan033f46e2011-01-06 05:51:24 +0800202
203 return 1;
204}
205
206
207int Script::internalCompile() {
208 // Create the ScriptCompiled object
Loganecf4cbd2011-01-06 05:34:11 +0800209 mCompiled = new (nothrow) ScriptCompiled(this);
210
211 if (!mCompiled) {
212 mErrorCode = BCC_OUT_OF_MEMORY;
213 LOGE("Out of memory: %s %d\n", __FILE__, __LINE__);
214 return 1;
215 }
216
217 mStatus = ScriptStatus::Compiled;
218
Logan033f46e2011-01-06 05:51:24 +0800219 // Register symbol lookup function
Loganecf4cbd2011-01-06 05:34:11 +0800220 if (mpExtSymbolLookupFn) {
221 mCompiled->registerSymbolCallback(mpExtSymbolLookupFn,
222 mpExtSymbolLookupFnContext);
223 }
224
Logan033f46e2011-01-06 05:51:24 +0800225 // Setup the source bitcode / module
Loganecf4cbd2011-01-06 05:34:11 +0800226 if (sourceBC) {
Loganf30a6712011-01-06 05:55:34 +0800227 if (mCompiled->readBC(sourceBC, sourceSize, sourceResName, 0) != 0) {
Logan033f46e2011-01-06 05:51:24 +0800228 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800229 }
230 } else if (sourceModule) {
Logan033f46e2011-01-06 05:51:24 +0800231 if (mCompiled->readModule(sourceModule) != 0) {
232 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800233 }
234 }
235
Logan033f46e2011-01-06 05:51:24 +0800236 // TODO(logan): Link source with the library
Logan04329712011-01-06 06:10:57 +0800237#if 0
238 if (libraryBC) {
239 if (mCompiled->linkBC(libraryBC, librarySize) != 0) {
240 return 1;
241 }
242 }
243#endif
Loganecf4cbd2011-01-06 05:34:11 +0800244
Logan04329712011-01-06 06:10:57 +0800245 if (mCompiled->compile() != 0) {
246 return 1;
247 }
248
249 // TODO(logan): Write the cache out
250#if 0
251 if (cacheFile && !getBooleanProp("debug.bcc.nocache")) {
252 FileHandler file;
253
254 if (file.open(cacheFile, OpenMode::WRITE) >= 0) {
255 CacheWriter writer;
256 writer.writeCacheFile(&file);
257 }
258 }
259#endif
260
261 return 0;
Logancf3e5212010-12-29 01:44:55 +0800262}
263
264
265char const *Script::getCompilerErrorMessage() {
266 if (mStatus != ScriptStatus::Compiled) {
267 mErrorCode = BCC_INVALID_OPERATION;
268 return NULL;
269 }
270
271 return mCompiled->getCompilerErrorMessage();
272}
273
274
275void *Script::lookup(const char *name) {
276 if (mStatus != ScriptStatus::Compiled) {
277 mErrorCode = BCC_INVALID_OPERATION;
278 return NULL;
279 }
280
281 return mCompiled->lookup(name);
282}
283
284
285void Script::getExportVars(BCCsizei *actualVarCount,
286 BCCsizei maxVarCount,
287 BCCvoid **vars) {
288 if (mStatus != ScriptStatus::Compiled) {
289 mErrorCode = BCC_INVALID_OPERATION;
290 return;
291 }
292
293 mCompiled->getExportVars(actualVarCount, maxVarCount, vars);
294}
295
296
297void Script::getExportFuncs(BCCsizei *actualFuncCount,
298 BCCsizei maxFuncCount,
299 BCCvoid **funcs) {
300 if (mStatus != ScriptStatus::Compiled) {
301 mErrorCode = BCC_INVALID_OPERATION;
302 return;
303 }
304
305 mCompiled->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
306}
307
308
309void Script::getPragmas(BCCsizei *actualStringCount,
310 BCCsizei maxStringCount,
311 BCCchar **strings) {
312 if (mStatus != ScriptStatus::Compiled) {
313 mErrorCode = BCC_INVALID_OPERATION;
314 return;
315 }
316
317 mCompiled->getPragmas(actualStringCount, maxStringCount, strings);
318}
319
320
321void Script::getFunctions(BCCsizei *actualFunctionCount,
322 BCCsizei maxFunctionCount,
323 BCCchar **functions) {
324 if (mStatus != ScriptStatus::Compiled) {
325 mErrorCode = BCC_INVALID_OPERATION;
326 return;
327 }
328
329 mCompiled->getFunctions(actualFunctionCount, maxFunctionCount, functions);
330}
331
332
333void Script::getFunctionBinary(BCCchar *function,
334 BCCvoid **base,
335 BCCsizei *length) {
336 if (mStatus != ScriptStatus::Compiled) {
337 mErrorCode = BCC_INVALID_OPERATION;
338 return;
339 }
340
341 mCompiled->getFunctionBinary(function, base, length);
342}
343
344
345void Script::registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
346 mpExtSymbolLookupFn = pFn;
347 mpExtSymbolLookupFnContext = pContext;
348
349 if (mStatus != ScriptStatus::Compiled) {
350 mErrorCode = BCC_INVALID_OPERATION;
351 return;
352 }
353
354 mCompiled->registerSymbolCallback(pFn, pContext);
355}
356
357} // namespace bcc