Silence a warning.
[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/Support/ErrorHandling.h"
15 #include "llvm/Support/DynamicLibrary.h"
16 #include "llvm/Target/TargetData.h"
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())
56     return new MCJIT(M, TM, *TJ, JMM, OptLevel, GVsWithCode);
57
58   if (ErrorStr)
59     *ErrorStr = "target does not support JIT code generation";
60   return 0;
61 }
62
63 MCJIT::MCJIT(Module *m, TargetMachine *tm, TargetJITInfo &tji,
64              JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
65              bool AllocateGVsWithCode)
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();
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) {
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) {
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))(intptr_t)FPtr)();
113   return GenericValue();
114 }