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 | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 20 | //#define BCC_CODE_ADDR 0x7e00000 |
| 21 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 22 | #define LOG_TAG "bcc" |
| 23 | #include <cutils/log.h> |
| 24 | |
| 25 | #include <ctype.h> |
| 26 | #include <errno.h> |
| 27 | #include <limits.h> |
| 28 | #include <stdarg.h> |
| 29 | #include <stdint.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 32 | #include <stddef.h> |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <unistd.h> |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 35 | #include <sys/mman.h> |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 36 | #include <sys/file.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 39 | |
| 40 | #include <cutils/hashmap.h> |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 41 | #include <utils/StopWatch.h> |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 42 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 43 | #if defined(__arm__) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 44 | # define DEFAULT_ARM_CODEGEN |
| 45 | # define PROVIDE_ARM_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 46 | #elif defined(__i386__) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 47 | # define DEFAULT_X86_CODEGEN |
| 48 | # define PROVIDE_X86_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 49 | #elif defined(__x86_64__) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 50 | # define DEFAULT_X64_CODEGEN |
| 51 | # define PROVIDE_X64_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | #if defined(FORCE_ARM_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 55 | # define DEFAULT_ARM_CODEGEN |
| 56 | # undef DEFAULT_X86_CODEGEN |
| 57 | # undef DEFAULT_X64_CODEGEN |
| 58 | # define PROVIDE_ARM_CODEGEN |
| 59 | # undef PROVIDE_X86_CODEGEN |
| 60 | # undef PROVIDE_X64_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 61 | #elif defined(FORCE_X86_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 62 | # undef DEFAULT_ARM_CODEGEN |
| 63 | # define DEFAULT_X86_CODEGEN |
| 64 | # undef DEFAULT_X64_CODEGEN |
| 65 | # undef PROVIDE_ARM_CODEGEN |
| 66 | # define PROVIDE_X86_CODEGEN |
| 67 | # undef PROVIDE_X64_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 68 | #elif defined(FORCE_X64_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 69 | # undef DEFAULT_ARM_CODEGEN |
| 70 | # undef DEFAULT_X86_CODEGEN |
| 71 | # define DEFAULT_X64_CODEGEN |
| 72 | # undef PROVIDE_ARM_CODEGEN |
| 73 | # undef PROVIDE_X86_CODEGEN |
| 74 | # define PROVIDE_X64_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 75 | #endif |
| 76 | |
| 77 | #if defined(DEFAULT_ARM_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 78 | # define TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 79 | #elif defined(DEFAULT_X86_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 80 | # define TARGET_TRIPLE_STRING "i686-unknown-linux" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 81 | #elif defined(DEFAULT_X64_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 82 | # define TARGET_TRIPLE_STRING "x86_64-unknown-linux" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 83 | #endif |
| 84 | |
| 85 | #if (defined(__VFP_FP__) && !defined(__SOFTFP__)) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 86 | # define ARM_USE_VFP |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 87 | #endif |
| 88 | |
| 89 | #include <bcc/bcc.h> |
| 90 | #include "bcc_runtime.h" |
| 91 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 92 | #define LOG_API(...) do {} while (0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 93 | // #define LOG_API(...) fprintf (stderr, __VA_ARGS__) |
| 94 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 95 | #define LOG_STACK(...) do {} while (0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 96 | // #define LOG_STACK(...) fprintf (stderr, __VA_ARGS__) |
| 97 | |
| 98 | // #define PROVIDE_TRACE_CODEGEN |
| 99 | |
| 100 | #if defined(USE_DISASSEMBLER) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 101 | # include "llvm/MC/MCInst.h" |
| 102 | # include "llvm/MC/MCAsmInfo.h" |
| 103 | # include "llvm/MC/MCInstPrinter.h" |
| 104 | # include "llvm/MC/MCDisassembler.h" |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 105 | // If you want the disassemble results written to file, define this: |
| 106 | # define USE_DISASSEMBLER_FILE |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 107 | #endif |
| 108 | |
| 109 | #include <set> |
| 110 | #include <map> |
| 111 | #include <list> |
| 112 | #include <cmath> |
| 113 | #include <string> |
| 114 | #include <cstring> |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 115 | #include <algorithm> // for std::reverse |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 116 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 117 | // VMCore |
| 118 | #include "llvm/Use.h" |
| 119 | #include "llvm/User.h" |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 120 | #include "llvm/Linker.h" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 121 | #include "llvm/Module.h" |
| 122 | #include "llvm/Function.h" |
| 123 | #include "llvm/Constant.h" |
| 124 | #include "llvm/Constants.h" |
| 125 | #include "llvm/Instruction.h" |
| 126 | #include "llvm/PassManager.h" |
| 127 | #include "llvm/LLVMContext.h" |
| 128 | #include "llvm/GlobalValue.h" |
| 129 | #include "llvm/Instructions.h" |
| 130 | #include "llvm/OperandTraits.h" |
| 131 | #include "llvm/TypeSymbolTable.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 132 | |
| 133 | // System |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 134 | #include "llvm/System/Host.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 135 | |
| 136 | // ADT |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 137 | #include "llvm/ADT/APInt.h" |
| 138 | #include "llvm/ADT/APFloat.h" |
| 139 | #include "llvm/ADT/DenseMap.h" |
| 140 | #include "llvm/ADT/ValueMap.h" |
| 141 | #include "llvm/ADT/StringMap.h" |
| 142 | #include "llvm/ADT/OwningPtr.h" |
| 143 | #include "llvm/ADT/SmallString.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 144 | |
| 145 | // Target |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 146 | #include "llvm/Target/TargetData.h" |
| 147 | #include "llvm/Target/TargetSelect.h" |
| 148 | #include "llvm/Target/TargetOptions.h" |
| 149 | #include "llvm/Target/TargetMachine.h" |
| 150 | #include "llvm/Target/TargetJITInfo.h" |
| 151 | #include "llvm/Target/TargetRegistry.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 152 | #include "llvm/Target/SubtargetFeature.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 153 | |
| 154 | // Support |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 155 | #include "llvm/Support/Casting.h" |
| 156 | #include "llvm/Support/raw_ostream.h" |
| 157 | #include "llvm/Support/ValueHandle.h" |
| 158 | #include "llvm/Support/MemoryBuffer.h" |
| 159 | #include "llvm/Support/MemoryObject.h" |
| 160 | #include "llvm/Support/ManagedStatic.h" |
| 161 | #include "llvm/Support/ErrorHandling.h" |
| 162 | #include "llvm/Support/StandardPasses.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 163 | #include "llvm/Support/FormattedStream.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 164 | |
| 165 | // Bitcode |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 166 | #include "llvm/Bitcode/ReaderWriter.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 167 | |
| 168 | // CodeGen |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 169 | #include "llvm/CodeGen/Passes.h" |
| 170 | #include "llvm/CodeGen/JITCodeEmitter.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 171 | #include "llvm/CodeGen/MachineFunction.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 172 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 173 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 174 | #include "llvm/CodeGen/MachineRelocation.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 175 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 176 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 177 | #include "llvm/CodeGen/MachineConstantPool.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 178 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 179 | |
| 180 | // ExecutionEngine |
| 181 | #include "llvm/ExecutionEngine/GenericValue.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 182 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 183 | |
Shih-wei Liao | e64c287 | 2010-10-25 13:44:53 -0700 | [diff] [blame] | 184 | extern "C" void LLVMInitializeARMDisassembler(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 185 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 186 | // For caching |
| 187 | struct oBCCHeader { |
| 188 | uint8_t magic[4]; // includes version number |
| 189 | uint8_t magicVersion[4]; |
| 190 | |
| 191 | uint32_t sourceWhen; |
| 192 | uint32_t rslibWhen; |
| 193 | uint32_t libRSWhen; |
| 194 | uint32_t libbccWhen; |
| 195 | |
| 196 | uint32_t cachedCodeDataAddr; |
| 197 | uint32_t rootAddr; |
| 198 | uint32_t initAddr; |
| 199 | |
| 200 | uint32_t relocOffset; // offset of reloc table. |
| 201 | uint32_t relocCount; |
| 202 | uint32_t exportVarsOffset; // offset of export var table |
| 203 | uint32_t exportVarsCount; |
| 204 | uint32_t exportFuncsOffset; // offset of export func table |
| 205 | uint32_t exportFuncsCount; |
| 206 | uint32_t exportPragmasOffset; // offset of export pragma table |
| 207 | uint32_t exportPragmasCount; |
| 208 | |
| 209 | uint32_t codeOffset; // offset of code: 64-bit alignment |
| 210 | uint32_t codeSize; |
| 211 | uint32_t dataOffset; // offset of data section |
| 212 | uint32_t dataSize; |
| 213 | |
| 214 | // uint32_t flags; // some info flags |
| 215 | uint32_t checksum; // adler32 checksum covering deps/opt |
| 216 | }; |
| 217 | |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 218 | struct oBCCRelocEntry { |
| 219 | uint32_t relocType; // target instruction relocation type |
| 220 | uint32_t relocOffset; // offset of hole (holeAddr - codeAddr) |
| 221 | uint32_t cachedResultAddr; // address resolved at compile time |
| 222 | |
| 223 | oBCCRelocEntry(uintptr_t off, uint32_t ty, void *addr) |
| 224 | : relocType(ty), |
| 225 | relocOffset(static_cast<uint32_t>(off)), |
| 226 | cachedResultAddr(reinterpret_cast<uint32_t>(addr)) { |
| 227 | } |
| 228 | }; |
| 229 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 230 | /* oBCCHeader Offset Table */ |
| 231 | #define k_magic offsetof(oBCCHeader, magic) |
| 232 | #define k_magicVersion offsetof(oBCCHeader, magicVersion) |
| 233 | #define k_sourceWhen offsetof(oBCCHeader, sourceWhen) |
| 234 | #define k_rslibWhen offsetof(oBCCHeader, rslibWhen) |
| 235 | #define k_libRSWhen offsetof(oBCCHeader, libRSWhen) |
| 236 | #define k_libbccWhen offsetof(oBCCHeader, libbccWhen) |
| 237 | #define k_cachedCodeDataAddr offsetof(oBCCHeader, cachedCodeDataAddr) |
| 238 | #define k_rootAddr offsetof(oBCCHeader, rootAddr) |
| 239 | #define k_initAddr offsetof(oBCCHeader, initAddr) |
| 240 | #define k_relocOffset offsetof(oBCCHeader, relocOffset) |
| 241 | #define k_relocCount offsetof(oBCCHeader, relocCount) |
| 242 | #define k_exportVarsOffset offsetof(oBCCHeader, exportVarsOffset) |
| 243 | #define k_exportVarsCount offsetof(oBCCHeader, exportVarsCount) |
| 244 | #define k_exportFuncsOffset offsetof(oBCCHeader, exportFuncsOffset) |
| 245 | #define k_exportFuncsCount offsetof(oBCCHeader, exportFuncsCount) |
| 246 | #define k_exportPragmasOffset offsetof(oBCCHeader, exportPragmasOffset) |
| 247 | #define k_exportPragmasCount offsetof(oBCCHeader, exportPragmasCount) |
| 248 | #define k_codeOffset offsetof(oBCCHeader, codeOffset) |
| 249 | #define k_codeSize offsetof(oBCCHeader, codeSize) |
| 250 | #define k_dataOffset offsetof(oBCCHeader, dataOffset) |
| 251 | #define k_dataSize offsetof(oBCCHeader, dataSize) |
| 252 | #define k_checksum offsetof(oBCCHeader, checksum) |
| 253 | |
| 254 | /* oBCC file magic number */ |
| 255 | #define OBCC_MAGIC "bcc\n" |
| 256 | /* version, encoded in 4 bytes of ASCII */ |
| 257 | #define OBCC_MAGIC_VERS "001\0" |
| 258 | |
| 259 | #define TEMP_FAILURE_RETRY1(exp) ({ \ |
| 260 | typeof (exp) _rc; \ |
| 261 | do { \ |
| 262 | _rc = (exp); \ |
| 263 | } while (_rc == -1 && errno == EINTR); \ |
| 264 | _rc; }) |
| 265 | |
| 266 | static int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg) |
| 267 | { |
| 268 | while (count != 0) { |
| 269 | ssize_t actual = TEMP_FAILURE_RETRY1(write(fd, buf, count)); |
| 270 | if (actual < 0) { |
| 271 | int err = errno; |
| 272 | LOGE("%s: write failed: %s\n", logMsg, strerror(err)); |
| 273 | return err; |
| 274 | } else if (actual != (ssize_t) count) { |
| 275 | LOGD("%s: partial write (will retry): (%d of %zd)\n", |
| 276 | logMsg, (int) actual, count); |
| 277 | buf = (const void*) (((const uint8_t*) buf) + actual); |
| 278 | } |
| 279 | count -= actual; |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 285 | // |
| 286 | // Compilation class that suits Android's needs. |
| 287 | // (Support: no argument passed, ...) |
| 288 | // |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 289 | namespace bcc { |
| 290 | |
| 291 | class Compiler { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 292 | // This part is designed to be orthogonal to those exported bcc*() functions |
| 293 | // implementation and internal struct BCCscript. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 294 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 295 | ////////////////////////////////////////////////////////////////////////////// |
| 296 | // The variable section below (e.g., Triple, CodeGenOptLevel) |
| 297 | // is initialized in GlobalInitialization() |
| 298 | // |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 299 | static bool GlobalInitialized; |
| 300 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 301 | // If given, this will be the name of the target triple to compile for. |
| 302 | // If not given, the initial values defined in this file will be used. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 303 | static std::string Triple; |
| 304 | |
| 305 | static llvm::CodeGenOpt::Level CodeGenOptLevel; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 306 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 307 | // End of section of GlobalInitializing variables |
| 308 | ////////////////////////////////////////////////////////////////////////////// |
| 309 | |
| 310 | // If given, the name of the target CPU to generate code for. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 311 | static std::string CPU; |
| 312 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 313 | // The list of target specific features to enable or disable -- this should |
| 314 | // be a list of strings starting with '+' (enable) or '-' (disable). |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 315 | static std::vector<std::string> Features; |
| 316 | |
| 317 | struct Runtime { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 318 | const char *mName; |
| 319 | void *mPtr; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 320 | }; |
| 321 | static struct Runtime Runtimes[]; |
| 322 | |
| 323 | static void GlobalInitialization() { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 324 | if (GlobalInitialized) |
| 325 | return; |
Shih-wei Liao | be5c531 | 2010-05-09 05:30:09 -0700 | [diff] [blame] | 326 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 327 | // if (!llvm::llvm_is_multithreaded()) |
| 328 | // llvm::llvm_start_multithreaded(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 329 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 330 | // Set Triple, CPU and Features here |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 331 | Triple = TARGET_TRIPLE_STRING; |
| 332 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 333 | // TODO(zonr): NEON for JIT |
| 334 | // Features.push_back("+neon"); |
| 335 | // Features.push_back("+vmlx"); |
| 336 | // Features.push_back("+neonfp"); |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 337 | Features.push_back("+vfp3"); |
Shih-wei Liao | 21ef307 | 2010-10-23 22:33:12 -0700 | [diff] [blame] | 338 | Features.push_back("+d16"); |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 339 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 340 | #if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN) |
| 341 | LLVMInitializeARMTargetInfo(); |
| 342 | LLVMInitializeARMTarget(); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 343 | #if defined(USE_DISASSEMBLER) |
| 344 | LLVMInitializeARMDisassembler(); |
| 345 | LLVMInitializeARMAsmPrinter(); |
| 346 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 347 | #endif |
| 348 | |
| 349 | #if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN) |
| 350 | LLVMInitializeX86TargetInfo(); |
| 351 | LLVMInitializeX86Target(); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 352 | #if defined(USE_DISASSEMBLER) |
| 353 | LLVMInitializeX86Disassembler(); |
| 354 | LLVMInitializeX86AsmPrinter(); |
| 355 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 356 | #endif |
| 357 | |
| 358 | #if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN) |
| 359 | LLVMInitializeX86TargetInfo(); |
| 360 | LLVMInitializeX86Target(); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 361 | #if defined(USE_DISASSEMBLER) |
| 362 | LLVMInitializeX86Disassembler(); |
| 363 | LLVMInitializeX86AsmPrinter(); |
| 364 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 365 | #endif |
| 366 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 367 | // -O0: llvm::CodeGenOpt::None |
| 368 | // -O1: llvm::CodeGenOpt::Less |
| 369 | // -O2: llvm::CodeGenOpt::Default |
| 370 | // -O3: llvm::CodeGenOpt::Aggressive |
Shih-wei Liao | bfda6c9 | 2010-10-24 02:43:04 -0700 | [diff] [blame] | 371 | CodeGenOptLevel = llvm::CodeGenOpt::None; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 372 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 373 | // Below are the global settings to LLVM |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 374 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 375 | // Disable frame pointer elimination optimization |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 376 | llvm::NoFramePointerElim = false; |
| 377 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 378 | // Use hardfloat ABI |
| 379 | // |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 380 | // TODO(all): Need to detect the CPU capability and decide whether to use |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 381 | // softfp. To use softfp, change following 2 lines to |
| 382 | // |
| 383 | // llvm::FloatABIType = llvm::FloatABI::Soft; |
| 384 | // llvm::UseSoftFloat = true; |
| 385 | // |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 386 | llvm::FloatABIType = llvm::FloatABI::Soft; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 387 | llvm::UseSoftFloat = false; |
| 388 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 389 | // BCC needs all unknown symbols resolved at JIT/compilation time. |
| 390 | // So we don't need any dynamic relocation model. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 391 | llvm::TargetMachine::setRelocationModel(llvm::Reloc::Static); |
| 392 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 393 | #if defined(DEFAULT_X64_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 394 | // Data address in X86_64 architecture may reside in a far-away place |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 395 | llvm::TargetMachine::setCodeModel(llvm::CodeModel::Medium); |
| 396 | #else |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 397 | // This is set for the linker (specify how large of the virtual addresses |
| 398 | // we can access for all unknown symbols.) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 399 | llvm::TargetMachine::setCodeModel(llvm::CodeModel::Small); |
| 400 | #endif |
| 401 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 402 | // Register the scheduler |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 403 | llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler); |
| 404 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 405 | // Register allocation policy: |
| 406 | // createFastRegisterAllocator: fast but bad quality |
| 407 | // createLinearScanRegisterAllocator: not so fast but good quality |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 408 | llvm::RegisterRegAlloc::setDefault |
| 409 | ((CodeGenOptLevel == llvm::CodeGenOpt::None) ? |
Shih-wei Liao | 1601601 | 2010-09-10 17:55:03 -0700 | [diff] [blame] | 410 | llvm::createFastRegisterAllocator : |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 411 | llvm::createLinearScanRegisterAllocator); |
| 412 | |
| 413 | GlobalInitialized = true; |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 418 | std::string *Error = static_cast<std::string*>(UserData); |
Shih-wei Liao | 066d5ef | 2010-05-11 03:28:39 -0700 | [diff] [blame] | 419 | Error->assign(Message); |
Nick Kralevich | fc97e9f | 2010-05-17 14:59:16 -0700 | [diff] [blame] | 420 | LOGE("%s", Message.c_str()); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 421 | exit(1); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static const llvm::StringRef PragmaMetadataName; |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 425 | static const llvm::StringRef ExportVarMetadataName; |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 426 | static const llvm::StringRef ExportFuncMetadataName; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 427 | |
| 428 | private: |
Shih-wei Liao | c561199 | 2010-05-09 06:37:55 -0700 | [diff] [blame] | 429 | std::string mError; |
| 430 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 431 | inline bool hasError() const { |
| 432 | return !mError.empty(); |
| 433 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 434 | inline void setError(const char *Error) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 435 | mError.assign(Error); // Copying |
| 436 | return; |
| 437 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 438 | inline void setError(const std::string &Error) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 439 | mError = Error; |
| 440 | return; |
| 441 | } |
| 442 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 443 | bool mNeverCache; // Set by readBC() |
| 444 | bool mCacheNew; // Set by readBC() |
| 445 | int mCacheFd; // Set by readBC() |
| 446 | char *mCacheMapAddr; // Set by loader() if mCacheNew is false |
| 447 | oBCCHeader *mCacheHdr; // Set by loader() |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 448 | size_t mCacheSize; // Set by loader() |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 449 | ptrdiff_t mCacheDiff; // Set by loader() |
| 450 | char *mCodeDataAddr; // Set by CodeMemoryManager if mCacheNew is true. |
| 451 | // Used by genCacheFile() for dumping |
| 452 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 453 | typedef std::list< std::pair<std::string, std::string> > PragmaList; |
| 454 | PragmaList mPragmas; |
| 455 | |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 456 | typedef std::list<void*> ExportVarList; |
| 457 | ExportVarList mExportVars; |
| 458 | |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 459 | typedef std::list<void*> ExportFuncList; |
| 460 | ExportFuncList mExportFuncs; |
| 461 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 462 | ////////////////////////////////////////////////////////////////////////////// |
| 463 | // Memory manager for the code reside in memory |
| 464 | // |
| 465 | // The memory for our code emitter is very simple and is conforming to the |
| 466 | // design decisions of Android RenderScript's Exection Environment: |
| 467 | // The code, data, and symbol sizes are limited (currently 100KB.) |
| 468 | // |
| 469 | // It's very different from typical compiler, which has no limitation |
| 470 | // on the code size. How does code emitter know the size of the code |
| 471 | // it is about to emit? It does not know beforehand. We want to solve |
| 472 | // this without complicating the code emitter too much. |
| 473 | // |
| 474 | // We solve this by pre-allocating a certain amount of memory, |
| 475 | // and then start the code emission. Once the buffer overflows, the emitter |
| 476 | // simply discards all the subsequent emission but still has a counter |
| 477 | // on how many bytes have been emitted. |
| 478 | // |
| 479 | // So once the whole emission is done, if there's a buffer overflow, |
| 480 | // it re-allocates the buffer with enough size (based on the |
| 481 | // counter from previous emission) and re-emit again. |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 482 | |
| 483 | // 128 KiB for code |
| 484 | static const unsigned int MaxCodeSize = 128 * 1024; |
| 485 | // 1 KiB for global offset table (GOT) |
| 486 | static const unsigned int MaxGOTSize = 1 * 1024; |
| 487 | // 128 KiB for global variable |
| 488 | static const unsigned int MaxGlobalVarSize = 128 * 1024; |
| 489 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 490 | class CodeMemoryManager : public llvm::JITMemoryManager { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 491 | private: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 492 | // |
| 493 | // Our memory layout is as follows: |
| 494 | // |
| 495 | // The direction of arrows (-> and <-) shows memory's growth direction |
| 496 | // when more space is needed. |
| 497 | // |
| 498 | // @mpCodeMem: |
| 499 | // +--------------------------------------------------------------+ |
| 500 | // | Function Memory ... -> <- ... Stub/GOT | |
| 501 | // +--------------------------------------------------------------+ |
| 502 | // |<------------------ Total: @MaxCodeSize KiB ----------------->| |
| 503 | // |
| 504 | // Where size of GOT is @MaxGOTSize KiB. |
| 505 | // |
| 506 | // @mpGVMem: |
| 507 | // +--------------------------------------------------------------+ |
| 508 | // | Global variable ... -> | |
| 509 | // +--------------------------------------------------------------+ |
| 510 | // |<--------------- Total: @MaxGlobalVarSize KiB --------------->| |
| 511 | // |
| 512 | // |
| 513 | // @mCurFuncMemIdx: The current index (starting from 0) of the last byte |
| 514 | // of function code's memory usage |
| 515 | // @mCurSGMemIdx: The current index (starting from tail) of the last byte |
| 516 | // of stub/GOT's memory usage |
| 517 | // @mCurGVMemIdx: The current index (starting from tail) of the last byte |
| 518 | // of global variable's memory usage |
| 519 | // |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 520 | uintptr_t mCurFuncMemIdx; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 521 | uintptr_t mCurSGMemIdx; |
| 522 | uintptr_t mCurGVMemIdx; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 523 | void *mpCodeMem; |
| 524 | void *mpGVMem; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 525 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 526 | // GOT Base |
| 527 | uint8_t *mpGOTBase; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 528 | |
| 529 | typedef std::map<const llvm::Function*, pair<void* /* start address */, |
| 530 | void* /* end address */> |
| 531 | > FunctionMapTy; |
| 532 | FunctionMapTy mFunctionMap; |
| 533 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 534 | inline intptr_t getFreeCodeMemSize() const { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 535 | return mCurSGMemIdx - mCurFuncMemIdx; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 536 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 537 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 538 | uint8_t *allocateSGMemory(uintptr_t Size, |
| 539 | unsigned Alignment = 1 /* no alignment */) { |
| 540 | intptr_t FreeMemSize = getFreeCodeMemSize(); |
| 541 | if ((FreeMemSize < 0) || (static_cast<uintptr_t>(FreeMemSize) < Size)) |
| 542 | // The code size excesses our limit |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 543 | return NULL; |
| 544 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 545 | if (Alignment == 0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 546 | Alignment = 1; |
| 547 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 548 | uint8_t *result = getCodeMemBase() + mCurSGMemIdx - Size; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 549 | result = (uint8_t*) (((intptr_t) result) & ~(intptr_t) (Alignment - 1)); |
| 550 | |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 551 | mCurSGMemIdx = result - getCodeMemBase(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 552 | |
| 553 | return result; |
| 554 | } |
| 555 | |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 556 | inline uintptr_t getFreeGVMemSize() const { |
| 557 | return MaxGlobalVarSize - mCurGVMemIdx; |
| 558 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 559 | inline uint8_t *getGVMemBase() const { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 560 | return reinterpret_cast<uint8_t*>(mpGVMem); |
| 561 | } |
| 562 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 563 | public: |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 564 | CodeMemoryManager() : mpCodeMem(NULL), mpGVMem(NULL), mpGOTBase(NULL) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 565 | reset(); |
| 566 | std::string ErrMsg; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 567 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 568 | #ifdef BCC_CODE_ADDR |
| 569 | mpCodeMem = mmap(reinterpret_cast<void*>(BCC_CODE_ADDR), |
| 570 | MaxCodeSize + MaxGlobalVarSize, |
| 571 | PROT_READ | PROT_EXEC | PROT_WRITE, |
| 572 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
| 573 | -1, |
| 574 | 0); |
| 575 | #else |
| 576 | mpCodeMem = mmap(NULL, |
| 577 | MaxCodeSize + MaxGlobalVarSize, |
| 578 | PROT_READ | PROT_EXEC | PROT_WRITE, |
| 579 | MAP_PRIVATE | MAP_ANON, |
| 580 | -1, |
| 581 | 0); |
| 582 | #endif |
| 583 | |
| 584 | if (mpCodeMem == MAP_FAILED) { |
| 585 | llvm::report_fatal_error("Failed to allocate memory for emitting " |
| 586 | "codes\n" + ErrMsg); |
| 587 | } |
| 588 | mpGVMem = (void *) ((int) mpCodeMem + MaxCodeSize); |
| 589 | |
| 590 | /* mpCodeMem = mmap(NULL, MaxCodeSize, PROT_READ | PROT_EXEC, |
Ying Wang | 78db12e | 2010-09-25 17:48:59 -0700 | [diff] [blame] | 591 | MAP_PRIVATE | MAP_ANON, -1, 0); |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 592 | if (mpCodeMem == MAP_FAILED) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 593 | llvm::report_fatal_error("Failed to allocate memory for emitting " |
| 594 | "function codes\n" + ErrMsg); |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 595 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 596 | mpGVMem = mmap(mpCodeMem, MaxGlobalVarSize, |
| 597 | PROT_READ | PROT_WRITE, |
| 598 | MAP_PRIVATE | MAP_ANON, -1, 0); |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 599 | if (mpGVMem == MAP_FAILED) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 600 | llvm::report_fatal_error("Failed to allocate memory for emitting " |
| 601 | "global variables\n" + ErrMsg); |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 602 | */ |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 603 | return; |
| 604 | } |
| 605 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 606 | inline uint8_t *getCodeMemBase() const { |
| 607 | return reinterpret_cast<uint8_t*>(mpCodeMem); |
| 608 | } |
| 609 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 610 | // setMemoryWritable - When code generation is in progress, the code pages |
| 611 | // may need permissions changed. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 612 | void setMemoryWritable() { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 613 | ::mprotect(mpCodeMem, MaxCodeSize, PROT_READ | PROT_WRITE | PROT_EXEC); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 614 | return; |
| 615 | } |
| 616 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 617 | // When code generation is done and we're ready to start execution, the |
| 618 | // code pages may need permissions changed. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 619 | void setMemoryExecutable() { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 620 | ::mprotect(mpCodeMem, MaxCodeSize, PROT_READ | PROT_EXEC); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 621 | return; |
| 622 | } |
| 623 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 624 | // Setting this flag to true makes the memory manager garbage values over |
| 625 | // freed memory. This is useful for testing and debugging, and is to be |
| 626 | // turned on by default in debug mode. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 627 | void setPoisonMemory(bool poison) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 628 | // no effect |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 629 | return; |
| 630 | } |
| 631 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 632 | // Global Offset Table Management |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 633 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 634 | // If the current table requires a Global Offset Table, this method is |
| 635 | // invoked to allocate it. This method is required to set HasGOT to true. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 636 | void AllocateGOT() { |
| 637 | assert(mpGOTBase != NULL && "Cannot allocate the GOT multiple times"); |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 638 | mpGOTBase = allocateSGMemory(MaxGOTSize); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 639 | HasGOT = true; |
| 640 | return; |
| 641 | } |
| 642 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 643 | // If this is managing a Global Offset Table, this method should return a |
| 644 | // pointer to its base. |
| 645 | uint8_t *getGOTBase() const { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 646 | return mpGOTBase; |
| 647 | } |
| 648 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 649 | // Main Allocation Functions |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 650 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 651 | // When we start JITing a function, the JIT calls this method to allocate a |
| 652 | // block of free RWX memory, which returns a pointer to it. If the JIT wants |
| 653 | // to request a block of memory of at least a certain size, it passes that |
| 654 | // value as ActualSize, and this method returns a block with at least that |
| 655 | // much space. If the JIT doesn't know ahead of time how much space it will |
| 656 | // need to emit the function, it passes 0 for the ActualSize. In either |
| 657 | // case, this method is required to pass back the size of the allocated |
| 658 | // block through ActualSize. The JIT will be careful to not write more than |
| 659 | // the returned ActualSize bytes of memory. |
| 660 | uint8_t *startFunctionBody(const llvm::Function *F, uintptr_t &ActualSize) { |
| 661 | intptr_t FreeMemSize = getFreeCodeMemSize(); |
| 662 | if ((FreeMemSize < 0) || |
| 663 | (static_cast<uintptr_t>(FreeMemSize) < ActualSize)) |
| 664 | // The code size excesses our limit |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 665 | return NULL; |
| 666 | |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 667 | ActualSize = getFreeCodeMemSize(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 668 | return (getCodeMemBase() + mCurFuncMemIdx); |
| 669 | } |
| 670 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 671 | // This method is called by the JIT to allocate space for a function stub |
| 672 | // (used to handle limited branch displacements) while it is JIT compiling a |
| 673 | // function. For example, if foo calls bar, and if bar either needs to be |
| 674 | // lazily compiled or is a native function that exists too far away from the |
| 675 | // call site to work, this method will be used to make a thunk for it. The |
| 676 | // stub should be "close" to the current function body, but should not be |
| 677 | // included in the 'actualsize' returned by startFunctionBody. |
| 678 | uint8_t *allocateStub(const llvm::GlobalValue *F, unsigned StubSize, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 679 | unsigned Alignment) { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 680 | return allocateSGMemory(StubSize, Alignment); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 683 | // This method is called when the JIT is done codegen'ing the specified |
| 684 | // function. At this point we know the size of the JIT compiled function. |
| 685 | // This passes in FunctionStart (which was returned by the startFunctionBody |
| 686 | // method) and FunctionEnd which is a pointer to the actual end of the |
| 687 | // function. This method should mark the space allocated and remember where |
| 688 | // it is in case the client wants to deallocate it. |
| 689 | void endFunctionBody(const llvm::Function *F, uint8_t *FunctionStart, |
| 690 | uint8_t *FunctionEnd) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 691 | assert(FunctionEnd > FunctionStart); |
| 692 | assert(FunctionStart == (getCodeMemBase() + mCurFuncMemIdx) && |
| 693 | "Mismatched function start/end!"); |
| 694 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 695 | // Advance the pointer |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 696 | intptr_t FunctionCodeSize = FunctionEnd - FunctionStart; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 697 | assert(FunctionCodeSize <= getFreeCodeMemSize() && |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 698 | "Code size excess the limitation!"); |
| 699 | mCurFuncMemIdx += FunctionCodeSize; |
| 700 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 701 | // Record there's a function in our memory start from @FunctionStart |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 702 | assert(mFunctionMap.find(F) == mFunctionMap.end() && |
| 703 | "Function already emitted!"); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 704 | mFunctionMap.insert( |
| 705 | std::make_pair<const llvm::Function*, std::pair<void*, void*> >( |
| 706 | F, std::make_pair(FunctionStart, FunctionEnd))); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 707 | |
| 708 | return; |
| 709 | } |
| 710 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 711 | // Allocate a (function code) memory block of the given size. This method |
| 712 | // cannot be called between calls to startFunctionBody and endFunctionBody. |
| 713 | uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) { |
| 714 | if (getFreeCodeMemSize() < Size) |
| 715 | // The code size excesses our limit |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 716 | return NULL; |
| 717 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 718 | if (Alignment == 0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 719 | Alignment = 1; |
| 720 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 721 | uint8_t *result = getCodeMemBase() + mCurFuncMemIdx; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 722 | result = (uint8_t*) (((intptr_t) result + Alignment - 1) & |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 723 | ~(intptr_t) (Alignment - 1)); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 724 | |
| 725 | mCurFuncMemIdx = (result + Size) - getCodeMemBase(); |
| 726 | |
| 727 | return result; |
| 728 | } |
| 729 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 730 | // Allocate memory for a global variable. |
| 731 | uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 732 | if (getFreeGVMemSize() < Size) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 733 | // The code size excesses our limit |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 734 | LOGE("No Global Memory"); |
| 735 | return NULL; |
| 736 | } |
| 737 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 738 | if (Alignment == 0) |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 739 | Alignment = 1; |
| 740 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 741 | uint8_t *result = getGVMemBase() + mCurGVMemIdx; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 742 | result = (uint8_t*) (((intptr_t) result + Alignment - 1) & |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 743 | ~(intptr_t) (Alignment - 1)); |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 744 | |
| 745 | mCurGVMemIdx = (result + Size) - getGVMemBase(); |
| 746 | |
| 747 | return result; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 750 | // Free the specified function body. The argument must be the return value |
| 751 | // from a call to startFunctionBody() that hasn't been deallocated yet. This |
| 752 | // is never called when the JIT is currently emitting a function. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 753 | void deallocateFunctionBody(void *Body) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 754 | // linear search |
| 755 | uint8_t *FunctionStart = NULL, *FunctionEnd = NULL; |
| 756 | for (FunctionMapTy::iterator I = mFunctionMap.begin(), |
| 757 | E = mFunctionMap.end(); |
| 758 | I != E; |
| 759 | I++) |
| 760 | if (I->second.first == Body) { |
| 761 | FunctionStart = reinterpret_cast<uint8_t*>(I->second.first); |
| 762 | FunctionEnd = reinterpret_cast<uint8_t*>(I->second.second); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 763 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 764 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 765 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 766 | assert((FunctionStart == NULL) && "Memory is never allocated!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 767 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 768 | // free the memory |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 769 | intptr_t SizeNeedMove = (getCodeMemBase() + mCurFuncMemIdx) - FunctionEnd; |
| 770 | |
| 771 | assert(SizeNeedMove >= 0 && |
| 772 | "Internal error: CodeMemoryManager::mCurFuncMemIdx may not" |
| 773 | " be correctly calculated!"); |
| 774 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 775 | if (SizeNeedMove > 0) |
| 776 | // there's data behind deallocating function |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 777 | ::memmove(FunctionStart, FunctionEnd, SizeNeedMove); |
| 778 | mCurFuncMemIdx -= (FunctionEnd - FunctionStart); |
| 779 | |
| 780 | return; |
| 781 | } |
| 782 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 783 | // When we finished JITing the function, if exception handling is set, we |
| 784 | // emit the exception table. |
| 785 | uint8_t *startExceptionTable(const llvm::Function *F, |
| 786 | uintptr_t &ActualSize) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 787 | assert(false && "Exception is not allowed in our language specification"); |
| 788 | return NULL; |
| 789 | } |
| 790 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 791 | // This method is called when the JIT is done emitting the exception table. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 792 | void endExceptionTable(const llvm::Function *F, uint8_t *TableStart, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 793 | uint8_t *TableEnd, uint8_t *FrameRegister) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 794 | assert(false && "Exception is not allowed in our language specification"); |
| 795 | return; |
| 796 | } |
| 797 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 798 | // Free the specified exception table's memory. The argument must be the |
| 799 | // return value from a call to startExceptionTable() that hasn't been |
| 800 | // deallocated yet. This is never called when the JIT is currently emitting |
| 801 | // an exception table. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 802 | void deallocateExceptionTable(void *ET) { |
| 803 | assert(false && "Exception is not allowed in our language specification"); |
| 804 | return; |
| 805 | } |
| 806 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 807 | // Below are the methods we create |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 808 | void reset() { |
| 809 | mpGOTBase = NULL; |
| 810 | HasGOT = false; |
| 811 | |
| 812 | mCurFuncMemIdx = 0; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 813 | mCurSGMemIdx = MaxCodeSize - 1; |
| 814 | mCurGVMemIdx = 0; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 815 | |
| 816 | mFunctionMap.clear(); |
| 817 | |
| 818 | return; |
| 819 | } |
| 820 | |
| 821 | ~CodeMemoryManager() { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 822 | if (mpCodeMem != NULL) |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 823 | ::munmap(mpCodeMem, MaxCodeSize); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 824 | if (mpGVMem != NULL) |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 825 | ::munmap(mpGVMem, MaxGlobalVarSize); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 826 | return; |
| 827 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 828 | }; |
| 829 | // End of class CodeMemoryManager |
| 830 | ////////////////////////////////////////////////////////////////////////////// |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 831 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 832 | // The memory manager for code emitter |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 833 | llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 834 | CodeMemoryManager *createCodeMemoryManager() { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 835 | mCodeMemMgr.reset(new CodeMemoryManager()); |
| 836 | return mCodeMemMgr.get(); |
| 837 | } |
| 838 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 839 | ////////////////////////////////////////////////////////////////////////////// |
| 840 | // Code emitter |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 841 | class CodeEmitter : public llvm::JITCodeEmitter { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 842 | public: |
| 843 | typedef llvm::DenseMap<const llvm::GlobalValue*, void*> GlobalAddressMapTy; |
| 844 | typedef GlobalAddressMapTy::const_iterator global_addresses_const_iterator; |
| 845 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 846 | GlobalAddressMapTy mGlobalAddressMap; |
| 847 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 848 | private: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 849 | CodeMemoryManager *mpMemMgr; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 850 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 851 | // The JITInfo for the target we are compiling to |
| 852 | const llvm::Target *mpTarget; |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 853 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 854 | llvm::TargetJITInfo *mpTJI; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 855 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 856 | const llvm::TargetData *mpTD; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 857 | |
| 858 | class EmittedFunctionCode { |
| 859 | public: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 860 | // Beginning of the function's allocation. |
| 861 | void *FunctionBody; |
| 862 | |
| 863 | // The address the function's code actually starts at. |
| 864 | void *Code; |
| 865 | |
| 866 | // The size of the function code |
| 867 | int Size; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 868 | |
| 869 | EmittedFunctionCode() : FunctionBody(NULL), Code(NULL) { return; } |
| 870 | }; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 871 | EmittedFunctionCode *mpCurEmitFunction; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 872 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 873 | typedef std::map<const std::string, |
| 874 | EmittedFunctionCode*> EmittedFunctionsMapTy; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 875 | EmittedFunctionsMapTy mEmittedFunctions; |
| 876 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 877 | // This vector is a mapping from MBB ID's to their address. It is filled in |
| 878 | // by the StartMachineBasicBlock callback and queried by the |
| 879 | // getMachineBasicBlockAddress callback. |
| 880 | std::vector<uintptr_t> mMBBLocations; |
| 881 | |
| 882 | // The constant pool for the current function. |
| 883 | llvm::MachineConstantPool *mpConstantPool; |
| 884 | |
| 885 | // A pointer to the first entry in the constant pool. |
| 886 | void *mpConstantPoolBase; |
| 887 | |
| 888 | // Addresses of individual constant pool entries. |
| 889 | llvm::SmallVector<uintptr_t, 8> mConstPoolAddresses; |
| 890 | |
| 891 | // The jump tables for the current function. |
| 892 | llvm::MachineJumpTableInfo *mpJumpTable; |
| 893 | |
| 894 | // A pointer to the first entry in the jump table. |
| 895 | void *mpJumpTableBase; |
| 896 | |
| 897 | // When outputting a function stub in the context of some other function, we |
| 898 | // save BufferBegin/BufferEnd/CurBufferPtr here. |
| 899 | uint8_t *mpSavedBufferBegin, *mpSavedBufferEnd, *mpSavedCurBufferPtr; |
| 900 | |
| 901 | // These are the relocations that the function needs, as emitted. |
| 902 | std::vector<llvm::MachineRelocation> mRelocations; |
| 903 | |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 904 | std::vector<oBCCRelocEntry> mCachingRelocations; |
| 905 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 906 | // This vector is a mapping from Label ID's to their address. |
| 907 | llvm::DenseMap<llvm::MCSymbol*, uintptr_t> mLabelLocations; |
| 908 | |
| 909 | // Machine module info for exception informations |
| 910 | llvm::MachineModuleInfo *mpMMI; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 911 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 912 | // Replace an existing mapping for GV with a new address. This updates both |
| 913 | // maps as required. If Addr is null, the entry for the global is removed |
| 914 | // from the mappings. |
| 915 | void *UpdateGlobalMapping(const llvm::GlobalValue *GV, void *Addr) { |
| 916 | if (Addr == NULL) { |
| 917 | // Removing mapping |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 918 | GlobalAddressMapTy::iterator I = mGlobalAddressMap.find(GV); |
| 919 | void *OldVal; |
| 920 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 921 | if (I == mGlobalAddressMap.end()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 922 | OldVal = NULL; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 923 | } else { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 924 | OldVal = I->second; |
| 925 | mGlobalAddressMap.erase(I); |
| 926 | } |
| 927 | |
| 928 | return OldVal; |
| 929 | } |
| 930 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 931 | void *&CurVal = mGlobalAddressMap[GV]; |
| 932 | void *OldVal = CurVal; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 933 | |
| 934 | CurVal = Addr; |
| 935 | |
| 936 | return OldVal; |
| 937 | } |
| 938 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 939 | // Tell the execution engine that the specified global is at the specified |
| 940 | // location. This is used internally as functions are JIT'd and as global |
| 941 | // variables are laid out in memory. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 942 | void AddGlobalMapping(const llvm::GlobalValue *GV, void *Addr) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 943 | void *&CurVal = mGlobalAddressMap[GV]; |
| 944 | assert((CurVal == 0 || Addr == 0) && |
| 945 | "GlobalMapping already established!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 946 | CurVal = Addr; |
| 947 | return; |
| 948 | } |
| 949 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 950 | // This returns the address of the specified global value if it is has |
| 951 | // already been codegen'd, otherwise it returns null. |
| 952 | void *GetPointerToGlobalIfAvailable(const llvm::GlobalValue *GV) { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 953 | GlobalAddressMapTy::iterator I = mGlobalAddressMap.find(GV); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 954 | return ((I != mGlobalAddressMap.end()) ? I->second : NULL); |
| 955 | } |
| 956 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 957 | unsigned int GetConstantPoolSizeInBytes(llvm::MachineConstantPool *MCP) { |
| 958 | const std::vector<llvm::MachineConstantPoolEntry> &Constants = |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 959 | MCP->getConstants(); |
| 960 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 961 | if (Constants.empty()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 962 | return 0; |
| 963 | |
| 964 | unsigned int Size = 0; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 965 | for (int i = 0, e = Constants.size(); i != e; i++) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 966 | llvm::MachineConstantPoolEntry CPE = Constants[i]; |
| 967 | unsigned int AlignMask = CPE.getAlignment() - 1; |
| 968 | Size = (Size + AlignMask) & ~AlignMask; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 969 | const llvm::Type *Ty = CPE.getType(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 970 | Size += mpTD->getTypeAllocSize(Ty); |
| 971 | } |
| 972 | |
| 973 | return Size; |
| 974 | } |
| 975 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 976 | // This function converts a Constant* into a GenericValue. The interesting |
| 977 | // part is if C is a ConstantExpr. |
| 978 | void GetConstantValue(const llvm::Constant *C, llvm::GenericValue &Result) { |
| 979 | if (C->getValueID() == llvm::Value::UndefValueVal) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 980 | return; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 981 | else if (C->getValueID() == llvm::Value::ConstantExprVal) { |
| 982 | const llvm::ConstantExpr *CE = (llvm::ConstantExpr*) C; |
| 983 | const llvm::Constant *Op0 = CE->getOperand(0); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 984 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 985 | switch (CE->getOpcode()) { |
| 986 | case llvm::Instruction::GetElementPtr: { |
| 987 | // Compute the index |
| 988 | llvm::SmallVector<llvm::Value*, 8> Indices(CE->op_begin() + 1, |
| 989 | CE->op_end()); |
| 990 | uint64_t Offset = mpTD->getIndexedOffset(Op0->getType(), |
| 991 | &Indices[0], |
| 992 | Indices.size()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 993 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 994 | GetConstantValue(Op0, Result); |
| 995 | Result.PointerVal = |
| 996 | static_cast<uint8_t*>(Result.PointerVal) + Offset; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 997 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 998 | return; |
| 999 | } |
| 1000 | case llvm::Instruction::Trunc: { |
| 1001 | uint32_t BitWidth = |
| 1002 | llvm::cast<llvm::IntegerType>(CE->getType())->getBitWidth(); |
| 1003 | |
| 1004 | GetConstantValue(Op0, Result); |
| 1005 | Result.IntVal = Result.IntVal.trunc(BitWidth); |
| 1006 | |
| 1007 | return; |
| 1008 | } |
| 1009 | case llvm::Instruction::ZExt: { |
| 1010 | uint32_t BitWidth = |
| 1011 | llvm::cast<llvm::IntegerType>(CE->getType())->getBitWidth(); |
| 1012 | |
| 1013 | GetConstantValue(Op0, Result); |
| 1014 | Result.IntVal = Result.IntVal.zext(BitWidth); |
| 1015 | |
| 1016 | return; |
| 1017 | } |
| 1018 | case llvm::Instruction::SExt: { |
| 1019 | uint32_t BitWidth = |
| 1020 | llvm::cast<llvm::IntegerType>(CE->getType())->getBitWidth(); |
| 1021 | |
| 1022 | GetConstantValue(Op0, Result); |
| 1023 | Result.IntVal = Result.IntVal.sext(BitWidth); |
| 1024 | |
| 1025 | return; |
| 1026 | } |
| 1027 | case llvm::Instruction::FPTrunc: { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 1028 | // TODO(all): fixme: long double |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1029 | GetConstantValue(Op0, Result); |
| 1030 | Result.FloatVal = static_cast<float>(Result.DoubleVal); |
| 1031 | return; |
| 1032 | } |
| 1033 | case llvm::Instruction::FPExt: { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 1034 | // TODO(all): fixme: long double |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1035 | GetConstantValue(Op0, Result); |
| 1036 | Result.DoubleVal = static_cast<double>(Result.FloatVal); |
| 1037 | return; |
| 1038 | } |
| 1039 | case llvm::Instruction::UIToFP: { |
| 1040 | GetConstantValue(Op0, Result); |
| 1041 | if (CE->getType()->isFloatTy()) |
| 1042 | Result.FloatVal = |
| 1043 | static_cast<float>(Result.IntVal.roundToDouble()); |
| 1044 | else if (CE->getType()->isDoubleTy()) |
| 1045 | Result.DoubleVal = Result.IntVal.roundToDouble(); |
| 1046 | else if (CE->getType()->isX86_FP80Ty()) { |
| 1047 | const uint64_t zero[] = { 0, 0 }; |
| 1048 | llvm::APFloat apf(llvm::APInt(80, 2, zero)); |
| 1049 | apf.convertFromAPInt(Result.IntVal, |
| 1050 | false, |
| 1051 | llvm::APFloat::rmNearestTiesToEven); |
| 1052 | Result.IntVal = apf.bitcastToAPInt(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1053 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1054 | return; |
| 1055 | } |
| 1056 | case llvm::Instruction::SIToFP: { |
| 1057 | GetConstantValue(Op0, Result); |
| 1058 | if (CE->getType()->isFloatTy()) |
| 1059 | Result.FloatVal = |
| 1060 | static_cast<float>(Result.IntVal.signedRoundToDouble()); |
| 1061 | else if (CE->getType()->isDoubleTy()) |
| 1062 | Result.DoubleVal = Result.IntVal.signedRoundToDouble(); |
| 1063 | else if (CE->getType()->isX86_FP80Ty()) { |
| 1064 | const uint64_t zero[] = { 0, 0 }; |
| 1065 | llvm::APFloat apf = llvm::APFloat(llvm::APInt(80, 2, zero)); |
| 1066 | apf.convertFromAPInt(Result.IntVal, |
| 1067 | true, |
| 1068 | llvm::APFloat::rmNearestTiesToEven); |
| 1069 | Result.IntVal = apf.bitcastToAPInt(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1070 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1071 | return; |
| 1072 | } |
| 1073 | // double->APInt conversion handles sign |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1074 | case llvm::Instruction::FPToUI: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1075 | case llvm::Instruction::FPToSI: { |
| 1076 | uint32_t BitWidth = |
| 1077 | llvm::cast<llvm::IntegerType>(CE->getType())->getBitWidth(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1078 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1079 | GetConstantValue(Op0, Result); |
| 1080 | if (Op0->getType()->isFloatTy()) |
| 1081 | Result.IntVal = |
| 1082 | llvm::APIntOps::RoundFloatToAPInt(Result.FloatVal, BitWidth); |
| 1083 | else if (Op0->getType()->isDoubleTy()) |
| 1084 | Result.IntVal = |
| 1085 | llvm::APIntOps::RoundDoubleToAPInt(Result.DoubleVal, |
| 1086 | BitWidth); |
| 1087 | else if (Op0->getType()->isX86_FP80Ty()) { |
| 1088 | llvm::APFloat apf = llvm::APFloat(Result.IntVal); |
| 1089 | uint64_t V; |
| 1090 | bool Ignored; |
| 1091 | apf.convertToInteger(&V, |
| 1092 | BitWidth, |
| 1093 | CE->getOpcode() == llvm::Instruction::FPToSI, |
| 1094 | llvm::APFloat::rmTowardZero, |
| 1095 | &Ignored); |
| 1096 | Result.IntVal = V; // endian? |
| 1097 | } |
| 1098 | return; |
| 1099 | } |
| 1100 | case llvm::Instruction::PtrToInt: { |
| 1101 | uint32_t PtrWidth = mpTD->getPointerSizeInBits(); |
| 1102 | |
| 1103 | GetConstantValue(Op0, Result); |
| 1104 | Result.IntVal = llvm::APInt(PtrWidth, uintptr_t |
| 1105 | (Result.PointerVal)); |
| 1106 | |
| 1107 | return; |
| 1108 | } |
| 1109 | case llvm::Instruction::IntToPtr: { |
| 1110 | uint32_t PtrWidth = mpTD->getPointerSizeInBits(); |
| 1111 | |
| 1112 | GetConstantValue(Op0, Result); |
| 1113 | if (PtrWidth != Result.IntVal.getBitWidth()) |
| 1114 | Result.IntVal = Result.IntVal.zextOrTrunc(PtrWidth); |
| 1115 | assert(Result.IntVal.getBitWidth() <= 64 && "Bad pointer width"); |
| 1116 | |
| 1117 | Result.PointerVal = |
| 1118 | llvm::PointerTy( |
| 1119 | static_cast<uintptr_t>(Result.IntVal.getZExtValue())); |
| 1120 | |
| 1121 | return; |
| 1122 | } |
| 1123 | case llvm::Instruction::BitCast: { |
| 1124 | GetConstantValue(Op0, Result); |
| 1125 | const llvm::Type *DestTy = CE->getType(); |
| 1126 | |
| 1127 | switch (Op0->getType()->getTypeID()) { |
| 1128 | case llvm::Type::IntegerTyID: { |
| 1129 | assert(DestTy->isFloatingPointTy() && "invalid bitcast"); |
| 1130 | if (DestTy->isFloatTy()) |
| 1131 | Result.FloatVal = Result.IntVal.bitsToFloat(); |
| 1132 | else if (DestTy->isDoubleTy()) |
| 1133 | Result.DoubleVal = Result.IntVal.bitsToDouble(); |
| 1134 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1135 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1136 | case llvm::Type::FloatTyID: { |
| 1137 | assert(DestTy->isIntegerTy(32) && "Invalid bitcast"); |
| 1138 | Result.IntVal.floatToBits(Result.FloatVal); |
| 1139 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1140 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1141 | case llvm::Type::DoubleTyID: { |
| 1142 | assert(DestTy->isIntegerTy(64) && "Invalid bitcast"); |
| 1143 | Result.IntVal.doubleToBits(Result.DoubleVal); |
| 1144 | break; |
| 1145 | } |
| 1146 | case llvm::Type::PointerTyID: { |
| 1147 | assert(DestTy->isPointerTy() && "Invalid bitcast"); |
| 1148 | break; // getConstantValue(Op0) above already converted it |
| 1149 | } |
| 1150 | default: { |
| 1151 | llvm_unreachable("Invalid bitcast operand"); |
| 1152 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1153 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1154 | return; |
| 1155 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1156 | case llvm::Instruction::Add: |
| 1157 | case llvm::Instruction::FAdd: |
| 1158 | case llvm::Instruction::Sub: |
| 1159 | case llvm::Instruction::FSub: |
| 1160 | case llvm::Instruction::Mul: |
| 1161 | case llvm::Instruction::FMul: |
| 1162 | case llvm::Instruction::UDiv: |
| 1163 | case llvm::Instruction::SDiv: |
| 1164 | case llvm::Instruction::URem: |
| 1165 | case llvm::Instruction::SRem: |
| 1166 | case llvm::Instruction::And: |
| 1167 | case llvm::Instruction::Or: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1168 | case llvm::Instruction::Xor: { |
| 1169 | llvm::GenericValue LHS, RHS; |
| 1170 | GetConstantValue(Op0, LHS); |
| 1171 | GetConstantValue(CE->getOperand(1), RHS); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1172 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1173 | switch (Op0->getType()->getTypeID()) { |
| 1174 | case llvm::Type::IntegerTyID: { |
| 1175 | switch (CE->getOpcode()) { |
| 1176 | case llvm::Instruction::Add: { |
| 1177 | Result.IntVal = LHS.IntVal + RHS.IntVal; |
| 1178 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1179 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1180 | case llvm::Instruction::Sub: { |
| 1181 | Result.IntVal = LHS.IntVal - RHS.IntVal; |
| 1182 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1183 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1184 | case llvm::Instruction::Mul: { |
| 1185 | Result.IntVal = LHS.IntVal * RHS.IntVal; |
| 1186 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1187 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1188 | case llvm::Instruction::UDiv: { |
| 1189 | Result.IntVal = LHS.IntVal.udiv(RHS.IntVal); |
| 1190 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1191 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1192 | case llvm::Instruction::SDiv: { |
| 1193 | Result.IntVal = LHS.IntVal.sdiv(RHS.IntVal); |
| 1194 | break; |
| 1195 | } |
| 1196 | case llvm::Instruction::URem: { |
| 1197 | Result.IntVal = LHS.IntVal.urem(RHS.IntVal); |
| 1198 | break; |
| 1199 | } |
| 1200 | case llvm::Instruction::SRem: { |
| 1201 | Result.IntVal = LHS.IntVal.srem(RHS.IntVal); |
| 1202 | break; |
| 1203 | } |
| 1204 | case llvm::Instruction::And: { |
| 1205 | Result.IntVal = LHS.IntVal & RHS.IntVal; |
| 1206 | break; |
| 1207 | } |
| 1208 | case llvm::Instruction::Or: { |
| 1209 | Result.IntVal = LHS.IntVal | RHS.IntVal; |
| 1210 | break; |
| 1211 | } |
| 1212 | case llvm::Instruction::Xor: { |
| 1213 | Result.IntVal = LHS.IntVal ^ RHS.IntVal; |
| 1214 | break; |
| 1215 | } |
| 1216 | default: { |
| 1217 | llvm_unreachable("Invalid integer opcode"); |
| 1218 | } |
| 1219 | } |
| 1220 | break; |
| 1221 | } |
| 1222 | case llvm::Type::FloatTyID: { |
| 1223 | switch (CE->getOpcode()) { |
| 1224 | case llvm::Instruction::FAdd: { |
| 1225 | Result.FloatVal = LHS.FloatVal + RHS.FloatVal; |
| 1226 | break; |
| 1227 | } |
| 1228 | case llvm::Instruction::FSub: { |
| 1229 | Result.FloatVal = LHS.FloatVal - RHS.FloatVal; |
| 1230 | break; |
| 1231 | } |
| 1232 | case llvm::Instruction::FMul: { |
| 1233 | Result.FloatVal = LHS.FloatVal * RHS.FloatVal; |
| 1234 | break; |
| 1235 | } |
| 1236 | case llvm::Instruction::FDiv: { |
| 1237 | Result.FloatVal = LHS.FloatVal / RHS.FloatVal; |
| 1238 | break; |
| 1239 | } |
| 1240 | case llvm::Instruction::FRem: { |
| 1241 | Result.FloatVal = ::fmodf(LHS.FloatVal, RHS.FloatVal); |
| 1242 | break; |
| 1243 | } |
| 1244 | default: { |
| 1245 | llvm_unreachable("Invalid float opcode"); |
| 1246 | } |
| 1247 | } |
| 1248 | break; |
| 1249 | } |
| 1250 | case llvm::Type::DoubleTyID: { |
| 1251 | switch (CE->getOpcode()) { |
| 1252 | case llvm::Instruction::FAdd: { |
| 1253 | Result.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; |
| 1254 | break; |
| 1255 | } |
| 1256 | case llvm::Instruction::FSub: { |
| 1257 | Result.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; |
| 1258 | break; |
| 1259 | } |
| 1260 | case llvm::Instruction::FMul: { |
| 1261 | Result.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; |
| 1262 | break; |
| 1263 | } |
| 1264 | case llvm::Instruction::FDiv: { |
| 1265 | Result.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; |
| 1266 | break; |
| 1267 | } |
| 1268 | case llvm::Instruction::FRem: { |
| 1269 | Result.DoubleVal = ::fmod(LHS.DoubleVal, RHS.DoubleVal); |
| 1270 | break; |
| 1271 | } |
| 1272 | default: { |
| 1273 | llvm_unreachable("Invalid double opcode"); |
| 1274 | } |
| 1275 | } |
| 1276 | break; |
| 1277 | } |
| 1278 | case llvm::Type::X86_FP80TyID: |
| 1279 | case llvm::Type::PPC_FP128TyID: |
| 1280 | case llvm::Type::FP128TyID: { |
| 1281 | llvm::APFloat apfLHS = llvm::APFloat(LHS.IntVal); |
| 1282 | switch (CE->getOpcode()) { |
| 1283 | case llvm::Instruction::FAdd: { |
| 1284 | apfLHS.add(llvm::APFloat(RHS.IntVal), |
| 1285 | llvm::APFloat::rmNearestTiesToEven); |
| 1286 | break; |
| 1287 | } |
| 1288 | case llvm::Instruction::FSub: { |
| 1289 | apfLHS.subtract(llvm::APFloat(RHS.IntVal), |
| 1290 | llvm::APFloat::rmNearestTiesToEven); |
| 1291 | break; |
| 1292 | } |
| 1293 | case llvm::Instruction::FMul: { |
| 1294 | apfLHS.multiply(llvm::APFloat(RHS.IntVal), |
| 1295 | llvm::APFloat::rmNearestTiesToEven); |
| 1296 | break; |
| 1297 | } |
| 1298 | case llvm::Instruction::FDiv: { |
| 1299 | apfLHS.divide(llvm::APFloat(RHS.IntVal), |
| 1300 | llvm::APFloat::rmNearestTiesToEven); |
| 1301 | break; |
| 1302 | } |
| 1303 | case llvm::Instruction::FRem: { |
| 1304 | apfLHS.mod(llvm::APFloat(RHS.IntVal), |
| 1305 | llvm::APFloat::rmNearestTiesToEven); |
| 1306 | break; |
| 1307 | } |
| 1308 | default: { |
| 1309 | llvm_unreachable("Invalid long double opcode"); |
| 1310 | } |
| 1311 | } |
| 1312 | Result.IntVal = apfLHS.bitcastToAPInt(); |
| 1313 | break; |
| 1314 | } |
| 1315 | default: { |
| 1316 | llvm_unreachable("Bad add type!"); |
| 1317 | } |
| 1318 | } // End switch (Op0->getType()->getTypeID()) |
| 1319 | return; |
| 1320 | } |
| 1321 | default: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1322 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1323 | } |
| 1324 | } // End switch (CE->getOpcode()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1325 | |
| 1326 | std::string msg; |
| 1327 | llvm::raw_string_ostream Msg(msg); |
| 1328 | Msg << "ConstantExpr not handled: " << *CE; |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1329 | llvm::report_fatal_error(Msg.str()); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1330 | } // C->getValueID() == llvm::Value::ConstantExprVal |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1331 | |
| 1332 | switch (C->getType()->getTypeID()) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1333 | case llvm::Type::FloatTyID: { |
| 1334 | Result.FloatVal = |
| 1335 | llvm::cast<llvm::ConstantFP>(C)->getValueAPF().convertToFloat(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1336 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1337 | } |
| 1338 | case llvm::Type::DoubleTyID: { |
| 1339 | Result.DoubleVal = |
| 1340 | llvm::cast<llvm::ConstantFP>(C)->getValueAPF().convertToDouble(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1341 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1342 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1343 | case llvm::Type::X86_FP80TyID: |
| 1344 | case llvm::Type::FP128TyID: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1345 | case llvm::Type::PPC_FP128TyID: { |
| 1346 | Result.IntVal = |
| 1347 | llvm::cast<llvm::ConstantFP>(C)->getValueAPF().bitcastToAPInt(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1348 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1349 | } |
| 1350 | case llvm::Type::IntegerTyID: { |
| 1351 | Result.IntVal = |
| 1352 | llvm::cast<llvm::ConstantInt>(C)->getValue(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1353 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1354 | } |
| 1355 | case llvm::Type::PointerTyID: { |
| 1356 | switch (C->getValueID()) { |
| 1357 | case llvm::Value::ConstantPointerNullVal: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1358 | Result.PointerVal = NULL; |
| 1359 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1360 | } |
| 1361 | case llvm::Value::FunctionVal: { |
| 1362 | const llvm::Function *F = static_cast<const llvm::Function*>(C); |
| 1363 | Result.PointerVal = |
| 1364 | GetPointerToFunctionOrStub(const_cast<llvm::Function*>(F)); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1365 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1366 | } |
| 1367 | case llvm::Value::GlobalVariableVal: { |
| 1368 | const llvm::GlobalVariable *GV = |
| 1369 | static_cast<const llvm::GlobalVariable*>(C); |
| 1370 | Result.PointerVal = |
| 1371 | GetOrEmitGlobalVariable(const_cast<llvm::GlobalVariable*>(GV)); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1372 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1373 | } |
| 1374 | case llvm::Value::BlockAddressVal: { |
| 1375 | assert(false && "JIT does not support address-of-label yet!"); |
| 1376 | } |
| 1377 | default: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1378 | llvm_unreachable("Unknown constant pointer type!"); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1379 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1380 | } |
| 1381 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1382 | } |
| 1383 | default: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1384 | std::string msg; |
| 1385 | llvm::raw_string_ostream Msg(msg); |
| 1386 | Msg << "ERROR: Constant unimplemented for type: " << *C->getType(); |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1387 | llvm::report_fatal_error(Msg.str()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1388 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1389 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1390 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1391 | return; |
| 1392 | } |
| 1393 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1394 | // Stores the data in @Val of type @Ty at address @Addr. |
| 1395 | void StoreValueToMemory(const llvm::GenericValue &Val, void *Addr, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1396 | const llvm::Type *Ty) { |
| 1397 | const unsigned int StoreBytes = mpTD->getTypeStoreSize(Ty); |
| 1398 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1399 | switch (Ty->getTypeID()) { |
| 1400 | case llvm::Type::IntegerTyID: { |
| 1401 | const llvm::APInt &IntVal = Val.IntVal; |
| 1402 | assert(((IntVal.getBitWidth() + 7) / 8 >= StoreBytes) && |
| 1403 | "Integer too small!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1404 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1405 | const uint8_t *Src = |
| 1406 | reinterpret_cast<const uint8_t*>(IntVal.getRawData()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1407 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1408 | if (llvm::sys::isLittleEndianHost()) { |
| 1409 | // Little-endian host - the source is ordered from LSB to MSB. |
| 1410 | // Order the destination from LSB to MSB: Do a straight copy. |
| 1411 | memcpy(Addr, Src, StoreBytes); |
| 1412 | } else { |
| 1413 | // Big-endian host - the source is an array of 64 bit words |
| 1414 | // ordered from LSW to MSW. |
| 1415 | // |
| 1416 | // Each word is ordered from MSB to LSB. |
| 1417 | // |
| 1418 | // Order the destination from MSB to LSB: |
| 1419 | // Reverse the word order, but not the bytes in a word. |
| 1420 | unsigned int i = StoreBytes; |
| 1421 | while (i > sizeof(uint64_t)) { |
| 1422 | i -= sizeof(uint64_t); |
| 1423 | ::memcpy(reinterpret_cast<uint8_t*>(Addr) + i, |
| 1424 | Src, |
| 1425 | sizeof(uint64_t)); |
| 1426 | Src += sizeof(uint64_t); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1427 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1428 | ::memcpy(Addr, Src + sizeof(uint64_t) - i, i); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1429 | } |
| 1430 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1431 | } |
| 1432 | case llvm::Type::FloatTyID: { |
| 1433 | *reinterpret_cast<float*>(Addr) = Val.FloatVal; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1434 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1435 | } |
| 1436 | case llvm::Type::DoubleTyID: { |
| 1437 | *reinterpret_cast<double*>(Addr) = Val.DoubleVal; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1438 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1439 | } |
| 1440 | case llvm::Type::X86_FP80TyID: { |
| 1441 | memcpy(Addr, Val.IntVal.getRawData(), 10); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1442 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1443 | } |
| 1444 | case llvm::Type::PointerTyID: { |
| 1445 | // Ensure 64 bit target pointers are fully initialized on 32 bit |
| 1446 | // hosts. |
| 1447 | if (StoreBytes != sizeof(llvm::PointerTy)) |
| 1448 | memset(Addr, 0, StoreBytes); |
| 1449 | *((llvm::PointerTy*) Addr) = Val.PointerVal; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1450 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1451 | } |
| 1452 | default: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1453 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1454 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1457 | if (llvm::sys::isLittleEndianHost() != mpTD->isLittleEndian()) |
| 1458 | std::reverse(reinterpret_cast<uint8_t*>(Addr), |
| 1459 | reinterpret_cast<uint8_t*>(Addr) + StoreBytes); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1460 | |
| 1461 | return; |
| 1462 | } |
| 1463 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1464 | // Recursive function to apply a @Constant value into the specified memory |
| 1465 | // location @Addr. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1466 | void InitializeConstantToMemory(const llvm::Constant *C, void *Addr) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1467 | switch (C->getValueID()) { |
| 1468 | case llvm::Value::UndefValueVal: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1469 | // Nothing to do |
| 1470 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1471 | } |
| 1472 | case llvm::Value::ConstantVectorVal: { |
| 1473 | // dynamic cast may hurt performance |
| 1474 | const llvm::ConstantVector *CP = (llvm::ConstantVector*) C; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1475 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1476 | unsigned int ElementSize = mpTD->getTypeAllocSize |
| 1477 | (CP->getType()->getElementType()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1478 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1479 | for (int i = 0, e = CP->getNumOperands(); i != e;i++) |
| 1480 | InitializeConstantToMemory( |
| 1481 | CP->getOperand(i), |
| 1482 | reinterpret_cast<uint8_t*>(Addr) + i * ElementSize); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1483 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1484 | } |
| 1485 | case llvm::Value::ConstantAggregateZeroVal: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1486 | memset(Addr, 0, (size_t) mpTD->getTypeAllocSize(C->getType())); |
| 1487 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1488 | } |
| 1489 | case llvm::Value::ConstantArrayVal: { |
| 1490 | const llvm::ConstantArray *CPA = (llvm::ConstantArray*) C; |
| 1491 | unsigned int ElementSize = mpTD->getTypeAllocSize |
| 1492 | (CPA->getType()->getElementType()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1493 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1494 | for (int i = 0, e = CPA->getNumOperands(); i != e; i++) |
| 1495 | InitializeConstantToMemory( |
| 1496 | CPA->getOperand(i), |
| 1497 | reinterpret_cast<uint8_t*>(Addr) + i * ElementSize); |
| 1498 | break; |
| 1499 | } |
| 1500 | case llvm::Value::ConstantStructVal: { |
| 1501 | const llvm::ConstantStruct *CPS = |
| 1502 | static_cast<const llvm::ConstantStruct*>(C); |
| 1503 | const llvm::StructLayout *SL = mpTD->getStructLayout |
| 1504 | (llvm::cast<llvm::StructType>(CPS->getType())); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1505 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1506 | for (int i = 0, e = CPS->getNumOperands(); i != e; i++) |
| 1507 | InitializeConstantToMemory( |
| 1508 | CPS->getOperand(i), |
| 1509 | reinterpret_cast<uint8_t*>(Addr) + SL->getElementOffset(i)); |
| 1510 | break; |
| 1511 | } |
| 1512 | default: { |
| 1513 | if (C->getType()->isFirstClassType()) { |
| 1514 | llvm::GenericValue Val; |
| 1515 | GetConstantValue(C, Val); |
| 1516 | StoreValueToMemory(Val, Addr, C->getType()); |
| 1517 | } else { |
| 1518 | llvm_unreachable("Unknown constant type to initialize memory " |
| 1519 | "with!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1520 | } |
| 1521 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1522 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1523 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1524 | return; |
| 1525 | } |
| 1526 | |
| 1527 | void emitConstantPool(llvm::MachineConstantPool *MCP) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1528 | if (mpTJI->hasCustomConstantPool()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1529 | return; |
| 1530 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1531 | // Constant pool address resolution is handled by the target itself in ARM |
| 1532 | // (TargetJITInfo::hasCustomConstantPool() returns true). |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1533 | #if !defined(PROVIDE_ARM_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1534 | const std::vector<llvm::MachineConstantPoolEntry> &Constants = |
| 1535 | MCP->getConstants(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1536 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1537 | if (Constants.empty()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1538 | return; |
| 1539 | |
| 1540 | unsigned Size = GetConstantPoolSizeInBytes(MCP); |
| 1541 | unsigned Align = MCP->getConstantPoolAlignment(); |
| 1542 | |
| 1543 | mpConstantPoolBase = allocateSpace(Size, Align); |
| 1544 | mpConstantPool = MCP; |
| 1545 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1546 | if (mpConstantPoolBase == NULL) |
| 1547 | return; // out of memory |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1548 | |
| 1549 | unsigned Offset = 0; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1550 | for (int i = 0, e = Constants.size(); i != e; i++) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1551 | llvm::MachineConstantPoolEntry CPE = Constants[i]; |
| 1552 | unsigned AlignMask = CPE.getAlignment() - 1; |
| 1553 | Offset = (Offset + AlignMask) & ~AlignMask; |
| 1554 | |
| 1555 | uintptr_t CAddr = (uintptr_t) mpConstantPoolBase + Offset; |
| 1556 | mConstPoolAddresses.push_back(CAddr); |
| 1557 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1558 | if (CPE.isMachineConstantPoolEntry()) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1559 | llvm::report_fatal_error |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1560 | ("Initialize memory with machine specific constant pool" |
| 1561 | " entry has not been implemented!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1562 | |
| 1563 | InitializeConstantToMemory(CPE.Val.ConstVal, (void*) CAddr); |
| 1564 | |
| 1565 | const llvm::Type *Ty = CPE.Val.ConstVal->getType(); |
| 1566 | Offset += mpTD->getTypeAllocSize(Ty); |
| 1567 | } |
| 1568 | #endif |
| 1569 | return; |
| 1570 | } |
| 1571 | |
| 1572 | void initJumpTableInfo(llvm::MachineJumpTableInfo *MJTI) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1573 | if (mpTJI->hasCustomJumpTables()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1574 | return; |
| 1575 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1576 | const std::vector<llvm::MachineJumpTableEntry> &JT = |
| 1577 | MJTI->getJumpTables(); |
| 1578 | if (JT.empty()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1579 | return; |
| 1580 | |
| 1581 | unsigned NumEntries = 0; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1582 | for (int i = 0, e = JT.size(); i != e; i++) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1583 | NumEntries += JT[i].MBBs.size(); |
| 1584 | |
| 1585 | unsigned EntrySize = MJTI->getEntrySize(*mpTD); |
| 1586 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1587 | mpJumpTable = MJTI; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1588 | mpJumpTableBase = allocateSpace(NumEntries * EntrySize, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1589 | MJTI->getEntryAlignment(*mpTD)); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1590 | |
| 1591 | return; |
| 1592 | } |
| 1593 | |
| 1594 | void emitJumpTableInfo(llvm::MachineJumpTableInfo *MJTI) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1595 | if (mpTJI->hasCustomJumpTables()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1596 | return; |
| 1597 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1598 | const std::vector<llvm::MachineJumpTableEntry> &JT = |
| 1599 | MJTI->getJumpTables(); |
| 1600 | if (JT.empty() || mpJumpTableBase == 0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1601 | return; |
| 1602 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1603 | assert(llvm::TargetMachine::getRelocationModel() == llvm::Reloc::Static && |
| 1604 | (MJTI->getEntrySize(*mpTD) == sizeof(mpTD /* a pointer type */)) && |
| 1605 | "Cross JIT'ing?"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1606 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1607 | // For each jump table, map each target in the jump table to the |
| 1608 | // address of an emitted MachineBasicBlock. |
| 1609 | intptr_t *SlotPtr = reinterpret_cast<intptr_t*>(mpJumpTableBase); |
| 1610 | for (int i = 0, ie = JT.size(); i != ie; i++) { |
| 1611 | const std::vector<llvm::MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 1612 | // Store the address of the basic block for this jump table slot in the |
| 1613 | // memory we allocated for the jump table in 'initJumpTableInfo' |
| 1614 | for (int j = 0, je = MBBs.size(); j != je; j++) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1615 | *SlotPtr++ = getMachineBasicBlockAddress(MBBs[j]); |
| 1616 | } |
| 1617 | } |
| 1618 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1619 | void *GetPointerToGlobal(llvm::GlobalValue *V, void *Reference, |
| 1620 | bool MayNeedFarStub) { |
| 1621 | switch (V->getValueID()) { |
| 1622 | case llvm::Value::FunctionVal: { |
| 1623 | llvm::Function *F = (llvm::Function*) V; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1624 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1625 | // If we have code, go ahead and return that. |
| 1626 | if (void *ResultPtr = GetPointerToGlobalIfAvailable(F)) |
| 1627 | return ResultPtr; |
Shih-wei Liao | 800e9c2 | 2010-04-18 16:08:16 -0700 | [diff] [blame] | 1628 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1629 | if (void *FnStub = GetLazyFunctionStubIfAvailable(F)) |
| 1630 | // Return the function stub if it's already created. |
| 1631 | // We do this first so that: |
| 1632 | // we're returning the same address for the function as any |
| 1633 | // previous call. |
| 1634 | // |
| 1635 | // TODO(llvm.org): Yes, this is wrong. The lazy stub isn't |
| 1636 | // guaranteed to be close enough to call. |
| 1637 | return FnStub; |
Shih-wei Liao | 800e9c2 | 2010-04-18 16:08:16 -0700 | [diff] [blame] | 1638 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1639 | // If we know the target can handle arbitrary-distance calls, try to |
| 1640 | // return a direct pointer. |
| 1641 | if (!MayNeedFarStub) { |
| 1642 | // |
| 1643 | // x86_64 architecture may encounter the bug: |
| 1644 | // http://llvm.org/bugs/show_bug.cgi?id=5201 |
| 1645 | // which generate instruction "call" instead of "callq". |
| 1646 | // |
| 1647 | // And once the real address of stub is greater than 64-bit |
| 1648 | // long, the replacement will truncate to 32-bit resulting a |
| 1649 | // serious problem. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1650 | #if !defined(__x86_64__) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1651 | // If this is an external function pointer, we can force the JIT |
| 1652 | // to 'compile' it, which really just adds it to the map. |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 1653 | if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { |
| 1654 | return GetPointerToFunction(F, /* AbortOnFailure = */false); |
| 1655 | // Changing to false because wanting to allow later calls to |
| 1656 | // mpTJI->relocate() without aborting. For caching purpose |
| 1657 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1658 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1659 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1660 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1661 | // Otherwise, we may need a to emit a stub, and, conservatively, we |
| 1662 | // always do so. |
| 1663 | return GetLazyFunctionStub(F); |
| 1664 | break; |
| 1665 | } |
| 1666 | case llvm::Value::GlobalVariableVal: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1667 | return GetOrEmitGlobalVariable((llvm::GlobalVariable*) V); |
| 1668 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1669 | } |
| 1670 | case llvm::Value::GlobalAliasVal: { |
| 1671 | llvm::GlobalAlias *GA = (llvm::GlobalAlias*) V; |
| 1672 | const llvm::GlobalValue *GV = GA->resolveAliasedGlobal(false); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1673 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1674 | switch (GV->getValueID()) { |
| 1675 | case llvm::Value::FunctionVal: { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 1676 | // TODO(all): is there's any possibility that the function is not |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1677 | // code-gen'd? |
| 1678 | return GetPointerToFunction( |
| 1679 | static_cast<const llvm::Function*>(GV), |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 1680 | /* AbortOnFailure = */false); |
| 1681 | // Changing to false because wanting to allow later calls to |
| 1682 | // mpTJI->relocate() without aborting. For caching purpose |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1683 | break; |
| 1684 | } |
| 1685 | case llvm::Value::GlobalVariableVal: { |
| 1686 | if (void *P = mGlobalAddressMap[GV]) |
| 1687 | return P; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1688 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1689 | llvm::GlobalVariable *GVar = (llvm::GlobalVariable*) GV; |
| 1690 | EmitGlobalVariable(GVar); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1691 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1692 | return mGlobalAddressMap[GV]; |
| 1693 | break; |
| 1694 | } |
| 1695 | case llvm::Value::GlobalAliasVal: { |
| 1696 | assert(false && "Alias should be resolved ultimately!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1700 | } |
| 1701 | default: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1702 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1703 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1704 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1705 | llvm_unreachable("Unknown type of global value!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1708 | // If the specified function has been code-gen'd, return a pointer to the |
| 1709 | // function. If not, compile it, or use a stub to implement lazy compilation |
| 1710 | // if available. |
| 1711 | void *GetPointerToFunctionOrStub(llvm::Function *F) { |
| 1712 | // If we have already code generated the function, just return the |
| 1713 | // address. |
| 1714 | if (void *Addr = GetPointerToGlobalIfAvailable(F)) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1715 | return Addr; |
| 1716 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1717 | // Get a stub if the target supports it. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1718 | return GetLazyFunctionStub(F); |
| 1719 | } |
| 1720 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1721 | typedef llvm::DenseMap<const llvm::Function*, |
| 1722 | void*> FunctionToLazyStubMapTy; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1723 | FunctionToLazyStubMapTy mFunctionToLazyStubMap; |
| 1724 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1725 | void *GetLazyFunctionStubIfAvailable(llvm::Function *F) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1726 | return mFunctionToLazyStubMap.lookup(F); |
| 1727 | } |
| 1728 | |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1729 | std::set<const llvm::Function*> PendingFunctions; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1730 | void *GetLazyFunctionStub(llvm::Function *F) { |
| 1731 | // If we already have a lazy stub for this function, recycle it. |
| 1732 | void *&Stub = mFunctionToLazyStubMap[F]; |
| 1733 | if (Stub) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1734 | return Stub; |
| 1735 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1736 | // In any cases, we should NOT resolve function at runtime (though we are |
| 1737 | // able to). We resolve this right now. |
| 1738 | void *Actual = NULL; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 1739 | if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { |
| 1740 | Actual = GetPointerToFunction(F, /* AbortOnFailure = */false); |
| 1741 | // Changing to false because wanting to allow later calls to |
| 1742 | // mpTJI->relocate() without aborting. For caching purpose |
| 1743 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1744 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1745 | // Codegen a new stub, calling the actual address of the external |
| 1746 | // function, if it was resolved. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1747 | llvm::TargetJITInfo::StubLayout SL = mpTJI->getStubLayout(); |
| 1748 | startGVStub(F, SL.Size, SL.Alignment); |
| 1749 | Stub = mpTJI->emitFunctionStub(F, Actual, *this); |
| 1750 | finishGVStub(); |
| 1751 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1752 | // We really want the address of the stub in the GlobalAddressMap for the |
| 1753 | // JIT, not the address of the external function. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1754 | UpdateGlobalMapping(F, Stub); |
| 1755 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1756 | if (!Actual) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1757 | PendingFunctions.insert(F); |
| 1758 | else |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1759 | Disassemble(F->getName(), reinterpret_cast<uint8_t*>(Stub), |
| 1760 | SL.Size, true); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1761 | |
| 1762 | return Stub; |
| 1763 | } |
| 1764 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1765 | void *GetPointerToFunction(const llvm::Function *F, bool AbortOnFailure) { |
| 1766 | void *Addr = GetPointerToGlobalIfAvailable(F); |
| 1767 | if (Addr) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1768 | return Addr; |
| 1769 | |
| 1770 | assert((F->isDeclaration() || F->hasAvailableExternallyLinkage()) && |
| 1771 | "Internal error: only external defined function routes here!"); |
| 1772 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1773 | // Handle the failure resolution by ourselves. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1774 | Addr = GetPointerToNamedSymbol(F->getName().str().c_str(), |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1775 | /* AbortOnFailure = */ false); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1776 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1777 | // If we resolved the symbol to a null address (eg. a weak external) |
| 1778 | // return a null pointer let the application handle it. |
| 1779 | if (Addr == NULL) { |
| 1780 | if (AbortOnFailure) |
| 1781 | llvm::report_fatal_error("Could not resolve external function " |
| 1782 | "address: " + F->getName()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1783 | else |
| 1784 | return NULL; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1785 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1786 | |
| 1787 | AddGlobalMapping(F, Addr); |
| 1788 | |
| 1789 | return Addr; |
| 1790 | } |
| 1791 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1792 | void *GetPointerToNamedSymbol(const std::string &Name, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1793 | bool AbortOnFailure) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1794 | if (void *Addr = FindRuntimeFunction(Name.c_str())) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1795 | return Addr; |
| 1796 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1797 | if (mpSymbolLookupFn) |
| 1798 | if (void *Addr = mpSymbolLookupFn(mpSymbolLookupContext, Name.c_str())) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1799 | return Addr; |
| 1800 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1801 | if (AbortOnFailure) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1802 | llvm::report_fatal_error("Program used external symbol '" + Name + |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1803 | "' which could not be resolved!"); |
| 1804 | |
| 1805 | return NULL; |
| 1806 | } |
| 1807 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1808 | // Return the address of the specified global variable, possibly emitting it |
| 1809 | // to memory if needed. This is used by the Emitter. |
| 1810 | void *GetOrEmitGlobalVariable(const llvm::GlobalVariable *GV) { |
| 1811 | void *Ptr = GetPointerToGlobalIfAvailable(GV); |
| 1812 | if (Ptr) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1813 | return Ptr; |
| 1814 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1815 | if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) { |
| 1816 | // If the global is external, just remember the address. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1817 | Ptr = GetPointerToNamedSymbol(GV->getName().str(), true); |
| 1818 | AddGlobalMapping(GV, Ptr); |
| 1819 | } else { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1820 | // If the global hasn't been emitted to memory yet, allocate space and |
| 1821 | // emit it into memory. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1822 | Ptr = GetMemoryForGV(GV); |
| 1823 | AddGlobalMapping(GV, Ptr); |
| 1824 | EmitGlobalVariable(GV); |
| 1825 | } |
| 1826 | |
| 1827 | return Ptr; |
| 1828 | } |
| 1829 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1830 | // This method abstracts memory allocation of global variable so that the |
| 1831 | // JIT can allocate thread local variables depending on the target. |
| 1832 | void *GetMemoryForGV(const llvm::GlobalVariable *GV) { |
| 1833 | void *Ptr; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1834 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1835 | const llvm::Type *GlobalType = GV->getType()->getElementType(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1836 | size_t S = mpTD->getTypeAllocSize(GlobalType); |
| 1837 | size_t A = mpTD->getPreferredAlignment(GV); |
| 1838 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1839 | if (GV->isThreadLocal()) { |
| 1840 | // We can support TLS by |
| 1841 | // |
| 1842 | // Ptr = TJI.allocateThreadLocalMemory(S); |
| 1843 | // |
| 1844 | // But I tend not to. |
| 1845 | // (should we disable this in the front-end (i.e., slang)?). |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1846 | llvm::report_fatal_error |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1847 | ("Compilation of Thread Local Storage (TLS) is disabled!"); |
| 1848 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1849 | } else if (mpTJI->allocateSeparateGVMemory()) { |
| 1850 | if (A <= 8) { |
| 1851 | Ptr = malloc(S); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1852 | } else { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1853 | // Allocate (S + A) bytes of memory, then use an aligned pointer |
| 1854 | // within that space. |
| 1855 | Ptr = malloc(S + A); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1856 | unsigned int MisAligned = ((intptr_t) Ptr & (A - 1)); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1857 | Ptr = reinterpret_cast<uint8_t*>(Ptr) + |
| 1858 | (MisAligned ? (A - MisAligned) : 0); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1859 | } |
| 1860 | } else { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1861 | Ptr = allocateGlobal(S, A); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | return Ptr; |
| 1865 | } |
| 1866 | |
| 1867 | void EmitGlobalVariable(const llvm::GlobalVariable *GV) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1868 | void *GA = GetPointerToGlobalIfAvailable(GV); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1869 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1870 | if (GV->isThreadLocal()) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1871 | llvm::report_fatal_error |
| 1872 | ("We don't support Thread Local Storage (TLS)!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1873 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1874 | if (GA == NULL) { |
| 1875 | // If it's not already specified, allocate memory for the global. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1876 | GA = GetMemoryForGV(GV); |
| 1877 | AddGlobalMapping(GV, GA); |
| 1878 | } |
| 1879 | |
| 1880 | InitializeConstantToMemory(GV->getInitializer(), GA); |
| 1881 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1882 | // You can do some statistics on global variable here. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1883 | return; |
| 1884 | } |
| 1885 | |
| 1886 | typedef std::map<llvm::AssertingVH<llvm::GlobalValue>, void* |
| 1887 | > GlobalToIndirectSymMapTy; |
| 1888 | GlobalToIndirectSymMapTy GlobalToIndirectSymMap; |
| 1889 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1890 | void *GetPointerToGVIndirectSym(llvm::GlobalValue *V, void *Reference) { |
| 1891 | // Make sure GV is emitted first, and create a stub containing the fully |
| 1892 | // resolved address. |
| 1893 | void *GVAddress = GetPointerToGlobal(V, Reference, false); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1894 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1895 | // If we already have a stub for this global variable, recycle it. |
| 1896 | void *&IndirectSym = GlobalToIndirectSymMap[V]; |
| 1897 | // Otherwise, codegen a new indirect symbol. |
| 1898 | if (!IndirectSym) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1899 | IndirectSym = mpTJI->emitGlobalValueIndirectSym(V, GVAddress, *this); |
| 1900 | |
| 1901 | return IndirectSym; |
| 1902 | } |
| 1903 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1904 | // This is the equivalent of FunctionToLazyStubMap for external functions. |
| 1905 | // |
| 1906 | // TODO(llvm.org): Of course, external functions don't need a lazy stub. |
| 1907 | // It's actually here to make it more likely that far calls |
| 1908 | // succeed, but no single stub can guarantee that. I'll |
| 1909 | // remove this in a subsequent checkin when I actually fix |
| 1910 | // far calls. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1911 | std::map<void*, void*> ExternalFnToStubMap; |
| 1912 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1913 | // Return a stub for the function at the specified address. |
| 1914 | void *GetExternalFunctionStub(void *FnAddr) { |
| 1915 | void *&Stub = ExternalFnToStubMap[FnAddr]; |
| 1916 | if (Stub) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1917 | return Stub; |
| 1918 | |
| 1919 | llvm::TargetJITInfo::StubLayout SL = mpTJI->getStubLayout(); |
| 1920 | startGVStub(0, SL.Size, SL.Alignment); |
| 1921 | Stub = mpTJI->emitFunctionStub(0, FnAddr, *this); |
| 1922 | finishGVStub(); |
| 1923 | |
| 1924 | return Stub; |
| 1925 | } |
| 1926 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1927 | #if defined(USE_DISASSEMBLER) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1928 | const llvm::MCAsmInfo *mpAsmInfo; |
| 1929 | const llvm::MCDisassembler *mpDisassmbler; |
| 1930 | llvm::MCInstPrinter *mpIP; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1931 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1932 | class BufferMemoryObject : public llvm::MemoryObject { |
| 1933 | private: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1934 | const uint8_t *mBytes; |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1935 | uint64_t mLength; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1936 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1937 | public: |
| 1938 | BufferMemoryObject(const uint8_t *Bytes, uint64_t Length) : |
| 1939 | mBytes(Bytes), mLength(Length) { } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1940 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1941 | uint64_t getBase() const { return 0; } |
| 1942 | uint64_t getExtent() const { return mLength; } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1943 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1944 | int readByte(uint64_t Addr, uint8_t *Byte) const { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1945 | if (Addr > getExtent()) |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1946 | return -1; |
| 1947 | *Byte = mBytes[Addr]; |
| 1948 | return 0; |
| 1949 | } |
| 1950 | }; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1951 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1952 | void Disassemble(const llvm::StringRef &Name, uint8_t *Start, |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1953 | size_t Length, bool IsStub) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1954 | llvm::raw_fd_ostream *OS; |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 1955 | #if defined(USE_DISASSEMBLER_FILE) |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1956 | std::string ErrorInfo; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1957 | OS = new llvm::raw_fd_ostream("/data/local/tmp/out.S", |
| 1958 | ErrorInfo, |
| 1959 | llvm::raw_fd_ostream::F_Append); |
| 1960 | if (!ErrorInfo.empty()) { // some errors occurred |
| 1961 | // LOGE("Error in creating disassembly file"); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1962 | delete OS; |
| 1963 | return; |
| 1964 | } |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 1965 | #else |
| 1966 | OS = &llvm::outs(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1967 | #endif |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1968 | *OS << "JIT: Disassembled code: " << Name << ((IsStub) ? " (stub)" : "") |
| 1969 | << "\n"; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1970 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1971 | if (mpAsmInfo == NULL) |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1972 | mpAsmInfo = mpTarget->createAsmInfo(Triple); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1973 | if (mpDisassmbler == NULL) |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1974 | mpDisassmbler = mpTarget->createMCDisassembler(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1975 | if (mpIP == NULL) |
| 1976 | mpIP = mpTarget->createMCInstPrinter(mpAsmInfo->getAssemblerDialect(), |
| 1977 | *mpAsmInfo); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1978 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1979 | const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Start, |
| 1980 | Length); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1981 | uint64_t Size; |
| 1982 | uint64_t Index; |
| 1983 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1984 | for (Index = 0; Index < Length; Index += Size) { |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1985 | llvm::MCInst Inst; |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 1986 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1987 | if (mpDisassmbler->getInstruction(Inst, Size, *BufferMObj, Index, |
| 1988 | /* REMOVED */ llvm::nulls())) { |
| 1989 | (*OS).indent(4) |
| 1990 | .write("0x", 2) |
| 1991 | .write_hex((uint32_t) Start + Index) |
| 1992 | .write(':'); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1993 | mpIP->printInst(&Inst, *OS); |
| 1994 | *OS << "\n"; |
| 1995 | } else { |
| 1996 | if (Size == 0) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1997 | Size = 1; // skip illegible bytes |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 1998 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2001 | *OS << "\n"; |
| 2002 | delete BufferMObj; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2003 | |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2004 | #if defined(USE_DISASSEMBLER_FILE) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2005 | // If you want the disassemble results write to file, uncomment this. |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2006 | OS->close(); |
| 2007 | delete OS; |
| 2008 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2009 | return; |
| 2010 | } |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2011 | #else |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2012 | inline void Disassemble(const std::string &Name, uint8_t *Start, |
| 2013 | size_t Length, bool IsStub) { |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2014 | return; |
| 2015 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2016 | #endif // defined(USE_DISASSEMBLER) |
| 2017 | |
| 2018 | // Resolver to undefined symbol in CodeEmitter |
| 2019 | BCCSymbolLookupFn mpSymbolLookupFn; |
| 2020 | void *mpSymbolLookupContext; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2021 | |
| 2022 | public: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2023 | // Will take the ownership of @MemMgr |
| 2024 | explicit CodeEmitter(CodeMemoryManager *pMemMgr) |
| 2025 | : mpMemMgr(pMemMgr), |
| 2026 | mpTarget(NULL), |
| 2027 | mpTJI(NULL), |
| 2028 | mpTD(NULL), |
| 2029 | mpCurEmitFunction(NULL), |
| 2030 | mpConstantPool(NULL), |
| 2031 | mpJumpTable(NULL), |
| 2032 | mpMMI(NULL), |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2033 | #if defined(USE_DISASSEMBLER) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2034 | mpAsmInfo(NULL), |
| 2035 | mpDisassmbler(NULL), |
| 2036 | mpIP(NULL), |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2037 | #endif |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2038 | mpSymbolLookupFn(NULL), |
| 2039 | mpSymbolLookupContext(NULL) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2040 | return; |
| 2041 | } |
| 2042 | |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 2043 | inline global_addresses_const_iterator global_address_begin() const { |
| 2044 | return mGlobalAddressMap.begin(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2045 | } |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 2046 | inline global_addresses_const_iterator global_address_end() const { |
| 2047 | return mGlobalAddressMap.end(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 2050 | std::vector<oBCCRelocEntry> const &getCachingRelocations() const { |
| 2051 | return mCachingRelocations; |
| 2052 | } |
| 2053 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2054 | void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2055 | mpSymbolLookupFn = pFn; |
| 2056 | mpSymbolLookupContext = pContext; |
| 2057 | return; |
| 2058 | } |
| 2059 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2060 | void setTargetMachine(llvm::TargetMachine &TM) { |
| 2061 | // Set Target |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2062 | mpTarget = &TM.getTarget(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2063 | // Set TargetJITInfo |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2064 | mpTJI = TM.getJITInfo(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2065 | // set TargetData |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2066 | mpTD = TM.getTargetData(); |
| 2067 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2068 | assert(!mpTJI->needsGOT() && "We don't support GOT needed target!"); |
| 2069 | |
| 2070 | return; |
| 2071 | } |
| 2072 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2073 | // This callback is invoked when the specified function is about to be code |
| 2074 | // generated. This initializes the BufferBegin/End/Ptr fields. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2075 | void startFunction(llvm::MachineFunction &F) { |
| 2076 | uintptr_t ActualSize = 0; |
| 2077 | |
| 2078 | mpMemMgr->setMemoryWritable(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2079 | |
| 2080 | // BufferBegin, BufferEnd and CurBufferPtr are all inherited from class |
| 2081 | // MachineCodeEmitter, which is the super class of the class |
| 2082 | // JITCodeEmitter. |
| 2083 | // |
| 2084 | // BufferBegin/BufferEnd - Pointers to the start and end of the memory |
| 2085 | // allocated for this code buffer. |
| 2086 | // |
| 2087 | // CurBufferPtr - Pointer to the next byte of memory to fill when emitting |
| 2088 | // code. This is guranteed to be in the range |
| 2089 | // [BufferBegin, BufferEnd]. If this pointer is at |
| 2090 | // BufferEnd, it will never move due to code emission, and |
| 2091 | // all code emission requests will be ignored (this is the |
| 2092 | // buffer overflow condition). |
| 2093 | BufferBegin = CurBufferPtr = |
| 2094 | mpMemMgr->startFunctionBody(F.getFunction(), ActualSize); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2095 | BufferEnd = BufferBegin + ActualSize; |
| 2096 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2097 | if (mpCurEmitFunction == NULL) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2098 | mpCurEmitFunction = new EmittedFunctionCode(); |
| 2099 | mpCurEmitFunction->FunctionBody = BufferBegin; |
| 2100 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2101 | // Ensure the constant pool/jump table info is at least 4-byte aligned. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2102 | emitAlignment(16); |
| 2103 | |
| 2104 | emitConstantPool(F.getConstantPool()); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2105 | if (llvm::MachineJumpTableInfo *MJTI = F.getJumpTableInfo()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2106 | initJumpTableInfo(MJTI); |
| 2107 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2108 | // About to start emitting the machine code for the function. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2109 | emitAlignment(std::max(F.getFunction()->getAlignment(), 8U)); |
| 2110 | |
| 2111 | UpdateGlobalMapping(F.getFunction(), CurBufferPtr); |
| 2112 | |
| 2113 | mpCurEmitFunction->Code = CurBufferPtr; |
| 2114 | |
| 2115 | mMBBLocations.clear(); |
| 2116 | |
| 2117 | return; |
| 2118 | } |
| 2119 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2120 | // This callback is invoked when the specified function has finished code |
| 2121 | // generation. If a buffer overflow has occurred, this method returns true |
| 2122 | // (the callee is required to try again). |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2123 | bool finishFunction(llvm::MachineFunction &F) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2124 | if (CurBufferPtr == BufferEnd) { |
| 2125 | // No enough memory |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2126 | mpMemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr); |
| 2127 | return false; |
| 2128 | } |
| 2129 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2130 | if (llvm::MachineJumpTableInfo *MJTI = F.getJumpTableInfo()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2131 | emitJumpTableInfo(MJTI); |
| 2132 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2133 | // FnStart is the start of the text, not the start of the constant pool |
| 2134 | // and other per-function data. |
| 2135 | uint8_t *FnStart = |
| 2136 | reinterpret_cast<uint8_t*>( |
| 2137 | GetPointerToGlobalIfAvailable(F.getFunction())); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2138 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2139 | // FnEnd is the end of the function's machine code. |
| 2140 | uint8_t *FnEnd = CurBufferPtr; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2141 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2142 | if (!mRelocations.empty()) { |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 2143 | ptrdiff_t BufferOffset = BufferBegin - mpMemMgr->getCodeMemBase(); |
| 2144 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2145 | // Resolve the relocations to concrete pointers. |
| 2146 | for (int i = 0, e = mRelocations.size(); i != e; i++) { |
| 2147 | llvm::MachineRelocation &MR = mRelocations[i]; |
| 2148 | void *ResultPtr = NULL; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2149 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2150 | if (!MR.letTargetResolve()) { |
| 2151 | if (MR.isExternalSymbol()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2152 | ResultPtr = GetPointerToNamedSymbol(MR.getExternalSymbol(), true); |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 2153 | |
| 2154 | if (MR.mayNeedFarStub()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2155 | ResultPtr = GetExternalFunctionStub(ResultPtr); |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 2156 | } |
| 2157 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2158 | } else if (MR.isGlobalValue()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2159 | ResultPtr = GetPointerToGlobal(MR.getGlobalValue(), |
| 2160 | BufferBegin |
| 2161 | + MR.getMachineCodeOffset(), |
| 2162 | MR.mayNeedFarStub()); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2163 | } else if (MR.isIndirectSymbol()) { |
| 2164 | ResultPtr = |
| 2165 | GetPointerToGVIndirectSym( |
| 2166 | MR.getGlobalValue(), |
| 2167 | BufferBegin + MR.getMachineCodeOffset()); |
| 2168 | } else if (MR.isBasicBlock()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2169 | ResultPtr = |
| 2170 | (void*) getMachineBasicBlockAddress(MR.getBasicBlock()); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2171 | } else if (MR.isConstantPoolIndex()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2172 | ResultPtr = |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2173 | (void*) getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2174 | } else { |
| 2175 | assert(MR.isJumpTableIndex() && "Unknown type of relocation"); |
| 2176 | ResultPtr = |
| 2177 | (void*) getJumpTableEntryAddress(MR.getJumpTableIndex()); |
| 2178 | } |
| 2179 | |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 2180 | if (!MR.isExternalSymbol() || MR.mayNeedFarStub()) { |
| 2181 | // TODO(logan): Cache external symbol relocation entry. |
| 2182 | // Currently, we are not caching them. But since Android |
| 2183 | // system is using prelink, it is not a problem. |
| 2184 | |
| 2185 | // Cache the relocation result address |
| 2186 | mCachingRelocations.push_back( |
| 2187 | oBCCRelocEntry(MR.getMachineCodeOffset() + BufferOffset, |
| 2188 | MR.getRelocationType(), |
| 2189 | ResultPtr)); |
| 2190 | } |
| 2191 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2192 | MR.setResultPointer(ResultPtr); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | mpTJI->relocate(BufferBegin, &mRelocations[0], mRelocations.size(), |
| 2197 | mpMemMgr->getGOTBase()); |
| 2198 | } |
| 2199 | |
| 2200 | mpMemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2201 | // CurBufferPtr may have moved beyond FnEnd, due to memory allocation for |
| 2202 | // global variables that were referenced in the relocations. |
| 2203 | if (CurBufferPtr == BufferEnd) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2204 | return false; |
| 2205 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2206 | // Now that we've succeeded in emitting the function. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2207 | mpCurEmitFunction->Size = CurBufferPtr - BufferBegin; |
| 2208 | BufferBegin = CurBufferPtr = 0; |
| 2209 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2210 | if (F.getFunction()->hasName()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2211 | mEmittedFunctions[F.getFunction()->getNameStr()] = mpCurEmitFunction; |
| 2212 | mpCurEmitFunction = NULL; |
| 2213 | |
| 2214 | mRelocations.clear(); |
| 2215 | mConstPoolAddresses.clear(); |
| 2216 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2217 | if (mpMMI) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2218 | mpMMI->EndFunction(); |
| 2219 | |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2220 | updateFunctionStub(F.getFunction()); |
| 2221 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2222 | // Mark code region readable and executable if it's not so already. |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 2223 | mpMemMgr->setMemoryExecutable(); |
| 2224 | |
| 2225 | Disassemble(F.getFunction()->getName(), FnStart, FnEnd - FnStart, false); |
| 2226 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2227 | return false; |
| 2228 | } |
| 2229 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2230 | void startGVStub(const llvm::GlobalValue *GV, unsigned StubSize, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2231 | unsigned Alignment) { |
| 2232 | mpSavedBufferBegin = BufferBegin; |
| 2233 | mpSavedBufferEnd = BufferEnd; |
| 2234 | mpSavedCurBufferPtr = CurBufferPtr; |
| 2235 | |
| 2236 | BufferBegin = CurBufferPtr = mpMemMgr->allocateStub(GV, StubSize, |
| 2237 | Alignment); |
| 2238 | BufferEnd = BufferBegin + StubSize + 1; |
| 2239 | |
| 2240 | return; |
| 2241 | } |
| 2242 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2243 | void startGVStub(void *Buffer, unsigned StubSize) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2244 | mpSavedBufferBegin = BufferBegin; |
| 2245 | mpSavedBufferEnd = BufferEnd; |
| 2246 | mpSavedCurBufferPtr = CurBufferPtr; |
| 2247 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2248 | BufferBegin = CurBufferPtr = reinterpret_cast<uint8_t *>(Buffer); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2249 | BufferEnd = BufferBegin + StubSize + 1; |
| 2250 | |
| 2251 | return; |
| 2252 | } |
| 2253 | |
| 2254 | void finishGVStub() { |
| 2255 | assert(CurBufferPtr != BufferEnd && "Stub overflowed allocated space."); |
| 2256 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2257 | // restore |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2258 | BufferBegin = mpSavedBufferBegin; |
| 2259 | BufferEnd = mpSavedBufferEnd; |
| 2260 | CurBufferPtr = mpSavedCurBufferPtr; |
| 2261 | |
| 2262 | return; |
| 2263 | } |
| 2264 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2265 | // Allocates and fills storage for an indirect GlobalValue, and returns the |
| 2266 | // address. |
| 2267 | void *allocIndirectGV(const llvm::GlobalValue *GV, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2268 | const uint8_t *Buffer, size_t Size, |
| 2269 | unsigned Alignment) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2270 | uint8_t *IndGV = mpMemMgr->allocateStub(GV, Size, Alignment); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2271 | memcpy(IndGV, Buffer, Size); |
| 2272 | return IndGV; |
| 2273 | } |
| 2274 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2275 | // Emits a label |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2276 | void emitLabel(llvm::MCSymbol *Label) { |
| 2277 | mLabelLocations[Label] = getCurrentPCValue(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2278 | return; |
| 2279 | } |
| 2280 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2281 | // Allocate memory for a global. Unlike allocateSpace, this method does not |
| 2282 | // allocate memory in the current output buffer, because a global may live |
| 2283 | // longer than the current function. |
| 2284 | void *allocateGlobal(uintptr_t Size, unsigned Alignment) { |
| 2285 | // Delegate this call through the memory manager. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2286 | return mpMemMgr->allocateGlobal(Size, Alignment); |
| 2287 | } |
| 2288 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2289 | // This should be called by the target when a new basic block is about to be |
| 2290 | // emitted. This way the MCE knows where the start of the block is, and can |
| 2291 | // implement getMachineBasicBlockAddress. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2292 | void StartMachineBasicBlock(llvm::MachineBasicBlock *MBB) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2293 | if (mMBBLocations.size() <= (unsigned) MBB->getNumber()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2294 | mMBBLocations.resize((MBB->getNumber() + 1) * 2); |
| 2295 | mMBBLocations[MBB->getNumber()] = getCurrentPCValue(); |
| 2296 | return; |
| 2297 | } |
| 2298 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2299 | // Whenever a relocatable address is needed, it should be noted with this |
| 2300 | // interface. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2301 | void addRelocation(const llvm::MachineRelocation &MR) { |
| 2302 | mRelocations.push_back(MR); |
| 2303 | return; |
| 2304 | } |
| 2305 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2306 | // Return the address of the @Index entry in the constant pool that was |
| 2307 | // last emitted with the emitConstantPool method. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2308 | uintptr_t getConstantPoolEntryAddress(unsigned Index) const { |
| 2309 | assert(Index < mpConstantPool->getConstants().size() && |
| 2310 | "Invalid constant pool index!"); |
| 2311 | return mConstPoolAddresses[Index]; |
| 2312 | } |
| 2313 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2314 | // Return the address of the jump table with index @Index in the function |
| 2315 | // that last called initJumpTableInfo. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2316 | uintptr_t getJumpTableEntryAddress(unsigned Index) const { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2317 | const std::vector<llvm::MachineJumpTableEntry> &JT = |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2318 | mpJumpTable->getJumpTables(); |
| 2319 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2320 | assert((Index < JT.size()) && "Invalid jump table index!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2321 | |
| 2322 | unsigned int Offset = 0; |
| 2323 | unsigned int EntrySize = mpJumpTable->getEntrySize(*mpTD); |
| 2324 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2325 | for (unsigned i = 0; i < Index; i++) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2326 | Offset += JT[i].MBBs.size(); |
| 2327 | Offset *= EntrySize; |
| 2328 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2329 | return (uintptr_t)(reinterpret_cast<uint8_t*>(mpJumpTableBase) + Offset); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2332 | // Return the address of the specified MachineBasicBlock, only usable after |
| 2333 | // the label for the MBB has been emitted. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2334 | uintptr_t getMachineBasicBlockAddress(llvm::MachineBasicBlock *MBB) const { |
| 2335 | assert(mMBBLocations.size() > (unsigned) MBB->getNumber() && |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2336 | mMBBLocations[MBB->getNumber()] && |
| 2337 | "MBB not emitted!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2338 | return mMBBLocations[MBB->getNumber()]; |
| 2339 | } |
| 2340 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2341 | // Return the address of the specified LabelID, only usable after the |
| 2342 | // LabelID has been emitted. |
| 2343 | uintptr_t getLabelAddress(llvm::MCSymbol *Label) const { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2344 | assert(mLabelLocations.count(Label) && "Label not emitted!"); |
| 2345 | return mLabelLocations.find(Label)->second; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2348 | // Specifies the MachineModuleInfo object. This is used for exception |
| 2349 | // handling purposes. |
| 2350 | void setModuleInfo(llvm::MachineModuleInfo *Info) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2351 | mpMMI = Info; |
| 2352 | return; |
| 2353 | } |
| 2354 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2355 | void updateFunctionStub(const llvm::Function *F) { |
| 2356 | // Get the empty stub we generated earlier. |
| 2357 | void *Stub; |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2358 | std::set<const llvm::Function*>::iterator I = PendingFunctions.find(F); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2359 | if (I != PendingFunctions.end()) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2360 | Stub = mFunctionToLazyStubMap[F]; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2361 | else |
| 2362 | return; |
| 2363 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2364 | void *Addr = GetPointerToGlobalIfAvailable(F); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2365 | |
| 2366 | assert(Addr != Stub && |
| 2367 | "Function must have non-stub address to be updated."); |
| 2368 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2369 | // Tell the target jit info to rewrite the stub at the specified address, |
| 2370 | // rather than creating a new one. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2371 | llvm::TargetJITInfo::StubLayout SL = mpTJI->getStubLayout(); |
| 2372 | startGVStub(Stub, SL.Size); |
| 2373 | mpTJI->emitFunctionStub(F, Addr, *this); |
| 2374 | finishGVStub(); |
| 2375 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2376 | Disassemble(F->getName(), reinterpret_cast<uint8_t*>(Stub), |
| 2377 | SL.Size, true); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2378 | |
| 2379 | PendingFunctions.erase(I); |
| 2380 | |
| 2381 | return; |
| 2382 | } |
| 2383 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2384 | // Once you finish the compilation on a translation unit, you can call this |
| 2385 | // function to recycle the memory (which is used at compilation time and not |
| 2386 | // needed for runtime). |
| 2387 | // |
| 2388 | // NOTE: You should not call this funtion until the code-gen passes for a |
| 2389 | // given module is done. Otherwise, the results is undefined and may |
| 2390 | // cause the system crash! |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2391 | void releaseUnnecessary() { |
| 2392 | mMBBLocations.clear(); |
| 2393 | mLabelLocations.clear(); |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2394 | mGlobalAddressMap.clear(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2395 | mFunctionToLazyStubMap.clear(); |
| 2396 | GlobalToIndirectSymMap.clear(); |
| 2397 | ExternalFnToStubMap.clear(); |
| 2398 | PendingFunctions.clear(); |
| 2399 | |
| 2400 | return; |
| 2401 | } |
| 2402 | |
| 2403 | void reset() { |
| 2404 | releaseUnnecessary(); |
| 2405 | |
| 2406 | mpSymbolLookupFn = NULL; |
| 2407 | mpSymbolLookupContext = NULL; |
| 2408 | |
| 2409 | mpTJI = NULL; |
| 2410 | mpTD = NULL; |
| 2411 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2412 | for (EmittedFunctionsMapTy::iterator I = mEmittedFunctions.begin(), |
| 2413 | E = mEmittedFunctions.end(); |
| 2414 | I != E; |
| 2415 | I++) |
| 2416 | if (I->second != NULL) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2417 | delete I->second; |
| 2418 | mEmittedFunctions.clear(); |
| 2419 | |
| 2420 | mpMemMgr->reset(); |
| 2421 | |
| 2422 | return; |
| 2423 | } |
| 2424 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2425 | void *lookup(const char *Name) { |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2426 | return lookup( llvm::StringRef(Name) ); |
| 2427 | } |
| 2428 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2429 | void *lookup(const llvm::StringRef &Name) { |
| 2430 | EmittedFunctionsMapTy::const_iterator I = |
| 2431 | mEmittedFunctions.find(Name.str()); |
| 2432 | if (I == mEmittedFunctions.end()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2433 | return NULL; |
| 2434 | else |
| 2435 | return I->second->Code; |
| 2436 | } |
| 2437 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2438 | void getFunctionNames(BCCsizei *actualFunctionCount, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2439 | BCCsizei maxFunctionCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2440 | BCCchar **functions) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2441 | int functionCount = mEmittedFunctions.size(); |
| 2442 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2443 | if (actualFunctionCount) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2444 | *actualFunctionCount = functionCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2445 | if (functionCount > maxFunctionCount) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2446 | functionCount = maxFunctionCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2447 | if (functions) |
| 2448 | for (EmittedFunctionsMapTy::const_iterator |
| 2449 | I = mEmittedFunctions.begin(), E = mEmittedFunctions.end(); |
| 2450 | (I != E) && (functionCount > 0); |
| 2451 | I++, functionCount--) |
| 2452 | *functions++ = const_cast<BCCchar*>(I->first.c_str()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2453 | |
| 2454 | return; |
| 2455 | } |
| 2456 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2457 | void getFunctionBinary(BCCchar *label, |
| 2458 | BCCvoid **base, |
| 2459 | BCCsizei *length) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2460 | EmittedFunctionsMapTy::const_iterator I = mEmittedFunctions.find(label); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2461 | if (I == mEmittedFunctions.end()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2462 | *base = NULL; |
| 2463 | *length = 0; |
| 2464 | } else { |
| 2465 | *base = I->second->Code; |
| 2466 | *length = I->second->Size; |
| 2467 | } |
| 2468 | return; |
| 2469 | } |
| 2470 | |
| 2471 | ~CodeEmitter() { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2472 | delete mpMemMgr; |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2473 | #if defined(USE_DISASSEMBLER) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2474 | delete mpAsmInfo; |
| 2475 | delete mpDisassmbler; |
| 2476 | delete mpIP; |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2477 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2478 | return; |
| 2479 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2480 | }; |
| 2481 | // End of Class CodeEmitter |
| 2482 | ////////////////////////////////////////////////////////////////////////////// |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2483 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2484 | // The CodeEmitter |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2485 | llvm::OwningPtr<CodeEmitter> mCodeEmitter; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2486 | CodeEmitter *createCodeEmitter() { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2487 | mCodeEmitter.reset(new CodeEmitter(mCodeMemMgr.take())); |
| 2488 | return mCodeEmitter.get(); |
| 2489 | } |
| 2490 | |
| 2491 | BCCSymbolLookupFn mpSymbolLookupFn; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2492 | void *mpSymbolLookupContext; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2493 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2494 | llvm::LLVMContext *mContext; |
| 2495 | llvm::Module *mModule; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2496 | |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame] | 2497 | bool mHasLinked; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2498 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2499 | public: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2500 | Compiler() |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2501 | : mNeverCache(true), |
| 2502 | mCacheNew(false), |
| 2503 | mCacheFd(-1), |
| 2504 | mCacheMapAddr(NULL), |
| 2505 | mCacheHdr(NULL), |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2506 | mCacheSize(0), |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2507 | mCacheDiff(0), |
| 2508 | mCodeDataAddr(NULL), |
| 2509 | mpSymbolLookupFn(NULL), |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2510 | mpSymbolLookupContext(NULL), |
| 2511 | mContext(NULL), |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame] | 2512 | mModule(NULL), |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2513 | mHasLinked(false) /* Turn off linker */ { |
Shih-wei Liao | c561199 | 2010-05-09 06:37:55 -0700 | [diff] [blame] | 2514 | llvm::remove_fatal_error_handler(); |
| 2515 | llvm::install_fatal_error_handler(LLVMErrorHandler, &mError); |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2516 | mContext = new llvm::LLVMContext(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2517 | return; |
| 2518 | } |
| 2519 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2520 | // interface for BCCscript::registerSymbolCallback() |
| 2521 | void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2522 | mpSymbolLookupFn = pFn; |
| 2523 | mpSymbolLookupContext = pContext; |
| 2524 | return; |
| 2525 | } |
| 2526 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2527 | int readModule(llvm::Module *module) { |
Zonr Chang | dbee68b | 2010-10-22 05:02:16 +0800 | [diff] [blame] | 2528 | GlobalInitialization(); |
| 2529 | mModule = module; |
| 2530 | return hasError(); |
| 2531 | } |
| 2532 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2533 | int readBC(const char *bitcode, |
| 2534 | size_t bitcodeSize, |
| 2535 | const BCCchar *resName) { |
| 2536 | GlobalInitialization(); |
| 2537 | |
| 2538 | if (resName) { |
| 2539 | // Turn off the default NeverCaching mode |
| 2540 | mNeverCache = false; |
| 2541 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2542 | mCacheFd = openCacheFile(resName, true /* createIfMissing */); |
| 2543 | if (mCacheFd >= 0 && !mCacheNew) { // Just use cache file |
| 2544 | return -mCacheFd; |
| 2545 | } |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | llvm::OwningPtr<llvm::MemoryBuffer> MEM; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2549 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2550 | if (bitcode == NULL || bitcodeSize <= 0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2551 | return 0; |
Shih-wei Liao | c561199 | 2010-05-09 06:37:55 -0700 | [diff] [blame] | 2552 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2553 | // Package input to object MemoryBuffer |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2554 | MEM.reset(llvm::MemoryBuffer::getMemBuffer( |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2555 | llvm::StringRef(bitcode, bitcodeSize))); |
| 2556 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2557 | if (MEM.get() == NULL) { |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2558 | setError("Error reading input program bitcode into memory"); |
| 2559 | return hasError(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2560 | } |
| 2561 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2562 | // Read the input Bitcode as a Module |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2563 | mModule = llvm::ParseBitcodeFile(MEM.get(), *mContext, &mError); |
| 2564 | MEM.reset(); |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2565 | return hasError(); |
| 2566 | } |
Shih-wei Liao | c561199 | 2010-05-09 06:37:55 -0700 | [diff] [blame] | 2567 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2568 | int linkBC(const char *bitcode, size_t bitcodeSize) { |
| 2569 | llvm::OwningPtr<llvm::MemoryBuffer> MEM; |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2570 | |
| 2571 | if (bitcode == NULL || bitcodeSize <= 0) |
| 2572 | return 0; |
| 2573 | |
| 2574 | if (mModule == NULL) { |
| 2575 | setError("No module presents for linking"); |
| 2576 | return hasError(); |
| 2577 | } |
| 2578 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2579 | MEM.reset(llvm::MemoryBuffer::getMemBuffer( |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2580 | llvm::StringRef(bitcode, bitcodeSize))); |
| 2581 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2582 | if (MEM.get() == NULL) { |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2583 | setError("Error reading input library bitcode into memory"); |
| 2584 | return hasError(); |
| 2585 | } |
| 2586 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2587 | llvm::OwningPtr<llvm::Module> Lib(llvm::ParseBitcodeFile(MEM.get(), |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2588 | *mContext, |
| 2589 | &mError)); |
| 2590 | if (Lib.get() == NULL) |
| 2591 | return hasError(); |
| 2592 | |
| 2593 | if (llvm::Linker::LinkModules(mModule, Lib.take(), &mError)) |
| 2594 | return hasError(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2595 | |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame] | 2596 | // Everything for linking should be settled down here with no error occurs |
| 2597 | mHasLinked = true; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2598 | return hasError(); |
| 2599 | } |
| 2600 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2601 | |
| 2602 | // interface for bccLoadBinary() |
| 2603 | int loader() { |
| 2604 | // Check File Descriptor |
| 2605 | if (mCacheFd < 0) { |
| 2606 | LOGE("loading cache from invalid mCacheFd = %d\n", (int)mCacheFd); |
| 2607 | goto giveup; |
| 2608 | } |
| 2609 | |
| 2610 | |
| 2611 | // Check File Size |
| 2612 | struct stat statCacheFd; |
| 2613 | if (fstat(mCacheFd, &statCacheFd) < 0) { |
| 2614 | LOGE("unable to stat mCacheFd = %d\n", (int)mCacheFd); |
| 2615 | goto giveup; |
| 2616 | } |
| 2617 | |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2618 | mCacheSize = statCacheFd.st_size; |
| 2619 | |
| 2620 | if (mCacheSize < sizeof(oBCCHeader) || |
| 2621 | mCacheSize <= MaxCodeSize + MaxGlobalVarSize) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2622 | LOGE("mCacheFd %d is too small to be correct\n", (int)mCacheFd); |
| 2623 | goto giveup; |
| 2624 | } |
| 2625 | |
| 2626 | |
| 2627 | // Read File Content |
| 2628 | { |
| 2629 | #ifdef BCC_CODE_ADDR |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2630 | off_t heuristicCodeOffset = mCacheSize - MaxCodeSize - MaxGlobalVarSize; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2631 | |
| 2632 | mCodeDataAddr = (char *) mmap(reinterpret_cast<void*>(BCC_CODE_ADDR), |
| 2633 | MaxCodeSize + MaxGlobalVarSize, |
| 2634 | PROT_READ | PROT_EXEC | PROT_WRITE, |
| 2635 | MAP_PRIVATE | MAP_FIXED, |
| 2636 | mCacheFd, heuristicCodeOffset); |
| 2637 | |
| 2638 | if (mCodeDataAddr == MAP_FAILED) { |
| 2639 | LOGE("unable to mmap .oBBC cache code/data: %s\n", strerror(errno)); |
| 2640 | flock(mCacheFd, LOCK_UN); |
| 2641 | goto giveup; |
| 2642 | } |
| 2643 | |
| 2644 | mCacheMapAddr = (char *)malloc(heuristicCodeOffset); |
| 2645 | |
| 2646 | if (!mCacheMapAddr) { |
| 2647 | flock(mCacheFd, LOCK_UN); |
| 2648 | LOGE("allocation failed.\n"); |
| 2649 | // TODO(all) |
| 2650 | goto bail; |
| 2651 | } |
| 2652 | |
| 2653 | size_t nread = TEMP_FAILURE_RETRY1(read(mCacheFd, mCacheMapAddr, |
| 2654 | heuristicCodeOffset)); |
| 2655 | |
| 2656 | flock(mCacheFd, LOCK_UN); |
| 2657 | |
| 2658 | if (nread != (size_t)heuristicCodeOffset) { |
| 2659 | LOGE("read(mCacheFd) failed\n"); |
| 2660 | free(mCacheMapAddr); |
| 2661 | goto bail; |
| 2662 | } |
| 2663 | |
| 2664 | mCacheHdr = reinterpret_cast<oBCCHeader *>(mCacheMapAddr); |
| 2665 | |
| 2666 | if (mCacheHdr->codeOffset != (uint32_t)heuristicCodeOffset) { |
| 2667 | LOGE("assertion failed: heuristic code offset is not correct.\n"); |
| 2668 | goto bail; |
| 2669 | } |
| 2670 | #else |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2671 | mCacheMapAddr = (char *) mmap(0, mCacheSize, |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2672 | PROT_READ | PROT_EXEC | PROT_WRITE, |
| 2673 | MAP_PRIVATE, mCacheFd, 0); |
| 2674 | |
| 2675 | if (mCodeDataAddr == MAP_FAILED) { |
| 2676 | LOGE("unable to mmap .oBBC cache: %s\n", strerror(errno)); |
| 2677 | flock(mCacheFd, LOCK_UN); |
| 2678 | goto giveup; |
| 2679 | } |
| 2680 | |
| 2681 | flock(mCacheFd, LOCK_UN); |
| 2682 | |
| 2683 | mCacheHdr = reinterpret_cast<oBCCHeader *>(mCacheMapAddr); |
| 2684 | |
| 2685 | mCodeDataAddr = mCacheMapAddr + mCacheHdr->codeOffset; |
| 2686 | #endif |
| 2687 | } |
| 2688 | |
| 2689 | |
| 2690 | // Verify the Cache File |
| 2691 | if (memcmp(mCacheHdr->magic, OBCC_MAGIC, 4) != 0) { |
| 2692 | LOGE("bad magic word\n"); |
| 2693 | goto bail; |
| 2694 | } |
| 2695 | |
| 2696 | if (memcmp(mCacheHdr->magicVersion, OBCC_MAGIC_VERS, 4) != 0) { |
| 2697 | LOGE("bad oBCC version 0x%08x\n", |
| 2698 | *reinterpret_cast<uint32_t *>(mCacheHdr->magicVersion)); |
| 2699 | goto bail; |
| 2700 | } |
| 2701 | |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2702 | if (mCacheSize < mCacheHdr->relocOffset + |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 2703 | mCacheHdr->relocCount * sizeof(oBCCRelocEntry)) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2704 | LOGE("relocate table overflow\n"); |
| 2705 | goto bail; |
| 2706 | } |
| 2707 | |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2708 | if (mCacheSize < mCacheHdr->exportVarsOffset + |
| 2709 | mCacheHdr->exportVarsCount * sizeof(uint32_t)) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2710 | LOGE("export variables table overflow\n"); |
| 2711 | goto bail; |
| 2712 | } |
| 2713 | |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2714 | if (mCacheSize < mCacheHdr->exportFuncsOffset + |
| 2715 | mCacheHdr->exportFuncsCount * sizeof(uint32_t)) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2716 | LOGE("export functions table overflow\n"); |
| 2717 | goto bail; |
| 2718 | } |
| 2719 | |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2720 | if (mCacheSize < mCacheHdr->exportPragmasOffset + |
| 2721 | mCacheHdr->exportPragmasCount * sizeof(uint32_t)) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2722 | LOGE("export pragmas table overflow\n"); |
| 2723 | goto bail; |
| 2724 | } |
| 2725 | |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2726 | if (mCacheSize < mCacheHdr->codeOffset + mCacheHdr->codeSize) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2727 | LOGE("code cache overflow\n"); |
| 2728 | goto bail; |
| 2729 | } |
| 2730 | |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2731 | if (mCacheSize < mCacheHdr->dataOffset + mCacheHdr->dataSize) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2732 | LOGE("data (global variable) cache overflow\n"); |
| 2733 | goto bail; |
| 2734 | } |
| 2735 | |
| 2736 | |
| 2737 | // Relocate |
| 2738 | { |
| 2739 | mCacheDiff = mCodeDataAddr - |
| 2740 | reinterpret_cast<char *>(mCacheHdr->cachedCodeDataAddr); |
| 2741 | |
| 2742 | #ifdef BCC_CODE_ADDR |
| 2743 | if (mCacheDiff != 0) { |
| 2744 | LOGE("mCacheDiff should be zero but mCacheDiff = %d\n", mCacheDiff); |
| 2745 | goto bail; |
| 2746 | } |
| 2747 | #else |
Logan | 7f00bef | 2010-11-20 02:01:48 +0800 | [diff] [blame] | 2748 | if (mCacheHdr->rootAddr) { |
| 2749 | mCacheHdr->rootAddr += mCacheDiff; |
| 2750 | } |
| 2751 | |
| 2752 | if (mCacheHdr->initAddr) { |
| 2753 | mCacheHdr->initAddr += mCacheDiff; |
| 2754 | } |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2755 | |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 2756 | oBCCRelocEntry *cachedRelocTable = |
| 2757 | reinterpret_cast<oBCCRelocEntry *>(mCacheMapAddr + |
| 2758 | mCacheHdr->relocOffset); |
| 2759 | |
| 2760 | std::vector<llvm::MachineRelocation> relocations; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2761 | |
| 2762 | // Read in the relocs |
| 2763 | for (size_t i = 0; i < mCacheHdr->relocCount; i++) { |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 2764 | oBCCRelocEntry *entry = &cachedRelocTable[i]; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2765 | |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 2766 | llvm::MachineRelocation reloc = |
| 2767 | llvm::MachineRelocation::getGV((uintptr_t)entry->relocOffset, |
| 2768 | (unsigned)entry->relocType, 0, 0); |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2769 | |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 2770 | reloc.setResultPointer( |
| 2771 | reinterpret_cast<char *>(entry->cachedResultAddr) + mCacheDiff); |
| 2772 | |
| 2773 | relocations.push_back(reloc); |
| 2774 | } |
| 2775 | |
| 2776 | // Rewrite machine code using llvm::TargetJITInfo relocate |
| 2777 | { |
| 2778 | llvm::TargetMachine *TM = NULL; |
| 2779 | const llvm::Target *Target; |
| 2780 | std::string FeaturesStr; |
| 2781 | |
| 2782 | // Create TargetMachine |
| 2783 | Target = llvm::TargetRegistry::lookupTarget(Triple, mError); |
| 2784 | if (hasError()) |
| 2785 | goto bail; |
| 2786 | |
| 2787 | if (!CPU.empty() || !Features.empty()) { |
| 2788 | llvm::SubtargetFeatures F; |
| 2789 | F.setCPU(CPU); |
| 2790 | for (std::vector<std::string>::const_iterator I = Features.begin(), |
| 2791 | E = Features.end(); I != E; I++) |
| 2792 | F.AddFeature(*I); |
| 2793 | FeaturesStr = F.getString(); |
| 2794 | } |
| 2795 | |
| 2796 | TM = Target->createTargetMachine(Triple, FeaturesStr); |
| 2797 | if (TM == NULL) { |
| 2798 | setError("Failed to create target machine implementation for the" |
| 2799 | " specified triple '" + Triple + "'"); |
| 2800 | goto bail; |
| 2801 | } |
| 2802 | |
| 2803 | TM->getJITInfo()->relocate(mCodeDataAddr, |
| 2804 | &relocations[0], relocations.size(), |
| 2805 | (unsigned char *)mCodeDataAddr+MaxCodeSize); |
| 2806 | |
| 2807 | delete TM; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2808 | } |
| 2809 | #endif |
| 2810 | } |
| 2811 | |
| 2812 | return 0; |
| 2813 | |
| 2814 | bail: |
| 2815 | #ifdef BCC_CODE_ADDR |
| 2816 | if (munmap(mCodeDataAddr, MaxCodeSize + MaxGlobalVarSize) != 0) { |
| 2817 | LOGE("munmap failed: %s\n", strerror(errno)); |
| 2818 | } |
| 2819 | |
| 2820 | if (mCacheMapAddr) { |
| 2821 | free(mCacheMapAddr); |
| 2822 | } |
| 2823 | #else |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 2824 | if (munmap(mCacheMapAddr, mCacheSize) != 0) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2825 | LOGE("munmap failed: %s\n", strerror(errno)); |
| 2826 | } |
| 2827 | #endif |
| 2828 | |
| 2829 | giveup: |
| 2830 | return 1; |
| 2831 | } |
| 2832 | |
| 2833 | // interace for bccCompileBC() |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2834 | int compile() { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2835 | llvm::TargetData *TD = NULL; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2836 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2837 | llvm::TargetMachine *TM = NULL; |
| 2838 | const llvm::Target *Target; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2839 | std::string FeaturesStr; |
| 2840 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2841 | llvm::FunctionPassManager *CodeGenPasses = NULL; |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2842 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2843 | const llvm::NamedMDNode *PragmaMetadata; |
| 2844 | const llvm::NamedMDNode *ExportVarMetadata; |
| 2845 | const llvm::NamedMDNode *ExportFuncMetadata; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2846 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2847 | if (mModule == NULL) // No module was loaded |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2848 | return 0; |
| 2849 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2850 | // Create TargetMachine |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2851 | Target = llvm::TargetRegistry::lookupTarget(Triple, mError); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2852 | if (hasError()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2853 | goto on_bcc_compile_error; |
| 2854 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2855 | if (!CPU.empty() || !Features.empty()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2856 | llvm::SubtargetFeatures F; |
| 2857 | F.setCPU(CPU); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2858 | for (std::vector<std::string>::const_iterator I = Features.begin(), |
| 2859 | E = Features.end(); |
| 2860 | I != E; |
| 2861 | I++) |
| 2862 | F.AddFeature(*I); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2863 | FeaturesStr = F.getString(); |
| 2864 | } |
| 2865 | |
| 2866 | TM = Target->createTargetMachine(Triple, FeaturesStr); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2867 | if (TM == NULL) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2868 | setError("Failed to create target machine implementation for the" |
| 2869 | " specified triple '" + Triple + "'"); |
| 2870 | goto on_bcc_compile_error; |
| 2871 | } |
| 2872 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2873 | // Create memory manager for creation of code emitter later. |
| 2874 | if (!mCodeMemMgr.get() && !createCodeMemoryManager()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2875 | setError("Failed to startup memory management for further compilation"); |
| 2876 | goto on_bcc_compile_error; |
| 2877 | } |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2878 | mCodeDataAddr = (char *) (mCodeMemMgr.get()->getCodeMemBase()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2879 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2880 | // Create code emitter |
| 2881 | if (!mCodeEmitter.get()) { |
| 2882 | if (!createCodeEmitter()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2883 | setError("Failed to create machine code emitter to complete" |
| 2884 | " the compilation"); |
| 2885 | goto on_bcc_compile_error; |
| 2886 | } |
| 2887 | } else { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2888 | // Reuse the code emitter |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2889 | mCodeEmitter->reset(); |
| 2890 | } |
| 2891 | |
| 2892 | mCodeEmitter->setTargetMachine(*TM); |
| 2893 | mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn, |
| 2894 | mpSymbolLookupContext); |
| 2895 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2896 | // Get target data from Module |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2897 | TD = new llvm::TargetData(mModule); |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame] | 2898 | |
Zonr Chang | 5d35b97 | 2010-10-23 14:36:47 +0800 | [diff] [blame] | 2899 | // Load named metadata |
| 2900 | ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName); |
| 2901 | ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName); |
| 2902 | PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName); |
| 2903 | |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame] | 2904 | // Create LTO passes and run them on the mModule |
| 2905 | if (mHasLinked) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 2906 | llvm::TimePassesIsEnabled = true; // TODO(all) |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame] | 2907 | llvm::PassManager LTOPasses; |
Zonr Chang | 5d35b97 | 2010-10-23 14:36:47 +0800 | [diff] [blame] | 2908 | LTOPasses.add(new llvm::TargetData(*TD)); |
| 2909 | |
| 2910 | std::vector<const char*> ExportSymbols; |
| 2911 | |
| 2912 | // A workaround for getting export variable and function name. Will refine |
| 2913 | // it soon. |
| 2914 | if (ExportVarMetadata) { |
| 2915 | for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { |
| 2916 | llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); |
| 2917 | if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { |
| 2918 | llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); |
| 2919 | if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 2920 | llvm::StringRef ExportVarName = |
| 2921 | static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); |
| 2922 | ExportSymbols.push_back(ExportVarName.data()); |
| 2923 | } |
| 2924 | } |
| 2925 | } |
| 2926 | } |
| 2927 | |
| 2928 | if (ExportFuncMetadata) { |
| 2929 | for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { |
| 2930 | llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); |
| 2931 | if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { |
| 2932 | llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); |
| 2933 | if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { |
| 2934 | llvm::StringRef ExportFuncName = |
| 2935 | static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); |
| 2936 | ExportSymbols.push_back(ExportFuncName.data()); |
| 2937 | } |
| 2938 | } |
| 2939 | } |
| 2940 | } |
| 2941 | // root() and init() are born to be exported |
| 2942 | ExportSymbols.push_back("root"); |
| 2943 | ExportSymbols.push_back("init"); |
| 2944 | |
Zonr Chang | e5c7a54 | 2010-10-24 01:07:27 +0800 | [diff] [blame] | 2945 | // We now create passes list performing LTO. These are copied from |
| 2946 | // (including comments) llvm::createStandardLTOPasses(). |
| 2947 | |
| 2948 | // Internalize all other symbols not listed in ExportSymbols |
Zonr Chang | 5d35b97 | 2010-10-23 14:36:47 +0800 | [diff] [blame] | 2949 | LTOPasses.add(llvm::createInternalizePass(ExportSymbols)); |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame] | 2950 | |
Zonr Chang | e5c7a54 | 2010-10-24 01:07:27 +0800 | [diff] [blame] | 2951 | // Propagate constants at call sites into the functions they call. This |
| 2952 | // opens opportunities for globalopt (and inlining) by substituting |
| 2953 | // function pointers passed as arguments to direct uses of functions. |
| 2954 | LTOPasses.add(llvm::createIPSCCPPass()); |
| 2955 | |
| 2956 | // Now that we internalized some globals, see if we can hack on them! |
| 2957 | LTOPasses.add(llvm::createGlobalOptimizerPass()); |
| 2958 | |
| 2959 | // Linking modules together can lead to duplicated global constants, only |
| 2960 | // keep one copy of each constant... |
| 2961 | LTOPasses.add(llvm::createConstantMergePass()); |
| 2962 | |
| 2963 | // Remove unused arguments from functions... |
| 2964 | LTOPasses.add(llvm::createDeadArgEliminationPass()); |
| 2965 | |
| 2966 | // Reduce the code after globalopt and ipsccp. Both can open up |
| 2967 | // significant simplification opportunities, and both can propagate |
| 2968 | // functions through function pointers. When this happens, we often have |
| 2969 | // to resolve varargs calls, etc, so let instcombine do this. |
| 2970 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
| 2971 | |
| 2972 | // Inline small functions |
| 2973 | LTOPasses.add(llvm::createFunctionInliningPass()); |
| 2974 | |
| 2975 | // Remove dead EH info. |
| 2976 | LTOPasses.add(llvm::createPruneEHPass()); |
| 2977 | |
| 2978 | // Internalize the globals again after inlining |
| 2979 | LTOPasses.add(llvm::createGlobalOptimizerPass()); |
| 2980 | |
| 2981 | // Remove dead functions. |
| 2982 | LTOPasses.add(llvm::createGlobalDCEPass()); |
| 2983 | |
| 2984 | // If we didn't decide to inline a function, check to see if we can |
| 2985 | // transform it to pass arguments by value instead of by reference. |
| 2986 | LTOPasses.add(llvm::createArgumentPromotionPass()); |
| 2987 | |
| 2988 | // The IPO passes may leave cruft around. Clean up after them. |
| 2989 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
| 2990 | LTOPasses.add(llvm::createJumpThreadingPass()); |
| 2991 | |
| 2992 | // Break up allocas |
| 2993 | LTOPasses.add(llvm::createScalarReplAggregatesPass()); |
| 2994 | |
| 2995 | // Run a few AA driven optimizations here and now, to cleanup the code. |
| 2996 | LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture. |
| 2997 | LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis. |
| 2998 | |
| 2999 | // Hoist loop invariants. |
| 3000 | LTOPasses.add(llvm::createLICMPass()); |
| 3001 | |
| 3002 | // Remove redundancies. |
| 3003 | LTOPasses.add(llvm::createGVNPass()); |
| 3004 | |
| 3005 | // Remove dead memcpys. |
| 3006 | LTOPasses.add(llvm::createMemCpyOptPass()); |
| 3007 | |
| 3008 | // Nuke dead stores. |
| 3009 | LTOPasses.add(llvm::createDeadStoreEliminationPass()); |
| 3010 | |
| 3011 | // Cleanup and simplify the code after the scalar optimizations. |
| 3012 | LTOPasses.add(llvm::createInstructionCombiningPass()); |
| 3013 | |
| 3014 | LTOPasses.add(llvm::createJumpThreadingPass()); |
| 3015 | |
| 3016 | // Delete basic blocks, which optimization passes may have killed. |
| 3017 | LTOPasses.add(llvm::createCFGSimplificationPass()); |
| 3018 | |
| 3019 | // Now that we have optimized the program, discard unreachable functions. |
| 3020 | LTOPasses.add(llvm::createGlobalDCEPass()); |
| 3021 | |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame] | 3022 | LTOPasses.run(*mModule); |
| 3023 | } |
| 3024 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3025 | // Create code-gen pass to run the code emitter |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3026 | CodeGenPasses = new llvm::FunctionPassManager(mModule); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3027 | CodeGenPasses->add(TD); // Will take the ownership of TD |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3028 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3029 | if (TM->addPassesToEmitMachineCode(*CodeGenPasses, |
| 3030 | *mCodeEmitter, |
| 3031 | CodeGenOptLevel)) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3032 | setError("The machine code emission is not supported by BCC on target '" |
| 3033 | + Triple + "'"); |
| 3034 | goto on_bcc_compile_error; |
| 3035 | } |
| 3036 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3037 | // Run the pass (the code emitter) on every non-declaration function in the |
| 3038 | // module |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3039 | CodeGenPasses->doInitialization(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3040 | for (llvm::Module::iterator I = mModule->begin(), E = mModule->end(); |
| 3041 | I != E; |
| 3042 | I++) |
| 3043 | if (!I->isDeclaration()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3044 | CodeGenPasses->run(*I); |
Shih-wei Liao | 066d5ef | 2010-05-11 03:28:39 -0700 | [diff] [blame] | 3045 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3046 | CodeGenPasses->doFinalization(); |
| 3047 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3048 | // Copy the global address mapping from code emitter and remapping |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3049 | if (ExportVarMetadata) { |
| 3050 | for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { |
| 3051 | llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); |
| 3052 | if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { |
| 3053 | llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); |
| 3054 | if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3055 | llvm::StringRef ExportVarName = |
| 3056 | static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3057 | CodeEmitter::global_addresses_const_iterator I, E; |
| 3058 | for (I = mCodeEmitter->global_address_begin(), |
| 3059 | E = mCodeEmitter->global_address_end(); |
| 3060 | I != E; |
| 3061 | I++) { |
| 3062 | if (I->first->getValueID() != llvm::Value::GlobalVariableVal) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3063 | continue; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3064 | if (ExportVarName == I->first->getName()) { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3065 | mExportVars.push_back(I->second); |
| 3066 | break; |
| 3067 | } |
| 3068 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3069 | if (I != mCodeEmitter->global_address_end()) |
| 3070 | continue; // found |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3071 | } |
| 3072 | } |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3073 | // if reaching here, we know the global variable record in metadata is |
| 3074 | // not found. So we make an empty slot |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3075 | mExportVars.push_back(NULL); |
| 3076 | } |
| 3077 | assert((mExportVars.size() == ExportVarMetadata->getNumOperands()) && |
| 3078 | "Number of slots doesn't match the number of export variables!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3079 | } |
| 3080 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3081 | if (ExportFuncMetadata) { |
| 3082 | for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { |
| 3083 | llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); |
| 3084 | if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { |
| 3085 | llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); |
| 3086 | if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 3087 | llvm::StringRef ExportFuncName = |
| 3088 | static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); |
| 3089 | mExportFuncs.push_back(mCodeEmitter->lookup(ExportFuncName)); |
| 3090 | } |
| 3091 | } |
| 3092 | } |
| 3093 | } |
| 3094 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3095 | // Tell code emitter now can release the memory using during the JIT since |
| 3096 | // we have done the code emission |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3097 | mCodeEmitter->releaseUnnecessary(); |
| 3098 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3099 | // Finally, read pragma information from the metadata node of the @Module if |
| 3100 | // any. |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3101 | if (PragmaMetadata) |
| 3102 | for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) { |
| 3103 | llvm::MDNode *Pragma = PragmaMetadata->getOperand(i); |
| 3104 | if (Pragma != NULL && |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3105 | Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3106 | llvm::Value *PragmaNameMDS = Pragma->getOperand(0); |
| 3107 | llvm::Value *PragmaValueMDS = Pragma->getOperand(1); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3108 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3109 | if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) && |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3110 | (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) { |
| 3111 | llvm::StringRef PragmaName = |
| 3112 | static_cast<llvm::MDString*>(PragmaNameMDS)->getString(); |
| 3113 | llvm::StringRef PragmaValue = |
| 3114 | static_cast<llvm::MDString*>(PragmaValueMDS)->getString(); |
| 3115 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3116 | mPragmas.push_back( |
| 3117 | std::make_pair(std::string(PragmaName.data(), |
| 3118 | PragmaName.size()), |
| 3119 | std::string(PragmaValue.data(), |
| 3120 | PragmaValue.size()))); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | on_bcc_compile_error: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3126 | // LOGE("on_bcc_compiler_error"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3127 | if (CodeGenPasses) { |
| 3128 | delete CodeGenPasses; |
| 3129 | } else if (TD) { |
| 3130 | delete TD; |
| 3131 | } |
| 3132 | if (TM) |
| 3133 | delete TM; |
| 3134 | |
Shih-wei Liao | 066d5ef | 2010-05-11 03:28:39 -0700 | [diff] [blame] | 3135 | if (mError.empty()) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3136 | if (!mNeverCache && mCacheFd >= 0 && mCacheNew) { |
| 3137 | genCacheFile(); |
| 3138 | flock(mCacheFd, LOCK_UN); |
| 3139 | } |
| 3140 | |
Shih-wei Liao | 066d5ef | 2010-05-11 03:28:39 -0700 | [diff] [blame] | 3141 | return false; |
| 3142 | } |
| 3143 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3144 | // LOGE(getErrorMessage()); |
Shih-wei Liao | 066d5ef | 2010-05-11 03:28:39 -0700 | [diff] [blame] | 3145 | return true; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3146 | } |
| 3147 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3148 | // interface for bccGetScriptInfoLog() |
| 3149 | char *getErrorMessage() { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3150 | return const_cast<char*>(mError.c_str()); |
| 3151 | } |
| 3152 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3153 | // interface for bccGetScriptLabel() |
| 3154 | void *lookup(const char *name) { |
| 3155 | void *addr = NULL; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3156 | if (!mNeverCache && mCacheFd >= 0 && !mCacheNew) { |
| 3157 | if (!strcmp(name, "root")) { |
| 3158 | addr = reinterpret_cast<void *>(mCacheHdr->rootAddr); |
| 3159 | } else if (!strcmp(name, "init")) { |
| 3160 | addr = reinterpret_cast<void *>(mCacheHdr->initAddr); |
| 3161 | } |
| 3162 | return addr; |
| 3163 | } |
| 3164 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3165 | if (mCodeEmitter.get()) |
| 3166 | // Find function pointer |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3167 | addr = mCodeEmitter->lookup(name); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3168 | return addr; |
| 3169 | } |
| 3170 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3171 | // Interface for bccGetExportVars() |
| 3172 | void getExportVars(BCCsizei *actualVarCount, |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3173 | BCCsizei maxVarCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3174 | BCCvoid **vars) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3175 | int varCount; |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3176 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3177 | if (!mNeverCache && mCacheFd >= 0 && !mCacheNew) { |
| 3178 | varCount = static_cast<int>(mCacheHdr->exportVarsCount); |
| 3179 | if (actualVarCount) |
| 3180 | *actualVarCount = varCount; |
| 3181 | if (varCount > maxVarCount) |
| 3182 | varCount = maxVarCount; |
| 3183 | if (vars) { |
| 3184 | uint32_t *cachedVars = (uint32_t *)(mCacheMapAddr + |
| 3185 | mCacheHdr->exportVarsOffset); |
| 3186 | |
| 3187 | for (int i = 0; i < varCount; i++) { |
| 3188 | *vars++ = (BCCvoid *)(reinterpret_cast<char *>(*cachedVars) + |
| 3189 | mCacheDiff); |
| 3190 | cachedVars++; |
| 3191 | } |
| 3192 | } |
| 3193 | return; |
| 3194 | } |
| 3195 | |
| 3196 | varCount = mExportVars.size(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3197 | if (actualVarCount) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3198 | *actualVarCount = varCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3199 | if (varCount > maxVarCount) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3200 | varCount = maxVarCount; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3201 | if (vars) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3202 | for (ExportVarList::const_iterator I = mExportVars.begin(), |
| 3203 | E = mExportVars.end(); |
| 3204 | I != E; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3205 | I++) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3206 | *vars++ = *I; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3207 | } |
| 3208 | } |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3209 | |
| 3210 | return; |
| 3211 | } |
| 3212 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3213 | // Interface for bccGetExportFuncs() |
| 3214 | void getExportFuncs(BCCsizei *actualFuncCount, |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 3215 | BCCsizei maxFuncCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3216 | BCCvoid **funcs) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3217 | int funcCount; |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 3218 | |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3219 | if (!mNeverCache && mCacheFd >= 0 && !mCacheNew) { |
| 3220 | funcCount = static_cast<int>(mCacheHdr->exportFuncsCount); |
| 3221 | if (actualFuncCount) |
| 3222 | *actualFuncCount = funcCount; |
| 3223 | if (funcCount > maxFuncCount) |
| 3224 | funcCount = maxFuncCount; |
| 3225 | if (funcs) { |
| 3226 | uint32_t *cachedFuncs = (uint32_t *)(mCacheMapAddr + |
| 3227 | mCacheHdr->exportFuncsOffset); |
| 3228 | |
| 3229 | for (int i = 0; i < funcCount; i++) { |
| 3230 | *funcs++ = (BCCvoid *)(reinterpret_cast<char *>(*cachedFuncs) + |
| 3231 | mCacheDiff); |
| 3232 | cachedFuncs++; |
| 3233 | } |
| 3234 | } |
| 3235 | return; |
| 3236 | } |
| 3237 | |
| 3238 | funcCount = mExportFuncs.size(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3239 | if (actualFuncCount) |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 3240 | *actualFuncCount = funcCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3241 | if (funcCount > maxFuncCount) |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 3242 | funcCount = maxFuncCount; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3243 | if (funcs) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3244 | for (ExportFuncList::const_iterator I = mExportFuncs.begin(), |
| 3245 | E = mExportFuncs.end(); |
| 3246 | I != E; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3247 | I++) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3248 | *funcs++ = *I; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3249 | } |
| 3250 | } |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 3251 | |
| 3252 | return; |
| 3253 | } |
| 3254 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3255 | // Interface for bccGetPragmas() |
| 3256 | void getPragmas(BCCsizei *actualStringCount, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3257 | BCCsizei maxStringCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3258 | BCCchar **strings) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3259 | int stringCount; |
| 3260 | if (!mNeverCache && mCacheFd >= 0 && !mCacheNew) { |
| 3261 | if (actualStringCount) |
| 3262 | *actualStringCount = 0; // XXX |
| 3263 | return; |
| 3264 | } |
| 3265 | |
| 3266 | stringCount = mPragmas.size() * 2; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3267 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3268 | if (actualStringCount) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3269 | *actualStringCount = stringCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3270 | if (stringCount > maxStringCount) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3271 | stringCount = maxStringCount; |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3272 | if (strings) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3273 | for (PragmaList::const_iterator it = mPragmas.begin(); |
| 3274 | stringCount > 0; |
| 3275 | stringCount -= 2, it++) { |
| 3276 | *strings++ = const_cast<BCCchar*>(it->first.c_str()); |
| 3277 | *strings++ = const_cast<BCCchar*>(it->second.c_str()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3278 | } |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3279 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3280 | |
| 3281 | return; |
| 3282 | } |
| 3283 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3284 | // Interface for bccGetFunctions() |
| 3285 | void getFunctions(BCCsizei *actualFunctionCount, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3286 | BCCsizei maxFunctionCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3287 | BCCchar **functions) { |
| 3288 | if (mCodeEmitter.get()) |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 3289 | mCodeEmitter->getFunctionNames(actualFunctionCount, |
| 3290 | maxFunctionCount, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3291 | functions); |
| 3292 | else |
| 3293 | *actualFunctionCount = 0; |
| 3294 | |
| 3295 | return; |
| 3296 | } |
| 3297 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3298 | // Interface for bccGetFunctionBinary() |
| 3299 | void getFunctionBinary(BCCchar *function, |
| 3300 | BCCvoid **base, |
| 3301 | BCCsizei *length) { |
| 3302 | if (mCodeEmitter.get()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3303 | mCodeEmitter->getFunctionBinary(function, base, length); |
| 3304 | } else { |
| 3305 | *base = NULL; |
| 3306 | *length = 0; |
| 3307 | } |
| 3308 | return; |
| 3309 | } |
| 3310 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3311 | inline const llvm::Module *getModule() const { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3312 | return mModule; |
| 3313 | } |
| 3314 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3315 | ~Compiler() { |
Shih-wei Liao | 7f941bb | 2010-11-19 01:40:16 -0800 | [diff] [blame] | 3316 | #ifdef BCC_CODE_ADDR |
| 3317 | if (mCodeDataAddr != 0 && mCodeDataAddr != MAP_FAILED) { |
| 3318 | if (munmap(mCodeDataAddr, MaxCodeSize + MaxGlobalVarSize) < 0) { |
| 3319 | LOGE("munmap failed while releasing mCodeDataAddr\n"); |
| 3320 | } |
| 3321 | } |
| 3322 | |
| 3323 | if (mCacheMapAddr) { |
| 3324 | free(mCacheMapAddr); |
| 3325 | } |
| 3326 | #else |
| 3327 | if (mCacheMapAddr != 0 && mCacheMapAddr != MAP_FAILED) { |
| 3328 | if (munmap(mCacheMapAddr, mCacheSize) < 0) { |
| 3329 | LOGE("munmap failed while releasing mCacheMapAddr\n"); |
| 3330 | } |
| 3331 | } |
| 3332 | #endif |
| 3333 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3334 | delete mModule; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3335 | // llvm::llvm_shutdown(); |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 3336 | delete mContext; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3337 | return; |
| 3338 | } |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3339 | |
| 3340 | private: |
| 3341 | // Note: loader() and genCacheFile() go hand in hand |
| 3342 | void genCacheFile() { |
| 3343 | if (lseek(mCacheFd, 0, SEEK_SET) != 0) { |
| 3344 | LOGE("Unable to seek to 0: %s\n", strerror(errno)); |
| 3345 | return; |
| 3346 | } |
| 3347 | |
| 3348 | bool codeOffsetNeedPadding = false; |
| 3349 | |
| 3350 | uint32_t offset = sizeof(oBCCHeader); |
| 3351 | |
| 3352 | // BCC Cache File Header |
| 3353 | oBCCHeader *hdr = (oBCCHeader *)malloc(sizeof(oBCCHeader)); |
| 3354 | |
| 3355 | if (!hdr) { |
| 3356 | LOGE("Unable to allocate oBCCHeader.\n"); |
| 3357 | return; |
| 3358 | } |
| 3359 | |
| 3360 | // Magic Words |
| 3361 | memcpy(hdr->magic, OBCC_MAGIC, 4); |
| 3362 | memcpy(hdr->magicVersion, OBCC_MAGIC_VERS, 4); |
| 3363 | |
| 3364 | // Timestamp |
| 3365 | hdr->sourceWhen = 0; // TODO(all) |
| 3366 | hdr->rslibWhen = 0; // TODO(all) |
| 3367 | hdr->libRSWhen = 0; // TODO(all) |
| 3368 | hdr->libbccWhen = 0; // TODO(all) |
| 3369 | |
| 3370 | // Current Memory Address (Saved for Recalculation) |
| 3371 | hdr->cachedCodeDataAddr = reinterpret_cast<uint32_t>(mCodeDataAddr); |
| 3372 | hdr->rootAddr = reinterpret_cast<uint32_t>(lookup("root")); |
| 3373 | hdr->initAddr = reinterpret_cast<uint32_t>(lookup("init")); |
| 3374 | |
| 3375 | // Relocation Table Offset and Entry Count |
| 3376 | hdr->relocOffset = sizeof(oBCCHeader); |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 3377 | hdr->relocCount = mCodeEmitter->getCachingRelocations().size(); |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3378 | |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 3379 | offset += hdr->relocCount * (sizeof(oBCCRelocEntry)); |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3380 | |
| 3381 | // Export Variable Table Offset and Entry Count |
| 3382 | hdr->exportVarsOffset = offset; |
| 3383 | hdr->exportVarsCount = mExportVars.size(); |
| 3384 | |
| 3385 | offset += hdr->exportVarsCount * sizeof(uint32_t); |
| 3386 | |
| 3387 | // Export Function Table Offset and Entry Count |
| 3388 | hdr->exportFuncsOffset = offset; |
| 3389 | hdr->exportFuncsCount = mExportFuncs.size(); |
| 3390 | |
| 3391 | offset += hdr->exportFuncsCount * sizeof(uint32_t); |
| 3392 | |
| 3393 | // Export Pragmas Table Offset and Entry Count |
| 3394 | hdr->exportPragmasOffset = offset; |
| 3395 | hdr->exportPragmasCount = 0; // TODO(all): mPragmas.size(); |
| 3396 | |
| 3397 | offset += hdr->exportPragmasCount * sizeof(uint32_t); |
| 3398 | |
| 3399 | // Code Offset and Size |
| 3400 | |
| 3401 | #ifdef BCC_CODE_ADDR |
| 3402 | { |
| 3403 | long pagesize = sysconf(_SC_PAGESIZE); |
| 3404 | |
| 3405 | if (offset % pagesize > 0) { |
| 3406 | codeOffsetNeedPadding = true; |
| 3407 | offset += pagesize - (offset % pagesize); |
| 3408 | } |
| 3409 | } |
| 3410 | #else |
| 3411 | if (offset & 0x07) { // Ensure that offset aligned to 64-bit (8 byte). |
| 3412 | codeOffsetNeedPadding = true; |
| 3413 | offset += 0x08 - (offset & 0x07); |
| 3414 | } |
| 3415 | #endif |
| 3416 | |
| 3417 | hdr->codeOffset = offset; |
| 3418 | hdr->codeSize = MaxCodeSize; |
| 3419 | |
| 3420 | offset += hdr->codeSize; |
| 3421 | |
| 3422 | // Data (Global Variable) Offset and Size |
| 3423 | hdr->dataOffset = offset; |
| 3424 | hdr->dataSize = MaxGlobalVarSize; |
| 3425 | |
| 3426 | offset += hdr->dataSize; |
| 3427 | |
| 3428 | // Checksum |
| 3429 | hdr->checksum = 0; // Set Field checksum. TODO(all) |
| 3430 | |
| 3431 | // Write Header |
| 3432 | sysWriteFully(mCacheFd, reinterpret_cast<char const *>(hdr), |
| 3433 | sizeof(oBCCHeader), "Write oBCC header"); |
| 3434 | |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 3435 | // Write Relocation Entry Table |
| 3436 | { |
| 3437 | size_t allocSize = hdr->relocCount * sizeof(oBCCRelocEntry); |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3438 | |
Logan | 824dd0a | 2010-11-20 01:45:54 +0800 | [diff] [blame] | 3439 | oBCCRelocEntry const*records = &mCodeEmitter->getCachingRelocations()[0]; |
| 3440 | |
| 3441 | sysWriteFully(mCacheFd, reinterpret_cast<char const *>(records), |
| 3442 | allocSize, "Write Relocation Entries"); |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3443 | } |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3444 | |
| 3445 | // Write Export Variables Table |
| 3446 | { |
| 3447 | uint32_t *record, *ptr; |
| 3448 | |
| 3449 | record = (uint32_t *)calloc(hdr->exportVarsCount, sizeof(uint32_t)); |
| 3450 | ptr = record; |
| 3451 | |
| 3452 | if (!record) { |
| 3453 | goto bail; |
| 3454 | } |
| 3455 | |
| 3456 | for (ExportVarList::const_iterator I = mExportVars.begin(), |
| 3457 | E = mExportVars.end(); I != E; I++) { |
| 3458 | *ptr++ = reinterpret_cast<uint32_t>(*I); |
| 3459 | } |
| 3460 | |
| 3461 | sysWriteFully(mCacheFd, reinterpret_cast<char const *>(record), |
| 3462 | hdr->exportVarsCount * sizeof(uint32_t), |
| 3463 | "Write ExportVars"); |
| 3464 | |
| 3465 | free(record); |
| 3466 | } |
| 3467 | |
| 3468 | // Write Export Functions Table |
| 3469 | { |
| 3470 | uint32_t *record, *ptr; |
| 3471 | |
| 3472 | record = (uint32_t *)calloc(hdr->exportFuncsCount, sizeof(uint32_t)); |
| 3473 | ptr = record; |
| 3474 | |
| 3475 | if (!record) { |
| 3476 | goto bail; |
| 3477 | } |
| 3478 | |
| 3479 | for (ExportFuncList::const_iterator I = mExportFuncs.begin(), |
| 3480 | E = mExportFuncs.end(); I != E; I++) { |
| 3481 | *ptr++ = reinterpret_cast<uint32_t>(*I); |
| 3482 | } |
| 3483 | |
| 3484 | sysWriteFully(mCacheFd, reinterpret_cast<char const *>(record), |
| 3485 | hdr->exportFuncsCount * sizeof(uint32_t), |
| 3486 | "Write ExportFuncs"); |
| 3487 | |
| 3488 | free(record); |
| 3489 | } |
| 3490 | |
| 3491 | |
| 3492 | // TODO(all): Write Export Pragmas Table |
| 3493 | #if 0 |
| 3494 | #else |
| 3495 | // Note: As long as we have comment out export pragmas table code, |
| 3496 | // we have to seek the position to correct offset. |
| 3497 | |
| 3498 | lseek(mCacheFd, hdr->codeOffset, SEEK_SET); |
| 3499 | #endif |
| 3500 | |
| 3501 | if (codeOffsetNeedPadding) { |
| 3502 | // requires additional padding |
| 3503 | lseek(mCacheFd, hdr->codeOffset, SEEK_SET); |
| 3504 | } |
| 3505 | |
| 3506 | // Write Generated Code and Global Variable |
| 3507 | sysWriteFully(mCacheFd, mCodeDataAddr, MaxCodeSize + MaxGlobalVarSize, |
| 3508 | "Write code and global variable"); |
| 3509 | |
| 3510 | goto close_return; |
| 3511 | |
| 3512 | bail: |
| 3513 | if (ftruncate(mCacheFd, 0) != 0) { |
| 3514 | LOGW("Warning: unable to truncate cache file: %s\n", strerror(errno)); |
| 3515 | } |
| 3516 | |
| 3517 | close_return: |
| 3518 | free(hdr); |
| 3519 | close(mCacheFd); |
| 3520 | mCacheFd = -1; |
| 3521 | return; |
| 3522 | } |
| 3523 | |
| 3524 | // OpenCacheFile() returns fd of the cache file. |
| 3525 | // Input: |
| 3526 | // BCCchar *resName: Used to genCacheFileName() |
| 3527 | // bool createIfMissing: If false, turn off caching |
| 3528 | // Output: |
| 3529 | // returns fd: If -1: Failed |
| 3530 | // mCacheNew: If true, the returned fd is new. Otherwise, the fd is the |
| 3531 | // cache file's file descriptor |
| 3532 | // Note: openCacheFile() will check the cache file's validity, |
| 3533 | // such as Magic number, sourceWhen... dependencies. |
| 3534 | int openCacheFile(const BCCchar *resName, bool createIfMissing) { |
| 3535 | int fd, cc; |
| 3536 | struct stat fdStat, fileStat; |
| 3537 | bool readOnly = false; |
| 3538 | |
| 3539 | char *cacheFileName = genCacheFileName(resName, ".oBCC"); |
| 3540 | |
| 3541 | mCacheNew = false; |
| 3542 | |
| 3543 | retry: |
| 3544 | /* |
| 3545 | * Try to open the cache file. If we've been asked to, |
| 3546 | * create it if it doesn't exist. |
| 3547 | */ |
| 3548 | fd = createIfMissing ? open(cacheFileName, O_CREAT|O_RDWR, 0644) : -1; |
| 3549 | if (fd < 0) { |
| 3550 | fd = open(cacheFileName, O_RDONLY, 0); |
| 3551 | if (fd < 0) { |
| 3552 | if (createIfMissing) { |
| 3553 | LOGE("Can't open bcc-cache '%s': %s\n", |
| 3554 | cacheFileName, strerror(errno)); |
| 3555 | mCacheNew = true; |
| 3556 | } |
| 3557 | return fd; |
| 3558 | } |
| 3559 | readOnly = true; |
| 3560 | } |
| 3561 | |
| 3562 | /* |
| 3563 | * Grab an exclusive lock on the cache file. If somebody else is |
| 3564 | * working on it, we'll block here until they complete. |
| 3565 | */ |
| 3566 | LOGV("bcc: locking cache file %s (fd=%d, boot=%d)\n", |
| 3567 | cacheFileName, fd); |
| 3568 | |
| 3569 | cc = flock(fd, LOCK_EX | LOCK_NB); |
| 3570 | if (cc != 0) { |
| 3571 | LOGD("bcc: sleeping on flock(%s)\n", cacheFileName); |
| 3572 | cc = flock(fd, LOCK_EX); |
| 3573 | } |
| 3574 | |
| 3575 | if (cc != 0) { |
| 3576 | LOGE("Can't lock bcc cache '%s': %d\n", cacheFileName, cc); |
| 3577 | close(fd); |
| 3578 | return -1; |
| 3579 | } |
| 3580 | LOGV("bcc: locked cache file\n"); |
| 3581 | |
| 3582 | /* |
| 3583 | * Check to see if the fd we opened and locked matches the file in |
| 3584 | * the filesystem. If they don't, then somebody else unlinked ours |
| 3585 | * and created a new file, and we need to use that one instead. (If |
| 3586 | * we caught them between the unlink and the create, we'll get an |
| 3587 | * ENOENT from the file stat.) |
| 3588 | */ |
| 3589 | cc = fstat(fd, &fdStat); |
| 3590 | if (cc != 0) { |
| 3591 | LOGE("Can't stat open file '%s'\n", cacheFileName); |
| 3592 | LOGV("bcc: unlocking cache file %s\n", cacheFileName); |
| 3593 | goto close_fail; |
| 3594 | } |
| 3595 | cc = stat(cacheFileName, &fileStat); |
| 3596 | if (cc != 0 || |
| 3597 | fdStat.st_dev != fileStat.st_dev || fdStat.st_ino != fileStat.st_ino) { |
| 3598 | LOGD("bcc: our open cache file is stale; sleeping and retrying\n"); |
| 3599 | LOGV("bcc: unlocking cache file %s\n", cacheFileName); |
| 3600 | flock(fd, LOCK_UN); |
| 3601 | close(fd); |
| 3602 | usleep(250 * 1000); // if something is hosed, don't peg machine |
| 3603 | goto retry; |
| 3604 | } |
| 3605 | |
| 3606 | /* |
| 3607 | * We have the correct file open and locked. If the file size is zero, |
| 3608 | * then it was just created by us, and we want to fill in some fields |
| 3609 | * in the "bcc" header and set "mCacheNew". Otherwise, we want to |
| 3610 | * verify that the fields in the header match our expectations, and |
| 3611 | * reset the file if they don't. |
| 3612 | */ |
| 3613 | if (fdStat.st_size == 0) { |
| 3614 | if (readOnly) { // The device is readOnly --> close_fail |
| 3615 | LOGW("bcc: file has zero length and isn't writable\n"); |
| 3616 | goto close_fail; |
| 3617 | } |
| 3618 | /*cc = createEmptyHeader(fd); |
| 3619 | if (cc != 0) |
| 3620 | goto close_fail; |
| 3621 | */ |
| 3622 | mCacheNew = true; |
| 3623 | LOGV("bcc: successfully initialized new cache file\n"); |
| 3624 | } else { |
| 3625 | // Calculate sourceWhen |
| 3626 | // XXX |
| 3627 | uint32_t sourceWhen = 0; |
| 3628 | uint32_t rslibWhen = 0; |
| 3629 | uint32_t libRSWhen = 0; |
| 3630 | uint32_t libbccWhen = 0; |
| 3631 | if (!checkHeaderAndDependencies(fd, |
| 3632 | sourceWhen, |
| 3633 | rslibWhen, |
| 3634 | libRSWhen, |
| 3635 | libbccWhen)) { |
| 3636 | // If checkHeaderAndDependencies returns 0: FAILED |
| 3637 | // Will truncate the file and retry to createIfMissing the file |
| 3638 | |
| 3639 | if (readOnly) { // Shouldn't be readonly. |
| 3640 | /* |
| 3641 | * We could unlink and rewrite the file if we own it or |
| 3642 | * the "sticky" bit isn't set on the directory. However, |
| 3643 | * we're not able to truncate it, which spoils things. So, |
| 3644 | * give up now. |
| 3645 | */ |
| 3646 | if (createIfMissing) { |
| 3647 | LOGW("Cached file %s is stale and not writable\n", |
| 3648 | cacheFileName); |
| 3649 | } |
| 3650 | goto close_fail; |
| 3651 | } |
| 3652 | |
| 3653 | /* |
| 3654 | * If we truncate the existing file before unlinking it, any |
| 3655 | * process that has it mapped will fail when it tries to touch |
| 3656 | * the pages? Probably OK because we use MAP_PRIVATE. |
| 3657 | */ |
| 3658 | LOGD("oBCC file is stale or bad; removing and retrying (%s)\n", |
| 3659 | cacheFileName); |
| 3660 | if (ftruncate(fd, 0) != 0) { |
| 3661 | LOGW("Warning: unable to truncate cache file '%s': %s\n", |
| 3662 | cacheFileName, strerror(errno)); |
| 3663 | /* keep going */ |
| 3664 | } |
| 3665 | if (unlink(cacheFileName) != 0) { |
| 3666 | LOGW("Warning: unable to remove cache file '%s': %d %s\n", |
| 3667 | cacheFileName, errno, strerror(errno)); |
| 3668 | /* keep going; permission failure should probably be fatal */ |
| 3669 | } |
| 3670 | LOGV("bcc: unlocking cache file %s\n", cacheFileName); |
| 3671 | flock(fd, LOCK_UN); |
| 3672 | close(fd); |
| 3673 | goto retry; |
| 3674 | } else { |
| 3675 | // Got cacheFile! Good to go. |
| 3676 | LOGV("Good cache file\n"); |
| 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | assert(fd >= 0); |
| 3681 | return fd; |
| 3682 | |
| 3683 | close_fail: |
| 3684 | flock(fd, LOCK_UN); |
| 3685 | close(fd); |
| 3686 | return -1; |
| 3687 | } // End of openCacheFile() |
| 3688 | |
| 3689 | char *genCacheFileName(const char *fileName, const char *subFileName) { |
| 3690 | char nameBuf[512]; |
| 3691 | static const char kCachePath[] = "bcc-cache"; |
| 3692 | char absoluteFile[sizeof(nameBuf)]; |
| 3693 | const size_t kBufLen = sizeof(nameBuf) - 1; |
| 3694 | const char *dataRoot; |
| 3695 | char *cp; |
| 3696 | |
| 3697 | // Get the absolute path of the raw/***.bc file. |
| 3698 | absoluteFile[0] = '\0'; |
| 3699 | if (fileName[0] != '/') { |
| 3700 | /* |
| 3701 | * Generate the absolute path. This doesn't do everything it |
| 3702 | * should, e.g. if filename is "./out/whatever" it doesn't crunch |
| 3703 | * the leading "./" out, but it'll do. |
| 3704 | */ |
| 3705 | if (getcwd(absoluteFile, kBufLen) == NULL) { |
| 3706 | LOGE("Can't get CWD while opening raw/***.bc file\n"); |
| 3707 | return NULL; |
| 3708 | } |
| 3709 | // TODO(srhines): strncat() is a bit dangerous |
| 3710 | strncat(absoluteFile, "/", kBufLen); |
| 3711 | } |
| 3712 | strncat(absoluteFile, fileName, kBufLen); |
| 3713 | |
| 3714 | if (subFileName != NULL) { |
| 3715 | strncat(absoluteFile, "/", kBufLen); |
| 3716 | strncat(absoluteFile, subFileName, kBufLen); |
| 3717 | } |
| 3718 | |
| 3719 | /* Turn the path into a flat filename by replacing |
| 3720 | * any slashes after the first one with '@' characters. |
| 3721 | */ |
| 3722 | cp = absoluteFile + 1; |
| 3723 | while (*cp != '\0') { |
| 3724 | if (*cp == '/') { |
| 3725 | *cp = '@'; |
| 3726 | } |
| 3727 | cp++; |
| 3728 | } |
| 3729 | |
| 3730 | /* Build the name of the cache directory. |
| 3731 | */ |
| 3732 | dataRoot = getenv("ANDROID_DATA"); |
| 3733 | if (dataRoot == NULL) |
| 3734 | dataRoot = "/data"; |
| 3735 | snprintf(nameBuf, kBufLen, "%s/%s", dataRoot, kCachePath); |
| 3736 | |
| 3737 | /* Tack on the file name for the actual cache file path. |
| 3738 | */ |
| 3739 | strncat(nameBuf, absoluteFile, kBufLen); |
| 3740 | |
| 3741 | LOGV("Cache file for '%s' '%s' is '%s'\n", fileName, subFileName, nameBuf); |
| 3742 | return strdup(nameBuf); |
| 3743 | } |
| 3744 | |
| 3745 | /* |
| 3746 | * Read the oBCC header, verify it, then read the dependent section |
| 3747 | * and verify that data as well. |
| 3748 | * |
| 3749 | * On successful return, the file will be seeked immediately past the |
| 3750 | * oBCC header. |
| 3751 | */ |
| 3752 | bool checkHeaderAndDependencies(int fd, |
| 3753 | uint32_t sourceWhen, |
| 3754 | uint32_t rslibWhen, |
| 3755 | uint32_t libRSWhen, |
| 3756 | uint32_t libbccWhen) { |
| 3757 | ssize_t actual; |
| 3758 | oBCCHeader optHdr; |
| 3759 | uint32_t val; |
| 3760 | uint8_t const *magic, *magicVer; |
| 3761 | |
| 3762 | /* |
| 3763 | * Start at the start. The "bcc" header, when present, will always be |
| 3764 | * the first thing in the file. |
| 3765 | */ |
| 3766 | if (lseek(fd, 0, SEEK_SET) != 0) { |
| 3767 | LOGE("bcc: failed to seek to start of file: %s\n", strerror(errno)); |
| 3768 | goto bail; |
| 3769 | } |
| 3770 | |
| 3771 | /* |
| 3772 | * Read and do trivial verification on the bcc header. The header is |
| 3773 | * always in host byte order. |
| 3774 | */ |
| 3775 | actual = read(fd, &optHdr, sizeof(optHdr)); |
| 3776 | if (actual < 0) { |
| 3777 | LOGE("bcc: failed reading bcc header: %s\n", strerror(errno)); |
| 3778 | goto bail; |
| 3779 | } else if (actual != sizeof(optHdr)) { |
| 3780 | LOGE("bcc: failed reading bcc header (got %d of %zd)\n", |
| 3781 | (int) actual, sizeof(optHdr)); |
| 3782 | goto bail; |
| 3783 | } |
| 3784 | |
| 3785 | magic = optHdr.magic; |
| 3786 | if (memcmp(magic, OBCC_MAGIC, 4) != 0) { |
| 3787 | /* not an oBCC file, or previous attempt was interrupted */ |
| 3788 | LOGD("bcc: incorrect opt magic number (0x%02x %02x %02x %02x)\n", |
| 3789 | magic[0], magic[1], magic[2], magic[3]); |
| 3790 | goto bail; |
| 3791 | } |
| 3792 | |
| 3793 | magicVer = optHdr.magicVersion; |
| 3794 | if (memcmp(magic+4, OBCC_MAGIC_VERS, 4) != 0) { |
| 3795 | LOGW("bcc: stale oBCC version (0x%02x %02x %02x %02x)\n", |
| 3796 | magicVer[0], magicVer[1], magicVer[2], magicVer[3]); |
| 3797 | goto bail; |
| 3798 | } |
| 3799 | |
| 3800 | /* |
| 3801 | * Do the header flags match up with what we want? |
| 3802 | * |
| 3803 | * This is useful because it allows us to automatically regenerate |
| 3804 | * a file when settings change (e.g. verification is now mandatory), |
| 3805 | * but can cause difficulties if the thing we depend upon |
| 3806 | * were handled differently than the current options specify. |
| 3807 | * |
| 3808 | * So, for now, we essentially ignore "expectVerify" and "expectOpt" |
| 3809 | * by limiting the match mask. |
| 3810 | * |
| 3811 | * The only thing we really can't handle is incorrect byte-ordering. |
| 3812 | */ |
| 3813 | |
| 3814 | val = optHdr.sourceWhen; |
| 3815 | if (val && (val != sourceWhen)) { |
| 3816 | LOGI("bcc: source file mod time mismatch (%08x vs %08x)\n", |
| 3817 | val, sourceWhen); |
| 3818 | goto bail; |
| 3819 | } |
| 3820 | val = optHdr.rslibWhen; |
| 3821 | if (val && (val != rslibWhen)) { |
| 3822 | LOGI("bcc: rslib file mod time mismatch (%08x vs %08x)\n", |
| 3823 | val, rslibWhen); |
| 3824 | goto bail; |
| 3825 | } |
| 3826 | val = optHdr.libRSWhen; |
| 3827 | if (val && (val != libRSWhen)) { |
| 3828 | LOGI("bcc: libRS file mod time mismatch (%08x vs %08x)\n", |
| 3829 | val, libRSWhen); |
| 3830 | goto bail; |
| 3831 | } |
| 3832 | val = optHdr.libbccWhen; |
| 3833 | if (val && (val != libbccWhen)) { |
| 3834 | LOGI("bcc: libbcc file mod time mismatch (%08x vs %08x)\n", |
| 3835 | val, libbccWhen); |
| 3836 | goto bail; |
| 3837 | } |
| 3838 | |
| 3839 | return true; |
| 3840 | |
| 3841 | bail: |
| 3842 | return false; |
| 3843 | } |
| 3844 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3845 | }; |
| 3846 | // End of Class Compiler |
| 3847 | //////////////////////////////////////////////////////////////////////////////// |
| 3848 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3849 | |
| 3850 | bool Compiler::GlobalInitialized = false; |
| 3851 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3852 | // Code generation optimization level for the compiler |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3853 | llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel; |
| 3854 | |
| 3855 | std::string Compiler::Triple; |
| 3856 | |
| 3857 | std::string Compiler::CPU; |
| 3858 | |
| 3859 | std::vector<std::string> Compiler::Features; |
| 3860 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3861 | // The named of metadata node that pragma resides (should be synced with |
| 3862 | // slang.cpp) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3863 | const llvm::StringRef Compiler::PragmaMetadataName = "#pragma"; |
| 3864 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3865 | // The named of metadata node that export variable name resides (should be |
| 3866 | // synced with slang_rs_metadata.h) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 3867 | const llvm::StringRef Compiler::ExportVarMetadataName = "#rs_export_var"; |
| 3868 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3869 | // The named of metadata node that export function name resides (should be |
| 3870 | // synced with slang_rs_metadata.h) |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 3871 | const llvm::StringRef Compiler::ExportFuncMetadataName = "#rs_export_func"; |
| 3872 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3873 | struct BCCscript { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3874 | ////////////////////////////////////////////////////////////////////////////// |
| 3875 | // Part I. Compiler |
| 3876 | ////////////////////////////////////////////////////////////////////////////// |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3877 | Compiler compiler; |
| 3878 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3879 | void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3880 | compiler.registerSymbolCallback(pFn, pContext); |
| 3881 | } |
| 3882 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3883 | ////////////////////////////////////////////////////////////////////////////// |
| 3884 | // Part II. Logistics & Error handling |
| 3885 | ////////////////////////////////////////////////////////////////////////////// |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3886 | BCCscript() { |
| 3887 | bccError = BCC_NO_ERROR; |
| 3888 | } |
| 3889 | |
| 3890 | ~BCCscript() { |
| 3891 | } |
| 3892 | |
| 3893 | void setError(BCCenum error) { |
| 3894 | if (bccError == BCC_NO_ERROR && error != BCC_NO_ERROR) { |
| 3895 | bccError = error; |
| 3896 | } |
| 3897 | } |
| 3898 | |
| 3899 | BCCenum getError() { |
| 3900 | BCCenum result = bccError; |
| 3901 | bccError = BCC_NO_ERROR; |
| 3902 | return result; |
| 3903 | } |
| 3904 | |
| 3905 | BCCenum bccError; |
| 3906 | }; |
| 3907 | |
| 3908 | |
| 3909 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3910 | BCCscript *bccCreateScript() { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3911 | return new BCCscript(); |
| 3912 | } |
| 3913 | |
| 3914 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3915 | BCCenum bccGetError(BCCscript *script) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3916 | return script->getError(); |
| 3917 | } |
| 3918 | |
| 3919 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3920 | void bccDeleteScript(BCCscript *script) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3921 | delete script; |
| 3922 | } |
| 3923 | |
| 3924 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3925 | void bccRegisterSymbolCallback(BCCscript *script, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3926 | BCCSymbolLookupFn pFn, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3927 | BCCvoid *pContext) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3928 | script->registerSymbolCallback(pFn, pContext); |
| 3929 | } |
| 3930 | |
| 3931 | extern "C" |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3932 | int bccReadModule(BCCscript *script, |
Zonr Chang | dbee68b | 2010-10-22 05:02:16 +0800 | [diff] [blame] | 3933 | BCCvoid *module) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3934 | return script->compiler.readModule(reinterpret_cast<llvm::Module*>(module)); |
Zonr Chang | dbee68b | 2010-10-22 05:02:16 +0800 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | extern "C" |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3938 | int bccReadBC(BCCscript *script, |
| 3939 | const BCCchar *bitcode, |
| 3940 | BCCint size, |
| 3941 | const BCCchar *resName) { |
| 3942 | return script->compiler.readBC(bitcode, size, resName); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | extern "C" |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3946 | void bccLinkBC(BCCscript *script, |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 3947 | const BCCchar *bitcode, |
| 3948 | BCCint size) { |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3949 | script->compiler.linkBC(bitcode, size); |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | extern "C" |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3953 | void bccLoadBinary(BCCscript *script) { |
| 3954 | int result = script->compiler.loader(); |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 3955 | if (result) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3956 | script->setError(BCC_INVALID_OPERATION); |
| 3957 | } |
| 3958 | |
| 3959 | extern "C" |
Shih-wei Liao | 7c5a5f7 | 2010-11-08 01:59:13 -0800 | [diff] [blame] | 3960 | void bccCompileBC(BCCscript *script) { |
| 3961 | { |
| 3962 | #if defined(__arm__) |
| 3963 | android::StopWatch compileTimer("RenderScript compile time"); |
| 3964 | #endif |
| 3965 | int result = script->compiler.compile(); |
| 3966 | if (result) |
| 3967 | script->setError(BCC_INVALID_OPERATION); |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3972 | void bccGetScriptInfoLog(BCCscript *script, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3973 | BCCsizei maxLength, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3974 | BCCsizei *length, |
| 3975 | BCCchar *infoLog) { |
| 3976 | char *message = script->compiler.getErrorMessage(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3977 | int messageLength = strlen(message) + 1; |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 3978 | if (length) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3979 | *length = messageLength; |
| 3980 | |
| 3981 | if (infoLog && maxLength > 0) { |
| 3982 | int trimmedLength = maxLength < messageLength ? maxLength : messageLength; |
| 3983 | memcpy(infoLog, message, trimmedLength); |
| 3984 | infoLog[trimmedLength] = 0; |
| 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 3989 | void bccGetScriptLabel(BCCscript *script, |
| 3990 | const BCCchar *name, |
| 3991 | BCCvoid **address) { |
| 3992 | void *value = script->compiler.lookup(name); |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 3993 | if (value) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3994 | *address = value; |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 3995 | else |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3996 | script->setError(BCC_INVALID_VALUE); |
| 3997 | } |
| 3998 | |
| 3999 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4000 | void bccGetExportVars(BCCscript *script, |
| 4001 | BCCsizei *actualVarCount, |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 4002 | BCCsizei maxVarCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4003 | BCCvoid **vars) { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 4004 | script->compiler.getExportVars(actualVarCount, maxVarCount, vars); |
| 4005 | } |
| 4006 | |
| 4007 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4008 | void bccGetExportFuncs(BCCscript *script, |
| 4009 | BCCsizei *actualFuncCount, |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 4010 | BCCsizei maxFuncCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4011 | BCCvoid **funcs) { |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 4012 | script->compiler.getExportFuncs(actualFuncCount, maxFuncCount, funcs); |
| 4013 | } |
| 4014 | |
| 4015 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4016 | void bccGetPragmas(BCCscript *script, |
| 4017 | BCCsizei *actualStringCount, |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 4018 | BCCsizei maxStringCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4019 | BCCchar **strings) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4020 | script->compiler.getPragmas(actualStringCount, maxStringCount, strings); |
| 4021 | } |
| 4022 | |
| 4023 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4024 | void bccGetFunctions(BCCscript *script, |
| 4025 | BCCsizei *actualFunctionCount, |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 4026 | BCCsizei maxFunctionCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4027 | BCCchar **functions) { |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 4028 | script->compiler.getFunctions(actualFunctionCount, |
| 4029 | maxFunctionCount, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4030 | functions); |
| 4031 | } |
| 4032 | |
| 4033 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4034 | void bccGetFunctionBinary(BCCscript *script, |
| 4035 | BCCchar *function, |
| 4036 | BCCvoid **base, |
| 4037 | BCCsizei *length) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4038 | script->compiler.getFunctionBinary(function, base, length); |
| 4039 | } |
| 4040 | |
| 4041 | struct BCCtype { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4042 | const Compiler *compiler; |
| 4043 | const llvm::Type *t; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 4044 | }; |
| 4045 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4046 | } // namespace bcc |