blob: 86f54f776f914660caade5ac4d125933432275fe [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
Logan39a2ca52010-11-27 01:03:06 +080023#include <bcc/bcc.h>
Shih-wei Liao77ed6142010-04-07 12:21:42 -070024
Logan39a2ca52010-11-27 01:03:06 +080025#include "bcc_compiler.h"
26
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080027#include <utils/StopWatch.h>
Shih-wei Liao77ed6142010-04-07 12:21:42 -070028
Shih-wei Liao77ed6142010-04-07 12:21:42 -070029
Logan39a2ca52010-11-27 01:03:06 +080030namespace llvm {
31 class Module;
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080032}
33
Logan39a2ca52010-11-27 01:03:06 +080034
Shih-wei Liao77ed6142010-04-07 12:21:42 -070035namespace bcc {
36
Logan39a2ca52010-11-27 01:03:06 +080037 struct BCCscript {
38 //////////////////////////////////////////////////////////////////////////
39 // Part I. Compiler
40 //////////////////////////////////////////////////////////////////////////
41 Compiler compiler;
Shih-wei Liao77ed6142010-04-07 12:21:42 -070042
Logan39a2ca52010-11-27 01:03:06 +080043 void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
44 compiler.registerSymbolCallback(pFn, pContext);
45 }
Shih-wei Liao77ed6142010-04-07 12:21:42 -070046
Logan39a2ca52010-11-27 01:03:06 +080047 //////////////////////////////////////////////////////////////////////////
48 // Part II. Logistics & Error handling
49 //////////////////////////////////////////////////////////////////////////
50 BCCscript() {
51 bccError = BCC_NO_ERROR;
52 }
Shih-wei Liao77ed6142010-04-07 12:21:42 -070053
Logan39a2ca52010-11-27 01:03:06 +080054 ~BCCscript() {
55 }
Shih-wei Liao77ed6142010-04-07 12:21:42 -070056
Logan39a2ca52010-11-27 01:03:06 +080057 void setError(BCCenum error) {
58 if (bccError == BCC_NO_ERROR && error != BCC_NO_ERROR) {
59 bccError = error;
60 }
61 }
Zonr Chang932648d2010-10-13 22:23:56 +080062
Logan39a2ca52010-11-27 01:03:06 +080063 BCCenum getError() {
64 BCCenum result = bccError;
65 bccError = BCC_NO_ERROR;
66 return result;
67 }
Shih-wei Liao77ed6142010-04-07 12:21:42 -070068
Logan39a2ca52010-11-27 01:03:06 +080069 BCCenum bccError;
Shih-wei Liao77ed6142010-04-07 12:21:42 -070070 };
Shih-wei Liao77ed6142010-04-07 12:21:42 -070071
Shih-wei Liaobe5c5312010-05-09 05:30:09 -070072
Logan39a2ca52010-11-27 01:03:06 +080073 extern "C" BCCscript *bccCreateScript() {
74 return new BCCscript();
Shih-wei Liao77ed6142010-04-07 12:21:42 -070075 }
76
Logan39a2ca52010-11-27 01:03:06 +080077 extern "C" BCCenum bccGetError(BCCscript *script) {
78 return script->getError();
Shih-wei Liao77ed6142010-04-07 12:21:42 -070079 }
80
Logan39a2ca52010-11-27 01:03:06 +080081 extern "C" void bccDeleteScript(BCCscript *script) {
82 delete script;
Shih-wei Liao77ed6142010-04-07 12:21:42 -070083 }
84
Logan39a2ca52010-11-27 01:03:06 +080085 extern "C" void bccRegisterSymbolCallback(BCCscript *script,
86 BCCSymbolLookupFn pFn,
87 BCCvoid *pContext) {
88 script->registerSymbolCallback(pFn, pContext);
Shih-wei Liao77ed6142010-04-07 12:21:42 -070089 }
90
Logan39a2ca52010-11-27 01:03:06 +080091 extern "C" int bccReadModule(BCCscript *script, BCCvoid *module) {
92 return script->compiler.readModule(reinterpret_cast<llvm::Module*>(module));
Shih-wei Liao77ed6142010-04-07 12:21:42 -070093 }
94
Logan39a2ca52010-11-27 01:03:06 +080095 extern "C" int bccReadBC(BCCscript *script,
96 const BCCchar *bitcode,
97 BCCint size,
98 const BCCchar *resName) {
99 return script->compiler.readBC(bitcode, size, resName);
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700100 }
101
Logan39a2ca52010-11-27 01:03:06 +0800102 extern "C" void bccLinkBC(BCCscript *script,
103 const BCCchar *bitcode,
104 BCCint size) {
105 script->compiler.linkBC(bitcode, size);
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700106 }
107
Logan39a2ca52010-11-27 01:03:06 +0800108 extern "C" void bccLoadBinary(BCCscript *script) {
109 int result = script->compiler.loadCacheFile();
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -0800110 if (result)
111 script->setError(BCC_INVALID_OPERATION);
112 }
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -0800113
Logan39a2ca52010-11-27 01:03:06 +0800114 extern "C" void bccCompileBC(BCCscript *script) {
115 {
116#if defined(__arm__)
117 android::StopWatch compileTimer("RenderScript compile time");
118#endif
119 int result = script->compiler.compile();
120 if (result)
121 script->setError(BCC_INVALID_OPERATION);
122 }
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700123 }
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700124
Logan39a2ca52010-11-27 01:03:06 +0800125 extern "C" void bccGetScriptInfoLog(BCCscript *script,
126 BCCsizei maxLength,
127 BCCsizei *length,
128 BCCchar *infoLog) {
129 char *message = script->compiler.getErrorMessage();
130 int messageLength = strlen(message) + 1;
131 if (length)
132 *length = messageLength;
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700133
Logan39a2ca52010-11-27 01:03:06 +0800134 if (infoLog && maxLength > 0) {
135 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
136 memcpy(infoLog, message, trimmedLength);
137 infoLog[trimmedLength] = 0;
138 }
139 }
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700140
Logan39a2ca52010-11-27 01:03:06 +0800141 extern "C" void bccGetScriptLabel(BCCscript *script,
142 const BCCchar *name,
143 BCCvoid **address) {
144 void *value = script->compiler.lookup(name);
145 if (value)
146 *address = value;
147 else
148 script->setError(BCC_INVALID_VALUE);
149 }
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700150
Logan39a2ca52010-11-27 01:03:06 +0800151 extern "C" void bccGetExportVars(BCCscript *script,
152 BCCsizei *actualVarCount,
153 BCCsizei maxVarCount,
154 BCCvoid **vars) {
155 script->compiler.getExportVars(actualVarCount, maxVarCount, vars);
156 }
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700157
Logan39a2ca52010-11-27 01:03:06 +0800158 extern "C" void bccGetExportFuncs(BCCscript *script,
159 BCCsizei *actualFuncCount,
160 BCCsizei maxFuncCount,
161 BCCvoid **funcs) {
162 script->compiler.getExportFuncs(actualFuncCount, maxFuncCount, funcs);
163 }
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700164
Logan39a2ca52010-11-27 01:03:06 +0800165 extern "C" void bccGetPragmas(BCCscript *script,
166 BCCsizei *actualStringCount,
167 BCCsizei maxStringCount,
168 BCCchar **strings) {
169 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
170 }
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700171
Logan39a2ca52010-11-27 01:03:06 +0800172 extern "C" void bccGetFunctions(BCCscript *script,
173 BCCsizei *actualFunctionCount,
174 BCCsizei maxFunctionCount,
175 BCCchar **functions) {
176 script->compiler.getFunctions(actualFunctionCount,
177 maxFunctionCount,
178 functions);
179 }
180
181 extern "C" void bccGetFunctionBinary(BCCscript *script,
182 BCCchar *function,
183 BCCvoid **base,
184 BCCsizei *length) {
185 script->compiler.getFunctionBinary(function, base, length);
186 }
187
Zonr Chang932648d2010-10-13 22:23:56 +0800188} // namespace bcc