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