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