Eric Christopher | d619067 | 2011-03-22 08:49:56 +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 | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 11 | #include "llvm/Function.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 12 | #include "llvm/ExecutionEngine/GenericValue.h" |
| 13 | #include "llvm/ExecutionEngine/MCJIT.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 14 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
| 15 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 17 | #include "llvm/Support/DynamicLibrary.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | static struct RegisterJIT { |
| 26 | RegisterJIT() { MCJIT::Register(); } |
| 27 | } JITRegistrator; |
| 28 | |
| 29 | } |
| 30 | |
| 31 | extern "C" void LLVMLinkInMCJIT() { |
| 32 | } |
| 33 | |
| 34 | ExecutionEngine *MCJIT::createJIT(Module *M, |
| 35 | std::string *ErrorStr, |
| 36 | JITMemoryManager *JMM, |
| 37 | CodeGenOpt::Level OptLevel, |
| 38 | bool GVsWithCode, |
| 39 | CodeModel::Model CMM, |
| 40 | StringRef MArch, |
| 41 | StringRef MCPU, |
| 42 | const SmallVectorImpl<std::string>& MAttrs) { |
| 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 | |
| 48 | // Pick a target either via -march or by guessing the native arch. |
| 49 | // |
| 50 | // FIXME: This should be lifted out of here, it isn't something which should |
| 51 | // be part of the JIT policy, rather the burden for this selection should be |
| 52 | // pushed to clients. |
| 53 | TargetMachine *TM = MCJIT::selectTarget(M, MArch, MCPU, MAttrs, ErrorStr); |
| 54 | if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0; |
| 55 | TM->setCodeModel(CMM); |
| 56 | |
| 57 | // If the target supports JIT code generation, create the JIT. |
| 58 | if (TargetJITInfo *TJ = TM->getJITInfo()) |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 59 | return new MCJIT(M, TM, *TJ, JMM, OptLevel, GVsWithCode); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 60 | |
| 61 | if (ErrorStr) |
| 62 | *ErrorStr = "target does not support JIT code generation"; |
| 63 | return 0; |
| 64 | } |
| 65 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 66 | MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji, |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 67 | JITMemoryManager *JMM, CodeGenOpt::Level OptLevel, |
| 68 | bool AllocateGVsWithCode) |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 69 | : ExecutionEngine(m), TM(tm), M(m), OS(Buffer) { |
| 70 | |
| 71 | PM.add(new TargetData(*TM->getTargetData())); |
| 72 | |
| 73 | // Turn the machine code intermediate representation into bytes in memory |
| 74 | // that may be executed. |
| 75 | if (TM->addPassesToEmitMC(PM, Ctx, OS, CodeGenOpt::Default, false)) { |
| 76 | report_fatal_error("Target does not support MC emission!"); |
| 77 | } |
| 78 | |
| 79 | // Initialize passes. |
| 80 | ExecutionEngine::addModule(M); |
| 81 | // FIXME: When we support multiple modules, we'll want to move the code |
| 82 | // gen and finalization out of the constructor here and do it more |
| 83 | // on-demand as part of getPointerToFunction(). |
| 84 | PM.run(*M); |
| 85 | // Flush the output buffer so the SmallVector gets its data. |
| 86 | OS.flush(); |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 87 | |
| 88 | // Load the object into the dynamic linker. |
| 89 | // FIXME: It would be nice to avoid making yet another copy. |
| 90 | MemoryBuffer *MB = MemoryBuffer::getMemBufferCopy(StringRef(Buffer.data(), |
| 91 | Buffer.size())); |
| 92 | Dyld.loadObject(MB); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | MCJIT::~MCJIT() { |
| 96 | } |
| 97 | |
| 98 | void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) { |
| 99 | report_fatal_error("not yet implemented"); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | void *MCJIT::getPointerToFunction(Function *F) { |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 104 | Twine Name = TM->getMCAsmInfo()->getGlobalPrefix() + F->getName(); |
| 105 | return Dyld.getSymbolAddress(Name.str()); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void *MCJIT::recompileAndRelinkFunction(Function *F) { |
| 109 | report_fatal_error("not yet implemented"); |
| 110 | } |
| 111 | |
| 112 | void MCJIT::freeMachineCodeForFunction(Function *F) { |
| 113 | report_fatal_error("not yet implemented"); |
| 114 | } |
| 115 | |
| 116 | GenericValue MCJIT::runFunction(Function *F, |
| 117 | const std::vector<GenericValue> &ArgValues) { |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 118 | assert(ArgValues.size() == 0 && "JIT arg passing not supported yet"); |
| 119 | void *FPtr = getPointerToFunction(F); |
| 120 | if (!FPtr) |
| 121 | report_fatal_error("Unable to locate function: '" + F->getName() + "'"); |
Jim Grosbach | 7b6747e | 2011-03-18 22:50:49 +0000 | [diff] [blame] | 122 | ((void(*)(void))(intptr_t)FPtr)(); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 123 | return GenericValue(); |
| 124 | } |