blob: 4426c67ef8fb7f2c1d0a372a6596e7fe322e430b [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
Chris Lattner4d326fa2003-12-20 01:46:27 +000035 // Compile LLVM Code down to machine code in the intermediate representation
36 TJI.addPassesToJITCompile(PM);
37
38 // Turn the machine code intermediate representation into bytes in memory that
39 // may be executed.
40 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
41 std::cerr << "lli: target '" << TM.getName()
42 << "' doesn't support machine code emission!\n";
43 abort();
44 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000045}
46
Chris Lattner4d326fa2003-12-20 01:46:27 +000047JIT::~JIT() {
48 delete MCE;
49 delete &TM;
50}
51
Brian Gaeke70975ee2003-09-05 18:42:01 +000052/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +000053///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000054GenericValue JIT::runFunction(Function *F,
55 const std::vector<GenericValue> &ArgValues) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000056 assert (F && "Function *F was null at entry to run()");
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000057 GenericValue rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000058
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000059 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");
63
64 // Call the function.
65 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
66 (const char **) GVTOP (ArgValues[2]));
67
68 rv.IntVal = ExitCode;
69 } else {
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);
77 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000078
Brian Gaeke70975ee2003-09-05 18:42:01 +000079 return rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000080}
Chris Lattner4d326fa2003-12-20 01:46:27 +000081
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.
85///
86void JIT::runJITOnFunction(Function *F) {
87 static bool isAlreadyCodeGenerating = false;
88 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
89
90 // JIT the function
91 isAlreadyCodeGenerating = true;
92 PM.run(*F);
93 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +000094
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);
102 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000103}
104
105/// getPointerToFunction - This method is used to get the address of the
106/// specified function, compiling it if neccesary.
107///
108void *JIT::getPointerToFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000109 if (void *Addr = getPointerToGlobalIfAvailable(F))
110 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000111
112 // Make sure we read in the function if it exists in this Module
Chris Lattnerda79bd22004-02-01 00:32:35 +0000113 try {
114 MP->materializeFunction(F);
115 } catch (...) {
116 std::cerr << "Error parsing bytecode file!\n";
117 abort();
118 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000119
Chris Lattnerc07ed132003-12-20 03:36:47 +0000120 if (F->isExternal()) {
121 void *Addr = getPointerToNamedFunction(F->getName());
122 addGlobalMapping(F, Addr);
123 return Addr;
124 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000125
126 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000127
128 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000129 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
130 return Addr;
131}
132
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.
136//
137void *JIT::getPointerToFunctionOrStub(Function *F) {
138 // If we have already code generated the function, just return the address.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000139 if (void *Addr = getPointerToGlobalIfAvailable(F))
140 return Addr;
Chris Lattner4d326fa2003-12-20 01:46:27 +0000141
142 // If the target supports "stubs" for functions, get a stub now.
143 if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
144 return Ptr;
145
146 // Otherwise, if the target doesn't support it, just codegen the function.
147 return getPointerToFunction(F);
148}
149
Chris Lattnerc07ed132003-12-20 03:36:47 +0000150/// getOrEmitGlobalVariable - Return the address of the specified global
151/// variable, possibly emitting it to memory if needed. This is used by the
152/// Emitter.
153void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
154 void *Ptr = getPointerToGlobalIfAvailable(GV);
155 if (Ptr) return Ptr;
156
157 // If the global is external, just remember the address.
158 if (GV->isExternal()) {
159 Ptr = GetAddressOfSymbol(GV->getName().c_str());
160 if (Ptr == 0) {
161 std::cerr << "Could not resolve external global address: "
162 << GV->getName() << "\n";
163 abort();
164 }
165 } else {
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
168 // compilation.
169 Ptr =new char[getTargetData().getTypeSize(GV->getType()->getElementType())];
170 PendingGlobals.push_back(GV);
171 }
172 addGlobalMapping(GV, Ptr);
173 return Ptr;
174}
175
176
Chris Lattner4d326fa2003-12-20 01:46:27 +0000177/// 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().
182///
183void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000184 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000185
Chris Lattnerc07ed132003-12-20 03:36:47 +0000186 // If it's not already compiled there is no reason to patch it up.
187 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000188
Chris Lattnerc07ed132003-12-20 03:36:47 +0000189 // Delete the old function mapping.
190 addGlobalMapping(F, 0);
191
Chris Lattnerc07ed132003-12-20 03:36:47 +0000192 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000193 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000194
195 // Update state, forward the old function to the new function.
196 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000197 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
198 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
199 return Addr;
200}