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 | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 23 | #include "Compiler.h" |
| 24 | #include "Script.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 25 | |
Logan | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 26 | #include <bcc/bcc.h> |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 27 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 28 | #include <utils/StopWatch.h> |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 29 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 30 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 31 | namespace llvm { |
| 32 | class Module; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Logan | 39a2ca5 | 2010-11-27 01:03:06 +0800 | [diff] [blame] | 35 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 36 | extern "C" BCCscript *bccCreateScript() { |
| 37 | return new BCCscript(); |
| 38 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 39 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 40 | extern "C" BCCenum bccGetError(BCCscript *script) { |
| 41 | return script->getError(); |
| 42 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 43 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 44 | extern "C" void bccDeleteScript(BCCscript *script) { |
Shih-wei Liao | 8f77997 | 2010-12-23 12:07:59 +0800 | [diff] [blame^] | 45 | //LOGE("Script deleted"); sliao |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 46 | delete script; |
| 47 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 48 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 49 | extern "C" void bccRegisterSymbolCallback(BCCscript *script, |
| 50 | BCCSymbolLookupFn pFn, |
| 51 | BCCvoid *pContext) { |
| 52 | script->registerSymbolCallback(pFn, pContext); |
| 53 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 54 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 55 | extern "C" int bccReadModule(BCCscript *script, BCCvoid *module) { |
| 56 | return script->compiler.readModule(reinterpret_cast<llvm::Module*>(module)); |
| 57 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 58 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 59 | extern "C" int bccReadBC(BCCscript *script, |
| 60 | const BCCchar *bitcode, |
Logan | b9b0416 | 2010-12-20 16:36:57 +0800 | [diff] [blame] | 61 | BCCint bitcodeSize, |
| 62 | long bitcodeFileModTime, |
| 63 | long bitcodeFileCRC32, |
Shih-wei Liao | e6a1851 | 2010-12-09 12:38:10 -0800 | [diff] [blame] | 64 | const BCCchar *resName, |
| 65 | const BCCchar *cacheDir) { |
Logan | b9b0416 | 2010-12-20 16:36:57 +0800 | [diff] [blame] | 66 | return script->compiler.readBC(bitcode, bitcodeSize, |
| 67 | bitcodeFileModTime, bitcodeFileCRC32, |
| 68 | resName, cacheDir); |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 69 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 70 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 71 | extern "C" void bccLinkBC(BCCscript *script, |
| 72 | const BCCchar *bitcode, |
| 73 | BCCint size) { |
| 74 | script->compiler.linkBC(bitcode, size); |
| 75 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 76 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 77 | extern "C" int bccLoadBinary(BCCscript *script) { |
| 78 | int result = script->compiler.loadCacheFile(); |
Shih-wei Liao | d6d488c | 2010-12-04 18:23:50 -0800 | [diff] [blame] | 79 | |
| 80 | #if defined(USE_DISASSEMBLER_FILE) |
| 81 | LOGI("[LoadBinary] result=%d", result); |
| 82 | #endif |
| 83 | if (result) { |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 84 | script->setError(BCC_INVALID_OPERATION); |
Shih-wei Liao | d6d488c | 2010-12-04 18:23:50 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 87 | return result; |
| 88 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 89 | |
Logan | d80e65b | 2010-12-03 21:28:04 +0800 | [diff] [blame] | 90 | extern "C" int bccCompileBC(BCCscript *script) { |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 91 | #if defined(__arm__) |
Logan | d80e65b | 2010-12-03 21:28:04 +0800 | [diff] [blame] | 92 | android::StopWatch compileTimer("RenderScript compile time"); |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 93 | #endif |
Logan | d80e65b | 2010-12-03 21:28:04 +0800 | [diff] [blame] | 94 | |
| 95 | int result = script->compiler.compile(); |
| 96 | if (result) |
| 97 | script->setError(BCC_INVALID_OPERATION); |
| 98 | |
| 99 | return result; |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 100 | } |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 101 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 102 | extern "C" void bccGetScriptInfoLog(BCCscript *script, |
| 103 | BCCsizei maxLength, |
| 104 | BCCsizei *length, |
| 105 | BCCchar *infoLog) { |
| 106 | char *message = script->compiler.getErrorMessage(); |
| 107 | int messageLength = strlen(message) + 1; |
| 108 | if (length) |
| 109 | *length = messageLength; |
| 110 | |
| 111 | if (infoLog && maxLength > 0) { |
| 112 | int trimmedLength = maxLength < messageLength ? maxLength : messageLength; |
| 113 | memcpy(infoLog, message, trimmedLength); |
| 114 | infoLog[trimmedLength] = 0; |
Shih-wei Liao | d6d488c | 2010-12-04 18:23:50 -0800 | [diff] [blame] | 115 | #if defined(USE_DISASSEMBLER_FILE) |
| 116 | LOGI("[GetScriptInfoLog] %s", infoLog); |
| 117 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 118 | } |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 119 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 120 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 121 | extern "C" void bccGetScriptLabel(BCCscript *script, |
| 122 | const BCCchar *name, |
| 123 | BCCvoid **address) { |
| 124 | void *value = script->compiler.lookup(name); |
Shih-wei Liao | d6d488c | 2010-12-04 18:23:50 -0800 | [diff] [blame] | 125 | if (value) { |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 126 | *address = value; |
Shih-wei Liao | d6d488c | 2010-12-04 18:23:50 -0800 | [diff] [blame] | 127 | #if defined(USE_DISASSEMBLER_FILE) |
| 128 | LOGI("[GetScriptLabel] %s @ 0x%x", name, value); |
| 129 | #endif |
| 130 | } else { |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 131 | script->setError(BCC_INVALID_VALUE); |
Shih-wei Liao | d6d488c | 2010-12-04 18:23:50 -0800 | [diff] [blame] | 132 | } |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 133 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 134 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 135 | extern "C" void bccGetExportVars(BCCscript *script, |
| 136 | BCCsizei *actualVarCount, |
| 137 | BCCsizei maxVarCount, |
| 138 | BCCvoid **vars) { |
| 139 | script->compiler.getExportVars(actualVarCount, maxVarCount, vars); |
Shih-wei Liao | d6d488c | 2010-12-04 18:23:50 -0800 | [diff] [blame] | 140 | |
| 141 | #if defined(USE_DISASSEMBLER_FILE) |
| 142 | int i; |
| 143 | if (actualVarCount) { |
| 144 | LOGI("[ExportVars] #=%d:", *actualVarCount); |
| 145 | } else { |
| 146 | for (i = 0; i < maxVarCount; i++) { |
| 147 | LOGI("[ExportVars] #%d=0x%x", i, vars[i]); |
| 148 | } |
| 149 | } |
| 150 | #endif |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 151 | } |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 152 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 153 | extern "C" void bccGetExportFuncs(BCCscript *script, |
| 154 | BCCsizei *actualFuncCount, |
| 155 | BCCsizei maxFuncCount, |
| 156 | BCCvoid **funcs) { |
| 157 | script->compiler.getExportFuncs(actualFuncCount, maxFuncCount, funcs); |
Shih-wei Liao | d6d488c | 2010-12-04 18:23:50 -0800 | [diff] [blame] | 158 | |
| 159 | #if defined(USE_DISASSEMBLER_FILE) |
| 160 | int i; |
| 161 | if (actualFuncCount) { |
| 162 | LOGI("[ExportFunc] #=%d:", *actualFuncCount); |
| 163 | } else { |
| 164 | for (i = 0; i < maxFuncCount; i++) { |
| 165 | LOGI("[ExportFunc] #%d=0x%x", i, funcs[i]); |
| 166 | } |
| 167 | } |
| 168 | #endif |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 169 | } |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 170 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 171 | extern "C" void bccGetPragmas(BCCscript *script, |
| 172 | BCCsizei *actualStringCount, |
| 173 | BCCsizei maxStringCount, |
| 174 | BCCchar **strings) { |
| 175 | script->compiler.getPragmas(actualStringCount, maxStringCount, strings); |
Shih-wei Liao | d6d488c | 2010-12-04 18:23:50 -0800 | [diff] [blame] | 176 | |
| 177 | #if defined(USE_DISASSEMBLER_FILE) |
| 178 | int i; |
| 179 | LOGI("[Pragma] #=%d:", *actualStringCount); |
| 180 | for (i = 0; i < *actualStringCount; i++) { |
| 181 | LOGI(" %s", strings[i]); |
| 182 | } |
| 183 | #endif |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 184 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 185 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 186 | extern "C" void bccGetFunctions(BCCscript *script, |
| 187 | BCCsizei *actualFunctionCount, |
| 188 | BCCsizei maxFunctionCount, |
| 189 | BCCchar **functions) { |
| 190 | script->compiler.getFunctions(actualFunctionCount, |
| 191 | maxFunctionCount, |
| 192 | functions); |
| 193 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 194 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 195 | extern "C" void bccGetFunctionBinary(BCCscript *script, |
| 196 | BCCchar *function, |
| 197 | BCCvoid **base, |
| 198 | BCCsizei *length) { |
| 199 | script->compiler.getFunctionBinary(function, base, length); |
| 200 | } |