blob: 3d4bc4c68826c7b6b4e3d07d0fd959357b63a781 [file] [log] [blame]
Chris Lattner4d326fa2003-12-20 01:46:27 +00001//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattnerbd199fb2002-12-24 00:01:05 +00009//
Chris Lattner4d326fa2003-12-20 01:46:27 +000010// This tool implements a just-in-time compiler for LLVM, allowing direct
11// execution of LLVM bytecode in an efficient manner.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4d326fa2003-12-20 01:46:27 +000015#include "JIT.h"
Chris Lattnerc07ed132003-12-20 03:36:47 +000016#include "llvm/DerivedTypes.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000017#include "llvm/Function.h"
Chris Lattnerc07ed132003-12-20 03:36:47 +000018#include "llvm/GlobalVariable.h"
Misha Brukman0f4f7d92003-10-16 21:19:34 +000019#include "llvm/ModuleProvider.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000020#include "llvm/CodeGen/MachineCodeEmitter.h"
21#include "llvm/CodeGen/MachineFunction.h"
Brian Gaeke97222942003-09-05 19:39:22 +000022#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000024#include "llvm/Target/TargetJITInfo.h"
Chris Lattnerc07ed132003-12-20 03:36:47 +000025#include "Support/DynamicLinker.h"
Chris Lattnerc19aade2003-12-08 08:06:28 +000026using namespace llvm;
Misha Brukmanabb027c2003-05-27 21:40:39 +000027
Chris Lattner4d326fa2003-12-20 01:46:27 +000028JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
Chris Lattner1e60a912003-12-20 01:22:19 +000029 : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000030 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000031
32 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000033 MCE = createEmitter(*this);
Chris Lattner1e60a912003-12-20 01:22:19 +000034
Brian Gaeke50872d52004-04-14 17:45:52 +000035 // Add target data
36 PM.add (new TargetData (TM.getTargetData ()));
37
Chris Lattner4d326fa2003-12-20 01:46:27 +000038 // Compile LLVM Code down to machine code in the intermediate representation
39 TJI.addPassesToJITCompile(PM);
40
41 // Turn the machine code intermediate representation into bytes in memory that
42 // may be executed.
43 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
44 std::cerr << "lli: target '" << TM.getName()
45 << "' doesn't support machine code emission!\n";
46 abort();
47 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000048}
49
Chris Lattner4d326fa2003-12-20 01:46:27 +000050JIT::~JIT() {
51 delete MCE;
52 delete &TM;
53}
54
Brian Gaeke70975ee2003-09-05 18:42:01 +000055/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +000056///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000057GenericValue JIT::runFunction(Function *F,
58 const std::vector<GenericValue> &ArgValues) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000059 assert (F && "Function *F was null at entry to run()");
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000060 GenericValue rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000061
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000062 if (ArgValues.size() == 3) {
63 int (*PF)(int, char **, const char **) =
64 (int(*)(int, char **, const char **))getPointerToFunction(F);
65 assert(PF && "Pointer to fn's code was null after getPointerToFunction");
66
67 // Call the function.
68 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
69 (const char **) GVTOP (ArgValues[2]));
70
71 rv.IntVal = ExitCode;
72 } else {
73 // FIXME: This code should handle a couple of common cases efficiently, but
74 // it should also implement the general case by code-gening a new anonymous
75 // nullary function to call.
76 assert(ArgValues.size() == 1);
77 void (*PF)(int) = (void(*)(int))getPointerToFunction(F);
78 assert(PF && "Pointer to fn's code was null after getPointerToFunction");
79 PF(ArgValues[0].IntVal);
80 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000081
Brian Gaeke70975ee2003-09-05 18:42:01 +000082 return rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000083}
Chris Lattner4d326fa2003-12-20 01:46:27 +000084
85/// runJITOnFunction - Run the FunctionPassManager full of
86/// just-in-time compilation passes on F, hopefully filling in
87/// GlobalAddress[F] with the address of F's machine code.
88///
89void JIT::runJITOnFunction(Function *F) {
90 static bool isAlreadyCodeGenerating = false;
91 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
92
93 // JIT the function
94 isAlreadyCodeGenerating = true;
95 PM.run(*F);
96 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +000097
98 // If the function referred to a global variable that had not yet been
99 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
100 // all of these globals now.
101 while (!PendingGlobals.empty()) {
102 const GlobalVariable *GV = PendingGlobals.back();
103 PendingGlobals.pop_back();
104 EmitGlobalVariable(GV);
105 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000106}
107
108/// getPointerToFunction - This method is used to get the address of the
109/// specified function, compiling it if neccesary.
110///
111void *JIT::getPointerToFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000112 if (void *Addr = getPointerToGlobalIfAvailable(F))
113 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000114
115 // Make sure we read in the function if it exists in this Module
Chris Lattnerda79bd22004-02-01 00:32:35 +0000116 try {
117 MP->materializeFunction(F);
118 } catch (...) {
119 std::cerr << "Error parsing bytecode file!\n";
120 abort();
121 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000122
Chris Lattnerc07ed132003-12-20 03:36:47 +0000123 if (F->isExternal()) {
124 void *Addr = getPointerToNamedFunction(F->getName());
125 addGlobalMapping(F, Addr);
126 return Addr;
127 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000128
129 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000130
131 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000132 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
133 return Addr;
134}
135
136// getPointerToFunctionOrStub - If the specified function has been
137// code-gen'd, return a pointer to the function. If not, compile it, or use
138// a stub to implement lazy compilation if available.
139//
140void *JIT::getPointerToFunctionOrStub(Function *F) {
141 // If we have already code generated the function, just return the address.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000142 if (void *Addr = getPointerToGlobalIfAvailable(F))
143 return Addr;
Chris Lattner4d326fa2003-12-20 01:46:27 +0000144
145 // If the target supports "stubs" for functions, get a stub now.
146 if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
147 return Ptr;
148
149 // Otherwise, if the target doesn't support it, just codegen the function.
150 return getPointerToFunction(F);
151}
152
Chris Lattnerc07ed132003-12-20 03:36:47 +0000153/// getOrEmitGlobalVariable - Return the address of the specified global
154/// variable, possibly emitting it to memory if needed. This is used by the
155/// Emitter.
156void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
157 void *Ptr = getPointerToGlobalIfAvailable(GV);
158 if (Ptr) return Ptr;
159
160 // If the global is external, just remember the address.
161 if (GV->isExternal()) {
162 Ptr = GetAddressOfSymbol(GV->getName().c_str());
163 if (Ptr == 0) {
164 std::cerr << "Could not resolve external global address: "
165 << GV->getName() << "\n";
166 abort();
167 }
168 } else {
169 // If the global hasn't been emitted to memory yet, allocate space. We will
170 // actually initialize the global after current function has finished
171 // compilation.
172 Ptr =new char[getTargetData().getTypeSize(GV->getType()->getElementType())];
173 PendingGlobals.push_back(GV);
174 }
175 addGlobalMapping(GV, Ptr);
176 return Ptr;
177}
178
179
Chris Lattner4d326fa2003-12-20 01:46:27 +0000180/// recompileAndRelinkFunction - This method is used to force a function
181/// which has already been compiled, to be compiled again, possibly
182/// after it has been modified. Then the entry to the old copy is overwritten
183/// with a branch to the new copy. If there was no old copy, this acts
184/// just like JIT::getPointerToFunction().
185///
186void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000187 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000188
Chris Lattnerc07ed132003-12-20 03:36:47 +0000189 // If it's not already compiled there is no reason to patch it up.
190 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000191
Chris Lattnerc07ed132003-12-20 03:36:47 +0000192 // Delete the old function mapping.
193 addGlobalMapping(F, 0);
194
Chris Lattnerc07ed132003-12-20 03:36:47 +0000195 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000196 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000197
198 // Update state, forward the old function to the new function.
199 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000200 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
201 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
202 return Addr;
203}