blob: 6b64d36b0422d3c75a81bfa3745e1d320ae8b7a8 [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();
73 if (result)
74 script->setError(BCC_INVALID_OPERATION);
75 return result;
76}
Shih-wei Liao77ed6142010-04-07 12:21:42 -070077
Logan3f3d31f2010-11-27 13:52:03 +080078extern "C" void bccCompileBC(BCCscript *script) {
79 {
80#if defined(__arm__)
81 android::StopWatch compileTimer("RenderScript compile time");
82#endif
83 int result = script->compiler.compile();
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080084 if (result)
85 script->setError(BCC_INVALID_OPERATION);
86 }
Logan3f3d31f2010-11-27 13:52:03 +080087}
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080088
Logan3f3d31f2010-11-27 13:52:03 +080089extern "C" void bccGetScriptInfoLog(BCCscript *script,
90 BCCsizei maxLength,
91 BCCsizei *length,
92 BCCchar *infoLog) {
93 char *message = script->compiler.getErrorMessage();
94 int messageLength = strlen(message) + 1;
95 if (length)
96 *length = messageLength;
97
98 if (infoLog && maxLength > 0) {
99 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
100 memcpy(infoLog, message, trimmedLength);
101 infoLog[trimmedLength] = 0;
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700102 }
Logan3f3d31f2010-11-27 13:52:03 +0800103}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700104
Logan3f3d31f2010-11-27 13:52:03 +0800105extern "C" void bccGetScriptLabel(BCCscript *script,
106 const BCCchar *name,
107 BCCvoid **address) {
108 void *value = script->compiler.lookup(name);
109 if (value)
110 *address = value;
111 else
112 script->setError(BCC_INVALID_VALUE);
113}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700114
Logan3f3d31f2010-11-27 13:52:03 +0800115extern "C" void bccGetExportVars(BCCscript *script,
116 BCCsizei *actualVarCount,
117 BCCsizei maxVarCount,
118 BCCvoid **vars) {
119 script->compiler.getExportVars(actualVarCount, maxVarCount, vars);
120}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700121
Logan3f3d31f2010-11-27 13:52:03 +0800122extern "C" void bccGetExportFuncs(BCCscript *script,
123 BCCsizei *actualFuncCount,
124 BCCsizei maxFuncCount,
125 BCCvoid **funcs) {
126 script->compiler.getExportFuncs(actualFuncCount, maxFuncCount, funcs);
127}
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700128
Logan3f3d31f2010-11-27 13:52:03 +0800129extern "C" void bccGetPragmas(BCCscript *script,
130 BCCsizei *actualStringCount,
131 BCCsizei maxStringCount,
132 BCCchar **strings) {
133 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
134}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700135
Logan3f3d31f2010-11-27 13:52:03 +0800136extern "C" void bccGetFunctions(BCCscript *script,
137 BCCsizei *actualFunctionCount,
138 BCCsizei maxFunctionCount,
139 BCCchar **functions) {
140 script->compiler.getFunctions(actualFunctionCount,
141 maxFunctionCount,
142 functions);
143}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700144
Logan3f3d31f2010-11-27 13:52:03 +0800145extern "C" void bccGetFunctionBinary(BCCscript *script,
146 BCCchar *function,
147 BCCvoid **base,
148 BCCsizei *length) {
149 script->compiler.getFunctionBinary(function, base, length);
150}