Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1 | /* |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2 | * Copyright 2010, The Android Open Source Project |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 3 | * |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 17 | // Bitcode compiler (bcc) for Android: |
| 18 | // This is an eager-compilation JIT running on Android. |
| 19 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 20 | #define LOG_TAG "bcc" |
| 21 | #include <cutils/log.h> |
| 22 | |
| 23 | #include <ctype.h> |
| 24 | #include <errno.h> |
| 25 | #include <limits.h> |
| 26 | #include <stdarg.h> |
| 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <unistd.h> |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 32 | #include <sys/mman.h> |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 33 | |
| 34 | #include <cutils/hashmap.h> |
| 35 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 36 | #if defined(__arm__) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 37 | # define DEFAULT_ARM_CODEGEN |
| 38 | # define PROVIDE_ARM_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 39 | #elif defined(__i386__) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 40 | # define DEFAULT_X86_CODEGEN |
| 41 | # define PROVIDE_X86_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 42 | #elif defined(__x86_64__) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 43 | # define DEFAULT_X64_CODEGEN |
| 44 | # define PROVIDE_X64_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | #if defined(FORCE_ARM_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 48 | # define DEFAULT_ARM_CODEGEN |
| 49 | # undef DEFAULT_X86_CODEGEN |
| 50 | # undef DEFAULT_X64_CODEGEN |
| 51 | # define PROVIDE_ARM_CODEGEN |
| 52 | # undef PROVIDE_X86_CODEGEN |
| 53 | # undef PROVIDE_X64_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 54 | #elif defined(FORCE_X86_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 55 | # undef DEFAULT_ARM_CODEGEN |
| 56 | # define DEFAULT_X86_CODEGEN |
| 57 | # undef DEFAULT_X64_CODEGEN |
| 58 | # undef PROVIDE_ARM_CODEGEN |
| 59 | # define PROVIDE_X86_CODEGEN |
| 60 | # undef PROVIDE_X64_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 61 | #elif defined(FORCE_X64_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 62 | # undef DEFAULT_ARM_CODEGEN |
| 63 | # undef DEFAULT_X86_CODEGEN |
| 64 | # define DEFAULT_X64_CODEGEN |
| 65 | # undef PROVIDE_ARM_CODEGEN |
| 66 | # undef PROVIDE_X86_CODEGEN |
| 67 | # define PROVIDE_X64_CODEGEN |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | #if defined(DEFAULT_ARM_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 71 | # define TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 72 | #elif defined(DEFAULT_X86_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 73 | # define TARGET_TRIPLE_STRING "i686-unknown-linux" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 74 | #elif defined(DEFAULT_X64_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 75 | # define TARGET_TRIPLE_STRING "x86_64-unknown-linux" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 76 | #endif |
| 77 | |
| 78 | #if (defined(__VFP_FP__) && !defined(__SOFTFP__)) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 79 | # define ARM_USE_VFP |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | #include <bcc/bcc.h> |
| 83 | #include "bcc_runtime.h" |
| 84 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 85 | #define LOG_API(...) do {} while (0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 86 | // #define LOG_API(...) fprintf (stderr, __VA_ARGS__) |
| 87 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 88 | #define LOG_STACK(...) do {} while (0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 89 | // #define LOG_STACK(...) fprintf (stderr, __VA_ARGS__) |
| 90 | |
| 91 | // #define PROVIDE_TRACE_CODEGEN |
| 92 | |
| 93 | #if defined(USE_DISASSEMBLER) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 94 | # include "llvm/MC/MCInst.h" |
| 95 | # include "llvm/MC/MCAsmInfo.h" |
| 96 | # include "llvm/MC/MCInstPrinter.h" |
| 97 | # include "llvm/MC/MCDisassembler.h" |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 98 | // If you want the disassemble results written to file, define this: |
| 99 | # define USE_DISASSEMBLER_FILE |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 100 | #endif |
| 101 | |
| 102 | #include <set> |
| 103 | #include <map> |
| 104 | #include <list> |
| 105 | #include <cmath> |
| 106 | #include <string> |
| 107 | #include <cstring> |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 108 | #include <algorithm> // for std::reverse |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 109 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 110 | // VMCore |
| 111 | #include "llvm/Use.h" |
| 112 | #include "llvm/User.h" |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 113 | #include "llvm/Linker.h" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 114 | #include "llvm/Module.h" |
| 115 | #include "llvm/Function.h" |
| 116 | #include "llvm/Constant.h" |
| 117 | #include "llvm/Constants.h" |
| 118 | #include "llvm/Instruction.h" |
| 119 | #include "llvm/PassManager.h" |
| 120 | #include "llvm/LLVMContext.h" |
| 121 | #include "llvm/GlobalValue.h" |
| 122 | #include "llvm/Instructions.h" |
| 123 | #include "llvm/OperandTraits.h" |
| 124 | #include "llvm/TypeSymbolTable.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 125 | |
| 126 | // System |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 127 | #include "llvm/System/Host.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 128 | |
| 129 | // ADT |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 130 | #include "llvm/ADT/APInt.h" |
| 131 | #include "llvm/ADT/APFloat.h" |
| 132 | #include "llvm/ADT/DenseMap.h" |
| 133 | #include "llvm/ADT/ValueMap.h" |
| 134 | #include "llvm/ADT/StringMap.h" |
| 135 | #include "llvm/ADT/OwningPtr.h" |
| 136 | #include "llvm/ADT/SmallString.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 137 | |
| 138 | // Target |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 139 | #include "llvm/Target/TargetData.h" |
| 140 | #include "llvm/Target/TargetSelect.h" |
| 141 | #include "llvm/Target/TargetOptions.h" |
| 142 | #include "llvm/Target/TargetMachine.h" |
| 143 | #include "llvm/Target/TargetJITInfo.h" |
| 144 | #include "llvm/Target/TargetRegistry.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 145 | #include "llvm/Target/SubtargetFeature.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 146 | |
| 147 | // Support |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 148 | #include "llvm/Support/Casting.h" |
| 149 | #include "llvm/Support/raw_ostream.h" |
| 150 | #include "llvm/Support/ValueHandle.h" |
| 151 | #include "llvm/Support/MemoryBuffer.h" |
| 152 | #include "llvm/Support/MemoryObject.h" |
| 153 | #include "llvm/Support/ManagedStatic.h" |
| 154 | #include "llvm/Support/ErrorHandling.h" |
| 155 | #include "llvm/Support/StandardPasses.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 156 | #include "llvm/Support/FormattedStream.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 157 | |
| 158 | // Bitcode |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 159 | #include "llvm/Bitcode/ReaderWriter.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 160 | |
| 161 | // CodeGen |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 162 | #include "llvm/CodeGen/Passes.h" |
| 163 | #include "llvm/CodeGen/JITCodeEmitter.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 164 | #include "llvm/CodeGen/MachineFunction.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 165 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 166 | #include "llvm/CodeGen/SchedulerRegistry.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 167 | #include "llvm/CodeGen/MachineRelocation.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 168 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 169 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 170 | #include "llvm/CodeGen/MachineConstantPool.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 171 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 172 | |
| 173 | // ExecutionEngine |
| 174 | #include "llvm/ExecutionEngine/GenericValue.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 175 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 176 | |
| 177 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 178 | // |
| 179 | // Compilation class that suits Android's needs. |
| 180 | // (Support: no argument passed, ...) |
| 181 | // |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 182 | namespace bcc { |
| 183 | |
| 184 | class Compiler { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 185 | // This part is designed to be orthogonal to those exported bcc*() functions |
| 186 | // implementation and internal struct BCCscript. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 187 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 188 | ////////////////////////////////////////////////////////////////////////////// |
| 189 | // The variable section below (e.g., Triple, CodeGenOptLevel) |
| 190 | // is initialized in GlobalInitialization() |
| 191 | // |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 192 | static bool GlobalInitialized; |
| 193 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 194 | // If given, this will be the name of the target triple to compile for. |
| 195 | // 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] | 196 | static std::string Triple; |
| 197 | |
| 198 | static llvm::CodeGenOpt::Level CodeGenOptLevel; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 199 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 200 | // End of section of GlobalInitializing variables |
| 201 | ////////////////////////////////////////////////////////////////////////////// |
| 202 | |
| 203 | // 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] | 204 | static std::string CPU; |
| 205 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 206 | // The list of target specific features to enable or disable -- this should |
| 207 | // be a list of strings starting with '+' (enable) or '-' (disable). |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 208 | static std::vector<std::string> Features; |
| 209 | |
| 210 | struct Runtime { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 211 | const char *mName; |
| 212 | void *mPtr; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 213 | }; |
| 214 | static struct Runtime Runtimes[]; |
| 215 | |
| 216 | static void GlobalInitialization() { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 217 | if (GlobalInitialized) |
| 218 | return; |
Shih-wei Liao | be5c531 | 2010-05-09 05:30:09 -0700 | [diff] [blame] | 219 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 220 | // if (!llvm::llvm_is_multithreaded()) |
| 221 | // llvm::llvm_start_multithreaded(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 222 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 223 | // Set Triple, CPU and Features here |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 224 | Triple = TARGET_TRIPLE_STRING; |
| 225 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 226 | // TODO(zonr): NEON for JIT |
| 227 | // Features.push_back("+neon"); |
| 228 | // Features.push_back("+vmlx"); |
| 229 | // Features.push_back("+neonfp"); |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 230 | Features.push_back("+vfp3"); |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 231 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 232 | #if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN) |
| 233 | LLVMInitializeARMTargetInfo(); |
| 234 | LLVMInitializeARMTarget(); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 235 | #if defined(USE_DISASSEMBLER) |
| 236 | LLVMInitializeARMDisassembler(); |
| 237 | LLVMInitializeARMAsmPrinter(); |
| 238 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 239 | #endif |
| 240 | |
| 241 | #if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN) |
| 242 | LLVMInitializeX86TargetInfo(); |
| 243 | LLVMInitializeX86Target(); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 244 | #if defined(USE_DISASSEMBLER) |
| 245 | LLVMInitializeX86Disassembler(); |
| 246 | LLVMInitializeX86AsmPrinter(); |
| 247 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 248 | #endif |
| 249 | |
| 250 | #if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN) |
| 251 | LLVMInitializeX86TargetInfo(); |
| 252 | LLVMInitializeX86Target(); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 253 | #if defined(USE_DISASSEMBLER) |
| 254 | LLVMInitializeX86Disassembler(); |
| 255 | LLVMInitializeX86AsmPrinter(); |
| 256 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 257 | #endif |
| 258 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 259 | // -O0: llvm::CodeGenOpt::None |
| 260 | // -O1: llvm::CodeGenOpt::Less |
| 261 | // -O2: llvm::CodeGenOpt::Default |
| 262 | // -O3: llvm::CodeGenOpt::Aggressive |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 263 | CodeGenOptLevel = llvm::CodeGenOpt::Aggressive; |
| 264 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 265 | // Below are the global settings to LLVM |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 266 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 267 | // Disable frame pointer elimination optimization |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 268 | llvm::NoFramePointerElim = false; |
| 269 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 270 | // Use hardfloat ABI |
| 271 | // |
| 272 | // FIXME: Need to detect the CPU capability and decide whether to use |
| 273 | // softfp. To use softfp, change following 2 lines to |
| 274 | // |
| 275 | // llvm::FloatABIType = llvm::FloatABI::Soft; |
| 276 | // llvm::UseSoftFloat = true; |
| 277 | // |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 278 | llvm::FloatABIType = llvm::FloatABI::Soft; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 279 | llvm::UseSoftFloat = false; |
| 280 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 281 | // BCC needs all unknown symbols resolved at JIT/compilation time. |
| 282 | // So we don't need any dynamic relocation model. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 283 | llvm::TargetMachine::setRelocationModel(llvm::Reloc::Static); |
| 284 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 285 | #if defined(DEFAULT_X64_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 286 | // 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] | 287 | llvm::TargetMachine::setCodeModel(llvm::CodeModel::Medium); |
| 288 | #else |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 289 | // This is set for the linker (specify how large of the virtual addresses |
| 290 | // we can access for all unknown symbols.) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 291 | llvm::TargetMachine::setCodeModel(llvm::CodeModel::Small); |
| 292 | #endif |
| 293 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 294 | // Register the scheduler |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 295 | llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler); |
| 296 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 297 | // Register allocation policy: |
| 298 | // createFastRegisterAllocator: fast but bad quality |
| 299 | // createLinearScanRegisterAllocator: not so fast but good quality |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 300 | llvm::RegisterRegAlloc::setDefault |
| 301 | ((CodeGenOptLevel == llvm::CodeGenOpt::None) ? |
Shih-wei Liao | 1601601 | 2010-09-10 17:55:03 -0700 | [diff] [blame] | 302 | llvm::createFastRegisterAllocator : |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 303 | llvm::createLinearScanRegisterAllocator); |
| 304 | |
| 305 | GlobalInitialized = true; |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 310 | std::string *Error = static_cast<std::string*>(UserData); |
Shih-wei Liao | 066d5ef | 2010-05-11 03:28:39 -0700 | [diff] [blame] | 311 | Error->assign(Message); |
Nick Kralevich | fc97e9f | 2010-05-17 14:59:16 -0700 | [diff] [blame] | 312 | LOGE("%s", Message.c_str()); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 313 | exit(1); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static const llvm::StringRef PragmaMetadataName; |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 317 | static const llvm::StringRef ExportVarMetadataName; |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 318 | static const llvm::StringRef ExportFuncMetadataName; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 319 | |
| 320 | private: |
Shih-wei Liao | c561199 | 2010-05-09 06:37:55 -0700 | [diff] [blame] | 321 | std::string mError; |
| 322 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 323 | inline bool hasError() const { |
| 324 | return !mError.empty(); |
| 325 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 326 | inline void setError(const char *Error) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 327 | mError.assign(Error); // Copying |
| 328 | return; |
| 329 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 330 | inline void setError(const std::string &Error) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 331 | mError = Error; |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | typedef std::list< std::pair<std::string, std::string> > PragmaList; |
| 336 | PragmaList mPragmas; |
| 337 | |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 338 | typedef std::list<void*> ExportVarList; |
| 339 | ExportVarList mExportVars; |
| 340 | |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 341 | typedef std::list<void*> ExportFuncList; |
| 342 | ExportFuncList mExportFuncs; |
| 343 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 344 | ////////////////////////////////////////////////////////////////////////////// |
| 345 | // Memory manager for the code reside in memory |
| 346 | // |
| 347 | // The memory for our code emitter is very simple and is conforming to the |
| 348 | // design decisions of Android RenderScript's Exection Environment: |
| 349 | // The code, data, and symbol sizes are limited (currently 100KB.) |
| 350 | // |
| 351 | // It's very different from typical compiler, which has no limitation |
| 352 | // on the code size. How does code emitter know the size of the code |
| 353 | // it is about to emit? It does not know beforehand. We want to solve |
| 354 | // this without complicating the code emitter too much. |
| 355 | // |
| 356 | // We solve this by pre-allocating a certain amount of memory, |
| 357 | // and then start the code emission. Once the buffer overflows, the emitter |
| 358 | // simply discards all the subsequent emission but still has a counter |
| 359 | // on how many bytes have been emitted. |
| 360 | // |
| 361 | // So once the whole emission is done, if there's a buffer overflow, |
| 362 | // it re-allocates the buffer with enough size (based on the |
| 363 | // counter from previous emission) and re-emit again. |
| 364 | // |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 365 | class CodeMemoryManager : public llvm::JITMemoryManager { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 366 | private: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 367 | // 128 KiB for code |
| 368 | static const unsigned int MaxCodeSize = 128 * 1024; |
| 369 | // 1 KiB for global offset table (GOT) |
| 370 | static const unsigned int MaxGOTSize = 1 * 1024; |
| 371 | // 128 KiB for global variable |
| 372 | static const unsigned int MaxGlobalVarSize = 128 * 1024; |
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 | // |
| 375 | // Our memory layout is as follows: |
| 376 | // |
| 377 | // The direction of arrows (-> and <-) shows memory's growth direction |
| 378 | // when more space is needed. |
| 379 | // |
| 380 | // @mpCodeMem: |
| 381 | // +--------------------------------------------------------------+ |
| 382 | // | Function Memory ... -> <- ... Stub/GOT | |
| 383 | // +--------------------------------------------------------------+ |
| 384 | // |<------------------ Total: @MaxCodeSize KiB ----------------->| |
| 385 | // |
| 386 | // Where size of GOT is @MaxGOTSize KiB. |
| 387 | // |
| 388 | // @mpGVMem: |
| 389 | // +--------------------------------------------------------------+ |
| 390 | // | Global variable ... -> | |
| 391 | // +--------------------------------------------------------------+ |
| 392 | // |<--------------- Total: @MaxGlobalVarSize KiB --------------->| |
| 393 | // |
| 394 | // |
| 395 | // @mCurFuncMemIdx: The current index (starting from 0) of the last byte |
| 396 | // of function code's memory usage |
| 397 | // @mCurSGMemIdx: The current index (starting from tail) of the last byte |
| 398 | // of stub/GOT's memory usage |
| 399 | // @mCurGVMemIdx: The current index (starting from tail) of the last byte |
| 400 | // of global variable's memory usage |
| 401 | // |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 402 | uintptr_t mCurFuncMemIdx; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 403 | uintptr_t mCurSGMemIdx; |
| 404 | uintptr_t mCurGVMemIdx; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 405 | void *mpCodeMem; |
| 406 | void *mpGVMem; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 407 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 408 | // GOT Base |
| 409 | uint8_t *mpGOTBase; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 410 | |
| 411 | typedef std::map<const llvm::Function*, pair<void* /* start address */, |
| 412 | void* /* end address */> |
| 413 | > FunctionMapTy; |
| 414 | FunctionMapTy mFunctionMap; |
| 415 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 416 | inline intptr_t getFreeCodeMemSize() const { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 417 | return mCurSGMemIdx - mCurFuncMemIdx; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 418 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 419 | inline uint8_t *getCodeMemBase() const { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 420 | return reinterpret_cast<uint8_t*>(mpCodeMem); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 423 | uint8_t *allocateSGMemory(uintptr_t Size, |
| 424 | unsigned Alignment = 1 /* no alignment */) { |
| 425 | intptr_t FreeMemSize = getFreeCodeMemSize(); |
| 426 | if ((FreeMemSize < 0) || (static_cast<uintptr_t>(FreeMemSize) < Size)) |
| 427 | // The code size excesses our limit |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 428 | return NULL; |
| 429 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 430 | if (Alignment == 0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 431 | Alignment = 1; |
| 432 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 433 | uint8_t *result = getCodeMemBase() + mCurSGMemIdx - Size; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 434 | result = (uint8_t*) (((intptr_t) result) & ~(intptr_t) (Alignment - 1)); |
| 435 | |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 436 | mCurSGMemIdx = result - getCodeMemBase(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 437 | |
| 438 | return result; |
| 439 | } |
| 440 | |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 441 | inline uintptr_t getFreeGVMemSize() const { |
| 442 | return MaxGlobalVarSize - mCurGVMemIdx; |
| 443 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 444 | inline uint8_t *getGVMemBase() const { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 445 | return reinterpret_cast<uint8_t*>(mpGVMem); |
| 446 | } |
| 447 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 448 | public: |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 449 | CodeMemoryManager() : mpCodeMem(NULL), mpGVMem(NULL), mpGOTBase(NULL) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 450 | reset(); |
| 451 | std::string ErrMsg; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 452 | |
Shih-wei Liao | c7c9cd5 | 2010-09-27 11:23:25 -0700 | [diff] [blame] | 453 | mpCodeMem = ::mmap(NULL, MaxCodeSize, PROT_READ | PROT_EXEC, |
Ying Wang | 78db12e | 2010-09-25 17:48:59 -0700 | [diff] [blame] | 454 | MAP_PRIVATE | MAP_ANON, -1, 0); |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 455 | if (mpCodeMem == MAP_FAILED) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 456 | llvm::report_fatal_error("Failed to allocate memory for emitting " |
| 457 | "function codes\n" + ErrMsg); |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 458 | |
| 459 | mpGVMem = ::mmap(mpCodeMem, MaxGlobalVarSize, |
Shih-wei Liao | c7c9cd5 | 2010-09-27 11:23:25 -0700 | [diff] [blame] | 460 | PROT_READ | PROT_WRITE, |
Ying Wang | 78db12e | 2010-09-25 17:48:59 -0700 | [diff] [blame] | 461 | MAP_PRIVATE | MAP_ANON, -1, 0); |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 462 | if (mpGVMem == MAP_FAILED) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 463 | llvm::report_fatal_error("Failed to allocate memory for emitting " |
| 464 | "global variables\n" + ErrMsg); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 465 | |
| 466 | return; |
| 467 | } |
| 468 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 469 | // setMemoryWritable - When code generation is in progress, the code pages |
| 470 | // may need permissions changed. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 471 | void setMemoryWritable() { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 472 | ::mprotect(mpCodeMem, MaxCodeSize, PROT_READ | PROT_WRITE | PROT_EXEC); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 476 | // When code generation is done and we're ready to start execution, the |
| 477 | // code pages may need permissions changed. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 478 | void setMemoryExecutable() { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 479 | ::mprotect(mpCodeMem, MaxCodeSize, PROT_READ | PROT_EXEC); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 483 | // Setting this flag to true makes the memory manager garbage values over |
| 484 | // freed memory. This is useful for testing and debugging, and is to be |
| 485 | // turned on by default in debug mode. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 486 | void setPoisonMemory(bool poison) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 487 | // no effect |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 491 | // Global Offset Table Management |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 492 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 493 | // If the current table requires a Global Offset Table, this method is |
| 494 | // 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] | 495 | void AllocateGOT() { |
| 496 | assert(mpGOTBase != NULL && "Cannot allocate the GOT multiple times"); |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 497 | mpGOTBase = allocateSGMemory(MaxGOTSize); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 498 | HasGOT = true; |
| 499 | return; |
| 500 | } |
| 501 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 502 | // If this is managing a Global Offset Table, this method should return a |
| 503 | // pointer to its base. |
| 504 | uint8_t *getGOTBase() const { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 505 | return mpGOTBase; |
| 506 | } |
| 507 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 508 | // Main Allocation Functions |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 509 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 510 | // When we start JITing a function, the JIT calls this method to allocate a |
| 511 | // block of free RWX memory, which returns a pointer to it. If the JIT wants |
| 512 | // to request a block of memory of at least a certain size, it passes that |
| 513 | // value as ActualSize, and this method returns a block with at least that |
| 514 | // much space. If the JIT doesn't know ahead of time how much space it will |
| 515 | // need to emit the function, it passes 0 for the ActualSize. In either |
| 516 | // case, this method is required to pass back the size of the allocated |
| 517 | // block through ActualSize. The JIT will be careful to not write more than |
| 518 | // the returned ActualSize bytes of memory. |
| 519 | uint8_t *startFunctionBody(const llvm::Function *F, uintptr_t &ActualSize) { |
| 520 | intptr_t FreeMemSize = getFreeCodeMemSize(); |
| 521 | if ((FreeMemSize < 0) || |
| 522 | (static_cast<uintptr_t>(FreeMemSize) < ActualSize)) |
| 523 | // The code size excesses our limit |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 524 | return NULL; |
| 525 | |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 526 | ActualSize = getFreeCodeMemSize(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 527 | return (getCodeMemBase() + mCurFuncMemIdx); |
| 528 | } |
| 529 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 530 | // This method is called by the JIT to allocate space for a function stub |
| 531 | // (used to handle limited branch displacements) while it is JIT compiling a |
| 532 | // function. For example, if foo calls bar, and if bar either needs to be |
| 533 | // lazily compiled or is a native function that exists too far away from the |
| 534 | // call site to work, this method will be used to make a thunk for it. The |
| 535 | // stub should be "close" to the current function body, but should not be |
| 536 | // included in the 'actualsize' returned by startFunctionBody. |
| 537 | uint8_t *allocateStub(const llvm::GlobalValue *F, unsigned StubSize, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 538 | unsigned Alignment) { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 539 | return allocateSGMemory(StubSize, Alignment); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 542 | // This method is called when the JIT is done codegen'ing the specified |
| 543 | // function. At this point we know the size of the JIT compiled function. |
| 544 | // This passes in FunctionStart (which was returned by the startFunctionBody |
| 545 | // method) and FunctionEnd which is a pointer to the actual end of the |
| 546 | // function. This method should mark the space allocated and remember where |
| 547 | // it is in case the client wants to deallocate it. |
| 548 | void endFunctionBody(const llvm::Function *F, uint8_t *FunctionStart, |
| 549 | uint8_t *FunctionEnd) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 550 | assert(FunctionEnd > FunctionStart); |
| 551 | assert(FunctionStart == (getCodeMemBase() + mCurFuncMemIdx) && |
| 552 | "Mismatched function start/end!"); |
| 553 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 554 | // Advance the pointer |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 555 | intptr_t FunctionCodeSize = FunctionEnd - FunctionStart; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 556 | assert(FunctionCodeSize <= getFreeCodeMemSize() && |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 557 | "Code size excess the limitation!"); |
| 558 | mCurFuncMemIdx += FunctionCodeSize; |
| 559 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 560 | // Record there's a function in our memory start from @FunctionStart |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 561 | assert(mFunctionMap.find(F) == mFunctionMap.end() && |
| 562 | "Function already emitted!"); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 563 | mFunctionMap.insert( |
| 564 | std::make_pair<const llvm::Function*, std::pair<void*, void*> >( |
| 565 | F, std::make_pair(FunctionStart, FunctionEnd))); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 566 | |
| 567 | return; |
| 568 | } |
| 569 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 570 | // Allocate a (function code) memory block of the given size. This method |
| 571 | // cannot be called between calls to startFunctionBody and endFunctionBody. |
| 572 | uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) { |
| 573 | if (getFreeCodeMemSize() < Size) |
| 574 | // The code size excesses our limit |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 575 | return NULL; |
| 576 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 577 | if (Alignment == 0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 578 | Alignment = 1; |
| 579 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 580 | uint8_t *result = getCodeMemBase() + mCurFuncMemIdx; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 581 | result = (uint8_t*) (((intptr_t) result + Alignment - 1) & |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 582 | ~(intptr_t) (Alignment - 1)); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 583 | |
| 584 | mCurFuncMemIdx = (result + Size) - getCodeMemBase(); |
| 585 | |
| 586 | return result; |
| 587 | } |
| 588 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 589 | // Allocate memory for a global variable. |
| 590 | uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) { |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 591 | if (getFreeGVMemSize() < Size) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 592 | // The code size excesses our limit |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 593 | LOGE("No Global Memory"); |
| 594 | return NULL; |
| 595 | } |
| 596 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 597 | if (Alignment == 0) |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 598 | Alignment = 1; |
| 599 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 600 | uint8_t *result = getGVMemBase() + mCurGVMemIdx; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 601 | result = (uint8_t*) (((intptr_t) result + Alignment - 1) & |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 602 | ~(intptr_t) (Alignment - 1)); |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 603 | |
| 604 | mCurGVMemIdx = (result + Size) - getGVMemBase(); |
| 605 | |
| 606 | return result; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 609 | // Free the specified function body. The argument must be the return value |
| 610 | // from a call to startFunctionBody() that hasn't been deallocated yet. This |
| 611 | // is never called when the JIT is currently emitting a function. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 612 | void deallocateFunctionBody(void *Body) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 613 | // linear search |
| 614 | uint8_t *FunctionStart = NULL, *FunctionEnd = NULL; |
| 615 | for (FunctionMapTy::iterator I = mFunctionMap.begin(), |
| 616 | E = mFunctionMap.end(); |
| 617 | I != E; |
| 618 | I++) |
| 619 | if (I->second.first == Body) { |
| 620 | FunctionStart = reinterpret_cast<uint8_t*>(I->second.first); |
| 621 | FunctionEnd = reinterpret_cast<uint8_t*>(I->second.second); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 622 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 623 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 624 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 625 | assert((FunctionStart == NULL) && "Memory is never allocated!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 626 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 627 | // free the memory |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 628 | intptr_t SizeNeedMove = (getCodeMemBase() + mCurFuncMemIdx) - FunctionEnd; |
| 629 | |
| 630 | assert(SizeNeedMove >= 0 && |
| 631 | "Internal error: CodeMemoryManager::mCurFuncMemIdx may not" |
| 632 | " be correctly calculated!"); |
| 633 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 634 | if (SizeNeedMove > 0) |
| 635 | // there's data behind deallocating function |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 636 | ::memmove(FunctionStart, FunctionEnd, SizeNeedMove); |
| 637 | mCurFuncMemIdx -= (FunctionEnd - FunctionStart); |
| 638 | |
| 639 | return; |
| 640 | } |
| 641 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 642 | // When we finished JITing the function, if exception handling is set, we |
| 643 | // emit the exception table. |
| 644 | uint8_t *startExceptionTable(const llvm::Function *F, |
| 645 | uintptr_t &ActualSize) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 646 | assert(false && "Exception is not allowed in our language specification"); |
| 647 | return NULL; |
| 648 | } |
| 649 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 650 | // 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] | 651 | void endExceptionTable(const llvm::Function *F, uint8_t *TableStart, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 652 | uint8_t *TableEnd, uint8_t *FrameRegister) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 653 | assert(false && "Exception is not allowed in our language specification"); |
| 654 | return; |
| 655 | } |
| 656 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 657 | // Free the specified exception table's memory. The argument must be the |
| 658 | // return value from a call to startExceptionTable() that hasn't been |
| 659 | // deallocated yet. This is never called when the JIT is currently emitting |
| 660 | // an exception table. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 661 | void deallocateExceptionTable(void *ET) { |
| 662 | assert(false && "Exception is not allowed in our language specification"); |
| 663 | return; |
| 664 | } |
| 665 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 666 | // Below are the methods we create |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 667 | void reset() { |
| 668 | mpGOTBase = NULL; |
| 669 | HasGOT = false; |
| 670 | |
| 671 | mCurFuncMemIdx = 0; |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 672 | mCurSGMemIdx = MaxCodeSize - 1; |
| 673 | mCurGVMemIdx = 0; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 674 | |
| 675 | mFunctionMap.clear(); |
| 676 | |
| 677 | return; |
| 678 | } |
| 679 | |
| 680 | ~CodeMemoryManager() { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 681 | if (mpCodeMem != NULL) |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 682 | ::munmap(mpCodeMem, MaxCodeSize); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 683 | if (mpGVMem != NULL) |
Shih-wei Liao | c4e4ddf | 2010-09-24 14:50:26 -0700 | [diff] [blame] | 684 | ::munmap(mpGVMem, MaxGlobalVarSize); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 685 | return; |
| 686 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 687 | }; |
| 688 | // End of class CodeMemoryManager |
| 689 | ////////////////////////////////////////////////////////////////////////////// |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 690 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 691 | // The memory manager for code emitter |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 692 | llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 693 | CodeMemoryManager *createCodeMemoryManager() { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 694 | mCodeMemMgr.reset(new CodeMemoryManager()); |
| 695 | return mCodeMemMgr.get(); |
| 696 | } |
| 697 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 698 | ////////////////////////////////////////////////////////////////////////////// |
| 699 | // Code emitter |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 700 | class CodeEmitter : public llvm::JITCodeEmitter { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 701 | public: |
| 702 | typedef llvm::DenseMap<const llvm::GlobalValue*, void*> GlobalAddressMapTy; |
| 703 | typedef GlobalAddressMapTy::const_iterator global_addresses_const_iterator; |
| 704 | |
| 705 | private: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 706 | CodeMemoryManager *mpMemMgr; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 707 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 708 | // The JITInfo for the target we are compiling to |
| 709 | const llvm::Target *mpTarget; |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 710 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 711 | llvm::TargetJITInfo *mpTJI; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 712 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 713 | const llvm::TargetData *mpTD; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 714 | |
| 715 | class EmittedFunctionCode { |
| 716 | public: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 717 | // Beginning of the function's allocation. |
| 718 | void *FunctionBody; |
| 719 | |
| 720 | // The address the function's code actually starts at. |
| 721 | void *Code; |
| 722 | |
| 723 | // The size of the function code |
| 724 | int Size; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 725 | |
| 726 | EmittedFunctionCode() : FunctionBody(NULL), Code(NULL) { return; } |
| 727 | }; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 728 | EmittedFunctionCode *mpCurEmitFunction; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 729 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 730 | typedef std::map<const std::string, |
| 731 | EmittedFunctionCode*> EmittedFunctionsMapTy; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 732 | EmittedFunctionsMapTy mEmittedFunctions; |
| 733 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 734 | // This vector is a mapping from MBB ID's to their address. It is filled in |
| 735 | // by the StartMachineBasicBlock callback and queried by the |
| 736 | // getMachineBasicBlockAddress callback. |
| 737 | std::vector<uintptr_t> mMBBLocations; |
| 738 | |
| 739 | // The constant pool for the current function. |
| 740 | llvm::MachineConstantPool *mpConstantPool; |
| 741 | |
| 742 | // A pointer to the first entry in the constant pool. |
| 743 | void *mpConstantPoolBase; |
| 744 | |
| 745 | // Addresses of individual constant pool entries. |
| 746 | llvm::SmallVector<uintptr_t, 8> mConstPoolAddresses; |
| 747 | |
| 748 | // The jump tables for the current function. |
| 749 | llvm::MachineJumpTableInfo *mpJumpTable; |
| 750 | |
| 751 | // A pointer to the first entry in the jump table. |
| 752 | void *mpJumpTableBase; |
| 753 | |
| 754 | // When outputting a function stub in the context of some other function, we |
| 755 | // save BufferBegin/BufferEnd/CurBufferPtr here. |
| 756 | uint8_t *mpSavedBufferBegin, *mpSavedBufferEnd, *mpSavedCurBufferPtr; |
| 757 | |
| 758 | // These are the relocations that the function needs, as emitted. |
| 759 | std::vector<llvm::MachineRelocation> mRelocations; |
| 760 | |
| 761 | // This vector is a mapping from Label ID's to their address. |
| 762 | llvm::DenseMap<llvm::MCSymbol*, uintptr_t> mLabelLocations; |
| 763 | |
| 764 | // Machine module info for exception informations |
| 765 | llvm::MachineModuleInfo *mpMMI; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 766 | |
| 767 | GlobalAddressMapTy mGlobalAddressMap; |
| 768 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 769 | // Replace an existing mapping for GV with a new address. This updates both |
| 770 | // maps as required. If Addr is null, the entry for the global is removed |
| 771 | // from the mappings. |
| 772 | void *UpdateGlobalMapping(const llvm::GlobalValue *GV, void *Addr) { |
| 773 | if (Addr == NULL) { |
| 774 | // Removing mapping |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 775 | GlobalAddressMapTy::iterator I = mGlobalAddressMap.find(GV); |
| 776 | void *OldVal; |
| 777 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 778 | if (I == mGlobalAddressMap.end()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 779 | OldVal = NULL; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 780 | } else { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 781 | OldVal = I->second; |
| 782 | mGlobalAddressMap.erase(I); |
| 783 | } |
| 784 | |
| 785 | return OldVal; |
| 786 | } |
| 787 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 788 | void *&CurVal = mGlobalAddressMap[GV]; |
| 789 | void *OldVal = CurVal; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 790 | |
| 791 | CurVal = Addr; |
| 792 | |
| 793 | return OldVal; |
| 794 | } |
| 795 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 796 | // Tell the execution engine that the specified global is at the specified |
| 797 | // location. This is used internally as functions are JIT'd and as global |
| 798 | // variables are laid out in memory. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 799 | void AddGlobalMapping(const llvm::GlobalValue *GV, void *Addr) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 800 | void *&CurVal = mGlobalAddressMap[GV]; |
| 801 | assert((CurVal == 0 || Addr == 0) && |
| 802 | "GlobalMapping already established!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 803 | CurVal = Addr; |
| 804 | return; |
| 805 | } |
| 806 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 807 | // This returns the address of the specified global value if it is has |
| 808 | // already been codegen'd, otherwise it returns null. |
| 809 | void *GetPointerToGlobalIfAvailable(const llvm::GlobalValue *GV) { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 810 | GlobalAddressMapTy::iterator I = mGlobalAddressMap.find(GV); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 811 | return ((I != mGlobalAddressMap.end()) ? I->second : NULL); |
| 812 | } |
| 813 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 814 | unsigned int GetConstantPoolSizeInBytes(llvm::MachineConstantPool *MCP) { |
| 815 | const std::vector<llvm::MachineConstantPoolEntry> &Constants = |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 816 | MCP->getConstants(); |
| 817 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 818 | if (Constants.empty()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 819 | return 0; |
| 820 | |
| 821 | unsigned int Size = 0; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 822 | for (int i = 0, e = Constants.size(); i != e; i++) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 823 | llvm::MachineConstantPoolEntry CPE = Constants[i]; |
| 824 | unsigned int AlignMask = CPE.getAlignment() - 1; |
| 825 | Size = (Size + AlignMask) & ~AlignMask; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 826 | const llvm::Type *Ty = CPE.getType(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 827 | Size += mpTD->getTypeAllocSize(Ty); |
| 828 | } |
| 829 | |
| 830 | return Size; |
| 831 | } |
| 832 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 833 | // This function converts a Constant* into a GenericValue. The interesting |
| 834 | // part is if C is a ConstantExpr. |
| 835 | void GetConstantValue(const llvm::Constant *C, llvm::GenericValue &Result) { |
| 836 | if (C->getValueID() == llvm::Value::UndefValueVal) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 837 | return; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 838 | else if (C->getValueID() == llvm::Value::ConstantExprVal) { |
| 839 | const llvm::ConstantExpr *CE = (llvm::ConstantExpr*) C; |
| 840 | const llvm::Constant *Op0 = CE->getOperand(0); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 841 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 842 | switch (CE->getOpcode()) { |
| 843 | case llvm::Instruction::GetElementPtr: { |
| 844 | // Compute the index |
| 845 | llvm::SmallVector<llvm::Value*, 8> Indices(CE->op_begin() + 1, |
| 846 | CE->op_end()); |
| 847 | uint64_t Offset = mpTD->getIndexedOffset(Op0->getType(), |
| 848 | &Indices[0], |
| 849 | Indices.size()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 850 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 851 | GetConstantValue(Op0, Result); |
| 852 | Result.PointerVal = |
| 853 | static_cast<uint8_t*>(Result.PointerVal) + Offset; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 854 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 855 | return; |
| 856 | } |
| 857 | case llvm::Instruction::Trunc: { |
| 858 | uint32_t BitWidth = |
| 859 | llvm::cast<llvm::IntegerType>(CE->getType())->getBitWidth(); |
| 860 | |
| 861 | GetConstantValue(Op0, Result); |
| 862 | Result.IntVal = Result.IntVal.trunc(BitWidth); |
| 863 | |
| 864 | return; |
| 865 | } |
| 866 | case llvm::Instruction::ZExt: { |
| 867 | uint32_t BitWidth = |
| 868 | llvm::cast<llvm::IntegerType>(CE->getType())->getBitWidth(); |
| 869 | |
| 870 | GetConstantValue(Op0, Result); |
| 871 | Result.IntVal = Result.IntVal.zext(BitWidth); |
| 872 | |
| 873 | return; |
| 874 | } |
| 875 | case llvm::Instruction::SExt: { |
| 876 | uint32_t BitWidth = |
| 877 | llvm::cast<llvm::IntegerType>(CE->getType())->getBitWidth(); |
| 878 | |
| 879 | GetConstantValue(Op0, Result); |
| 880 | Result.IntVal = Result.IntVal.sext(BitWidth); |
| 881 | |
| 882 | return; |
| 883 | } |
| 884 | case llvm::Instruction::FPTrunc: { |
| 885 | // FIXME: long double |
| 886 | GetConstantValue(Op0, Result); |
| 887 | Result.FloatVal = static_cast<float>(Result.DoubleVal); |
| 888 | return; |
| 889 | } |
| 890 | case llvm::Instruction::FPExt: { |
| 891 | // FIXME: long double |
| 892 | GetConstantValue(Op0, Result); |
| 893 | Result.DoubleVal = static_cast<double>(Result.FloatVal); |
| 894 | return; |
| 895 | } |
| 896 | case llvm::Instruction::UIToFP: { |
| 897 | GetConstantValue(Op0, Result); |
| 898 | if (CE->getType()->isFloatTy()) |
| 899 | Result.FloatVal = |
| 900 | static_cast<float>(Result.IntVal.roundToDouble()); |
| 901 | else if (CE->getType()->isDoubleTy()) |
| 902 | Result.DoubleVal = Result.IntVal.roundToDouble(); |
| 903 | else if (CE->getType()->isX86_FP80Ty()) { |
| 904 | const uint64_t zero[] = { 0, 0 }; |
| 905 | llvm::APFloat apf(llvm::APInt(80, 2, zero)); |
| 906 | apf.convertFromAPInt(Result.IntVal, |
| 907 | false, |
| 908 | llvm::APFloat::rmNearestTiesToEven); |
| 909 | Result.IntVal = apf.bitcastToAPInt(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 910 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | case llvm::Instruction::SIToFP: { |
| 914 | GetConstantValue(Op0, Result); |
| 915 | if (CE->getType()->isFloatTy()) |
| 916 | Result.FloatVal = |
| 917 | static_cast<float>(Result.IntVal.signedRoundToDouble()); |
| 918 | else if (CE->getType()->isDoubleTy()) |
| 919 | Result.DoubleVal = Result.IntVal.signedRoundToDouble(); |
| 920 | else if (CE->getType()->isX86_FP80Ty()) { |
| 921 | const uint64_t zero[] = { 0, 0 }; |
| 922 | llvm::APFloat apf = llvm::APFloat(llvm::APInt(80, 2, zero)); |
| 923 | apf.convertFromAPInt(Result.IntVal, |
| 924 | true, |
| 925 | llvm::APFloat::rmNearestTiesToEven); |
| 926 | Result.IntVal = apf.bitcastToAPInt(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 927 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | // double->APInt conversion handles sign |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 931 | case llvm::Instruction::FPToUI: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 932 | case llvm::Instruction::FPToSI: { |
| 933 | uint32_t BitWidth = |
| 934 | llvm::cast<llvm::IntegerType>(CE->getType())->getBitWidth(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 935 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 936 | GetConstantValue(Op0, Result); |
| 937 | if (Op0->getType()->isFloatTy()) |
| 938 | Result.IntVal = |
| 939 | llvm::APIntOps::RoundFloatToAPInt(Result.FloatVal, BitWidth); |
| 940 | else if (Op0->getType()->isDoubleTy()) |
| 941 | Result.IntVal = |
| 942 | llvm::APIntOps::RoundDoubleToAPInt(Result.DoubleVal, |
| 943 | BitWidth); |
| 944 | else if (Op0->getType()->isX86_FP80Ty()) { |
| 945 | llvm::APFloat apf = llvm::APFloat(Result.IntVal); |
| 946 | uint64_t V; |
| 947 | bool Ignored; |
| 948 | apf.convertToInteger(&V, |
| 949 | BitWidth, |
| 950 | CE->getOpcode() == llvm::Instruction::FPToSI, |
| 951 | llvm::APFloat::rmTowardZero, |
| 952 | &Ignored); |
| 953 | Result.IntVal = V; // endian? |
| 954 | } |
| 955 | return; |
| 956 | } |
| 957 | case llvm::Instruction::PtrToInt: { |
| 958 | uint32_t PtrWidth = mpTD->getPointerSizeInBits(); |
| 959 | |
| 960 | GetConstantValue(Op0, Result); |
| 961 | Result.IntVal = llvm::APInt(PtrWidth, uintptr_t |
| 962 | (Result.PointerVal)); |
| 963 | |
| 964 | return; |
| 965 | } |
| 966 | case llvm::Instruction::IntToPtr: { |
| 967 | uint32_t PtrWidth = mpTD->getPointerSizeInBits(); |
| 968 | |
| 969 | GetConstantValue(Op0, Result); |
| 970 | if (PtrWidth != Result.IntVal.getBitWidth()) |
| 971 | Result.IntVal = Result.IntVal.zextOrTrunc(PtrWidth); |
| 972 | assert(Result.IntVal.getBitWidth() <= 64 && "Bad pointer width"); |
| 973 | |
| 974 | Result.PointerVal = |
| 975 | llvm::PointerTy( |
| 976 | static_cast<uintptr_t>(Result.IntVal.getZExtValue())); |
| 977 | |
| 978 | return; |
| 979 | } |
| 980 | case llvm::Instruction::BitCast: { |
| 981 | GetConstantValue(Op0, Result); |
| 982 | const llvm::Type *DestTy = CE->getType(); |
| 983 | |
| 984 | switch (Op0->getType()->getTypeID()) { |
| 985 | case llvm::Type::IntegerTyID: { |
| 986 | assert(DestTy->isFloatingPointTy() && "invalid bitcast"); |
| 987 | if (DestTy->isFloatTy()) |
| 988 | Result.FloatVal = Result.IntVal.bitsToFloat(); |
| 989 | else if (DestTy->isDoubleTy()) |
| 990 | Result.DoubleVal = Result.IntVal.bitsToDouble(); |
| 991 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 992 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 993 | case llvm::Type::FloatTyID: { |
| 994 | assert(DestTy->isIntegerTy(32) && "Invalid bitcast"); |
| 995 | Result.IntVal.floatToBits(Result.FloatVal); |
| 996 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 997 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 998 | case llvm::Type::DoubleTyID: { |
| 999 | assert(DestTy->isIntegerTy(64) && "Invalid bitcast"); |
| 1000 | Result.IntVal.doubleToBits(Result.DoubleVal); |
| 1001 | break; |
| 1002 | } |
| 1003 | case llvm::Type::PointerTyID: { |
| 1004 | assert(DestTy->isPointerTy() && "Invalid bitcast"); |
| 1005 | break; // getConstantValue(Op0) above already converted it |
| 1006 | } |
| 1007 | default: { |
| 1008 | llvm_unreachable("Invalid bitcast operand"); |
| 1009 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1010 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1011 | return; |
| 1012 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1013 | case llvm::Instruction::Add: |
| 1014 | case llvm::Instruction::FAdd: |
| 1015 | case llvm::Instruction::Sub: |
| 1016 | case llvm::Instruction::FSub: |
| 1017 | case llvm::Instruction::Mul: |
| 1018 | case llvm::Instruction::FMul: |
| 1019 | case llvm::Instruction::UDiv: |
| 1020 | case llvm::Instruction::SDiv: |
| 1021 | case llvm::Instruction::URem: |
| 1022 | case llvm::Instruction::SRem: |
| 1023 | case llvm::Instruction::And: |
| 1024 | case llvm::Instruction::Or: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1025 | case llvm::Instruction::Xor: { |
| 1026 | llvm::GenericValue LHS, RHS; |
| 1027 | GetConstantValue(Op0, LHS); |
| 1028 | GetConstantValue(CE->getOperand(1), RHS); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1029 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1030 | switch (Op0->getType()->getTypeID()) { |
| 1031 | case llvm::Type::IntegerTyID: { |
| 1032 | switch (CE->getOpcode()) { |
| 1033 | case llvm::Instruction::Add: { |
| 1034 | Result.IntVal = LHS.IntVal + RHS.IntVal; |
| 1035 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1036 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1037 | case llvm::Instruction::Sub: { |
| 1038 | Result.IntVal = LHS.IntVal - RHS.IntVal; |
| 1039 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1040 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1041 | case llvm::Instruction::Mul: { |
| 1042 | Result.IntVal = LHS.IntVal * RHS.IntVal; |
| 1043 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1044 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1045 | case llvm::Instruction::UDiv: { |
| 1046 | Result.IntVal = LHS.IntVal.udiv(RHS.IntVal); |
| 1047 | break; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1048 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1049 | case llvm::Instruction::SDiv: { |
| 1050 | Result.IntVal = LHS.IntVal.sdiv(RHS.IntVal); |
| 1051 | break; |
| 1052 | } |
| 1053 | case llvm::Instruction::URem: { |
| 1054 | Result.IntVal = LHS.IntVal.urem(RHS.IntVal); |
| 1055 | break; |
| 1056 | } |
| 1057 | case llvm::Instruction::SRem: { |
| 1058 | Result.IntVal = LHS.IntVal.srem(RHS.IntVal); |
| 1059 | break; |
| 1060 | } |
| 1061 | case llvm::Instruction::And: { |
| 1062 | Result.IntVal = LHS.IntVal & RHS.IntVal; |
| 1063 | break; |
| 1064 | } |
| 1065 | case llvm::Instruction::Or: { |
| 1066 | Result.IntVal = LHS.IntVal | RHS.IntVal; |
| 1067 | break; |
| 1068 | } |
| 1069 | case llvm::Instruction::Xor: { |
| 1070 | Result.IntVal = LHS.IntVal ^ RHS.IntVal; |
| 1071 | break; |
| 1072 | } |
| 1073 | default: { |
| 1074 | llvm_unreachable("Invalid integer opcode"); |
| 1075 | } |
| 1076 | } |
| 1077 | break; |
| 1078 | } |
| 1079 | case llvm::Type::FloatTyID: { |
| 1080 | switch (CE->getOpcode()) { |
| 1081 | case llvm::Instruction::FAdd: { |
| 1082 | Result.FloatVal = LHS.FloatVal + RHS.FloatVal; |
| 1083 | break; |
| 1084 | } |
| 1085 | case llvm::Instruction::FSub: { |
| 1086 | Result.FloatVal = LHS.FloatVal - RHS.FloatVal; |
| 1087 | break; |
| 1088 | } |
| 1089 | case llvm::Instruction::FMul: { |
| 1090 | Result.FloatVal = LHS.FloatVal * RHS.FloatVal; |
| 1091 | break; |
| 1092 | } |
| 1093 | case llvm::Instruction::FDiv: { |
| 1094 | Result.FloatVal = LHS.FloatVal / RHS.FloatVal; |
| 1095 | break; |
| 1096 | } |
| 1097 | case llvm::Instruction::FRem: { |
| 1098 | Result.FloatVal = ::fmodf(LHS.FloatVal, RHS.FloatVal); |
| 1099 | break; |
| 1100 | } |
| 1101 | default: { |
| 1102 | llvm_unreachable("Invalid float opcode"); |
| 1103 | } |
| 1104 | } |
| 1105 | break; |
| 1106 | } |
| 1107 | case llvm::Type::DoubleTyID: { |
| 1108 | switch (CE->getOpcode()) { |
| 1109 | case llvm::Instruction::FAdd: { |
| 1110 | Result.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; |
| 1111 | break; |
| 1112 | } |
| 1113 | case llvm::Instruction::FSub: { |
| 1114 | Result.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; |
| 1115 | break; |
| 1116 | } |
| 1117 | case llvm::Instruction::FMul: { |
| 1118 | Result.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; |
| 1119 | break; |
| 1120 | } |
| 1121 | case llvm::Instruction::FDiv: { |
| 1122 | Result.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; |
| 1123 | break; |
| 1124 | } |
| 1125 | case llvm::Instruction::FRem: { |
| 1126 | Result.DoubleVal = ::fmod(LHS.DoubleVal, RHS.DoubleVal); |
| 1127 | break; |
| 1128 | } |
| 1129 | default: { |
| 1130 | llvm_unreachable("Invalid double opcode"); |
| 1131 | } |
| 1132 | } |
| 1133 | break; |
| 1134 | } |
| 1135 | case llvm::Type::X86_FP80TyID: |
| 1136 | case llvm::Type::PPC_FP128TyID: |
| 1137 | case llvm::Type::FP128TyID: { |
| 1138 | llvm::APFloat apfLHS = llvm::APFloat(LHS.IntVal); |
| 1139 | switch (CE->getOpcode()) { |
| 1140 | case llvm::Instruction::FAdd: { |
| 1141 | apfLHS.add(llvm::APFloat(RHS.IntVal), |
| 1142 | llvm::APFloat::rmNearestTiesToEven); |
| 1143 | break; |
| 1144 | } |
| 1145 | case llvm::Instruction::FSub: { |
| 1146 | apfLHS.subtract(llvm::APFloat(RHS.IntVal), |
| 1147 | llvm::APFloat::rmNearestTiesToEven); |
| 1148 | break; |
| 1149 | } |
| 1150 | case llvm::Instruction::FMul: { |
| 1151 | apfLHS.multiply(llvm::APFloat(RHS.IntVal), |
| 1152 | llvm::APFloat::rmNearestTiesToEven); |
| 1153 | break; |
| 1154 | } |
| 1155 | case llvm::Instruction::FDiv: { |
| 1156 | apfLHS.divide(llvm::APFloat(RHS.IntVal), |
| 1157 | llvm::APFloat::rmNearestTiesToEven); |
| 1158 | break; |
| 1159 | } |
| 1160 | case llvm::Instruction::FRem: { |
| 1161 | apfLHS.mod(llvm::APFloat(RHS.IntVal), |
| 1162 | llvm::APFloat::rmNearestTiesToEven); |
| 1163 | break; |
| 1164 | } |
| 1165 | default: { |
| 1166 | llvm_unreachable("Invalid long double opcode"); |
| 1167 | } |
| 1168 | } |
| 1169 | Result.IntVal = apfLHS.bitcastToAPInt(); |
| 1170 | break; |
| 1171 | } |
| 1172 | default: { |
| 1173 | llvm_unreachable("Bad add type!"); |
| 1174 | } |
| 1175 | } // End switch (Op0->getType()->getTypeID()) |
| 1176 | return; |
| 1177 | } |
| 1178 | default: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1179 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1180 | } |
| 1181 | } // End switch (CE->getOpcode()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1182 | |
| 1183 | std::string msg; |
| 1184 | llvm::raw_string_ostream Msg(msg); |
| 1185 | Msg << "ConstantExpr not handled: " << *CE; |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1186 | llvm::report_fatal_error(Msg.str()); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1187 | } // C->getValueID() == llvm::Value::ConstantExprVal |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1188 | |
| 1189 | switch (C->getType()->getTypeID()) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1190 | case llvm::Type::FloatTyID: { |
| 1191 | Result.FloatVal = |
| 1192 | llvm::cast<llvm::ConstantFP>(C)->getValueAPF().convertToFloat(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1193 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1194 | } |
| 1195 | case llvm::Type::DoubleTyID: { |
| 1196 | Result.DoubleVal = |
| 1197 | llvm::cast<llvm::ConstantFP>(C)->getValueAPF().convertToDouble(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1198 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1199 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1200 | case llvm::Type::X86_FP80TyID: |
| 1201 | case llvm::Type::FP128TyID: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1202 | case llvm::Type::PPC_FP128TyID: { |
| 1203 | Result.IntVal = |
| 1204 | llvm::cast<llvm::ConstantFP>(C)->getValueAPF().bitcastToAPInt(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1205 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1206 | } |
| 1207 | case llvm::Type::IntegerTyID: { |
| 1208 | Result.IntVal = |
| 1209 | llvm::cast<llvm::ConstantInt>(C)->getValue(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1210 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1211 | } |
| 1212 | case llvm::Type::PointerTyID: { |
| 1213 | switch (C->getValueID()) { |
| 1214 | case llvm::Value::ConstantPointerNullVal: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1215 | Result.PointerVal = NULL; |
| 1216 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1217 | } |
| 1218 | case llvm::Value::FunctionVal: { |
| 1219 | const llvm::Function *F = static_cast<const llvm::Function*>(C); |
| 1220 | Result.PointerVal = |
| 1221 | GetPointerToFunctionOrStub(const_cast<llvm::Function*>(F)); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1222 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1223 | } |
| 1224 | case llvm::Value::GlobalVariableVal: { |
| 1225 | const llvm::GlobalVariable *GV = |
| 1226 | static_cast<const llvm::GlobalVariable*>(C); |
| 1227 | Result.PointerVal = |
| 1228 | GetOrEmitGlobalVariable(const_cast<llvm::GlobalVariable*>(GV)); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1229 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1230 | } |
| 1231 | case llvm::Value::BlockAddressVal: { |
| 1232 | assert(false && "JIT does not support address-of-label yet!"); |
| 1233 | } |
| 1234 | default: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1235 | llvm_unreachable("Unknown constant pointer type!"); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1236 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1237 | } |
| 1238 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1239 | } |
| 1240 | default: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1241 | std::string msg; |
| 1242 | llvm::raw_string_ostream Msg(msg); |
| 1243 | Msg << "ERROR: Constant unimplemented for type: " << *C->getType(); |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1244 | llvm::report_fatal_error(Msg.str()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1245 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1246 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1247 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1248 | return; |
| 1249 | } |
| 1250 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1251 | // Stores the data in @Val of type @Ty at address @Addr. |
| 1252 | void StoreValueToMemory(const llvm::GenericValue &Val, void *Addr, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1253 | const llvm::Type *Ty) { |
| 1254 | const unsigned int StoreBytes = mpTD->getTypeStoreSize(Ty); |
| 1255 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1256 | switch (Ty->getTypeID()) { |
| 1257 | case llvm::Type::IntegerTyID: { |
| 1258 | const llvm::APInt &IntVal = Val.IntVal; |
| 1259 | assert(((IntVal.getBitWidth() + 7) / 8 >= StoreBytes) && |
| 1260 | "Integer too small!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1261 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1262 | const uint8_t *Src = |
| 1263 | reinterpret_cast<const uint8_t*>(IntVal.getRawData()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1264 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1265 | if (llvm::sys::isLittleEndianHost()) { |
| 1266 | // Little-endian host - the source is ordered from LSB to MSB. |
| 1267 | // Order the destination from LSB to MSB: Do a straight copy. |
| 1268 | memcpy(Addr, Src, StoreBytes); |
| 1269 | } else { |
| 1270 | // Big-endian host - the source is an array of 64 bit words |
| 1271 | // ordered from LSW to MSW. |
| 1272 | // |
| 1273 | // Each word is ordered from MSB to LSB. |
| 1274 | // |
| 1275 | // Order the destination from MSB to LSB: |
| 1276 | // Reverse the word order, but not the bytes in a word. |
| 1277 | unsigned int i = StoreBytes; |
| 1278 | while (i > sizeof(uint64_t)) { |
| 1279 | i -= sizeof(uint64_t); |
| 1280 | ::memcpy(reinterpret_cast<uint8_t*>(Addr) + i, |
| 1281 | Src, |
| 1282 | sizeof(uint64_t)); |
| 1283 | Src += sizeof(uint64_t); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1284 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1285 | ::memcpy(Addr, Src + sizeof(uint64_t) - i, i); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1286 | } |
| 1287 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1288 | } |
| 1289 | case llvm::Type::FloatTyID: { |
| 1290 | *reinterpret_cast<float*>(Addr) = Val.FloatVal; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1291 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1292 | } |
| 1293 | case llvm::Type::DoubleTyID: { |
| 1294 | *reinterpret_cast<double*>(Addr) = Val.DoubleVal; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1295 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1296 | } |
| 1297 | case llvm::Type::X86_FP80TyID: { |
| 1298 | memcpy(Addr, Val.IntVal.getRawData(), 10); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1299 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1300 | } |
| 1301 | case llvm::Type::PointerTyID: { |
| 1302 | // Ensure 64 bit target pointers are fully initialized on 32 bit |
| 1303 | // hosts. |
| 1304 | if (StoreBytes != sizeof(llvm::PointerTy)) |
| 1305 | memset(Addr, 0, StoreBytes); |
| 1306 | *((llvm::PointerTy*) Addr) = Val.PointerVal; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1307 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1308 | } |
| 1309 | default: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1310 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1311 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1314 | if (llvm::sys::isLittleEndianHost() != mpTD->isLittleEndian()) |
| 1315 | std::reverse(reinterpret_cast<uint8_t*>(Addr), |
| 1316 | reinterpret_cast<uint8_t*>(Addr) + StoreBytes); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1317 | |
| 1318 | return; |
| 1319 | } |
| 1320 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1321 | // Recursive function to apply a @Constant value into the specified memory |
| 1322 | // location @Addr. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1323 | void InitializeConstantToMemory(const llvm::Constant *C, void *Addr) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1324 | switch (C->getValueID()) { |
| 1325 | case llvm::Value::UndefValueVal: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1326 | // Nothing to do |
| 1327 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1328 | } |
| 1329 | case llvm::Value::ConstantVectorVal: { |
| 1330 | // dynamic cast may hurt performance |
| 1331 | const llvm::ConstantVector *CP = (llvm::ConstantVector*) C; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1332 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1333 | unsigned int ElementSize = mpTD->getTypeAllocSize |
| 1334 | (CP->getType()->getElementType()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1335 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1336 | for (int i = 0, e = CP->getNumOperands(); i != e;i++) |
| 1337 | InitializeConstantToMemory( |
| 1338 | CP->getOperand(i), |
| 1339 | reinterpret_cast<uint8_t*>(Addr) + i * ElementSize); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1340 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1341 | } |
| 1342 | case llvm::Value::ConstantAggregateZeroVal: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1343 | memset(Addr, 0, (size_t) mpTD->getTypeAllocSize(C->getType())); |
| 1344 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1345 | } |
| 1346 | case llvm::Value::ConstantArrayVal: { |
| 1347 | const llvm::ConstantArray *CPA = (llvm::ConstantArray*) C; |
| 1348 | unsigned int ElementSize = mpTD->getTypeAllocSize |
| 1349 | (CPA->getType()->getElementType()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1350 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1351 | for (int i = 0, e = CPA->getNumOperands(); i != e; i++) |
| 1352 | InitializeConstantToMemory( |
| 1353 | CPA->getOperand(i), |
| 1354 | reinterpret_cast<uint8_t*>(Addr) + i * ElementSize); |
| 1355 | break; |
| 1356 | } |
| 1357 | case llvm::Value::ConstantStructVal: { |
| 1358 | const llvm::ConstantStruct *CPS = |
| 1359 | static_cast<const llvm::ConstantStruct*>(C); |
| 1360 | const llvm::StructLayout *SL = mpTD->getStructLayout |
| 1361 | (llvm::cast<llvm::StructType>(CPS->getType())); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1362 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1363 | for (int i = 0, e = CPS->getNumOperands(); i != e; i++) |
| 1364 | InitializeConstantToMemory( |
| 1365 | CPS->getOperand(i), |
| 1366 | reinterpret_cast<uint8_t*>(Addr) + SL->getElementOffset(i)); |
| 1367 | break; |
| 1368 | } |
| 1369 | default: { |
| 1370 | if (C->getType()->isFirstClassType()) { |
| 1371 | llvm::GenericValue Val; |
| 1372 | GetConstantValue(C, Val); |
| 1373 | StoreValueToMemory(Val, Addr, C->getType()); |
| 1374 | } else { |
| 1375 | llvm_unreachable("Unknown constant type to initialize memory " |
| 1376 | "with!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1377 | } |
| 1378 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1379 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1380 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1381 | return; |
| 1382 | } |
| 1383 | |
| 1384 | void emitConstantPool(llvm::MachineConstantPool *MCP) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1385 | if (mpTJI->hasCustomConstantPool()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1386 | return; |
| 1387 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1388 | // Constant pool address resolution is handled by the target itself in ARM |
| 1389 | // (TargetJITInfo::hasCustomConstantPool() returns true). |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1390 | #if !defined(PROVIDE_ARM_CODEGEN) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1391 | const std::vector<llvm::MachineConstantPoolEntry> &Constants = |
| 1392 | MCP->getConstants(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1393 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1394 | if (Constants.empty()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1395 | return; |
| 1396 | |
| 1397 | unsigned Size = GetConstantPoolSizeInBytes(MCP); |
| 1398 | unsigned Align = MCP->getConstantPoolAlignment(); |
| 1399 | |
| 1400 | mpConstantPoolBase = allocateSpace(Size, Align); |
| 1401 | mpConstantPool = MCP; |
| 1402 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1403 | if (mpConstantPoolBase == NULL) |
| 1404 | return; // out of memory |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1405 | |
| 1406 | unsigned Offset = 0; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1407 | for (int i = 0, e = Constants.size(); i != e; i++) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1408 | llvm::MachineConstantPoolEntry CPE = Constants[i]; |
| 1409 | unsigned AlignMask = CPE.getAlignment() - 1; |
| 1410 | Offset = (Offset + AlignMask) & ~AlignMask; |
| 1411 | |
| 1412 | uintptr_t CAddr = (uintptr_t) mpConstantPoolBase + Offset; |
| 1413 | mConstPoolAddresses.push_back(CAddr); |
| 1414 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1415 | if (CPE.isMachineConstantPoolEntry()) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1416 | llvm::report_fatal_error |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1417 | ("Initialize memory with machine specific constant pool" |
| 1418 | " entry has not been implemented!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1419 | |
| 1420 | InitializeConstantToMemory(CPE.Val.ConstVal, (void*) CAddr); |
| 1421 | |
| 1422 | const llvm::Type *Ty = CPE.Val.ConstVal->getType(); |
| 1423 | Offset += mpTD->getTypeAllocSize(Ty); |
| 1424 | } |
| 1425 | #endif |
| 1426 | return; |
| 1427 | } |
| 1428 | |
| 1429 | void initJumpTableInfo(llvm::MachineJumpTableInfo *MJTI) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1430 | if (mpTJI->hasCustomJumpTables()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1431 | return; |
| 1432 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1433 | const std::vector<llvm::MachineJumpTableEntry> &JT = |
| 1434 | MJTI->getJumpTables(); |
| 1435 | if (JT.empty()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1436 | return; |
| 1437 | |
| 1438 | unsigned NumEntries = 0; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1439 | for (int i = 0, e = JT.size(); i != e; i++) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1440 | NumEntries += JT[i].MBBs.size(); |
| 1441 | |
| 1442 | unsigned EntrySize = MJTI->getEntrySize(*mpTD); |
| 1443 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1444 | mpJumpTable = MJTI; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1445 | mpJumpTableBase = allocateSpace(NumEntries * EntrySize, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1446 | MJTI->getEntryAlignment(*mpTD)); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1447 | |
| 1448 | return; |
| 1449 | } |
| 1450 | |
| 1451 | void emitJumpTableInfo(llvm::MachineJumpTableInfo *MJTI) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1452 | if (mpTJI->hasCustomJumpTables()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1453 | return; |
| 1454 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1455 | const std::vector<llvm::MachineJumpTableEntry> &JT = |
| 1456 | MJTI->getJumpTables(); |
| 1457 | if (JT.empty() || mpJumpTableBase == 0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1458 | return; |
| 1459 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1460 | assert(llvm::TargetMachine::getRelocationModel() == llvm::Reloc::Static && |
| 1461 | (MJTI->getEntrySize(*mpTD) == sizeof(mpTD /* a pointer type */)) && |
| 1462 | "Cross JIT'ing?"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1463 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1464 | // For each jump table, map each target in the jump table to the |
| 1465 | // address of an emitted MachineBasicBlock. |
| 1466 | intptr_t *SlotPtr = reinterpret_cast<intptr_t*>(mpJumpTableBase); |
| 1467 | for (int i = 0, ie = JT.size(); i != ie; i++) { |
| 1468 | const std::vector<llvm::MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 1469 | // Store the address of the basic block for this jump table slot in the |
| 1470 | // memory we allocated for the jump table in 'initJumpTableInfo' |
| 1471 | for (int j = 0, je = MBBs.size(); j != je; j++) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1472 | *SlotPtr++ = getMachineBasicBlockAddress(MBBs[j]); |
| 1473 | } |
| 1474 | } |
| 1475 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1476 | void *GetPointerToGlobal(llvm::GlobalValue *V, void *Reference, |
| 1477 | bool MayNeedFarStub) { |
| 1478 | switch (V->getValueID()) { |
| 1479 | case llvm::Value::FunctionVal: { |
| 1480 | llvm::Function *F = (llvm::Function*) V; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1481 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1482 | // If we have code, go ahead and return that. |
| 1483 | if (void *ResultPtr = GetPointerToGlobalIfAvailable(F)) |
| 1484 | return ResultPtr; |
Shih-wei Liao | 800e9c2 | 2010-04-18 16:08:16 -0700 | [diff] [blame] | 1485 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1486 | if (void *FnStub = GetLazyFunctionStubIfAvailable(F)) |
| 1487 | // Return the function stub if it's already created. |
| 1488 | // We do this first so that: |
| 1489 | // we're returning the same address for the function as any |
| 1490 | // previous call. |
| 1491 | // |
| 1492 | // TODO(llvm.org): Yes, this is wrong. The lazy stub isn't |
| 1493 | // guaranteed to be close enough to call. |
| 1494 | return FnStub; |
Shih-wei Liao | 800e9c2 | 2010-04-18 16:08:16 -0700 | [diff] [blame] | 1495 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1496 | // If we know the target can handle arbitrary-distance calls, try to |
| 1497 | // return a direct pointer. |
| 1498 | if (!MayNeedFarStub) { |
| 1499 | // |
| 1500 | // x86_64 architecture may encounter the bug: |
| 1501 | // http://llvm.org/bugs/show_bug.cgi?id=5201 |
| 1502 | // which generate instruction "call" instead of "callq". |
| 1503 | // |
| 1504 | // And once the real address of stub is greater than 64-bit |
| 1505 | // long, the replacement will truncate to 32-bit resulting a |
| 1506 | // serious problem. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1507 | #if !defined(__x86_64__) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1508 | // If this is an external function pointer, we can force the JIT |
| 1509 | // to 'compile' it, which really just adds it to the map. |
| 1510 | if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) |
| 1511 | return GetPointerToFunction(F, /* AbortOnFailure = */true); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1512 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1513 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1514 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1515 | // Otherwise, we may need a to emit a stub, and, conservatively, we |
| 1516 | // always do so. |
| 1517 | return GetLazyFunctionStub(F); |
| 1518 | break; |
| 1519 | } |
| 1520 | case llvm::Value::GlobalVariableVal: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1521 | return GetOrEmitGlobalVariable((llvm::GlobalVariable*) V); |
| 1522 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1523 | } |
| 1524 | case llvm::Value::GlobalAliasVal: { |
| 1525 | llvm::GlobalAlias *GA = (llvm::GlobalAlias*) V; |
| 1526 | const llvm::GlobalValue *GV = GA->resolveAliasedGlobal(false); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1527 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1528 | switch (GV->getValueID()) { |
| 1529 | case llvm::Value::FunctionVal: { |
| 1530 | // FIXME: is there's any possibility that the function is not |
| 1531 | // code-gen'd? |
| 1532 | return GetPointerToFunction( |
| 1533 | static_cast<const llvm::Function*>(GV), |
| 1534 | /* AbortOnFailure = */true); |
| 1535 | break; |
| 1536 | } |
| 1537 | case llvm::Value::GlobalVariableVal: { |
| 1538 | if (void *P = mGlobalAddressMap[GV]) |
| 1539 | return P; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1540 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1541 | llvm::GlobalVariable *GVar = (llvm::GlobalVariable*) GV; |
| 1542 | EmitGlobalVariable(GVar); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1543 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1544 | return mGlobalAddressMap[GV]; |
| 1545 | break; |
| 1546 | } |
| 1547 | case llvm::Value::GlobalAliasVal: { |
| 1548 | assert(false && "Alias should be resolved ultimately!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1552 | } |
| 1553 | default: { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1554 | break; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1555 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1556 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1557 | llvm_unreachable("Unknown type of global value!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1560 | // If the specified function has been code-gen'd, return a pointer to the |
| 1561 | // function. If not, compile it, or use a stub to implement lazy compilation |
| 1562 | // if available. |
| 1563 | void *GetPointerToFunctionOrStub(llvm::Function *F) { |
| 1564 | // If we have already code generated the function, just return the |
| 1565 | // address. |
| 1566 | if (void *Addr = GetPointerToGlobalIfAvailable(F)) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1567 | return Addr; |
| 1568 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1569 | // Get a stub if the target supports it. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1570 | return GetLazyFunctionStub(F); |
| 1571 | } |
| 1572 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1573 | typedef llvm::DenseMap<const llvm::Function*, |
| 1574 | void*> FunctionToLazyStubMapTy; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1575 | FunctionToLazyStubMapTy mFunctionToLazyStubMap; |
| 1576 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1577 | void *GetLazyFunctionStubIfAvailable(llvm::Function *F) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1578 | return mFunctionToLazyStubMap.lookup(F); |
| 1579 | } |
| 1580 | |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1581 | std::set<const llvm::Function*> PendingFunctions; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1582 | void *GetLazyFunctionStub(llvm::Function *F) { |
| 1583 | // If we already have a lazy stub for this function, recycle it. |
| 1584 | void *&Stub = mFunctionToLazyStubMap[F]; |
| 1585 | if (Stub) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1586 | return Stub; |
| 1587 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1588 | // In any cases, we should NOT resolve function at runtime (though we are |
| 1589 | // able to). We resolve this right now. |
| 1590 | void *Actual = NULL; |
| 1591 | if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) |
| 1592 | Actual = GetPointerToFunction(F, /* AbortOnFailure = */true); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1593 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1594 | // Codegen a new stub, calling the actual address of the external |
| 1595 | // function, if it was resolved. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1596 | llvm::TargetJITInfo::StubLayout SL = mpTJI->getStubLayout(); |
| 1597 | startGVStub(F, SL.Size, SL.Alignment); |
| 1598 | Stub = mpTJI->emitFunctionStub(F, Actual, *this); |
| 1599 | finishGVStub(); |
| 1600 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1601 | // We really want the address of the stub in the GlobalAddressMap for the |
| 1602 | // JIT, not the address of the external function. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1603 | UpdateGlobalMapping(F, Stub); |
| 1604 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1605 | if (!Actual) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1606 | PendingFunctions.insert(F); |
| 1607 | else |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1608 | Disassemble(F->getName(), reinterpret_cast<uint8_t*>(Stub), |
| 1609 | SL.Size, true); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1610 | |
| 1611 | return Stub; |
| 1612 | } |
| 1613 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1614 | void *GetPointerToFunction(const llvm::Function *F, bool AbortOnFailure) { |
| 1615 | void *Addr = GetPointerToGlobalIfAvailable(F); |
| 1616 | if (Addr) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1617 | return Addr; |
| 1618 | |
| 1619 | assert((F->isDeclaration() || F->hasAvailableExternallyLinkage()) && |
| 1620 | "Internal error: only external defined function routes here!"); |
| 1621 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1622 | // Handle the failure resolution by ourselves. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1623 | Addr = GetPointerToNamedSymbol(F->getName().str().c_str(), |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1624 | /* AbortOnFailure = */ false); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1625 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1626 | // If we resolved the symbol to a null address (eg. a weak external) |
| 1627 | // return a null pointer let the application handle it. |
| 1628 | if (Addr == NULL) { |
| 1629 | if (AbortOnFailure) |
| 1630 | llvm::report_fatal_error("Could not resolve external function " |
| 1631 | "address: " + F->getName()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1632 | else |
| 1633 | return NULL; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1634 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1635 | |
| 1636 | AddGlobalMapping(F, Addr); |
| 1637 | |
| 1638 | return Addr; |
| 1639 | } |
| 1640 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1641 | void *GetPointerToNamedSymbol(const std::string &Name, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1642 | bool AbortOnFailure) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1643 | if (void *Addr = FindRuntimeFunction(Name.c_str())) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1644 | return Addr; |
| 1645 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1646 | if (mpSymbolLookupFn) |
| 1647 | if (void *Addr = mpSymbolLookupFn(mpSymbolLookupContext, Name.c_str())) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1648 | return Addr; |
| 1649 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1650 | if (AbortOnFailure) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1651 | llvm::report_fatal_error("Program used external symbol '" + Name + |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1652 | "' which could not be resolved!"); |
| 1653 | |
| 1654 | return NULL; |
| 1655 | } |
| 1656 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1657 | // Return the address of the specified global variable, possibly emitting it |
| 1658 | // to memory if needed. This is used by the Emitter. |
| 1659 | void *GetOrEmitGlobalVariable(const llvm::GlobalVariable *GV) { |
| 1660 | void *Ptr = GetPointerToGlobalIfAvailable(GV); |
| 1661 | if (Ptr) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1662 | return Ptr; |
| 1663 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1664 | if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) { |
| 1665 | // If the global is external, just remember the address. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1666 | Ptr = GetPointerToNamedSymbol(GV->getName().str(), true); |
| 1667 | AddGlobalMapping(GV, Ptr); |
| 1668 | } else { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1669 | // If the global hasn't been emitted to memory yet, allocate space and |
| 1670 | // emit it into memory. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1671 | Ptr = GetMemoryForGV(GV); |
| 1672 | AddGlobalMapping(GV, Ptr); |
| 1673 | EmitGlobalVariable(GV); |
| 1674 | } |
| 1675 | |
| 1676 | return Ptr; |
| 1677 | } |
| 1678 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1679 | // This method abstracts memory allocation of global variable so that the |
| 1680 | // JIT can allocate thread local variables depending on the target. |
| 1681 | void *GetMemoryForGV(const llvm::GlobalVariable *GV) { |
| 1682 | void *Ptr; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1683 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1684 | const llvm::Type *GlobalType = GV->getType()->getElementType(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1685 | size_t S = mpTD->getTypeAllocSize(GlobalType); |
| 1686 | size_t A = mpTD->getPreferredAlignment(GV); |
| 1687 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1688 | if (GV->isThreadLocal()) { |
| 1689 | // We can support TLS by |
| 1690 | // |
| 1691 | // Ptr = TJI.allocateThreadLocalMemory(S); |
| 1692 | // |
| 1693 | // But I tend not to. |
| 1694 | // (should we disable this in the front-end (i.e., slang)?). |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1695 | llvm::report_fatal_error |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1696 | ("Compilation of Thread Local Storage (TLS) is disabled!"); |
| 1697 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1698 | } else if (mpTJI->allocateSeparateGVMemory()) { |
| 1699 | if (A <= 8) { |
| 1700 | Ptr = malloc(S); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1701 | } else { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1702 | // Allocate (S + A) bytes of memory, then use an aligned pointer |
| 1703 | // within that space. |
| 1704 | Ptr = malloc(S + A); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1705 | unsigned int MisAligned = ((intptr_t) Ptr & (A - 1)); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1706 | Ptr = reinterpret_cast<uint8_t*>(Ptr) + |
| 1707 | (MisAligned ? (A - MisAligned) : 0); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1708 | } |
| 1709 | } else { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1710 | Ptr = allocateGlobal(S, A); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | return Ptr; |
| 1714 | } |
| 1715 | |
| 1716 | void EmitGlobalVariable(const llvm::GlobalVariable *GV) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1717 | void *GA = GetPointerToGlobalIfAvailable(GV); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1718 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1719 | if (GV->isThreadLocal()) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 1720 | llvm::report_fatal_error |
| 1721 | ("We don't support Thread Local Storage (TLS)!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1722 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1723 | if (GA == NULL) { |
| 1724 | // If it's not already specified, allocate memory for the global. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1725 | GA = GetMemoryForGV(GV); |
| 1726 | AddGlobalMapping(GV, GA); |
| 1727 | } |
| 1728 | |
| 1729 | InitializeConstantToMemory(GV->getInitializer(), GA); |
| 1730 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1731 | // You can do some statistics on global variable here. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1732 | return; |
| 1733 | } |
| 1734 | |
| 1735 | typedef std::map<llvm::AssertingVH<llvm::GlobalValue>, void* |
| 1736 | > GlobalToIndirectSymMapTy; |
| 1737 | GlobalToIndirectSymMapTy GlobalToIndirectSymMap; |
| 1738 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1739 | void *GetPointerToGVIndirectSym(llvm::GlobalValue *V, void *Reference) { |
| 1740 | // Make sure GV is emitted first, and create a stub containing the fully |
| 1741 | // resolved address. |
| 1742 | void *GVAddress = GetPointerToGlobal(V, Reference, false); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1743 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1744 | // If we already have a stub for this global variable, recycle it. |
| 1745 | void *&IndirectSym = GlobalToIndirectSymMap[V]; |
| 1746 | // Otherwise, codegen a new indirect symbol. |
| 1747 | if (!IndirectSym) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1748 | IndirectSym = mpTJI->emitGlobalValueIndirectSym(V, GVAddress, *this); |
| 1749 | |
| 1750 | return IndirectSym; |
| 1751 | } |
| 1752 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1753 | // This is the equivalent of FunctionToLazyStubMap for external functions. |
| 1754 | // |
| 1755 | // TODO(llvm.org): Of course, external functions don't need a lazy stub. |
| 1756 | // It's actually here to make it more likely that far calls |
| 1757 | // succeed, but no single stub can guarantee that. I'll |
| 1758 | // remove this in a subsequent checkin when I actually fix |
| 1759 | // far calls. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1760 | std::map<void*, void*> ExternalFnToStubMap; |
| 1761 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1762 | // Return a stub for the function at the specified address. |
| 1763 | void *GetExternalFunctionStub(void *FnAddr) { |
| 1764 | void *&Stub = ExternalFnToStubMap[FnAddr]; |
| 1765 | if (Stub) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1766 | return Stub; |
| 1767 | |
| 1768 | llvm::TargetJITInfo::StubLayout SL = mpTJI->getStubLayout(); |
| 1769 | startGVStub(0, SL.Size, SL.Alignment); |
| 1770 | Stub = mpTJI->emitFunctionStub(0, FnAddr, *this); |
| 1771 | finishGVStub(); |
| 1772 | |
| 1773 | return Stub; |
| 1774 | } |
| 1775 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1776 | #if defined(USE_DISASSEMBLER) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1777 | const llvm::MCAsmInfo *mpAsmInfo; |
| 1778 | const llvm::MCDisassembler *mpDisassmbler; |
| 1779 | llvm::MCInstPrinter *mpIP; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1780 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1781 | class BufferMemoryObject : public llvm::MemoryObject { |
| 1782 | private: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1783 | const uint8_t *mBytes; |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1784 | uint64_t mLength; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1785 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1786 | public: |
| 1787 | BufferMemoryObject(const uint8_t *Bytes, uint64_t Length) : |
| 1788 | mBytes(Bytes), mLength(Length) { } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1789 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1790 | uint64_t getBase() const { return 0; } |
| 1791 | uint64_t getExtent() const { return mLength; } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1792 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1793 | int readByte(uint64_t Addr, uint8_t *Byte) const { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1794 | if (Addr > getExtent()) |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1795 | return -1; |
| 1796 | *Byte = mBytes[Addr]; |
| 1797 | return 0; |
| 1798 | } |
| 1799 | }; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1800 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1801 | void Disassemble(const llvm::StringRef &Name, uint8_t *Start, |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1802 | size_t Length, bool IsStub) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1803 | llvm::raw_fd_ostream *OS; |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 1804 | #if defined(USE_DISASSEMBLER_FILE) |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1805 | std::string ErrorInfo; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1806 | OS = new llvm::raw_fd_ostream("/data/local/tmp/out.S", |
| 1807 | ErrorInfo, |
| 1808 | llvm::raw_fd_ostream::F_Append); |
| 1809 | if (!ErrorInfo.empty()) { // some errors occurred |
| 1810 | // LOGE("Error in creating disassembly file"); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1811 | delete OS; |
| 1812 | return; |
| 1813 | } |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 1814 | #else |
| 1815 | OS = &llvm::outs(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1816 | #endif |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1817 | *OS << "JIT: Disassembled code: " << Name << ((IsStub) ? " (stub)" : "") |
| 1818 | << "\n"; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1819 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1820 | if (mpAsmInfo == NULL) |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1821 | mpAsmInfo = mpTarget->createAsmInfo(Triple); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1822 | if (mpDisassmbler == NULL) |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1823 | mpDisassmbler = mpTarget->createMCDisassembler(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1824 | if (mpIP == NULL) |
| 1825 | mpIP = mpTarget->createMCInstPrinter(mpAsmInfo->getAssemblerDialect(), |
| 1826 | *mpAsmInfo); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1827 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1828 | const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Start, |
| 1829 | Length); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1830 | uint64_t Size; |
| 1831 | uint64_t Index; |
| 1832 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1833 | for (Index = 0; Index < Length; Index += Size) { |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1834 | llvm::MCInst Inst; |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 1835 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1836 | if (mpDisassmbler->getInstruction(Inst, Size, *BufferMObj, Index, |
| 1837 | /* REMOVED */ llvm::nulls())) { |
| 1838 | (*OS).indent(4) |
| 1839 | .write("0x", 2) |
| 1840 | .write_hex((uint32_t) Start + Index) |
| 1841 | .write(':'); |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1842 | mpIP->printInst(&Inst, *OS); |
| 1843 | *OS << "\n"; |
| 1844 | } else { |
| 1845 | if (Size == 0) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1846 | Size = 1; // skip illegible bytes |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 1847 | } |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1850 | *OS << "\n"; |
| 1851 | delete BufferMObj; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1852 | |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 1853 | #if defined(USE_DISASSEMBLER_FILE) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1854 | // If you want the disassemble results write to file, uncomment this. |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1855 | OS->close(); |
| 1856 | delete OS; |
| 1857 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1858 | return; |
| 1859 | } |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1860 | #else |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1861 | inline void Disassemble(const std::string &Name, uint8_t *Start, |
| 1862 | size_t Length, bool IsStub) { |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1863 | return; |
| 1864 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1865 | #endif // defined(USE_DISASSEMBLER) |
| 1866 | |
| 1867 | // Resolver to undefined symbol in CodeEmitter |
| 1868 | BCCSymbolLookupFn mpSymbolLookupFn; |
| 1869 | void *mpSymbolLookupContext; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1870 | |
| 1871 | public: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1872 | // Will take the ownership of @MemMgr |
| 1873 | explicit CodeEmitter(CodeMemoryManager *pMemMgr) |
| 1874 | : mpMemMgr(pMemMgr), |
| 1875 | mpTarget(NULL), |
| 1876 | mpTJI(NULL), |
| 1877 | mpTD(NULL), |
| 1878 | mpCurEmitFunction(NULL), |
| 1879 | mpConstantPool(NULL), |
| 1880 | mpJumpTable(NULL), |
| 1881 | mpMMI(NULL), |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1882 | #if defined(USE_DISASSEMBLER) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1883 | mpAsmInfo(NULL), |
| 1884 | mpDisassmbler(NULL), |
| 1885 | mpIP(NULL), |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1886 | #endif |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1887 | mpSymbolLookupFn(NULL), |
| 1888 | mpSymbolLookupContext(NULL) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1889 | return; |
| 1890 | } |
| 1891 | |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 1892 | inline global_addresses_const_iterator global_address_begin() const { |
| 1893 | return mGlobalAddressMap.begin(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1894 | } |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 1895 | inline global_addresses_const_iterator global_address_end() const { |
| 1896 | return mGlobalAddressMap.end(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1897 | } |
| 1898 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1899 | void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1900 | mpSymbolLookupFn = pFn; |
| 1901 | mpSymbolLookupContext = pContext; |
| 1902 | return; |
| 1903 | } |
| 1904 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1905 | void setTargetMachine(llvm::TargetMachine &TM) { |
| 1906 | // Set Target |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 1907 | mpTarget = &TM.getTarget(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1908 | // Set TargetJITInfo |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1909 | mpTJI = TM.getJITInfo(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1910 | // set TargetData |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1911 | mpTD = TM.getTargetData(); |
| 1912 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1913 | assert(!mpTJI->needsGOT() && "We don't support GOT needed target!"); |
| 1914 | |
| 1915 | return; |
| 1916 | } |
| 1917 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1918 | // This callback is invoked when the specified function is about to be code |
| 1919 | // generated. This initializes the BufferBegin/End/Ptr fields. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1920 | void startFunction(llvm::MachineFunction &F) { |
| 1921 | uintptr_t ActualSize = 0; |
| 1922 | |
| 1923 | mpMemMgr->setMemoryWritable(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1924 | |
| 1925 | // BufferBegin, BufferEnd and CurBufferPtr are all inherited from class |
| 1926 | // MachineCodeEmitter, which is the super class of the class |
| 1927 | // JITCodeEmitter. |
| 1928 | // |
| 1929 | // BufferBegin/BufferEnd - Pointers to the start and end of the memory |
| 1930 | // allocated for this code buffer. |
| 1931 | // |
| 1932 | // CurBufferPtr - Pointer to the next byte of memory to fill when emitting |
| 1933 | // code. This is guranteed to be in the range |
| 1934 | // [BufferBegin, BufferEnd]. If this pointer is at |
| 1935 | // BufferEnd, it will never move due to code emission, and |
| 1936 | // all code emission requests will be ignored (this is the |
| 1937 | // buffer overflow condition). |
| 1938 | BufferBegin = CurBufferPtr = |
| 1939 | mpMemMgr->startFunctionBody(F.getFunction(), ActualSize); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1940 | BufferEnd = BufferBegin + ActualSize; |
| 1941 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1942 | if (mpCurEmitFunction == NULL) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1943 | mpCurEmitFunction = new EmittedFunctionCode(); |
| 1944 | mpCurEmitFunction->FunctionBody = BufferBegin; |
| 1945 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1946 | // 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] | 1947 | emitAlignment(16); |
| 1948 | |
| 1949 | emitConstantPool(F.getConstantPool()); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1950 | if (llvm::MachineJumpTableInfo *MJTI = F.getJumpTableInfo()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1951 | initJumpTableInfo(MJTI); |
| 1952 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1953 | // About to start emitting the machine code for the function. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1954 | emitAlignment(std::max(F.getFunction()->getAlignment(), 8U)); |
| 1955 | |
| 1956 | UpdateGlobalMapping(F.getFunction(), CurBufferPtr); |
| 1957 | |
| 1958 | mpCurEmitFunction->Code = CurBufferPtr; |
| 1959 | |
| 1960 | mMBBLocations.clear(); |
| 1961 | |
| 1962 | return; |
| 1963 | } |
| 1964 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1965 | // This callback is invoked when the specified function has finished code |
| 1966 | // generation. If a buffer overflow has occurred, this method returns true |
| 1967 | // (the callee is required to try again). |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1968 | bool finishFunction(llvm::MachineFunction &F) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1969 | if (CurBufferPtr == BufferEnd) { |
| 1970 | // No enough memory |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1971 | mpMemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr); |
| 1972 | return false; |
| 1973 | } |
| 1974 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1975 | if (llvm::MachineJumpTableInfo *MJTI = F.getJumpTableInfo()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1976 | emitJumpTableInfo(MJTI); |
| 1977 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1978 | // FnStart is the start of the text, not the start of the constant pool |
| 1979 | // and other per-function data. |
| 1980 | uint8_t *FnStart = |
| 1981 | reinterpret_cast<uint8_t*>( |
| 1982 | GetPointerToGlobalIfAvailable(F.getFunction())); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1983 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1984 | // FnEnd is the end of the function's machine code. |
| 1985 | uint8_t *FnEnd = CurBufferPtr; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1986 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1987 | if (!mRelocations.empty()) { |
| 1988 | // Resolve the relocations to concrete pointers. |
| 1989 | for (int i = 0, e = mRelocations.size(); i != e; i++) { |
| 1990 | llvm::MachineRelocation &MR = mRelocations[i]; |
| 1991 | void *ResultPtr = NULL; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1992 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1993 | if (!MR.letTargetResolve()) { |
| 1994 | if (MR.isExternalSymbol()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1995 | ResultPtr = GetPointerToNamedSymbol(MR.getExternalSymbol(), true); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1996 | if (MR.mayNeedFarStub()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1997 | ResultPtr = GetExternalFunctionStub(ResultPtr); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1998 | } else if (MR.isGlobalValue()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 1999 | ResultPtr = GetPointerToGlobal(MR.getGlobalValue(), |
| 2000 | BufferBegin |
| 2001 | + MR.getMachineCodeOffset(), |
| 2002 | MR.mayNeedFarStub()); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2003 | } else if (MR.isIndirectSymbol()) { |
| 2004 | ResultPtr = |
| 2005 | GetPointerToGVIndirectSym( |
| 2006 | MR.getGlobalValue(), |
| 2007 | BufferBegin + MR.getMachineCodeOffset()); |
| 2008 | } else if (MR.isBasicBlock()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2009 | ResultPtr = |
| 2010 | (void*) getMachineBasicBlockAddress(MR.getBasicBlock()); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2011 | } else if (MR.isConstantPoolIndex()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2012 | ResultPtr = |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2013 | (void*) getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2014 | } else { |
| 2015 | assert(MR.isJumpTableIndex() && "Unknown type of relocation"); |
| 2016 | ResultPtr = |
| 2017 | (void*) getJumpTableEntryAddress(MR.getJumpTableIndex()); |
| 2018 | } |
| 2019 | |
| 2020 | MR.setResultPointer(ResultPtr); |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | mpTJI->relocate(BufferBegin, &mRelocations[0], mRelocations.size(), |
| 2025 | mpMemMgr->getGOTBase()); |
| 2026 | } |
| 2027 | |
| 2028 | mpMemMgr->endFunctionBody(F.getFunction(), BufferBegin, CurBufferPtr); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2029 | // CurBufferPtr may have moved beyond FnEnd, due to memory allocation for |
| 2030 | // global variables that were referenced in the relocations. |
| 2031 | if (CurBufferPtr == BufferEnd) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2032 | return false; |
| 2033 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2034 | // Now that we've succeeded in emitting the function. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2035 | mpCurEmitFunction->Size = CurBufferPtr - BufferBegin; |
| 2036 | BufferBegin = CurBufferPtr = 0; |
| 2037 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2038 | if (F.getFunction()->hasName()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2039 | mEmittedFunctions[F.getFunction()->getNameStr()] = mpCurEmitFunction; |
| 2040 | mpCurEmitFunction = NULL; |
| 2041 | |
| 2042 | mRelocations.clear(); |
| 2043 | mConstPoolAddresses.clear(); |
| 2044 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2045 | if (mpMMI) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2046 | mpMMI->EndFunction(); |
| 2047 | |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2048 | updateFunctionStub(F.getFunction()); |
| 2049 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2050 | // 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] | 2051 | mpMemMgr->setMemoryExecutable(); |
| 2052 | |
| 2053 | Disassemble(F.getFunction()->getName(), FnStart, FnEnd - FnStart, false); |
| 2054 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2055 | return false; |
| 2056 | } |
| 2057 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2058 | void startGVStub(const llvm::GlobalValue *GV, unsigned StubSize, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2059 | unsigned Alignment) { |
| 2060 | mpSavedBufferBegin = BufferBegin; |
| 2061 | mpSavedBufferEnd = BufferEnd; |
| 2062 | mpSavedCurBufferPtr = CurBufferPtr; |
| 2063 | |
| 2064 | BufferBegin = CurBufferPtr = mpMemMgr->allocateStub(GV, StubSize, |
| 2065 | Alignment); |
| 2066 | BufferEnd = BufferBegin + StubSize + 1; |
| 2067 | |
| 2068 | return; |
| 2069 | } |
| 2070 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2071 | void startGVStub(void *Buffer, unsigned StubSize) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2072 | mpSavedBufferBegin = BufferBegin; |
| 2073 | mpSavedBufferEnd = BufferEnd; |
| 2074 | mpSavedCurBufferPtr = CurBufferPtr; |
| 2075 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2076 | BufferBegin = CurBufferPtr = reinterpret_cast<uint8_t *>(Buffer); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2077 | BufferEnd = BufferBegin + StubSize + 1; |
| 2078 | |
| 2079 | return; |
| 2080 | } |
| 2081 | |
| 2082 | void finishGVStub() { |
| 2083 | assert(CurBufferPtr != BufferEnd && "Stub overflowed allocated space."); |
| 2084 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2085 | // restore |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2086 | BufferBegin = mpSavedBufferBegin; |
| 2087 | BufferEnd = mpSavedBufferEnd; |
| 2088 | CurBufferPtr = mpSavedCurBufferPtr; |
| 2089 | |
| 2090 | return; |
| 2091 | } |
| 2092 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2093 | // Allocates and fills storage for an indirect GlobalValue, and returns the |
| 2094 | // address. |
| 2095 | void *allocIndirectGV(const llvm::GlobalValue *GV, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2096 | const uint8_t *Buffer, size_t Size, |
| 2097 | unsigned Alignment) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2098 | uint8_t *IndGV = mpMemMgr->allocateStub(GV, Size, Alignment); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2099 | memcpy(IndGV, Buffer, Size); |
| 2100 | return IndGV; |
| 2101 | } |
| 2102 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2103 | // Emits a label |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2104 | void emitLabel(llvm::MCSymbol *Label) { |
| 2105 | mLabelLocations[Label] = getCurrentPCValue(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2106 | return; |
| 2107 | } |
| 2108 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2109 | // Allocate memory for a global. Unlike allocateSpace, this method does not |
| 2110 | // allocate memory in the current output buffer, because a global may live |
| 2111 | // longer than the current function. |
| 2112 | void *allocateGlobal(uintptr_t Size, unsigned Alignment) { |
| 2113 | // Delegate this call through the memory manager. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2114 | return mpMemMgr->allocateGlobal(Size, Alignment); |
| 2115 | } |
| 2116 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2117 | // This should be called by the target when a new basic block is about to be |
| 2118 | // emitted. This way the MCE knows where the start of the block is, and can |
| 2119 | // implement getMachineBasicBlockAddress. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2120 | void StartMachineBasicBlock(llvm::MachineBasicBlock *MBB) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2121 | if (mMBBLocations.size() <= (unsigned) MBB->getNumber()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2122 | mMBBLocations.resize((MBB->getNumber() + 1) * 2); |
| 2123 | mMBBLocations[MBB->getNumber()] = getCurrentPCValue(); |
| 2124 | return; |
| 2125 | } |
| 2126 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2127 | // Whenever a relocatable address is needed, it should be noted with this |
| 2128 | // interface. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2129 | void addRelocation(const llvm::MachineRelocation &MR) { |
| 2130 | mRelocations.push_back(MR); |
| 2131 | return; |
| 2132 | } |
| 2133 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2134 | // Return the address of the @Index entry in the constant pool that was |
| 2135 | // last emitted with the emitConstantPool method. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2136 | uintptr_t getConstantPoolEntryAddress(unsigned Index) const { |
| 2137 | assert(Index < mpConstantPool->getConstants().size() && |
| 2138 | "Invalid constant pool index!"); |
| 2139 | return mConstPoolAddresses[Index]; |
| 2140 | } |
| 2141 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2142 | // Return the address of the jump table with index @Index in the function |
| 2143 | // that last called initJumpTableInfo. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2144 | uintptr_t getJumpTableEntryAddress(unsigned Index) const { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2145 | const std::vector<llvm::MachineJumpTableEntry> &JT = |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2146 | mpJumpTable->getJumpTables(); |
| 2147 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2148 | assert((Index < JT.size()) && "Invalid jump table index!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2149 | |
| 2150 | unsigned int Offset = 0; |
| 2151 | unsigned int EntrySize = mpJumpTable->getEntrySize(*mpTD); |
| 2152 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2153 | for (unsigned i = 0; i < Index; i++) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2154 | Offset += JT[i].MBBs.size(); |
| 2155 | Offset *= EntrySize; |
| 2156 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2157 | return (uintptr_t)(reinterpret_cast<uint8_t*>(mpJumpTableBase) + Offset); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2160 | // Return the address of the specified MachineBasicBlock, only usable after |
| 2161 | // the label for the MBB has been emitted. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2162 | uintptr_t getMachineBasicBlockAddress(llvm::MachineBasicBlock *MBB) const { |
| 2163 | assert(mMBBLocations.size() > (unsigned) MBB->getNumber() && |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2164 | mMBBLocations[MBB->getNumber()] && |
| 2165 | "MBB not emitted!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2166 | return mMBBLocations[MBB->getNumber()]; |
| 2167 | } |
| 2168 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2169 | // Return the address of the specified LabelID, only usable after the |
| 2170 | // LabelID has been emitted. |
| 2171 | uintptr_t getLabelAddress(llvm::MCSymbol *Label) const { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2172 | assert(mLabelLocations.count(Label) && "Label not emitted!"); |
| 2173 | return mLabelLocations.find(Label)->second; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2176 | // Specifies the MachineModuleInfo object. This is used for exception |
| 2177 | // handling purposes. |
| 2178 | void setModuleInfo(llvm::MachineModuleInfo *Info) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2179 | mpMMI = Info; |
| 2180 | return; |
| 2181 | } |
| 2182 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2183 | void updateFunctionStub(const llvm::Function *F) { |
| 2184 | // Get the empty stub we generated earlier. |
| 2185 | void *Stub; |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2186 | std::set<const llvm::Function*>::iterator I = PendingFunctions.find(F); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2187 | if (I != PendingFunctions.end()) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2188 | Stub = mFunctionToLazyStubMap[F]; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2189 | else |
| 2190 | return; |
| 2191 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2192 | void *Addr = GetPointerToGlobalIfAvailable(F); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2193 | |
| 2194 | assert(Addr != Stub && |
| 2195 | "Function must have non-stub address to be updated."); |
| 2196 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2197 | // Tell the target jit info to rewrite the stub at the specified address, |
| 2198 | // rather than creating a new one. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2199 | llvm::TargetJITInfo::StubLayout SL = mpTJI->getStubLayout(); |
| 2200 | startGVStub(Stub, SL.Size); |
| 2201 | mpTJI->emitFunctionStub(F, Addr, *this); |
| 2202 | finishGVStub(); |
| 2203 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2204 | Disassemble(F->getName(), reinterpret_cast<uint8_t*>(Stub), |
| 2205 | SL.Size, true); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2206 | |
| 2207 | PendingFunctions.erase(I); |
| 2208 | |
| 2209 | return; |
| 2210 | } |
| 2211 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2212 | // Once you finish the compilation on a translation unit, you can call this |
| 2213 | // function to recycle the memory (which is used at compilation time and not |
| 2214 | // needed for runtime). |
| 2215 | // |
| 2216 | // NOTE: You should not call this funtion until the code-gen passes for a |
| 2217 | // given module is done. Otherwise, the results is undefined and may |
| 2218 | // cause the system crash! |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2219 | void releaseUnnecessary() { |
| 2220 | mMBBLocations.clear(); |
| 2221 | mLabelLocations.clear(); |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2222 | mGlobalAddressMap.clear(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2223 | mFunctionToLazyStubMap.clear(); |
| 2224 | GlobalToIndirectSymMap.clear(); |
| 2225 | ExternalFnToStubMap.clear(); |
| 2226 | PendingFunctions.clear(); |
| 2227 | |
| 2228 | return; |
| 2229 | } |
| 2230 | |
| 2231 | void reset() { |
| 2232 | releaseUnnecessary(); |
| 2233 | |
| 2234 | mpSymbolLookupFn = NULL; |
| 2235 | mpSymbolLookupContext = NULL; |
| 2236 | |
| 2237 | mpTJI = NULL; |
| 2238 | mpTD = NULL; |
| 2239 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2240 | for (EmittedFunctionsMapTy::iterator I = mEmittedFunctions.begin(), |
| 2241 | E = mEmittedFunctions.end(); |
| 2242 | I != E; |
| 2243 | I++) |
| 2244 | if (I->second != NULL) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2245 | delete I->second; |
| 2246 | mEmittedFunctions.clear(); |
| 2247 | |
| 2248 | mpMemMgr->reset(); |
| 2249 | |
| 2250 | return; |
| 2251 | } |
| 2252 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2253 | void *lookup(const char *Name) { |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2254 | return lookup( llvm::StringRef(Name) ); |
| 2255 | } |
| 2256 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2257 | void *lookup(const llvm::StringRef &Name) { |
| 2258 | EmittedFunctionsMapTy::const_iterator I = |
| 2259 | mEmittedFunctions.find(Name.str()); |
| 2260 | if (I == mEmittedFunctions.end()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2261 | return NULL; |
| 2262 | else |
| 2263 | return I->second->Code; |
| 2264 | } |
| 2265 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2266 | void getFunctionNames(BCCsizei *actualFunctionCount, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2267 | BCCsizei maxFunctionCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2268 | BCCchar **functions) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2269 | int functionCount = mEmittedFunctions.size(); |
| 2270 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2271 | if (actualFunctionCount) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2272 | *actualFunctionCount = functionCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2273 | if (functionCount > maxFunctionCount) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2274 | functionCount = maxFunctionCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2275 | if (functions) |
| 2276 | for (EmittedFunctionsMapTy::const_iterator |
| 2277 | I = mEmittedFunctions.begin(), E = mEmittedFunctions.end(); |
| 2278 | (I != E) && (functionCount > 0); |
| 2279 | I++, functionCount--) |
| 2280 | *functions++ = const_cast<BCCchar*>(I->first.c_str()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2281 | |
| 2282 | return; |
| 2283 | } |
| 2284 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2285 | void getFunctionBinary(BCCchar *label, |
| 2286 | BCCvoid **base, |
| 2287 | BCCsizei *length) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2288 | EmittedFunctionsMapTy::const_iterator I = mEmittedFunctions.find(label); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2289 | if (I == mEmittedFunctions.end()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2290 | *base = NULL; |
| 2291 | *length = 0; |
| 2292 | } else { |
| 2293 | *base = I->second->Code; |
| 2294 | *length = I->second->Size; |
| 2295 | } |
| 2296 | return; |
| 2297 | } |
| 2298 | |
| 2299 | ~CodeEmitter() { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2300 | delete mpMemMgr; |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2301 | #if defined(USE_DISASSEMBLER) |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2302 | delete mpAsmInfo; |
| 2303 | delete mpDisassmbler; |
| 2304 | delete mpIP; |
Shih-wei Liao | cd61af3 | 2010-04-29 00:02:57 -0700 | [diff] [blame] | 2305 | #endif |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2306 | return; |
| 2307 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2308 | }; |
| 2309 | // End of Class CodeEmitter |
| 2310 | ////////////////////////////////////////////////////////////////////////////// |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2311 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2312 | // The CodeEmitter |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2313 | llvm::OwningPtr<CodeEmitter> mCodeEmitter; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2314 | CodeEmitter *createCodeEmitter() { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2315 | mCodeEmitter.reset(new CodeEmitter(mCodeMemMgr.take())); |
| 2316 | return mCodeEmitter.get(); |
| 2317 | } |
| 2318 | |
| 2319 | BCCSymbolLookupFn mpSymbolLookupFn; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2320 | void *mpSymbolLookupContext; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2321 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2322 | llvm::LLVMContext *mContext; |
| 2323 | llvm::Module *mModule; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2324 | |
| 2325 | bool mTypeInformationPrepared; |
| 2326 | std::vector<const llvm::Type*> mTypes; |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame^] | 2327 | bool mHasLinked; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2328 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2329 | public: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2330 | Compiler() |
| 2331 | : mpSymbolLookupFn(NULL), |
| 2332 | mpSymbolLookupContext(NULL), |
| 2333 | mContext(NULL), |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame^] | 2334 | mModule(NULL), |
| 2335 | mHasLinked(NULL) { |
Shih-wei Liao | c561199 | 2010-05-09 06:37:55 -0700 | [diff] [blame] | 2336 | llvm::remove_fatal_error_handler(); |
| 2337 | llvm::install_fatal_error_handler(LLVMErrorHandler, &mError); |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2338 | mContext = new llvm::LLVMContext(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2339 | return; |
| 2340 | } |
| 2341 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2342 | // interface for BCCscript::registerSymbolCallback() |
| 2343 | void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2344 | mpSymbolLookupFn = pFn; |
| 2345 | mpSymbolLookupContext = pContext; |
| 2346 | return; |
| 2347 | } |
| 2348 | |
Zonr Chang | dbee68b | 2010-10-22 05:02:16 +0800 | [diff] [blame] | 2349 | int loadModule(llvm::Module *module) { |
| 2350 | GlobalInitialization(); |
| 2351 | mModule = module; |
| 2352 | return hasError(); |
| 2353 | } |
| 2354 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2355 | int loadModule(const char *bitcode, size_t bitcodeSize) { |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2356 | llvm::OwningPtr<llvm::MemoryBuffer> SB; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2357 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2358 | if (bitcode == NULL || bitcodeSize <= 0) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2359 | return 0; |
Shih-wei Liao | c561199 | 2010-05-09 06:37:55 -0700 | [diff] [blame] | 2360 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2361 | GlobalInitialization(); |
| 2362 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2363 | // Package input to object MemoryBuffer |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2364 | SB.reset(llvm::MemoryBuffer::getMemBuffer( |
| 2365 | llvm::StringRef(bitcode, bitcodeSize))); |
| 2366 | |
| 2367 | if (SB.get() == NULL) { |
| 2368 | setError("Error reading input program bitcode into memory"); |
| 2369 | return hasError(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2370 | } |
| 2371 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2372 | // Read the input Bitcode as a Module |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2373 | mModule = llvm::ParseBitcodeFile(SB.get(), *mContext, &mError); |
| 2374 | SB.reset(); |
| 2375 | return hasError(); |
| 2376 | } |
Shih-wei Liao | c561199 | 2010-05-09 06:37:55 -0700 | [diff] [blame] | 2377 | |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2378 | int linkModule(const char *bitcode, size_t bitcodeSize) { |
| 2379 | llvm::OwningPtr<llvm::MemoryBuffer> SB; |
| 2380 | |
| 2381 | if (bitcode == NULL || bitcodeSize <= 0) |
| 2382 | return 0; |
| 2383 | |
| 2384 | if (mModule == NULL) { |
| 2385 | setError("No module presents for linking"); |
| 2386 | return hasError(); |
| 2387 | } |
| 2388 | |
| 2389 | SB.reset(llvm::MemoryBuffer::getMemBuffer( |
| 2390 | llvm::StringRef(bitcode, bitcodeSize))); |
| 2391 | |
| 2392 | if (SB.get() == NULL) { |
| 2393 | setError("Error reading input library bitcode into memory"); |
| 2394 | return hasError(); |
| 2395 | } |
| 2396 | |
| 2397 | llvm::OwningPtr<llvm::Module> Lib(llvm::ParseBitcodeFile(SB.get(), |
| 2398 | *mContext, |
| 2399 | &mError)); |
| 2400 | if (Lib.get() == NULL) |
| 2401 | return hasError(); |
| 2402 | |
| 2403 | if (llvm::Linker::LinkModules(mModule, Lib.take(), &mError)) |
| 2404 | return hasError(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2405 | |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame^] | 2406 | // Everything for linking should be settled down here with no error occurs |
| 2407 | mHasLinked = true; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2408 | return hasError(); |
| 2409 | } |
| 2410 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2411 | // interace for bccCompileScript() |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2412 | int compile() { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2413 | llvm::TargetData *TD = NULL; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2414 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2415 | llvm::TargetMachine *TM = NULL; |
| 2416 | const llvm::Target *Target; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2417 | std::string FeaturesStr; |
| 2418 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2419 | llvm::FunctionPassManager *CodeGenPasses = NULL; |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2420 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2421 | const llvm::NamedMDNode *PragmaMetadata; |
| 2422 | const llvm::NamedMDNode *ExportVarMetadata; |
| 2423 | const llvm::NamedMDNode *ExportFuncMetadata; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2424 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2425 | if (mModule == NULL) // No module was loaded |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2426 | return 0; |
| 2427 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2428 | // Create TargetMachine |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2429 | Target = llvm::TargetRegistry::lookupTarget(Triple, mError); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2430 | if (hasError()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2431 | goto on_bcc_compile_error; |
| 2432 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2433 | if (!CPU.empty() || !Features.empty()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2434 | llvm::SubtargetFeatures F; |
| 2435 | F.setCPU(CPU); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2436 | for (std::vector<std::string>::const_iterator I = Features.begin(), |
| 2437 | E = Features.end(); |
| 2438 | I != E; |
| 2439 | I++) |
| 2440 | F.AddFeature(*I); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2441 | FeaturesStr = F.getString(); |
| 2442 | } |
| 2443 | |
| 2444 | TM = Target->createTargetMachine(Triple, FeaturesStr); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2445 | if (TM == NULL) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2446 | setError("Failed to create target machine implementation for the" |
| 2447 | " specified triple '" + Triple + "'"); |
| 2448 | goto on_bcc_compile_error; |
| 2449 | } |
| 2450 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2451 | // Create memory manager for creation of code emitter later. |
| 2452 | if (!mCodeMemMgr.get() && !createCodeMemoryManager()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2453 | setError("Failed to startup memory management for further compilation"); |
| 2454 | goto on_bcc_compile_error; |
| 2455 | } |
| 2456 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2457 | // Create code emitter |
| 2458 | if (!mCodeEmitter.get()) { |
| 2459 | if (!createCodeEmitter()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2460 | setError("Failed to create machine code emitter to complete" |
| 2461 | " the compilation"); |
| 2462 | goto on_bcc_compile_error; |
| 2463 | } |
| 2464 | } else { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2465 | // Reuse the code emitter |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2466 | mCodeEmitter->reset(); |
| 2467 | } |
| 2468 | |
| 2469 | mCodeEmitter->setTargetMachine(*TM); |
| 2470 | mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn, |
| 2471 | mpSymbolLookupContext); |
| 2472 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2473 | // Get target data from Module |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2474 | TD = new llvm::TargetData(mModule); |
Zonr Chang | 6e1d6c3 | 2010-10-23 11:57:16 +0800 | [diff] [blame^] | 2475 | |
| 2476 | // Create LTO passes and run them on the mModule |
| 2477 | if (mHasLinked) { |
| 2478 | llvm::PassManager LTOPasses; |
| 2479 | LTOPasses.add(TD); |
| 2480 | |
| 2481 | llvm::createStandardLTOPasses(<OPasses, |
| 2482 | /* Internalize = */false, |
| 2483 | /* RunInliner = */true, |
| 2484 | /* VerifyEach = */false); |
| 2485 | LTOPasses.run(*mModule); |
| 2486 | } |
| 2487 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2488 | // Create code-gen pass to run the code emitter |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2489 | CodeGenPasses = new llvm::FunctionPassManager(mModule); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2490 | CodeGenPasses->add(TD); // Will take the ownership of TD |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2491 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2492 | if (TM->addPassesToEmitMachineCode(*CodeGenPasses, |
| 2493 | *mCodeEmitter, |
| 2494 | CodeGenOptLevel)) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2495 | setError("The machine code emission is not supported by BCC on target '" |
| 2496 | + Triple + "'"); |
| 2497 | goto on_bcc_compile_error; |
| 2498 | } |
| 2499 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2500 | // Run the pass (the code emitter) on every non-declaration function in the |
| 2501 | // module |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2502 | CodeGenPasses->doInitialization(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2503 | for (llvm::Module::iterator I = mModule->begin(), E = mModule->end(); |
| 2504 | I != E; |
| 2505 | I++) |
| 2506 | if (!I->isDeclaration()) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2507 | CodeGenPasses->run(*I); |
Shih-wei Liao | 066d5ef | 2010-05-11 03:28:39 -0700 | [diff] [blame] | 2508 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2509 | CodeGenPasses->doFinalization(); |
| 2510 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2511 | // Copy the global address mapping from code emitter and remapping |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2512 | ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2513 | if (ExportVarMetadata) { |
| 2514 | for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) { |
| 2515 | llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i); |
| 2516 | if (ExportVar != NULL && ExportVar->getNumOperands() > 1) { |
| 2517 | llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0); |
| 2518 | if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2519 | llvm::StringRef ExportVarName = |
| 2520 | static_cast<llvm::MDString*>(ExportVarNameMDS)->getString(); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2521 | CodeEmitter::global_addresses_const_iterator I, E; |
| 2522 | for (I = mCodeEmitter->global_address_begin(), |
| 2523 | E = mCodeEmitter->global_address_end(); |
| 2524 | I != E; |
| 2525 | I++) { |
| 2526 | if (I->first->getValueID() != llvm::Value::GlobalVariableVal) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2527 | continue; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2528 | if (ExportVarName == I->first->getName()) { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2529 | mExportVars.push_back(I->second); |
| 2530 | break; |
| 2531 | } |
| 2532 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2533 | if (I != mCodeEmitter->global_address_end()) |
| 2534 | continue; // found |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2535 | } |
| 2536 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2537 | // if here, the global variable record in metadata is not found, make an |
| 2538 | // empty slot |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2539 | mExportVars.push_back(NULL); |
| 2540 | } |
| 2541 | assert((mExportVars.size() == ExportVarMetadata->getNumOperands()) && |
| 2542 | "Number of slots doesn't match the number of export variables!"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2543 | } |
| 2544 | |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2545 | ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2546 | if (ExportFuncMetadata) { |
| 2547 | for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) { |
| 2548 | llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i); |
| 2549 | if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) { |
| 2550 | llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0); |
| 2551 | if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) { |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2552 | llvm::StringRef ExportFuncName = |
| 2553 | static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString(); |
| 2554 | mExportFuncs.push_back(mCodeEmitter->lookup(ExportFuncName)); |
| 2555 | } |
| 2556 | } |
| 2557 | } |
| 2558 | } |
| 2559 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2560 | // Tell code emitter now can release the memory using during the JIT since |
| 2561 | // we have done the code emission |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2562 | mCodeEmitter->releaseUnnecessary(); |
| 2563 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2564 | // Finally, read pragma information from the metadata node of the @Module if |
| 2565 | // any. |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2566 | PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2567 | if (PragmaMetadata) |
| 2568 | for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) { |
| 2569 | llvm::MDNode *Pragma = PragmaMetadata->getOperand(i); |
| 2570 | if (Pragma != NULL && |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2571 | Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2572 | llvm::Value *PragmaNameMDS = Pragma->getOperand(0); |
| 2573 | llvm::Value *PragmaValueMDS = Pragma->getOperand(1); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2574 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2575 | if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) && |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2576 | (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) { |
| 2577 | llvm::StringRef PragmaName = |
| 2578 | static_cast<llvm::MDString*>(PragmaNameMDS)->getString(); |
| 2579 | llvm::StringRef PragmaValue = |
| 2580 | static_cast<llvm::MDString*>(PragmaValueMDS)->getString(); |
| 2581 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2582 | mPragmas.push_back( |
| 2583 | std::make_pair(std::string(PragmaName.data(), |
| 2584 | PragmaName.size()), |
| 2585 | std::string(PragmaValue.data(), |
| 2586 | PragmaValue.size()))); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2587 | } |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | on_bcc_compile_error: |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2592 | // LOGE("on_bcc_compiler_error"); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2593 | if (CodeGenPasses) { |
| 2594 | delete CodeGenPasses; |
| 2595 | } else if (TD) { |
| 2596 | delete TD; |
| 2597 | } |
| 2598 | if (TM) |
| 2599 | delete TM; |
| 2600 | |
Shih-wei Liao | 066d5ef | 2010-05-11 03:28:39 -0700 | [diff] [blame] | 2601 | if (mError.empty()) { |
| 2602 | return false; |
| 2603 | } |
| 2604 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2605 | // LOGE(getErrorMessage()); |
Shih-wei Liao | 066d5ef | 2010-05-11 03:28:39 -0700 | [diff] [blame] | 2606 | return true; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2609 | // interface for bccGetScriptInfoLog() |
| 2610 | char *getErrorMessage() { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2611 | return const_cast<char*>(mError.c_str()); |
| 2612 | } |
| 2613 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2614 | // interface for bccGetScriptLabel() |
| 2615 | void *lookup(const char *name) { |
| 2616 | void *addr = NULL; |
| 2617 | if (mCodeEmitter.get()) |
| 2618 | // Find function pointer |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2619 | addr = mCodeEmitter->lookup(name); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2620 | return addr; |
| 2621 | } |
| 2622 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2623 | // Interface for bccGetExportVars() |
| 2624 | void getExportVars(BCCsizei *actualVarCount, |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2625 | BCCsizei maxVarCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2626 | BCCvoid **vars) { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2627 | int varCount = mExportVars.size(); |
| 2628 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2629 | if (actualVarCount) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2630 | *actualVarCount = varCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2631 | if (varCount > maxVarCount) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2632 | varCount = maxVarCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2633 | if (vars) |
| 2634 | for (ExportVarList::const_iterator I = mExportVars.begin(), |
| 2635 | E = mExportVars.end(); |
| 2636 | I != E; |
| 2637 | I++) |
| 2638 | *vars++ = *I; |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2639 | |
| 2640 | return; |
| 2641 | } |
| 2642 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2643 | // Interface for bccGetExportFuncs() |
| 2644 | void getExportFuncs(BCCsizei *actualFuncCount, |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2645 | BCCsizei maxFuncCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2646 | BCCvoid **funcs) { |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2647 | int funcCount = mExportFuncs.size(); |
| 2648 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2649 | if (actualFuncCount) |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2650 | *actualFuncCount = funcCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2651 | if (funcCount > maxFuncCount) |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2652 | funcCount = maxFuncCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2653 | if (funcs) |
| 2654 | for (ExportFuncList::const_iterator I = mExportFuncs.begin(), |
| 2655 | E = mExportFuncs.end(); |
| 2656 | I != E; |
| 2657 | I++) |
| 2658 | *funcs++ = *I; |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2659 | |
| 2660 | return; |
| 2661 | } |
| 2662 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2663 | // Interface for bccGetPragmas() |
| 2664 | void getPragmas(BCCsizei *actualStringCount, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2665 | BCCsizei maxStringCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2666 | BCCchar **strings) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2667 | int stringCount = mPragmas.size() * 2; |
| 2668 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2669 | if (actualStringCount) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2670 | *actualStringCount = stringCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2671 | if (stringCount > maxStringCount) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2672 | stringCount = maxStringCount; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2673 | if (strings) |
| 2674 | for (PragmaList::const_iterator it = mPragmas.begin(); |
| 2675 | stringCount > 0; |
| 2676 | stringCount -= 2, it++) { |
| 2677 | *strings++ = const_cast<BCCchar*>(it->first.c_str()); |
| 2678 | *strings++ = const_cast<BCCchar*>(it->second.c_str()); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | return; |
| 2682 | } |
| 2683 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2684 | // Interface for bccGetFunctions() |
| 2685 | void getFunctions(BCCsizei *actualFunctionCount, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2686 | BCCsizei maxFunctionCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2687 | BCCchar **functions) { |
| 2688 | if (mCodeEmitter.get()) |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 2689 | mCodeEmitter->getFunctionNames(actualFunctionCount, |
| 2690 | maxFunctionCount, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2691 | functions); |
| 2692 | else |
| 2693 | *actualFunctionCount = 0; |
| 2694 | |
| 2695 | return; |
| 2696 | } |
| 2697 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2698 | // Interface for bccGetFunctionBinary() |
| 2699 | void getFunctionBinary(BCCchar *function, |
| 2700 | BCCvoid **base, |
| 2701 | BCCsizei *length) { |
| 2702 | if (mCodeEmitter.get()) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2703 | mCodeEmitter->getFunctionBinary(function, base, length); |
| 2704 | } else { |
| 2705 | *base = NULL; |
| 2706 | *length = 0; |
| 2707 | } |
| 2708 | return; |
| 2709 | } |
| 2710 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2711 | inline const llvm::Module *getModule() const { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2712 | return mModule; |
| 2713 | } |
| 2714 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2715 | inline const std::vector<const llvm::Type*> &getTypes() const { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2716 | return mTypes; |
| 2717 | } |
| 2718 | |
| 2719 | ~Compiler() { |
| 2720 | delete mModule; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2721 | // llvm::llvm_shutdown(); |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2722 | delete mContext; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2723 | return; |
| 2724 | } |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2725 | }; |
| 2726 | // End of Class Compiler |
| 2727 | //////////////////////////////////////////////////////////////////////////////// |
| 2728 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2729 | |
| 2730 | bool Compiler::GlobalInitialized = false; |
| 2731 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2732 | // Code generation optimization level for the compiler |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2733 | llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel; |
| 2734 | |
| 2735 | std::string Compiler::Triple; |
| 2736 | |
| 2737 | std::string Compiler::CPU; |
| 2738 | |
| 2739 | std::vector<std::string> Compiler::Features; |
| 2740 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2741 | // The named of metadata node that pragma resides (should be synced with |
| 2742 | // slang.cpp) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2743 | const llvm::StringRef Compiler::PragmaMetadataName = "#pragma"; |
| 2744 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2745 | // The named of metadata node that export variable name resides (should be |
| 2746 | // synced with slang_rs_metadata.h) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2747 | const llvm::StringRef Compiler::ExportVarMetadataName = "#rs_export_var"; |
| 2748 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2749 | // The named of metadata node that export function name resides (should be |
| 2750 | // synced with slang_rs_metadata.h) |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2751 | const llvm::StringRef Compiler::ExportFuncMetadataName = "#rs_export_func"; |
| 2752 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2753 | struct BCCscript { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2754 | ////////////////////////////////////////////////////////////////////////////// |
| 2755 | // Part I. Compiler |
| 2756 | ////////////////////////////////////////////////////////////////////////////// |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2757 | Compiler compiler; |
| 2758 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2759 | void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2760 | compiler.registerSymbolCallback(pFn, pContext); |
| 2761 | } |
| 2762 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2763 | ////////////////////////////////////////////////////////////////////////////// |
| 2764 | // Part II. Logistics & Error handling |
| 2765 | ////////////////////////////////////////////////////////////////////////////// |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2766 | BCCscript() { |
| 2767 | bccError = BCC_NO_ERROR; |
| 2768 | } |
| 2769 | |
| 2770 | ~BCCscript() { |
| 2771 | } |
| 2772 | |
| 2773 | void setError(BCCenum error) { |
| 2774 | if (bccError == BCC_NO_ERROR && error != BCC_NO_ERROR) { |
| 2775 | bccError = error; |
| 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | BCCenum getError() { |
| 2780 | BCCenum result = bccError; |
| 2781 | bccError = BCC_NO_ERROR; |
| 2782 | return result; |
| 2783 | } |
| 2784 | |
| 2785 | BCCenum bccError; |
| 2786 | }; |
| 2787 | |
| 2788 | |
| 2789 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2790 | BCCscript *bccCreateScript() { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2791 | return new BCCscript(); |
| 2792 | } |
| 2793 | |
| 2794 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2795 | BCCenum bccGetError(BCCscript *script) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2796 | return script->getError(); |
| 2797 | } |
| 2798 | |
| 2799 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2800 | void bccDeleteScript(BCCscript *script) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2801 | delete script; |
| 2802 | } |
| 2803 | |
| 2804 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2805 | void bccRegisterSymbolCallback(BCCscript *script, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2806 | BCCSymbolLookupFn pFn, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2807 | BCCvoid *pContext) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2808 | script->registerSymbolCallback(pFn, pContext); |
| 2809 | } |
| 2810 | |
| 2811 | extern "C" |
Zonr Chang | dbee68b | 2010-10-22 05:02:16 +0800 | [diff] [blame] | 2812 | void bccScriptModule(BCCscript *script, |
| 2813 | BCCvoid *module) { |
| 2814 | script->compiler.loadModule(reinterpret_cast<llvm::Module*>(module)); |
| 2815 | } |
| 2816 | |
| 2817 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2818 | void bccScriptBitcode(BCCscript *script, |
| 2819 | const BCCchar *bitcode, |
| 2820 | BCCint size) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2821 | script->compiler.loadModule(bitcode, size); |
| 2822 | } |
| 2823 | |
| 2824 | extern "C" |
Zonr Chang | 97f5e61 | 2010-10-22 20:38:26 +0800 | [diff] [blame] | 2825 | void bccLinkBitcode(BCCscript *script, |
| 2826 | const BCCchar *bitcode, |
| 2827 | BCCint size) { |
| 2828 | script->compiler.linkModule(bitcode, size); |
| 2829 | } |
| 2830 | |
| 2831 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2832 | void bccCompileScript(BCCscript *script) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2833 | int result = script->compiler.compile(); |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 2834 | if (result) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2835 | script->setError(BCC_INVALID_OPERATION); |
| 2836 | } |
| 2837 | |
| 2838 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2839 | void bccGetScriptInfoLog(BCCscript *script, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2840 | BCCsizei maxLength, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2841 | BCCsizei *length, |
| 2842 | BCCchar *infoLog) { |
| 2843 | char *message = script->compiler.getErrorMessage(); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2844 | int messageLength = strlen(message) + 1; |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 2845 | if (length) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2846 | *length = messageLength; |
| 2847 | |
| 2848 | if (infoLog && maxLength > 0) { |
| 2849 | int trimmedLength = maxLength < messageLength ? maxLength : messageLength; |
| 2850 | memcpy(infoLog, message, trimmedLength); |
| 2851 | infoLog[trimmedLength] = 0; |
| 2852 | } |
| 2853 | } |
| 2854 | |
| 2855 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2856 | void bccGetScriptLabel(BCCscript *script, |
| 2857 | const BCCchar *name, |
| 2858 | BCCvoid **address) { |
| 2859 | void *value = script->compiler.lookup(name); |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 2860 | if (value) |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2861 | *address = value; |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 2862 | else |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2863 | script->setError(BCC_INVALID_VALUE); |
| 2864 | } |
| 2865 | |
| 2866 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2867 | void bccGetExportVars(BCCscript *script, |
| 2868 | BCCsizei *actualVarCount, |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2869 | BCCsizei maxVarCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2870 | BCCvoid **vars) { |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 2871 | script->compiler.getExportVars(actualVarCount, maxVarCount, vars); |
| 2872 | } |
| 2873 | |
| 2874 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2875 | void bccGetExportFuncs(BCCscript *script, |
| 2876 | BCCsizei *actualFuncCount, |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2877 | BCCsizei maxFuncCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2878 | BCCvoid **funcs) { |
Shih-wei Liao | 6bfd542 | 2010-05-07 05:20:22 -0700 | [diff] [blame] | 2879 | script->compiler.getExportFuncs(actualFuncCount, maxFuncCount, funcs); |
| 2880 | } |
| 2881 | |
| 2882 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2883 | void bccGetPragmas(BCCscript *script, |
| 2884 | BCCsizei *actualStringCount, |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 2885 | BCCsizei maxStringCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2886 | BCCchar **strings) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2887 | script->compiler.getPragmas(actualStringCount, maxStringCount, strings); |
| 2888 | } |
| 2889 | |
| 2890 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2891 | void bccGetFunctions(BCCscript *script, |
| 2892 | BCCsizei *actualFunctionCount, |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 2893 | BCCsizei maxFunctionCount, |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2894 | BCCchar **functions) { |
Shih-wei Liao | 3cf39d1 | 2010-04-29 19:30:51 -0700 | [diff] [blame] | 2895 | script->compiler.getFunctions(actualFunctionCount, |
| 2896 | maxFunctionCount, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2897 | functions); |
| 2898 | } |
| 2899 | |
| 2900 | extern "C" |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2901 | void bccGetFunctionBinary(BCCscript *script, |
| 2902 | BCCchar *function, |
| 2903 | BCCvoid **base, |
| 2904 | BCCsizei *length) { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2905 | script->compiler.getFunctionBinary(function, base, length); |
| 2906 | } |
| 2907 | |
| 2908 | struct BCCtype { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2909 | const Compiler *compiler; |
| 2910 | const llvm::Type *t; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 2911 | }; |
| 2912 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 2913 | } // namespace bcc |