blob: 848786f31489f70fedd1e31baa84e4a5b352b540 [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
Gabor Greifa99be512007-07-05 17:07:56 +000011// execution of LLVM bitcode 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"
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +000030
31#include "llvm/Config/config.h"
32
Chris Lattnerc19aade2003-12-08 08:06:28 +000033using namespace llvm;
Misha Brukmanabb027c2003-05-27 21:40:39 +000034
Nate Begemanb76ea742006-07-22 16:59:38 +000035#ifdef __APPLE__
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +000036// Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
37// of atexit). It passes the address of linker generated symbol __dso_handle
38// to the function.
39// This configuration change happened at version 5330.
40# include <AvailabilityMacros.h>
41# if defined(MAC_OS_X_VERSION_10_4) && \
42 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
43 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
44 __APPLE_CC__ >= 5330))
45# ifndef HAVE___DSO_HANDLE
46# define HAVE___DSO_HANDLE 1
47# endif
48# endif
Evan Cheng5f42c552006-07-21 23:06:20 +000049#endif
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +000050
51#if HAVE___DSO_HANDLE
52extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
Nate Begemanb76ea742006-07-22 16:59:38 +000053#endif
Evan Cheng5f42c552006-07-21 23:06:20 +000054
Chris Lattner2fe4bb02006-03-22 06:07:50 +000055static struct RegisterJIT {
56 RegisterJIT() { JIT::Register(); }
57} JITRegistrator;
58
Jeff Cohen2f519142006-03-24 02:53:49 +000059namespace llvm {
60 void LinkInJIT() {
61 }
62}
63
Chris Lattner4d326fa2003-12-20 01:46:27 +000064JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
Chris Lattner2c639ad2007-04-20 22:40:05 +000065 : ExecutionEngine(MP), TM(tm), TJI(tji), jitstate(MP) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000066 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000067
68 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000069 MCE = createEmitter(*this);
Misha Brukmanf976c852005-04-21 22:55:34 +000070
Brian Gaeke50872d52004-04-14 17:45:52 +000071 // Add target data
Reid Spenceree448632005-07-12 15:51:55 +000072 MutexGuard locked(lock);
Chris Lattner2c639ad2007-04-20 22:40:05 +000073 FunctionPassManager &PM = jitstate.getPM(locked);
Chris Lattnerb93b0342006-05-04 21:18:40 +000074 PM.add(new TargetData(*TM.getTargetData()));
Brian Gaeke50872d52004-04-14 17:45:52 +000075
Chris Lattner4d326fa2003-12-20 01:46:27 +000076 // Turn the machine code intermediate representation into bytes in memory that
77 // may be executed.
Chris Lattner1911fd42006-09-04 04:14:57 +000078 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
Bill Wendling832171c2006-12-07 20:04:42 +000079 cerr << "Target does not support machine code emission!\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +000080 abort();
81 }
Chris Lattner1911fd42006-09-04 04:14:57 +000082
83 // Initialize passes.
84 PM.doInitialization();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000085}
86
Chris Lattner4d326fa2003-12-20 01:46:27 +000087JIT::~JIT() {
88 delete MCE;
89 delete &TM;
90}
91
Brian Gaeke70975ee2003-09-05 18:42:01 +000092/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +000093///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +000094GenericValue JIT::runFunction(Function *F,
95 const std::vector<GenericValue> &ArgValues) {
Chris Lattnerb47130c2004-08-15 23:29:50 +000096 assert(F && "Function *F was null at entry to run()");
Chris Lattnerb47130c2004-08-15 23:29:50 +000097
98 void *FPtr = getPointerToFunction(F);
Chris Lattner7c45d782004-08-15 23:31:43 +000099 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000100 const FunctionType *FTy = F->getFunctionType();
Chris Lattnere5eab142004-08-15 23:53:06 +0000101 const Type *RetTy = FTy->getReturnType();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000102
Chris Lattnere5eab142004-08-15 23:53:06 +0000103 assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
104 "Too many arguments passed into function!");
105 assert(FTy->getNumParams() == ArgValues.size() &&
106 "This doesn't support passing arguments through varargs (yet)!");
107
Misha Brukmanec843022004-10-22 23:35:57 +0000108 // Handle some common cases first. These cases correspond to common `main'
Chris Lattnere5eab142004-08-15 23:53:06 +0000109 // prototypes.
Chris Lattner79e038a2007-08-08 16:19:57 +0000110 if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000111 switch (ArgValues.size()) {
112 case 3:
Chris Lattner79e038a2007-08-08 16:19:57 +0000113 if (FTy->getParamType(0) == Type::Int32Ty &&
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000114 isa<PointerType>(FTy->getParamType(1)) &&
115 isa<PointerType>(FTy->getParamType(2))) {
116 int (*PF)(int, char **, const char **) =
Chris Lattner870286a2006-06-01 17:29:22 +0000117 (int(*)(int, char **, const char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000118
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000119 // Call the function.
Chris Lattnere5eab142004-08-15 23:53:06 +0000120 GenericValue rv;
Reid Spencer38f6a152007-03-06 03:11:31 +0000121 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
122 (char **)GVTOP(ArgValues[1]),
123 (const char **)GVTOP(ArgValues[2])));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000124 return rv;
125 }
126 break;
Chris Lattner174f2262004-08-16 01:07:04 +0000127 case 2:
Chris Lattner79e038a2007-08-08 16:19:57 +0000128 if (FTy->getParamType(0) == Type::Int32Ty &&
Chris Lattner174f2262004-08-16 01:07:04 +0000129 isa<PointerType>(FTy->getParamType(1))) {
Chris Lattner870286a2006-06-01 17:29:22 +0000130 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000131
Chris Lattner174f2262004-08-16 01:07:04 +0000132 // Call the function.
133 GenericValue rv;
Reid Spencer38f6a152007-03-06 03:11:31 +0000134 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
135 (char **)GVTOP(ArgValues[1])));
Chris Lattner174f2262004-08-16 01:07:04 +0000136 return rv;
137 }
138 break;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000139 case 1:
140 if (FTy->getNumParams() == 1 &&
Chris Lattner79e038a2007-08-08 16:19:57 +0000141 FTy->getParamType(0) == Type::Int32Ty) {
Chris Lattnere5eab142004-08-15 23:53:06 +0000142 GenericValue rv;
Chris Lattner870286a2006-06-01 17:29:22 +0000143 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
Reid Spencer38f6a152007-03-06 03:11:31 +0000144 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000145 return rv;
146 }
147 break;
Chris Lattnere5eab142004-08-15 23:53:06 +0000148 }
149 }
150
151 // Handle cases where no arguments are passed first.
152 if (ArgValues.empty()) {
153 GenericValue rv;
154 switch (RetTy->getTypeID()) {
155 default: assert(0 && "Unknown return type for function call!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000156 case Type::IntegerTyID: {
157 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
158 if (BitWidth == 1)
Reid Spencer38f6a152007-03-06 03:11:31 +0000159 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000160 else if (BitWidth <= 8)
Reid Spencer38f6a152007-03-06 03:11:31 +0000161 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000162 else if (BitWidth <= 16)
Reid Spencer38f6a152007-03-06 03:11:31 +0000163 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000164 else if (BitWidth <= 32)
Reid Spencer38f6a152007-03-06 03:11:31 +0000165 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000166 else if (BitWidth <= 64)
Reid Spencer38f6a152007-03-06 03:11:31 +0000167 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000168 else
169 assert(0 && "Integer types > 64 bits not supported");
Chris Lattnerd297aea2004-08-15 23:34:48 +0000170 return rv;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000171 }
Chris Lattnere5eab142004-08-15 23:53:06 +0000172 case Type::VoidTyID:
Reid Spencer38f6a152007-03-06 03:11:31 +0000173 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
Chris Lattnere5eab142004-08-15 23:53:06 +0000174 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000175 case Type::FloatTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000176 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000177 return rv;
178 case Type::DoubleTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000179 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000180 return rv;
181 case Type::PointerTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000182 return PTOGV(((void*(*)())(intptr_t)FPtr)());
Chris Lattnerd297aea2004-08-15 23:34:48 +0000183 }
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000184 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000185
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000186 // Okay, this is not one of our quick and easy cases. Because we don't have a
187 // full FFI, we have to codegen a nullary stub function that just calls the
188 // function we are interested in, passing in constants for all of the
189 // arguments. Make this function and return.
190
191 // First, create the function.
192 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
193 Function *Stub = new Function(STy, Function::InternalLinkage, "",
194 F->getParent());
195
196 // Insert a basic block.
197 BasicBlock *StubBB = new BasicBlock("", Stub);
198
199 // Convert all of the GenericValue arguments over to constants. Note that we
200 // currently don't support varargs.
Chris Lattner990b8492007-02-13 06:01:22 +0000201 SmallVector<Value*, 8> Args;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000202 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
203 Constant *C = 0;
204 const Type *ArgTy = FTy->getParamType(i);
205 const GenericValue &AV = ArgValues[i];
206 switch (ArgTy->getTypeID()) {
207 default: assert(0 && "Unknown argument type for function call!");
Reid Spencer38f6a152007-03-06 03:11:31 +0000208 case Type::IntegerTyID: C = ConstantInt::get(AV.IntVal); break;
Dale Johannesen43421b32007-09-06 18:13:44 +0000209 case Type::FloatTyID: C = ConstantFP ::get(ArgTy, APFloat(AV.FloatVal));
210 break;
211 case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, APFloat(AV.DoubleVal));
212 break;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000213 case Type::PointerTyID:
214 void *ArgPtr = GVTOP(AV);
215 if (sizeof(void*) == 4) {
Reid Spencere49661b2006-12-31 05:51:36 +0000216 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000217 } else {
Reid Spencere49661b2006-12-31 05:51:36 +0000218 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000219 }
Reid Spencer15f46d62006-12-12 01:17:41 +0000220 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000221 break;
222 }
223 Args.push_back(C);
224 }
225
David Greene52eec542007-08-01 03:43:44 +0000226 CallInst *TheCall = new CallInst(F, Args.begin(), Args.end(), "", StubBB);
Chris Lattnera471e042005-05-06 06:48:54 +0000227 TheCall->setTailCall();
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000228 if (TheCall->getType() != Type::VoidTy)
229 new ReturnInst(TheCall, StubBB); // Return result of the call.
230 else
231 new ReturnInst(StubBB); // Just return void.
232
233 // Finally, return the value returned by our nullary stub function.
234 return runFunction(Stub, std::vector<GenericValue>());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000235}
Chris Lattner4d326fa2003-12-20 01:46:27 +0000236
237/// runJITOnFunction - Run the FunctionPassManager full of
238/// just-in-time compilation passes on F, hopefully filling in
239/// GlobalAddress[F] with the address of F's machine code.
240///
241void JIT::runJITOnFunction(Function *F) {
242 static bool isAlreadyCodeGenerating = false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000243
Reid Spenceree448632005-07-12 15:51:55 +0000244 MutexGuard locked(lock);
Chris Lattner17f218e2007-08-13 20:08:16 +0000245 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Chris Lattner4d326fa2003-12-20 01:46:27 +0000246
247 // JIT the function
248 isAlreadyCodeGenerating = true;
Chris Lattner2c639ad2007-04-20 22:40:05 +0000249 jitstate.getPM(locked).run(*F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000250 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +0000251
252 // If the function referred to a global variable that had not yet been
253 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
254 // all of these globals now.
Chris Lattner2c639ad2007-04-20 22:40:05 +0000255 while (!jitstate.getPendingGlobals(locked).empty()) {
256 const GlobalVariable *GV = jitstate.getPendingGlobals(locked).back();
257 jitstate.getPendingGlobals(locked).pop_back();
Chris Lattnerc07ed132003-12-20 03:36:47 +0000258 EmitGlobalVariable(GV);
259 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000260}
261
262/// getPointerToFunction - This method is used to get the address of the
263/// specified function, compiling it if neccesary.
264///
265void *JIT::getPointerToFunction(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000266 MutexGuard locked(lock);
267
Chris Lattnerc07ed132003-12-20 03:36:47 +0000268 if (void *Addr = getPointerToGlobalIfAvailable(F))
269 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000270
Chris Lattnerfe854032006-08-16 01:24:12 +0000271 // Make sure we read in the function if it exists in this Module.
Gabor Greifa99be512007-07-05 17:07:56 +0000272 if (F->hasNotBeenReadFromBitcode()) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000273 // Determine the module provider this function is provided by.
274 Module *M = F->getParent();
275 ModuleProvider *MP = 0;
276 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
277 if (Modules[i]->getModule() == M) {
278 MP = Modules[i];
279 break;
280 }
281 }
282 assert(MP && "Function isn't in a module we know about!");
283
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000284 std::string ErrorMsg;
285 if (MP->materializeFunction(F, &ErrorMsg)) {
Bill Wendling832171c2006-12-07 20:04:42 +0000286 cerr << "Error reading function '" << F->getName()
Gabor Greifa99be512007-07-05 17:07:56 +0000287 << "' from bitcode file: " << ErrorMsg << "\n";
Chris Lattner0050ef82004-11-15 23:18:09 +0000288 abort();
289 }
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000290 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000291
Reid Spencer5cbf9852007-01-30 20:08:39 +0000292 if (F->isDeclaration()) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000293 void *Addr = getPointerToNamedFunction(F->getName());
294 addGlobalMapping(F, Addr);
295 return Addr;
296 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000297
298 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000299
300 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000301 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
302 return Addr;
303}
304
Chris Lattnerc07ed132003-12-20 03:36:47 +0000305/// getOrEmitGlobalVariable - Return the address of the specified global
306/// variable, possibly emitting it to memory if needed. This is used by the
307/// Emitter.
308void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
Reid Spenceree448632005-07-12 15:51:55 +0000309 MutexGuard locked(lock);
310
Chris Lattnerc07ed132003-12-20 03:36:47 +0000311 void *Ptr = getPointerToGlobalIfAvailable(GV);
312 if (Ptr) return Ptr;
313
314 // If the global is external, just remember the address.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000315 if (GV->isDeclaration()) {
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +0000316#if HAVE___DSO_HANDLE
Evan Cheng5f42c552006-07-21 23:06:20 +0000317 if (GV->getName() == "__dso_handle")
318 return (void*)&__dso_handle;
Evan Chengb82ab942006-07-22 00:42:03 +0000319#endif
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000320 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
Chris Lattnerc07ed132003-12-20 03:36:47 +0000321 if (Ptr == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000322 cerr << "Could not resolve external global address: "
323 << GV->getName() << "\n";
Chris Lattnerc07ed132003-12-20 03:36:47 +0000324 abort();
325 }
326 } else {
327 // If the global hasn't been emitted to memory yet, allocate space. We will
328 // actually initialize the global after current function has finished
329 // compilation.
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000330 const Type *GlobalType = GV->getType()->getElementType();
Owen Andersona69571c2006-05-03 01:29:57 +0000331 size_t S = getTargetData()->getTypeSize(GlobalType);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000332 size_t A = getTargetData()->getPrefTypeAlignment(GlobalType);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000333 if (A <= 8) {
334 Ptr = malloc(S);
335 } else {
336 // Allocate S+A bytes of memory, then use an aligned pointer within that
337 // space.
338 Ptr = malloc(S+A);
339 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
Chris Lattner21c39a62006-07-12 00:31:47 +0000340 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000341 }
Chris Lattner2c639ad2007-04-20 22:40:05 +0000342 jitstate.getPendingGlobals(locked).push_back(GV);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000343 }
344 addGlobalMapping(GV, Ptr);
345 return Ptr;
346}
347
348
Chris Lattner4d326fa2003-12-20 01:46:27 +0000349/// recompileAndRelinkFunction - This method is used to force a function
350/// which has already been compiled, to be compiled again, possibly
351/// after it has been modified. Then the entry to the old copy is overwritten
352/// with a branch to the new copy. If there was no old copy, this acts
353/// just like JIT::getPointerToFunction().
354///
355void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000356 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000357
Chris Lattnerc07ed132003-12-20 03:36:47 +0000358 // If it's not already compiled there is no reason to patch it up.
359 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000360
Chris Lattnerc07ed132003-12-20 03:36:47 +0000361 // Delete the old function mapping.
362 addGlobalMapping(F, 0);
363
Chris Lattnerc07ed132003-12-20 03:36:47 +0000364 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000365 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000366
367 // Update state, forward the old function to the new function.
368 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000369 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
370 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
371 return Addr;
372}
Misha Brukman895eddf2004-11-07 23:58:46 +0000373