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