blob: ab362a64ba11d316d82cf1c00756a540761c8ccb [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 Lattner4d326fa2003-12-20 01:46:27 +000054GenericValue JIT::run(Function *F, const std::vector<GenericValue> &ArgValues) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000055 assert (F && "Function *F was null at entry to run()");
Chris Lattnerbd199fb2002-12-24 00:01:05 +000056
Brian Gaeke70975ee2003-09-05 18:42:01 +000057 int (*PF)(int, char **, const char **) =
58 (int(*)(int, char **, const char **))getPointerToFunction(F);
59 assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerbd199fb2002-12-24 00:01:05 +000060
Brian Gaeke70975ee2003-09-05 18:42:01 +000061 // Call the function.
62 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
63 (const char **) GVTOP (ArgValues[2]));
Chris Lattner22080f92003-05-14 13:53:40 +000064
65 // Run any atexit handlers now!
66 runAtExitHandlers();
Brian Gaeke70975ee2003-09-05 18:42:01 +000067
68 GenericValue rv;
69 rv.IntVal = ExitCode;
70 return rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000071}
Chris Lattner4d326fa2003-12-20 01:46:27 +000072
73/// runJITOnFunction - Run the FunctionPassManager full of
74/// just-in-time compilation passes on F, hopefully filling in
75/// GlobalAddress[F] with the address of F's machine code.
76///
77void JIT::runJITOnFunction(Function *F) {
78 static bool isAlreadyCodeGenerating = false;
79 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
80
81 // JIT the function
82 isAlreadyCodeGenerating = true;
83 PM.run(*F);
84 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +000085
86 // If the function referred to a global variable that had not yet been
87 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
88 // all of these globals now.
89 while (!PendingGlobals.empty()) {
90 const GlobalVariable *GV = PendingGlobals.back();
91 PendingGlobals.pop_back();
92 EmitGlobalVariable(GV);
93 }
Chris Lattner4d326fa2003-12-20 01:46:27 +000094}
95
96/// getPointerToFunction - This method is used to get the address of the
97/// specified function, compiling it if neccesary.
98///
99void *JIT::getPointerToFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000100 if (void *Addr = getPointerToGlobalIfAvailable(F))
101 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000102
103 // Make sure we read in the function if it exists in this Module
104 MP->materializeFunction(F);
105
Chris Lattnerc07ed132003-12-20 03:36:47 +0000106 if (F->isExternal()) {
107 void *Addr = getPointerToNamedFunction(F->getName());
108 addGlobalMapping(F, Addr);
109 return Addr;
110 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000111
112 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000113
114 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000115 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
116 return Addr;
117}
118
119// getPointerToFunctionOrStub - If the specified function has been
120// code-gen'd, return a pointer to the function. If not, compile it, or use
121// a stub to implement lazy compilation if available.
122//
123void *JIT::getPointerToFunctionOrStub(Function *F) {
124 // If we have already code generated the function, just return the address.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000125 if (void *Addr = getPointerToGlobalIfAvailable(F))
126 return Addr;
Chris Lattner4d326fa2003-12-20 01:46:27 +0000127
128 // If the target supports "stubs" for functions, get a stub now.
129 if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
130 return Ptr;
131
132 // Otherwise, if the target doesn't support it, just codegen the function.
133 return getPointerToFunction(F);
134}
135
Chris Lattnerc07ed132003-12-20 03:36:47 +0000136/// getOrEmitGlobalVariable - Return the address of the specified global
137/// variable, possibly emitting it to memory if needed. This is used by the
138/// Emitter.
139void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
140 void *Ptr = getPointerToGlobalIfAvailable(GV);
141 if (Ptr) return Ptr;
142
143 // If the global is external, just remember the address.
144 if (GV->isExternal()) {
145 Ptr = GetAddressOfSymbol(GV->getName().c_str());
146 if (Ptr == 0) {
147 std::cerr << "Could not resolve external global address: "
148 << GV->getName() << "\n";
149 abort();
150 }
151 } else {
152 // If the global hasn't been emitted to memory yet, allocate space. We will
153 // actually initialize the global after current function has finished
154 // compilation.
155 Ptr =new char[getTargetData().getTypeSize(GV->getType()->getElementType())];
156 PendingGlobals.push_back(GV);
157 }
158 addGlobalMapping(GV, Ptr);
159 return Ptr;
160}
161
162
Chris Lattner4d326fa2003-12-20 01:46:27 +0000163/// recompileAndRelinkFunction - This method is used to force a function
164/// which has already been compiled, to be compiled again, possibly
165/// after it has been modified. Then the entry to the old copy is overwritten
166/// with a branch to the new copy. If there was no old copy, this acts
167/// just like JIT::getPointerToFunction().
168///
169void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000170 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000171
Chris Lattnerc07ed132003-12-20 03:36:47 +0000172 // If it's not already compiled there is no reason to patch it up.
173 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000174
Chris Lattnerc07ed132003-12-20 03:36:47 +0000175 // Delete the old function mapping.
176 addGlobalMapping(F, 0);
177
178 // Destroy the machine code for this function. FIXME: this should be
179 // incorporated into the code generator!
Chris Lattner4d326fa2003-12-20 01:46:27 +0000180 MachineFunction::destruct(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000181
182 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000183 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000184
185 // Update state, forward the old function to the new function.
186 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000187 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
188 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
189 return Addr;
190}