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