blob: cb698c35d28e413d5159eeca72c1a1ee92d0ed2e [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)
Chris Lattner2c639ad2007-04-20 22:40:05 +000053 : ExecutionEngine(MP), TM(tm), TJI(tji), jitstate(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 Lattner2c639ad2007-04-20 22:40:05 +000061 FunctionPassManager &PM = jitstate.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 Spencer38f6a152007-03-06 03:11:31 +0000110 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
111 (char **)GVTOP(ArgValues[1]),
112 (const char **)GVTOP(ArgValues[2])));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000113 return rv;
114 }
115 break;
Chris Lattner174f2262004-08-16 01:07:04 +0000116 case 2:
Reid Spencere49661b2006-12-31 05:51:36 +0000117 if ((FTy->getParamType(0) == Type::Int32Ty ||
118 FTy->getParamType(0) == Type::Int32Ty) &&
Chris Lattner174f2262004-08-16 01:07:04 +0000119 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;
Reid Spencer38f6a152007-03-06 03:11:31 +0000124 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
125 (char **)GVTOP(ArgValues[1])));
Chris Lattner174f2262004-08-16 01:07:04 +0000126 return rv;
127 }
128 break;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000129 case 1:
130 if (FTy->getNumParams() == 1 &&
Reid Spencere49661b2006-12-31 05:51:36 +0000131 (FTy->getParamType(0) == Type::Int32Ty ||
132 FTy->getParamType(0) == Type::Int32Ty)) {
Chris Lattnere5eab142004-08-15 23:53:06 +0000133 GenericValue rv;
Chris Lattner870286a2006-06-01 17:29:22 +0000134 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
Reid Spencer38f6a152007-03-06 03:11:31 +0000135 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000136 return rv;
137 }
138 break;
Chris Lattnere5eab142004-08-15 23:53:06 +0000139 }
140 }
141
142 // Handle cases where no arguments are passed first.
143 if (ArgValues.empty()) {
144 GenericValue rv;
145 switch (RetTy->getTypeID()) {
146 default: assert(0 && "Unknown return type for function call!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000147 case Type::IntegerTyID: {
148 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
149 if (BitWidth == 1)
Reid Spencer38f6a152007-03-06 03:11:31 +0000150 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000151 else if (BitWidth <= 8)
Reid Spencer38f6a152007-03-06 03:11:31 +0000152 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000153 else if (BitWidth <= 16)
Reid Spencer38f6a152007-03-06 03:11:31 +0000154 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000155 else if (BitWidth <= 32)
Reid Spencer38f6a152007-03-06 03:11:31 +0000156 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000157 else if (BitWidth <= 64)
Reid Spencer38f6a152007-03-06 03:11:31 +0000158 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000159 else
160 assert(0 && "Integer types > 64 bits not supported");
Chris Lattnerd297aea2004-08-15 23:34:48 +0000161 return rv;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000162 }
Chris Lattnere5eab142004-08-15 23:53:06 +0000163 case Type::VoidTyID:
Reid Spencer38f6a152007-03-06 03:11:31 +0000164 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
Chris Lattnere5eab142004-08-15 23:53:06 +0000165 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000166 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.
Chris Lattner990b8492007-02-13 06:01:22 +0000192 SmallVector<Value*, 8> Args;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000193 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!");
Reid Spencer38f6a152007-03-06 03:11:31 +0000199 case Type::IntegerTyID: C = ConstantInt::get(AV.IntVal); break;
200 case Type::FloatTyID: C = ConstantFP ::get(ArgTy, AV.FloatVal); break;
201 case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, AV.DoubleVal); break;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000202 case Type::PointerTyID:
203 void *ArgPtr = GVTOP(AV);
204 if (sizeof(void*) == 4) {
Reid Spencere49661b2006-12-31 05:51:36 +0000205 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000206 } else {
Reid Spencere49661b2006-12-31 05:51:36 +0000207 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000208 }
Reid Spencer15f46d62006-12-12 01:17:41 +0000209 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000210 break;
211 }
212 Args.push_back(C);
213 }
214
Chris Lattner990b8492007-02-13 06:01:22 +0000215 CallInst *TheCall = new CallInst(F, &Args[0], Args.size(), "", StubBB);
Chris Lattnera471e042005-05-06 06:48:54 +0000216 TheCall->setTailCall();
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000217 if (TheCall->getType() != Type::VoidTy)
218 new ReturnInst(TheCall, StubBB); // Return result of the call.
219 else
220 new ReturnInst(StubBB); // Just return void.
221
222 // Finally, return the value returned by our nullary stub function.
223 return runFunction(Stub, std::vector<GenericValue>());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000224}
Chris Lattner4d326fa2003-12-20 01:46:27 +0000225
226/// runJITOnFunction - Run the FunctionPassManager full of
227/// just-in-time compilation passes on F, hopefully filling in
228/// GlobalAddress[F] with the address of F's machine code.
229///
230void JIT::runJITOnFunction(Function *F) {
231 static bool isAlreadyCodeGenerating = false;
232 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000233
Reid Spenceree448632005-07-12 15:51:55 +0000234 MutexGuard locked(lock);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000235
236 // JIT the function
237 isAlreadyCodeGenerating = true;
Chris Lattner2c639ad2007-04-20 22:40:05 +0000238 jitstate.getPM(locked).run(*F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000239 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +0000240
241 // If the function referred to a global variable that had not yet been
242 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
243 // all of these globals now.
Chris Lattner2c639ad2007-04-20 22:40:05 +0000244 while (!jitstate.getPendingGlobals(locked).empty()) {
245 const GlobalVariable *GV = jitstate.getPendingGlobals(locked).back();
246 jitstate.getPendingGlobals(locked).pop_back();
Chris Lattnerc07ed132003-12-20 03:36:47 +0000247 EmitGlobalVariable(GV);
248 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000249}
250
251/// getPointerToFunction - This method is used to get the address of the
252/// specified function, compiling it if neccesary.
253///
254void *JIT::getPointerToFunction(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000255 MutexGuard locked(lock);
256
Chris Lattnerc07ed132003-12-20 03:36:47 +0000257 if (void *Addr = getPointerToGlobalIfAvailable(F))
258 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000259
Chris Lattnerfe854032006-08-16 01:24:12 +0000260 // Make sure we read in the function if it exists in this Module.
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000261 if (F->hasNotBeenReadFromBytecode()) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000262 // Determine the module provider this function is provided by.
263 Module *M = F->getParent();
264 ModuleProvider *MP = 0;
265 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
266 if (Modules[i]->getModule() == M) {
267 MP = Modules[i];
268 break;
269 }
270 }
271 assert(MP && "Function isn't in a module we know about!");
272
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000273 std::string ErrorMsg;
274 if (MP->materializeFunction(F, &ErrorMsg)) {
Bill Wendling832171c2006-12-07 20:04:42 +0000275 cerr << "Error reading function '" << F->getName()
276 << "' from bytecode file: " << ErrorMsg << "\n";
Chris Lattner0050ef82004-11-15 23:18:09 +0000277 abort();
278 }
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000279 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000280
Reid Spencer5cbf9852007-01-30 20:08:39 +0000281 if (F->isDeclaration()) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000282 void *Addr = getPointerToNamedFunction(F->getName());
283 addGlobalMapping(F, Addr);
284 return Addr;
285 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000286
287 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000288
289 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000290 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
291 return Addr;
292}
293
Chris Lattnerc07ed132003-12-20 03:36:47 +0000294/// getOrEmitGlobalVariable - Return the address of the specified global
295/// variable, possibly emitting it to memory if needed. This is used by the
296/// Emitter.
297void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
Reid Spenceree448632005-07-12 15:51:55 +0000298 MutexGuard locked(lock);
299
Chris Lattnerc07ed132003-12-20 03:36:47 +0000300 void *Ptr = getPointerToGlobalIfAvailable(GV);
301 if (Ptr) return Ptr;
302
303 // If the global is external, just remember the address.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000304 if (GV->isDeclaration()) {
Chris Lattnerc1f400c2006-12-17 21:04:02 +0000305#if defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_4) && \
306 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
307 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
308 __APPLE_CC__ >= 5330))
Evan Chengfb9c0d72006-09-01 07:09:56 +0000309 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
310 // of atexit). It passes the address of linker generated symbol __dso_handle
311 // to the function.
312 // This configuration change happened at version 5330.
Evan Cheng5f42c552006-07-21 23:06:20 +0000313 if (GV->getName() == "__dso_handle")
314 return (void*)&__dso_handle;
Evan Chengb82ab942006-07-22 00:42:03 +0000315#endif
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000316 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
Chris Lattnerc07ed132003-12-20 03:36:47 +0000317 if (Ptr == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000318 cerr << "Could not resolve external global address: "
319 << GV->getName() << "\n";
Chris Lattnerc07ed132003-12-20 03:36:47 +0000320 abort();
321 }
322 } else {
323 // If the global hasn't been emitted to memory yet, allocate space. We will
324 // actually initialize the global after current function has finished
325 // compilation.
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000326 const Type *GlobalType = GV->getType()->getElementType();
Owen Andersona69571c2006-05-03 01:29:57 +0000327 size_t S = getTargetData()->getTypeSize(GlobalType);
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000328 size_t A = getTargetData()->getPrefTypeAlignment(GlobalType);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000329 if (A <= 8) {
330 Ptr = malloc(S);
331 } else {
332 // Allocate S+A bytes of memory, then use an aligned pointer within that
333 // space.
334 Ptr = malloc(S+A);
335 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
Chris Lattner21c39a62006-07-12 00:31:47 +0000336 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000337 }
Chris Lattner2c639ad2007-04-20 22:40:05 +0000338 jitstate.getPendingGlobals(locked).push_back(GV);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000339 }
340 addGlobalMapping(GV, Ptr);
341 return Ptr;
342}
343
344
Chris Lattner4d326fa2003-12-20 01:46:27 +0000345/// recompileAndRelinkFunction - This method is used to force a function
346/// which has already been compiled, to be compiled again, possibly
347/// after it has been modified. Then the entry to the old copy is overwritten
348/// with a branch to the new copy. If there was no old copy, this acts
349/// just like JIT::getPointerToFunction().
350///
351void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000352 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000353
Chris Lattnerc07ed132003-12-20 03:36:47 +0000354 // If it's not already compiled there is no reason to patch it up.
355 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000356
Chris Lattnerc07ed132003-12-20 03:36:47 +0000357 // Delete the old function mapping.
358 addGlobalMapping(F, 0);
359
Chris Lattnerc07ed132003-12-20 03:36:47 +0000360 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000361 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000362
363 // Update state, forward the old function to the new function.
364 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000365 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
366 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
367 return Addr;
368}
Misha Brukman895eddf2004-11-07 23:58:46 +0000369