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