1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This tool implements a just-in-time compiler for LLVM, allowing direct
11 // execution of LLVM bytecode in an efficient manner.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/ModuleProvider.h"
20 #include "llvm/CodeGen/MachineCodeEmitter.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/ExecutionEngine/GenericValue.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetJITInfo.h"
25 #include "Support/DynamicLinker.h"
28 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
29 : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) {
30 setTargetData(TM.getTargetData());
33 MCE = createEmitter(*this);
35 // Compile LLVM Code down to machine code in the intermediate representation
36 TJI.addPassesToJITCompile(PM);
38 // Turn the machine code intermediate representation into bytes in memory that
40 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
41 std::cerr << "lli: target '" << TM.getName()
42 << "' doesn't support machine code emission!\n";
52 /// run - Start execution with the specified function and arguments.
54 GenericValue JIT::runFunction(Function *F,
55 const std::vector<GenericValue> &ArgValues) {
56 assert (F && "Function *F was null at entry to run()");
59 if (ArgValues.size() == 3) {
60 int (*PF)(int, char **, const char **) =
61 (int(*)(int, char **, const char **))getPointerToFunction(F);
62 assert(PF && "Pointer to fn's code was null after getPointerToFunction");
65 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
66 (const char **) GVTOP (ArgValues[2]));
70 // FIXME: This code should handle a couple of common cases efficiently, but
71 // it should also implement the general case by code-gening a new anonymous
72 // nullary function to call.
73 assert(ArgValues.size() == 1);
74 void (*PF)(int) = (void(*)(int))getPointerToFunction(F);
75 assert(PF && "Pointer to fn's code was null after getPointerToFunction");
76 PF(ArgValues[0].IntVal);
82 /// runJITOnFunction - Run the FunctionPassManager full of
83 /// just-in-time compilation passes on F, hopefully filling in
84 /// GlobalAddress[F] with the address of F's machine code.
86 void JIT::runJITOnFunction(Function *F) {
87 static bool isAlreadyCodeGenerating = false;
88 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
91 isAlreadyCodeGenerating = true;
93 isAlreadyCodeGenerating = false;
95 // If the function referred to a global variable that had not yet been
96 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
97 // all of these globals now.
98 while (!PendingGlobals.empty()) {
99 const GlobalVariable *GV = PendingGlobals.back();
100 PendingGlobals.pop_back();
101 EmitGlobalVariable(GV);
105 /// getPointerToFunction - This method is used to get the address of the
106 /// specified function, compiling it if neccesary.
108 void *JIT::getPointerToFunction(Function *F) {
109 if (void *Addr = getPointerToGlobalIfAvailable(F))
110 return Addr; // Check if function already code gen'd
112 // Make sure we read in the function if it exists in this Module
114 MP->materializeFunction(F);
116 std::cerr << "Error parsing bytecode file!\n";
120 if (F->isExternal()) {
121 void *Addr = getPointerToNamedFunction(F->getName());
122 addGlobalMapping(F, Addr);
128 void *Addr = getPointerToGlobalIfAvailable(F);
129 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
133 // getPointerToFunctionOrStub - If the specified function has been
134 // code-gen'd, return a pointer to the function. If not, compile it, or use
135 // a stub to implement lazy compilation if available.
137 void *JIT::getPointerToFunctionOrStub(Function *F) {
138 // If we have already code generated the function, just return the address.
139 if (void *Addr = getPointerToGlobalIfAvailable(F))
142 // If the target supports "stubs" for functions, get a stub now.
143 if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
146 // Otherwise, if the target doesn't support it, just codegen the function.
147 return getPointerToFunction(F);
150 /// getOrEmitGlobalVariable - Return the address of the specified global
151 /// variable, possibly emitting it to memory if needed. This is used by the
153 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
154 void *Ptr = getPointerToGlobalIfAvailable(GV);
157 // If the global is external, just remember the address.
158 if (GV->isExternal()) {
159 Ptr = GetAddressOfSymbol(GV->getName().c_str());
161 std::cerr << "Could not resolve external global address: "
162 << GV->getName() << "\n";
166 // If the global hasn't been emitted to memory yet, allocate space. We will
167 // actually initialize the global after current function has finished
169 Ptr =new char[getTargetData().getTypeSize(GV->getType()->getElementType())];
170 PendingGlobals.push_back(GV);
172 addGlobalMapping(GV, Ptr);
177 /// recompileAndRelinkFunction - This method is used to force a function
178 /// which has already been compiled, to be compiled again, possibly
179 /// after it has been modified. Then the entry to the old copy is overwritten
180 /// with a branch to the new copy. If there was no old copy, this acts
181 /// just like JIT::getPointerToFunction().
183 void *JIT::recompileAndRelinkFunction(Function *F) {
184 void *OldAddr = getPointerToGlobalIfAvailable(F);
186 // If it's not already compiled there is no reason to patch it up.
187 if (OldAddr == 0) { return getPointerToFunction(F); }
189 // Delete the old function mapping.
190 addGlobalMapping(F, 0);
192 // Recodegen the function
195 // Update state, forward the old function to the new function.
196 void *Addr = getPointerToGlobalIfAvailable(F);
197 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
198 TJI.replaceMachineCodeForFunction(OldAddr, Addr);