blob: 14d8d5b12aeb668bace664bda555f8d6a7903af4 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tool implements a just-in-time compiler for LLVM, allowing direct
11// execution of LLVM bitcode in an efficient manner.
12//
13//===----------------------------------------------------------------------===//
14
15#include "JIT.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/GlobalVariable.h"
20#include "llvm/Instructions.h"
21#include "llvm/ModuleProvider.h"
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000022#include "llvm/CodeGen/JITCodeEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/ExecutionEngine/GenericValue.h"
Argiris Kirtzidis6841c1a2009-05-18 21:06:40 +000024#include "llvm/CodeGen/MachineCodeInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetJITInfo.h"
Chris Lattner9e309642009-03-03 20:10:23 +000028#include "llvm/Support/Dwarf.h"
29#include "llvm/Support/MutexGuard.h"
30#include "llvm/System/DynamicLibrary.h"
Anton Korobeynikov52f44db2007-07-30 20:02:02 +000031#include "llvm/Config/config.h"
32
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35#ifdef __APPLE__
Anton Korobeynikov52f44db2007-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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049#endif
Anton Korobeynikov52f44db2007-07-30 20:02:02 +000050
51#if HAVE___DSO_HANDLE
52extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053#endif
54
Dan Gohman089efff2008-05-13 00:00:25 +000055namespace {
56
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057static struct RegisterJIT {
58 RegisterJIT() { JIT::Register(); }
59} JITRegistrator;
60
Dan Gohman089efff2008-05-13 00:00:25 +000061}
62
Bob Wilson3217b3e2009-06-24 21:09:18 +000063extern "C" void LLVMLinkInJIT() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064}
65
Nicolas Geoffray72dfb8e2008-08-28 22:34:49 +000066
Evan Chengaf8dce02009-02-01 06:42:27 +000067#if defined(__GNUC__) && !defined(__ARM__EABI__)
Nicolas Geoffray72dfb8e2008-08-28 22:34:49 +000068
69// libgcc defines the __register_frame function to dynamically register new
70// dwarf frames for exception handling. This functionality is not portable
71// across compilers and is only provided by GCC. We use the __register_frame
72// function here so that code generated by the JIT cooperates with the unwinding
73// runtime of libgcc. When JITting with exception handling enable, LLVM
74// generates dwarf frames and registers it to libgcc with __register_frame.
75//
76// The __register_frame function works with Linux.
77//
78// Unfortunately, this functionality seems to be in libgcc after the unwinding
79// library of libgcc for darwin was written. The code for darwin overwrites the
80// value updated by __register_frame with a value fetched with "keymgr".
81// "keymgr" is an obsolete functionality, which should be rewritten some day.
82// In the meantime, since "keymgr" is on all libgccs shipped with apple-gcc, we
83// need a workaround in LLVM which uses the "keymgr" to dynamically modify the
84// values of an opaque key, used by libgcc to find dwarf tables.
85
Anton Korobeynikov6556e9e2008-03-22 08:53:09 +000086extern "C" void __register_frame(void*);
Nicolas Geoffray72dfb8e2008-08-28 22:34:49 +000087
Evan Cheng36304af2009-04-14 22:31:59 +000088#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED <= 1050
89# define USE_KEYMGR 1
90#else
91# define USE_KEYMGR 0
92#endif
93
94#if USE_KEYMGR
Nicolas Geoffray72dfb8e2008-08-28 22:34:49 +000095
96namespace {
97
98// LibgccObject - This is the structure defined in libgcc. There is no #include
99// provided for this structure, so we also define it here. libgcc calls it
100// "struct object". The structure is undocumented in libgcc.
101struct LibgccObject {
102 void *unused1;
103 void *unused2;
104 void *unused3;
105
106 /// frame - Pointer to the exception table.
107 void *frame;
108
109 /// encoding - The encoding of the object?
110 union {
111 struct {
112 unsigned long sorted : 1;
113 unsigned long from_array : 1;
114 unsigned long mixed_encoding : 1;
115 unsigned long encoding : 8;
116 unsigned long count : 21;
117 } b;
118 size_t i;
119 } encoding;
120
121 /// fde_end - libgcc defines this field only if some macro is defined. We
122 /// include this field even if it may not there, to make libgcc happy.
123 char *fde_end;
124
125 /// next - At least we know it's a chained list!
126 struct LibgccObject *next;
127};
128
129// "kemgr" stuff. Apparently, all frame tables are stored there.
130extern "C" void _keymgr_set_and_unlock_processwide_ptr(int, void *);
131extern "C" void *_keymgr_get_and_lock_processwide_ptr(int);
132#define KEYMGR_GCC3_DW2_OBJ_LIST 302 /* Dwarf2 object list */
133
134/// LibgccObjectInfo - libgcc defines this struct as km_object_info. It
135/// probably contains all dwarf tables that are loaded.
136struct LibgccObjectInfo {
137
138 /// seenObjects - LibgccObjects already parsed by the unwinding runtime.
139 ///
140 struct LibgccObject* seenObjects;
141
142 /// unseenObjects - LibgccObjects not parsed yet by the unwinding runtime.
143 ///
144 struct LibgccObject* unseenObjects;
145
146 unsigned unused[2];
147};
148
Nicolas Geoffray72dfb8e2008-08-28 22:34:49 +0000149/// darwin_register_frame - Since __register_frame does not work with darwin's
150/// libgcc,we provide our own function, which "tricks" libgcc by modifying the
151/// "Dwarf2 object list" key.
152void DarwinRegisterFrame(void* FrameBegin) {
153 // Get the key.
Chris Lattner9e309642009-03-03 20:10:23 +0000154 LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
Nicolas Geoffray72dfb8e2008-08-28 22:34:49 +0000155 _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
Chris Lattner9e309642009-03-03 20:10:23 +0000156 assert(LOI && "This should be preallocated by the runtime");
Nicolas Geoffray72dfb8e2008-08-28 22:34:49 +0000157
158 // Allocate a new LibgccObject to represent this frame. Deallocation of this
159 // object may be impossible: since darwin code in libgcc was written after
160 // the ability to dynamically register frames, things may crash if we
161 // deallocate it.
162 struct LibgccObject* ob = (struct LibgccObject*)
163 malloc(sizeof(struct LibgccObject));
164
165 // Do like libgcc for the values of the field.
166 ob->unused1 = (void *)-1;
167 ob->unused2 = 0;
168 ob->unused3 = 0;
169 ob->frame = FrameBegin;
170 ob->encoding.i = 0;
171 ob->encoding.b.encoding = llvm::dwarf::DW_EH_PE_omit;
172
173 // Put the info on both places, as libgcc uses the first or the the second
174 // field. Note that we rely on having two pointers here. If fde_end was a
175 // char, things would get complicated.
176 ob->fde_end = (char*)LOI->unseenObjects;
177 ob->next = LOI->unseenObjects;
178
179 // Update the key's unseenObjects list.
180 LOI->unseenObjects = ob;
181
182 // Finally update the "key". Apparently, libgcc requires it.
183 _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST,
184 LOI);
185
186}
187
188}
189#endif // __APPLE__
190#endif // __GNUC__
Anton Korobeynikov6556e9e2008-03-22 08:53:09 +0000191
Chris Lattner4db98aa2007-12-06 01:34:04 +0000192/// createJIT - This is the factory method for creating a JIT for the current
193/// machine, it does not fall back to the interpreter. This takes ownership
194/// of the module provider.
195ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP,
196 std::string *ErrorStr,
Evan Chenga6394fc2008-08-08 08:11:34 +0000197 JITMemoryManager *JMM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000198 CodeGenOpt::Level OptLevel) {
Bill Wendling277d7272009-04-29 00:32:19 +0000199 ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM, OptLevel);
Chris Lattner4db98aa2007-12-06 01:34:04 +0000200 if (!EE) return 0;
201
Chris Lattner4db98aa2007-12-06 01:34:04 +0000202 // Make sure we can resolve symbols in the program as well. The zero arg
203 // to the function tells DynamicLibrary to load the program, not a library.
204 sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr);
205 return EE;
206}
207
208JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000209 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel)
Nate Begemanf7113d92008-05-21 16:34:48 +0000210 : ExecutionEngine(MP), TM(tm), TJI(tji) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 setTargetData(TM.getTargetData());
212
Nate Begemanf7113d92008-05-21 16:34:48 +0000213 jitstate = new JITState(MP);
214
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000215 // Initialize JCE
216 JCE = createEmitter(*this, JMM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217
218 // Add target data
219 MutexGuard locked(lock);
Nate Begemanf7113d92008-05-21 16:34:48 +0000220 FunctionPassManager &PM = jitstate->getPM(locked);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 PM.add(new TargetData(*TM.getTargetData()));
222
223 // Turn the machine code intermediate representation into bytes in memory that
224 // may be executed.
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000225 if (TM.addPassesToEmitMachineCode(PM, *JCE, OptLevel)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 cerr << "Target does not support machine code emission!\n";
227 abort();
228 }
229
Nicolas Geoffraybed46f82008-08-18 14:53:56 +0000230 // Register routine for informing unwinding runtime about new EH frames
Evan Chengaf8dce02009-02-01 06:42:27 +0000231#if defined(__GNUC__) && !defined(__ARM_EABI__)
Evan Cheng36304af2009-04-14 22:31:59 +0000232#if USE_KEYMGR
Nicolas Geoffray72dfb8e2008-08-28 22:34:49 +0000233 struct LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
234 _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
235
236 // The key is created on demand, and libgcc creates it the first time an
237 // exception occurs. Since we need the key to register frames, we create
238 // it now.
Chris Lattner0973f802009-04-16 21:47:59 +0000239 if (!LOI)
240 LOI = (LibgccObjectInfo*)calloc(sizeof(struct LibgccObjectInfo), 1);
241 _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST, LOI);
Nicolas Geoffray72dfb8e2008-08-28 22:34:49 +0000242 InstallExceptionTableRegister(DarwinRegisterFrame);
243#else
Nicolas Geoffraybed46f82008-08-18 14:53:56 +0000244 InstallExceptionTableRegister(__register_frame);
Nicolas Geoffray72dfb8e2008-08-28 22:34:49 +0000245#endif // __APPLE__
246#endif // __GNUC__
Nicolas Geoffraybed46f82008-08-18 14:53:56 +0000247
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 // Initialize passes.
249 PM.doInitialization();
250}
251
252JIT::~JIT() {
Nate Begemanf7113d92008-05-21 16:34:48 +0000253 delete jitstate;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000254 delete JCE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 delete &TM;
256}
257
Nate Begemanf7113d92008-05-21 16:34:48 +0000258/// addModuleProvider - Add a new ModuleProvider to the JIT. If we previously
259/// removed the last ModuleProvider, we need re-initialize jitstate with a valid
260/// ModuleProvider.
261void JIT::addModuleProvider(ModuleProvider *MP) {
262 MutexGuard locked(lock);
263
264 if (Modules.empty()) {
265 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
266
267 jitstate = new JITState(MP);
268
269 FunctionPassManager &PM = jitstate->getPM(locked);
270 PM.add(new TargetData(*TM.getTargetData()));
271
272 // Turn the machine code intermediate representation into bytes in memory
273 // that may be executed.
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000274 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
Nate Begemanf7113d92008-05-21 16:34:48 +0000275 cerr << "Target does not support machine code emission!\n";
276 abort();
277 }
278
279 // Initialize passes.
280 PM.doInitialization();
281 }
282
283 ExecutionEngine::addModuleProvider(MP);
284}
285
286/// removeModuleProvider - If we are removing the last ModuleProvider,
287/// invalidate the jitstate since the PassManager it contains references a
288/// released ModuleProvider.
289Module *JIT::removeModuleProvider(ModuleProvider *MP, std::string *E) {
290 Module *result = ExecutionEngine::removeModuleProvider(MP, E);
291
292 MutexGuard locked(lock);
Nate Begeman7b1a8472009-02-18 08:31:02 +0000293
294 if (jitstate->getMP() == MP) {
Nate Begemanf7113d92008-05-21 16:34:48 +0000295 delete jitstate;
296 jitstate = 0;
297 }
298
Nate Begeman7b1a8472009-02-18 08:31:02 +0000299 if (!jitstate && !Modules.empty()) {
300 jitstate = new JITState(Modules[0]);
301
302 FunctionPassManager &PM = jitstate->getPM(locked);
303 PM.add(new TargetData(*TM.getTargetData()));
304
305 // Turn the machine code intermediate representation into bytes in memory
306 // that may be executed.
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000307 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
Nate Begeman7b1a8472009-02-18 08:31:02 +0000308 cerr << "Target does not support machine code emission!\n";
309 abort();
310 }
311
312 // Initialize passes.
313 PM.doInitialization();
314 }
Nate Begemanf7113d92008-05-21 16:34:48 +0000315 return result;
316}
317
Nate Begemanb34045e2009-01-23 19:27:28 +0000318/// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
319/// and deletes the ModuleProvider and owned Module. Avoids materializing
320/// the underlying module.
321void JIT::deleteModuleProvider(ModuleProvider *MP, std::string *E) {
322 ExecutionEngine::deleteModuleProvider(MP, E);
323
324 MutexGuard locked(lock);
Nate Begeman7b1a8472009-02-18 08:31:02 +0000325
326 if (jitstate->getMP() == MP) {
Nate Begemanb34045e2009-01-23 19:27:28 +0000327 delete jitstate;
328 jitstate = 0;
329 }
Nate Begeman7b1a8472009-02-18 08:31:02 +0000330
331 if (!jitstate && !Modules.empty()) {
332 jitstate = new JITState(Modules[0]);
333
334 FunctionPassManager &PM = jitstate->getPM(locked);
335 PM.add(new TargetData(*TM.getTargetData()));
336
337 // Turn the machine code intermediate representation into bytes in memory
338 // that may be executed.
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000339 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
Nate Begeman7b1a8472009-02-18 08:31:02 +0000340 cerr << "Target does not support machine code emission!\n";
341 abort();
342 }
343
344 // Initialize passes.
345 PM.doInitialization();
346 }
Nate Begemanb34045e2009-01-23 19:27:28 +0000347}
348
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349/// run - Start execution with the specified function and arguments.
350///
351GenericValue JIT::runFunction(Function *F,
352 const std::vector<GenericValue> &ArgValues) {
353 assert(F && "Function *F was null at entry to run()");
354
355 void *FPtr = getPointerToFunction(F);
356 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
357 const FunctionType *FTy = F->getFunctionType();
358 const Type *RetTy = FTy->getReturnType();
359
Dan Gohman4e28da42009-02-19 02:55:18 +0000360 assert((FTy->getNumParams() == ArgValues.size() ||
361 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
362 "Wrong number of arguments passed into function!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 assert(FTy->getNumParams() == ArgValues.size() &&
364 "This doesn't support passing arguments through varargs (yet)!");
365
366 // Handle some common cases first. These cases correspond to common `main'
367 // prototypes.
Chris Lattnerbbf22702007-08-08 16:19:57 +0000368 if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 switch (ArgValues.size()) {
370 case 3:
Chris Lattnerbbf22702007-08-08 16:19:57 +0000371 if (FTy->getParamType(0) == Type::Int32Ty &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 isa<PointerType>(FTy->getParamType(1)) &&
373 isa<PointerType>(FTy->getParamType(2))) {
374 int (*PF)(int, char **, const char **) =
375 (int(*)(int, char **, const char **))(intptr_t)FPtr;
376
377 // Call the function.
378 GenericValue rv;
379 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
380 (char **)GVTOP(ArgValues[1]),
381 (const char **)GVTOP(ArgValues[2])));
382 return rv;
383 }
384 break;
385 case 2:
Chris Lattnerbbf22702007-08-08 16:19:57 +0000386 if (FTy->getParamType(0) == Type::Int32Ty &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 isa<PointerType>(FTy->getParamType(1))) {
388 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
389
390 // Call the function.
391 GenericValue rv;
392 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
393 (char **)GVTOP(ArgValues[1])));
394 return rv;
395 }
396 break;
397 case 1:
398 if (FTy->getNumParams() == 1 &&
Chris Lattnerbbf22702007-08-08 16:19:57 +0000399 FTy->getParamType(0) == Type::Int32Ty) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 GenericValue rv;
401 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
402 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
403 return rv;
404 }
405 break;
406 }
407 }
408
409 // Handle cases where no arguments are passed first.
410 if (ArgValues.empty()) {
411 GenericValue rv;
412 switch (RetTy->getTypeID()) {
413 default: assert(0 && "Unknown return type for function call!");
414 case Type::IntegerTyID: {
415 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
416 if (BitWidth == 1)
417 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
418 else if (BitWidth <= 8)
419 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
420 else if (BitWidth <= 16)
421 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
422 else if (BitWidth <= 32)
423 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
424 else if (BitWidth <= 64)
425 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
426 else
427 assert(0 && "Integer types > 64 bits not supported");
428 return rv;
429 }
430 case Type::VoidTyID:
431 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
432 return rv;
433 case Type::FloatTyID:
434 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
435 return rv;
436 case Type::DoubleTyID:
437 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
438 return rv;
Dale Johannesenc560da62007-09-17 18:44:13 +0000439 case Type::X86_FP80TyID:
440 case Type::FP128TyID:
441 case Type::PPC_FP128TyID:
442 assert(0 && "long double not supported yet");
443 return rv;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 case Type::PointerTyID:
445 return PTOGV(((void*(*)())(intptr_t)FPtr)());
446 }
447 }
448
449 // Okay, this is not one of our quick and easy cases. Because we don't have a
450 // full FFI, we have to codegen a nullary stub function that just calls the
451 // function we are interested in, passing in constants for all of the
452 // arguments. Make this function and return.
453
454 // First, create the function.
455 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000456 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
457 F->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458
459 // Insert a basic block.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000460 BasicBlock *StubBB = BasicBlock::Create("", Stub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461
462 // Convert all of the GenericValue arguments over to constants. Note that we
463 // currently don't support varargs.
464 SmallVector<Value*, 8> Args;
465 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
466 Constant *C = 0;
467 const Type *ArgTy = FTy->getParamType(i);
468 const GenericValue &AV = ArgValues[i];
469 switch (ArgTy->getTypeID()) {
470 default: assert(0 && "Unknown argument type for function call!");
Chris Lattner5e0610f2008-04-20 00:41:09 +0000471 case Type::IntegerTyID:
472 C = ConstantInt::get(AV.IntVal);
473 break;
474 case Type::FloatTyID:
475 C = ConstantFP::get(APFloat(AV.FloatVal));
476 break;
477 case Type::DoubleTyID:
478 C = ConstantFP::get(APFloat(AV.DoubleVal));
479 break;
Dale Johannesenc560da62007-09-17 18:44:13 +0000480 case Type::PPC_FP128TyID:
481 case Type::X86_FP80TyID:
Chris Lattner5e0610f2008-04-20 00:41:09 +0000482 case Type::FP128TyID:
483 C = ConstantFP::get(APFloat(AV.IntVal));
484 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 case Type::PointerTyID:
486 void *ArgPtr = GVTOP(AV);
Chris Lattner5e0610f2008-04-20 00:41:09 +0000487 if (sizeof(void*) == 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
Chris Lattner5e0610f2008-04-20 00:41:09 +0000489 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
492 break;
493 }
494 Args.push_back(C);
495 }
496
Gabor Greifb91ea9d2008-05-15 10:04:30 +0000497 CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(),
498 "", StubBB);
Chris Lattner62ef4f12008-11-23 08:00:11 +0000499 TheCall->setCallingConv(F->getCallingConv());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 TheCall->setTailCall();
501 if (TheCall->getType() != Type::VoidTy)
Gabor Greifb91ea9d2008-05-15 10:04:30 +0000502 ReturnInst::Create(TheCall, StubBB); // Return result of the call.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 else
Gabor Greifb91ea9d2008-05-15 10:04:30 +0000504 ReturnInst::Create(StubBB); // Just return void.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505
506 // Finally, return the value returned by our nullary stub function.
507 return runFunction(Stub, std::vector<GenericValue>());
508}
509
510/// runJITOnFunction - Run the FunctionPassManager full of
511/// just-in-time compilation passes on F, hopefully filling in
512/// GlobalAddress[F] with the address of F's machine code.
513///
Argiris Kirtzidis6841c1a2009-05-18 21:06:40 +0000514void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 MutexGuard locked(lock);
Argiris Kirtzidis6841c1a2009-05-18 21:06:40 +0000516
517 registerMachineCodeInfo(MCI);
518
Dan Gohman2970af12009-02-06 21:25:08 +0000519 runJITOnFunctionUnlocked(F, locked);
Argiris Kirtzidis6841c1a2009-05-18 21:06:40 +0000520
521 registerMachineCodeInfo(0);
Dan Gohman2970af12009-02-06 21:25:08 +0000522}
523
524void JIT::runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked) {
525 static bool isAlreadyCodeGenerating = false;
Chris Lattner700fb1d2007-08-13 20:08:16 +0000526 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527
528 // JIT the function
529 isAlreadyCodeGenerating = true;
Nate Begemanf7113d92008-05-21 16:34:48 +0000530 jitstate->getPM(locked).run(*F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 isAlreadyCodeGenerating = false;
532
Nate Begeman7b1a8472009-02-18 08:31:02 +0000533 // If the function referred to another function that had not yet been
534 // read from bitcode, but we are jitting non-lazily, emit it now.
535 while (!jitstate->getPendingFunctions(locked).empty()) {
536 Function *PF = jitstate->getPendingFunctions(locked).back();
537 jitstate->getPendingFunctions(locked).pop_back();
538
539 // JIT the function
540 isAlreadyCodeGenerating = true;
541 jitstate->getPM(locked).run(*PF);
542 isAlreadyCodeGenerating = false;
543
544 // Now that the function has been jitted, ask the JITEmitter to rewrite
545 // the stub with real address of the function.
546 updateFunctionStub(PF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 }
Nate Begeman7b1a8472009-02-18 08:31:02 +0000548
549 // If the JIT is configured to emit info so that dlsym can be used to
550 // rewrite stubs to external globals, do so now.
551 if (areDlsymStubsEnabled() && isLazyCompilationDisabled())
552 updateDlsymStubTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553}
554
555/// getPointerToFunction - This method is used to get the address of the
556/// specified function, compiling it if neccesary.
557///
558void *JIT::getPointerToFunction(Function *F) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559
560 if (void *Addr = getPointerToGlobalIfAvailable(F))
561 return Addr; // Check if function already code gen'd
562
Dan Gohman039d2a72009-02-19 02:40:15 +0000563 MutexGuard locked(lock);
Nicolas Geoffray81de4bf2009-06-12 14:11:08 +0000564
565 // Now that this thread owns the lock, check if another thread has already
566 // code gen'd the function.
567 if (void *Addr = getPointerToGlobalIfAvailable(F))
568 return Addr;
Dan Gohman039d2a72009-02-19 02:40:15 +0000569
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 // Make sure we read in the function if it exists in this Module.
571 if (F->hasNotBeenReadFromBitcode()) {
572 // Determine the module provider this function is provided by.
573 Module *M = F->getParent();
574 ModuleProvider *MP = 0;
575 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
576 if (Modules[i]->getModule() == M) {
577 MP = Modules[i];
578 break;
579 }
580 }
581 assert(MP && "Function isn't in a module we know about!");
582
583 std::string ErrorMsg;
584 if (MP->materializeFunction(F, &ErrorMsg)) {
585 cerr << "Error reading function '" << F->getName()
586 << "' from bitcode file: " << ErrorMsg << "\n";
587 abort();
588 }
Mon P Wanga21f8d42008-10-10 01:47:42 +0000589
Dan Gohman0ae39622009-01-05 05:32:42 +0000590 // Now retry to get the address.
591 if (void *Addr = getPointerToGlobalIfAvailable(F))
592 return Addr;
Nicolas Geoffray8ae32352008-04-20 08:33:02 +0000593 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594
595 if (F->isDeclaration()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000596 bool AbortOnFailure =
597 !areDlsymStubsEnabled() && !F->hasExternalWeakLinkage();
Dan Gohman0ae39622009-01-05 05:32:42 +0000598 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 addGlobalMapping(F, Addr);
600 return Addr;
601 }
602
Dan Gohman2970af12009-02-06 21:25:08 +0000603 runJITOnFunctionUnlocked(F, locked);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604
605 void *Addr = getPointerToGlobalIfAvailable(F);
606 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
607 return Addr;
608}
609
610/// getOrEmitGlobalVariable - Return the address of the specified global
611/// variable, possibly emitting it to memory if needed. This is used by the
612/// Emitter.
613void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
614 MutexGuard locked(lock);
615
616 void *Ptr = getPointerToGlobalIfAvailable(GV);
617 if (Ptr) return Ptr;
618
619 // If the global is external, just remember the address.
620 if (GV->isDeclaration()) {
Anton Korobeynikov52f44db2007-07-30 20:02:02 +0000621#if HAVE___DSO_HANDLE
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 if (GV->getName() == "__dso_handle")
623 return (void*)&__dso_handle;
624#endif
625 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
Nate Begemane1ba87f2009-03-04 19:10:38 +0000626 if (Ptr == 0 && !areDlsymStubsEnabled()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 cerr << "Could not resolve external global address: "
628 << GV->getName() << "\n";
629 abort();
630 }
Nate Begemane1ba87f2009-03-04 19:10:38 +0000631 addGlobalMapping(GV, Ptr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 } else {
Evan Cheng49777792009-01-16 19:14:49 +0000633 // GlobalVariable's which are not "constant" will cause trouble in a server
634 // situation. It's returned in the same block of memory as code which may
635 // not be writable.
636 if (isGVCompilationDisabled() && !GV->isConstant()) {
Evan Chenga134caf2008-12-09 07:31:49 +0000637 cerr << "Compilation of non-internal GlobalValue is disabled!\n";
Evan Cheng9b8bc832008-09-24 16:25:55 +0000638 abort();
639 }
Dale Johannesen0ba4a0e2008-08-07 01:30:15 +0000640 // If the global hasn't been emitted to memory yet, allocate space and
641 // emit it into memory. It goes in the same array as the generated
642 // code, jump tables, etc.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 const Type *GlobalType = GV->getType()->getElementType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000644 size_t S = getTargetData()->getTypeAllocSize(GlobalType);
Duncan Sands935686e2008-01-29 06:23:44 +0000645 size_t A = getTargetData()->getPreferredAlignment(GV);
Nicolas Geoffray46fa1532008-10-25 15:41:43 +0000646 if (GV->isThreadLocal()) {
647 MutexGuard locked(lock);
648 Ptr = TJI.allocateThreadLocalMemory(S);
Evan Cheng4a8e3b42008-11-04 09:30:48 +0000649 } else if (TJI.allocateSeparateGVMemory()) {
650 if (A <= 8) {
651 Ptr = malloc(S);
652 } else {
653 // Allocate S+A bytes of memory, then use an aligned pointer within that
654 // space.
655 Ptr = malloc(S+A);
656 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
657 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
658 }
Nicolas Geoffray46fa1532008-10-25 15:41:43 +0000659 } else {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000660 Ptr = JCE->allocateSpace(S, A);
Nicolas Geoffray46fa1532008-10-25 15:41:43 +0000661 }
Dale Johannesen0ba4a0e2008-08-07 01:30:15 +0000662 addGlobalMapping(GV, Ptr);
663 EmitGlobalVariable(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 return Ptr;
666}
667
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668/// recompileAndRelinkFunction - This method is used to force a function
669/// which has already been compiled, to be compiled again, possibly
670/// after it has been modified. Then the entry to the old copy is overwritten
671/// with a branch to the new copy. If there was no old copy, this acts
672/// just like JIT::getPointerToFunction().
673///
674void *JIT::recompileAndRelinkFunction(Function *F) {
675 void *OldAddr = getPointerToGlobalIfAvailable(F);
676
677 // If it's not already compiled there is no reason to patch it up.
678 if (OldAddr == 0) { return getPointerToFunction(F); }
679
680 // Delete the old function mapping.
681 addGlobalMapping(F, 0);
682
683 // Recodegen the function
684 runJITOnFunction(F);
685
686 // Update state, forward the old function to the new function.
687 void *Addr = getPointerToGlobalIfAvailable(F);
688 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
689 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
690 return Addr;
691}
692
Nicolas Geoffray46fa1532008-10-25 15:41:43 +0000693/// getMemoryForGV - This method abstracts memory allocation of global
694/// variable so that the JIT can allocate thread local variables depending
695/// on the target.
696///
697char* JIT::getMemoryForGV(const GlobalVariable* GV) {
698 const Type *ElTy = GV->getType()->getElementType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000699 size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy);
Nicolas Geoffray46fa1532008-10-25 15:41:43 +0000700 if (GV->isThreadLocal()) {
701 MutexGuard locked(lock);
702 return TJI.allocateThreadLocalMemory(GVSize);
703 } else {
704 return new char[GVSize];
705 }
706}
Nate Begeman7b1a8472009-02-18 08:31:02 +0000707
708void JIT::addPendingFunction(Function *F) {
709 MutexGuard locked(lock);
710 jitstate->getPendingFunctions(locked).push_back(F);
711}