blob: f88c1e070c697b7e7711570ba67c51e396a3cecf [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
Shih-wei Liao4079b592011-01-11 04:54:13 -080092extern "C" int bccLinkBC(BCCscript *script,
93 const BCCchar *bitcode,
94 BCCint size) {
Logan3c01aaa2011-01-01 01:40:39 +080095 BCC_FUNC_LOGGER();
Shih-wei Liao4079b592011-01-11 04:54:13 -080096 return script->linkBC(bitcode, size);
Logan3f3d31f2010-11-27 13:52:03 +080097}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070098
Shih-wei Liaof6267d12011-01-07 19:23:58 -080099extern "C" int bccPrepareExecutable(BCCscript *script) {
Logan3c01aaa2011-01-01 01:40:39 +0800100 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +0800101#if defined(__arm__)
Shih-wei Liaof6267d12011-01-07 19:23:58 -0800102 android::StopWatch compileTimer("bcc: PrepareExecutable time");
Logan3f3d31f2010-11-27 13:52:03 +0800103#endif
Logand80e65b2010-12-03 21:28:04 +0800104
Loganeaa0cc32010-12-29 01:04:20 +0800105 int result = script->compile();
Logand80e65b2010-12-03 21:28:04 +0800106 if (result)
107 script->setError(BCC_INVALID_OPERATION);
108
109 return result;
Logan3f3d31f2010-11-27 13:52:03 +0800110}
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -0800111
Logan3f3d31f2010-11-27 13:52:03 +0800112extern "C" void bccGetScriptInfoLog(BCCscript *script,
113 BCCsizei maxLength,
114 BCCsizei *length,
115 BCCchar *infoLog) {
Logan3c01aaa2011-01-01 01:40:39 +0800116 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800117 char const *message = script->getCompilerErrorMessage();
Logan3f3d31f2010-11-27 13:52:03 +0800118 int messageLength = strlen(message) + 1;
119 if (length)
120 *length = messageLength;
121
122 if (infoLog && maxLength > 0) {
123 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
124 memcpy(infoLog, message, trimmedLength);
125 infoLog[trimmedLength] = 0;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800126#if defined(USE_DISASSEMBLER_FILE)
127 LOGI("[GetScriptInfoLog] %s", infoLog);
128#endif
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700129 }
Logan3f3d31f2010-11-27 13:52:03 +0800130}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700131
Logan3f3d31f2010-11-27 13:52:03 +0800132extern "C" void bccGetScriptLabel(BCCscript *script,
133 const BCCchar *name,
134 BCCvoid **address) {
Logan3c01aaa2011-01-01 01:40:39 +0800135 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800136 void *value = script->lookup(name);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800137 if (value) {
Logan3f3d31f2010-11-27 13:52:03 +0800138 *address = value;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800139#if defined(USE_DISASSEMBLER_FILE)
140 LOGI("[GetScriptLabel] %s @ 0x%x", name, value);
141#endif
142 } else {
Logan3f3d31f2010-11-27 13:52:03 +0800143 script->setError(BCC_INVALID_VALUE);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800144 }
Logan3f3d31f2010-11-27 13:52:03 +0800145}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700146
Logan3f3d31f2010-11-27 13:52:03 +0800147extern "C" void bccGetExportVars(BCCscript *script,
148 BCCsizei *actualVarCount,
149 BCCsizei maxVarCount,
150 BCCvoid **vars) {
Logan3c01aaa2011-01-01 01:40:39 +0800151 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800152 script->getExportVars(actualVarCount, maxVarCount, vars);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800153
154#if defined(USE_DISASSEMBLER_FILE)
155 int i;
156 if (actualVarCount) {
157 LOGI("[ExportVars] #=%d:", *actualVarCount);
158 } else {
159 for (i = 0; i < maxVarCount; i++) {
160 LOGI("[ExportVars] #%d=0x%x", i, vars[i]);
161 }
162 }
163#endif
Logan3f3d31f2010-11-27 13:52:03 +0800164}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700165
Logan3f3d31f2010-11-27 13:52:03 +0800166extern "C" void bccGetExportFuncs(BCCscript *script,
167 BCCsizei *actualFuncCount,
168 BCCsizei maxFuncCount,
169 BCCvoid **funcs) {
Logan3c01aaa2011-01-01 01:40:39 +0800170 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800171 script->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800172
173#if defined(USE_DISASSEMBLER_FILE)
174 int i;
175 if (actualFuncCount) {
176 LOGI("[ExportFunc] #=%d:", *actualFuncCount);
177 } else {
178 for (i = 0; i < maxFuncCount; i++) {
179 LOGI("[ExportFunc] #%d=0x%x", i, funcs[i]);
180 }
181 }
182#endif
Logan3f3d31f2010-11-27 13:52:03 +0800183}
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700184
Logan3f3d31f2010-11-27 13:52:03 +0800185extern "C" void bccGetPragmas(BCCscript *script,
186 BCCsizei *actualStringCount,
187 BCCsizei maxStringCount,
188 BCCchar **strings) {
Logan3c01aaa2011-01-01 01:40:39 +0800189 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800190 script->getPragmas(actualStringCount, maxStringCount, strings);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800191
192#if defined(USE_DISASSEMBLER_FILE)
193 int i;
194 LOGI("[Pragma] #=%d:", *actualStringCount);
195 for (i = 0; i < *actualStringCount; i++) {
196 LOGI(" %s", strings[i]);
197 }
198#endif
Logan3f3d31f2010-11-27 13:52:03 +0800199}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700200
Logan3f3d31f2010-11-27 13:52:03 +0800201extern "C" void bccGetFunctions(BCCscript *script,
202 BCCsizei *actualFunctionCount,
203 BCCsizei maxFunctionCount,
204 BCCchar **functions) {
Logan3c01aaa2011-01-01 01:40:39 +0800205 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800206 script->getFunctions(actualFunctionCount,
Logan3f3d31f2010-11-27 13:52:03 +0800207 maxFunctionCount,
208 functions);
209}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700210
Logan3f3d31f2010-11-27 13:52:03 +0800211extern "C" void bccGetFunctionBinary(BCCscript *script,
212 BCCchar *function,
213 BCCvoid **base,
214 BCCsizei *length) {
Logan3c01aaa2011-01-01 01:40:39 +0800215 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800216 script->getFunctionBinary(function, base, length);
Logan3f3d31f2010-11-27 13:52:03 +0800217}