Hook up the MCJIT to the RuntimeDyld library.
[oota-llvm.git] / lib / ExecutionEngine / MCJIT / MCJIT.cpp
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"
11 #include "llvm/Function.h"
12 #include "llvm/ExecutionEngine/GenericValue.h"
13 #include "llvm/ExecutionEngine/MCJIT.h"
14 #include "llvm/ExecutionEngine/JITMemoryManager.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/DynamicLibrary.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Target/TargetData.h"
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())
59     return new MCJIT(M, TM, *TJ, JMM, OptLevel, GVsWithCode);
60
61   if (ErrorStr)
62     *ErrorStr = "target does not support JIT code generation";
63   return 0;
64 }
65
66 MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
67              JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
68              bool AllocateGVsWithCode)
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();
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);
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) {
104   Twine Name = TM->getMCAsmInfo()->getGlobalPrefix() + F->getName();
105   return Dyld.getSymbolAddress(Name.str());
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) {
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() + "'");
122   ((void(*)(void))(intptr_t)FPtr)();
123   return GenericValue();
124 }