LLVM API Documentation

JIT.cpp

Go to the documentation of this file.
00001 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This tool implements a just-in-time compiler for LLVM, allowing direct
00011 // execution of LLVM bitcode in an efficient manner.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "JIT.h"
00016 #include "llvm/Constants.h"
00017 #include "llvm/DerivedTypes.h"
00018 #include "llvm/Function.h"
00019 #include "llvm/GlobalVariable.h"
00020 #include "llvm/Instructions.h"
00021 #include "llvm/ModuleProvider.h"
00022 #include "llvm/CodeGen/JITCodeEmitter.h"
00023 #include "llvm/CodeGen/MachineCodeInfo.h"
00024 #include "llvm/ExecutionEngine/GenericValue.h"
00025 #include "llvm/ExecutionEngine/JITEventListener.h"
00026 #include "llvm/Target/TargetData.h"
00027 #include "llvm/Target/TargetMachine.h"
00028 #include "llvm/Target/TargetJITInfo.h"
00029 #include "llvm/Support/Dwarf.h"
00030 #include "llvm/Support/ErrorHandling.h"
00031 #include "llvm/Support/MutexGuard.h"
00032 #include "llvm/System/DynamicLibrary.h"
00033 #include "llvm/Config/config.h"
00034 
00035 using namespace llvm;
00036 
00037 #ifdef __APPLE__ 
00038 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
00039 // of atexit). It passes the address of linker generated symbol __dso_handle
00040 // to the function.
00041 // This configuration change happened at version 5330.
00042 # include <AvailabilityMacros.h>
00043 # if defined(MAC_OS_X_VERSION_10_4) && \
00044      ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
00045       (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
00046        __APPLE_CC__ >= 5330))
00047 #  ifndef HAVE___DSO_HANDLE
00048 #   define HAVE___DSO_HANDLE 1
00049 #  endif
00050 # endif
00051 #endif
00052 
00053 #if HAVE___DSO_HANDLE
00054 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
00055 #endif
00056 
00057 namespace {
00058 
00059 static struct RegisterJIT {
00060   RegisterJIT() { JIT::Register(); }
00061 } JITRegistrator;
00062 
00063 }
00064 
00065 extern "C" void LLVMLinkInJIT() {
00066 }
00067 
00068 
00069 #if defined(__GNUC__) && !defined(__ARM__EABI__)
00070  
00071 // libgcc defines the __register_frame function to dynamically register new
00072 // dwarf frames for exception handling. This functionality is not portable
00073 // across compilers and is only provided by GCC. We use the __register_frame
00074 // function here so that code generated by the JIT cooperates with the unwinding
00075 // runtime of libgcc. When JITting with exception handling enable, LLVM
00076 // generates dwarf frames and registers it to libgcc with __register_frame.
00077 //
00078 // The __register_frame function works with Linux.
00079 //
00080 // Unfortunately, this functionality seems to be in libgcc after the unwinding
00081 // library of libgcc for darwin was written. The code for darwin overwrites the
00082 // value updated by __register_frame with a value fetched with "keymgr".
00083 // "keymgr" is an obsolete functionality, which should be rewritten some day.
00084 // In the meantime, since "keymgr" is on all libgccs shipped with apple-gcc, we
00085 // need a workaround in LLVM which uses the "keymgr" to dynamically modify the
00086 // values of an opaque key, used by libgcc to find dwarf tables.
00087 
00088 extern "C" void __register_frame(void*);
00089 
00090 #if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED <= 1050
00091 # define USE_KEYMGR 1
00092 #else
00093 # define USE_KEYMGR 0
00094 #endif
00095 
00096 #if USE_KEYMGR
00097 
00098 namespace {
00099 
00100 // LibgccObject - This is the structure defined in libgcc. There is no #include
00101 // provided for this structure, so we also define it here. libgcc calls it
00102 // "struct object". The structure is undocumented in libgcc.
00103 struct LibgccObject {
00104   void *unused1;
00105   void *unused2;
00106   void *unused3;
00107   
00108   /// frame - Pointer to the exception table.
00109   void *frame;
00110   
00111   /// encoding -  The encoding of the object?
00112   union {
00113     struct {
00114       unsigned long sorted : 1;
00115       unsigned long from_array : 1;
00116       unsigned long mixed_encoding : 1;
00117       unsigned long encoding : 8;
00118       unsigned long count : 21; 
00119     } b;
00120     size_t i;
00121   } encoding;
00122   
00123   /// fde_end - libgcc defines this field only if some macro is defined. We
00124   /// include this field even if it may not there, to make libgcc happy.
00125   char *fde_end;
00126   
00127   /// next - At least we know it's a chained list!
00128   struct LibgccObject *next;
00129 };
00130 
00131 // "kemgr" stuff. Apparently, all frame tables are stored there.
00132 extern "C" void _keymgr_set_and_unlock_processwide_ptr(int, void *);
00133 extern "C" void *_keymgr_get_and_lock_processwide_ptr(int);
00134 #define KEYMGR_GCC3_DW2_OBJ_LIST        302     /* Dwarf2 object list  */
00135 
00136 /// LibgccObjectInfo - libgcc defines this struct as km_object_info. It
00137 /// probably contains all dwarf tables that are loaded.
00138 struct LibgccObjectInfo {
00139 
00140   /// seenObjects - LibgccObjects already parsed by the unwinding runtime.
00141   ///
00142   struct LibgccObject* seenObjects;
00143 
00144   /// unseenObjects - LibgccObjects not parsed yet by the unwinding runtime.
00145   ///
00146   struct LibgccObject* unseenObjects;
00147   
00148   unsigned unused[2];
00149 };
00150 
00151 /// darwin_register_frame - Since __register_frame does not work with darwin's
00152 /// libgcc,we provide our own function, which "tricks" libgcc by modifying the
00153 /// "Dwarf2 object list" key.
00154 void DarwinRegisterFrame(void* FrameBegin) {
00155   // Get the key.
00156   LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
00157     _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
00158   assert(LOI && "This should be preallocated by the runtime");
00159   
00160   // Allocate a new LibgccObject to represent this frame. Deallocation of this
00161   // object may be impossible: since darwin code in libgcc was written after
00162   // the ability to dynamically register frames, things may crash if we
00163   // deallocate it.
00164   struct LibgccObject* ob = (struct LibgccObject*)
00165     malloc(sizeof(struct LibgccObject));
00166   
00167   // Do like libgcc for the values of the field.
00168   ob->unused1 = (void *)-1;
00169   ob->unused2 = 0;
00170   ob->unused3 = 0;
00171   ob->frame = FrameBegin;
00172   ob->encoding.i = 0; 
00173   ob->encoding.b.encoding = llvm::dwarf::DW_EH_PE_omit;
00174   
00175   // Put the info on both places, as libgcc uses the first or the the second
00176   // field. Note that we rely on having two pointers here. If fde_end was a
00177   // char, things would get complicated.
00178   ob->fde_end = (char*)LOI->unseenObjects;
00179   ob->next = LOI->unseenObjects;
00180   
00181   // Update the key's unseenObjects list.
00182   LOI->unseenObjects = ob;
00183   
00184   // Finally update the "key". Apparently, libgcc requires it. 
00185   _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST,
00186                                          LOI);
00187 
00188 }
00189 
00190 }
00191 #endif // __APPLE__
00192 #endif // __GNUC__
00193 
00194 /// createJIT - This is the factory method for creating a JIT for the current
00195 /// machine, it does not fall back to the interpreter.  This takes ownership
00196 /// of the module provider.
00197 ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP,
00198                                             std::string *ErrorStr,
00199                                             JITMemoryManager *JMM,
00200                                             CodeGenOpt::Level OptLevel,
00201                                             bool GVsWithCode) {
00202     return JIT::createJIT(MP, ErrorStr, JMM, OptLevel, GVsWithCode);
00203 }
00204 
00205 ExecutionEngine *JIT::createJIT(ModuleProvider *MP,
00206                                 std::string *ErrorStr,
00207                                 JITMemoryManager *JMM,
00208                                 CodeGenOpt::Level OptLevel,
00209                                 bool GVsWithCode) {
00210   // Make sure we can resolve symbols in the program as well. The zero arg
00211   // to the function tells DynamicLibrary to load the program, not a library.
00212   if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
00213     return 0;
00214 
00215   // Pick a target either via -march or by guessing the native arch.
00216   TargetMachine *TM = JIT::selectTarget(MP, ErrorStr);
00217   if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
00218 
00219   // If the target supports JIT code generation, create a the JIT.
00220   if (TargetJITInfo *TJ = TM->getJITInfo()) {
00221     return new JIT(MP, *TM, *TJ, JMM, OptLevel, GVsWithCode);
00222   } else {
00223     if (ErrorStr)
00224       *ErrorStr = "target does not support JIT code generation";
00225     return 0;
00226   }
00227 }
00228 
00229 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
00230          JITMemoryManager *JMM, CodeGenOpt::Level OptLevel, bool GVsWithCode)
00231   : ExecutionEngine(MP), TM(tm), TJI(tji), AllocateGVsWithCode(GVsWithCode) {
00232   setTargetData(TM.getTargetData());
00233 
00234   jitstate = new JITState(MP);
00235 
00236   // Initialize JCE
00237   JCE = createEmitter(*this, JMM);
00238 
00239   // Add target data
00240   MutexGuard locked(lock);
00241   FunctionPassManager &PM = jitstate->getPM(locked);
00242   PM.add(new TargetData(*TM.getTargetData()));
00243 
00244   // Turn the machine code intermediate representation into bytes in memory that
00245   // may be executed.
00246   if (TM.addPassesToEmitMachineCode(PM, *JCE, OptLevel)) {
00247     llvm_report_error("Target does not support machine code emission!");
00248   }
00249   
00250   // Register routine for informing unwinding runtime about new EH frames
00251 #if defined(__GNUC__) && !defined(__ARM_EABI__)
00252 #if USE_KEYMGR
00253   struct LibgccObjectInfo* LOI = (struct LibgccObjectInfo*)
00254     _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST);
00255   
00256   // The key is created on demand, and libgcc creates it the first time an
00257   // exception occurs. Since we need the key to register frames, we create
00258   // it now.
00259   if (!LOI)
00260     LOI = (LibgccObjectInfo*)calloc(sizeof(struct LibgccObjectInfo), 1); 
00261   _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST, LOI);
00262   InstallExceptionTableRegister(DarwinRegisterFrame);
00263 #else
00264   InstallExceptionTableRegister(__register_frame);
00265 #endif // __APPLE__
00266 #endif // __GNUC__
00267   
00268   // Initialize passes.
00269   PM.doInitialization();
00270 }
00271 
00272 JIT::~JIT() {
00273   delete jitstate;
00274   delete JCE;
00275   delete &TM;
00276 }
00277 
00278 /// addModuleProvider - Add a new ModuleProvider to the JIT.  If we previously
00279 /// removed the last ModuleProvider, we need re-initialize jitstate with a valid
00280 /// ModuleProvider.
00281 void JIT::addModuleProvider(ModuleProvider *MP) {
00282   MutexGuard locked(lock);
00283 
00284   if (Modules.empty()) {
00285     assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
00286 
00287     jitstate = new JITState(MP);
00288 
00289     FunctionPassManager &PM = jitstate->getPM(locked);
00290     PM.add(new TargetData(*TM.getTargetData()));
00291 
00292     // Turn the machine code intermediate representation into bytes in memory
00293     // that may be executed.
00294     if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
00295       llvm_report_error("Target does not support machine code emission!");
00296     }
00297     
00298     // Initialize passes.
00299     PM.doInitialization();
00300   }
00301   
00302   ExecutionEngine::addModuleProvider(MP);
00303 }
00304 
00305 /// removeModuleProvider - If we are removing the last ModuleProvider, 
00306 /// invalidate the jitstate since the PassManager it contains references a
00307 /// released ModuleProvider.
00308 Module *JIT::removeModuleProvider(ModuleProvider *MP, std::string *E) {
00309   Module *result = ExecutionEngine::removeModuleProvider(MP, E);
00310   
00311   MutexGuard locked(lock);
00312   
00313   if (jitstate->getMP() == MP) {
00314     delete jitstate;
00315     jitstate = 0;
00316   }
00317   
00318   if (!jitstate && !Modules.empty()) {
00319     jitstate = new JITState(Modules[0]);
00320 
00321     FunctionPassManager &PM = jitstate->getPM(locked);
00322     PM.add(new TargetData(*TM.getTargetData()));
00323     
00324     // Turn the machine code intermediate representation into bytes in memory
00325     // that may be executed.
00326     if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
00327       llvm_report_error("Target does not support machine code emission!");
00328     }
00329     
00330     // Initialize passes.
00331     PM.doInitialization();
00332   }    
00333   return result;
00334 }
00335 
00336 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
00337 /// and deletes the ModuleProvider and owned Module.  Avoids materializing 
00338 /// the underlying module.
00339 void JIT::deleteModuleProvider(ModuleProvider *MP, std::string *E) {
00340   ExecutionEngine::deleteModuleProvider(MP, E);
00341   
00342   MutexGuard locked(lock);
00343   
00344   if (jitstate->getMP() == MP) {
00345     delete jitstate;
00346     jitstate = 0;
00347   }
00348 
00349   if (!jitstate && !Modules.empty()) {
00350     jitstate = new JITState(Modules[0]);
00351     
00352     FunctionPassManager &PM = jitstate->getPM(locked);
00353     PM.add(new TargetData(*TM.getTargetData()));
00354     
00355     // Turn the machine code intermediate representation into bytes in memory
00356     // that may be executed.
00357     if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) {
00358       llvm_report_error("Target does not support machine code emission!");
00359     }
00360     
00361     // Initialize passes.
00362     PM.doInitialization();
00363   }    
00364 }
00365 
00366 /// run - Start execution with the specified function and arguments.
00367 ///
00368 GenericValue JIT::runFunction(Function *F,
00369                               const std::vector<GenericValue> &ArgValues) {
00370   assert(F && "Function *F was null at entry to run()");
00371 
00372   void *FPtr = getPointerToFunction(F);
00373   assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
00374   const FunctionType *FTy = F->getFunctionType();
00375   const Type *RetTy = FTy->getReturnType();
00376 
00377   assert((FTy->getNumParams() == ArgValues.size() ||
00378           (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
00379          "Wrong number of arguments passed into function!");
00380   assert(FTy->getNumParams() == ArgValues.size() &&
00381          "This doesn't support passing arguments through varargs (yet)!");
00382 
00383   // Handle some common cases first.  These cases correspond to common `main'
00384   // prototypes.
00385   if (RetTy == Type::getInt32Ty(F->getContext()) ||
00386       RetTy == Type::getVoidTy(F->getContext())) {
00387     switch (ArgValues.size()) {
00388     case 3:
00389       if (FTy->getParamType(0) == Type::getInt32Ty(F->getContext()) &&
00390           isa<PointerType>(FTy->getParamType(1)) &&
00391           isa<PointerType>(FTy->getParamType(2))) {
00392         int (*PF)(int, char **, const char **) =
00393           (int(*)(int, char **, const char **))(intptr_t)FPtr;
00394 
00395         // Call the function.
00396         GenericValue rv;
00397         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), 
00398                                  (char **)GVTOP(ArgValues[1]),
00399                                  (const char **)GVTOP(ArgValues[2])));
00400         return rv;
00401       }
00402       break;
00403     case 2:
00404       if (FTy->getParamType(0) == Type::getInt32Ty(F->getContext()) &&
00405           isa<PointerType>(FTy->getParamType(1))) {
00406         int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
00407 
00408         // Call the function.
00409         GenericValue rv;
00410         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), 
00411                                  (char **)GVTOP(ArgValues[1])));
00412         return rv;
00413       }
00414       break;
00415     case 1:
00416       if (FTy->getNumParams() == 1 &&
00417           FTy->getParamType(0) == Type::getInt32Ty(F->getContext())) {
00418         GenericValue rv;
00419         int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
00420         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
00421         return rv;
00422       }
00423       break;
00424     }
00425   }
00426 
00427   // Handle cases where no arguments are passed first.
00428   if (ArgValues.empty()) {
00429     GenericValue rv;
00430     switch (RetTy->getTypeID()) {
00431     default: llvm_unreachable("Unknown return type for function call!");
00432     case Type::IntegerTyID: {
00433       unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
00434       if (BitWidth == 1)
00435         rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
00436       else if (BitWidth <= 8)
00437         rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
00438       else if (BitWidth <= 16)
00439         rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
00440       else if (BitWidth <= 32)
00441         rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
00442       else if (BitWidth <= 64)
00443         rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
00444       else 
00445         llvm_unreachable("Integer types > 64 bits not supported");
00446       return rv;
00447     }
00448     case Type::VoidTyID:
00449       rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
00450       return rv;
00451     case Type::FloatTyID:
00452       rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
00453       return rv;
00454     case Type::DoubleTyID:
00455       rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
00456       return rv;
00457     case Type::X86_FP80TyID:
00458     case Type::FP128TyID:
00459     case Type::PPC_FP128TyID:
00460       llvm_unreachable("long double not supported yet");
00461       return rv;
00462     case Type::PointerTyID:
00463       return PTOGV(((void*(*)())(intptr_t)FPtr)());
00464     }
00465   }
00466 
00467   // Okay, this is not one of our quick and easy cases.  Because we don't have a
00468   // full FFI, we have to codegen a nullary stub function that just calls the
00469   // function we are interested in, passing in constants for all of the
00470   // arguments.  Make this function and return.
00471 
00472   // First, create the function.
00473   FunctionType *STy=FunctionType::get(RetTy, false);
00474   Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
00475                                     F->getParent());
00476 
00477   // Insert a basic block.
00478   BasicBlock *StubBB = BasicBlock::Create(F->getContext(), "", Stub);
00479 
00480   // Convert all of the GenericValue arguments over to constants.  Note that we
00481   // currently don't support varargs.
00482   SmallVector<Value*, 8> Args;
00483   for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
00484     Constant *C = 0;
00485     const Type *ArgTy = FTy->getParamType(i);
00486     const GenericValue &AV = ArgValues[i];
00487     switch (ArgTy->getTypeID()) {
00488     default: llvm_unreachable("Unknown argument type for function call!");
00489     case Type::IntegerTyID:
00490         C = ConstantInt::get(F->getContext(), AV.IntVal);
00491         break;
00492     case Type::FloatTyID:
00493         C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal));
00494         break;
00495     case Type::DoubleTyID:
00496         C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal));
00497         break;
00498     case Type::PPC_FP128TyID:
00499     case Type::X86_FP80TyID:
00500     case Type::FP128TyID:
00501         C = ConstantFP::get(F->getContext(), APFloat(AV.IntVal));
00502         break;
00503     case Type::PointerTyID:
00504       void *ArgPtr = GVTOP(AV);
00505       if (sizeof(void*) == 4)
00506         C = ConstantInt::get(Type::getInt32Ty(F->getContext()), 
00507                              (int)(intptr_t)ArgPtr);
00508       else
00509         C = ConstantInt::get(Type::getInt64Ty(F->getContext()),
00510                              (intptr_t)ArgPtr);
00511       // Cast the integer to pointer
00512       C = ConstantExpr::getIntToPtr(C, ArgTy);
00513       break;
00514     }
00515     Args.push_back(C);
00516   }
00517 
00518   CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(),
00519                                        "", StubBB);
00520   TheCall->setCallingConv(F->getCallingConv());
00521   TheCall->setTailCall();
00522   if (TheCall->getType() != Type::getVoidTy(F->getContext()))
00523     // Return result of the call.
00524     ReturnInst::Create(F->getContext(), TheCall, StubBB);
00525   else
00526     ReturnInst::Create(F->getContext(), StubBB);           // Just return void.
00527 
00528   // Finally, return the value returned by our nullary stub function.
00529   return runFunction(Stub, std::vector<GenericValue>());
00530 }
00531 
00532 void JIT::RegisterJITEventListener(JITEventListener *L) {
00533   if (L == NULL)
00534     return;
00535   MutexGuard locked(lock);
00536   EventListeners.push_back(L);
00537 }
00538 void JIT::UnregisterJITEventListener(JITEventListener *L) {
00539   if (L == NULL)
00540     return;
00541   MutexGuard locked(lock);
00542   std::vector<JITEventListener*>::reverse_iterator I=
00543       std::find(EventListeners.rbegin(), EventListeners.rend(), L);
00544   if (I != EventListeners.rend()) {
00545     std::swap(*I, EventListeners.back());
00546     EventListeners.pop_back();
00547   }
00548 }
00549 void JIT::NotifyFunctionEmitted(
00550     const Function &F,
00551     void *Code, size_t Size,
00552     const JITEvent_EmittedFunctionDetails &Details) {
00553   MutexGuard locked(lock);
00554   for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
00555     EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details);
00556   }
00557 }
00558 
00559 void JIT::NotifyFreeingMachineCode(const Function &F, void *OldPtr) {
00560   MutexGuard locked(lock);
00561   for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
00562     EventListeners[I]->NotifyFreeingMachineCode(F, OldPtr);
00563   }
00564 }
00565 
00566 /// runJITOnFunction - Run the FunctionPassManager full of
00567 /// just-in-time compilation passes on F, hopefully filling in
00568 /// GlobalAddress[F] with the address of F's machine code.
00569 ///
00570 void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
00571   MutexGuard locked(lock);
00572 
00573   class MCIListener : public JITEventListener {
00574     MachineCodeInfo *const MCI;
00575    public:
00576     MCIListener(MachineCodeInfo *mci) : MCI(mci) {}
00577     virtual void NotifyFunctionEmitted(const Function &,
00578                                        void *Code, size_t Size,
00579                                        const EmittedFunctionDetails &) {
00580       MCI->setAddress(Code);
00581       MCI->setSize(Size);
00582     }
00583   };
00584   MCIListener MCIL(MCI);
00585   RegisterJITEventListener(&MCIL);
00586 
00587   runJITOnFunctionUnlocked(F, locked);
00588 
00589   UnregisterJITEventListener(&MCIL);
00590 }
00591 
00592 void JIT::runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked) {
00593   static bool isAlreadyCodeGenerating = false;
00594   assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
00595 
00596   // JIT the function
00597   isAlreadyCodeGenerating = true;
00598   jitstate->getPM(locked).run(*F);
00599   isAlreadyCodeGenerating = false;
00600 
00601   // If the function referred to another function that had not yet been
00602   // read from bitcode, but we are jitting non-lazily, emit it now.
00603   while (!jitstate->getPendingFunctions(locked).empty()) {
00604     Function *PF = jitstate->getPendingFunctions(locked).back();
00605     jitstate->getPendingFunctions(locked).pop_back();
00606 
00607     // JIT the function
00608     isAlreadyCodeGenerating = true;
00609     jitstate->getPM(locked).run(*PF);
00610     isAlreadyCodeGenerating = false;
00611     
00612     // Now that the function has been jitted, ask the JITEmitter to rewrite
00613     // the stub with real address of the function.
00614     updateFunctionStub(PF);
00615   }
00616   
00617   // If the JIT is configured to emit info so that dlsym can be used to
00618   // rewrite stubs to external globals, do so now.
00619   if (areDlsymStubsEnabled() && isLazyCompilationDisabled())
00620     updateDlsymStubTable();
00621 }
00622 
00623 /// getPointerToFunction - This method is used to get the address of the
00624 /// specified function, compiling it if neccesary.
00625 ///
00626 void *JIT::getPointerToFunction(Function *F) {
00627 
00628   if (void *Addr = getPointerToGlobalIfAvailable(F))
00629     return Addr;   // Check if function already code gen'd
00630 
00631   MutexGuard locked(lock);
00632   
00633   // Now that this thread owns the lock, check if another thread has already
00634   // code gen'd the function.
00635   if (void *Addr = getPointerToGlobalIfAvailable(F))
00636     return Addr;  
00637 
00638   // Make sure we read in the function if it exists in this Module.
00639   if (F->hasNotBeenReadFromBitcode()) {
00640     // Determine the module provider this function is provided by.
00641     Module *M = F->getParent();
00642     ModuleProvider *MP = 0;
00643     for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
00644       if (Modules[i]->getModule() == M) {
00645         MP = Modules[i];
00646         break;
00647       }
00648     }
00649     assert(MP && "Function isn't in a module we know about!");
00650     
00651     std::string ErrorMsg;
00652     if (MP->materializeFunction(F, &ErrorMsg)) {
00653       llvm_report_error("Error reading function '" + F->getName()+
00654                         "' from bitcode file: " + ErrorMsg);
00655     }
00656 
00657     // Now retry to get the address.
00658     if (void *Addr = getPointerToGlobalIfAvailable(F))
00659       return Addr;
00660   }
00661 
00662   if (F->isDeclaration()) {
00663     bool AbortOnFailure =
00664       !areDlsymStubsEnabled() && !F->hasExternalWeakLinkage();
00665     void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
00666     addGlobalMapping(F, Addr);
00667     return Addr;
00668   }
00669 
00670   runJITOnFunctionUnlocked(F, locked);
00671 
00672   void *Addr = getPointerToGlobalIfAvailable(F);
00673   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
00674   return Addr;
00675 }
00676 
00677 /// getOrEmitGlobalVariable - Return the address of the specified global
00678 /// variable, possibly emitting it to memory if needed.  This is used by the
00679 /// Emitter.
00680 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
00681   MutexGuard locked(lock);
00682 
00683   void *Ptr = getPointerToGlobalIfAvailable(GV);
00684   if (Ptr) return Ptr;
00685 
00686   // If the global is external, just remember the address.
00687   if (GV->isDeclaration()) {
00688 #if HAVE___DSO_HANDLE
00689     if (GV->getName() == "__dso_handle")
00690       return (void*)&__dso_handle;
00691 #endif
00692     Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName());
00693     if (Ptr == 0 && !areDlsymStubsEnabled()) {
00694       llvm_report_error("Could not resolve external global address: "
00695                         +GV->getName());
00696     }
00697     addGlobalMapping(GV, Ptr);
00698   } else {
00699     // If the global hasn't been emitted to memory yet, allocate space and
00700     // emit it into memory.
00701     Ptr = getMemoryForGV(GV);
00702     addGlobalMapping(GV, Ptr);
00703     EmitGlobalVariable(GV);  // Initialize the variable.
00704   }
00705   return Ptr;
00706 }
00707 
00708 /// recompileAndRelinkFunction - This method is used to force a function
00709 /// which has already been compiled, to be compiled again, possibly
00710 /// after it has been modified. Then the entry to the old copy is overwritten
00711 /// with a branch to the new copy. If there was no old copy, this acts
00712 /// just like JIT::getPointerToFunction().
00713 ///
00714 void *JIT::recompileAndRelinkFunction(Function *F) {
00715   void *OldAddr = getPointerToGlobalIfAvailable(F);
00716 
00717   // If it's not already compiled there is no reason to patch it up.
00718   if (OldAddr == 0) { return getPointerToFunction(F); }
00719 
00720   // Delete the old function mapping.
00721   addGlobalMapping(F, 0);
00722 
00723   // Recodegen the function
00724   runJITOnFunction(F);
00725 
00726   // Update state, forward the old function to the new function.
00727   void *Addr = getPointerToGlobalIfAvailable(F);
00728   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
00729   TJI.replaceMachineCodeForFunction(OldAddr, Addr);
00730   return Addr;
00731 }
00732 
00733 /// getMemoryForGV - This method abstracts memory allocation of global
00734 /// variable so that the JIT can allocate thread local variables depending
00735 /// on the target.
00736 ///
00737 char* JIT::getMemoryForGV(const GlobalVariable* GV) {
00738   char *Ptr;
00739 
00740   // GlobalVariable's which are not "constant" will cause trouble in a server
00741   // situation. It's returned in the same block of memory as code which may
00742   // not be writable.
00743   if (isGVCompilationDisabled() && !GV->isConstant()) {
00744     llvm_report_error("Compilation of non-internal GlobalValue is disabled!");
00745   }
00746 
00747   // Some applications require globals and code to live together, so they may
00748   // be allocated into the same buffer, but in general globals are allocated
00749   // through the memory manager which puts them near the code but not in the
00750   // same buffer.
00751   const Type *GlobalType = GV->getType()->getElementType();
00752   size_t S = getTargetData()->getTypeAllocSize(GlobalType);
00753   size_t A = getTargetData()->getPreferredAlignment(GV);
00754   if (GV->isThreadLocal()) {
00755     MutexGuard locked(lock);
00756     Ptr = TJI.allocateThreadLocalMemory(S);
00757   } else if (TJI.allocateSeparateGVMemory()) {
00758     if (A <= 8) {
00759       Ptr = (char*)malloc(S);
00760     } else {
00761       // Allocate S+A bytes of memory, then use an aligned pointer within that
00762       // space.
00763       Ptr = (char*)malloc(S+A);
00764       unsigned MisAligned = ((intptr_t)Ptr & (A-1));
00765       Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0);
00766     }
00767   } else if (AllocateGVsWithCode) {
00768     Ptr = (char*)JCE->allocateSpace(S, A);
00769   } else {
00770     Ptr = (char*)JCE->allocateGlobal(S, A);
00771   }
00772   return Ptr;
00773 }
00774 
00775 void JIT::addPendingFunction(Function *F) {
00776   MutexGuard locked(lock);
00777   jitstate->getPendingFunctions(locked).push_back(F);
00778 }
00779 
00780 
00781 JITEventListener::~JITEventListener() {}