blob: 7bd463380e6e81867bd5e4c5627fb67d76aa01a7 [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,
84 long bitcodeFileModTime,
85 long bitcodeFileCRC32,
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();
Loganeaa0cc32010-12-29 01:04:20 +080089 return script->readBC(bitcode, bitcodeSize,
90 bitcodeFileModTime, bitcodeFileCRC32,
91 resName, cacheDir);
Logan3f3d31f2010-11-27 13:52:03 +080092}
Zonr Chang932648d2010-10-13 22:23:56 +080093
Logan3f3d31f2010-11-27 13:52:03 +080094extern "C" void bccLinkBC(BCCscript *script,
95 const BCCchar *bitcode,
96 BCCint size) {
Logan3c01aaa2011-01-01 01:40:39 +080097 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +080098 script->linkBC(bitcode, size);
Logan3f3d31f2010-11-27 13:52:03 +080099}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700100
Logan3f3d31f2010-11-27 13:52:03 +0800101extern "C" int bccLoadBinary(BCCscript *script) {
Logan3c01aaa2011-01-01 01:40:39 +0800102 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800103 int result = script->loadCacheFile();
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800104
105#if defined(USE_DISASSEMBLER_FILE)
106 LOGI("[LoadBinary] result=%d", result);
107#endif
108 if (result) {
Logan3f3d31f2010-11-27 13:52:03 +0800109 script->setError(BCC_INVALID_OPERATION);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800110 }
111
Logan3f3d31f2010-11-27 13:52:03 +0800112 return result;
113}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700114
Logand80e65b2010-12-03 21:28:04 +0800115extern "C" int bccCompileBC(BCCscript *script) {
Logan3c01aaa2011-01-01 01:40:39 +0800116 BCC_FUNC_LOGGER();
Logan3f3d31f2010-11-27 13:52:03 +0800117#if defined(__arm__)
Logand80e65b2010-12-03 21:28:04 +0800118 android::StopWatch compileTimer("RenderScript compile time");
Logan3f3d31f2010-11-27 13:52:03 +0800119#endif
Logand80e65b2010-12-03 21:28:04 +0800120
Loganeaa0cc32010-12-29 01:04:20 +0800121 int result = script->compile();
Logand80e65b2010-12-03 21:28:04 +0800122 if (result)
123 script->setError(BCC_INVALID_OPERATION);
124
125 return result;
Logan3f3d31f2010-11-27 13:52:03 +0800126}
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -0800127
Logan3f3d31f2010-11-27 13:52:03 +0800128extern "C" void bccGetScriptInfoLog(BCCscript *script,
129 BCCsizei maxLength,
130 BCCsizei *length,
131 BCCchar *infoLog) {
Logan3c01aaa2011-01-01 01:40:39 +0800132 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800133 char const *message = script->getCompilerErrorMessage();
Logan3f3d31f2010-11-27 13:52:03 +0800134 int messageLength = strlen(message) + 1;
135 if (length)
136 *length = messageLength;
137
138 if (infoLog && maxLength > 0) {
139 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
140 memcpy(infoLog, message, trimmedLength);
141 infoLog[trimmedLength] = 0;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800142#if defined(USE_DISASSEMBLER_FILE)
143 LOGI("[GetScriptInfoLog] %s", infoLog);
144#endif
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700145 }
Logan3f3d31f2010-11-27 13:52:03 +0800146}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700147
Logan3f3d31f2010-11-27 13:52:03 +0800148extern "C" void bccGetScriptLabel(BCCscript *script,
149 const BCCchar *name,
150 BCCvoid **address) {
Logan3c01aaa2011-01-01 01:40:39 +0800151 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800152 void *value = script->lookup(name);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800153 if (value) {
Logan3f3d31f2010-11-27 13:52:03 +0800154 *address = value;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800155#if defined(USE_DISASSEMBLER_FILE)
156 LOGI("[GetScriptLabel] %s @ 0x%x", name, value);
157#endif
158 } else {
Logan3f3d31f2010-11-27 13:52:03 +0800159 script->setError(BCC_INVALID_VALUE);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800160 }
Logan3f3d31f2010-11-27 13:52:03 +0800161}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700162
Logan3f3d31f2010-11-27 13:52:03 +0800163extern "C" void bccGetExportVars(BCCscript *script,
164 BCCsizei *actualVarCount,
165 BCCsizei maxVarCount,
166 BCCvoid **vars) {
Logan3c01aaa2011-01-01 01:40:39 +0800167 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800168 script->getExportVars(actualVarCount, maxVarCount, vars);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800169
170#if defined(USE_DISASSEMBLER_FILE)
171 int i;
172 if (actualVarCount) {
173 LOGI("[ExportVars] #=%d:", *actualVarCount);
174 } else {
175 for (i = 0; i < maxVarCount; i++) {
176 LOGI("[ExportVars] #%d=0x%x", i, vars[i]);
177 }
178 }
179#endif
Logan3f3d31f2010-11-27 13:52:03 +0800180}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700181
Logan3f3d31f2010-11-27 13:52:03 +0800182extern "C" void bccGetExportFuncs(BCCscript *script,
183 BCCsizei *actualFuncCount,
184 BCCsizei maxFuncCount,
185 BCCvoid **funcs) {
Logan3c01aaa2011-01-01 01:40:39 +0800186 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800187 script->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800188
189#if defined(USE_DISASSEMBLER_FILE)
190 int i;
191 if (actualFuncCount) {
192 LOGI("[ExportFunc] #=%d:", *actualFuncCount);
193 } else {
194 for (i = 0; i < maxFuncCount; i++) {
195 LOGI("[ExportFunc] #%d=0x%x", i, funcs[i]);
196 }
197 }
198#endif
Logan3f3d31f2010-11-27 13:52:03 +0800199}
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700200
Logan3f3d31f2010-11-27 13:52:03 +0800201extern "C" void bccGetPragmas(BCCscript *script,
202 BCCsizei *actualStringCount,
203 BCCsizei maxStringCount,
204 BCCchar **strings) {
Logan3c01aaa2011-01-01 01:40:39 +0800205 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800206 script->getPragmas(actualStringCount, maxStringCount, strings);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800207
208#if defined(USE_DISASSEMBLER_FILE)
209 int i;
210 LOGI("[Pragma] #=%d:", *actualStringCount);
211 for (i = 0; i < *actualStringCount; i++) {
212 LOGI(" %s", strings[i]);
213 }
214#endif
Logan3f3d31f2010-11-27 13:52:03 +0800215}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700216
Logan3f3d31f2010-11-27 13:52:03 +0800217extern "C" void bccGetFunctions(BCCscript *script,
218 BCCsizei *actualFunctionCount,
219 BCCsizei maxFunctionCount,
220 BCCchar **functions) {
Logan3c01aaa2011-01-01 01:40:39 +0800221 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800222 script->getFunctions(actualFunctionCount,
Logan3f3d31f2010-11-27 13:52:03 +0800223 maxFunctionCount,
224 functions);
225}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700226
Logan3f3d31f2010-11-27 13:52:03 +0800227extern "C" void bccGetFunctionBinary(BCCscript *script,
228 BCCchar *function,
229 BCCvoid **base,
230 BCCsizei *length) {
Logan3c01aaa2011-01-01 01:40:39 +0800231 BCC_FUNC_LOGGER();
Loganeaa0cc32010-12-29 01:04:20 +0800232 script->getFunctionBinary(function, base, length);
Logan3f3d31f2010-11-27 13:52:03 +0800233}