blob: 149139c87712edbb6d9124707bc6f68bc19c5c73 [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,
Loganb9b04162010-12-20 16:36:57 +080060 BCCint bitcodeSize,
61 long bitcodeFileModTime,
62 long bitcodeFileCRC32,
Shih-wei Liaoe6a18512010-12-09 12:38:10 -080063 const BCCchar *resName,
64 const BCCchar *cacheDir) {
Loganb9b04162010-12-20 16:36:57 +080065 return script->compiler.readBC(bitcode, bitcodeSize,
66 bitcodeFileModTime, bitcodeFileCRC32,
67 resName, cacheDir);
Logan3f3d31f2010-11-27 13:52:03 +080068}
Zonr Chang932648d2010-10-13 22:23:56 +080069
Logan3f3d31f2010-11-27 13:52:03 +080070extern "C" void bccLinkBC(BCCscript *script,
71 const BCCchar *bitcode,
72 BCCint size) {
73 script->compiler.linkBC(bitcode, size);
74}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070075
Logan3f3d31f2010-11-27 13:52:03 +080076extern "C" int bccLoadBinary(BCCscript *script) {
77 int result = script->compiler.loadCacheFile();
Shih-wei Liaod6d488c2010-12-04 18:23:50 -080078
79#if defined(USE_DISASSEMBLER_FILE)
80 LOGI("[LoadBinary] result=%d", result);
81#endif
82 if (result) {
Logan3f3d31f2010-11-27 13:52:03 +080083 script->setError(BCC_INVALID_OPERATION);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -080084 }
85
Logan3f3d31f2010-11-27 13:52:03 +080086 return result;
87}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070088
Logand80e65b2010-12-03 21:28:04 +080089extern "C" int bccCompileBC(BCCscript *script) {
Logan3f3d31f2010-11-27 13:52:03 +080090#if defined(__arm__)
Logand80e65b2010-12-03 21:28:04 +080091 android::StopWatch compileTimer("RenderScript compile time");
Logan3f3d31f2010-11-27 13:52:03 +080092#endif
Logand80e65b2010-12-03 21:28:04 +080093
94 int result = script->compiler.compile();
95 if (result)
96 script->setError(BCC_INVALID_OPERATION);
97
98 return result;
Logan3f3d31f2010-11-27 13:52:03 +080099}
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -0800100
Logan3f3d31f2010-11-27 13:52:03 +0800101extern "C" void bccGetScriptInfoLog(BCCscript *script,
102 BCCsizei maxLength,
103 BCCsizei *length,
104 BCCchar *infoLog) {
105 char *message = script->compiler.getErrorMessage();
106 int messageLength = strlen(message) + 1;
107 if (length)
108 *length = messageLength;
109
110 if (infoLog && maxLength > 0) {
111 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
112 memcpy(infoLog, message, trimmedLength);
113 infoLog[trimmedLength] = 0;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800114#if defined(USE_DISASSEMBLER_FILE)
115 LOGI("[GetScriptInfoLog] %s", infoLog);
116#endif
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700117 }
Logan3f3d31f2010-11-27 13:52:03 +0800118}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700119
Logan3f3d31f2010-11-27 13:52:03 +0800120extern "C" void bccGetScriptLabel(BCCscript *script,
121 const BCCchar *name,
122 BCCvoid **address) {
123 void *value = script->compiler.lookup(name);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800124 if (value) {
Logan3f3d31f2010-11-27 13:52:03 +0800125 *address = value;
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800126#if defined(USE_DISASSEMBLER_FILE)
127 LOGI("[GetScriptLabel] %s @ 0x%x", name, value);
128#endif
129 } else {
Logan3f3d31f2010-11-27 13:52:03 +0800130 script->setError(BCC_INVALID_VALUE);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800131 }
Logan3f3d31f2010-11-27 13:52:03 +0800132}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700133
Logan3f3d31f2010-11-27 13:52:03 +0800134extern "C" void bccGetExportVars(BCCscript *script,
135 BCCsizei *actualVarCount,
136 BCCsizei maxVarCount,
137 BCCvoid **vars) {
138 script->compiler.getExportVars(actualVarCount, maxVarCount, vars);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800139
140#if defined(USE_DISASSEMBLER_FILE)
141 int i;
142 if (actualVarCount) {
143 LOGI("[ExportVars] #=%d:", *actualVarCount);
144 } else {
145 for (i = 0; i < maxVarCount; i++) {
146 LOGI("[ExportVars] #%d=0x%x", i, vars[i]);
147 }
148 }
149#endif
Logan3f3d31f2010-11-27 13:52:03 +0800150}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700151
Logan3f3d31f2010-11-27 13:52:03 +0800152extern "C" void bccGetExportFuncs(BCCscript *script,
153 BCCsizei *actualFuncCount,
154 BCCsizei maxFuncCount,
155 BCCvoid **funcs) {
156 script->compiler.getExportFuncs(actualFuncCount, maxFuncCount, funcs);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800157
158#if defined(USE_DISASSEMBLER_FILE)
159 int i;
160 if (actualFuncCount) {
161 LOGI("[ExportFunc] #=%d:", *actualFuncCount);
162 } else {
163 for (i = 0; i < maxFuncCount; i++) {
164 LOGI("[ExportFunc] #%d=0x%x", i, funcs[i]);
165 }
166 }
167#endif
Logan3f3d31f2010-11-27 13:52:03 +0800168}
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700169
Logan3f3d31f2010-11-27 13:52:03 +0800170extern "C" void bccGetPragmas(BCCscript *script,
171 BCCsizei *actualStringCount,
172 BCCsizei maxStringCount,
173 BCCchar **strings) {
174 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
Shih-wei Liaod6d488c2010-12-04 18:23:50 -0800175
176#if defined(USE_DISASSEMBLER_FILE)
177 int i;
178 LOGI("[Pragma] #=%d:", *actualStringCount);
179 for (i = 0; i < *actualStringCount; i++) {
180 LOGI(" %s", strings[i]);
181 }
182#endif
Logan3f3d31f2010-11-27 13:52:03 +0800183}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700184
Logan3f3d31f2010-11-27 13:52:03 +0800185extern "C" void bccGetFunctions(BCCscript *script,
186 BCCsizei *actualFunctionCount,
187 BCCsizei maxFunctionCount,
188 BCCchar **functions) {
189 script->compiler.getFunctions(actualFunctionCount,
190 maxFunctionCount,
191 functions);
192}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700193
Logan3f3d31f2010-11-27 13:52:03 +0800194extern "C" void bccGetFunctionBinary(BCCscript *script,
195 BCCchar *function,
196 BCCvoid **base,
197 BCCsizei *length) {
198 script->compiler.getFunctionBinary(function, base, length);
199}