blob: 776129e6b20db92bb0178e716a1cc66ad181b136 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Anton Korobeynikov299d9d72008-03-22 08:53:09 +000064#if defined (__GNUC__)
65extern "C" void __register_frame(void*);
66#endif
67
Chris Lattner34c94332007-12-06 01:34:04 +000068/// createJIT - This is the factory method for creating a JIT for the current
69/// machine, it does not fall back to the interpreter. This takes ownership
70/// of the module provider.
71ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP,
72 std::string *ErrorStr,
73 JITMemoryManager *JMM) {
74 ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM);
75 if (!EE) return 0;
76
Anton Korobeynikov299d9d72008-03-22 08:53:09 +000077 // Register routine for informing unwinding runtime about new EH frames
78#if defined(__GNUC__)
79 EE->InstallExceptionTableRegister(__register_frame);
80#endif
81
Chris Lattner34c94332007-12-06 01:34:04 +000082 // Make sure we can resolve symbols in the program as well. The zero arg
83 // to the function tells DynamicLibrary to load the program, not a library.
84 sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr);
85 return EE;
86}
87
88JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
89 JITMemoryManager *JMM)
Chris Lattner2c639ad2007-04-20 22:40:05 +000090 : ExecutionEngine(MP), TM(tm), TJI(tji), jitstate(MP) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000091 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000092
93 // Initialize MCE
Chris Lattner34c94332007-12-06 01:34:04 +000094 MCE = createEmitter(*this, JMM);
Misha Brukmanf976c852005-04-21 22:55:34 +000095
Brian Gaeke50872d52004-04-14 17:45:52 +000096 // Add target data
Reid Spenceree448632005-07-12 15:51:55 +000097 MutexGuard locked(lock);
Chris Lattner2c639ad2007-04-20 22:40:05 +000098 FunctionPassManager &PM = jitstate.getPM(locked);
Chris Lattnerb93b0342006-05-04 21:18:40 +000099 PM.add(new TargetData(*TM.getTargetData()));
Brian Gaeke50872d52004-04-14 17:45:52 +0000100
Chris Lattner4d326fa2003-12-20 01:46:27 +0000101 // Turn the machine code intermediate representation into bytes in memory that
102 // may be executed.
Chris Lattner1911fd42006-09-04 04:14:57 +0000103 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
Bill Wendling832171c2006-12-07 20:04:42 +0000104 cerr << "Target does not support machine code emission!\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +0000105 abort();
106 }
Chris Lattner1911fd42006-09-04 04:14:57 +0000107
108 // Initialize passes.
109 PM.doInitialization();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000110}
111
Chris Lattner4d326fa2003-12-20 01:46:27 +0000112JIT::~JIT() {
113 delete MCE;
114 delete &TM;
115}
116
Brian Gaeke70975ee2003-09-05 18:42:01 +0000117/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +0000118///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000119GenericValue JIT::runFunction(Function *F,
120 const std::vector<GenericValue> &ArgValues) {
Chris Lattnerb47130c2004-08-15 23:29:50 +0000121 assert(F && "Function *F was null at entry to run()");
Chris Lattnerb47130c2004-08-15 23:29:50 +0000122
123 void *FPtr = getPointerToFunction(F);
Chris Lattner7c45d782004-08-15 23:31:43 +0000124 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000125 const FunctionType *FTy = F->getFunctionType();
Chris Lattnere5eab142004-08-15 23:53:06 +0000126 const Type *RetTy = FTy->getReturnType();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000127
Chris Lattnere5eab142004-08-15 23:53:06 +0000128 assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
129 "Too many arguments passed into function!");
130 assert(FTy->getNumParams() == ArgValues.size() &&
131 "This doesn't support passing arguments through varargs (yet)!");
132
Misha Brukmanec843022004-10-22 23:35:57 +0000133 // Handle some common cases first. These cases correspond to common `main'
Chris Lattnere5eab142004-08-15 23:53:06 +0000134 // prototypes.
Chris Lattner79e038a2007-08-08 16:19:57 +0000135 if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000136 switch (ArgValues.size()) {
137 case 3:
Chris Lattner79e038a2007-08-08 16:19:57 +0000138 if (FTy->getParamType(0) == Type::Int32Ty &&
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000139 isa<PointerType>(FTy->getParamType(1)) &&
140 isa<PointerType>(FTy->getParamType(2))) {
141 int (*PF)(int, char **, const char **) =
Chris Lattner870286a2006-06-01 17:29:22 +0000142 (int(*)(int, char **, const char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000143
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000144 // Call the function.
Chris Lattnere5eab142004-08-15 23:53:06 +0000145 GenericValue rv;
Reid Spencer38f6a152007-03-06 03:11:31 +0000146 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
147 (char **)GVTOP(ArgValues[1]),
148 (const char **)GVTOP(ArgValues[2])));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000149 return rv;
150 }
151 break;
Chris Lattner174f2262004-08-16 01:07:04 +0000152 case 2:
Chris Lattner79e038a2007-08-08 16:19:57 +0000153 if (FTy->getParamType(0) == Type::Int32Ty &&
Chris Lattner174f2262004-08-16 01:07:04 +0000154 isa<PointerType>(FTy->getParamType(1))) {
Chris Lattner870286a2006-06-01 17:29:22 +0000155 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000156
Chris Lattner174f2262004-08-16 01:07:04 +0000157 // Call the function.
158 GenericValue rv;
Reid Spencer38f6a152007-03-06 03:11:31 +0000159 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
160 (char **)GVTOP(ArgValues[1])));
Chris Lattner174f2262004-08-16 01:07:04 +0000161 return rv;
162 }
163 break;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000164 case 1:
165 if (FTy->getNumParams() == 1 &&
Chris Lattner79e038a2007-08-08 16:19:57 +0000166 FTy->getParamType(0) == Type::Int32Ty) {
Chris Lattnere5eab142004-08-15 23:53:06 +0000167 GenericValue rv;
Chris Lattner870286a2006-06-01 17:29:22 +0000168 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
Reid Spencer38f6a152007-03-06 03:11:31 +0000169 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000170 return rv;
171 }
172 break;
Chris Lattnere5eab142004-08-15 23:53:06 +0000173 }
174 }
175
176 // Handle cases where no arguments are passed first.
177 if (ArgValues.empty()) {
178 GenericValue rv;
179 switch (RetTy->getTypeID()) {
180 default: assert(0 && "Unknown return type for function call!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000181 case Type::IntegerTyID: {
182 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
183 if (BitWidth == 1)
Reid Spencer38f6a152007-03-06 03:11:31 +0000184 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000185 else if (BitWidth <= 8)
Reid Spencer38f6a152007-03-06 03:11:31 +0000186 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000187 else if (BitWidth <= 16)
Reid Spencer38f6a152007-03-06 03:11:31 +0000188 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000189 else if (BitWidth <= 32)
Reid Spencer38f6a152007-03-06 03:11:31 +0000190 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000191 else if (BitWidth <= 64)
Reid Spencer38f6a152007-03-06 03:11:31 +0000192 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000193 else
194 assert(0 && "Integer types > 64 bits not supported");
Chris Lattnerd297aea2004-08-15 23:34:48 +0000195 return rv;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000196 }
Chris Lattnere5eab142004-08-15 23:53:06 +0000197 case Type::VoidTyID:
Reid Spencer38f6a152007-03-06 03:11:31 +0000198 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
Chris Lattnere5eab142004-08-15 23:53:06 +0000199 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000200 case Type::FloatTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000201 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000202 return rv;
203 case Type::DoubleTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000204 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000205 return rv;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000206 case Type::X86_FP80TyID:
207 case Type::FP128TyID:
208 case Type::PPC_FP128TyID:
209 assert(0 && "long double not supported yet");
210 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000211 case Type::PointerTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000212 return PTOGV(((void*(*)())(intptr_t)FPtr)());
Chris Lattnerd297aea2004-08-15 23:34:48 +0000213 }
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000214 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000215
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000216 // Okay, this is not one of our quick and easy cases. Because we don't have a
217 // full FFI, we have to codegen a nullary stub function that just calls the
218 // function we are interested in, passing in constants for all of the
219 // arguments. Make this function and return.
220
221 // First, create the function.
222 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
Gabor Greif051a9502008-04-06 20:25:17 +0000223 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
224 F->getParent());
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000225
226 // Insert a basic block.
Gabor Greif051a9502008-04-06 20:25:17 +0000227 BasicBlock *StubBB = BasicBlock::Create("", Stub);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000228
229 // Convert all of the GenericValue arguments over to constants. Note that we
230 // currently don't support varargs.
Chris Lattner990b8492007-02-13 06:01:22 +0000231 SmallVector<Value*, 8> Args;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000232 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
233 Constant *C = 0;
234 const Type *ArgTy = FTy->getParamType(i);
235 const GenericValue &AV = ArgValues[i];
236 switch (ArgTy->getTypeID()) {
237 default: assert(0 && "Unknown argument type for function call!");
Reid Spencer38f6a152007-03-06 03:11:31 +0000238 case Type::IntegerTyID: C = ConstantInt::get(AV.IntVal); break;
Dale Johannesen43421b32007-09-06 18:13:44 +0000239 case Type::FloatTyID: C = ConstantFP ::get(ArgTy, APFloat(AV.FloatVal));
240 break;
241 case Type::DoubleTyID: C = ConstantFP ::get(ArgTy, APFloat(AV.DoubleVal));
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000242 break;
243 case Type::PPC_FP128TyID:
244 case Type::X86_FP80TyID:
245 case Type::FP128TyID: C = ConstantFP ::get(ArgTy, APFloat(AV.IntVal));
246 break;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000247 case Type::PointerTyID:
248 void *ArgPtr = GVTOP(AV);
249 if (sizeof(void*) == 4) {
Reid Spencere49661b2006-12-31 05:51:36 +0000250 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000251 } else {
Reid Spencere49661b2006-12-31 05:51:36 +0000252 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000253 }
Reid Spencer15f46d62006-12-12 01:17:41 +0000254 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000255 break;
256 }
257 Args.push_back(C);
258 }
259
Gabor Greif051a9502008-04-06 20:25:17 +0000260 CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(), "", StubBB);
Chris Lattnera471e042005-05-06 06:48:54 +0000261 TheCall->setTailCall();
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000262 if (TheCall->getType() != Type::VoidTy)
Gabor Greif051a9502008-04-06 20:25:17 +0000263 ReturnInst::Create(TheCall, StubBB); // Return result of the call.
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000264 else
Gabor Greif051a9502008-04-06 20:25:17 +0000265 ReturnInst::Create(StubBB); // Just return void.
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000266
267 // Finally, return the value returned by our nullary stub function.
268 return runFunction(Stub, std::vector<GenericValue>());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000269}
Chris Lattner4d326fa2003-12-20 01:46:27 +0000270
271/// runJITOnFunction - Run the FunctionPassManager full of
272/// just-in-time compilation passes on F, hopefully filling in
273/// GlobalAddress[F] with the address of F's machine code.
274///
275void JIT::runJITOnFunction(Function *F) {
276 static bool isAlreadyCodeGenerating = false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000277
Reid Spenceree448632005-07-12 15:51:55 +0000278 MutexGuard locked(lock);
Chris Lattner17f218e2007-08-13 20:08:16 +0000279 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Chris Lattner4d326fa2003-12-20 01:46:27 +0000280
281 // JIT the function
282 isAlreadyCodeGenerating = true;
Chris Lattner2c639ad2007-04-20 22:40:05 +0000283 jitstate.getPM(locked).run(*F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000284 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +0000285
286 // If the function referred to a global variable that had not yet been
287 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
288 // all of these globals now.
Chris Lattner2c639ad2007-04-20 22:40:05 +0000289 while (!jitstate.getPendingGlobals(locked).empty()) {
290 const GlobalVariable *GV = jitstate.getPendingGlobals(locked).back();
291 jitstate.getPendingGlobals(locked).pop_back();
Chris Lattnerc07ed132003-12-20 03:36:47 +0000292 EmitGlobalVariable(GV);
293 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000294}
295
296/// getPointerToFunction - This method is used to get the address of the
297/// specified function, compiling it if neccesary.
298///
299void *JIT::getPointerToFunction(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000300 MutexGuard locked(lock);
301
Chris Lattnerc07ed132003-12-20 03:36:47 +0000302 if (void *Addr = getPointerToGlobalIfAvailable(F))
303 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000304
Chris Lattnerfe854032006-08-16 01:24:12 +0000305 // Make sure we read in the function if it exists in this Module.
Gabor Greifa99be512007-07-05 17:07:56 +0000306 if (F->hasNotBeenReadFromBitcode()) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000307 // Determine the module provider this function is provided by.
308 Module *M = F->getParent();
309 ModuleProvider *MP = 0;
310 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
311 if (Modules[i]->getModule() == M) {
312 MP = Modules[i];
313 break;
314 }
315 }
316 assert(MP && "Function isn't in a module we know about!");
317
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000318 std::string ErrorMsg;
319 if (MP->materializeFunction(F, &ErrorMsg)) {
Bill Wendling832171c2006-12-07 20:04:42 +0000320 cerr << "Error reading function '" << F->getName()
Gabor Greifa99be512007-07-05 17:07:56 +0000321 << "' from bitcode file: " << ErrorMsg << "\n";
Chris Lattner0050ef82004-11-15 23:18:09 +0000322 abort();
323 }
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000324 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000325
Reid Spencer5cbf9852007-01-30 20:08:39 +0000326 if (F->isDeclaration()) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000327 void *Addr = getPointerToNamedFunction(F->getName());
328 addGlobalMapping(F, Addr);
329 return Addr;
330 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000331
332 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000333
334 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000335 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
336 return Addr;
337}
338
Chris Lattnerc07ed132003-12-20 03:36:47 +0000339/// getOrEmitGlobalVariable - Return the address of the specified global
340/// variable, possibly emitting it to memory if needed. This is used by the
341/// Emitter.
342void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
Reid Spenceree448632005-07-12 15:51:55 +0000343 MutexGuard locked(lock);
344
Chris Lattnerc07ed132003-12-20 03:36:47 +0000345 void *Ptr = getPointerToGlobalIfAvailable(GV);
346 if (Ptr) return Ptr;
347
348 // If the global is external, just remember the address.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000349 if (GV->isDeclaration()) {
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +0000350#if HAVE___DSO_HANDLE
Evan Cheng5f42c552006-07-21 23:06:20 +0000351 if (GV->getName() == "__dso_handle")
352 return (void*)&__dso_handle;
Evan Chengb82ab942006-07-22 00:42:03 +0000353#endif
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000354 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
Chris Lattnerc07ed132003-12-20 03:36:47 +0000355 if (Ptr == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000356 cerr << "Could not resolve external global address: "
357 << GV->getName() << "\n";
Chris Lattnerc07ed132003-12-20 03:36:47 +0000358 abort();
359 }
360 } else {
361 // If the global hasn't been emitted to memory yet, allocate space. We will
362 // actually initialize the global after current function has finished
363 // compilation.
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000364 const Type *GlobalType = GV->getType()->getElementType();
Duncan Sands514ab342007-11-01 20:53:16 +0000365 size_t S = getTargetData()->getABITypeSize(GlobalType);
Duncan Sandsd1025932008-01-29 06:23:44 +0000366 size_t A = getTargetData()->getPreferredAlignment(GV);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000367 if (A <= 8) {
368 Ptr = malloc(S);
369 } else {
370 // Allocate S+A bytes of memory, then use an aligned pointer within that
371 // space.
372 Ptr = malloc(S+A);
373 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
Chris Lattner21c39a62006-07-12 00:31:47 +0000374 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000375 }
Chris Lattner2c639ad2007-04-20 22:40:05 +0000376 jitstate.getPendingGlobals(locked).push_back(GV);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000377 }
378 addGlobalMapping(GV, Ptr);
379 return Ptr;
380}
381
382
Chris Lattner4d326fa2003-12-20 01:46:27 +0000383/// recompileAndRelinkFunction - This method is used to force a function
384/// which has already been compiled, to be compiled again, possibly
385/// after it has been modified. Then the entry to the old copy is overwritten
386/// with a branch to the new copy. If there was no old copy, this acts
387/// just like JIT::getPointerToFunction().
388///
389void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000390 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000391
Chris Lattnerc07ed132003-12-20 03:36:47 +0000392 // If it's not already compiled there is no reason to patch it up.
393 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000394
Chris Lattnerc07ed132003-12-20 03:36:47 +0000395 // Delete the old function mapping.
396 addGlobalMapping(F, 0);
397
Chris Lattnerc07ed132003-12-20 03:36:47 +0000398 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000399 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000400
401 // Update state, forward the old function to the new function.
402 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000403 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
404 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
405 return Addr;
406}
Misha Brukman895eddf2004-11-07 23:58:46 +0000407