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