Cleanup the JIT as per PR176. This renames the VM class to JIT, and merges the
[oota-llvm.git] / lib / ExecutionEngine / JIT / JIT.cpp
1 //===-- JIT.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 "JIT.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/ExecutionEngine/GenericValue.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetJITInfo.h"
23 using namespace llvm;
24
25 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
26   : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) {
27   setTargetData(TM.getTargetData());
28
29   // Initialize MCE
30   MCE = createEmitter(*this);
31   
32   // Compile LLVM Code down to machine code in the intermediate representation
33   TJI.addPassesToJITCompile(PM);
34
35   // Turn the machine code intermediate representation into bytes in memory that
36   // may be executed.
37   if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
38     std::cerr << "lli: target '" << TM.getName()
39               << "' doesn't support machine code emission!\n";
40     abort();
41   }
42
43   emitGlobals();
44 }
45
46 JIT::~JIT() {
47   delete MCE;
48   delete &TM;
49 }
50
51 /// run - Start execution with the specified function and arguments.
52 ///
53 GenericValue JIT::run(Function *F, const std::vector<GenericValue> &ArgValues) {
54   assert (F && "Function *F was null at entry to run()");
55
56   int (*PF)(int, char **, const char **) =
57     (int(*)(int, char **, const char **))getPointerToFunction(F);
58   assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction");
59
60   // Call the function.
61   int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
62                     (const char **) GVTOP (ArgValues[2]));
63
64   // Run any atexit handlers now!
65   runAtExitHandlers();
66
67   GenericValue rv;
68   rv.IntVal = ExitCode;
69   return rv;
70 }
71
72 /// runJITOnFunction - Run the FunctionPassManager full of
73 /// just-in-time compilation passes on F, hopefully filling in
74 /// GlobalAddress[F] with the address of F's machine code.
75 ///
76 void JIT::runJITOnFunction(Function *F) {
77   static bool isAlreadyCodeGenerating = false;
78   assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
79
80   // JIT the function
81   isAlreadyCodeGenerating = true;
82   PM.run(*F);
83   isAlreadyCodeGenerating = false;
84 }
85
86 /// getPointerToFunction - This method is used to get the address of the
87 /// specified function, compiling it if neccesary.
88 ///
89 void *JIT::getPointerToFunction(Function *F) {
90   void *&Addr = GlobalAddress[F];   // Check if function already code gen'd
91   if (Addr) return Addr;
92
93   // Make sure we read in the function if it exists in this Module
94   MP->materializeFunction(F);
95
96   if (F->isExternal())
97     return Addr = getPointerToNamedFunction(F->getName());
98
99   runJITOnFunction(F);
100   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
101   return Addr;
102 }
103
104 // getPointerToFunctionOrStub - If the specified function has been
105 // code-gen'd, return a pointer to the function.  If not, compile it, or use
106 // a stub to implement lazy compilation if available.
107 //
108 void *JIT::getPointerToFunctionOrStub(Function *F) {
109   // If we have already code generated the function, just return the address.
110   std::map<const GlobalValue*, void *>::iterator I = GlobalAddress.find(F);
111   if (I != GlobalAddress.end()) return I->second;
112
113   // If the target supports "stubs" for functions, get a stub now.
114   if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
115     return Ptr;
116
117   // Otherwise, if the target doesn't support it, just codegen the function.
118   return getPointerToFunction(F);
119 }
120
121 /// recompileAndRelinkFunction - This method is used to force a function
122 /// which has already been compiled, to be compiled again, possibly
123 /// after it has been modified. Then the entry to the old copy is overwritten
124 /// with a branch to the new copy. If there was no old copy, this acts
125 /// just like JIT::getPointerToFunction().
126 ///
127 void *JIT::recompileAndRelinkFunction(Function *F) {
128   void *&Addr = GlobalAddress[F];   // Check if function already code gen'd
129
130   // If it's not already compiled (this is kind of weird) there is no
131   // reason to patch it up.
132   if (!Addr) { return getPointerToFunction (F); }
133
134   void *OldAddr = Addr;
135   Addr = 0;
136   MachineFunction::destruct(F);
137   runJITOnFunction(F);
138   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
139   TJI.replaceMachineCodeForFunction(OldAddr, Addr);
140   return Addr;
141 }