Eric Christopher | bb498ca | 2011-04-22 03:07:06 +0000 | [diff] [blame] | 1 | //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===// |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "MCJIT.h" |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 11 | #include "MCJITMemoryManager.h" |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 12 | #include "llvm/DerivedTypes.h" |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 13 | #include "llvm/Function.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 14 | #include "llvm/ExecutionEngine/GenericValue.h" |
| 15 | #include "llvm/ExecutionEngine/MCJIT.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
| 17 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 19 | #include "llvm/Support/DynamicLibrary.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MutexGuard.h" |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | static struct RegisterJIT { |
| 29 | RegisterJIT() { MCJIT::Register(); } |
| 30 | } JITRegistrator; |
| 31 | |
| 32 | } |
| 33 | |
| 34 | extern "C" void LLVMLinkInMCJIT() { |
| 35 | } |
| 36 | |
| 37 | ExecutionEngine *MCJIT::createJIT(Module *M, |
| 38 | std::string *ErrorStr, |
| 39 | JITMemoryManager *JMM, |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 40 | bool GVsWithCode, |
Dylan Noblesmith | c5b2858 | 2011-05-13 21:51:29 +0000 | [diff] [blame] | 41 | TargetMachine *TM) { |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 42 | // Try to register the program as a source of symbols to resolve against. |
| 43 | // |
| 44 | // FIXME: Don't do this here. |
| 45 | sys::DynamicLibrary::LoadLibraryPermanently(0, NULL); |
| 46 | |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 47 | // If the target supports JIT code generation, create the JIT. |
| 48 | if (TargetJITInfo *TJ = TM->getJITInfo()) |
Benjamin Kramer | 95a9d93 | 2012-06-06 19:47:08 +0000 | [diff] [blame] | 49 | return new MCJIT(M, TM, *TJ, new MCJITMemoryManager(JMM), GVsWithCode); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 50 | |
| 51 | if (ErrorStr) |
| 52 | *ErrorStr = "target does not support JIT code generation"; |
| 53 | return 0; |
| 54 | } |
| 55 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 56 | MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji, |
Dylan Noblesmith | 9ea4717 | 2011-12-12 04:20:36 +0000 | [diff] [blame] | 57 | RTDyldMemoryManager *MM, bool AllocateGVsWithCode) |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 58 | : ExecutionEngine(m), TM(tm), Ctx(0), MemMgr(MM), Dyld(MM), |
| 59 | isCompiled(false), M(m), OS(Buffer) { |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 60 | |
Danil Malyshev | 0ba3c0a | 2011-09-30 16:40:10 +0000 | [diff] [blame] | 61 | setTargetData(TM->getTargetData()); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | MCJIT::~MCJIT() { |
| 65 | delete MemMgr; |
| 66 | delete TM; |
| 67 | } |
| 68 | |
| 69 | void MCJIT::emitObject(Module *m) { |
| 70 | /// Currently, MCJIT only supports a single module and the module passed to |
| 71 | /// this function call is expected to be the contained module. The module |
| 72 | /// is passed as a parameter here to prepare for multiple module support in |
| 73 | /// the future. |
| 74 | assert(M == m); |
| 75 | |
| 76 | // Get a thread lock to make sure we aren't trying to compile multiple times |
| 77 | MutexGuard locked(lock); |
| 78 | |
| 79 | // FIXME: Track compilation state on a per-module basis when multiple modules |
| 80 | // are supported. |
| 81 | // Re-compilation is not supported |
| 82 | if (isCompiled) |
| 83 | return; |
| 84 | |
| 85 | PassManager PM; |
| 86 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 87 | PM.add(new TargetData(*TM->getTargetData())); |
| 88 | |
| 89 | // Turn the machine code intermediate representation into bytes in memory |
| 90 | // that may be executed. |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 91 | if (TM->addPassesToEmitMC(PM, Ctx, OS, false)) { |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 92 | report_fatal_error("Target does not support MC emission!"); |
| 93 | } |
| 94 | |
| 95 | // Initialize passes. |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 96 | // FIXME: When we support multiple modules, we'll want to move the code |
| 97 | // gen and finalization out of the constructor here and do it more |
| 98 | // on-demand as part of getPointerToFunction(). |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 99 | PM.run(*m); |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 100 | // Flush the output buffer so the SmallVector gets its data. |
| 101 | OS.flush(); |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 102 | |
| 103 | // Load the object into the dynamic linker. |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 104 | MemoryBuffer* MB = MemoryBuffer::getMemBuffer(StringRef(Buffer.data(), |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 105 | Buffer.size()), |
| 106 | "", false); |
Jim Grosbach | 8086f3b | 2011-03-23 19:51:34 +0000 | [diff] [blame] | 107 | if (Dyld.loadObject(MB)) |
| 108 | report_fatal_error(Dyld.getErrorString()); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 109 | |
Jim Grosbach | 69e8132 | 2011-04-13 15:28:10 +0000 | [diff] [blame] | 110 | // Resolve any relocations. |
| 111 | Dyld.resolveRelocations(); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 112 | |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 113 | // FIXME: Add support for per-module compilation state |
| 114 | isCompiled = true; |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) { |
| 118 | report_fatal_error("not yet implemented"); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void *MCJIT::getPointerToFunction(Function *F) { |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 122 | // FIXME: Add support for per-module compilation state |
| 123 | if (!isCompiled) |
| 124 | emitObject(M); |
| 125 | |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 126 | if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { |
| 127 | bool AbortOnFailure = !F->hasExternalWeakLinkage(); |
| 128 | void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure); |
| 129 | addGlobalMapping(F, Addr); |
| 130 | return Addr; |
| 131 | } |
| 132 | |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 133 | // FIXME: Should the Dyld be retaining module information? Probably not. |
Jim Grosbach | 3ec2c7c | 2011-05-18 23:53:21 +0000 | [diff] [blame] | 134 | // FIXME: Should we be using the mangler for this? Probably. |
| 135 | StringRef BaseName = F->getName(); |
| 136 | if (BaseName[0] == '\1') |
Jim Grosbach | c0ceedb | 2011-05-19 00:45:05 +0000 | [diff] [blame] | 137 | return (void*)Dyld.getSymbolAddress(BaseName.substr(1)); |
| 138 | return (void*)Dyld.getSymbolAddress((TM->getMCAsmInfo()->getGlobalPrefix() |
| 139 | + BaseName).str()); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void *MCJIT::recompileAndRelinkFunction(Function *F) { |
| 143 | report_fatal_error("not yet implemented"); |
| 144 | } |
| 145 | |
| 146 | void MCJIT::freeMachineCodeForFunction(Function *F) { |
| 147 | report_fatal_error("not yet implemented"); |
| 148 | } |
| 149 | |
| 150 | GenericValue MCJIT::runFunction(Function *F, |
| 151 | const std::vector<GenericValue> &ArgValues) { |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 152 | assert(F && "Function *F was null at entry to run()"); |
| 153 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 154 | void *FPtr = getPointerToFunction(F); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 155 | assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 156 | FunctionType *FTy = F->getFunctionType(); |
| 157 | Type *RetTy = FTy->getReturnType(); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 158 | |
| 159 | assert((FTy->getNumParams() == ArgValues.size() || |
| 160 | (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && |
| 161 | "Wrong number of arguments passed into function!"); |
| 162 | assert(FTy->getNumParams() == ArgValues.size() && |
| 163 | "This doesn't support passing arguments through varargs (yet)!"); |
| 164 | |
| 165 | // Handle some common cases first. These cases correspond to common `main' |
| 166 | // prototypes. |
| 167 | if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) { |
| 168 | switch (ArgValues.size()) { |
| 169 | case 3: |
| 170 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 171 | FTy->getParamType(1)->isPointerTy() && |
| 172 | FTy->getParamType(2)->isPointerTy()) { |
| 173 | int (*PF)(int, char **, const char **) = |
| 174 | (int(*)(int, char **, const char **))(intptr_t)FPtr; |
| 175 | |
| 176 | // Call the function. |
| 177 | GenericValue rv; |
| 178 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 179 | (char **)GVTOP(ArgValues[1]), |
| 180 | (const char **)GVTOP(ArgValues[2]))); |
| 181 | return rv; |
| 182 | } |
| 183 | break; |
| 184 | case 2: |
| 185 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 186 | FTy->getParamType(1)->isPointerTy()) { |
| 187 | int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; |
| 188 | |
| 189 | // Call the function. |
| 190 | GenericValue rv; |
| 191 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 192 | (char **)GVTOP(ArgValues[1]))); |
| 193 | return rv; |
| 194 | } |
| 195 | break; |
| 196 | case 1: |
| 197 | if (FTy->getNumParams() == 1 && |
| 198 | FTy->getParamType(0)->isIntegerTy(32)) { |
| 199 | GenericValue rv; |
| 200 | int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; |
| 201 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); |
| 202 | return rv; |
| 203 | } |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // Handle cases where no arguments are passed first. |
| 209 | if (ArgValues.empty()) { |
| 210 | GenericValue rv; |
| 211 | switch (RetTy->getTypeID()) { |
| 212 | default: llvm_unreachable("Unknown return type for function call!"); |
| 213 | case Type::IntegerTyID: { |
| 214 | unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); |
| 215 | if (BitWidth == 1) |
| 216 | rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); |
| 217 | else if (BitWidth <= 8) |
| 218 | rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); |
| 219 | else if (BitWidth <= 16) |
| 220 | rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); |
| 221 | else if (BitWidth <= 32) |
| 222 | rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); |
| 223 | else if (BitWidth <= 64) |
| 224 | rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); |
| 225 | else |
| 226 | llvm_unreachable("Integer types > 64 bits not supported"); |
| 227 | return rv; |
| 228 | } |
| 229 | case Type::VoidTyID: |
| 230 | rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); |
| 231 | return rv; |
| 232 | case Type::FloatTyID: |
| 233 | rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); |
| 234 | return rv; |
| 235 | case Type::DoubleTyID: |
| 236 | rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); |
| 237 | return rv; |
| 238 | case Type::X86_FP80TyID: |
| 239 | case Type::FP128TyID: |
| 240 | case Type::PPC_FP128TyID: |
| 241 | llvm_unreachable("long double not supported yet"); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 242 | case Type::PointerTyID: |
| 243 | return PTOGV(((void*(*)())(intptr_t)FPtr)()); |
| 244 | } |
| 245 | } |
| 246 | |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 247 | llvm_unreachable("Full-featured argument passing not supported yet!"); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 248 | } |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 249 | |
| 250 | void *MCJIT::getPointerToNamedFunction(const std::string &Name, |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 251 | bool AbortOnFailure) { |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 252 | // FIXME: Add support for per-module compilation state |
| 253 | if (!isCompiled) |
| 254 | emitObject(M); |
| 255 | |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 256 | if (!isSymbolSearchingDisabled() && MemMgr) { |
| 257 | void *ptr = MemMgr->getPointerToNamedFunction(Name, false); |
| 258 | if (ptr) |
| 259 | return ptr; |
| 260 | } |
| 261 | |
| 262 | /// If a LazyFunctionCreator is installed, use it to get/create the function. |
| 263 | if (LazyFunctionCreator) |
| 264 | if (void *RP = LazyFunctionCreator(Name)) |
| 265 | return RP; |
| 266 | |
| 267 | if (AbortOnFailure) { |
| 268 | report_fatal_error("Program used external function '"+Name+ |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 269 | "' which could not be resolved!"); |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 270 | } |
| 271 | return 0; |
| 272 | } |