blob: 586936f21c7607ce8eff893c9ebac43e4c893c5f [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) {
Chris Lattnerb47130c2004-08-15 23:29:50 +000061 assert(F && "Function *F was null at entry to run()");
62 GenericValue rv;
63
64 void *FPtr = getPointerToFunction(F);
Chris Lattner7c45d782004-08-15 23:31:43 +000065 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerbd199fb2002-12-24 00:01:05 +000066
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000067 if (ArgValues.size() == 3) {
68 int (*PF)(int, char **, const char **) =
Chris Lattnerb47130c2004-08-15 23:29:50 +000069 (int(*)(int, char **, const char **))FPtr;
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000070
71 // Call the function.
Chris Lattnerb47130c2004-08-15 23:29:50 +000072 rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]),
73 (const char **)GVTOP(ArgValues[2]));
74 return rv;
75 } else if (ArgValues.size() == 1) {
76 int (*PF)(int) = (int(*)(int))FPtr;
77 rv.IntVal = PF(ArgValues[0].IntVal);
78 return rv;
Chris Lattner7c45d782004-08-15 23:31:43 +000079 } else if (ArgValues.size() == 0) {
80 int (*PF)() = (int(*)())FPtr;
81 rv.IntVal = PF();
82 return rv;
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000083 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000084
Chris Lattnerb47130c2004-08-15 23:29:50 +000085 // FIXME: This code should handle a couple of common cases efficiently, but
86 // it should also implement the general case by code-gening a new anonymous
87 // nullary function to call.
88 std::cerr << "Sorry, unimplemented feature in the LLVM JIT. See LLVM"
89 << " PR#419\n for details.\n";
90 abort();
Brian Gaeke70975ee2003-09-05 18:42:01 +000091 return rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000092}
Chris Lattner4d326fa2003-12-20 01:46:27 +000093
94/// runJITOnFunction - Run the FunctionPassManager full of
95/// just-in-time compilation passes on F, hopefully filling in
96/// GlobalAddress[F] with the address of F's machine code.
97///
98void JIT::runJITOnFunction(Function *F) {
99 static bool isAlreadyCodeGenerating = false;
100 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
101
102 // JIT the function
103 isAlreadyCodeGenerating = true;
104 PM.run(*F);
105 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +0000106
107 // If the function referred to a global variable that had not yet been
108 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
109 // all of these globals now.
110 while (!PendingGlobals.empty()) {
111 const GlobalVariable *GV = PendingGlobals.back();
112 PendingGlobals.pop_back();
113 EmitGlobalVariable(GV);
114 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000115}
116
117/// getPointerToFunction - This method is used to get the address of the
118/// specified function, compiling it if neccesary.
119///
120void *JIT::getPointerToFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000121 if (void *Addr = getPointerToGlobalIfAvailable(F))
122 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000123
124 // Make sure we read in the function if it exists in this Module
Chris Lattnerda79bd22004-02-01 00:32:35 +0000125 try {
126 MP->materializeFunction(F);
Reid Spencere2947532004-07-07 21:01:38 +0000127 } catch ( std::string& errmsg ) {
Reid Spencerf86cafd2004-07-07 21:20:28 +0000128 std::cerr << "Error reading bytecode file: " << errmsg << "\n";
Reid Spencere2947532004-07-07 21:01:38 +0000129 abort();
Chris Lattnerda79bd22004-02-01 00:32:35 +0000130 } catch (...) {
Reid Spencerf86cafd2004-07-07 21:20:28 +0000131 std::cerr << "Error reading bytecode file!\n";
Chris Lattnerda79bd22004-02-01 00:32:35 +0000132 abort();
133 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000134
Chris Lattnerc07ed132003-12-20 03:36:47 +0000135 if (F->isExternal()) {
136 void *Addr = getPointerToNamedFunction(F->getName());
137 addGlobalMapping(F, Addr);
138 return Addr;
139 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000140
141 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000142
143 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000144 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
145 return Addr;
146}
147
148// getPointerToFunctionOrStub - If the specified function has been
149// code-gen'd, return a pointer to the function. If not, compile it, or use
150// a stub to implement lazy compilation if available.
151//
152void *JIT::getPointerToFunctionOrStub(Function *F) {
153 // If we have already code generated the function, just return the address.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000154 if (void *Addr = getPointerToGlobalIfAvailable(F))
155 return Addr;
Chris Lattner4d326fa2003-12-20 01:46:27 +0000156
157 // If the target supports "stubs" for functions, get a stub now.
158 if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
159 return Ptr;
160
161 // Otherwise, if the target doesn't support it, just codegen the function.
162 return getPointerToFunction(F);
163}
164
Chris Lattnerc07ed132003-12-20 03:36:47 +0000165/// getOrEmitGlobalVariable - Return the address of the specified global
166/// variable, possibly emitting it to memory if needed. This is used by the
167/// Emitter.
168void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
169 void *Ptr = getPointerToGlobalIfAvailable(GV);
170 if (Ptr) return Ptr;
171
172 // If the global is external, just remember the address.
173 if (GV->isExternal()) {
174 Ptr = GetAddressOfSymbol(GV->getName().c_str());
175 if (Ptr == 0) {
176 std::cerr << "Could not resolve external global address: "
177 << GV->getName() << "\n";
178 abort();
179 }
180 } else {
181 // If the global hasn't been emitted to memory yet, allocate space. We will
182 // actually initialize the global after current function has finished
183 // compilation.
184 Ptr =new char[getTargetData().getTypeSize(GV->getType()->getElementType())];
185 PendingGlobals.push_back(GV);
186 }
187 addGlobalMapping(GV, Ptr);
188 return Ptr;
189}
190
191
Chris Lattner4d326fa2003-12-20 01:46:27 +0000192/// recompileAndRelinkFunction - This method is used to force a function
193/// which has already been compiled, to be compiled again, possibly
194/// after it has been modified. Then the entry to the old copy is overwritten
195/// with a branch to the new copy. If there was no old copy, this acts
196/// just like JIT::getPointerToFunction().
197///
198void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000199 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000200
Chris Lattnerc07ed132003-12-20 03:36:47 +0000201 // If it's not already compiled there is no reason to patch it up.
202 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000203
Chris Lattnerc07ed132003-12-20 03:36:47 +0000204 // Delete the old function mapping.
205 addGlobalMapping(F, 0);
206
Chris Lattnerc07ed132003-12-20 03:36:47 +0000207 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000208 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000209
210 // Update state, forward the old function to the new function.
211 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000212 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
213 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
214 return Addr;
215}