blob: bf13bce8572ab290df2efe3fe514a9113655bd7c [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
22#include "ScriptCompiled.h"
23
24#include <new>
25
Logan033f46e2011-01-06 05:51:24 +080026#include <cutils/properties.h>
27
Loganecf4cbd2011-01-06 05:34:11 +080028namespace {
29
30// Input: cacheDir
31// Input: resName
32// Input: extName
33//
34// Note: cacheFile = resName + extName
35//
36// Output: Returns cachePath == cacheDir + cacheFile
37char *genCacheFileName(const char *cacheDir,
38 const char *resName,
39 const char *extName) {
40 char cachePath[512];
41 char cacheFile[sizeof(cachePath)];
42 const size_t kBufLen = sizeof(cachePath) - 1;
43
44 cacheFile[0] = '\0';
45 // Note: resName today is usually something like
46 // "/com.android.fountain:raw/fountain"
47 if (resName[0] != '/') {
48 // Get the absolute path of the raw/***.bc file.
49
50 // Generate the absolute path. This doesn't do everything it
51 // should, e.g. if resName is "./out/whatever" it doesn't crunch
52 // the leading "./" out because this if-block is not triggered,
53 // but it'll make do.
54 //
55 if (getcwd(cacheFile, kBufLen) == NULL) {
56 LOGE("Can't get CWD while opening raw/***.bc file\n");
57 return NULL;
58 }
59 // Append "/" at the end of cacheFile so far.
60 strncat(cacheFile, "/", kBufLen);
61 }
62
63 // cacheFile = resName + extName
64 //
65 strncat(cacheFile, resName, kBufLen);
66 if (extName != NULL) {
67 // TODO(srhines): strncat() is a bit dangerous
68 strncat(cacheFile, extName, kBufLen);
69 }
70
71 // Turn the path into a flat filename by replacing
72 // any slashes after the first one with '@' characters.
73 char *cp = cacheFile + 1;
74 while (*cp != '\0') {
75 if (*cp == '/') {
76 *cp = '@';
77 }
78 cp++;
79 }
80
81 // Tack on the file name for the actual cache file path.
82 strncpy(cachePath, cacheDir, kBufLen);
83 strncat(cachePath, cacheFile, kBufLen);
84
85 LOGV("Cache file for '%s' '%s' is '%s'\n", resName, extName, cachePath);
86 return strdup(cachePath);
87}
88
Logan033f46e2011-01-06 05:51:24 +080089bool getBooleanProp(const char *str) {
90 char buf[PROPERTY_VALUE_MAX];
91 property_get(str, buf, "0");
92 return strcmp(buf, "0") != 0;
93}
94
Loganecf4cbd2011-01-06 05:34:11 +080095} // namespace anonymous
96
Logancf3e5212010-12-29 01:44:55 +080097namespace bcc {
98
99Script::~Script() {
100 if (mStatus == ScriptStatus::Compiled) {
101 delete mCompiled;
102 }
103}
104
105
106int Script::readBC(const char *bitcode,
107 size_t bitcodeSize,
108 long bitcodeFileModTime,
109 long bitcodeFileCRC32,
110 const BCCchar *resName,
111 const BCCchar *cacheDir) {
Loganecf4cbd2011-01-06 05:34:11 +0800112 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800113 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800114 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800115 return 1;
116 }
117
Loganecf4cbd2011-01-06 05:34:11 +0800118 sourceBC = bitcode;
119 sourceResName = resName;
120 sourceSize = bitcodeSize;
121
Logan033f46e2011-01-06 05:51:24 +0800122 if (cacheDir && resName) {
123 cacheFile = genCacheFileName(cacheDir, resName, ".oBCC");
124 }
125
Loganecf4cbd2011-01-06 05:34:11 +0800126 return 0;
Logancf3e5212010-12-29 01:44:55 +0800127}
128
129
130int Script::readModule(llvm::Module *module) {
131 if (mStatus != ScriptStatus::Unknown) {
132 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800133 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800134 return 1;
135 }
136
Loganecf4cbd2011-01-06 05:34:11 +0800137 sourceModule = module;
138 return 0;
Logancf3e5212010-12-29 01:44:55 +0800139}
140
141
142int Script::linkBC(const char *bitcode, size_t bitcodeSize) {
143 if (mStatus != ScriptStatus::Compiled) {
144 mErrorCode = BCC_INVALID_OPERATION;
145 return 1;
146 }
147
148 return mCompiled->linkBC(bitcode, bitcodeSize);
149}
150
151
Logancf3e5212010-12-29 01:44:55 +0800152int Script::compile() {
Loganecf4cbd2011-01-06 05:34:11 +0800153 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800154 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800155 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800156 return 1;
157 }
158
Loganecf4cbd2011-01-06 05:34:11 +0800159 // Load Cache File
Logan033f46e2011-01-06 05:51:24 +0800160 if (cacheFile && internalLoadCache() == 0) {
161 return 0;
162 }
Loganecf4cbd2011-01-06 05:34:11 +0800163
Logan033f46e2011-01-06 05:51:24 +0800164 return internalCompile();
165}
166
167
168int Script::internalLoadCache() {
169 if (getBooleanProp("debug.bcc.nocache")) {
170 // Android system environment property disable the cache mechanism by
171 // setting "debug.bcc.nocache". So we will not load the cache file any
172 // way.
173 return 1;
174 }
175
176#if 0
177 if (resName && !mCacheLoadFailed) {
178 mUseCache = true;
179
180 mCacheFd = openCacheFile(resName, cacheDir, true /* createIfMissing */);
181 if (mCacheFd >= 0 && !mCacheNew) { // Just use cache file
182 return -mCacheFd - 1;
183 }
184 }
185#endif
186 // TODO(logan): Implement this.
187
188 return 1;
189}
190
191
192int Script::internalCompile() {
193 // Create the ScriptCompiled object
Loganecf4cbd2011-01-06 05:34:11 +0800194 mCompiled = new (nothrow) ScriptCompiled(this);
195
196 if (!mCompiled) {
197 mErrorCode = BCC_OUT_OF_MEMORY;
198 LOGE("Out of memory: %s %d\n", __FILE__, __LINE__);
199 return 1;
200 }
201
202 mStatus = ScriptStatus::Compiled;
203
Logan033f46e2011-01-06 05:51:24 +0800204 // Register symbol lookup function
Loganecf4cbd2011-01-06 05:34:11 +0800205 if (mpExtSymbolLookupFn) {
206 mCompiled->registerSymbolCallback(mpExtSymbolLookupFn,
207 mpExtSymbolLookupFnContext);
208 }
209
Logan033f46e2011-01-06 05:51:24 +0800210 // Setup the source bitcode / module
Loganecf4cbd2011-01-06 05:34:11 +0800211 if (sourceBC) {
Logan033f46e2011-01-06 05:51:24 +0800212 if (mCompiled->readBC(sourceBC, sourceSize, 0, 0, sourceResName, 0) != 0) {
213 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800214 }
215 } else if (sourceModule) {
Logan033f46e2011-01-06 05:51:24 +0800216 if (mCompiled->readModule(sourceModule) != 0) {
217 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800218 }
219 }
220
Logan033f46e2011-01-06 05:51:24 +0800221 // TODO(logan): Link source with the library
222 //if (libraryBC) {
223 // if (mCompiled->linkBC(libraryBC, librarySize) != 0) {
224 // return 1;
225 // }
226 //}
Loganecf4cbd2011-01-06 05:34:11 +0800227
Logancf3e5212010-12-29 01:44:55 +0800228 return mCompiled->compile();
229}
230
231
232char const *Script::getCompilerErrorMessage() {
233 if (mStatus != ScriptStatus::Compiled) {
234 mErrorCode = BCC_INVALID_OPERATION;
235 return NULL;
236 }
237
238 return mCompiled->getCompilerErrorMessage();
239}
240
241
242void *Script::lookup(const char *name) {
243 if (mStatus != ScriptStatus::Compiled) {
244 mErrorCode = BCC_INVALID_OPERATION;
245 return NULL;
246 }
247
248 return mCompiled->lookup(name);
249}
250
251
252void Script::getExportVars(BCCsizei *actualVarCount,
253 BCCsizei maxVarCount,
254 BCCvoid **vars) {
255 if (mStatus != ScriptStatus::Compiled) {
256 mErrorCode = BCC_INVALID_OPERATION;
257 return;
258 }
259
260 mCompiled->getExportVars(actualVarCount, maxVarCount, vars);
261}
262
263
264void Script::getExportFuncs(BCCsizei *actualFuncCount,
265 BCCsizei maxFuncCount,
266 BCCvoid **funcs) {
267 if (mStatus != ScriptStatus::Compiled) {
268 mErrorCode = BCC_INVALID_OPERATION;
269 return;
270 }
271
272 mCompiled->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
273}
274
275
276void Script::getPragmas(BCCsizei *actualStringCount,
277 BCCsizei maxStringCount,
278 BCCchar **strings) {
279 if (mStatus != ScriptStatus::Compiled) {
280 mErrorCode = BCC_INVALID_OPERATION;
281 return;
282 }
283
284 mCompiled->getPragmas(actualStringCount, maxStringCount, strings);
285}
286
287
288void Script::getFunctions(BCCsizei *actualFunctionCount,
289 BCCsizei maxFunctionCount,
290 BCCchar **functions) {
291 if (mStatus != ScriptStatus::Compiled) {
292 mErrorCode = BCC_INVALID_OPERATION;
293 return;
294 }
295
296 mCompiled->getFunctions(actualFunctionCount, maxFunctionCount, functions);
297}
298
299
300void Script::getFunctionBinary(BCCchar *function,
301 BCCvoid **base,
302 BCCsizei *length) {
303 if (mStatus != ScriptStatus::Compiled) {
304 mErrorCode = BCC_INVALID_OPERATION;
305 return;
306 }
307
308 mCompiled->getFunctionBinary(function, base, length);
309}
310
311
312void Script::registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
313 mpExtSymbolLookupFn = pFn;
314 mpExtSymbolLookupFnContext = pContext;
315
316 if (mStatus != ScriptStatus::Compiled) {
317 mErrorCode = BCC_INVALID_OPERATION;
318 return;
319 }
320
321 mCompiled->registerSymbolCallback(pFn, pContext);
322}
323
324} // namespace bcc