1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tool implements a just-in-time compiler for LLVM, allowing direct
11 // execution of LLVM bitcode in an efficient manner.
13 //===----------------------------------------------------------------------===//
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"
22 #include "llvm/CodeGen/MachineCodeEmitter.h"
23 #include "llvm/ExecutionEngine/GenericValue.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetJITInfo.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/MutexGuard.h"
29 #include "llvm/System/DynamicLibrary.h"
30 #include "llvm/Config/config.h"
35 // 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
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
51 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
56 static struct RegisterJIT {
57 RegisterJIT() { JIT::Register(); }
68 #if defined(__GNUC__) && !defined(__ARM__EABI__)
70 // libgcc defines the __register_frame function to dynamically register new
71 // dwarf frames for exception handling. This functionality is not portable
72 // across compilers and is only provided by GCC. We use the __register_frame
73 // function here so that code generated by the JIT cooperates with the unwinding
74 // runtime of libgcc. When JITting with exception handling enable, LLVM
75 // generates dwarf frames and registers it to libgcc with __register_frame.
77 // The __register_frame function works with Linux.
79 // Unfortunately, this functionality seems to be in libgcc after the unwinding
80 // library of libgcc for darwin was written. The code for darwin overwrites the
81 // value updated by __register_frame with a value fetched with "keymgr".
82 // "keymgr" is an obsolete functionality, which should be rewritten some day.
83 // In the meantime, since "keymgr" is on all libgccs shipped with apple-gcc, we
84 // need a workaround in LLVM which uses the "keymgr" to dynamically modify the
85 // values of an opaque key, used by libgcc to find dwarf tables.
87 extern "C" void __register_frame(void*);
89 #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED <= 1050
99 // LibgccObject - This is the structure defined in libgcc. There is no #include
100 // provided for this structure, so we also define it here. libgcc calls it
101 // "struct object". The structure is undocumented in libgcc.
102 struct LibgccObject {
107 /// frame - Pointer to the exception table.
110 /// encoding - The encoding of the object?
113 unsigned long sorted : 1;
114 unsigned long from_array : 1;
115 unsigned long mixed_encoding : 1;
116 unsigned long encoding : 8;
117 unsigned long count : 21;
122 /// fde_end - libgcc defines this field only if some macro is defined. We
123 /// include this field even if it may not there, to make libgcc happy.
126 /// next - At least we know it's a chained list!
127 struct LibgccObject *next;
130 // "kemgr" stuff. Apparently, all frame tables are stored there.
131 extern "C" void _keymgr_set_and_unlock_processwide_ptr(int, void *);
132 extern "C" void *_keymgr_get_and_lock_processwide_ptr(int);
133 #define KEYMGR_GCC3_DW2_OBJ_LIST 302 /* Dwarf2 object list */
135 /// LibgccObjectInfo - libgcc defines this struct as km_object_info. It
136 /// probably contains all dwarf tables that are loaded.
137 struct LibgccObjectInfo {
139 /// seenObjects - LibgccObjects already parsed by the unwinding runtime.
141 struct LibgccObject* seenObjects;
143 /// unseenObjects - LibgccObjects not parsed yet by the unwinding runtime.
145 struct LibgccObject* unseenObjects;
150 /// darwin_register_frame - Since __register_frame does not work with darwin's
151 /// libgcc,we provide our own function, which "tricks" libgcc by modifying the
152 /// "Dwarf2 object list" key.
153 void DarwinRegisterFrame(void* FrameBegin) {
155 LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
156 _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
157 assert(LOI && "This should be preallocated by the runtime");
159 // Allocate a new LibgccObject to represent this frame. Deallocation of this
160 // object may be impossible: since darwin code in libgcc was written after
161 // the ability to dynamically register frames, things may crash if we
163 struct LibgccObject* ob = (struct LibgccObject*)
164 malloc(sizeof(struct LibgccObject));
166 // Do like libgcc for the values of the field.
167 ob->unused1 = (void *)-1;
170 ob->frame = FrameBegin;
172 ob->encoding.b.encoding = llvm::dwarf::DW_EH_PE_omit;
174 // Put the info on both places, as libgcc uses the first or the the second
175 // field. Note that we rely on having two pointers here. If fde_end was a
176 // char, things would get complicated.
177 ob->fde_end = (char*)LOI->unseenObjects;
178 ob->next = LOI->unseenObjects;
180 // Update the key's unseenObjects list.
181 LOI->unseenObjects = ob;
183 // Finally update the "key". Apparently, libgcc requires it.
184 _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST,
193 /// createJIT - This is the factory method for creating a JIT for the current
194 /// machine, it does not fall back to the interpreter. This takes ownership
195 /// of the module provider.
196 ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP,
197 std::string *ErrorStr,
198 JITMemoryManager *JMM,
200 ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM, Fast);
203 // Make sure we can resolve symbols in the program as well. The zero arg
204 // to the function tells DynamicLibrary to load the program, not a library.
205 sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr);
209 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
210 JITMemoryManager *JMM, bool Fast)
211 : ExecutionEngine(MP), TM(tm), TJI(tji) {
212 setTargetData(TM.getTargetData());
214 jitstate = new JITState(MP);
217 MCE = createEmitter(*this, JMM);
220 MutexGuard locked(lock);
221 FunctionPassManager &PM = jitstate->getPM(locked);
222 PM.add(new TargetData(*TM.getTargetData()));
224 // Turn the machine code intermediate representation into bytes in memory that
226 if (TM.addPassesToEmitMachineCode(PM, *MCE, Fast)) {
227 cerr << "Target does not support machine code emission!\n";
231 // Register routine for informing unwinding runtime about new EH frames
232 #if defined(__GNUC__) && !defined(__ARM_EABI__)
234 struct LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
235 _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
237 // The key is created on demand, and libgcc creates it the first time an
238 // exception occurs. Since we need the key to register frames, we create
241 LOI = (LibgccObjectInfo*)malloc(sizeof(struct LibgccObjectInfo));
242 _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST,
245 InstallExceptionTableRegister(DarwinRegisterFrame);
247 InstallExceptionTableRegister(__register_frame);
251 // Initialize passes.
252 PM.doInitialization();
261 /// addModuleProvider - Add a new ModuleProvider to the JIT. If we previously
262 /// removed the last ModuleProvider, we need re-initialize jitstate with a valid
264 void JIT::addModuleProvider(ModuleProvider *MP) {
265 MutexGuard locked(lock);
267 if (Modules.empty()) {
268 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
270 jitstate = new JITState(MP);
272 FunctionPassManager &PM = jitstate->getPM(locked);
273 PM.add(new TargetData(*TM.getTargetData()));
275 // Turn the machine code intermediate representation into bytes in memory
276 // that may be executed.
277 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
278 cerr << "Target does not support machine code emission!\n";
282 // Initialize passes.
283 PM.doInitialization();
286 ExecutionEngine::addModuleProvider(MP);
289 /// removeModuleProvider - If we are removing the last ModuleProvider,
290 /// invalidate the jitstate since the PassManager it contains references a
291 /// released ModuleProvider.
292 Module *JIT::removeModuleProvider(ModuleProvider *MP, std::string *E) {
293 Module *result = ExecutionEngine::removeModuleProvider(MP, E);
295 MutexGuard locked(lock);
297 if (jitstate->getMP() == MP) {
302 if (!jitstate && !Modules.empty()) {
303 jitstate = new JITState(Modules[0]);
305 FunctionPassManager &PM = jitstate->getPM(locked);
306 PM.add(new TargetData(*TM.getTargetData()));
308 // Turn the machine code intermediate representation into bytes in memory
309 // that may be executed.
310 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
311 cerr << "Target does not support machine code emission!\n";
315 // Initialize passes.
316 PM.doInitialization();
321 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
322 /// and deletes the ModuleProvider and owned Module. Avoids materializing
323 /// the underlying module.
324 void JIT::deleteModuleProvider(ModuleProvider *MP, std::string *E) {
325 ExecutionEngine::deleteModuleProvider(MP, E);
327 MutexGuard locked(lock);
329 if (jitstate->getMP() == MP) {
334 if (!jitstate && !Modules.empty()) {
335 jitstate = new JITState(Modules[0]);
337 FunctionPassManager &PM = jitstate->getPM(locked);
338 PM.add(new TargetData(*TM.getTargetData()));
340 // Turn the machine code intermediate representation into bytes in memory
341 // that may be executed.
342 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
343 cerr << "Target does not support machine code emission!\n";
347 // Initialize passes.
348 PM.doInitialization();
352 /// run - Start execution with the specified function and arguments.
354 GenericValue JIT::runFunction(Function *F,
355 const std::vector<GenericValue> &ArgValues) {
356 assert(F && "Function *F was null at entry to run()");
358 void *FPtr = getPointerToFunction(F);
359 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
360 const FunctionType *FTy = F->getFunctionType();
361 const Type *RetTy = FTy->getReturnType();
363 assert((FTy->getNumParams() == ArgValues.size() ||
364 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
365 "Wrong number of arguments passed into function!");
366 assert(FTy->getNumParams() == ArgValues.size() &&
367 "This doesn't support passing arguments through varargs (yet)!");
369 // Handle some common cases first. These cases correspond to common `main'
371 if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
372 switch (ArgValues.size()) {
374 if (FTy->getParamType(0) == Type::Int32Ty &&
375 isa<PointerType>(FTy->getParamType(1)) &&
376 isa<PointerType>(FTy->getParamType(2))) {
377 int (*PF)(int, char **, const char **) =
378 (int(*)(int, char **, const char **))(intptr_t)FPtr;
380 // Call the function.
382 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
383 (char **)GVTOP(ArgValues[1]),
384 (const char **)GVTOP(ArgValues[2])));
389 if (FTy->getParamType(0) == Type::Int32Ty &&
390 isa<PointerType>(FTy->getParamType(1))) {
391 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
393 // Call the function.
395 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
396 (char **)GVTOP(ArgValues[1])));
401 if (FTy->getNumParams() == 1 &&
402 FTy->getParamType(0) == Type::Int32Ty) {
404 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
405 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
412 // Handle cases where no arguments are passed first.
413 if (ArgValues.empty()) {
415 switch (RetTy->getTypeID()) {
416 default: assert(0 && "Unknown return type for function call!");
417 case Type::IntegerTyID: {
418 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
420 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
421 else if (BitWidth <= 8)
422 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
423 else if (BitWidth <= 16)
424 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
425 else if (BitWidth <= 32)
426 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
427 else if (BitWidth <= 64)
428 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
430 assert(0 && "Integer types > 64 bits not supported");
434 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
436 case Type::FloatTyID:
437 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
439 case Type::DoubleTyID:
440 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
442 case Type::X86_FP80TyID:
443 case Type::FP128TyID:
444 case Type::PPC_FP128TyID:
445 assert(0 && "long double not supported yet");
447 case Type::PointerTyID:
448 return PTOGV(((void*(*)())(intptr_t)FPtr)());
452 // Okay, this is not one of our quick and easy cases. Because we don't have a
453 // full FFI, we have to codegen a nullary stub function that just calls the
454 // function we are interested in, passing in constants for all of the
455 // arguments. Make this function and return.
457 // First, create the function.
458 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
459 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
462 // Insert a basic block.
463 BasicBlock *StubBB = BasicBlock::Create("", Stub);
465 // Convert all of the GenericValue arguments over to constants. Note that we
466 // currently don't support varargs.
467 SmallVector<Value*, 8> Args;
468 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
470 const Type *ArgTy = FTy->getParamType(i);
471 const GenericValue &AV = ArgValues[i];
472 switch (ArgTy->getTypeID()) {
473 default: assert(0 && "Unknown argument type for function call!");
474 case Type::IntegerTyID:
475 C = ConstantInt::get(AV.IntVal);
477 case Type::FloatTyID:
478 C = ConstantFP::get(APFloat(AV.FloatVal));
480 case Type::DoubleTyID:
481 C = ConstantFP::get(APFloat(AV.DoubleVal));
483 case Type::PPC_FP128TyID:
484 case Type::X86_FP80TyID:
485 case Type::FP128TyID:
486 C = ConstantFP::get(APFloat(AV.IntVal));
488 case Type::PointerTyID:
489 void *ArgPtr = GVTOP(AV);
490 if (sizeof(void*) == 4)
491 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
493 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
494 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
500 CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(),
502 TheCall->setCallingConv(F->getCallingConv());
503 TheCall->setTailCall();
504 if (TheCall->getType() != Type::VoidTy)
505 ReturnInst::Create(TheCall, StubBB); // Return result of the call.
507 ReturnInst::Create(StubBB); // Just return void.
509 // Finally, return the value returned by our nullary stub function.
510 return runFunction(Stub, std::vector<GenericValue>());
513 /// runJITOnFunction - Run the FunctionPassManager full of
514 /// just-in-time compilation passes on F, hopefully filling in
515 /// GlobalAddress[F] with the address of F's machine code.
517 void JIT::runJITOnFunction(Function *F) {
518 MutexGuard locked(lock);
519 runJITOnFunctionUnlocked(F, locked);
522 void JIT::runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked) {
523 static bool isAlreadyCodeGenerating = false;
524 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
527 isAlreadyCodeGenerating = true;
528 jitstate->getPM(locked).run(*F);
529 isAlreadyCodeGenerating = false;
531 // If the function referred to another function that had not yet been
532 // read from bitcode, but we are jitting non-lazily, emit it now.
533 while (!jitstate->getPendingFunctions(locked).empty()) {
534 Function *PF = jitstate->getPendingFunctions(locked).back();
535 jitstate->getPendingFunctions(locked).pop_back();
538 isAlreadyCodeGenerating = true;
539 jitstate->getPM(locked).run(*PF);
540 isAlreadyCodeGenerating = false;
542 // Now that the function has been jitted, ask the JITEmitter to rewrite
543 // the stub with real address of the function.
544 updateFunctionStub(PF);
547 // If the JIT is configured to emit info so that dlsym can be used to
548 // rewrite stubs to external globals, do so now.
549 if (areDlsymStubsEnabled() && isLazyCompilationDisabled())
550 updateDlsymStubTable();
553 /// getPointerToFunction - This method is used to get the address of the
554 /// specified function, compiling it if neccesary.
556 void *JIT::getPointerToFunction(Function *F) {
558 if (void *Addr = getPointerToGlobalIfAvailable(F))
559 return Addr; // Check if function already code gen'd
561 MutexGuard locked(lock);
563 // Make sure we read in the function if it exists in this Module.
564 if (F->hasNotBeenReadFromBitcode()) {
565 // Determine the module provider this function is provided by.
566 Module *M = F->getParent();
567 ModuleProvider *MP = 0;
568 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
569 if (Modules[i]->getModule() == M) {
574 assert(MP && "Function isn't in a module we know about!");
576 std::string ErrorMsg;
577 if (MP->materializeFunction(F, &ErrorMsg)) {
578 cerr << "Error reading function '" << F->getName()
579 << "' from bitcode file: " << ErrorMsg << "\n";
583 // Now retry to get the address.
584 if (void *Addr = getPointerToGlobalIfAvailable(F))
588 if (F->isDeclaration()) {
589 bool AbortOnFailure =
590 !areDlsymStubsEnabled() && !F->hasExternalWeakLinkage();
591 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
592 addGlobalMapping(F, Addr);
596 runJITOnFunctionUnlocked(F, locked);
598 void *Addr = getPointerToGlobalIfAvailable(F);
599 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
603 /// getOrEmitGlobalVariable - Return the address of the specified global
604 /// variable, possibly emitting it to memory if needed. This is used by the
606 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
607 MutexGuard locked(lock);
609 void *Ptr = getPointerToGlobalIfAvailable(GV);
612 // If the global is external, just remember the address.
613 if (GV->isDeclaration()) {
614 #if HAVE___DSO_HANDLE
615 if (GV->getName() == "__dso_handle")
616 return (void*)&__dso_handle;
618 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
619 if (Ptr == 0 && !areDlsymStubsEnabled()) {
620 cerr << "Could not resolve external global address: "
621 << GV->getName() << "\n";
624 addGlobalMapping(GV, Ptr);
626 // GlobalVariable's which are not "constant" will cause trouble in a server
627 // situation. It's returned in the same block of memory as code which may
629 if (isGVCompilationDisabled() && !GV->isConstant()) {
630 cerr << "Compilation of non-internal GlobalValue is disabled!\n";
633 // If the global hasn't been emitted to memory yet, allocate space and
634 // emit it into memory. It goes in the same array as the generated
635 // code, jump tables, etc.
636 const Type *GlobalType = GV->getType()->getElementType();
637 size_t S = getTargetData()->getTypePaddedSize(GlobalType);
638 size_t A = getTargetData()->getPreferredAlignment(GV);
639 if (GV->isThreadLocal()) {
640 MutexGuard locked(lock);
641 Ptr = TJI.allocateThreadLocalMemory(S);
642 } else if (TJI.allocateSeparateGVMemory()) {
646 // Allocate S+A bytes of memory, then use an aligned pointer within that
649 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
650 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
653 Ptr = MCE->allocateSpace(S, A);
655 addGlobalMapping(GV, Ptr);
656 EmitGlobalVariable(GV);
661 /// recompileAndRelinkFunction - This method is used to force a function
662 /// which has already been compiled, to be compiled again, possibly
663 /// after it has been modified. Then the entry to the old copy is overwritten
664 /// with a branch to the new copy. If there was no old copy, this acts
665 /// just like JIT::getPointerToFunction().
667 void *JIT::recompileAndRelinkFunction(Function *F) {
668 void *OldAddr = getPointerToGlobalIfAvailable(F);
670 // If it's not already compiled there is no reason to patch it up.
671 if (OldAddr == 0) { return getPointerToFunction(F); }
673 // Delete the old function mapping.
674 addGlobalMapping(F, 0);
676 // Recodegen the function
679 // Update state, forward the old function to the new function.
680 void *Addr = getPointerToGlobalIfAvailable(F);
681 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
682 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
686 /// getMemoryForGV - This method abstracts memory allocation of global
687 /// variable so that the JIT can allocate thread local variables depending
690 char* JIT::getMemoryForGV(const GlobalVariable* GV) {
691 const Type *ElTy = GV->getType()->getElementType();
692 size_t GVSize = (size_t)getTargetData()->getTypePaddedSize(ElTy);
693 if (GV->isThreadLocal()) {
694 MutexGuard locked(lock);
695 return TJI.allocateThreadLocalMemory(GVSize);
697 return new char[GVSize];
701 void JIT::addPendingFunction(Function *F) {
702 MutexGuard locked(lock);
703 jitstate->getPendingFunctions(locked).push_back(F);