blob: 03a9306679ef84f914319a98abbfd28fbd369419 [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) {
Shih-wei Liao8f779972010-12-23 12:07:59 +080045 //LOGE("Script deleted"); sliao
Logan3f3d31f2010-11-27 13:52:03 +080046 delete script;
47}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070048
Logan3f3d31f2010-11-27 13:52:03 +080049extern "C" void bccRegisterSymbolCallback(BCCscript *script,
50 BCCSymbolLookupFn pFn,
51 BCCvoid *pContext) {
52 script->registerSymbolCallback(pFn, pContext);
53}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070054
Logan3f3d31f2010-11-27 13:52:03 +080055extern "C" int bccReadModule(BCCscript *script, BCCvoid *module) {
56 return script->compiler.readModule(reinterpret_cast<llvm::Module*>(module));
57}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070058
Logan3f3d31f2010-11-27 13:52:03 +080059extern "C" int bccReadBC(BCCscript *script,
60 const BCCchar *bitcode,
Loganb9b04162010-12-20 16:36:57 +080061 BCCint bitcodeSize,
62 long bitcodeFileModTime,
63 long bitcodeFileCRC32,
Shih-wei Liaoe6a18512010-12-09 12:38:10 -080064 const BCCchar *resName,
65 const BCCchar *cacheDir) {
Loganb9b04162010-12-20 16:36:57 +080066 return script->compiler.readBC(bitcode, bitcodeSize,
67 bitcodeFileModTime, bitcodeFileCRC32,
68 resName, cacheDir);
Logan3f3d31f2010-11-27 13:52:03 +080069}
Zonr Chang932648d2010-10-13 22:23:56 +080070
Logan3f3d31f2010-11-27 13:52:03 +080071extern "C" void bccLinkBC(BCCscript *script,
72 const BCCchar *bitcode,
73 BCCint size) {
74 script->compiler.linkBC(bitcode, size);
75}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070076
Logan3f3d31f2010-11-27 13:52:03 +080077extern "C" int bccLoadBinary(BCCscript *script) {
78 int result = script->compiler.loadCacheFile();
Shih-wei Liaod6d488c2010-12-04 18:23:50 -080079
80#if defined(USE_DISASSEMBLER_FILE)
81 LOGI("[LoadBinary] result=%d", result);
82#endif
83 if (result) {
Logan3f3d31f2010-11-27 13:52:03 +080084 script->setError(BCC_INVALID_OPERATION);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -080085 }
86
Logan3f3d31f2010-11-27 13:52:03 +080087 return result;
88}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070089
Logand80e65b2010-12-03 21:28:04 +080090extern "C" int bccCompileBC(BCCscript *script) {
Logan3f3d31f2010-11-27 13:52:03 +080091#if defined(__arm__)
Logand80e65b2010-12-03 21:28:04 +080092 android::StopWatch compileTimer("RenderScript compile time");
Logan3f3d31f2010-11-27 13:52:03 +080093#endif
Logand80e65b2010-12-03 21:28:04 +080094
95 int result = script->compiler.compile();
96 if (result)
97 script->setError(BCC_INVALID_OPERATION);
98
99 return result;
Logan3f3d31f2010-11-27 13:52:03 +0800100}
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -0800101
Logan3f3d31f2010-11-27 13:52:03 +0800102extern "C" void bccGetScriptInfoLog(BCCscript *script,
103 BCCsizei maxLength,
104 BCCsizei *length,
105 BCCchar *infoLog) {
Logan38d06072010-12-29 00:16:39 +0800106 char const *message = script->compiler.getErrorMessage();
Logan3f3d31f2010-11-27 13:52:03 +0800107 int messageLength = strlen(message) + 1;
108 if (length)
109 *length = messageLength;
110
111 if (infoLog && maxLength > 0) {
112 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
113 memcpy(infoLog, message, trimmedLength);
114 infoLog[trimmedLength] = 0;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800115#if defined(USE_DISASSEMBLER_FILE)
116 LOGI("[GetScriptInfoLog] %s", infoLog);
117#endif
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700118 }
Logan3f3d31f2010-11-27 13:52:03 +0800119}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700120
Logan3f3d31f2010-11-27 13:52:03 +0800121extern "C" void bccGetScriptLabel(BCCscript *script,
122 const BCCchar *name,
123 BCCvoid **address) {
124 void *value = script->compiler.lookup(name);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800125 if (value) {
Logan3f3d31f2010-11-27 13:52:03 +0800126 *address = value;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800127#if defined(USE_DISASSEMBLER_FILE)
128 LOGI("[GetScriptLabel] %s @ 0x%x", name, value);
129#endif
130 } else {
Logan3f3d31f2010-11-27 13:52:03 +0800131 script->setError(BCC_INVALID_VALUE);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800132 }
Logan3f3d31f2010-11-27 13:52:03 +0800133}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700134
Logan3f3d31f2010-11-27 13:52:03 +0800135extern "C" void bccGetExportVars(BCCscript *script,
136 BCCsizei *actualVarCount,
137 BCCsizei maxVarCount,
138 BCCvoid **vars) {
139 script->compiler.getExportVars(actualVarCount, maxVarCount, vars);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800140
141#if defined(USE_DISASSEMBLER_FILE)
142 int i;
143 if (actualVarCount) {
144 LOGI("[ExportVars] #=%d:", *actualVarCount);
145 } else {
146 for (i = 0; i < maxVarCount; i++) {
147 LOGI("[ExportVars] #%d=0x%x", i, vars[i]);
148 }
149 }
150#endif
Logan3f3d31f2010-11-27 13:52:03 +0800151}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700152
Logan3f3d31f2010-11-27 13:52:03 +0800153extern "C" void bccGetExportFuncs(BCCscript *script,
154 BCCsizei *actualFuncCount,
155 BCCsizei maxFuncCount,
156 BCCvoid **funcs) {
157 script->compiler.getExportFuncs(actualFuncCount, maxFuncCount, funcs);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800158
159#if defined(USE_DISASSEMBLER_FILE)
160 int i;
161 if (actualFuncCount) {
162 LOGI("[ExportFunc] #=%d:", *actualFuncCount);
163 } else {
164 for (i = 0; i < maxFuncCount; i++) {
165 LOGI("[ExportFunc] #%d=0x%x", i, funcs[i]);
166 }
167 }
168#endif
Logan3f3d31f2010-11-27 13:52:03 +0800169}
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700170
Logan3f3d31f2010-11-27 13:52:03 +0800171extern "C" void bccGetPragmas(BCCscript *script,
172 BCCsizei *actualStringCount,
173 BCCsizei maxStringCount,
174 BCCchar **strings) {
175 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800176
177#if defined(USE_DISASSEMBLER_FILE)
178 int i;
179 LOGI("[Pragma] #=%d:", *actualStringCount);
180 for (i = 0; i < *actualStringCount; i++) {
181 LOGI(" %s", strings[i]);
182 }
183#endif
Logan3f3d31f2010-11-27 13:52:03 +0800184}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700185
Logan3f3d31f2010-11-27 13:52:03 +0800186extern "C" void bccGetFunctions(BCCscript *script,
187 BCCsizei *actualFunctionCount,
188 BCCsizei maxFunctionCount,
189 BCCchar **functions) {
190 script->compiler.getFunctions(actualFunctionCount,
191 maxFunctionCount,
192 functions);
193}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700194
Logan3f3d31f2010-11-27 13:52:03 +0800195extern "C" void bccGetFunctionBinary(BCCscript *script,
196 BCCchar *function,
197 BCCvoid **base,
198 BCCsizei *length) {
199 script->compiler.getFunctionBinary(function, base, length);
200}