Rip JIT specific stuff out of TargetMachine, as per PR176
[oota-llvm.git] / lib / ExecutionEngine / JIT / VM.cpp
1 //===-- VM.cpp - LLVM Just in Time Compiler -------------------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This tool implements a just-in-time compiler for LLVM, allowing direct
11 // execution of LLVM bytecode in an efficient manner.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "VM.h"
16 #include "llvm/Function.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/CodeGen/MachineCodeEmitter.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Target/TargetJITInfo.h"
22 using namespace llvm;
23
24 VM::~VM() {
25   delete MCE;
26   delete &TM;
27 }
28
29 /// setupPassManager - Initialize the VM PassManager object with all of the
30 /// passes needed for the target to generate code.
31 ///
32 void VM::setupPassManager() {
33   // Compile LLVM Code down to machine code in the intermediate representation
34   TJI.addPassesToJITCompile(PM);
35
36   // Turn the machine code intermediate representation into bytes in memory that
37   // may be executed.
38   if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
39     std::cerr << "lli: target '" << TM.getName()
40               << "' doesn't support machine code emission!\n";
41     abort();
42   }
43 }
44
45 /// runJITOnFunction - Run the FunctionPassManager full of
46 /// just-in-time compilation passes on F, hopefully filling in
47 /// GlobalAddress[F] with the address of F's machine code.
48 ///
49 void VM::runJITOnFunction(Function *F) {
50   static bool isAlreadyCodeGenerating = false;
51   assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
52
53   // JIT the function
54   isAlreadyCodeGenerating = true;
55   PM.run(*F);
56   isAlreadyCodeGenerating = false;
57 }
58
59 /// getPointerToFunction - This method is used to get the address of the
60 /// specified function, compiling it if neccesary.
61 ///
62 void *VM::getPointerToFunction(Function *F) {
63   void *&Addr = GlobalAddress[F];   // Check if function already code gen'd
64   if (Addr) return Addr;
65
66   // Make sure we read in the function if it exists in this Module
67   MP->materializeFunction(F);
68
69   if (F->isExternal())
70     return Addr = getPointerToNamedFunction(F->getName());
71
72   runJITOnFunction(F);
73   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
74   return Addr;
75 }
76
77 // getPointerToFunctionOrStub - If the specified function has been
78 // code-gen'd, return a pointer to the function.  If not, compile it, or use
79 // a stub to implement lazy compilation if available.
80 //
81 void *VM::getPointerToFunctionOrStub(Function *F) {
82   // If we have already code generated the function, just return the address.
83   std::map<const GlobalValue*, void *>::iterator I = GlobalAddress.find(F);
84   if (I != GlobalAddress.end()) return I->second;
85
86   // If the target supports "stubs" for functions, get a stub now.
87   if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
88     return Ptr;
89
90   // Otherwise, if the target doesn't support it, just codegen the function.
91   return getPointerToFunction(F);
92 }
93
94 /// recompileAndRelinkFunction - This method is used to force a function
95 /// which has already been compiled, to be compiled again, possibly
96 /// after it has been modified. Then the entry to the old copy is overwritten
97 /// with a branch to the new copy. If there was no old copy, this acts
98 /// just like VM::getPointerToFunction().
99 ///
100 void *VM::recompileAndRelinkFunction(Function *F) {
101   void *&Addr = GlobalAddress[F];   // Check if function already code gen'd
102
103   // If it's not already compiled (this is kind of weird) there is no
104   // reason to patch it up.
105   if (!Addr) { return getPointerToFunction (F); }
106
107   void *OldAddr = Addr;
108   Addr = 0;
109   MachineFunction::destruct(F);
110   runJITOnFunction(F);
111   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
112   TJI.replaceMachineCodeForFunction(OldAddr, Addr);
113   return Addr;
114 }