Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010-2012, The Android Open Source Project |
| 3 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | // Bitcode compiler (bcc) for Android: |
| 18 | // This is an eager-compilation JIT running on Android. |
| 19 | |
Stephen Hines | 2f6a493 | 2012-05-03 12:27:13 -0700 | [diff] [blame] | 20 | #include <bcc/bcc.h> |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame^] | 21 | #include "bcc_internal.h" |
| 22 | |
| 23 | #include "Config.h" |
| 24 | |
| 25 | #include "Compiler.h" |
| 26 | #include "DebugHelper.h" |
| 27 | #include "Script.h" |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 28 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 29 | #include <string> |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 30 | |
| 31 | #include <utils/StopWatch.h> |
| 32 | |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame^] | 33 | #include <llvm/Support/CodeGen.h> |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 34 | |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 35 | using namespace bcc; |
| 36 | |
| 37 | namespace llvm { |
| 38 | class Module; |
| 39 | } |
| 40 | |
| 41 | static bool bccBuildStampPrinted = false; |
| 42 | |
| 43 | static void bccPrintBuildStamp() { |
| 44 | if (!bccBuildStampPrinted) { |
| 45 | ALOGI("LIBBCC build time: %s", bccGetBuildTime()); |
| 46 | ALOGI("LIBBCC build revision: %s", bccGetBuildRev()); |
| 47 | bccBuildStampPrinted = true; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | extern "C" BCCScriptRef bccCreateScript() { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 52 | BCC_FUNC_LOGGER(); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 53 | bccPrintBuildStamp(); |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame^] | 54 | return wrap(new bcc::Script()); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
| 58 | extern "C" void bccDisposeScript(BCCScriptRef script) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 59 | BCC_FUNC_LOGGER(); |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 60 | delete unwrap(script); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
| 64 | extern "C" int bccRegisterSymbolCallback(BCCScriptRef script, |
| 65 | BCCSymbolLookupFn pFn, |
| 66 | void *pContext) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 67 | BCC_FUNC_LOGGER(); |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 68 | return unwrap(script)->registerSymbolCallback(pFn, pContext); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
| 72 | extern "C" int bccGetError(BCCScriptRef script) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 73 | BCC_FUNC_LOGGER(); |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 74 | return unwrap(script)->getError(); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 77 | extern "C" int bccReadBC(BCCScriptRef script, |
| 78 | char const *resName, |
| 79 | char const *bitcode, |
| 80 | size_t bitcodeSize, |
| 81 | unsigned long flags) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 82 | BCC_FUNC_LOGGER(); |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame^] | 83 | return unwrap(script)->addSourceBC(0, resName, bitcode, bitcodeSize, flags); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
| 87 | extern "C" int bccReadModule(BCCScriptRef script, |
| 88 | char const *resName /* deprecated */, |
| 89 | LLVMModuleRef module, |
| 90 | unsigned long flags) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 91 | BCC_FUNC_LOGGER(); |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame^] | 92 | return unwrap(script)->addSourceModule(0, unwrap(module), flags); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | extern "C" int bccReadFile(BCCScriptRef script, |
| 97 | char const *path, |
| 98 | unsigned long flags) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 99 | BCC_FUNC_LOGGER(); |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame^] | 100 | return unwrap(script)->addSourceFile(0, path, flags); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
| 104 | extern "C" int bccLinkBC(BCCScriptRef script, |
| 105 | char const *resName, |
| 106 | char const *bitcode, |
| 107 | size_t bitcodeSize, |
| 108 | unsigned long flags) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 109 | BCC_FUNC_LOGGER(); |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame^] | 110 | return unwrap(script)->addSourceBC(1, resName, bitcode, bitcodeSize, flags); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | extern "C" int bccLinkFile(BCCScriptRef script, |
| 115 | char const *path, |
| 116 | unsigned long flags) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 117 | BCC_FUNC_LOGGER(); |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame^] | 118 | return unwrap(script)->addSourceFile(1, path, flags); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
| 122 | extern "C" void bccMarkExternalSymbol(BCCScriptRef script, char const *name) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 123 | BCC_FUNC_LOGGER(); |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 124 | unwrap(script)->markExternalSymbol(name); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
| 128 | extern "C" int bccPrepareRelocatable(BCCScriptRef script, |
| 129 | char const *objPath, |
| 130 | bccRelocModelEnum RelocModel, |
| 131 | unsigned long flags) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 132 | BCC_FUNC_LOGGER(); |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 133 | llvm::Reloc::Model RM; |
| 134 | |
| 135 | switch (RelocModel) { |
| 136 | case bccRelocDefault: { |
| 137 | RM = llvm::Reloc::Default; |
| 138 | break; |
| 139 | } |
| 140 | case bccRelocStatic: { |
| 141 | RM = llvm::Reloc::Static; |
| 142 | break; |
| 143 | } |
| 144 | case bccRelocPIC: { |
| 145 | RM = llvm::Reloc::PIC_; |
| 146 | break; |
| 147 | } |
| 148 | case bccRelocDynamicNoPIC: { |
| 149 | RM = llvm::Reloc::DynamicNoPIC; |
| 150 | break; |
| 151 | } |
| 152 | default: { |
| 153 | ALOGE("Unrecognized relocation model for bccPrepareObject!"); |
| 154 | return BCC_INVALID_VALUE; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return unwrap(script)->prepareRelocatable(objPath, RM, flags); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | |
| 162 | extern "C" int bccPrepareSharedObject(BCCScriptRef script, |
| 163 | char const *objPath, |
| 164 | char const *dsoPath, |
| 165 | unsigned long flags) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 166 | BCC_FUNC_LOGGER(); |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 167 | return unwrap(script)->prepareSharedObject(objPath, dsoPath, flags); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
| 171 | extern "C" int bccPrepareExecutable(BCCScriptRef script, |
| 172 | char const *cacheDir, |
| 173 | char const *cacheName, |
| 174 | unsigned long flags) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 175 | BCC_FUNC_LOGGER(); |
| 176 | |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 177 | android::StopWatch compileTimer("bcc: PrepareExecutable time"); |
| 178 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 179 | return unwrap(script)->prepareExecutable(cacheDir, cacheName, flags); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
| 183 | extern "C" void *bccGetFuncAddr(BCCScriptRef script, char const *funcname) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 184 | BCC_FUNC_LOGGER(); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 185 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 186 | void *addr = unwrap(script)->lookup(funcname); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 187 | |
| 188 | #if DEBUG_BCC_REFLECT |
| 189 | ALOGD("Function Address: %s --> %p\n", funcname, addr); |
| 190 | #endif |
| 191 | |
| 192 | return addr; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | extern "C" void bccGetExportVarList(BCCScriptRef script, |
| 197 | size_t varListSize, |
| 198 | void **varList) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 199 | BCC_FUNC_LOGGER(); |
| 200 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 201 | if (varList) { |
| 202 | unwrap(script)->getExportVarList(varListSize, varList); |
| 203 | |
| 204 | #if DEBUG_BCC_REFLECT |
| 205 | size_t count = unwrap(script)->getExportVarCount(); |
| 206 | ALOGD("ExportVarCount = %lu\n", (unsigned long)count); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 207 | |
| 208 | if (count > varListSize) { |
| 209 | count = varListSize; |
| 210 | } |
| 211 | |
| 212 | for (size_t i = 0; i < count; ++i) { |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 213 | ALOGD("ExportVarList[%lu] = %p\n", (unsigned long)i, varList[i]); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 214 | } |
| 215 | #endif |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | |
| 220 | extern "C" void bccGetExportFuncList(BCCScriptRef script, |
| 221 | size_t funcListSize, |
| 222 | void **funcList) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 223 | BCC_FUNC_LOGGER(); |
| 224 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 225 | if (funcList) { |
| 226 | unwrap(script)->getExportFuncList(funcListSize, funcList); |
| 227 | |
| 228 | #if DEBUG_BCC_REFLECT |
| 229 | size_t count = unwrap(script)->getExportFuncCount(); |
| 230 | ALOGD("ExportFuncCount = %lu\n", (unsigned long)count); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 231 | |
| 232 | if (count > funcListSize) { |
| 233 | count = funcListSize; |
| 234 | } |
| 235 | |
| 236 | for (size_t i = 0; i < count; ++i) { |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 237 | ALOGD("ExportFuncList[%lu] = %p\n", (unsigned long)i, funcList[i]); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 238 | } |
| 239 | #endif |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | extern "C" void bccGetExportForEachList(BCCScriptRef script, |
| 245 | size_t forEachListSize, |
| 246 | void **forEachList) { |
Stephen Hines | 5b94819 | 2012-05-03 12:26:56 -0700 | [diff] [blame] | 247 | BCC_FUNC_LOGGER(); |
| 248 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 249 | if (forEachList) { |
| 250 | unwrap(script)->getExportForEachList(forEachListSize, forEachList); |
| 251 | |
| 252 | #if DEBUG_BCC_REFLECT |
| 253 | size_t count = unwrap(script)->getExportForEachCount(); |
| 254 | ALOGD("ExportForEachCount = %lu\n", (unsigned long)count); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 255 | |
| 256 | if (count > forEachListSize) { |
| 257 | count = forEachListSize; |
| 258 | } |
| 259 | |
| 260 | for (size_t i = 0; i < count; ++i) { |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 261 | ALOGD("ExportForEachList[%lu] = %p\n", (unsigned long)i, forEachList[i]); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 262 | } |
| 263 | #endif |
| 264 | } |
| 265 | } |