blob: 4b888ef018ce42c121557d1d1985d8b00eeb3d22 [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
Dan Gohman844731a2008-05-13 00:00:25 +000055namespace {
56
Chris Lattner2fe4bb02006-03-22 06:07:50 +000057static struct RegisterJIT {
58 RegisterJIT() { JIT::Register(); }
59} JITRegistrator;
60
Dan Gohman844731a2008-05-13 00:00:25 +000061}
62
Jeff Cohen2f519142006-03-24 02:53:49 +000063namespace llvm {
64 void LinkInJIT() {
65 }
66}
67
Anton Korobeynikov299d9d72008-03-22 08:53:09 +000068#if defined (__GNUC__)
69extern "C" void __register_frame(void*);
70#endif
71
Chris Lattner34c94332007-12-06 01:34:04 +000072/// createJIT - This is the factory method for creating a JIT for the current
73/// machine, it does not fall back to the interpreter. This takes ownership
74/// of the module provider.
75ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP,
76 std::string *ErrorStr,
77 JITMemoryManager *JMM) {
78 ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM);
79 if (!EE) return 0;
80
Anton Korobeynikov299d9d72008-03-22 08:53:09 +000081 // Register routine for informing unwinding runtime about new EH frames
82#if defined(__GNUC__)
83 EE->InstallExceptionTableRegister(__register_frame);
84#endif
85
Chris Lattner34c94332007-12-06 01:34:04 +000086 // Make sure we can resolve symbols in the program as well. The zero arg
87 // to the function tells DynamicLibrary to load the program, not a library.
88 sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr);
89 return EE;
90}
91
92JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
93 JITMemoryManager *JMM)
Chris Lattner2c639ad2007-04-20 22:40:05 +000094 : ExecutionEngine(MP), TM(tm), TJI(tji), jitstate(MP) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000095 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000096
97 // Initialize MCE
Chris Lattner34c94332007-12-06 01:34:04 +000098 MCE = createEmitter(*this, JMM);
Misha Brukmanf976c852005-04-21 22:55:34 +000099
Brian Gaeke50872d52004-04-14 17:45:52 +0000100 // Add target data
Reid Spenceree448632005-07-12 15:51:55 +0000101 MutexGuard locked(lock);
Chris Lattner2c639ad2007-04-20 22:40:05 +0000102 FunctionPassManager &PM = jitstate.getPM(locked);
Chris Lattnerb93b0342006-05-04 21:18:40 +0000103 PM.add(new TargetData(*TM.getTargetData()));
Brian Gaeke50872d52004-04-14 17:45:52 +0000104
Chris Lattner4d326fa2003-12-20 01:46:27 +0000105 // Turn the machine code intermediate representation into bytes in memory that
106 // may be executed.
Chris Lattner1911fd42006-09-04 04:14:57 +0000107 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
Bill Wendling832171c2006-12-07 20:04:42 +0000108 cerr << "Target does not support machine code emission!\n";
Chris Lattner4d326fa2003-12-20 01:46:27 +0000109 abort();
110 }
Chris Lattner1911fd42006-09-04 04:14:57 +0000111
112 // Initialize passes.
113 PM.doInitialization();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000114}
115
Chris Lattner4d326fa2003-12-20 01:46:27 +0000116JIT::~JIT() {
117 delete MCE;
118 delete &TM;
119}
120
Brian Gaeke70975ee2003-09-05 18:42:01 +0000121/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +0000122///
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000123GenericValue JIT::runFunction(Function *F,
124 const std::vector<GenericValue> &ArgValues) {
Chris Lattnerb47130c2004-08-15 23:29:50 +0000125 assert(F && "Function *F was null at entry to run()");
Chris Lattnerb47130c2004-08-15 23:29:50 +0000126
127 void *FPtr = getPointerToFunction(F);
Chris Lattner7c45d782004-08-15 23:31:43 +0000128 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000129 const FunctionType *FTy = F->getFunctionType();
Chris Lattnere5eab142004-08-15 23:53:06 +0000130 const Type *RetTy = FTy->getReturnType();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000131
Chris Lattnere5eab142004-08-15 23:53:06 +0000132 assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
133 "Too many arguments passed into function!");
134 assert(FTy->getNumParams() == ArgValues.size() &&
135 "This doesn't support passing arguments through varargs (yet)!");
136
Misha Brukmanec843022004-10-22 23:35:57 +0000137 // Handle some common cases first. These cases correspond to common `main'
Chris Lattnere5eab142004-08-15 23:53:06 +0000138 // prototypes.
Chris Lattner79e038a2007-08-08 16:19:57 +0000139 if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000140 switch (ArgValues.size()) {
141 case 3:
Chris Lattner79e038a2007-08-08 16:19:57 +0000142 if (FTy->getParamType(0) == Type::Int32Ty &&
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000143 isa<PointerType>(FTy->getParamType(1)) &&
144 isa<PointerType>(FTy->getParamType(2))) {
145 int (*PF)(int, char **, const char **) =
Chris Lattner870286a2006-06-01 17:29:22 +0000146 (int(*)(int, char **, const char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000147
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000148 // Call the function.
Chris Lattnere5eab142004-08-15 23:53:06 +0000149 GenericValue rv;
Reid Spencer38f6a152007-03-06 03:11:31 +0000150 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
151 (char **)GVTOP(ArgValues[1]),
152 (const char **)GVTOP(ArgValues[2])));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000153 return rv;
154 }
155 break;
Chris Lattner174f2262004-08-16 01:07:04 +0000156 case 2:
Chris Lattner79e038a2007-08-08 16:19:57 +0000157 if (FTy->getParamType(0) == Type::Int32Ty &&
Chris Lattner174f2262004-08-16 01:07:04 +0000158 isa<PointerType>(FTy->getParamType(1))) {
Chris Lattner870286a2006-06-01 17:29:22 +0000159 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
Misha Brukmanec843022004-10-22 23:35:57 +0000160
Chris Lattner174f2262004-08-16 01:07:04 +0000161 // Call the function.
162 GenericValue rv;
Reid Spencer38f6a152007-03-06 03:11:31 +0000163 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
164 (char **)GVTOP(ArgValues[1])));
Chris Lattner174f2262004-08-16 01:07:04 +0000165 return rv;
166 }
167 break;
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000168 case 1:
169 if (FTy->getNumParams() == 1 &&
Chris Lattner79e038a2007-08-08 16:19:57 +0000170 FTy->getParamType(0) == Type::Int32Ty) {
Chris Lattnere5eab142004-08-15 23:53:06 +0000171 GenericValue rv;
Chris Lattner870286a2006-06-01 17:29:22 +0000172 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
Reid Spencer38f6a152007-03-06 03:11:31 +0000173 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
Chris Lattnerf7bedf42004-08-15 23:39:59 +0000174 return rv;
175 }
176 break;
Chris Lattnere5eab142004-08-15 23:53:06 +0000177 }
178 }
179
180 // Handle cases where no arguments are passed first.
181 if (ArgValues.empty()) {
182 GenericValue rv;
183 switch (RetTy->getTypeID()) {
184 default: assert(0 && "Unknown return type for function call!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000185 case Type::IntegerTyID: {
186 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
187 if (BitWidth == 1)
Reid Spencer38f6a152007-03-06 03:11:31 +0000188 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000189 else if (BitWidth <= 8)
Reid Spencer38f6a152007-03-06 03:11:31 +0000190 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000191 else if (BitWidth <= 16)
Reid Spencer38f6a152007-03-06 03:11:31 +0000192 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000193 else if (BitWidth <= 32)
Reid Spencer38f6a152007-03-06 03:11:31 +0000194 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000195 else if (BitWidth <= 64)
Reid Spencer38f6a152007-03-06 03:11:31 +0000196 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000197 else
198 assert(0 && "Integer types > 64 bits not supported");
Chris Lattnerd297aea2004-08-15 23:34:48 +0000199 return rv;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000200 }
Chris Lattnere5eab142004-08-15 23:53:06 +0000201 case Type::VoidTyID:
Reid Spencer38f6a152007-03-06 03:11:31 +0000202 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
Chris Lattnere5eab142004-08-15 23:53:06 +0000203 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000204 case Type::FloatTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000205 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000206 return rv;
207 case Type::DoubleTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000208 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
Chris Lattnere5eab142004-08-15 23:53:06 +0000209 return rv;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000210 case Type::X86_FP80TyID:
211 case Type::FP128TyID:
212 case Type::PPC_FP128TyID:
213 assert(0 && "long double not supported yet");
214 return rv;
Chris Lattnere5eab142004-08-15 23:53:06 +0000215 case Type::PointerTyID:
Chris Lattner870286a2006-06-01 17:29:22 +0000216 return PTOGV(((void*(*)())(intptr_t)FPtr)());
Chris Lattnerd297aea2004-08-15 23:34:48 +0000217 }
Chris Lattnerff0f1bb2003-12-26 06:13:47 +0000218 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000219
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000220 // Okay, this is not one of our quick and easy cases. Because we don't have a
221 // full FFI, we have to codegen a nullary stub function that just calls the
222 // function we are interested in, passing in constants for all of the
223 // arguments. Make this function and return.
224
225 // First, create the function.
226 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
Gabor Greif051a9502008-04-06 20:25:17 +0000227 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
228 F->getParent());
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000229
230 // Insert a basic block.
Gabor Greif051a9502008-04-06 20:25:17 +0000231 BasicBlock *StubBB = BasicBlock::Create("", Stub);
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000232
233 // Convert all of the GenericValue arguments over to constants. Note that we
234 // currently don't support varargs.
Chris Lattner990b8492007-02-13 06:01:22 +0000235 SmallVector<Value*, 8> Args;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000236 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
237 Constant *C = 0;
238 const Type *ArgTy = FTy->getParamType(i);
239 const GenericValue &AV = ArgValues[i];
240 switch (ArgTy->getTypeID()) {
241 default: assert(0 && "Unknown argument type for function call!");
Chris Lattner02a260a2008-04-20 00:41:09 +0000242 case Type::IntegerTyID:
243 C = ConstantInt::get(AV.IntVal);
244 break;
245 case Type::FloatTyID:
246 C = ConstantFP::get(APFloat(AV.FloatVal));
247 break;
248 case Type::DoubleTyID:
249 C = ConstantFP::get(APFloat(AV.DoubleVal));
250 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000251 case Type::PPC_FP128TyID:
252 case Type::X86_FP80TyID:
Chris Lattner02a260a2008-04-20 00:41:09 +0000253 case Type::FP128TyID:
254 C = ConstantFP::get(APFloat(AV.IntVal));
255 break;
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000256 case Type::PointerTyID:
257 void *ArgPtr = GVTOP(AV);
Chris Lattner02a260a2008-04-20 00:41:09 +0000258 if (sizeof(void*) == 4)
Reid Spencere49661b2006-12-31 05:51:36 +0000259 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
Chris Lattner02a260a2008-04-20 00:41:09 +0000260 else
Reid Spencere49661b2006-12-31 05:51:36 +0000261 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
Reid Spencer15f46d62006-12-12 01:17:41 +0000262 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000263 break;
264 }
265 Args.push_back(C);
266 }
267
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000268 CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(),
269 "", StubBB);
Chris Lattnera471e042005-05-06 06:48:54 +0000270 TheCall->setTailCall();
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000271 if (TheCall->getType() != Type::VoidTy)
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000272 ReturnInst::Create(TheCall, StubBB); // Return result of the call.
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000273 else
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000274 ReturnInst::Create(StubBB); // Just return void.
Chris Lattnercc22e9f2004-08-16 00:14:18 +0000275
276 // Finally, return the value returned by our nullary stub function.
277 return runFunction(Stub, std::vector<GenericValue>());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000278}
Chris Lattner4d326fa2003-12-20 01:46:27 +0000279
280/// runJITOnFunction - Run the FunctionPassManager full of
281/// just-in-time compilation passes on F, hopefully filling in
282/// GlobalAddress[F] with the address of F's machine code.
283///
284void JIT::runJITOnFunction(Function *F) {
285 static bool isAlreadyCodeGenerating = false;
Jeff Cohen00b168892005-07-27 06:12:32 +0000286
Reid Spenceree448632005-07-12 15:51:55 +0000287 MutexGuard locked(lock);
Chris Lattner17f218e2007-08-13 20:08:16 +0000288 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Chris Lattner4d326fa2003-12-20 01:46:27 +0000289
290 // JIT the function
291 isAlreadyCodeGenerating = true;
Chris Lattner2c639ad2007-04-20 22:40:05 +0000292 jitstate.getPM(locked).run(*F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000293 isAlreadyCodeGenerating = false;
Chris Lattnerc07ed132003-12-20 03:36:47 +0000294
295 // If the function referred to a global variable that had not yet been
296 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
297 // all of these globals now.
Chris Lattner2c639ad2007-04-20 22:40:05 +0000298 while (!jitstate.getPendingGlobals(locked).empty()) {
299 const GlobalVariable *GV = jitstate.getPendingGlobals(locked).back();
300 jitstate.getPendingGlobals(locked).pop_back();
Chris Lattnerc07ed132003-12-20 03:36:47 +0000301 EmitGlobalVariable(GV);
302 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000303}
304
305/// getPointerToFunction - This method is used to get the address of the
306/// specified function, compiling it if neccesary.
307///
308void *JIT::getPointerToFunction(Function *F) {
Reid Spenceree448632005-07-12 15:51:55 +0000309
Chris Lattnerc07ed132003-12-20 03:36:47 +0000310 if (void *Addr = getPointerToGlobalIfAvailable(F))
311 return Addr; // Check if function already code gen'd
Chris Lattner4d326fa2003-12-20 01:46:27 +0000312
Chris Lattnerfe854032006-08-16 01:24:12 +0000313 // Make sure we read in the function if it exists in this Module.
Gabor Greifa99be512007-07-05 17:07:56 +0000314 if (F->hasNotBeenReadFromBitcode()) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000315 // Determine the module provider this function is provided by.
316 Module *M = F->getParent();
317 ModuleProvider *MP = 0;
318 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
319 if (Modules[i]->getModule() == M) {
320 MP = Modules[i];
321 break;
322 }
323 }
324 assert(MP && "Function isn't in a module we know about!");
325
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000326 std::string ErrorMsg;
327 if (MP->materializeFunction(F, &ErrorMsg)) {
Bill Wendling832171c2006-12-07 20:04:42 +0000328 cerr << "Error reading function '" << F->getName()
Gabor Greifa99be512007-07-05 17:07:56 +0000329 << "' from bitcode file: " << ErrorMsg << "\n";
Chris Lattner0050ef82004-11-15 23:18:09 +0000330 abort();
331 }
Chris Lattner5c72a3a2006-07-07 17:18:09 +0000332 }
Nicolas Geoffrayfd7d9912008-04-20 08:33:02 +0000333
334 if (void *Addr = getPointerToGlobalIfAvailable(F)) {
335 return Addr;
336 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000337
Nicolas Geoffrayfd7d9912008-04-20 08:33:02 +0000338 MutexGuard locked(lock);
339
Reid Spencer5cbf9852007-01-30 20:08:39 +0000340 if (F->isDeclaration()) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000341 void *Addr = getPointerToNamedFunction(F->getName());
342 addGlobalMapping(F, Addr);
343 return Addr;
344 }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000345
346 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000347
348 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000349 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
350 return Addr;
351}
352
Chris Lattnerc07ed132003-12-20 03:36:47 +0000353/// getOrEmitGlobalVariable - Return the address of the specified global
354/// variable, possibly emitting it to memory if needed. This is used by the
355/// Emitter.
356void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
Reid Spenceree448632005-07-12 15:51:55 +0000357 MutexGuard locked(lock);
358
Chris Lattnerc07ed132003-12-20 03:36:47 +0000359 void *Ptr = getPointerToGlobalIfAvailable(GV);
360 if (Ptr) return Ptr;
361
362 // If the global is external, just remember the address.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000363 if (GV->isDeclaration()) {
Anton Korobeynikov3b30a6e2007-07-30 20:02:02 +0000364#if HAVE___DSO_HANDLE
Evan Cheng5f42c552006-07-21 23:06:20 +0000365 if (GV->getName() == "__dso_handle")
366 return (void*)&__dso_handle;
Evan Chengb82ab942006-07-22 00:42:03 +0000367#endif
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000368 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
Chris Lattnerc07ed132003-12-20 03:36:47 +0000369 if (Ptr == 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000370 cerr << "Could not resolve external global address: "
371 << GV->getName() << "\n";
Chris Lattnerc07ed132003-12-20 03:36:47 +0000372 abort();
373 }
374 } else {
375 // If the global hasn't been emitted to memory yet, allocate space. We will
376 // actually initialize the global after current function has finished
377 // compilation.
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000378 const Type *GlobalType = GV->getType()->getElementType();
Duncan Sands514ab342007-11-01 20:53:16 +0000379 size_t S = getTargetData()->getABITypeSize(GlobalType);
Duncan Sandsd1025932008-01-29 06:23:44 +0000380 size_t A = getTargetData()->getPreferredAlignment(GV);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000381 if (A <= 8) {
382 Ptr = malloc(S);
383 } else {
384 // Allocate S+A bytes of memory, then use an aligned pointer within that
385 // space.
386 Ptr = malloc(S+A);
387 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
Chris Lattner21c39a62006-07-12 00:31:47 +0000388 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
Chris Lattnerf5d438c2006-05-02 21:57:51 +0000389 }
Chris Lattner2c639ad2007-04-20 22:40:05 +0000390 jitstate.getPendingGlobals(locked).push_back(GV);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000391 }
392 addGlobalMapping(GV, Ptr);
393 return Ptr;
394}
395
396
Chris Lattner4d326fa2003-12-20 01:46:27 +0000397/// recompileAndRelinkFunction - This method is used to force a function
398/// which has already been compiled, to be compiled again, possibly
399/// after it has been modified. Then the entry to the old copy is overwritten
400/// with a branch to the new copy. If there was no old copy, this acts
401/// just like JIT::getPointerToFunction().
402///
403void *JIT::recompileAndRelinkFunction(Function *F) {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000404 void *OldAddr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000405
Chris Lattnerc07ed132003-12-20 03:36:47 +0000406 // If it's not already compiled there is no reason to patch it up.
407 if (OldAddr == 0) { return getPointerToFunction(F); }
Chris Lattner4d326fa2003-12-20 01:46:27 +0000408
Chris Lattnerc07ed132003-12-20 03:36:47 +0000409 // Delete the old function mapping.
410 addGlobalMapping(F, 0);
411
Chris Lattnerc07ed132003-12-20 03:36:47 +0000412 // Recodegen the function
Chris Lattner4d326fa2003-12-20 01:46:27 +0000413 runJITOnFunction(F);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000414
415 // Update state, forward the old function to the new function.
416 void *Addr = getPointerToGlobalIfAvailable(F);
Chris Lattner4d326fa2003-12-20 01:46:27 +0000417 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
418 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
419 return Addr;
420}
Misha Brukman895eddf2004-11-07 23:58:46 +0000421