blob: d4f190bfc76538076f8742e2e3a82341356ebd8e [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"
Brian Gaeke97222942003-09-05 19:39:22 +000023#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnere7fd5532006-05-08 22:00:52 +000024#include "llvm/Support/MutexGuard.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000025#include "llvm/System/DynamicLibrary.h"
Owen Anderson07000c62006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000027#include "llvm/Target/TargetMachine.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000028#include "llvm/Target/TargetJITInfo.h"
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +000029
30#include "llvm/Config/config.h"
31
Chris Lattnerc19aade2003-12-08 08:06:28 +000032using namespace llvm;
Misha Brukmanabb027c2003-05-27 21:40:39 +000033
Nate Begemanb76ea742006-07-22 16:59:38 +000034#ifdef __APPLE__
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +000035// Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
36// of atexit). It passes the address of linker generated symbol __dso_handle
37// to the function.
38// This configuration change happened at version 5330.
39# include <AvailabilityMacros.h>
40# if defined(MAC_OS_X_VERSION_10_4) && \
41 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
42 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
43 __APPLE_CC__ >= 5330))
44# ifndef HAVE___DSO_HANDLE
45# define HAVE___DSO_HANDLE 1
46# endif
47# endif
Evan Cheng5f42c552006-07-21 23:06:20 +000048#endif
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +000049
50#if HAVE___DSO_HANDLE
51extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
Nate Begemanb76ea742006-07-22 16:59:38 +000052#endif
Evan Cheng5f42c552006-07-21 23:06:20 +000053
Dan Gohman844731a2008-05-13 00:00:25 +000054namespace {
55
Chris Lattner2fe4bb02006-03-22 06:07:50 +000056static struct RegisterJIT {
57 RegisterJIT() { JIT::Register(); }
58} JITRegistrator;
59
Dan Gohman844731a2008-05-13 00:00:25 +000060}
61
Jeff Cohen2f519142006-03-24 02:53:49 +000062namespace llvm {
63 void LinkInJIT() {
64 }
65}
66
Anton Korobeynikov299d9d72008-03-22 08:53:09 +000067#if defined (__GNUC__)
68extern "C" void __register_frame(void*);
69#endif
70
Chris Lattner34c94332007-12-06 01:34:04 +000071/// createJIT - This is the factory method for creating a JIT for the current
72/// machine, it does not fall back to the interpreter. This takes ownership
73/// of the module provider.
74ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP,
75 std::string *ErrorStr,
76 JITMemoryManager *JMM) {
77 ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM);
78 if (!EE) return 0;
79
Anton Korobeynikov299d9d72008-03-22 08:53:09 +000080 // Register routine for informing unwinding runtime about new EH frames
81#if defined(__GNUC__)
82 EE->InstallExceptionTableRegister(__register_frame);
83#endif
84
Chris Lattner34c94332007-12-06 01:34:04 +000085 // Make sure we can resolve symbols in the program as well. The zero arg
86 // to the function tells DynamicLibrary to load the program, not a library.
87 sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr);
88 return EE;
89}
90
91JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
92 JITMemoryManager *JMM)
Nate Begemanf049e072008-05-21 16:34:48 +000093 : ExecutionEngine(MP), TM(tm), TJI(tji) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000094 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000095
Nate Begemanf049e072008-05-21 16:34:48 +000096 jitstate = new JITState(MP);
97
Misha Brukmanabb027c2003-05-27 21:40:39 +000098 // Initialize MCE
Chris Lattner34c94332007-12-06 01:34:04 +000099 MCE = createEmitter(*this, JMM);
Misha Brukmanf976c852005-04-21 22:55:34 +0000100
Brian Gaeke50872d52004-04-14 17:45:52 +0000101 // Add target data
Reid Spenceree448632005-07-12 15:51:55 +0000102 MutexGuard locked(lock);
Nate Begemanf049e072008-05-21 16:34:48 +0000103 FunctionPassManager &PM = jitstate->getPM(locked);
Chris Lattnerb93b0342006-05-04 21:18:40 +0000104 PM.add(new TargetData(*TM.getTargetData()));
Brian Gaeke50872d52004-04-14 17:45:52 +0000105
Chris Lattner4d326fa2003-12-20 01:46:27 +0000106 // Turn the machine code intermediate representation into bytes in memory that
107 // may be executed.
Chris Lattner1911fd42006-09-04 04:14:57 +0000108 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
Bill Wendling832171c2006-12-07 20:04:42 +0000109 cerr << "Target does not support machine code emission!\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +0000110 abort();
111 }
Chris Lattner1911fd42006-09-04 04:14:57 +0000112
113 // Initialize passes.
114 PM.doInitialization();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000115}
116
Chris Lattner4d326fa2003-12-20 01:46:27 +0000117JIT::~JIT() {
Nate Begemanf049e072008-05-21 16:34:48 +0000118 delete jitstate;
Chris Lattner4d326fa2003-12-20 01:46:27 +0000119 delete MCE;
120 delete &TM;
121}
122
Nate Begemanf049e072008-05-21 16:34:48 +0000123/// addModuleProvider - Add a new ModuleProvider to the JIT. If we previously
124/// removed the last ModuleProvider, we need re-initialize jitstate with a valid
125/// ModuleProvider.
126void JIT::addModuleProvider(ModuleProvider *MP) {
127 MutexGuard locked(lock);
128
129 if (Modules.empty()) {
130 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
131
132 jitstate = new JITState(MP);
133
134 FunctionPassManager &PM = jitstate->getPM(locked);
135 PM.add(new TargetData(*TM.getTargetData()));
136
137 // Turn the machine code intermediate representation into bytes in memory
138 // that may be executed.
139 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
140 cerr << "Target does not support machine code emission!\n";
141 abort();
142 }
143
144 // Initialize passes.
145 PM.doInitialization();
146 }
147
148 ExecutionEngine::addModuleProvider(MP);
149}
150
151/// removeModuleProvider - If we are removing the last ModuleProvider,
152/// invalidate the jitstate since the PassManager it contains references a
153/// released ModuleProvider.
154Module *JIT::removeModuleProvider(ModuleProvider *MP, std::string *E) {
155 Module *result = ExecutionEngine::removeModuleProvider(MP, E);
156
157 MutexGuard locked(lock);
158 if (Modules.empty()) {
159 delete jitstate;
160 jitstate = 0;
161 }
162
163 return result;
164}
165
Brian Gaeke70975ee2003-09-05 18:42:01 +0000166/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +0000167///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000168GenericValue JIT::runFunction(Function *F,
169 const std::vector<GenericValue> &ArgValues) {
Chris Lattnerb47130c2004-08-15 23:29:50 +0000170 assert(F && "Function *F was null at entry to run()");
Chris Lattnerb47130c2004-08-15 23:29:50 +0000171
172 void *FPtr = getPointerToFunction(F);
Chris Lattner7c45d782004-08-15 23:31:43 +0000173 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000174 const FunctionType *FTy = F->getFunctionType();
Chris Lattnere5eab142004-08-15 23:53:06 +0000175 const Type *RetTy = FTy->getReturnType();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000176
Chris Lattnere5eab142004-08-15 23:53:06 +0000177 assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
178 "Too many arguments passed into function!");
179 assert(FTy->getNumParams() == ArgValues.size() &&
180 "This doesn't support passing arguments through varargs (yet)!");
181
Misha Brukmanec843022004-10-22 23:35:57 +0000182 // Handle some common cases first. These cases correspond to common `main'
Chris Lattnere5eab142004-08-15 23:53:06 +0000183 // prototypes.
Chris Lattner79e038a2007-08-08 16:19:57 +0000184 if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000185 switch (ArgValues.size()) {
186 case 3:
Chris Lattner79e038a2007-08-08 16:19:57 +0000187 if (FTy->getParamType(0) == Type::Int32Ty &&
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000188 isa<PointerType>(FTy->getParamType(1)) &&
189 isa<PointerType>(FTy->getParamType(2))) {
190 int (*PF)(int, char **, const char **) =
Chris Lattner870286a2006-06-01 17:29:22 +0000191 (int(*)(int, char **, const char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000192
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000193 // Call the function.
Chris Lattnere5eab142004-08-15 23:53:06 +0000194 GenericValue rv;
Reid Spencer38f6a152007-03-06 03:11:31 +0000195 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
196 (char **)GVTOP(ArgValues[1]),
197 (const char **)GVTOP(ArgValues[2])));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000198 return rv;
199 }
200 break;
Chris Lattner174f2262004-08-16 01:07:04 +0000201 case 2:
Chris Lattner79e038a2007-08-08 16:19:57 +0000202 if (FTy->getParamType(0) == Type::Int32Ty &&
Chris Lattner174f2262004-08-16 01:07:04 +0000203 isa<PointerType>(FTy->getParamType(1))) {
Chris Lattner870286a2006-06-01 17:29:22 +0000204 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000205
Chris Lattner174f2262004-08-16 01:07:04 +0000206 // Call the function.
207 GenericValue rv;
Reid Spencer38f6a152007-03-06 03:11:31 +0000208 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
209 (char **)GVTOP(ArgValues[1])));
Chris Lattner174f2262004-08-16 01:07:04 +0000210 return rv;
211 }
212 break;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000213 case 1:
214 if (FTy->getNumParams() == 1 &&
Chris Lattner79e038a2007-08-08 16:19:57 +0000215 FTy->getParamType(0) == Type::Int32Ty) {
Chris Lattnere5eab142004-08-15 23:53:06 +0000216 GenericValue rv;
Chris Lattner870286a2006-06-01 17:29:22 +0000217 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
Reid Spencer38f6a152007-03-06 03:11:31 +0000218 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000219 return rv;
220 }
221 break;
Chris Lattnere5eab142004-08-15 23:53:06 +0000222 }
223 }
224
225 // Handle cases where no arguments are passed first.
226 if (ArgValues.empty()) {
227 GenericValue rv;
228 switch (RetTy->getTypeID()) {
229 default: assert(0 && "Unknown return type for function call!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000230 case Type::IntegerTyID: {
231 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
232 if (BitWidth == 1)
Reid Spencer38f6a152007-03-06 03:11:31 +0000233 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000234 else if (BitWidth <= 8)
Reid Spencer38f6a152007-03-06 03:11:31 +0000235 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000236 else if (BitWidth <= 16)
Reid Spencer38f6a152007-03-06 03:11:31 +0000237 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000238 else if (BitWidth <= 32)
Reid Spencer38f6a152007-03-06 03:11:31 +0000239 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000240 else if (BitWidth <= 64)
Reid Spencer38f6a152007-03-06 03:11:31 +0000241 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000242 else
243 assert(0 && "Integer types > 64 bits not supported");
Chris Lattnerd297aea2004-08-15 23:34:48 +0000244 return rv;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000245 }
Chris Lattnere5eab142004-08-15 23:53:06 +0000246 case Type::VoidTyID:
Reid Spencer38f6a152007-03-06 03:11:31 +0000247 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
Chris Lattnere5eab142004-08-15 23:53:06 +0000248 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000249 case Type::FloatTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000250 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000251 return rv;
252 case Type::DoubleTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000253 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000254 return rv;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000255 case Type::X86_FP80TyID:
256 case Type::FP128TyID:
257 case Type::PPC_FP128TyID:
258 assert(0 && "long double not supported yet");
259 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000260 case Type::PointerTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000261 return PTOGV(((void*(*)())(intptr_t)FPtr)());
Chris Lattnerd297aea2004-08-15 23:34:48 +0000262 }
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000263 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000264
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000265 // Okay, this is not one of our quick and easy cases. Because we don't have a
266 // full FFI, we have to codegen a nullary stub function that just calls the
267 // function we are interested in, passing in constants for all of the
268 // arguments. Make this function and return.
269
270 // First, create the function.
271 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
Gabor Greif051a9502008-04-06 20:25:17 +0000272 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
273 F->getParent());
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000274
275 // Insert a basic block.
Gabor Greif051a9502008-04-06 20:25:17 +0000276 BasicBlock *StubBB = BasicBlock::Create("", Stub);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000277
278 // Convert all of the GenericValue arguments over to constants. Note that we
279 // currently don't support varargs.
Chris Lattner990b8492007-02-13 06:01:22 +0000280 SmallVector<Value*, 8> Args;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000281 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
282 Constant *C = 0;
283 const Type *ArgTy = FTy->getParamType(i);
284 const GenericValue &AV = ArgValues[i];
285 switch (ArgTy->getTypeID()) {
286 default: assert(0 && "Unknown argument type for function call!");
Chris Lattner02a260a2008-04-20 00:41:09 +0000287 case Type::IntegerTyID:
288 C = ConstantInt::get(AV.IntVal);
289 break;
290 case Type::FloatTyID:
291 C = ConstantFP::get(APFloat(AV.FloatVal));
292 break;
293 case Type::DoubleTyID:
294 C = ConstantFP::get(APFloat(AV.DoubleVal));
295 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000296 case Type::PPC_FP128TyID:
297 case Type::X86_FP80TyID:
Chris Lattner02a260a2008-04-20 00:41:09 +0000298 case Type::FP128TyID:
299 C = ConstantFP::get(APFloat(AV.IntVal));
300 break;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000301 case Type::PointerTyID:
302 void *ArgPtr = GVTOP(AV);
Chris Lattner02a260a2008-04-20 00:41:09 +0000303 if (sizeof(void*) == 4)
Reid Spencere49661b2006-12-31 05:51:36 +0000304 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
Chris Lattner02a260a2008-04-20 00:41:09 +0000305 else
Reid Spencere49661b2006-12-31 05:51:36 +0000306 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
Reid Spencer15f46d62006-12-12 01:17:41 +0000307 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000308 break;
309 }
310 Args.push_back(C);
311 }
312
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000313 CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(),
314 "", StubBB);
Chris Lattnera471e042005-05-06 06:48:54 +0000315 TheCall->setTailCall();
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000316 if (TheCall->getType() != Type::VoidTy)
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000317 ReturnInst::Create(TheCall, StubBB); // Return result of the call.
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000318 else
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000319 ReturnInst::Create(StubBB); // Just return void.
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000320
321 // Finally, return the value returned by our nullary stub function.
322 return runFunction(Stub, std::vector<GenericValue>());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000323}
Chris Lattner4d326fa2003-12-20 01:46:27 +0000324
325/// runJITOnFunction - Run the FunctionPassManager full of
326/// just-in-time compilation passes on F, hopefully filling in
327/// GlobalAddress[F] with the address of F's machine code.
328///
329void JIT::runJITOnFunction(Function *F) {
330 static bool isAlreadyCodeGenerating = false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000331
Reid Spenceree448632005-07-12 15:51:55 +0000332 MutexGuard locked(lock);
Chris Lattner17f218e2007-08-13 20:08:16 +0000333 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Chris Lattner4d326fa2003-12-20 01:46:27 +0000334
335 // JIT the function
336 isAlreadyCodeGenerating = true;
Nate Begemanf049e072008-05-21 16:34:48 +0000337 jitstate->getPM(locked).run(*F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000338 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +0000339
340 // If the function referred to a global variable that had not yet been
341 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
342 // all of these globals now.
Nate Begemanf049e072008-05-21 16:34:48 +0000343 while (!jitstate->getPendingGlobals(locked).empty()) {
344 const GlobalVariable *GV = jitstate->getPendingGlobals(locked).back();
345 jitstate->getPendingGlobals(locked).pop_back();
Chris Lattnerc07ed132003-12-20 03:36:47 +0000346 EmitGlobalVariable(GV);
347 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000348}
349
350/// getPointerToFunction - This method is used to get the address of the
351/// specified function, compiling it if neccesary.
352///
353void *JIT::getPointerToFunction(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000354
Chris Lattnerc07ed132003-12-20 03:36:47 +0000355 if (void *Addr = getPointerToGlobalIfAvailable(F))
356 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000357
Chris Lattnerfe854032006-08-16 01:24:12 +0000358 // Make sure we read in the function if it exists in this Module.
Gabor Greifa99be512007-07-05 17:07:56 +0000359 if (F->hasNotBeenReadFromBitcode()) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000360 // Determine the module provider this function is provided by.
361 Module *M = F->getParent();
362 ModuleProvider *MP = 0;
363 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
364 if (Modules[i]->getModule() == M) {
365 MP = Modules[i];
366 break;
367 }
368 }
369 assert(MP && "Function isn't in a module we know about!");
370
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000371 std::string ErrorMsg;
372 if (MP->materializeFunction(F, &ErrorMsg)) {
Bill Wendling832171c2006-12-07 20:04:42 +0000373 cerr << "Error reading function '" << F->getName()
Gabor Greifa99be512007-07-05 17:07:56 +0000374 << "' from bitcode file: " << ErrorMsg << "\n";
Chris Lattner0050ef82004-11-15 23:18:09 +0000375 abort();
376 }
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000377 }
Nicolas Geoffrayfd7d9912008-04-20 08:33:02 +0000378
379 if (void *Addr = getPointerToGlobalIfAvailable(F)) {
380 return Addr;
381 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000382
Nicolas Geoffrayfd7d9912008-04-20 08:33:02 +0000383 MutexGuard locked(lock);
384
Reid Spencer5cbf9852007-01-30 20:08:39 +0000385 if (F->isDeclaration()) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000386 void *Addr = getPointerToNamedFunction(F->getName());
387 addGlobalMapping(F, Addr);
388 return Addr;
389 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000390
391 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000392
393 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000394 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
395 return Addr;
396}
397
Chris Lattnerc07ed132003-12-20 03:36:47 +0000398/// getOrEmitGlobalVariable - Return the address of the specified global
399/// variable, possibly emitting it to memory if needed. This is used by the
400/// Emitter.
401void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
Reid Spenceree448632005-07-12 15:51:55 +0000402 MutexGuard locked(lock);
403
Chris Lattnerc07ed132003-12-20 03:36:47 +0000404 void *Ptr = getPointerToGlobalIfAvailable(GV);
405 if (Ptr) return Ptr;
406
407 // If the global is external, just remember the address.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000408 if (GV->isDeclaration()) {
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +0000409#if HAVE___DSO_HANDLE
Evan Cheng5f42c552006-07-21 23:06:20 +0000410 if (GV->getName() == "__dso_handle")
411 return (void*)&__dso_handle;
Evan Chengb82ab942006-07-22 00:42:03 +0000412#endif
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000413 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
Chris Lattnerc07ed132003-12-20 03:36:47 +0000414 if (Ptr == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000415 cerr << "Could not resolve external global address: "
416 << GV->getName() << "\n";
Chris Lattnerc07ed132003-12-20 03:36:47 +0000417 abort();
Dale Johannesendd947ea2008-08-07 01:30:15 +0000418 addGlobalMapping(GV, Ptr);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000419 }
420 } else {
Dale Johannesendd947ea2008-08-07 01:30:15 +0000421 // If the global hasn't been emitted to memory yet, allocate space and
422 // emit it into memory. It goes in the same array as the generated
423 // code, jump tables, etc.
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000424 const Type *GlobalType = GV->getType()->getElementType();
Duncan Sands514ab342007-11-01 20:53:16 +0000425 size_t S = getTargetData()->getABITypeSize(GlobalType);
Duncan Sandsd1025932008-01-29 06:23:44 +0000426 size_t A = getTargetData()->getPreferredAlignment(GV);
Dale Johannesendd947ea2008-08-07 01:30:15 +0000427 Ptr = MCE->allocateSpace(S, A);
428 addGlobalMapping(GV, Ptr);
429 EmitGlobalVariable(GV);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000430 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000431 return Ptr;
432}
433
Chris Lattner4d326fa2003-12-20 01:46:27 +0000434/// recompileAndRelinkFunction - This method is used to force a function
435/// which has already been compiled, to be compiled again, possibly
436/// after it has been modified. Then the entry to the old copy is overwritten
437/// with a branch to the new copy. If there was no old copy, this acts
438/// just like JIT::getPointerToFunction().
439///
440void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000441 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000442
Chris Lattnerc07ed132003-12-20 03:36:47 +0000443 // If it's not already compiled there is no reason to patch it up.
444 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000445
Chris Lattnerc07ed132003-12-20 03:36:47 +0000446 // Delete the old function mapping.
447 addGlobalMapping(F, 0);
448
Chris Lattnerc07ed132003-12-20 03:36:47 +0000449 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000450 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000451
452 // Update state, forward the old function to the new function.
453 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000454 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
455 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
456 return Addr;
457}
Misha Brukman895eddf2004-11-07 23:58:46 +0000458