blob: 054c9952e34fae04c7ec7e9e36ee066db18968c4 [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
Logand80e65b2010-12-03 21:28:04 +080078extern "C" int bccCompileBC(BCCscript *script) {
Logan3f3d31f2010-11-27 13:52:03 +080079#if defined(__arm__)
Logand80e65b2010-12-03 21:28:04 +080080 android::StopWatch compileTimer("RenderScript compile time");
Logan3f3d31f2010-11-27 13:52:03 +080081#endif
Logand80e65b2010-12-03 21:28:04 +080082
83 int result = script->compiler.compile();
84 if (result)
85 script->setError(BCC_INVALID_OPERATION);
86
87 return result;
Logan3f3d31f2010-11-27 13:52:03 +080088}
Shih-wei Liao7c5a5f72010-11-08 01:59:13 -080089
Logan3f3d31f2010-11-27 13:52:03 +080090extern "C" void bccGetScriptInfoLog(BCCscript *script,
91 BCCsizei maxLength,
92 BCCsizei *length,
93 BCCchar *infoLog) {
94 char *message = script->compiler.getErrorMessage();
95 int messageLength = strlen(message) + 1;
96 if (length)
97 *length = messageLength;
98
99 if (infoLog && maxLength > 0) {
100 int trimmedLength = maxLength < messageLength ? maxLength : messageLength;
101 memcpy(infoLog, message, trimmedLength);
102 infoLog[trimmedLength] = 0;
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700103 }
Logan3f3d31f2010-11-27 13:52:03 +0800104}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700105
Logan3f3d31f2010-11-27 13:52:03 +0800106extern "C" void bccGetScriptLabel(BCCscript *script,
107 const BCCchar *name,
108 BCCvoid **address) {
109 void *value = script->compiler.lookup(name);
110 if (value)
111 *address = value;
112 else
113 script->setError(BCC_INVALID_VALUE);
114}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700115
Logan3f3d31f2010-11-27 13:52:03 +0800116extern "C" void bccGetExportVars(BCCscript *script,
117 BCCsizei *actualVarCount,
118 BCCsizei maxVarCount,
119 BCCvoid **vars) {
120 script->compiler.getExportVars(actualVarCount, maxVarCount, vars);
121}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -0700122
Logan3f3d31f2010-11-27 13:52:03 +0800123extern "C" void bccGetExportFuncs(BCCscript *script,
124 BCCsizei *actualFuncCount,
125 BCCsizei maxFuncCount,
126 BCCvoid **funcs) {
127 script->compiler.getExportFuncs(actualFuncCount, maxFuncCount, funcs);
128}
Shih-wei Liao6bfd5422010-05-07 05:20:22 -0700129
Logan3f3d31f2010-11-27 13:52:03 +0800130extern "C" void bccGetPragmas(BCCscript *script,
131 BCCsizei *actualStringCount,
132 BCCsizei maxStringCount,
133 BCCchar **strings) {
134 script->compiler.getPragmas(actualStringCount, maxStringCount, strings);
135}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700136
Logan3f3d31f2010-11-27 13:52:03 +0800137extern "C" void bccGetFunctions(BCCscript *script,
138 BCCsizei *actualFunctionCount,
139 BCCsizei maxFunctionCount,
140 BCCchar **functions) {
141 script->compiler.getFunctions(actualFunctionCount,
142 maxFunctionCount,
143 functions);
144}
Shih-wei Liao77ed6142010-04-07 12:21:42 -0700145
Logan3f3d31f2010-11-27 13:52:03 +0800146extern "C" void bccGetFunctionBinary(BCCscript *script,
147 BCCchar *function,
148 BCCvoid **base,
149 BCCsizei *length) {
150 script->compiler.getFunctionBinary(function, base, length);
151}