blob: 649edbed7a4bf3607ee71471cceedcdef9db02a7 [file] [log] [blame]
Chris Lattner4d326fa2003-12-20 01:46:27 +00001//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattnercc22e9f2004-08-16 00:14:18 +000016#include "llvm/Constants.h"
Chris Lattnerc07ed132003-12-20 03:36:47 +000017#include "llvm/DerivedTypes.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000018#include "llvm/Function.h"
Chris Lattnerc07ed132003-12-20 03:36:47 +000019#include "llvm/GlobalVariable.h"
Chris Lattnercc22e9f2004-08-16 00:14:18 +000020#include "llvm/Instructions.h"
Misha Brukman0f4f7d92003-10-16 21:19:34 +000021#include "llvm/ModuleProvider.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000022#include "llvm/CodeGen/MachineCodeEmitter.h"
23#include "llvm/CodeGen/MachineFunction.h"
Brian Gaeke97222942003-09-05 19:39:22 +000024#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnere7fd5532006-05-08 22:00:52 +000025#include "llvm/Support/MutexGuard.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000026#include "llvm/System/DynamicLibrary.h"
Owen Anderson07000c62006-05-12 06:33:49 +000027#include "llvm/Target/TargetData.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000029#include "llvm/Target/TargetJITInfo.h"
Reid Spencer954da372004-07-04 12:19:56 +000030#include <iostream>
Chris Lattnerc19aade2003-12-08 08:06:28 +000031using namespace llvm;
Misha Brukmanabb027c2003-05-27 21:40:39 +000032
Nate Begemanb76ea742006-07-22 16:59:38 +000033#ifdef __APPLE__
34#include <AvailabilityMacros.h>
Evan Cheng78050d62006-09-01 18:42:59 +000035#if (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
Evan Chengb5aaee02006-09-01 18:45:22 +000036 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
Evan Cheng78050d62006-09-01 18:42:59 +000037 __APPLE_CC__ >= 5330)
Evan Cheng5f42c552006-07-21 23:06:20 +000038// __dso_handle is resolved by Mac OS X dynamic linker.
39extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
40#endif
Nate Begemanb76ea742006-07-22 16:59:38 +000041#endif
Evan Cheng5f42c552006-07-21 23:06:20 +000042
Chris Lattner2fe4bb02006-03-22 06:07:50 +000043static struct RegisterJIT {
44 RegisterJIT() { JIT::Register(); }
45} JITRegistrator;
46
Jeff Cohen2f519142006-03-24 02:53:49 +000047namespace llvm {
48 void LinkInJIT() {
49 }
50}
51
Chris Lattner4d326fa2003-12-20 01:46:27 +000052JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
Reid Spenceree448632005-07-12 15:51:55 +000053 : ExecutionEngine(MP), TM(tm), TJI(tji), state(MP) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000054 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000055
56 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000057 MCE = createEmitter(*this);
Misha Brukmanf976c852005-04-21 22:55:34 +000058
Brian Gaeke50872d52004-04-14 17:45:52 +000059 // Add target data
Reid Spenceree448632005-07-12 15:51:55 +000060 MutexGuard locked(lock);
61 FunctionPassManager& PM = state.getPM(locked);
Chris Lattnerb93b0342006-05-04 21:18:40 +000062 PM.add(new TargetData(*TM.getTargetData()));
Brian Gaeke50872d52004-04-14 17:45:52 +000063
Chris Lattner4d326fa2003-12-20 01:46:27 +000064 // Compile LLVM Code down to machine code in the intermediate representation
65 TJI.addPassesToJITCompile(PM);
66
67 // Turn the machine code intermediate representation into bytes in memory that
68 // may be executed.
69 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
Chris Lattnercc22e9f2004-08-16 00:14:18 +000070 std::cerr << "Target '" << TM.getName()
Chris Lattner4d326fa2003-12-20 01:46:27 +000071 << "' doesn't support machine code emission!\n";
72 abort();
73 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000074}
75
Chris Lattner4d326fa2003-12-20 01:46:27 +000076JIT::~JIT() {
77 delete MCE;
78 delete &TM;
79}
80
Brian Gaeke70975ee2003-09-05 18:42:01 +000081/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +000082///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000083GenericValue JIT::runFunction(Function *F,
84 const std::vector<GenericValue> &ArgValues) {
Chris Lattnerb47130c2004-08-15 23:29:50 +000085 assert(F && "Function *F was null at entry to run()");
Chris Lattnerb47130c2004-08-15 23:29:50 +000086
87 void *FPtr = getPointerToFunction(F);
Chris Lattner7c45d782004-08-15 23:31:43 +000088 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerf7bedf42004-08-15 23:39:59 +000089 const FunctionType *FTy = F->getFunctionType();
Chris Lattnere5eab142004-08-15 23:53:06 +000090 const Type *RetTy = FTy->getReturnType();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000091
Chris Lattnere5eab142004-08-15 23:53:06 +000092 assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
93 "Too many arguments passed into function!");
94 assert(FTy->getNumParams() == ArgValues.size() &&
95 "This doesn't support passing arguments through varargs (yet)!");
96
Misha Brukmanec843022004-10-22 23:35:57 +000097 // Handle some common cases first. These cases correspond to common `main'
Chris Lattnere5eab142004-08-15 23:53:06 +000098 // prototypes.
Chris Lattnerd297aea2004-08-15 23:34:48 +000099 if (RetTy == Type::IntTy || RetTy == Type::UIntTy || RetTy == Type::VoidTy) {
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000100 switch (ArgValues.size()) {
101 case 3:
Misha Brukmanf976c852005-04-21 22:55:34 +0000102 if ((FTy->getParamType(0) == Type::IntTy ||
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000103 FTy->getParamType(0) == Type::UIntTy) &&
104 isa<PointerType>(FTy->getParamType(1)) &&
105 isa<PointerType>(FTy->getParamType(2))) {
106 int (*PF)(int, char **, const char **) =
Chris Lattner870286a2006-06-01 17:29:22 +0000107 (int(*)(int, char **, const char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000108
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000109 // Call the function.
Chris Lattnere5eab142004-08-15 23:53:06 +0000110 GenericValue rv;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000111 rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]),
112 (const char **)GVTOP(ArgValues[2]));
113 return rv;
114 }
115 break;
Chris Lattner174f2262004-08-16 01:07:04 +0000116 case 2:
Misha Brukmanf976c852005-04-21 22:55:34 +0000117 if ((FTy->getParamType(0) == Type::IntTy ||
Chris Lattner174f2262004-08-16 01:07:04 +0000118 FTy->getParamType(0) == Type::UIntTy) &&
119 isa<PointerType>(FTy->getParamType(1))) {
Chris Lattner870286a2006-06-01 17:29:22 +0000120 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000121
Chris Lattner174f2262004-08-16 01:07:04 +0000122 // Call the function.
123 GenericValue rv;
124 rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]));
125 return rv;
126 }
127 break;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000128 case 1:
129 if (FTy->getNumParams() == 1 &&
Misha Brukmanf976c852005-04-21 22:55:34 +0000130 (FTy->getParamType(0) == Type::IntTy ||
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000131 FTy->getParamType(0) == Type::UIntTy)) {
Chris Lattnere5eab142004-08-15 23:53:06 +0000132 GenericValue rv;
Chris Lattner870286a2006-06-01 17:29:22 +0000133 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000134 rv.IntVal = PF(ArgValues[0].IntVal);
135 return rv;
136 }
137 break;
Chris Lattnere5eab142004-08-15 23:53:06 +0000138 }
139 }
140
141 // Handle cases where no arguments are passed first.
142 if (ArgValues.empty()) {
143 GenericValue rv;
144 switch (RetTy->getTypeID()) {
145 default: assert(0 && "Unknown return type for function call!");
146 case Type::BoolTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000147 rv.BoolVal = ((bool(*)())(intptr_t)FPtr)();
Chris Lattnerd297aea2004-08-15 23:34:48 +0000148 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000149 case Type::SByteTyID:
150 case Type::UByteTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000151 rv.SByteVal = ((char(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000152 return rv;
153 case Type::ShortTyID:
154 case Type::UShortTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000155 rv.ShortVal = ((short(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000156 return rv;
157 case Type::VoidTyID:
158 case Type::IntTyID:
159 case Type::UIntTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000160 rv.IntVal = ((int(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000161 return rv;
162 case Type::LongTyID:
163 case Type::ULongTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000164 rv.LongVal = ((int64_t(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000165 return rv;
166 case Type::FloatTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000167 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000168 return rv;
169 case Type::DoubleTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000170 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000171 return rv;
172 case Type::PointerTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000173 return PTOGV(((void*(*)())(intptr_t)FPtr)());
Chris Lattnerd297aea2004-08-15 23:34:48 +0000174 }
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000175 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000176
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000177 // Okay, this is not one of our quick and easy cases. Because we don't have a
178 // full FFI, we have to codegen a nullary stub function that just calls the
179 // function we are interested in, passing in constants for all of the
180 // arguments. Make this function and return.
181
182 // First, create the function.
183 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
184 Function *Stub = new Function(STy, Function::InternalLinkage, "",
185 F->getParent());
186
187 // Insert a basic block.
188 BasicBlock *StubBB = new BasicBlock("", Stub);
189
190 // Convert all of the GenericValue arguments over to constants. Note that we
191 // currently don't support varargs.
192 std::vector<Value*> Args;
193 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
194 Constant *C = 0;
195 const Type *ArgTy = FTy->getParamType(i);
196 const GenericValue &AV = ArgValues[i];
197 switch (ArgTy->getTypeID()) {
198 default: assert(0 && "Unknown argument type for function call!");
199 case Type::BoolTyID: C = ConstantBool::get(AV.BoolVal); break;
200 case Type::SByteTyID: C = ConstantSInt::get(ArgTy, AV.SByteVal); break;
201 case Type::UByteTyID: C = ConstantUInt::get(ArgTy, AV.UByteVal); break;
202 case Type::ShortTyID: C = ConstantSInt::get(ArgTy, AV.ShortVal); break;
203 case Type::UShortTyID: C = ConstantUInt::get(ArgTy, AV.UShortVal); break;
204 case Type::IntTyID: C = ConstantSInt::get(ArgTy, AV.IntVal); break;
205 case Type::UIntTyID: C = ConstantUInt::get(ArgTy, AV.UIntVal); break;
206 case Type::LongTyID: C = ConstantSInt::get(ArgTy, AV.LongVal); break;
207 case Type::ULongTyID: C = ConstantUInt::get(ArgTy, AV.ULongVal); break;
208 case Type::FloatTyID: C = ConstantFP ::get(ArgTy, AV.FloatVal); break;
209 case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, AV.DoubleVal); break;
210 case Type::PointerTyID:
211 void *ArgPtr = GVTOP(AV);
212 if (sizeof(void*) == 4) {
213 C = ConstantSInt::get(Type::IntTy, (int)(intptr_t)ArgPtr);
214 } else {
215 C = ConstantSInt::get(Type::LongTy, (intptr_t)ArgPtr);
216 }
217 C = ConstantExpr::getCast(C, ArgTy); // Cast the integer to pointer
218 break;
219 }
220 Args.push_back(C);
221 }
222
Chris Lattnera471e042005-05-06 06:48:54 +0000223 CallInst *TheCall = new CallInst(F, Args, "", StubBB);
224 TheCall->setTailCall();
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000225 if (TheCall->getType() != Type::VoidTy)
226 new ReturnInst(TheCall, StubBB); // Return result of the call.
227 else
228 new ReturnInst(StubBB); // Just return void.
229
230 // Finally, return the value returned by our nullary stub function.
231 return runFunction(Stub, std::vector<GenericValue>());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000232}
Chris Lattner4d326fa2003-12-20 01:46:27 +0000233
234/// runJITOnFunction - Run the FunctionPassManager full of
235/// just-in-time compilation passes on F, hopefully filling in
236/// GlobalAddress[F] with the address of F's machine code.
237///
238void JIT::runJITOnFunction(Function *F) {
239 static bool isAlreadyCodeGenerating = false;
240 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000241
Reid Spenceree448632005-07-12 15:51:55 +0000242 MutexGuard locked(lock);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000243
244 // JIT the function
245 isAlreadyCodeGenerating = true;
Reid Spenceree448632005-07-12 15:51:55 +0000246 state.getPM(locked).run(*F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000247 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +0000248
249 // If the function referred to a global variable that had not yet been
250 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
251 // all of these globals now.
Reid Spenceree448632005-07-12 15:51:55 +0000252 while (!state.getPendingGlobals(locked).empty()) {
253 const GlobalVariable *GV = state.getPendingGlobals(locked).back();
254 state.getPendingGlobals(locked).pop_back();
Chris Lattnerc07ed132003-12-20 03:36:47 +0000255 EmitGlobalVariable(GV);
256 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000257}
258
259/// getPointerToFunction - This method is used to get the address of the
260/// specified function, compiling it if neccesary.
261///
262void *JIT::getPointerToFunction(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000263 MutexGuard locked(lock);
264
Chris Lattnerc07ed132003-12-20 03:36:47 +0000265 if (void *Addr = getPointerToGlobalIfAvailable(F))
266 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000267
Chris Lattnerfe854032006-08-16 01:24:12 +0000268 // Make sure we read in the function if it exists in this Module.
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000269 if (F->hasNotBeenReadFromBytecode()) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000270 // Determine the module provider this function is provided by.
271 Module *M = F->getParent();
272 ModuleProvider *MP = 0;
273 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
274 if (Modules[i]->getModule() == M) {
275 MP = Modules[i];
276 break;
277 }
278 }
279 assert(MP && "Function isn't in a module we know about!");
280
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000281 std::string ErrorMsg;
282 if (MP->materializeFunction(F, &ErrorMsg)) {
Chris Lattner0050ef82004-11-15 23:18:09 +0000283 std::cerr << "Error reading function '" << F->getName()
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000284 << "' from bytecode file: " << ErrorMsg << "\n";
Chris Lattner0050ef82004-11-15 23:18:09 +0000285 abort();
286 }
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000287 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000288
Chris Lattnerc07ed132003-12-20 03:36:47 +0000289 if (F->isExternal()) {
290 void *Addr = getPointerToNamedFunction(F->getName());
291 addGlobalMapping(F, Addr);
292 return Addr;
293 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000294
295 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000296
297 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000298 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
299 return Addr;
300}
301
Chris Lattnerc07ed132003-12-20 03:36:47 +0000302/// getOrEmitGlobalVariable - Return the address of the specified global
303/// variable, possibly emitting it to memory if needed. This is used by the
304/// Emitter.
305void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
Reid Spenceree448632005-07-12 15:51:55 +0000306 MutexGuard locked(lock);
307
Chris Lattnerc07ed132003-12-20 03:36:47 +0000308 void *Ptr = getPointerToGlobalIfAvailable(GV);
309 if (Ptr) return Ptr;
310
311 // If the global is external, just remember the address.
312 if (GV->isExternal()) {
Evan Chengb82ab942006-07-22 00:42:03 +0000313#ifdef __APPLE__
Evan Cheng78050d62006-09-01 18:42:59 +0000314#if (MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
Evan Chengb5aaee02006-09-01 18:45:22 +0000315 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
Evan Cheng78050d62006-09-01 18:42:59 +0000316 __APPLE_CC__ >= 5330)
Evan Chengfb9c0d72006-09-01 07:09:56 +0000317 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
318 // of atexit). It passes the address of linker generated symbol __dso_handle
319 // to the function.
320 // This configuration change happened at version 5330.
Evan Cheng5f42c552006-07-21 23:06:20 +0000321 if (GV->getName() == "__dso_handle")
322 return (void*)&__dso_handle;
Evan Chengb82ab942006-07-22 00:42:03 +0000323#endif
Nate Begemanb76ea742006-07-22 16:59:38 +0000324#endif
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000325 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
Chris Lattnerc07ed132003-12-20 03:36:47 +0000326 if (Ptr == 0) {
327 std::cerr << "Could not resolve external global address: "
328 << GV->getName() << "\n";
329 abort();
330 }
331 } else {
332 // If the global hasn't been emitted to memory yet, allocate space. We will
333 // actually initialize the global after current function has finished
334 // compilation.
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000335 const Type *GlobalType = GV->getType()->getElementType();
Owen Andersona69571c2006-05-03 01:29:57 +0000336 size_t S = getTargetData()->getTypeSize(GlobalType);
337 size_t A = getTargetData()->getTypeAlignment(GlobalType);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000338 if (A <= 8) {
339 Ptr = malloc(S);
340 } else {
341 // Allocate S+A bytes of memory, then use an aligned pointer within that
342 // space.
343 Ptr = malloc(S+A);
344 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
Chris Lattner21c39a62006-07-12 00:31:47 +0000345 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000346 }
Reid Spenceree448632005-07-12 15:51:55 +0000347 state.getPendingGlobals(locked).push_back(GV);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000348 }
349 addGlobalMapping(GV, Ptr);
350 return Ptr;
351}
352
353
Chris Lattner4d326fa2003-12-20 01:46:27 +0000354/// recompileAndRelinkFunction - This method is used to force a function
355/// which has already been compiled, to be compiled again, possibly
356/// after it has been modified. Then the entry to the old copy is overwritten
357/// with a branch to the new copy. If there was no old copy, this acts
358/// just like JIT::getPointerToFunction().
359///
360void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000361 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000362
Chris Lattnerc07ed132003-12-20 03:36:47 +0000363 // If it's not already compiled there is no reason to patch it up.
364 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000365
Chris Lattnerc07ed132003-12-20 03:36:47 +0000366 // Delete the old function mapping.
367 addGlobalMapping(F, 0);
368
Chris Lattnerc07ed132003-12-20 03:36:47 +0000369 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000370 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000371
372 // Update state, forward the old function to the new function.
373 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000374 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
375 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
376 return Addr;
377}
Misha Brukman895eddf2004-11-07 23:58:46 +0000378