blob: 5340a19e3ed041d902f033dd120a06db85398c49 [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"
Reid Spencer954da372004-07-04 12:19:56 +000026#include <iostream>
27
Chris Lattnerc19aade2003-12-08 08:06:28 +000028using namespace llvm;
Misha Brukmanabb027c2003-05-27 21:40:39 +000029
Chris Lattner4d326fa2003-12-20 01:46:27 +000030JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
Chris Lattner1e60a912003-12-20 01:22:19 +000031 : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000032 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000033
34 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000035 MCE = createEmitter(*this);
Chris Lattner1e60a912003-12-20 01:22:19 +000036
Brian Gaeke50872d52004-04-14 17:45:52 +000037 // Add target data
38 PM.add (new TargetData (TM.getTargetData ()));
39
Chris Lattner4d326fa2003-12-20 01:46:27 +000040 // Compile LLVM Code down to machine code in the intermediate representation
41 TJI.addPassesToJITCompile(PM);
42
43 // Turn the machine code intermediate representation into bytes in memory that
44 // may be executed.
45 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
46 std::cerr << "lli: target '" << TM.getName()
47 << "' doesn't support machine code emission!\n";
48 abort();
49 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000050}
51
Chris Lattner4d326fa2003-12-20 01:46:27 +000052JIT::~JIT() {
53 delete MCE;
54 delete &TM;
55}
56
Brian Gaeke70975ee2003-09-05 18:42:01 +000057/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +000058///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000059GenericValue JIT::runFunction(Function *F,
60 const std::vector<GenericValue> &ArgValues) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000061 assert (F && "Function *F was null at entry to run()");
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000062 GenericValue rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000063
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000064 if (ArgValues.size() == 3) {
65 int (*PF)(int, char **, const char **) =
66 (int(*)(int, char **, const char **))getPointerToFunction(F);
67 assert(PF && "Pointer to fn's code was null after getPointerToFunction");
68
69 // Call the function.
70 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
71 (const char **) GVTOP (ArgValues[2]));
72
73 rv.IntVal = ExitCode;
74 } else {
75 // FIXME: This code should handle a couple of common cases efficiently, but
76 // it should also implement the general case by code-gening a new anonymous
77 // nullary function to call.
78 assert(ArgValues.size() == 1);
79 void (*PF)(int) = (void(*)(int))getPointerToFunction(F);
80 assert(PF && "Pointer to fn's code was null after getPointerToFunction");
81 PF(ArgValues[0].IntVal);
82 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000083
Brian Gaeke70975ee2003-09-05 18:42:01 +000084 return rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000085}
Chris Lattner4d326fa2003-12-20 01:46:27 +000086
87/// runJITOnFunction - Run the FunctionPassManager full of
88/// just-in-time compilation passes on F, hopefully filling in
89/// GlobalAddress[F] with the address of F's machine code.
90///
91void JIT::runJITOnFunction(Function *F) {
92 static bool isAlreadyCodeGenerating = false;
93 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
94
95 // JIT the function
96 isAlreadyCodeGenerating = true;
97 PM.run(*F);
98 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +000099
100 // If the function referred to a global variable that had not yet been
101 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
102 // all of these globals now.
103 while (!PendingGlobals.empty()) {
104 const GlobalVariable *GV = PendingGlobals.back();
105 PendingGlobals.pop_back();
106 EmitGlobalVariable(GV);
107 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000108}
109
110/// getPointerToFunction - This method is used to get the address of the
111/// specified function, compiling it if neccesary.
112///
113void *JIT::getPointerToFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000114 if (void *Addr = getPointerToGlobalIfAvailable(F))
115 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000116
117 // Make sure we read in the function if it exists in this Module
Chris Lattnerda79bd22004-02-01 00:32:35 +0000118 try {
119 MP->materializeFunction(F);
120 } catch (...) {
121 std::cerr << "Error parsing bytecode file!\n";
122 abort();
123 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000124
Chris Lattnerc07ed132003-12-20 03:36:47 +0000125 if (F->isExternal()) {
126 void *Addr = getPointerToNamedFunction(F->getName());
127 addGlobalMapping(F, Addr);
128 return Addr;
129 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000130
131 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000132
133 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000134 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
135 return Addr;
136}
137
138// getPointerToFunctionOrStub - If the specified function has been
139// code-gen'd, return a pointer to the function. If not, compile it, or use
140// a stub to implement lazy compilation if available.
141//
142void *JIT::getPointerToFunctionOrStub(Function *F) {
143 // If we have already code generated the function, just return the address.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000144 if (void *Addr = getPointerToGlobalIfAvailable(F))
145 return Addr;
Chris Lattner4d326fa2003-12-20 01:46:27 +0000146
147 // If the target supports "stubs" for functions, get a stub now.
148 if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
149 return Ptr;
150
151 // Otherwise, if the target doesn't support it, just codegen the function.
152 return getPointerToFunction(F);
153}
154
Chris Lattnerc07ed132003-12-20 03:36:47 +0000155/// getOrEmitGlobalVariable - Return the address of the specified global
156/// variable, possibly emitting it to memory if needed. This is used by the
157/// Emitter.
158void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
159 void *Ptr = getPointerToGlobalIfAvailable(GV);
160 if (Ptr) return Ptr;
161
162 // If the global is external, just remember the address.
163 if (GV->isExternal()) {
164 Ptr = GetAddressOfSymbol(GV->getName().c_str());
165 if (Ptr == 0) {
166 std::cerr << "Could not resolve external global address: "
167 << GV->getName() << "\n";
168 abort();
169 }
170 } else {
171 // If the global hasn't been emitted to memory yet, allocate space. We will
172 // actually initialize the global after current function has finished
173 // compilation.
174 Ptr =new char[getTargetData().getTypeSize(GV->getType()->getElementType())];
175 PendingGlobals.push_back(GV);
176 }
177 addGlobalMapping(GV, Ptr);
178 return Ptr;
179}
180
181
Chris Lattner4d326fa2003-12-20 01:46:27 +0000182/// recompileAndRelinkFunction - This method is used to force a function
183/// which has already been compiled, to be compiled again, possibly
184/// after it has been modified. Then the entry to the old copy is overwritten
185/// with a branch to the new copy. If there was no old copy, this acts
186/// just like JIT::getPointerToFunction().
187///
188void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000189 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000190
Chris Lattnerc07ed132003-12-20 03:36:47 +0000191 // If it's not already compiled there is no reason to patch it up.
192 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000193
Chris Lattnerc07ed132003-12-20 03:36:47 +0000194 // Delete the old function mapping.
195 addGlobalMapping(F, 0);
196
Chris Lattnerc07ed132003-12-20 03:36:47 +0000197 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000198 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000199
200 // Update state, forward the old function to the new function.
201 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000202 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
203 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
204 return Addr;
205}