Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1 | /* |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2 | * Copyright 2010, The Android Open Source Project |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3 | * |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4 | * 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 Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 17 | // Bitcode compiler (bcc) for Android: |
| 18 | // This is an eager-compilation JIT running on Android. |
| 19 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 20 | #define LOG_TAG "bcc" |
| 21 | #include <cutils/log.h> |
| 22 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 23 | #include <bcc/bcc.h> |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 24 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 25 | #include "bcc_compiler.h" |
| 26 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 27 | #include <utils/StopWatch.h> |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 28 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 29 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 30 | namespace llvm { |
| 31 | class Module; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 34 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 35 | namespace bcc { |
| 36 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 37 | struct BCCscript { |
| 38 | ////////////////////////////////////////////////////////////////////////// |
| 39 | // Part I. Compiler |
| 40 | ////////////////////////////////////////////////////////////////////////// |
| 41 | Compiler compiler; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 42 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 43 | void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) { |
| 44 | compiler.registerSymbolCallback(pFn, pContext); |
| 45 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 46 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 47 | ////////////////////////////////////////////////////////////////////////// |
| 48 | // Part II. Logistics & Error handling |
| 49 | ////////////////////////////////////////////////////////////////////////// |
| 50 | BCCscript() { |
| 51 | bccError = BCC_NO_ERROR; |
| 52 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 53 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 54 | ~BCCscript() { |
| 55 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 56 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 57 | void setError(BCCenum error) { |
| 58 | if (bccError == BCC_NO_ERROR && error != BCC_NO_ERROR) { |
| 59 | bccError = error; |
| 60 | } |
| 61 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 62 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 63 | BCCenum getError() { |
| 64 | BCCenum result = bccError; |
| 65 | bccError = BCC_NO_ERROR; |
| 66 | return result; |
| 67 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 68 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 69 | BCCenum bccError; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 70 | }; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 71 | |
Shih-wei Liao | be5c531 | 2010-05-09 05:30:09 -0700 | [diff] [blame] | 72 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 73 | extern "C" BCCscript *bccCreateScript() { |
| 74 | return new BCCscript(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 77 | extern "C" BCCenum bccGetError(BCCscript *script) { |
| 78 | return script->getError(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 81 | extern "C" void bccDeleteScript(BCCscript *script) { |
| 82 | delete script; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 85 | extern "C" void bccRegisterSymbolCallback(BCCscript *script, |
| 86 | BCCSymbolLookupFn pFn, |
| 87 | BCCvoid *pContext) { |
| 88 | script->registerSymbolCallback(pFn, pContext); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 91 | extern "C" int bccReadModule(BCCscript *script, BCCvoid *module) { |
| 92 | return script->compiler.readModule(reinterpret_cast<llvm::Module*>(module)); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 95 | 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 Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 102 | extern "C" void bccLinkBC(BCCscript *script, |
| 103 | const BCCchar *bitcode, |
| 104 | BCCint size) { |
| 105 | script->compiler.linkBC(bitcode, size); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 108 | extern "C" void bccLoadBinary(BCCscript *script) { |
| 109 | int result = script->compiler.loadCacheFile(); |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 110 | if (result) |
| 111 | script->setError(BCC_INVALID_OPERATION); |
| 112 | } |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 113 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 114 | 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 Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 123 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 124 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 125 | 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 Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 133 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 134 | if (infoLog && maxLength > 0) { |
| 135 | int trimmedLength = maxLength < messageLength ? maxLength : messageLength; |
| 136 | memcpy(infoLog, message, trimmedLength); |
| 137 | infoLog[trimmedLength] = 0; |
| 138 | } |
| 139 | } |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 140 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 141 | 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 Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 150 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 151 | 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 Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 157 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 158 | 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 Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 164 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 165 | 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 Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 171 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 172 | 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 Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 188 | } // namespace bcc |