blob: a2039e5237856c9da5453e155aceabcd44fc32d0 [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
Logancf3e5212010-12-29 01:44:55 +080017#include "Script.h"
18
Logan35849002011-01-15 07:30:43 +080019#include "Config.h"
20
Loganf7f0ac52011-01-07 03:53:43 +080021#include "CacheReader.h"
Logan89eb47f2011-01-07 10:45:16 +080022#include "CacheWriter.h"
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070023#include "MCCacheReader.h"
24#include "MCCacheWriter.h"
Logan42598052011-01-26 22:41:13 +080025#include "ContextManager.h"
Logan4dcd6792011-02-28 05:12:00 +080026#include "DebugHelper.h"
Logan04329712011-01-06 06:10:57 +080027#include "FileHandle.h"
Logancf3e5212010-12-29 01:44:55 +080028#include "ScriptCompiled.h"
Logan9a5f8682011-01-07 06:09:57 +080029#include "ScriptCached.h"
30#include "Sha1Helper.h"
Logan474cbd22011-01-31 01:47:44 +080031#include "SourceInfo.h"
Logancf3e5212010-12-29 01:44:55 +080032
Logan89eb47f2011-01-07 10:45:16 +080033#include <errno.h>
Logan474cbd22011-01-31 01:47:44 +080034#include <sys/stat.h>
35#include <sys/types.h>
Logancf3e5212010-12-29 01:44:55 +080036
Logan89eb47f2011-01-07 10:45:16 +080037#include <new>
38#include <string.h>
Logan033f46e2011-01-06 05:51:24 +080039#include <cutils/properties.h>
40
Logan89eb47f2011-01-07 10:45:16 +080041
Loganecf4cbd2011-01-06 05:34:11 +080042namespace {
43
Logan033f46e2011-01-06 05:51:24 +080044bool getBooleanProp(const char *str) {
Loganf340bf72011-01-14 17:51:40 +080045 char buf[PROPERTY_VALUE_MAX];
46 property_get(str, buf, "0");
47 return strcmp(buf, "0") != 0;
Logan033f46e2011-01-06 05:51:24 +080048}
49
Loganecf4cbd2011-01-06 05:34:11 +080050} // namespace anonymous
51
Logancf3e5212010-12-29 01:44:55 +080052namespace bcc {
53
54Script::~Script() {
Logan35849002011-01-15 07:30:43 +080055 switch (mStatus) {
56 case ScriptStatus::Compiled:
Logancf3e5212010-12-29 01:44:55 +080057 delete mCompiled;
Logan35849002011-01-15 07:30:43 +080058 break;
59
60#if USE_CACHE
61 case ScriptStatus::Cached:
Shih-wei Liaoc4cf6542011-01-13 01:43:01 -080062 delete mCached;
Logan35849002011-01-15 07:30:43 +080063 break;
64#endif
65
66 default:
67 break;
Logancf3e5212010-12-29 01:44:55 +080068 }
Logan474cbd22011-01-31 01:47:44 +080069
70 for (size_t i = 0; i < 2; ++i) {
71 delete mSourceList[i];
72 }
Logancf3e5212010-12-29 01:44:55 +080073}
74
75
Logan474cbd22011-01-31 01:47:44 +080076int Script::addSourceBC(size_t idx,
77 char const *resName,
78 const char *bitcode,
79 size_t bitcodeSize,
80 unsigned long flags) {
Shih-wei Liao898c5a92011-05-18 07:02:39 -070081
82 if (!resName) {
83 mErrorCode = BCC_INVALID_VALUE;
84 LOGE("Invalid argument: resName = NULL\n");
85 return 1;
86 }
87
Loganecf4cbd2011-01-06 05:34:11 +080088 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +080089 mErrorCode = BCC_INVALID_OPERATION;
Logan474cbd22011-01-31 01:47:44 +080090 LOGE("Bad operation: Adding source after bccPrepareExecutable\n");
Logancf3e5212010-12-29 01:44:55 +080091 return 1;
92 }
93
Logan474cbd22011-01-31 01:47:44 +080094 if (!bitcode) {
95 mErrorCode = BCC_INVALID_VALUE;
96 LOGE("Invalid argument: bitcode = NULL\n");
97 return 1;
98 }
99
100 mSourceList[idx] = SourceInfo::createFromBuffer(resName,
101 bitcode, bitcodeSize,
102 flags);
103
104 if (!mSourceList[idx]) {
105 mErrorCode = BCC_OUT_OF_MEMORY;
106 LOGE("Out of memory while adding source bitcode\n");
107 return 1;
108 }
109
Loganecf4cbd2011-01-06 05:34:11 +0800110 return 0;
Logancf3e5212010-12-29 01:44:55 +0800111}
112
113
Logan474cbd22011-01-31 01:47:44 +0800114int Script::addSourceModule(size_t idx,
115 llvm::Module *module,
116 unsigned long flags) {
Logancf3e5212010-12-29 01:44:55 +0800117 if (mStatus != ScriptStatus::Unknown) {
118 mErrorCode = BCC_INVALID_OPERATION;
Logan474cbd22011-01-31 01:47:44 +0800119 LOGE("Bad operation: Adding source after bccPrepareExecutable\n");
Logancf3e5212010-12-29 01:44:55 +0800120 return 1;
121 }
122
Logan474cbd22011-01-31 01:47:44 +0800123 if (!module) {
124 mErrorCode = BCC_INVALID_VALUE;
125 LOGE("Invalid argument: module = NULL\n");
126 return 1;
127 }
128
129 mSourceList[idx] = SourceInfo::createFromModule(module, flags);
130
131 if (!mSourceList[idx]) {
132 mErrorCode = BCC_OUT_OF_MEMORY;
133 LOGE("Out of memory when add source module\n");
134 return 1;
135 }
136
Loganecf4cbd2011-01-06 05:34:11 +0800137 return 0;
Logancf3e5212010-12-29 01:44:55 +0800138}
139
140
Logan474cbd22011-01-31 01:47:44 +0800141int Script::addSourceFile(size_t idx,
142 char const *path,
143 unsigned long flags) {
Logan3133c412011-01-06 06:15:40 +0800144 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800145 mErrorCode = BCC_INVALID_OPERATION;
Logan474cbd22011-01-31 01:47:44 +0800146 LOGE("Bad operation: Adding source after bccPrepareExecutable\n");
Logancf3e5212010-12-29 01:44:55 +0800147 return 1;
148 }
149
Logan474cbd22011-01-31 01:47:44 +0800150 if (!path) {
151 mErrorCode = BCC_INVALID_VALUE;
152 LOGE("Invalid argument: path = NULL\n");
153 return 1;
154 }
155
156 struct stat sb;
157 if (stat(path, &sb) != 0) {
158 mErrorCode = BCC_INVALID_VALUE;
159 LOGE("File not found: %s\n", path);
160 return 1;
161 }
162
163 mSourceList[idx] = SourceInfo::createFromFile(path, flags);
164
165 if (!mSourceList[idx]) {
166 mErrorCode = BCC_OUT_OF_MEMORY;
167 LOGE("Out of memory while adding source file\n");
168 return 1;
169 }
170
Logan3133c412011-01-06 06:15:40 +0800171 return 0;
Logancf3e5212010-12-29 01:44:55 +0800172}
173
174
Loganf340bf72011-01-14 17:51:40 +0800175int Script::prepareExecutable(char const *cachePath, unsigned long flags) {
Loganecf4cbd2011-01-06 05:34:11 +0800176 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800177 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800178 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800179 return 1;
180 }
181
Logan35849002011-01-15 07:30:43 +0800182#if USE_CACHE
Loganecf4cbd2011-01-06 05:34:11 +0800183 // Load Cache File
Loganf340bf72011-01-14 17:51:40 +0800184 mCachePath = cachePath;
185 if (cachePath && internalLoadCache() == 0) {
Logan033f46e2011-01-06 05:51:24 +0800186 return 0;
187 }
Logan35849002011-01-15 07:30:43 +0800188#endif
Loganecf4cbd2011-01-06 05:34:11 +0800189
Stephen Hines27b35102011-05-11 17:58:48 -0700190 int status = internalCompile();
191 if (status != 0) {
192 LOGE("LLVM error message: %s\n", getCompilerErrorMessage());
193 }
194 return status;
Logan033f46e2011-01-06 05:51:24 +0800195}
196
197
Logan35849002011-01-15 07:30:43 +0800198#if USE_CACHE
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700199bool getObjPath(std::string &objPath) {
200 size_t found0 = objPath.find("@");
201 size_t found1 = objPath.rfind("@");
202
203 if (found0 == found1 ||
204 found0 == std::string::npos ||
205 found1 == std::string::npos) {
206 LOGE("Ill formatted resource name '%s'. The name should contain 2 @s",
207 objPath.c_str());
208 return false;
209 }
210
211 objPath.replace(found0, found1 - found0 + 1, "", 0);
212 objPath.resize(objPath.length() - 3);
213
214 LOGV("objPath = %s", objPath.c_str());
215 return true;
216}
217
218bool getInfoPath(std::string &infoPath) {
219 getObjPath(infoPath);
220 infoPath.erase(infoPath.size() - 1, 1);
221 infoPath.append("info");
222
223 LOGV("infoPath = %s", infoPath.c_str());
224 return true;
225}
226
Logan033f46e2011-01-06 05:51:24 +0800227int Script::internalLoadCache() {
228 if (getBooleanProp("debug.bcc.nocache")) {
229 // Android system environment property disable the cache mechanism by
230 // setting "debug.bcc.nocache". So we will not load the cache file any
231 // way.
232 return 1;
233 }
234
Loganf340bf72011-01-14 17:51:40 +0800235 if (!mCachePath) {
236 // The application developer has not specify the cachePath, so
Logan04329712011-01-06 06:10:57 +0800237 // we don't know where to open the cache file.
238 return 1;
Logan033f46e2011-01-06 05:51:24 +0800239 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700240 FileHandle objFile, infoFile;
Logan04329712011-01-06 06:10:57 +0800241
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700242 std::string objPath(mCachePath);
243 std::string infoPath(mCachePath);
Logan04329712011-01-06 06:10:57 +0800244
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700245#if USE_MCJIT
246 getObjPath(objPath);
247 getInfoPath(infoPath);
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700248#endif
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700249
250 if (objFile.open(objPath.c_str(), OpenMode::Read) < 0) {
Logan04329712011-01-06 06:10:57 +0800251 // Unable to open the cache file in read mode.
252 return 1;
253 }
254
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700255#if USE_OLD_JIT
Logane7eb7732011-01-07 07:11:56 +0800256 CacheReader reader;
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700257#endif
258
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700259#if USE_MCJIT
260 if (infoFile.open(infoPath.c_str(), OpenMode::Read) < 0) {
261 // Unable to open the cache file in read mode.
262 return 1;
263 }
264
265 MCCacheReader reader;
266
267 // Register symbol lookup function
268 if (mpExtSymbolLookupFn) {
269 reader.registerSymbolCallback(mpExtSymbolLookupFn,
270 mpExtSymbolLookupFnContext);
271 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700272#endif
Logan04329712011-01-06 06:10:57 +0800273
Logan9a5f8682011-01-07 06:09:57 +0800274 // Dependencies
Joseph Wen2ca6e572011-06-24 14:12:23 -0700275 reader.addDependency(BCC_FILE_RESOURCE, pathLibBCC_SHA1, sha1LibBCC_SHA1);
Logan9a5f8682011-01-07 06:09:57 +0800276
Logan474cbd22011-01-31 01:47:44 +0800277 for (size_t i = 0; i < 2; ++i) {
278 if (mSourceList[i]) {
279 mSourceList[i]->introDependency(reader);
280 }
Logan9a5f8682011-01-07 06:09:57 +0800281 }
282
283 // Read cache file
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700284#if USE_MCJIT
285 ScriptCached *cached = reader.readCacheFile(&objFile, &infoFile, this);
286#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700287
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700288#if USE_OLD_JIT
289 ScriptCached *cached = reader.readCacheFile(&objFile, this);
290#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700291
Logan04329712011-01-06 06:10:57 +0800292 if (!cached) {
Logan42598052011-01-26 22:41:13 +0800293 mIsContextSlotNotAvail = reader.isContextSlotNotAvail();
Logan04329712011-01-06 06:10:57 +0800294 return 1;
295 }
296
297 mCached = cached;
298 mStatus = ScriptStatus::Cached;
Logan033f46e2011-01-06 05:51:24 +0800299
Loganf3c83ce2011-01-07 06:36:33 +0800300 // Dirty hack for libRS.
301 // TODO(all): This dirty hack should be removed in the future.
Shih-wei Liao8eb5fe92011-02-01 04:17:38 -0800302 if (!cached->isLibRSThreadable() && mpExtSymbolLookupFn) {
Loganf3c83ce2011-01-07 06:36:33 +0800303 mpExtSymbolLookupFn(mpExtSymbolLookupFnContext, "__clearThreadable");
304 }
305
Loganf7f0ac52011-01-07 03:53:43 +0800306 return 0;
Logan033f46e2011-01-06 05:51:24 +0800307}
Logan35849002011-01-15 07:30:43 +0800308#endif
Logan033f46e2011-01-06 05:51:24 +0800309
Logan033f46e2011-01-06 05:51:24 +0800310int Script::internalCompile() {
311 // Create the ScriptCompiled object
Nowar Gu09b6c1c2011-05-24 23:49:07 +0800312 mCompiled = new (std::nothrow) ScriptCompiled(this);
Loganecf4cbd2011-01-06 05:34:11 +0800313
314 if (!mCompiled) {
315 mErrorCode = BCC_OUT_OF_MEMORY;
316 LOGE("Out of memory: %s %d\n", __FILE__, __LINE__);
317 return 1;
318 }
319
320 mStatus = ScriptStatus::Compiled;
321
Logan033f46e2011-01-06 05:51:24 +0800322 // Register symbol lookup function
Loganecf4cbd2011-01-06 05:34:11 +0800323 if (mpExtSymbolLookupFn) {
324 mCompiled->registerSymbolCallback(mpExtSymbolLookupFn,
325 mpExtSymbolLookupFnContext);
326 }
327
Logan474cbd22011-01-31 01:47:44 +0800328 // Parse Bitcode File (if necessary)
329 for (size_t i = 0; i < 2; ++i) {
330 if (mSourceList[i] && mSourceList[i]->prepareModule(mCompiled) != 0) {
331 LOGE("Unable to parse bitcode for source[%lu]\n", (unsigned long)i);
Logan033f46e2011-01-06 05:51:24 +0800332 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800333 }
Logan474cbd22011-01-31 01:47:44 +0800334 }
335
336 // Set the main source module
337 if (!mSourceList[0] || !mSourceList[0]->getModule()) {
338 LOGE("Source bitcode is not setted.\n");
339 return 1;
340 }
341
342 if (mCompiled->readModule(mSourceList[0]->takeModule()) != 0) {
343 LOGE("Unable to read source module\n");
344 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800345 }
346
Logan3133c412011-01-06 06:15:40 +0800347 // Link the source module with the library module
Logan474cbd22011-01-31 01:47:44 +0800348 if (mSourceList[1]) {
349 if (mCompiled->linkModule(mSourceList[1]->takeModule()) != 0) {
350 LOGE("Unable to link library module\n");
Logan04329712011-01-06 06:10:57 +0800351 return 1;
352 }
353 }
Loganecf4cbd2011-01-06 05:34:11 +0800354
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700355 mCompiled->setCachePath(mCachePath);
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700356
Logan3133c412011-01-06 06:15:40 +0800357 // Compile and JIT the code
Logan04329712011-01-06 06:10:57 +0800358 if (mCompiled->compile() != 0) {
Logan65719812011-01-07 11:17:14 +0800359 LOGE("Unable to compile.\n");
Logan04329712011-01-06 06:10:57 +0800360 return 1;
361 }
362
Logan35849002011-01-15 07:30:43 +0800363#if USE_CACHE
Logan42598052011-01-26 22:41:13 +0800364 // Note: If we re-compile the script because the cached context slot not
365 // available, then we don't have to write the cache.
366
367 // Note: If the address of the context is not in the context slot, then
368 // we don't have to cache it.
369
Logan42598052011-01-26 22:41:13 +0800370 if (mCachePath &&
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700371#if USE_OLD_JIT
Logan1dc63142011-02-25 17:14:51 +0800372 !mIsContextSlotNotAvail &&
373 ContextManager::get().isManagingContext(getContext()) &&
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700374#endif
Logan42598052011-01-26 22:41:13 +0800375 !getBooleanProp("debug.bcc.nocache")) {
376
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700377 FileHandle infoFile, objFile;
378
379 std::string objPath(mCachePath);
380 std::string infoPath(mCachePath);
381
382#if USE_MCJIT
383 getObjPath(objPath);
384 getInfoPath(infoPath);
385#endif
Logan04329712011-01-06 06:10:57 +0800386
Jeff Brown937a0bc2011-01-26 23:20:14 -0800387 // Remove the file if it already exists before writing the new file.
388 // The old file may still be mapped elsewhere in memory and we do not want
389 // to modify its contents. (The same script may be running concurrently in
390 // the same process or a different process!)
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700391 ::unlink(objPath.c_str());
392#if USE_MCJIT
393 ::unlink(infoPath.c_str());
394#endif
Jeff Brown937a0bc2011-01-26 23:20:14 -0800395
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700396 if (objFile.open(objPath.c_str(), OpenMode::Write) >= 0
397#if USE_MCJIT
398 && infoFile.open(infoPath.c_str(), OpenMode::Write) >= 0
399#endif
400 ) {
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700401
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700402#if USE_MCJIT
403 MCCacheWriter writer;
404#endif
405#if USE_OLD_JIT
Logan04329712011-01-06 06:10:57 +0800406 CacheWriter writer;
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700407#endif
Logana27a83f2011-01-07 10:25:48 +0800408
Joseph Wen2ca6e572011-06-24 14:12:23 -0700409#ifdef TARGET_BUILD
Logana2e15af2011-01-07 11:46:08 +0800410 // Dependencies
Joseph Wen2ca6e572011-06-24 14:12:23 -0700411 writer.addDependency(BCC_FILE_RESOURCE, pathLibBCC_SHA1, sha1LibBCC_SHA1);
Logane1323992011-01-12 04:47:13 +0800412#endif
Logana2e15af2011-01-07 11:46:08 +0800413
Logan474cbd22011-01-31 01:47:44 +0800414 for (size_t i = 0; i < 2; ++i) {
Logan825c3b22011-02-28 05:05:48 +0800415 if (mSourceList[i]) {
416 mSourceList[i]->introDependency(writer);
417 }
Logana2e15af2011-01-07 11:46:08 +0800418 }
419
Logana27a83f2011-01-07 10:25:48 +0800420 // libRS is threadable dirty hack
421 // TODO: This should be removed in the future
422 uint32_t libRS_threadable = 0;
423 if (mpExtSymbolLookupFn) {
Logan89eb47f2011-01-07 10:45:16 +0800424 libRS_threadable =
425 (uint32_t)mpExtSymbolLookupFn(mpExtSymbolLookupFnContext,
426 "__isThreadable");
Logana27a83f2011-01-07 10:25:48 +0800427 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700428
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700429#if USE_OLD_JIT
430 if (!writer.writeCacheFile(&objFile, this, libRS_threadable)) {
431#endif
432#if USE_MCJIT
433 if (!writer.writeCacheFile(&objFile, &infoFile, this, libRS_threadable)) {
434#endif
435 objFile.truncate();
436 objFile.close();
Logana27a83f2011-01-07 10:25:48 +0800437
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700438 if (unlink(objPath.c_str()) != 0) {
Logan89eb47f2011-01-07 10:45:16 +0800439 LOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700440 objPath.c_str(), strerror(errno));
Logan89eb47f2011-01-07 10:45:16 +0800441 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700442
443#if USE_MCJIT
444 infoFile.truncate();
445 infoFile.close();
446
447 if (unlink(infoPath.c_str()) != 0) {
448 LOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
449 infoPath.c_str(), strerror(errno));
450 }
451#endif
Logan89eb47f2011-01-07 10:45:16 +0800452 }
453 }
Logan04329712011-01-06 06:10:57 +0800454 }
Logan35849002011-01-15 07:30:43 +0800455#endif // USE_CACHE
Logan04329712011-01-06 06:10:57 +0800456
457 return 0;
Logancf3e5212010-12-29 01:44:55 +0800458}
459
460
461char const *Script::getCompilerErrorMessage() {
462 if (mStatus != ScriptStatus::Compiled) {
463 mErrorCode = BCC_INVALID_OPERATION;
464 return NULL;
465 }
466
467 return mCompiled->getCompilerErrorMessage();
468}
469
470
471void *Script::lookup(const char *name) {
Logan89eb47f2011-01-07 10:45:16 +0800472 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700473 case ScriptStatus::Compiled: {
474 return mCompiled->lookup(name);
475 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700476
Logan35849002011-01-15 07:30:43 +0800477#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700478 case ScriptStatus::Cached: {
479 return mCached->lookup(name);
480 }
Logan35849002011-01-15 07:30:43 +0800481#endif
Logan89eb47f2011-01-07 10:45:16 +0800482
Shih-wei Liaod50be322011-07-01 22:53:31 -0700483 default: {
484 mErrorCode = BCC_INVALID_OPERATION;
485 return NULL;
486 }
Logancf3e5212010-12-29 01:44:55 +0800487 }
Logancf3e5212010-12-29 01:44:55 +0800488}
489
490
Loganbe79ada2011-01-13 01:33:45 +0800491size_t Script::getExportVarCount() const {
Logan89eb47f2011-01-07 10:45:16 +0800492 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700493 case ScriptStatus::Compiled: {
494 return mCompiled->getExportVarCount();
495 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700496
Logan35849002011-01-15 07:30:43 +0800497#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700498 case ScriptStatus::Cached: {
499 return mCached->getExportVarCount();
500 }
Logan35849002011-01-15 07:30:43 +0800501#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700502
Shih-wei Liaod50be322011-07-01 22:53:31 -0700503 default: {
504 return 0;
505 }
Loganbe79ada2011-01-13 01:33:45 +0800506 }
507}
508
509
510size_t Script::getExportFuncCount() const {
511 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700512 case ScriptStatus::Compiled: {
513 return mCompiled->getExportFuncCount();
514 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700515
Logan35849002011-01-15 07:30:43 +0800516#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700517 case ScriptStatus::Cached: {
518 return mCached->getExportFuncCount();
519 }
Logan35849002011-01-15 07:30:43 +0800520#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700521
Shih-wei Liaod50be322011-07-01 22:53:31 -0700522 default: {
523 return 0;
524 }
Loganbe79ada2011-01-13 01:33:45 +0800525 }
526}
527
528
529size_t Script::getPragmaCount() const {
530 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700531 case ScriptStatus::Compiled: {
532 return mCompiled->getPragmaCount();
533 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700534
Logan35849002011-01-15 07:30:43 +0800535#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700536 case ScriptStatus::Cached: {
537 return mCached->getPragmaCount();
538 }
Logan35849002011-01-15 07:30:43 +0800539#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700540
Shih-wei Liaod50be322011-07-01 22:53:31 -0700541 default: {
542 return 0;
543 }
Loganbe79ada2011-01-13 01:33:45 +0800544 }
545}
546
547
548size_t Script::getFuncCount() const {
549 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700550 case ScriptStatus::Compiled: {
551 return mCompiled->getFuncCount();
552 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700553
Logan35849002011-01-15 07:30:43 +0800554#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700555 case ScriptStatus::Cached: {
556 return mCached->getFuncCount();
557 }
Logan35849002011-01-15 07:30:43 +0800558#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700559
Shih-wei Liaod50be322011-07-01 22:53:31 -0700560 default: {
561 return 0;
562 }
Loganbe79ada2011-01-13 01:33:45 +0800563 }
564}
565
566
Stephen Hines071288a2011-01-27 14:38:26 -0800567size_t Script::getObjectSlotCount() const {
568 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700569 case ScriptStatus::Compiled: {
570 return mCompiled->getObjectSlotCount();
571 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700572
Stephen Hines071288a2011-01-27 14:38:26 -0800573#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700574 case ScriptStatus::Cached: {
575 return mCached->getObjectSlotCount();
576 }
Stephen Hines071288a2011-01-27 14:38:26 -0800577#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700578
Shih-wei Liaod50be322011-07-01 22:53:31 -0700579 default: {
580 return 0;
581 }
Stephen Hines071288a2011-01-27 14:38:26 -0800582 }
583}
584
585
Loganbe79ada2011-01-13 01:33:45 +0800586void Script::getExportVarList(size_t varListSize, void **varList) {
587 switch (mStatus) {
588#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700589 case ScriptStatus::STATUS: \
590 m##STATUS->getExportVarList(varListSize, varList); \
591 break;
Logancf3e5212010-12-29 01:44:55 +0800592
Logan35849002011-01-15 07:30:43 +0800593#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700594 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800595#endif
596
Shih-wei Liaod50be322011-07-01 22:53:31 -0700597 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800598#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800599
Shih-wei Liaod50be322011-07-01 22:53:31 -0700600 default: {
601 mErrorCode = BCC_INVALID_OPERATION;
602 }
Logan89eb47f2011-01-07 10:45:16 +0800603 }
Logancf3e5212010-12-29 01:44:55 +0800604}
605
606
Loganbe79ada2011-01-13 01:33:45 +0800607void Script::getExportFuncList(size_t funcListSize, void **funcList) {
Logan89eb47f2011-01-07 10:45:16 +0800608 switch (mStatus) {
Loganbe79ada2011-01-13 01:33:45 +0800609#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700610 case ScriptStatus::STATUS: \
611 m##STATUS->getExportFuncList(funcListSize, funcList); \
612 break;
Logancf3e5212010-12-29 01:44:55 +0800613
Logan35849002011-01-15 07:30:43 +0800614#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700615 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800616#endif
617
Shih-wei Liaod50be322011-07-01 22:53:31 -0700618 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800619#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800620
Shih-wei Liaod50be322011-07-01 22:53:31 -0700621 default: {
622 mErrorCode = BCC_INVALID_OPERATION;
623 }
Logan89eb47f2011-01-07 10:45:16 +0800624 }
Logancf3e5212010-12-29 01:44:55 +0800625}
626
627
Loganbe79ada2011-01-13 01:33:45 +0800628void Script::getPragmaList(size_t pragmaListSize,
629 char const **keyList,
630 char const **valueList) {
Logan89eb47f2011-01-07 10:45:16 +0800631 switch (mStatus) {
Loganbe79ada2011-01-13 01:33:45 +0800632#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700633 case ScriptStatus::STATUS: \
634 m##STATUS->getPragmaList(pragmaListSize, keyList, valueList); \
635 break;
Logancf3e5212010-12-29 01:44:55 +0800636
Logan35849002011-01-15 07:30:43 +0800637#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700638 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800639#endif
640
Shih-wei Liaod50be322011-07-01 22:53:31 -0700641 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800642#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800643
Shih-wei Liaod50be322011-07-01 22:53:31 -0700644 default: {
645 mErrorCode = BCC_INVALID_OPERATION;
646 }
Logan89eb47f2011-01-07 10:45:16 +0800647 }
Logancf3e5212010-12-29 01:44:55 +0800648}
649
650
Loganf340bf72011-01-14 17:51:40 +0800651void Script::getFuncInfoList(size_t funcInfoListSize,
652 FuncInfo *funcInfoList) {
Logan89eb47f2011-01-07 10:45:16 +0800653 switch (mStatus) {
Loganbe79ada2011-01-13 01:33:45 +0800654#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700655 case ScriptStatus::STATUS: \
656 m##STATUS->getFuncInfoList(funcInfoListSize, funcInfoList); \
657 break;
Logancf3e5212010-12-29 01:44:55 +0800658
Logan35849002011-01-15 07:30:43 +0800659#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700660 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800661#endif
662
Shih-wei Liaod50be322011-07-01 22:53:31 -0700663 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800664#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800665
Shih-wei Liaod50be322011-07-01 22:53:31 -0700666 default: {
667 mErrorCode = BCC_INVALID_OPERATION;
668 }
Logan89eb47f2011-01-07 10:45:16 +0800669 }
Logancf3e5212010-12-29 01:44:55 +0800670}
671
Stephen Hines071288a2011-01-27 14:38:26 -0800672
673void Script::getObjectSlotList(size_t objectSlotListSize,
674 uint32_t *objectSlotList) {
675 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700676#define DELEGATE(STATUS) \
677 case ScriptStatus::STATUS: \
678 m##STATUS->getObjectSlotList(objectSlotListSize, objectSlotList); \
679 break;
Stephen Hines071288a2011-01-27 14:38:26 -0800680
681#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700682 DELEGATE(Cached);
Stephen Hines071288a2011-01-27 14:38:26 -0800683#endif
684
Shih-wei Liaod50be322011-07-01 22:53:31 -0700685 DELEGATE(Compiled);
Stephen Hines071288a2011-01-27 14:38:26 -0800686#undef DELEGATE
687
Shih-wei Liaod50be322011-07-01 22:53:31 -0700688 default: {
689 mErrorCode = BCC_INVALID_OPERATION;
690 }
Stephen Hines071288a2011-01-27 14:38:26 -0800691 }
692}
693
694
Logana27a83f2011-01-07 10:25:48 +0800695char *Script::getContext() {
696 switch (mStatus) {
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700697
Logan35849002011-01-15 07:30:43 +0800698#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700699 case ScriptStatus::Cached: {
700 return mCached->getContext();
701 }
Logan35849002011-01-15 07:30:43 +0800702#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700703
Shih-wei Liaod50be322011-07-01 22:53:31 -0700704 case ScriptStatus::Compiled: {
705 return mCompiled->getContext();
706 }
Logana27a83f2011-01-07 10:25:48 +0800707
Shih-wei Liaod50be322011-07-01 22:53:31 -0700708 default: {
709 mErrorCode = BCC_INVALID_OPERATION;
710 return NULL;
711 }
Logan02286cb2011-01-07 00:30:47 +0800712 }
Logan02286cb2011-01-07 00:30:47 +0800713}
714
Logancf3e5212010-12-29 01:44:55 +0800715
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800716int Script::registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logancf3e5212010-12-29 01:44:55 +0800717 mpExtSymbolLookupFn = pFn;
718 mpExtSymbolLookupFnContext = pContext;
719
Logan7d2219f2011-01-06 06:19:25 +0800720 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800721 mErrorCode = BCC_INVALID_OPERATION;
Logan7d2219f2011-01-06 06:19:25 +0800722 LOGE("Invalid operation: %s\n", __func__);
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800723 return 1;
Logancf3e5212010-12-29 01:44:55 +0800724 }
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800725 return 0;
Logancf3e5212010-12-29 01:44:55 +0800726}
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700727
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700728#if USE_MCJIT
729size_t Script::getELFSize() const {
730 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700731 case ScriptStatus::Compiled: {
732 return mCompiled->getELFSize();
733 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700734
Shih-wei Liaod50be322011-07-01 22:53:31 -0700735 default: {
736 return 0;
737 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700738 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700739}
740
741const char *Script::getELF() const {
742 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700743 case ScriptStatus::Compiled: {
744 return mCompiled->getELF();
745 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700746
Shih-wei Liaod50be322011-07-01 22:53:31 -0700747 default: {
748 return NULL;
749 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700750 }
751}
752#endif
Logancf3e5212010-12-29 01:44:55 +0800753
754} // namespace bcc