blob: 9efa0206aeb1015ed3522009fb286815cc15e396 [file] [log] [blame]
Shih-wei Liao77ed6142010-04-07 12:21:42 -07001/*
Zonr Chang932648d2010-10-13 22:23:56 +08002 * Copyright 2010, The Android Open Source Project
Shih-wei Liao77ed6142010-04-07 12:21:42 -07003 *
Zonr Chang932648d2010-10-13 22:23:56 +08004 * 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.
Shih-wei Liao77ed6142010-04-07 12:21:42 -070015 */
16
Zonr Chang932648d2010-10-13 22:23:56 +080017// Bitcode compiler (bcc) for Android:
18// This is an eager-compilation JIT running on Android.
19
Shih-wei Liao77ed6142010-04-07 12:21:42 -070020#define LOG_TAG "bcc"
21#include <cutils/log.h>
22
Loganc4395232010-11-27 18:54:17 +080023#include "Compiler.h"
24#include "Script.h"
Shih-wei Liao77ed6142010-04-07 12:21:42 -070025
Loganc4395232010-11-27 18:54:17 +080026#include <bcc/bcc.h>
Logan39a2ca52010-11-27 01:03:06 +080027
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080028#include <utils/StopWatch.h>
Shih-wei Liao77ed6142010-04-07 12:21:42 -070029
Logane1323992011-01-12 04:47:13 +080030char const libbcc_build_time[24] = __DATE__ " " __TIME__;
31
Logan3c01aaa2011-01-01 01:40:39 +080032namespace bcc {
33 class FuncLogger {
34 private:
35 char const *mFuncName;
36
37 public:
38 FuncLogger(char const *name) : mFuncName(name) {
Shih-wei Liao707a2942011-01-03 23:08:20 +080039 // LOGI("---> BEGIN: libbcc [ %s ]\n", name);
Logan3c01aaa2011-01-01 01:40:39 +080040 }
41
42 ~FuncLogger() {
Shih-wei Liao707a2942011-01-03 23:08:20 +080043 // LOGI("---> END: libbcc [ %s ]\n", mFuncName);
Logan3c01aaa2011-01-01 01:40:39 +080044 }
45 };
46
47#define BCC_FUNC_LOGGER() bcc::FuncLogger XX__funcLogger(__FUNCTION__)
48} // namespace bcc
49
Shih-wei Liao77ed6142010-04-07 12:21:42 -070050
Logan39a2ca52010-11-27 01:03:06 +080051namespace llvm {
52 class Module;
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080053}
54
Logan39a2ca52010-11-27 01:03:06 +080055
Logan3f3d31f2010-11-27 13:52:03 +080056extern "C" BCCscript *bccCreateScript() {
Logan3c01aaa2011-01-01 01:40:39 +080057 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +080058 return new BCCscript();
59}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070060
Logan3f3d31f2010-11-27 13:52:03 +080061extern "C" BCCenum bccGetError(BCCscript *script) {
Logan3c01aaa2011-01-01 01:40:39 +080062 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +080063 return script->getError();
64}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070065
Logan3f3d31f2010-11-27 13:52:03 +080066extern "C" void bccDeleteScript(BCCscript *script) {
Logan3c01aaa2011-01-01 01:40:39 +080067 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +080068 delete script;
69}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070070
Logan3f3d31f2010-11-27 13:52:03 +080071extern "C" void bccRegisterSymbolCallback(BCCscript *script,
72 BCCSymbolLookupFn pFn,
73 BCCvoid *pContext) {
Logan3c01aaa2011-01-01 01:40:39 +080074 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +080075 script->registerSymbolCallback(pFn, pContext);
76}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070077
Logan3f3d31f2010-11-27 13:52:03 +080078extern "C" int bccReadModule(BCCscript *script, BCCvoid *module) {
Logan3c01aaa2011-01-01 01:40:39 +080079 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +080080 return script->readModule(reinterpret_cast<llvm::Module*>(module));
Logan3f3d31f2010-11-27 13:52:03 +080081}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070082
Logan3f3d31f2010-11-27 13:52:03 +080083extern "C" int bccReadBC(BCCscript *script,
84 const BCCchar *bitcode,
Loganb9b04162010-12-20 16:36:57 +080085 BCCint bitcodeSize,
Logan9d547692011-01-06 05:26:34 +080086 long __DONT_USE_PARAM_1,
87 long __DONT_USE_PARAM_2,
Shih-wei Liaoe6a18512010-12-09 12:38:10 -080088 const BCCchar *resName,
89 const BCCchar *cacheDir) {
Logan3c01aaa2011-01-01 01:40:39 +080090 BCC_FUNC_LOGGER();
Loganf30a6712011-01-06 05:55:34 +080091 return script->readBC(bitcode, bitcodeSize, resName, cacheDir);
Logan3f3d31f2010-11-27 13:52:03 +080092}
Zonr Chang932648d2010-10-13 22:23:56 +080093
Shih-wei Liao4079b592011-01-11 04:54:13 -080094extern "C" int bccLinkBC(BCCscript *script,
95 const BCCchar *bitcode,
96 BCCint size) {
Logan3c01aaa2011-01-01 01:40:39 +080097 BCC_FUNC_LOGGER();
Shih-wei Liao4079b592011-01-11 04:54:13 -080098 return script->linkBC(bitcode, size);
Logan3f3d31f2010-11-27 13:52:03 +080099}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700100
Shih-wei Liaof6267d12011-01-07 19:23:58 -0800101extern "C" int bccPrepareExecutable(BCCscript *script) {
Logan3c01aaa2011-01-01 01:40:39 +0800102 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +0800103#if defined(__arm__)
Shih-wei Liaof6267d12011-01-07 19:23:58 -0800104 android::StopWatch compileTimer("bcc: PrepareExecutable time");
Logan3f3d31f2010-11-27 13:52:03 +0800105#endif
Logand80e65b2010-12-03 21:28:04 +0800106
Loganeaa0cc32010-12-29 01:04:20 +0800107 int result = script->compile();
Logand80e65b2010-12-03 21:28:04 +0800108 if (result)
109 script->setError(BCC_INVALID_OPERATION);
110
111 return result;
Logan3f3d31f2010-11-27 13:52:03 +0800112}
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -0800113
Logan3f3d31f2010-11-27 13:52:03 +0800114extern "C" void bccGetScriptInfoLog(BCCscript *script,
115 BCCsizei maxLength,
116 BCCsizei *length,
117 BCCchar *infoLog) {
Logan3c01aaa2011-01-01 01:40:39 +0800118 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800119 char const *message = script->getCompilerErrorMessage();
Logan3f3d31f2010-11-27 13:52:03 +0800120 int messageLength = strlen(message) + 1;
121 if (length)
122 *length = messageLength;
123
124 if (infoLog && maxLength > 0) {
125 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
126 memcpy(infoLog, message, trimmedLength);
127 infoLog[trimmedLength] = 0;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800128#if defined(USE_DISASSEMBLER_FILE)
129 LOGI("[GetScriptInfoLog] %s", infoLog);
130#endif
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700131 }
Logan3f3d31f2010-11-27 13:52:03 +0800132}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700133
Logan3f3d31f2010-11-27 13:52:03 +0800134extern "C" void bccGetScriptLabel(BCCscript *script,
135 const BCCchar *name,
136 BCCvoid **address) {
Logan3c01aaa2011-01-01 01:40:39 +0800137 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800138 void *value = script->lookup(name);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800139 if (value) {
Logan3f3d31f2010-11-27 13:52:03 +0800140 *address = value;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800141#if defined(USE_DISASSEMBLER_FILE)
142 LOGI("[GetScriptLabel] %s @ 0x%x", name, value);
143#endif
144 } else {
Logan3f3d31f2010-11-27 13:52:03 +0800145 script->setError(BCC_INVALID_VALUE);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800146 }
Logan3f3d31f2010-11-27 13:52:03 +0800147}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700148
Logan3f3d31f2010-11-27 13:52:03 +0800149extern "C" void bccGetExportVars(BCCscript *script,
150 BCCsizei *actualVarCount,
151 BCCsizei maxVarCount,
152 BCCvoid **vars) {
Logan3c01aaa2011-01-01 01:40:39 +0800153 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800154 script->getExportVars(actualVarCount, maxVarCount, vars);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800155
156#if defined(USE_DISASSEMBLER_FILE)
157 int i;
158 if (actualVarCount) {
159 LOGI("[ExportVars] #=%d:", *actualVarCount);
160 } else {
161 for (i = 0; i < maxVarCount; i++) {
162 LOGI("[ExportVars] #%d=0x%x", i, vars[i]);
163 }
164 }
165#endif
Logan3f3d31f2010-11-27 13:52:03 +0800166}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700167
Logan3f3d31f2010-11-27 13:52:03 +0800168extern "C" void bccGetExportFuncs(BCCscript *script,
169 BCCsizei *actualFuncCount,
170 BCCsizei maxFuncCount,
171 BCCvoid **funcs) {
Logan3c01aaa2011-01-01 01:40:39 +0800172 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800173 script->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800174
175#if defined(USE_DISASSEMBLER_FILE)
176 int i;
177 if (actualFuncCount) {
178 LOGI("[ExportFunc] #=%d:", *actualFuncCount);
179 } else {
180 for (i = 0; i < maxFuncCount; i++) {
181 LOGI("[ExportFunc] #%d=0x%x", i, funcs[i]);
182 }
183 }
184#endif
Logan3f3d31f2010-11-27 13:52:03 +0800185}
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700186
Logan3f3d31f2010-11-27 13:52:03 +0800187extern "C" void bccGetPragmas(BCCscript *script,
188 BCCsizei *actualStringCount,
189 BCCsizei maxStringCount,
190 BCCchar **strings) {
Logan3c01aaa2011-01-01 01:40:39 +0800191 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800192 script->getPragmas(actualStringCount, maxStringCount, strings);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800193
194#if defined(USE_DISASSEMBLER_FILE)
195 int i;
196 LOGI("[Pragma] #=%d:", *actualStringCount);
197 for (i = 0; i < *actualStringCount; i++) {
198 LOGI(" %s", strings[i]);
199 }
200#endif
Logan3f3d31f2010-11-27 13:52:03 +0800201}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700202
Logan3f3d31f2010-11-27 13:52:03 +0800203extern "C" void bccGetFunctions(BCCscript *script,
204 BCCsizei *actualFunctionCount,
205 BCCsizei maxFunctionCount,
206 BCCchar **functions) {
Logan3c01aaa2011-01-01 01:40:39 +0800207 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800208 script->getFunctions(actualFunctionCount,
Logan3f3d31f2010-11-27 13:52:03 +0800209 maxFunctionCount,
210 functions);
211}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700212
Logan3f3d31f2010-11-27 13:52:03 +0800213extern "C" void bccGetFunctionBinary(BCCscript *script,
214 BCCchar *function,
215 BCCvoid **base,
216 BCCsizei *length) {
Logan3c01aaa2011-01-01 01:40:39 +0800217 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800218 script->getFunctionBinary(function, base, length);
Logan3f3d31f2010-11-27 13:52:03 +0800219}