blob: 2f33c5127171af9c82d8f5ae53f5fed5efb78040 [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,
61 const BCCchar *resName) {
62 return script->compiler.readBC(bitcode, size, resName);
63}
Zonr Chang932648d2010-10-13 22:23:56 +080064
Logan3f3d31f2010-11-27 13:52:03 +080065extern "C" void bccLinkBC(BCCscript *script,
66 const BCCchar *bitcode,
67 BCCint size) {
68 script->compiler.linkBC(bitcode, size);
69}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070070
Logan3f3d31f2010-11-27 13:52:03 +080071extern "C" int bccLoadBinary(BCCscript *script) {
72 int result = script->compiler.loadCacheFile();
Shih-wei Liaod6d488c2010-12-04 18:23:50 -080073
74#if defined(USE_DISASSEMBLER_FILE)
75 LOGI("[LoadBinary] result=%d", result);
76#endif
77 if (result) {
Logan3f3d31f2010-11-27 13:52:03 +080078 script->setError(BCC_INVALID_OPERATION);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -080079 }
80
Logan3f3d31f2010-11-27 13:52:03 +080081 return result;
82}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070083
Logand80e65b2010-12-03 21:28:04 +080084extern "C" int bccCompileBC(BCCscript *script) {
Logan3f3d31f2010-11-27 13:52:03 +080085#if defined(__arm__)
Logand80e65b2010-12-03 21:28:04 +080086 android::StopWatch compileTimer("RenderScript compile time");
Logan3f3d31f2010-11-27 13:52:03 +080087#endif
Logand80e65b2010-12-03 21:28:04 +080088
89 int result = script->compiler.compile();
90 if (result)
91 script->setError(BCC_INVALID_OPERATION);
92
93 return result;
Logan3f3d31f2010-11-27 13:52:03 +080094}
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080095
Logan3f3d31f2010-11-27 13:52:03 +080096extern "C" void bccGetScriptInfoLog(BCCscript *script,
97 BCCsizei maxLength,
98 BCCsizei *length,
99 BCCchar *infoLog) {
100 char *message = script->compiler.getErrorMessage();
101 int messageLength = strlen(message) + 1;
102 if (length)
103 *length = messageLength;
104
105 if (infoLog && maxLength > 0) {
106 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
107 memcpy(infoLog, message, trimmedLength);
108 infoLog[trimmedLength] = 0;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800109#if defined(USE_DISASSEMBLER_FILE)
110 LOGI("[GetScriptInfoLog] %s", infoLog);
111#endif
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700112 }
Logan3f3d31f2010-11-27 13:52:03 +0800113}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700114
Logan3f3d31f2010-11-27 13:52:03 +0800115extern "C" void bccGetScriptLabel(BCCscript *script,
116 const BCCchar *name,
117 BCCvoid **address) {
118 void *value = script->compiler.lookup(name);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800119 if (value) {
Logan3f3d31f2010-11-27 13:52:03 +0800120 *address = value;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800121#if defined(USE_DISASSEMBLER_FILE)
122 LOGI("[GetScriptLabel] %s @ 0x%x", name, value);
123#endif
124 } else {
Logan3f3d31f2010-11-27 13:52:03 +0800125 script->setError(BCC_INVALID_VALUE);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800126 }
Logan3f3d31f2010-11-27 13:52:03 +0800127}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700128
Logan3f3d31f2010-11-27 13:52:03 +0800129extern "C" void bccGetExportVars(BCCscript *script,
130 BCCsizei *actualVarCount,
131 BCCsizei maxVarCount,
132 BCCvoid **vars) {
133 script->compiler.getExportVars(actualVarCount, maxVarCount, vars);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800134
135#if defined(USE_DISASSEMBLER_FILE)
136 int i;
137 if (actualVarCount) {
138 LOGI("[ExportVars] #=%d:", *actualVarCount);
139 } else {
140 for (i = 0; i < maxVarCount; i++) {
141 LOGI("[ExportVars] #%d=0x%x", i, vars[i]);
142 }
143 }
144#endif
Logan3f3d31f2010-11-27 13:52:03 +0800145}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700146
Logan3f3d31f2010-11-27 13:52:03 +0800147extern "C" void bccGetExportFuncs(BCCscript *script,
148 BCCsizei *actualFuncCount,
149 BCCsizei maxFuncCount,
150 BCCvoid **funcs) {
151 script->compiler.getExportFuncs(actualFuncCount, maxFuncCount, funcs);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800152
153#if defined(USE_DISASSEMBLER_FILE)
154 int i;
155 if (actualFuncCount) {
156 LOGI("[ExportFunc] #=%d:", *actualFuncCount);
157 } else {
158 for (i = 0; i < maxFuncCount; i++) {
159 LOGI("[ExportFunc] #%d=0x%x", i, funcs[i]);
160 }
161 }
162#endif
Logan3f3d31f2010-11-27 13:52:03 +0800163}
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700164
Logan3f3d31f2010-11-27 13:52:03 +0800165extern "C" void bccGetPragmas(BCCscript *script,
166 BCCsizei *actualStringCount,
167 BCCsizei maxStringCount,
168 BCCchar **strings) {
169 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800170
171#if defined(USE_DISASSEMBLER_FILE)
172 int i;
173 LOGI("[Pragma] #=%d:", *actualStringCount);
174 for (i = 0; i < *actualStringCount; i++) {
175 LOGI(" %s", strings[i]);
176 }
177#endif
Logan3f3d31f2010-11-27 13:52:03 +0800178}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700179
Logan3f3d31f2010-11-27 13:52:03 +0800180extern "C" void bccGetFunctions(BCCscript *script,
181 BCCsizei *actualFunctionCount,
182 BCCsizei maxFunctionCount,
183 BCCchar **functions) {
184 script->compiler.getFunctions(actualFunctionCount,
185 maxFunctionCount,
186 functions);
187}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700188
Logan3f3d31f2010-11-27 13:52:03 +0800189extern "C" void bccGetFunctionBinary(BCCscript *script,
190 BCCchar *function,
191 BCCvoid **base,
192 BCCsizei *length) {
193 script->compiler.getFunctionBinary(function, base, length);
194}