blob: ca4c880dc67164e29b8dee9f7f6748f912a2691b [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
Shih-wei Liao77ed6142010-04-07 12:21:42 -070030
Logan39a2ca52010-11-27 01:03:06 +080031namespace llvm {
32 class Module;
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080033}
34
Logan39a2ca52010-11-27 01:03:06 +080035
Logan3f3d31f2010-11-27 13:52:03 +080036extern "C" BCCscript *bccCreateScript() {
37 return new BCCscript();
38}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070039
Logan3f3d31f2010-11-27 13:52:03 +080040extern "C" BCCenum bccGetError(BCCscript *script) {
41 return script->getError();
42}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070043
Logan3f3d31f2010-11-27 13:52:03 +080044extern "C" void bccDeleteScript(BCCscript *script) {
45 delete script;
46}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070047
Logan3f3d31f2010-11-27 13:52:03 +080048extern "C" void bccRegisterSymbolCallback(BCCscript *script,
49 BCCSymbolLookupFn pFn,
50 BCCvoid *pContext) {
51 script->registerSymbolCallback(pFn, pContext);
52}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070053
Logan3f3d31f2010-11-27 13:52:03 +080054extern "C" int bccReadModule(BCCscript *script, BCCvoid *module) {
55 return script->compiler.readModule(reinterpret_cast<llvm::Module*>(module));
56}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070057
Logan3f3d31f2010-11-27 13:52:03 +080058extern "C" int bccReadBC(BCCscript *script,
59 const BCCchar *bitcode,
60 BCCint size,
Shih-wei Liaoe6a18512010-12-09 12:38:10 -080061 const BCCchar *resName,
62 const BCCchar *cacheDir) {
63 return script->compiler.readBC(bitcode, size, resName, cacheDir);
Logan3f3d31f2010-11-27 13:52:03 +080064}
Zonr Chang932648d2010-10-13 22:23:56 +080065
Logan3f3d31f2010-11-27 13:52:03 +080066extern "C" void bccLinkBC(BCCscript *script,
67 const BCCchar *bitcode,
68 BCCint size) {
69 script->compiler.linkBC(bitcode, size);
70}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070071
Logan3f3d31f2010-11-27 13:52:03 +080072extern "C" int bccLoadBinary(BCCscript *script) {
73 int result = script->compiler.loadCacheFile();
Shih-wei Liaod6d488c2010-12-04 18:23:50 -080074
75#if defined(USE_DISASSEMBLER_FILE)
76 LOGI("[LoadBinary] result=%d", result);
77#endif
78 if (result) {
Logan3f3d31f2010-11-27 13:52:03 +080079 script->setError(BCC_INVALID_OPERATION);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -080080 }
81
Logan3f3d31f2010-11-27 13:52:03 +080082 return result;
83}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070084
Logand80e65b2010-12-03 21:28:04 +080085extern "C" int bccCompileBC(BCCscript *script) {
Logan3f3d31f2010-11-27 13:52:03 +080086#if defined(__arm__)
Logand80e65b2010-12-03 21:28:04 +080087 android::StopWatch compileTimer("RenderScript compile time");
Logan3f3d31f2010-11-27 13:52:03 +080088#endif
Logand80e65b2010-12-03 21:28:04 +080089
90 int result = script->compiler.compile();
91 if (result)
92 script->setError(BCC_INVALID_OPERATION);
93
94 return result;
Logan3f3d31f2010-11-27 13:52:03 +080095}
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080096
Logan3f3d31f2010-11-27 13:52:03 +080097extern "C" void bccGetScriptInfoLog(BCCscript *script,
98 BCCsizei maxLength,
99 BCCsizei *length,
100 BCCchar *infoLog) {
101 char *message = script->compiler.getErrorMessage();
102 int messageLength = strlen(message) + 1;
103 if (length)
104 *length = messageLength;
105
106 if (infoLog && maxLength > 0) {
107 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
108 memcpy(infoLog, message, trimmedLength);
109 infoLog[trimmedLength] = 0;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800110#if defined(USE_DISASSEMBLER_FILE)
111 LOGI("[GetScriptInfoLog] %s", infoLog);
112#endif
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700113 }
Logan3f3d31f2010-11-27 13:52:03 +0800114}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700115
Logan3f3d31f2010-11-27 13:52:03 +0800116extern "C" void bccGetScriptLabel(BCCscript *script,
117 const BCCchar *name,
118 BCCvoid **address) {
119 void *value = script->compiler.lookup(name);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800120 if (value) {
Logan3f3d31f2010-11-27 13:52:03 +0800121 *address = value;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800122#if defined(USE_DISASSEMBLER_FILE)
123 LOGI("[GetScriptLabel] %s @ 0x%x", name, value);
124#endif
125 } else {
Logan3f3d31f2010-11-27 13:52:03 +0800126 script->setError(BCC_INVALID_VALUE);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800127 }
Logan3f3d31f2010-11-27 13:52:03 +0800128}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700129
Logan3f3d31f2010-11-27 13:52:03 +0800130extern "C" void bccGetExportVars(BCCscript *script,
131 BCCsizei *actualVarCount,
132 BCCsizei maxVarCount,
133 BCCvoid **vars) {
134 script->compiler.getExportVars(actualVarCount, maxVarCount, vars);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800135
136#if defined(USE_DISASSEMBLER_FILE)
137 int i;
138 if (actualVarCount) {
139 LOGI("[ExportVars] #=%d:", *actualVarCount);
140 } else {
141 for (i = 0; i < maxVarCount; i++) {
142 LOGI("[ExportVars] #%d=0x%x", i, vars[i]);
143 }
144 }
145#endif
Logan3f3d31f2010-11-27 13:52:03 +0800146}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700147
Logan3f3d31f2010-11-27 13:52:03 +0800148extern "C" void bccGetExportFuncs(BCCscript *script,
149 BCCsizei *actualFuncCount,
150 BCCsizei maxFuncCount,
151 BCCvoid **funcs) {
152 script->compiler.getExportFuncs(actualFuncCount, maxFuncCount, funcs);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800153
154#if defined(USE_DISASSEMBLER_FILE)
155 int i;
156 if (actualFuncCount) {
157 LOGI("[ExportFunc] #=%d:", *actualFuncCount);
158 } else {
159 for (i = 0; i < maxFuncCount; i++) {
160 LOGI("[ExportFunc] #%d=0x%x", i, funcs[i]);
161 }
162 }
163#endif
Logan3f3d31f2010-11-27 13:52:03 +0800164}
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700165
Logan3f3d31f2010-11-27 13:52:03 +0800166extern "C" void bccGetPragmas(BCCscript *script,
167 BCCsizei *actualStringCount,
168 BCCsizei maxStringCount,
169 BCCchar **strings) {
170 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800171
172#if defined(USE_DISASSEMBLER_FILE)
173 int i;
174 LOGI("[Pragma] #=%d:", *actualStringCount);
175 for (i = 0; i < *actualStringCount; i++) {
176 LOGI(" %s", strings[i]);
177 }
178#endif
Logan3f3d31f2010-11-27 13:52:03 +0800179}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700180
Logan3f3d31f2010-11-27 13:52:03 +0800181extern "C" void bccGetFunctions(BCCscript *script,
182 BCCsizei *actualFunctionCount,
183 BCCsizei maxFunctionCount,
184 BCCchar **functions) {
185 script->compiler.getFunctions(actualFunctionCount,
186 maxFunctionCount,
187 functions);
188}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700189
Logan3f3d31f2010-11-27 13:52:03 +0800190extern "C" void bccGetFunctionBinary(BCCscript *script,
191 BCCchar *function,
192 BCCvoid **base,
193 BCCsizei *length) {
194 script->compiler.getFunctionBinary(function, base, length);
195}