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