blob: 661c1b3d0283c63109f3ea0222326a506af7c6aa [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() {
Jason Samseeeccd92011-07-07 14:20:17 -0700228 // Temporarly disable the cache.
229 if (1 || getBooleanProp("debug.bcc.nocache")) {
Logan033f46e2011-01-06 05:51:24 +0800230 // Android system environment property disable the cache mechanism by
231 // setting "debug.bcc.nocache". So we will not load the cache file any
232 // way.
233 return 1;
234 }
235
Loganf340bf72011-01-14 17:51:40 +0800236 if (!mCachePath) {
237 // The application developer has not specify the cachePath, so
Logan04329712011-01-06 06:10:57 +0800238 // we don't know where to open the cache file.
239 return 1;
Logan033f46e2011-01-06 05:51:24 +0800240 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700241 FileHandle objFile, infoFile;
Logan04329712011-01-06 06:10:57 +0800242
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700243 std::string objPath(mCachePath);
244 std::string infoPath(mCachePath);
Logan04329712011-01-06 06:10:57 +0800245
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700246#if USE_MCJIT
247 getObjPath(objPath);
248 getInfoPath(infoPath);
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700249#endif
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700250
251 if (objFile.open(objPath.c_str(), OpenMode::Read) < 0) {
Logan04329712011-01-06 06:10:57 +0800252 // Unable to open the cache file in read mode.
253 return 1;
254 }
255
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700256#if USE_OLD_JIT
Logane7eb7732011-01-07 07:11:56 +0800257 CacheReader reader;
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700258#endif
259
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700260#if USE_MCJIT
261 if (infoFile.open(infoPath.c_str(), OpenMode::Read) < 0) {
262 // Unable to open the cache file in read mode.
263 return 1;
264 }
265
266 MCCacheReader reader;
267
268 // Register symbol lookup function
269 if (mpExtSymbolLookupFn) {
270 reader.registerSymbolCallback(mpExtSymbolLookupFn,
271 mpExtSymbolLookupFnContext);
272 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700273#endif
Logan04329712011-01-06 06:10:57 +0800274
Logan9a5f8682011-01-07 06:09:57 +0800275 // Dependencies
Joseph Wen2ca6e572011-06-24 14:12:23 -0700276 reader.addDependency(BCC_FILE_RESOURCE, pathLibBCC_SHA1, sha1LibBCC_SHA1);
Logan9a5f8682011-01-07 06:09:57 +0800277
Logan474cbd22011-01-31 01:47:44 +0800278 for (size_t i = 0; i < 2; ++i) {
279 if (mSourceList[i]) {
280 mSourceList[i]->introDependency(reader);
281 }
Logan9a5f8682011-01-07 06:09:57 +0800282 }
283
284 // Read cache file
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700285#if USE_MCJIT
286 ScriptCached *cached = reader.readCacheFile(&objFile, &infoFile, this);
287#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700288
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700289#if USE_OLD_JIT
290 ScriptCached *cached = reader.readCacheFile(&objFile, this);
291#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700292
Logan04329712011-01-06 06:10:57 +0800293 if (!cached) {
Logan42598052011-01-26 22:41:13 +0800294 mIsContextSlotNotAvail = reader.isContextSlotNotAvail();
Logan04329712011-01-06 06:10:57 +0800295 return 1;
296 }
297
298 mCached = cached;
299 mStatus = ScriptStatus::Cached;
Logan033f46e2011-01-06 05:51:24 +0800300
Loganf3c83ce2011-01-07 06:36:33 +0800301 // Dirty hack for libRS.
302 // TODO(all): This dirty hack should be removed in the future.
Shih-wei Liao8eb5fe92011-02-01 04:17:38 -0800303 if (!cached->isLibRSThreadable() && mpExtSymbolLookupFn) {
Loganf3c83ce2011-01-07 06:36:33 +0800304 mpExtSymbolLookupFn(mpExtSymbolLookupFnContext, "__clearThreadable");
305 }
306
Loganf7f0ac52011-01-07 03:53:43 +0800307 return 0;
Logan033f46e2011-01-06 05:51:24 +0800308}
Logan35849002011-01-15 07:30:43 +0800309#endif
Logan033f46e2011-01-06 05:51:24 +0800310
Logan033f46e2011-01-06 05:51:24 +0800311int Script::internalCompile() {
312 // Create the ScriptCompiled object
Nowar Gu09b6c1c2011-05-24 23:49:07 +0800313 mCompiled = new (std::nothrow) ScriptCompiled(this);
Loganecf4cbd2011-01-06 05:34:11 +0800314
315 if (!mCompiled) {
316 mErrorCode = BCC_OUT_OF_MEMORY;
317 LOGE("Out of memory: %s %d\n", __FILE__, __LINE__);
318 return 1;
319 }
320
321 mStatus = ScriptStatus::Compiled;
322
Logan033f46e2011-01-06 05:51:24 +0800323 // Register symbol lookup function
Loganecf4cbd2011-01-06 05:34:11 +0800324 if (mpExtSymbolLookupFn) {
325 mCompiled->registerSymbolCallback(mpExtSymbolLookupFn,
326 mpExtSymbolLookupFnContext);
327 }
328
Logan474cbd22011-01-31 01:47:44 +0800329 // Parse Bitcode File (if necessary)
330 for (size_t i = 0; i < 2; ++i) {
331 if (mSourceList[i] && mSourceList[i]->prepareModule(mCompiled) != 0) {
332 LOGE("Unable to parse bitcode for source[%lu]\n", (unsigned long)i);
Logan033f46e2011-01-06 05:51:24 +0800333 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800334 }
Logan474cbd22011-01-31 01:47:44 +0800335 }
336
337 // Set the main source module
338 if (!mSourceList[0] || !mSourceList[0]->getModule()) {
339 LOGE("Source bitcode is not setted.\n");
340 return 1;
341 }
342
343 if (mCompiled->readModule(mSourceList[0]->takeModule()) != 0) {
344 LOGE("Unable to read source module\n");
345 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800346 }
347
Logan3133c412011-01-06 06:15:40 +0800348 // Link the source module with the library module
Logan474cbd22011-01-31 01:47:44 +0800349 if (mSourceList[1]) {
350 if (mCompiled->linkModule(mSourceList[1]->takeModule()) != 0) {
351 LOGE("Unable to link library module\n");
Logan04329712011-01-06 06:10:57 +0800352 return 1;
353 }
354 }
Loganecf4cbd2011-01-06 05:34:11 +0800355
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700356 mCompiled->setCachePath(mCachePath);
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700357
Logan3133c412011-01-06 06:15:40 +0800358 // Compile and JIT the code
Logan04329712011-01-06 06:10:57 +0800359 if (mCompiled->compile() != 0) {
Logan65719812011-01-07 11:17:14 +0800360 LOGE("Unable to compile.\n");
Logan04329712011-01-06 06:10:57 +0800361 return 1;
362 }
363
Logan35849002011-01-15 07:30:43 +0800364#if USE_CACHE
Logan42598052011-01-26 22:41:13 +0800365 // Note: If we re-compile the script because the cached context slot not
366 // available, then we don't have to write the cache.
367
368 // Note: If the address of the context is not in the context slot, then
369 // we don't have to cache it.
370
Logan42598052011-01-26 22:41:13 +0800371 if (mCachePath &&
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700372#if USE_OLD_JIT
Logan1dc63142011-02-25 17:14:51 +0800373 !mIsContextSlotNotAvail &&
374 ContextManager::get().isManagingContext(getContext()) &&
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700375#endif
Logan42598052011-01-26 22:41:13 +0800376 !getBooleanProp("debug.bcc.nocache")) {
377
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700378 FileHandle infoFile, objFile;
379
380 std::string objPath(mCachePath);
381 std::string infoPath(mCachePath);
382
383#if USE_MCJIT
384 getObjPath(objPath);
385 getInfoPath(infoPath);
386#endif
Logan04329712011-01-06 06:10:57 +0800387
Jeff Brown937a0bc2011-01-26 23:20:14 -0800388 // Remove the file if it already exists before writing the new file.
389 // The old file may still be mapped elsewhere in memory and we do not want
390 // to modify its contents. (The same script may be running concurrently in
391 // the same process or a different process!)
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700392 ::unlink(objPath.c_str());
393#if USE_MCJIT
394 ::unlink(infoPath.c_str());
395#endif
Jeff Brown937a0bc2011-01-26 23:20:14 -0800396
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700397 if (objFile.open(objPath.c_str(), OpenMode::Write) >= 0
398#if USE_MCJIT
399 && infoFile.open(infoPath.c_str(), OpenMode::Write) >= 0
400#endif
401 ) {
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700402
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700403#if USE_MCJIT
404 MCCacheWriter writer;
405#endif
406#if USE_OLD_JIT
Logan04329712011-01-06 06:10:57 +0800407 CacheWriter writer;
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700408#endif
Logana27a83f2011-01-07 10:25:48 +0800409
Joseph Wen2ca6e572011-06-24 14:12:23 -0700410#ifdef TARGET_BUILD
Logana2e15af2011-01-07 11:46:08 +0800411 // Dependencies
Joseph Wen2ca6e572011-06-24 14:12:23 -0700412 writer.addDependency(BCC_FILE_RESOURCE, pathLibBCC_SHA1, sha1LibBCC_SHA1);
Logane1323992011-01-12 04:47:13 +0800413#endif
Logana2e15af2011-01-07 11:46:08 +0800414
Logan474cbd22011-01-31 01:47:44 +0800415 for (size_t i = 0; i < 2; ++i) {
Logan825c3b22011-02-28 05:05:48 +0800416 if (mSourceList[i]) {
417 mSourceList[i]->introDependency(writer);
418 }
Logana2e15af2011-01-07 11:46:08 +0800419 }
420
Logana27a83f2011-01-07 10:25:48 +0800421 // libRS is threadable dirty hack
422 // TODO: This should be removed in the future
423 uint32_t libRS_threadable = 0;
424 if (mpExtSymbolLookupFn) {
Logan89eb47f2011-01-07 10:45:16 +0800425 libRS_threadable =
426 (uint32_t)mpExtSymbolLookupFn(mpExtSymbolLookupFnContext,
427 "__isThreadable");
Logana27a83f2011-01-07 10:25:48 +0800428 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700429
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700430#if USE_OLD_JIT
431 if (!writer.writeCacheFile(&objFile, this, libRS_threadable)) {
432#endif
433#if USE_MCJIT
434 if (!writer.writeCacheFile(&objFile, &infoFile, this, libRS_threadable)) {
435#endif
436 objFile.truncate();
437 objFile.close();
Logana27a83f2011-01-07 10:25:48 +0800438
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700439 if (unlink(objPath.c_str()) != 0) {
Logan89eb47f2011-01-07 10:45:16 +0800440 LOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700441 objPath.c_str(), strerror(errno));
Logan89eb47f2011-01-07 10:45:16 +0800442 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700443
444#if USE_MCJIT
445 infoFile.truncate();
446 infoFile.close();
447
448 if (unlink(infoPath.c_str()) != 0) {
449 LOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
450 infoPath.c_str(), strerror(errno));
451 }
452#endif
Logan89eb47f2011-01-07 10:45:16 +0800453 }
454 }
Logan04329712011-01-06 06:10:57 +0800455 }
Logan35849002011-01-15 07:30:43 +0800456#endif // USE_CACHE
Logan04329712011-01-06 06:10:57 +0800457
458 return 0;
Logancf3e5212010-12-29 01:44:55 +0800459}
460
461
462char const *Script::getCompilerErrorMessage() {
463 if (mStatus != ScriptStatus::Compiled) {
464 mErrorCode = BCC_INVALID_OPERATION;
465 return NULL;
466 }
467
468 return mCompiled->getCompilerErrorMessage();
469}
470
471
472void *Script::lookup(const char *name) {
Logan89eb47f2011-01-07 10:45:16 +0800473 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700474 case ScriptStatus::Compiled: {
475 return mCompiled->lookup(name);
476 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700477
Logan35849002011-01-15 07:30:43 +0800478#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700479 case ScriptStatus::Cached: {
480 return mCached->lookup(name);
481 }
Logan35849002011-01-15 07:30:43 +0800482#endif
Logan89eb47f2011-01-07 10:45:16 +0800483
Shih-wei Liaod50be322011-07-01 22:53:31 -0700484 default: {
485 mErrorCode = BCC_INVALID_OPERATION;
486 return NULL;
487 }
Logancf3e5212010-12-29 01:44:55 +0800488 }
Logancf3e5212010-12-29 01:44:55 +0800489}
490
491
Loganbe79ada2011-01-13 01:33:45 +0800492size_t Script::getExportVarCount() const {
Logan89eb47f2011-01-07 10:45:16 +0800493 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700494 case ScriptStatus::Compiled: {
495 return mCompiled->getExportVarCount();
496 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700497
Logan35849002011-01-15 07:30:43 +0800498#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700499 case ScriptStatus::Cached: {
500 return mCached->getExportVarCount();
501 }
Logan35849002011-01-15 07:30:43 +0800502#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700503
Shih-wei Liaod50be322011-07-01 22:53:31 -0700504 default: {
505 return 0;
506 }
Loganbe79ada2011-01-13 01:33:45 +0800507 }
508}
509
510
511size_t Script::getExportFuncCount() const {
512 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700513 case ScriptStatus::Compiled: {
514 return mCompiled->getExportFuncCount();
515 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700516
Logan35849002011-01-15 07:30:43 +0800517#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700518 case ScriptStatus::Cached: {
519 return mCached->getExportFuncCount();
520 }
Logan35849002011-01-15 07:30:43 +0800521#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700522
Shih-wei Liaod50be322011-07-01 22:53:31 -0700523 default: {
524 return 0;
525 }
Loganbe79ada2011-01-13 01:33:45 +0800526 }
527}
528
529
530size_t Script::getPragmaCount() const {
531 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700532 case ScriptStatus::Compiled: {
533 return mCompiled->getPragmaCount();
534 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700535
Logan35849002011-01-15 07:30:43 +0800536#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700537 case ScriptStatus::Cached: {
538 return mCached->getPragmaCount();
539 }
Logan35849002011-01-15 07:30:43 +0800540#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700541
Shih-wei Liaod50be322011-07-01 22:53:31 -0700542 default: {
543 return 0;
544 }
Loganbe79ada2011-01-13 01:33:45 +0800545 }
546}
547
548
549size_t Script::getFuncCount() const {
550 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700551 case ScriptStatus::Compiled: {
552 return mCompiled->getFuncCount();
553 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700554
Logan35849002011-01-15 07:30:43 +0800555#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700556 case ScriptStatus::Cached: {
557 return mCached->getFuncCount();
558 }
Logan35849002011-01-15 07:30:43 +0800559#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700560
Shih-wei Liaod50be322011-07-01 22:53:31 -0700561 default: {
562 return 0;
563 }
Loganbe79ada2011-01-13 01:33:45 +0800564 }
565}
566
567
Stephen Hines071288a2011-01-27 14:38:26 -0800568size_t Script::getObjectSlotCount() const {
569 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700570 case ScriptStatus::Compiled: {
571 return mCompiled->getObjectSlotCount();
572 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700573
Stephen Hines071288a2011-01-27 14:38:26 -0800574#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700575 case ScriptStatus::Cached: {
576 return mCached->getObjectSlotCount();
577 }
Stephen Hines071288a2011-01-27 14:38:26 -0800578#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700579
Shih-wei Liaod50be322011-07-01 22:53:31 -0700580 default: {
581 return 0;
582 }
Stephen Hines071288a2011-01-27 14:38:26 -0800583 }
584}
585
586
Loganbe79ada2011-01-13 01:33:45 +0800587void Script::getExportVarList(size_t varListSize, void **varList) {
588 switch (mStatus) {
589#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700590 case ScriptStatus::STATUS: \
591 m##STATUS->getExportVarList(varListSize, varList); \
592 break;
Logancf3e5212010-12-29 01:44:55 +0800593
Logan35849002011-01-15 07:30:43 +0800594#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700595 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800596#endif
597
Shih-wei Liaod50be322011-07-01 22:53:31 -0700598 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800599#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800600
Shih-wei Liaod50be322011-07-01 22:53:31 -0700601 default: {
602 mErrorCode = BCC_INVALID_OPERATION;
603 }
Logan89eb47f2011-01-07 10:45:16 +0800604 }
Logancf3e5212010-12-29 01:44:55 +0800605}
606
Joseph Wenf36637f2011-07-06 18:27:12 -0700607void Script::getExportVarNameList(std::vector<std::string> &varList) {
608 switch (mStatus) {
609 case ScriptStatus::Compiled: {
610 return mCompiled->getExportVarNameList(varList);
611 }
612
613 default: {
614 mErrorCode = BCC_INVALID_OPERATION;
615 }
616 }
617}
618
Logancf3e5212010-12-29 01:44:55 +0800619
Loganbe79ada2011-01-13 01:33:45 +0800620void Script::getExportFuncList(size_t funcListSize, void **funcList) {
Logan89eb47f2011-01-07 10:45:16 +0800621 switch (mStatus) {
Loganbe79ada2011-01-13 01:33:45 +0800622#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700623 case ScriptStatus::STATUS: \
624 m##STATUS->getExportFuncList(funcListSize, funcList); \
625 break;
Logancf3e5212010-12-29 01:44:55 +0800626
Logan35849002011-01-15 07:30:43 +0800627#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700628 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800629#endif
630
Shih-wei Liaod50be322011-07-01 22:53:31 -0700631 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800632#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800633
Shih-wei Liaod50be322011-07-01 22:53:31 -0700634 default: {
635 mErrorCode = BCC_INVALID_OPERATION;
636 }
Logan89eb47f2011-01-07 10:45:16 +0800637 }
Logancf3e5212010-12-29 01:44:55 +0800638}
639
Joseph Wenf36637f2011-07-06 18:27:12 -0700640void Script::getExportFuncNameList(std::vector<std::string> &funcList) {
641 switch (mStatus) {
642 case ScriptStatus::Compiled: {
643 return mCompiled->getExportFuncNameList(funcList);
644 }
645
646 default: {
647 mErrorCode = BCC_INVALID_OPERATION;
648 }
649 }
650}
651
Logancf3e5212010-12-29 01:44:55 +0800652
Loganbe79ada2011-01-13 01:33:45 +0800653void Script::getPragmaList(size_t pragmaListSize,
654 char const **keyList,
655 char const **valueList) {
Logan89eb47f2011-01-07 10:45:16 +0800656 switch (mStatus) {
Loganbe79ada2011-01-13 01:33:45 +0800657#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700658 case ScriptStatus::STATUS: \
659 m##STATUS->getPragmaList(pragmaListSize, keyList, valueList); \
660 break;
Logancf3e5212010-12-29 01:44:55 +0800661
Logan35849002011-01-15 07:30:43 +0800662#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700663 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800664#endif
665
Shih-wei Liaod50be322011-07-01 22:53:31 -0700666 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800667#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800668
Shih-wei Liaod50be322011-07-01 22:53:31 -0700669 default: {
670 mErrorCode = BCC_INVALID_OPERATION;
671 }
Logan89eb47f2011-01-07 10:45:16 +0800672 }
Logancf3e5212010-12-29 01:44:55 +0800673}
674
675
Loganf340bf72011-01-14 17:51:40 +0800676void Script::getFuncInfoList(size_t funcInfoListSize,
677 FuncInfo *funcInfoList) {
Logan89eb47f2011-01-07 10:45:16 +0800678 switch (mStatus) {
Loganbe79ada2011-01-13 01:33:45 +0800679#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700680 case ScriptStatus::STATUS: \
681 m##STATUS->getFuncInfoList(funcInfoListSize, funcInfoList); \
682 break;
Logancf3e5212010-12-29 01:44:55 +0800683
Logan35849002011-01-15 07:30:43 +0800684#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700685 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800686#endif
687
Shih-wei Liaod50be322011-07-01 22:53:31 -0700688 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800689#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800690
Shih-wei Liaod50be322011-07-01 22:53:31 -0700691 default: {
692 mErrorCode = BCC_INVALID_OPERATION;
693 }
Logan89eb47f2011-01-07 10:45:16 +0800694 }
Logancf3e5212010-12-29 01:44:55 +0800695}
696
Stephen Hines071288a2011-01-27 14:38:26 -0800697
698void Script::getObjectSlotList(size_t objectSlotListSize,
699 uint32_t *objectSlotList) {
700 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700701#define DELEGATE(STATUS) \
702 case ScriptStatus::STATUS: \
703 m##STATUS->getObjectSlotList(objectSlotListSize, objectSlotList); \
704 break;
Stephen Hines071288a2011-01-27 14:38:26 -0800705
706#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700707 DELEGATE(Cached);
Stephen Hines071288a2011-01-27 14:38:26 -0800708#endif
709
Shih-wei Liaod50be322011-07-01 22:53:31 -0700710 DELEGATE(Compiled);
Stephen Hines071288a2011-01-27 14:38:26 -0800711#undef DELEGATE
712
Shih-wei Liaod50be322011-07-01 22:53:31 -0700713 default: {
714 mErrorCode = BCC_INVALID_OPERATION;
715 }
Stephen Hines071288a2011-01-27 14:38:26 -0800716 }
717}
718
719
Logana27a83f2011-01-07 10:25:48 +0800720char *Script::getContext() {
721 switch (mStatus) {
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700722
Logan35849002011-01-15 07:30:43 +0800723#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700724 case ScriptStatus::Cached: {
725 return mCached->getContext();
726 }
Logan35849002011-01-15 07:30:43 +0800727#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700728
Shih-wei Liaod50be322011-07-01 22:53:31 -0700729 case ScriptStatus::Compiled: {
730 return mCompiled->getContext();
731 }
Logana27a83f2011-01-07 10:25:48 +0800732
Shih-wei Liaod50be322011-07-01 22:53:31 -0700733 default: {
734 mErrorCode = BCC_INVALID_OPERATION;
735 return NULL;
736 }
Logan02286cb2011-01-07 00:30:47 +0800737 }
Logan02286cb2011-01-07 00:30:47 +0800738}
739
Logancf3e5212010-12-29 01:44:55 +0800740
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800741int Script::registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logancf3e5212010-12-29 01:44:55 +0800742 mpExtSymbolLookupFn = pFn;
743 mpExtSymbolLookupFnContext = pContext;
744
Logan7d2219f2011-01-06 06:19:25 +0800745 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800746 mErrorCode = BCC_INVALID_OPERATION;
Logan7d2219f2011-01-06 06:19:25 +0800747 LOGE("Invalid operation: %s\n", __func__);
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800748 return 1;
Logancf3e5212010-12-29 01:44:55 +0800749 }
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800750 return 0;
Logancf3e5212010-12-29 01:44:55 +0800751}
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700752
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700753#if USE_MCJIT
754size_t Script::getELFSize() const {
755 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700756 case ScriptStatus::Compiled: {
757 return mCompiled->getELFSize();
758 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700759
Shih-wei Liaod50be322011-07-01 22:53:31 -0700760 default: {
761 return 0;
762 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700763 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700764}
765
766const char *Script::getELF() const {
767 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700768 case ScriptStatus::Compiled: {
769 return mCompiled->getELF();
770 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700771
Shih-wei Liaod50be322011-07-01 22:53:31 -0700772 default: {
773 return NULL;
774 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700775 }
776}
777#endif
Logancf3e5212010-12-29 01:44:55 +0800778
779} // namespace bcc