29ffa30215087a900196c97f926b5f718d8ca1fb
[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 using namespace llvm;
22
23 VM::~VM() {
24   delete MCE;
25   delete &TM;
26 }
27
28 /// setupPassManager - Initialize the VM PassManager object with all of the
29 /// passes needed for the target to generate code.
30 ///
31 void VM::setupPassManager() {
32   // Compile LLVM Code down to machine code in the intermediate representation
33   if (TM.addPassesToJITCompile(PM)) {
34     std::cerr << "lli: target '" << TM.getName()
35               << "' doesn't support JIT compilation!\n";
36     abort();
37   }
38
39   // Turn the machine code intermediate representation into bytes in memory that
40   // may be executed.
41   if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
42     std::cerr << "lli: target '" << TM.getName()
43               << "' doesn't support machine code emission!\n";
44     abort();
45   }
46 }
47
48 /// runJITOnFunction - Run the FunctionPassManager full of
49 /// just-in-time compilation passes on F, hopefully filling in
50 /// GlobalAddress[F] with the address of F's machine code.
51 ///
52 void VM::runJITOnFunction(Function *F) {
53   static bool isAlreadyCodeGenerating = false;
54   assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
55
56   // JIT the function
57   isAlreadyCodeGenerating = true;
58   PM.run(*F);
59   isAlreadyCodeGenerating = false;
60 }
61
62 /// getPointerToFunction - This method is used to get the address of the
63 /// specified function, compiling it if neccesary.
64 ///
65 void *VM::getPointerToFunction(Function *F) {
66   void *&Addr = GlobalAddress[F];   // Check if function already code gen'd
67   if (Addr) return Addr;
68
69   // Make sure we read in the function if it exists in this Module
70   MP->materializeFunction(F);
71
72   if (F->isExternal())
73     return Addr = getPointerToNamedFunction(F->getName());
74
75   runJITOnFunction(F);
76   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
77   return Addr;
78 }
79
80 // getPointerToFunctionOrStub - If the specified function has been
81 // code-gen'd, return a pointer to the function.  If not, compile it, or use
82 // a stub to implement lazy compilation if available.
83 //
84 void *VM::getPointerToFunctionOrStub(Function *F) {
85   // If we have already code generated the function, just return the address.
86   std::map<const GlobalValue*, void *>::iterator I = GlobalAddress.find(F);
87   if (I != GlobalAddress.end()) return I->second;
88
89   // If the target supports "stubs" for functions, get a stub now.
90   if (void *Ptr = TM.getJITStubForFunction(F, *MCE))
91     return Ptr;
92
93   // Otherwise, if the target doesn't support it, just codegen the function.
94   return getPointerToFunction(F);
95 }
96
97 /// recompileAndRelinkFunction - This method is used to force a function
98 /// which has already been compiled, to be compiled again, possibly
99 /// after it has been modified. Then the entry to the old copy is overwritten
100 /// with a branch to the new copy. If there was no old copy, this acts
101 /// just like VM::getPointerToFunction().
102 ///
103 void *VM::recompileAndRelinkFunction(Function *F) {
104   void *&Addr = GlobalAddress[F];   // Check if function already code gen'd
105
106   // If it's not already compiled (this is kind of weird) there is no
107   // reason to patch it up.
108   if (!Addr) { return getPointerToFunction (F); }
109
110   void *OldAddr = Addr;
111   Addr = 0;
112   MachineFunction::destruct(F);
113   runJITOnFunction(F);
114   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
115   TM.replaceMachineCodeForFunction(OldAddr, Addr);
116   return Addr;
117 }