blob: c88e393d6a76f9f5baa5e665e91c557564522f4c [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"
Chris Lattnerc19aade2003-12-08 08:06:28 +000030using namespace llvm;
Misha Brukmanabb027c2003-05-27 21:40:39 +000031
Nate Begemanb76ea742006-07-22 16:59:38 +000032#ifdef __APPLE__
33#include <AvailabilityMacros.h>
Chris Lattnerc1f400c2006-12-17 21:04:02 +000034#if defined(MAC_OS_X_VERSION_10_4) && \
35 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
36 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
37 __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);
Chris Lattner1911fd42006-09-04 04:14:57 +000061 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 // Turn the machine code intermediate representation into bytes in memory that
65 // may be executed.
Chris Lattner1911fd42006-09-04 04:14:57 +000066 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
Bill Wendling832171c2006-12-07 20:04:42 +000067 cerr << "Target does not support machine code emission!\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000068 abort();
69 }
Chris Lattner1911fd42006-09-04 04:14:57 +000070
71 // Initialize passes.
72 PM.doInitialization();
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.
Reid Spencere49661b2006-12-31 05:51:36 +000098 if (RetTy == Type::Int32Ty || RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
Chris Lattnerf7bedf42004-08-15 23:39:59 +000099 switch (ArgValues.size()) {
100 case 3:
Reid Spencere49661b2006-12-31 05:51:36 +0000101 if ((FTy->getParamType(0) == Type::Int32Ty ||
102 FTy->getParamType(0) == Type::Int32Ty) &&
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000103 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;
Reid Spencere49661b2006-12-31 05:51:36 +0000110 rv.Int32Val = PF(ArgValues[0].Int32Val, (char **)GVTOP(ArgValues[1]),
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000111 (const char **)GVTOP(ArgValues[2]));
112 return rv;
113 }
114 break;
Chris Lattner174f2262004-08-16 01:07:04 +0000115 case 2:
Reid Spencere49661b2006-12-31 05:51:36 +0000116 if ((FTy->getParamType(0) == Type::Int32Ty ||
117 FTy->getParamType(0) == Type::Int32Ty) &&
Chris Lattner174f2262004-08-16 01:07:04 +0000118 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;
Reid Spencere49661b2006-12-31 05:51:36 +0000123 rv.Int32Val = PF(ArgValues[0].Int32Val, (char **)GVTOP(ArgValues[1]));
Chris Lattner174f2262004-08-16 01:07:04 +0000124 return rv;
125 }
126 break;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000127 case 1:
128 if (FTy->getNumParams() == 1 &&
Reid Spencere49661b2006-12-31 05:51:36 +0000129 (FTy->getParamType(0) == Type::Int32Ty ||
130 FTy->getParamType(0) == Type::Int32Ty)) {
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;
Reid Spencere49661b2006-12-31 05:51:36 +0000133 rv.Int32Val = PF(ArgValues[0].Int32Val);
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000134 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!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000145 case Type::IntegerTyID: {
146 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
147 if (BitWidth == 1)
148 rv.Int1Val = ((bool(*)())(intptr_t)FPtr)();
149 else if (BitWidth <= 8)
150 rv.Int8Val = ((char(*)())(intptr_t)FPtr)();
151 else if (BitWidth <= 16)
152 rv.Int16Val = ((short(*)())(intptr_t)FPtr)();
153 else if (BitWidth <= 32)
154 rv.Int32Val = ((int(*)())(intptr_t)FPtr)();
155 else if (BitWidth <= 64)
156 rv.Int64Val = ((int64_t(*)())(intptr_t)FPtr)();
157 else
158 assert(0 && "Integer types > 64 bits not supported");
Chris Lattnerd297aea2004-08-15 23:34:48 +0000159 return rv;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000160 }
Chris Lattnere5eab142004-08-15 23:53:06 +0000161 case Type::VoidTyID:
Reid Spencere49661b2006-12-31 05:51:36 +0000162 rv.Int32Val = ((int(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000163 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000164 case Type::FloatTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000165 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000166 return rv;
167 case Type::DoubleTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000168 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000169 return rv;
170 case Type::PointerTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000171 return PTOGV(((void*(*)())(intptr_t)FPtr)());
Chris Lattnerd297aea2004-08-15 23:34:48 +0000172 }
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000173 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000174
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000175 // Okay, this is not one of our quick and easy cases. Because we don't have a
176 // full FFI, we have to codegen a nullary stub function that just calls the
177 // function we are interested in, passing in constants for all of the
178 // arguments. Make this function and return.
179
180 // First, create the function.
181 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
182 Function *Stub = new Function(STy, Function::InternalLinkage, "",
183 F->getParent());
184
185 // Insert a basic block.
186 BasicBlock *StubBB = new BasicBlock("", Stub);
187
188 // Convert all of the GenericValue arguments over to constants. Note that we
189 // currently don't support varargs.
190 std::vector<Value*> Args;
191 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
192 Constant *C = 0;
193 const Type *ArgTy = FTy->getParamType(i);
194 const GenericValue &AV = ArgValues[i];
195 switch (ArgTy->getTypeID()) {
196 default: assert(0 && "Unknown argument type for function call!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000197 case Type::IntegerTyID: {
198 unsigned BitWidth = cast<IntegerType>(ArgTy)->getBitWidth();
199 if (BitWidth == 1)
200 C = ConstantInt::get(ArgTy, AV.Int1Val);
201 else if (BitWidth <= 8)
202 C = ConstantInt::get(ArgTy, AV.Int8Val);
203 else if (BitWidth <= 16)
204 C = ConstantInt::get(ArgTy, AV.Int16Val);
205 else if (BitWidth <= 32)
206 C = ConstantInt::get(ArgTy, AV.Int32Val);
207 else if (BitWidth <= 64)
208 C = ConstantInt::get(ArgTy, AV.Int64Val);
209 else
210 assert(0 && "Integer types > 64 bits not supported");
211 break;
212 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000213 case Type::FloatTyID: C = ConstantFP ::get(ArgTy, AV.FloatVal); break;
214 case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, AV.DoubleVal); break;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000215 case Type::PointerTyID:
216 void *ArgPtr = GVTOP(AV);
217 if (sizeof(void*) == 4) {
Reid Spencere49661b2006-12-31 05:51:36 +0000218 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000219 } else {
Reid Spencere49661b2006-12-31 05:51:36 +0000220 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000221 }
Reid Spencer15f46d62006-12-12 01:17:41 +0000222 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000223 break;
224 }
225 Args.push_back(C);
226 }
227
Chris Lattnera471e042005-05-06 06:48:54 +0000228 CallInst *TheCall = new CallInst(F, Args, "", StubBB);
229 TheCall->setTailCall();
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000230 if (TheCall->getType() != Type::VoidTy)
231 new ReturnInst(TheCall, StubBB); // Return result of the call.
232 else
233 new ReturnInst(StubBB); // Just return void.
234
235 // Finally, return the value returned by our nullary stub function.
236 return runFunction(Stub, std::vector<GenericValue>());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000237}
Chris Lattner4d326fa2003-12-20 01:46:27 +0000238
239/// runJITOnFunction - Run the FunctionPassManager full of
240/// just-in-time compilation passes on F, hopefully filling in
241/// GlobalAddress[F] with the address of F's machine code.
242///
243void JIT::runJITOnFunction(Function *F) {
244 static bool isAlreadyCodeGenerating = false;
245 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000246
Reid Spenceree448632005-07-12 15:51:55 +0000247 MutexGuard locked(lock);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000248
249 // JIT the function
250 isAlreadyCodeGenerating = true;
Reid Spenceree448632005-07-12 15:51:55 +0000251 state.getPM(locked).run(*F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000252 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +0000253
254 // If the function referred to a global variable that had not yet been
255 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
256 // all of these globals now.
Reid Spenceree448632005-07-12 15:51:55 +0000257 while (!state.getPendingGlobals(locked).empty()) {
258 const GlobalVariable *GV = state.getPendingGlobals(locked).back();
259 state.getPendingGlobals(locked).pop_back();
Chris Lattnerc07ed132003-12-20 03:36:47 +0000260 EmitGlobalVariable(GV);
261 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000262}
263
264/// getPointerToFunction - This method is used to get the address of the
265/// specified function, compiling it if neccesary.
266///
267void *JIT::getPointerToFunction(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000268 MutexGuard locked(lock);
269
Chris Lattnerc07ed132003-12-20 03:36:47 +0000270 if (void *Addr = getPointerToGlobalIfAvailable(F))
271 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000272
Chris Lattnerfe854032006-08-16 01:24:12 +0000273 // Make sure we read in the function if it exists in this Module.
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000274 if (F->hasNotBeenReadFromBytecode()) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000275 // Determine the module provider this function is provided by.
276 Module *M = F->getParent();
277 ModuleProvider *MP = 0;
278 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
279 if (Modules[i]->getModule() == M) {
280 MP = Modules[i];
281 break;
282 }
283 }
284 assert(MP && "Function isn't in a module we know about!");
285
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000286 std::string ErrorMsg;
287 if (MP->materializeFunction(F, &ErrorMsg)) {
Bill Wendling832171c2006-12-07 20:04:42 +0000288 cerr << "Error reading function '" << F->getName()
289 << "' from bytecode file: " << ErrorMsg << "\n";
Chris Lattner0050ef82004-11-15 23:18:09 +0000290 abort();
291 }
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000292 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000293
Chris Lattnerc07ed132003-12-20 03:36:47 +0000294 if (F->isExternal()) {
295 void *Addr = getPointerToNamedFunction(F->getName());
296 addGlobalMapping(F, Addr);
297 return Addr;
298 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000299
300 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000301
302 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000303 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
304 return Addr;
305}
306
Chris Lattnerc07ed132003-12-20 03:36:47 +0000307/// getOrEmitGlobalVariable - Return the address of the specified global
308/// variable, possibly emitting it to memory if needed. This is used by the
309/// Emitter.
310void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
Reid Spenceree448632005-07-12 15:51:55 +0000311 MutexGuard locked(lock);
312
Chris Lattnerc07ed132003-12-20 03:36:47 +0000313 void *Ptr = getPointerToGlobalIfAvailable(GV);
314 if (Ptr) return Ptr;
315
316 // If the global is external, just remember the address.
317 if (GV->isExternal()) {
Chris Lattnerc1f400c2006-12-17 21:04:02 +0000318#if defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_4) && \
319 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
320 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
321 __APPLE_CC__ >= 5330))
Evan Chengfb9c0d72006-09-01 07:09:56 +0000322 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
323 // of atexit). It passes the address of linker generated symbol __dso_handle
324 // to the function.
325 // This configuration change happened at version 5330.
Evan Cheng5f42c552006-07-21 23:06:20 +0000326 if (GV->getName() == "__dso_handle")
327 return (void*)&__dso_handle;
Evan Chengb82ab942006-07-22 00:42:03 +0000328#endif
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000329 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
Chris Lattnerc07ed132003-12-20 03:36:47 +0000330 if (Ptr == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000331 cerr << "Could not resolve external global address: "
332 << GV->getName() << "\n";
Chris Lattnerc07ed132003-12-20 03:36:47 +0000333 abort();
334 }
335 } else {
336 // If the global hasn't been emitted to memory yet, allocate space. We will
337 // actually initialize the global after current function has finished
338 // compilation.
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000339 const Type *GlobalType = GV->getType()->getElementType();
Owen Andersona69571c2006-05-03 01:29:57 +0000340 size_t S = getTargetData()->getTypeSize(GlobalType);
Chris Lattner58092e32007-01-20 22:35:55 +0000341 size_t A = getTargetData()->getTypeAlignmentPref(GlobalType);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000342 if (A <= 8) {
343 Ptr = malloc(S);
344 } else {
345 // Allocate S+A bytes of memory, then use an aligned pointer within that
346 // space.
347 Ptr = malloc(S+A);
348 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
Chris Lattner21c39a62006-07-12 00:31:47 +0000349 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000350 }
Reid Spenceree448632005-07-12 15:51:55 +0000351 state.getPendingGlobals(locked).push_back(GV);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000352 }
353 addGlobalMapping(GV, Ptr);
354 return Ptr;
355}
356
357
Chris Lattner4d326fa2003-12-20 01:46:27 +0000358/// recompileAndRelinkFunction - This method is used to force a function
359/// which has already been compiled, to be compiled again, possibly
360/// after it has been modified. Then the entry to the old copy is overwritten
361/// with a branch to the new copy. If there was no old copy, this acts
362/// just like JIT::getPointerToFunction().
363///
364void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000365 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000366
Chris Lattnerc07ed132003-12-20 03:36:47 +0000367 // If it's not already compiled there is no reason to patch it up.
368 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000369
Chris Lattnerc07ed132003-12-20 03:36:47 +0000370 // Delete the old function mapping.
371 addGlobalMapping(F, 0);
372
Chris Lattnerc07ed132003-12-20 03:36:47 +0000373 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000374 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000375
376 // Update state, forward the old function to the new function.
377 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000378 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
379 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
380 return Addr;
381}
Misha Brukman895eddf2004-11-07 23:58:46 +0000382