blob: e5941ad30506e90c4ad0d9dca6a8d2166dff0bc3 [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
Logan3c01aaa2011-01-01 01:40:39 +080030namespace bcc {
31 class FuncLogger {
32 private:
33 char const *mFuncName;
34
35 public:
36 FuncLogger(char const *name) : mFuncName(name) {
Shih-wei Liao707a2942011-01-03 23:08:20 +080037 // LOGI("---> BEGIN: libbcc [ %s ]\n", name);
Logan3c01aaa2011-01-01 01:40:39 +080038 }
39
40 ~FuncLogger() {
Shih-wei Liao707a2942011-01-03 23:08:20 +080041 // LOGI("---> END: libbcc [ %s ]\n", mFuncName);
Logan3c01aaa2011-01-01 01:40:39 +080042 }
43 };
44
45#define BCC_FUNC_LOGGER() bcc::FuncLogger XX__funcLogger(__FUNCTION__)
46} // namespace bcc
47
Shih-wei Liao77ed6142010-04-07 12:21:42 -070048
Logan39a2ca52010-11-27 01:03:06 +080049namespace llvm {
50 class Module;
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080051}
52
Logan39a2ca52010-11-27 01:03:06 +080053
Logan3f3d31f2010-11-27 13:52:03 +080054extern "C" BCCscript *bccCreateScript() {
Logan3c01aaa2011-01-01 01:40:39 +080055 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +080056 return new BCCscript();
57}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070058
Logan3f3d31f2010-11-27 13:52:03 +080059extern "C" BCCenum bccGetError(BCCscript *script) {
Logan3c01aaa2011-01-01 01:40:39 +080060 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +080061 return script->getError();
62}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070063
Logan3f3d31f2010-11-27 13:52:03 +080064extern "C" void bccDeleteScript(BCCscript *script) {
Logan3c01aaa2011-01-01 01:40:39 +080065 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +080066 delete script;
67}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070068
Logan3f3d31f2010-11-27 13:52:03 +080069extern "C" void bccRegisterSymbolCallback(BCCscript *script,
70 BCCSymbolLookupFn pFn,
71 BCCvoid *pContext) {
Logan3c01aaa2011-01-01 01:40:39 +080072 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +080073 script->registerSymbolCallback(pFn, pContext);
74}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070075
Logan3f3d31f2010-11-27 13:52:03 +080076extern "C" int bccReadModule(BCCscript *script, BCCvoid *module) {
Logan3c01aaa2011-01-01 01:40:39 +080077 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +080078 return script->readModule(reinterpret_cast<llvm::Module*>(module));
Logan3f3d31f2010-11-27 13:52:03 +080079}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070080
Logan3f3d31f2010-11-27 13:52:03 +080081extern "C" int bccReadBC(BCCscript *script,
82 const BCCchar *bitcode,
Loganb9b04162010-12-20 16:36:57 +080083 BCCint bitcodeSize,
Logan9d547692011-01-06 05:26:34 +080084 long __DONT_USE_PARAM_1,
85 long __DONT_USE_PARAM_2,
Shih-wei Liaoe6a18512010-12-09 12:38:10 -080086 const BCCchar *resName,
87 const BCCchar *cacheDir) {
Logan3c01aaa2011-01-01 01:40:39 +080088 BCC_FUNC_LOGGER();
Loganf30a6712011-01-06 05:55:34 +080089 return script->readBC(bitcode, bitcodeSize, resName, cacheDir);
Logan3f3d31f2010-11-27 13:52:03 +080090}
Zonr Chang932648d2010-10-13 22:23:56 +080091
Logan3f3d31f2010-11-27 13:52:03 +080092extern "C" void bccLinkBC(BCCscript *script,
93 const BCCchar *bitcode,
94 BCCint size) {
Logan3c01aaa2011-01-01 01:40:39 +080095 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +080096 script->linkBC(bitcode, size);
Logan3f3d31f2010-11-27 13:52:03 +080097}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070098
Logan3f3d31f2010-11-27 13:52:03 +080099extern "C" int bccLoadBinary(BCCscript *script) {
Loganecf4cbd2011-01-06 05:34:11 +0800100 LOGE("bccLoadBinary is deprecated **************************\n");
101 return 1;
Logan3f3d31f2010-11-27 13:52:03 +0800102}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700103
Shih-wei Liaof6267d12011-01-07 19:23:58 -0800104extern "C" int bccPrepareExecutable(BCCscript *script) {
Logan3c01aaa2011-01-01 01:40:39 +0800105 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +0800106#if defined(__arm__)
Shih-wei Liaof6267d12011-01-07 19:23:58 -0800107 android::StopWatch compileTimer("bcc: PrepareExecutable time");
Logan3f3d31f2010-11-27 13:52:03 +0800108#endif
Logand80e65b2010-12-03 21:28:04 +0800109
Loganeaa0cc32010-12-29 01:04:20 +0800110 int result = script->compile();
Logand80e65b2010-12-03 21:28:04 +0800111 if (result)
112 script->setError(BCC_INVALID_OPERATION);
113
114 return result;
Logan3f3d31f2010-11-27 13:52:03 +0800115}
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -0800116
Logan3f3d31f2010-11-27 13:52:03 +0800117extern "C" void bccGetScriptInfoLog(BCCscript *script,
118 BCCsizei maxLength,
119 BCCsizei *length,
120 BCCchar *infoLog) {
Logan3c01aaa2011-01-01 01:40:39 +0800121 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800122 char const *message = script->getCompilerErrorMessage();
Logan3f3d31f2010-11-27 13:52:03 +0800123 int messageLength = strlen(message) + 1;
124 if (length)
125 *length = messageLength;
126
127 if (infoLog && maxLength > 0) {
128 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
129 memcpy(infoLog, message, trimmedLength);
130 infoLog[trimmedLength] = 0;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800131#if defined(USE_DISASSEMBLER_FILE)
132 LOGI("[GetScriptInfoLog] %s", infoLog);
133#endif
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700134 }
Logan3f3d31f2010-11-27 13:52:03 +0800135}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700136
Logan3f3d31f2010-11-27 13:52:03 +0800137extern "C" void bccGetScriptLabel(BCCscript *script,
138 const BCCchar *name,
139 BCCvoid **address) {
Logan3c01aaa2011-01-01 01:40:39 +0800140 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800141 void *value = script->lookup(name);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800142 if (value) {
Logan3f3d31f2010-11-27 13:52:03 +0800143 *address = value;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800144#if defined(USE_DISASSEMBLER_FILE)
145 LOGI("[GetScriptLabel] %s @ 0x%x", name, value);
146#endif
147 } else {
Logan3f3d31f2010-11-27 13:52:03 +0800148 script->setError(BCC_INVALID_VALUE);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800149 }
Logan3f3d31f2010-11-27 13:52:03 +0800150}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700151
Logan3f3d31f2010-11-27 13:52:03 +0800152extern "C" void bccGetExportVars(BCCscript *script,
153 BCCsizei *actualVarCount,
154 BCCsizei maxVarCount,
155 BCCvoid **vars) {
Logan3c01aaa2011-01-01 01:40:39 +0800156 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800157 script->getExportVars(actualVarCount, maxVarCount, vars);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800158
159#if defined(USE_DISASSEMBLER_FILE)
160 int i;
161 if (actualVarCount) {
162 LOGI("[ExportVars] #=%d:", *actualVarCount);
163 } else {
164 for (i = 0; i < maxVarCount; i++) {
165 LOGI("[ExportVars] #%d=0x%x", i, vars[i]);
166 }
167 }
168#endif
Logan3f3d31f2010-11-27 13:52:03 +0800169}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700170
Logan3f3d31f2010-11-27 13:52:03 +0800171extern "C" void bccGetExportFuncs(BCCscript *script,
172 BCCsizei *actualFuncCount,
173 BCCsizei maxFuncCount,
174 BCCvoid **funcs) {
Logan3c01aaa2011-01-01 01:40:39 +0800175 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800176 script->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800177
178#if defined(USE_DISASSEMBLER_FILE)
179 int i;
180 if (actualFuncCount) {
181 LOGI("[ExportFunc] #=%d:", *actualFuncCount);
182 } else {
183 for (i = 0; i < maxFuncCount; i++) {
184 LOGI("[ExportFunc] #%d=0x%x", i, funcs[i]);
185 }
186 }
187#endif
Logan3f3d31f2010-11-27 13:52:03 +0800188}
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700189
Logan3f3d31f2010-11-27 13:52:03 +0800190extern "C" void bccGetPragmas(BCCscript *script,
191 BCCsizei *actualStringCount,
192 BCCsizei maxStringCount,
193 BCCchar **strings) {
Logan3c01aaa2011-01-01 01:40:39 +0800194 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800195 script->getPragmas(actualStringCount, maxStringCount, strings);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800196
197#if defined(USE_DISASSEMBLER_FILE)
198 int i;
199 LOGI("[Pragma] #=%d:", *actualStringCount);
200 for (i = 0; i < *actualStringCount; i++) {
201 LOGI(" %s", strings[i]);
202 }
203#endif
Logan3f3d31f2010-11-27 13:52:03 +0800204}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700205
Logan3f3d31f2010-11-27 13:52:03 +0800206extern "C" void bccGetFunctions(BCCscript *script,
207 BCCsizei *actualFunctionCount,
208 BCCsizei maxFunctionCount,
209 BCCchar **functions) {
Logan3c01aaa2011-01-01 01:40:39 +0800210 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800211 script->getFunctions(actualFunctionCount,
Logan3f3d31f2010-11-27 13:52:03 +0800212 maxFunctionCount,
213 functions);
214}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700215
Logan3f3d31f2010-11-27 13:52:03 +0800216extern "C" void bccGetFunctionBinary(BCCscript *script,
217 BCCchar *function,
218 BCCvoid **base,
219 BCCsizei *length) {
Logan3c01aaa2011-01-01 01:40:39 +0800220 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800221 script->getFunctionBinary(function, base, length);
Logan3f3d31f2010-11-27 13:52:03 +0800222}