blob: 919f9af002b688afa50744f2d028c5b6ad971563 [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 Cheng5f271af2006-09-01 07:00:46 +000035#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 && \
36 __APPLE_CC__ >= 5330
Evan Cheng5f42c552006-07-21 23:06:20 +000037// __dso_handle is resolved by Mac OS X dynamic linker.
38extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
39#endif
Nate Begemanb76ea742006-07-22 16:59:38 +000040#endif
Evan Cheng5f42c552006-07-21 23:06:20 +000041
Chris Lattner2fe4bb02006-03-22 06:07:50 +000042static struct RegisterJIT {
43 RegisterJIT() { JIT::Register(); }
44} JITRegistrator;
45
Jeff Cohen2f519142006-03-24 02:53:49 +000046namespace llvm {
47 void LinkInJIT() {
48 }
49}
50
Chris Lattner4d326fa2003-12-20 01:46:27 +000051JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
Reid Spenceree448632005-07-12 15:51:55 +000052 : ExecutionEngine(MP), TM(tm), TJI(tji), state(MP) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000053 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000054
55 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000056 MCE = createEmitter(*this);
Misha Brukmanf976c852005-04-21 22:55:34 +000057
Brian Gaeke50872d52004-04-14 17:45:52 +000058 // Add target data
Reid Spenceree448632005-07-12 15:51:55 +000059 MutexGuard locked(lock);
60 FunctionPassManager& PM = state.getPM(locked);
Chris Lattnerb93b0342006-05-04 21:18:40 +000061 PM.add(new TargetData(*TM.getTargetData()));
Brian Gaeke50872d52004-04-14 17:45:52 +000062
Chris Lattner4d326fa2003-12-20 01:46:27 +000063 // Compile LLVM Code down to machine code in the intermediate representation
64 TJI.addPassesToJITCompile(PM);
65
66 // Turn the machine code intermediate representation into bytes in memory that
67 // may be executed.
68 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
Chris Lattnercc22e9f2004-08-16 00:14:18 +000069 std::cerr << "Target '" << TM.getName()
Chris Lattner4d326fa2003-12-20 01:46:27 +000070 << "' doesn't support machine code emission!\n";
71 abort();
72 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000073}
74
Chris Lattner4d326fa2003-12-20 01:46:27 +000075JIT::~JIT() {
76 delete MCE;
77 delete &TM;
78}
79
Brian Gaeke70975ee2003-09-05 18:42:01 +000080/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +000081///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000082GenericValue JIT::runFunction(Function *F,
83 const std::vector<GenericValue> &ArgValues) {
Chris Lattnerb47130c2004-08-15 23:29:50 +000084 assert(F && "Function *F was null at entry to run()");
Chris Lattnerb47130c2004-08-15 23:29:50 +000085
86 void *FPtr = getPointerToFunction(F);
Chris Lattner7c45d782004-08-15 23:31:43 +000087 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerf7bedf42004-08-15 23:39:59 +000088 const FunctionType *FTy = F->getFunctionType();
Chris Lattnere5eab142004-08-15 23:53:06 +000089 const Type *RetTy = FTy->getReturnType();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000090
Chris Lattnere5eab142004-08-15 23:53:06 +000091 assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
92 "Too many arguments passed into function!");
93 assert(FTy->getNumParams() == ArgValues.size() &&
94 "This doesn't support passing arguments through varargs (yet)!");
95
Misha Brukmanec843022004-10-22 23:35:57 +000096 // Handle some common cases first. These cases correspond to common `main'
Chris Lattnere5eab142004-08-15 23:53:06 +000097 // prototypes.
Chris Lattnerd297aea2004-08-15 23:34:48 +000098 if (RetTy == Type::IntTy || RetTy == Type::UIntTy || RetTy == Type::VoidTy) {
Chris Lattnerf7bedf42004-08-15 23:39:59 +000099 switch (ArgValues.size()) {
100 case 3:
Misha Brukmanf976c852005-04-21 22:55:34 +0000101 if ((FTy->getParamType(0) == Type::IntTy ||
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000102 FTy->getParamType(0) == Type::UIntTy) &&
103 isa<PointerType>(FTy->getParamType(1)) &&
104 isa<PointerType>(FTy->getParamType(2))) {
105 int (*PF)(int, char **, const char **) =
Chris Lattner870286a2006-06-01 17:29:22 +0000106 (int(*)(int, char **, const char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000107
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000108 // Call the function.
Chris Lattnere5eab142004-08-15 23:53:06 +0000109 GenericValue rv;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000110 rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]),
111 (const char **)GVTOP(ArgValues[2]));
112 return rv;
113 }
114 break;
Chris Lattner174f2262004-08-16 01:07:04 +0000115 case 2:
Misha Brukmanf976c852005-04-21 22:55:34 +0000116 if ((FTy->getParamType(0) == Type::IntTy ||
Chris Lattner174f2262004-08-16 01:07:04 +0000117 FTy->getParamType(0) == Type::UIntTy) &&
118 isa<PointerType>(FTy->getParamType(1))) {
Chris Lattner870286a2006-06-01 17:29:22 +0000119 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000120
Chris Lattner174f2262004-08-16 01:07:04 +0000121 // Call the function.
122 GenericValue rv;
123 rv.IntVal = PF(ArgValues[0].IntVal, (char **)GVTOP(ArgValues[1]));
124 return rv;
125 }
126 break;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000127 case 1:
128 if (FTy->getNumParams() == 1 &&
Misha Brukmanf976c852005-04-21 22:55:34 +0000129 (FTy->getParamType(0) == Type::IntTy ||
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000130 FTy->getParamType(0) == Type::UIntTy)) {
Chris Lattnere5eab142004-08-15 23:53:06 +0000131 GenericValue rv;
Chris Lattner870286a2006-06-01 17:29:22 +0000132 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000133 rv.IntVal = PF(ArgValues[0].IntVal);
134 return rv;
135 }
136 break;
Chris Lattnere5eab142004-08-15 23:53:06 +0000137 }
138 }
139
140 // Handle cases where no arguments are passed first.
141 if (ArgValues.empty()) {
142 GenericValue rv;
143 switch (RetTy->getTypeID()) {
144 default: assert(0 && "Unknown return type for function call!");
145 case Type::BoolTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000146 rv.BoolVal = ((bool(*)())(intptr_t)FPtr)();
Chris Lattnerd297aea2004-08-15 23:34:48 +0000147 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000148 case Type::SByteTyID:
149 case Type::UByteTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000150 rv.SByteVal = ((char(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000151 return rv;
152 case Type::ShortTyID:
153 case Type::UShortTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000154 rv.ShortVal = ((short(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000155 return rv;
156 case Type::VoidTyID:
157 case Type::IntTyID:
158 case Type::UIntTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000159 rv.IntVal = ((int(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000160 return rv;
161 case Type::LongTyID:
162 case Type::ULongTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000163 rv.LongVal = ((int64_t(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000164 return rv;
165 case Type::FloatTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000166 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000167 return rv;
168 case Type::DoubleTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000169 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000170 return rv;
171 case Type::PointerTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000172 return PTOGV(((void*(*)())(intptr_t)FPtr)());
Chris Lattnerd297aea2004-08-15 23:34:48 +0000173 }
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000174 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000175
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000176 // Okay, this is not one of our quick and easy cases. Because we don't have a
177 // full FFI, we have to codegen a nullary stub function that just calls the
178 // function we are interested in, passing in constants for all of the
179 // arguments. Make this function and return.
180
181 // First, create the function.
182 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
183 Function *Stub = new Function(STy, Function::InternalLinkage, "",
184 F->getParent());
185
186 // Insert a basic block.
187 BasicBlock *StubBB = new BasicBlock("", Stub);
188
189 // Convert all of the GenericValue arguments over to constants. Note that we
190 // currently don't support varargs.
191 std::vector<Value*> Args;
192 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
193 Constant *C = 0;
194 const Type *ArgTy = FTy->getParamType(i);
195 const GenericValue &AV = ArgValues[i];
196 switch (ArgTy->getTypeID()) {
197 default: assert(0 && "Unknown argument type for function call!");
198 case Type::BoolTyID: C = ConstantBool::get(AV.BoolVal); break;
199 case Type::SByteTyID: C = ConstantSInt::get(ArgTy, AV.SByteVal); break;
200 case Type::UByteTyID: C = ConstantUInt::get(ArgTy, AV.UByteVal); break;
201 case Type::ShortTyID: C = ConstantSInt::get(ArgTy, AV.ShortVal); break;
202 case Type::UShortTyID: C = ConstantUInt::get(ArgTy, AV.UShortVal); break;
203 case Type::IntTyID: C = ConstantSInt::get(ArgTy, AV.IntVal); break;
204 case Type::UIntTyID: C = ConstantUInt::get(ArgTy, AV.UIntVal); break;
205 case Type::LongTyID: C = ConstantSInt::get(ArgTy, AV.LongVal); break;
206 case Type::ULongTyID: C = ConstantUInt::get(ArgTy, AV.ULongVal); break;
207 case Type::FloatTyID: C = ConstantFP ::get(ArgTy, AV.FloatVal); break;
208 case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, AV.DoubleVal); break;
209 case Type::PointerTyID:
210 void *ArgPtr = GVTOP(AV);
211 if (sizeof(void*) == 4) {
212 C = ConstantSInt::get(Type::IntTy, (int)(intptr_t)ArgPtr);
213 } else {
214 C = ConstantSInt::get(Type::LongTy, (intptr_t)ArgPtr);
215 }
216 C = ConstantExpr::getCast(C, ArgTy); // Cast the integer to pointer
217 break;
218 }
219 Args.push_back(C);
220 }
221
Chris Lattnera471e042005-05-06 06:48:54 +0000222 CallInst *TheCall = new CallInst(F, Args, "", StubBB);
223 TheCall->setTailCall();
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000224 if (TheCall->getType() != Type::VoidTy)
225 new ReturnInst(TheCall, StubBB); // Return result of the call.
226 else
227 new ReturnInst(StubBB); // Just return void.
228
229 // Finally, return the value returned by our nullary stub function.
230 return runFunction(Stub, std::vector<GenericValue>());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000231}
Chris Lattner4d326fa2003-12-20 01:46:27 +0000232
233/// runJITOnFunction - Run the FunctionPassManager full of
234/// just-in-time compilation passes on F, hopefully filling in
235/// GlobalAddress[F] with the address of F's machine code.
236///
237void JIT::runJITOnFunction(Function *F) {
238 static bool isAlreadyCodeGenerating = false;
239 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000240
Reid Spenceree448632005-07-12 15:51:55 +0000241 MutexGuard locked(lock);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000242
243 // JIT the function
244 isAlreadyCodeGenerating = true;
Reid Spenceree448632005-07-12 15:51:55 +0000245 state.getPM(locked).run(*F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000246 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +0000247
248 // If the function referred to a global variable that had not yet been
249 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
250 // all of these globals now.
Reid Spenceree448632005-07-12 15:51:55 +0000251 while (!state.getPendingGlobals(locked).empty()) {
252 const GlobalVariable *GV = state.getPendingGlobals(locked).back();
253 state.getPendingGlobals(locked).pop_back();
Chris Lattnerc07ed132003-12-20 03:36:47 +0000254 EmitGlobalVariable(GV);
255 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000256}
257
258/// getPointerToFunction - This method is used to get the address of the
259/// specified function, compiling it if neccesary.
260///
261void *JIT::getPointerToFunction(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000262 MutexGuard locked(lock);
263
Chris Lattnerc07ed132003-12-20 03:36:47 +0000264 if (void *Addr = getPointerToGlobalIfAvailable(F))
265 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000266
Chris Lattnerfe854032006-08-16 01:24:12 +0000267 // Make sure we read in the function if it exists in this Module.
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000268 if (F->hasNotBeenReadFromBytecode()) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000269 // Determine the module provider this function is provided by.
270 Module *M = F->getParent();
271 ModuleProvider *MP = 0;
272 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
273 if (Modules[i]->getModule() == M) {
274 MP = Modules[i];
275 break;
276 }
277 }
278 assert(MP && "Function isn't in a module we know about!");
279
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000280 std::string ErrorMsg;
281 if (MP->materializeFunction(F, &ErrorMsg)) {
Chris Lattner0050ef82004-11-15 23:18:09 +0000282 std::cerr << "Error reading function '" << F->getName()
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000283 << "' from bytecode file: " << ErrorMsg << "\n";
Chris Lattner0050ef82004-11-15 23:18:09 +0000284 abort();
285 }
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000286 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000287
Chris Lattnerc07ed132003-12-20 03:36:47 +0000288 if (F->isExternal()) {
289 void *Addr = getPointerToNamedFunction(F->getName());
290 addGlobalMapping(F, Addr);
291 return Addr;
292 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000293
294 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000295
296 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000297 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
298 return Addr;
299}
300
Chris Lattnerc07ed132003-12-20 03:36:47 +0000301/// getOrEmitGlobalVariable - Return the address of the specified global
302/// variable, possibly emitting it to memory if needed. This is used by the
303/// Emitter.
304void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
Reid Spenceree448632005-07-12 15:51:55 +0000305 MutexGuard locked(lock);
306
Chris Lattnerc07ed132003-12-20 03:36:47 +0000307 void *Ptr = getPointerToGlobalIfAvailable(GV);
308 if (Ptr) return Ptr;
309
310 // If the global is external, just remember the address.
311 if (GV->isExternal()) {
Evan Chengb82ab942006-07-22 00:42:03 +0000312#ifdef __APPLE__
Evan Cheng5f271af2006-09-01 07:00:46 +0000313#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4 && \
314 __APPLE_CC__ >= 5330
Evan Chengfb9c0d72006-09-01 07:09:56 +0000315 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
316 // of atexit). It passes the address of linker generated symbol __dso_handle
317 // to the function.
318 // This configuration change happened at version 5330.
Evan Cheng5f42c552006-07-21 23:06:20 +0000319 if (GV->getName() == "__dso_handle")
320 return (void*)&__dso_handle;
Evan Chengb82ab942006-07-22 00:42:03 +0000321#endif
Nate Begemanb76ea742006-07-22 16:59:38 +0000322#endif
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000323 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
Chris Lattnerc07ed132003-12-20 03:36:47 +0000324 if (Ptr == 0) {
325 std::cerr << "Could not resolve external global address: "
326 << GV->getName() << "\n";
327 abort();
328 }
329 } else {
330 // If the global hasn't been emitted to memory yet, allocate space. We will
331 // actually initialize the global after current function has finished
332 // compilation.
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000333 const Type *GlobalType = GV->getType()->getElementType();
Owen Andersona69571c2006-05-03 01:29:57 +0000334 size_t S = getTargetData()->getTypeSize(GlobalType);
335 size_t A = getTargetData()->getTypeAlignment(GlobalType);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000336 if (A <= 8) {
337 Ptr = malloc(S);
338 } else {
339 // Allocate S+A bytes of memory, then use an aligned pointer within that
340 // space.
341 Ptr = malloc(S+A);
342 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
Chris Lattner21c39a62006-07-12 00:31:47 +0000343 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000344 }
Reid Spenceree448632005-07-12 15:51:55 +0000345 state.getPendingGlobals(locked).push_back(GV);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000346 }
347 addGlobalMapping(GV, Ptr);
348 return Ptr;
349}
350
351
Chris Lattner4d326fa2003-12-20 01:46:27 +0000352/// recompileAndRelinkFunction - This method is used to force a function
353/// which has already been compiled, to be compiled again, possibly
354/// after it has been modified. Then the entry to the old copy is overwritten
355/// with a branch to the new copy. If there was no old copy, this acts
356/// just like JIT::getPointerToFunction().
357///
358void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000359 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000360
Chris Lattnerc07ed132003-12-20 03:36:47 +0000361 // If it's not already compiled there is no reason to patch it up.
362 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000363
Chris Lattnerc07ed132003-12-20 03:36:47 +0000364 // Delete the old function mapping.
365 addGlobalMapping(F, 0);
366
Chris Lattnerc07ed132003-12-20 03:36:47 +0000367 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000368 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000369
370 // Update state, forward the old function to the new function.
371 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000372 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
373 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
374 return Addr;
375}
Misha Brukman895eddf2004-11-07 23:58:46 +0000376