LLVM API Documentation

ExecutionEngine.cpp
Go to the documentation of this file.
00001 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
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 file defines the common interface used by the various execution engine
00011 // subclasses.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/ExecutionEngine/ExecutionEngine.h"
00016 #include "llvm/ADT/SmallString.h"
00017 #include "llvm/ADT/Statistic.h"
00018 #include "llvm/ExecutionEngine/GenericValue.h"
00019 #include "llvm/ExecutionEngine/JITMemoryManager.h"
00020 #include "llvm/ExecutionEngine/ObjectCache.h"
00021 #include "llvm/IR/Constants.h"
00022 #include "llvm/IR/DataLayout.h"
00023 #include "llvm/IR/DerivedTypes.h"
00024 #include "llvm/IR/Module.h"
00025 #include "llvm/IR/Operator.h"
00026 #include "llvm/IR/ValueHandle.h"
00027 #include "llvm/Object/ObjectFile.h"
00028 #include "llvm/Support/Debug.h"
00029 #include "llvm/Support/DynamicLibrary.h"
00030 #include "llvm/Support/ErrorHandling.h"
00031 #include "llvm/Support/Host.h"
00032 #include "llvm/Support/MutexGuard.h"
00033 #include "llvm/Support/TargetRegistry.h"
00034 #include "llvm/Support/raw_ostream.h"
00035 #include "llvm/Target/TargetMachine.h"
00036 #include <cmath>
00037 #include <cstring>
00038 using namespace llvm;
00039 
00040 #define DEBUG_TYPE "jit"
00041 
00042 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
00043 STATISTIC(NumGlobals  , "Number of global vars initialized");
00044 
00045 // Pin the vtable to this file.
00046 void ObjectCache::anchor() {}
00047 void ObjectBuffer::anchor() {}
00048 void ObjectBufferStream::anchor() {}
00049 
00050 ExecutionEngine *(*ExecutionEngine::JITCtor)(
00051   Module *M,
00052   std::string *ErrorStr,
00053   JITMemoryManager *JMM,
00054   bool GVsWithCode,
00055   TargetMachine *TM) = nullptr;
00056 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
00057   Module *M,
00058   std::string *ErrorStr,
00059   RTDyldMemoryManager *MCJMM,
00060   bool GVsWithCode,
00061   TargetMachine *TM) = nullptr;
00062 ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
00063                                                 std::string *ErrorStr) =nullptr;
00064 
00065 ExecutionEngine::ExecutionEngine(Module *M)
00066   : EEState(*this),
00067     LazyFunctionCreator(nullptr) {
00068   CompilingLazily         = false;
00069   GVCompilationDisabled   = false;
00070   SymbolSearchingDisabled = false;
00071 
00072   // IR module verification is enabled by default in debug builds, and disabled
00073   // by default in release builds.
00074 #ifndef NDEBUG
00075   VerifyModules = true;
00076 #else
00077   VerifyModules = false;
00078 #endif
00079 
00080   Modules.push_back(M);
00081   assert(M && "Module is null?");
00082 }
00083 
00084 ExecutionEngine::~ExecutionEngine() {
00085   clearAllGlobalMappings();
00086   for (unsigned i = 0, e = Modules.size(); i != e; ++i)
00087     delete Modules[i];
00088 }
00089 
00090 namespace {
00091 /// \brief Helper class which uses a value handler to automatically deletes the
00092 /// memory block when the GlobalVariable is destroyed.
00093 class GVMemoryBlock : public CallbackVH {
00094   GVMemoryBlock(const GlobalVariable *GV)
00095     : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
00096 
00097 public:
00098   /// \brief Returns the address the GlobalVariable should be written into.  The
00099   /// GVMemoryBlock object prefixes that.
00100   static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
00101     Type *ElTy = GV->getType()->getElementType();
00102     size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
00103     void *RawMemory = ::operator new(
00104       DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
00105                                    TD.getPreferredAlignment(GV))
00106       + GVSize);
00107     new(RawMemory) GVMemoryBlock(GV);
00108     return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
00109   }
00110 
00111   void deleted() override {
00112     // We allocated with operator new and with some extra memory hanging off the
00113     // end, so don't just delete this.  I'm not sure if this is actually
00114     // required.
00115     this->~GVMemoryBlock();
00116     ::operator delete(this);
00117   }
00118 };
00119 }  // anonymous namespace
00120 
00121 char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
00122   return GVMemoryBlock::Create(GV, *getDataLayout());
00123 }
00124 
00125 void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
00126   llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
00127 }
00128 
00129 bool ExecutionEngine::removeModule(Module *M) {
00130   for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
00131         E = Modules.end(); I != E; ++I) {
00132     Module *Found = *I;
00133     if (Found == M) {
00134       Modules.erase(I);
00135       clearGlobalMappingsFromModule(M);
00136       return true;
00137     }
00138   }
00139   return false;
00140 }
00141 
00142 Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
00143   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
00144     if (Function *F = Modules[i]->getFunction(FnName))
00145       return F;
00146   }
00147   return nullptr;
00148 }
00149 
00150 
00151 void *ExecutionEngineState::RemoveMapping(const GlobalValue *ToUnmap) {
00152   GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
00153   void *OldVal;
00154 
00155   // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
00156   // GlobalAddressMap.
00157   if (I == GlobalAddressMap.end())
00158     OldVal = nullptr;
00159   else {
00160     OldVal = I->second;
00161     GlobalAddressMap.erase(I);
00162   }
00163 
00164   GlobalAddressReverseMap.erase(OldVal);
00165   return OldVal;
00166 }
00167 
00168 void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
00169   MutexGuard locked(lock);
00170 
00171   DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
00172         << "\' to [" << Addr << "]\n";);
00173   void *&CurVal = EEState.getGlobalAddressMap()[GV];
00174   assert((!CurVal || !Addr) && "GlobalMapping already established!");
00175   CurVal = Addr;
00176 
00177   // If we are using the reverse mapping, add it too.
00178   if (!EEState.getGlobalAddressReverseMap().empty()) {
00179     AssertingVH<const GlobalValue> &V =
00180       EEState.getGlobalAddressReverseMap()[Addr];
00181     assert((!V || !GV) && "GlobalMapping already established!");
00182     V = GV;
00183   }
00184 }
00185 
00186 void ExecutionEngine::clearAllGlobalMappings() {
00187   MutexGuard locked(lock);
00188 
00189   EEState.getGlobalAddressMap().clear();
00190   EEState.getGlobalAddressReverseMap().clear();
00191 }
00192 
00193 void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
00194   MutexGuard locked(lock);
00195 
00196   for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
00197     EEState.RemoveMapping(FI);
00198   for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
00199        GI != GE; ++GI)
00200     EEState.RemoveMapping(GI);
00201 }
00202 
00203 void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
00204   MutexGuard locked(lock);
00205 
00206   ExecutionEngineState::GlobalAddressMapTy &Map =
00207     EEState.getGlobalAddressMap();
00208 
00209   // Deleting from the mapping?
00210   if (!Addr)
00211     return EEState.RemoveMapping(GV);
00212 
00213   void *&CurVal = Map[GV];
00214   void *OldVal = CurVal;
00215 
00216   if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
00217     EEState.getGlobalAddressReverseMap().erase(CurVal);
00218   CurVal = Addr;
00219 
00220   // If we are using the reverse mapping, add it too.
00221   if (!EEState.getGlobalAddressReverseMap().empty()) {
00222     AssertingVH<const GlobalValue> &V =
00223       EEState.getGlobalAddressReverseMap()[Addr];
00224     assert((!V || !GV) && "GlobalMapping already established!");
00225     V = GV;
00226   }
00227   return OldVal;
00228 }
00229 
00230 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
00231   MutexGuard locked(lock);
00232 
00233   ExecutionEngineState::GlobalAddressMapTy::iterator I =
00234     EEState.getGlobalAddressMap().find(GV);
00235   return I != EEState.getGlobalAddressMap().end() ? I->second : nullptr;
00236 }
00237 
00238 const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
00239   MutexGuard locked(lock);
00240 
00241   // If we haven't computed the reverse mapping yet, do so first.
00242   if (EEState.getGlobalAddressReverseMap().empty()) {
00243     for (ExecutionEngineState::GlobalAddressMapTy::iterator
00244          I = EEState.getGlobalAddressMap().begin(),
00245          E = EEState.getGlobalAddressMap().end(); I != E; ++I)
00246       EEState.getGlobalAddressReverseMap().insert(std::make_pair(
00247                                                           I->second, I->first));
00248   }
00249 
00250   std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
00251     EEState.getGlobalAddressReverseMap().find(Addr);
00252   return I != EEState.getGlobalAddressReverseMap().end() ? I->second : nullptr;
00253 }
00254 
00255 namespace {
00256 class ArgvArray {
00257   char *Array;
00258   std::vector<char*> Values;
00259 public:
00260   ArgvArray() : Array(nullptr) {}
00261   ~ArgvArray() { clear(); }
00262   void clear() {
00263     delete[] Array;
00264     Array = nullptr;
00265     for (size_t I = 0, E = Values.size(); I != E; ++I) {
00266       delete[] Values[I];
00267     }
00268     Values.clear();
00269   }
00270   /// Turn a vector of strings into a nice argv style array of pointers to null
00271   /// terminated strings.
00272   void *reset(LLVMContext &C, ExecutionEngine *EE,
00273               const std::vector<std::string> &InputArgv);
00274 };
00275 }  // anonymous namespace
00276 void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
00277                        const std::vector<std::string> &InputArgv) {
00278   clear();  // Free the old contents.
00279   unsigned PtrSize = EE->getDataLayout()->getPointerSize();
00280   Array = new char[(InputArgv.size()+1)*PtrSize];
00281 
00282   DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
00283   Type *SBytePtr = Type::getInt8PtrTy(C);
00284 
00285   for (unsigned i = 0; i != InputArgv.size(); ++i) {
00286     unsigned Size = InputArgv[i].size()+1;
00287     char *Dest = new char[Size];
00288     Values.push_back(Dest);
00289     DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
00290 
00291     std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
00292     Dest[Size-1] = 0;
00293 
00294     // Endian safe: Array[i] = (PointerTy)Dest;
00295     EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
00296                            SBytePtr);
00297   }
00298 
00299   // Null terminate it
00300   EE->StoreValueToMemory(PTOGV(nullptr),
00301                          (GenericValue*)(Array+InputArgv.size()*PtrSize),
00302                          SBytePtr);
00303   return Array;
00304 }
00305 
00306 void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
00307                                                        bool isDtors) {
00308   const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
00309   GlobalVariable *GV = module->getNamedGlobal(Name);
00310 
00311   // If this global has internal linkage, or if it has a use, then it must be
00312   // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
00313   // this is the case, don't execute any of the global ctors, __main will do
00314   // it.
00315   if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
00316 
00317   // Should be an array of '{ i32, void ()* }' structs.  The first value is
00318   // the init priority, which we ignore.
00319   ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
00320   if (!InitList)
00321     return;
00322   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
00323     ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
00324     if (!CS) continue;
00325 
00326     Constant *FP = CS->getOperand(1);
00327     if (FP->isNullValue())
00328       continue;  // Found a sentinal value, ignore.
00329 
00330     // Strip off constant expression casts.
00331     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
00332       if (CE->isCast())
00333         FP = CE->getOperand(0);
00334 
00335     // Execute the ctor/dtor function!
00336     if (Function *F = dyn_cast<Function>(FP))
00337       runFunction(F, std::vector<GenericValue>());
00338 
00339     // FIXME: It is marginally lame that we just do nothing here if we see an
00340     // entry we don't recognize. It might not be unreasonable for the verifier
00341     // to not even allow this and just assert here.
00342   }
00343 }
00344 
00345 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
00346   // Execute global ctors/dtors for each module in the program.
00347   for (unsigned i = 0, e = Modules.size(); i != e; ++i)
00348     runStaticConstructorsDestructors(Modules[i], isDtors);
00349 }
00350 
00351 #ifndef NDEBUG
00352 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
00353 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
00354   unsigned PtrSize = EE->getDataLayout()->getPointerSize();
00355   for (unsigned i = 0; i < PtrSize; ++i)
00356     if (*(i + (uint8_t*)Loc))
00357       return false;
00358   return true;
00359 }
00360 #endif
00361 
00362 int ExecutionEngine::runFunctionAsMain(Function *Fn,
00363                                        const std::vector<std::string> &argv,
00364                                        const char * const * envp) {
00365   std::vector<GenericValue> GVArgs;
00366   GenericValue GVArgc;
00367   GVArgc.IntVal = APInt(32, argv.size());
00368 
00369   // Check main() type
00370   unsigned NumArgs = Fn->getFunctionType()->getNumParams();
00371   FunctionType *FTy = Fn->getFunctionType();
00372   Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
00373 
00374   // Check the argument types.
00375   if (NumArgs > 3)
00376     report_fatal_error("Invalid number of arguments of main() supplied");
00377   if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
00378     report_fatal_error("Invalid type for third argument of main() supplied");
00379   if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
00380     report_fatal_error("Invalid type for second argument of main() supplied");
00381   if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
00382     report_fatal_error("Invalid type for first argument of main() supplied");
00383   if (!FTy->getReturnType()->isIntegerTy() &&
00384       !FTy->getReturnType()->isVoidTy())
00385     report_fatal_error("Invalid return type of main() supplied");
00386 
00387   ArgvArray CArgv;
00388   ArgvArray CEnv;
00389   if (NumArgs) {
00390     GVArgs.push_back(GVArgc); // Arg #0 = argc.
00391     if (NumArgs > 1) {
00392       // Arg #1 = argv.
00393       GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
00394       assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
00395              "argv[0] was null after CreateArgv");
00396       if (NumArgs > 2) {
00397         std::vector<std::string> EnvVars;
00398         for (unsigned i = 0; envp[i]; ++i)
00399           EnvVars.push_back(envp[i]);
00400         // Arg #2 = envp.
00401         GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
00402       }
00403     }
00404   }
00405 
00406   return runFunction(Fn, GVArgs).IntVal.getZExtValue();
00407 }
00408 
00409 ExecutionEngine *ExecutionEngine::create(Module *M,
00410                                          bool ForceInterpreter,
00411                                          std::string *ErrorStr,
00412                                          CodeGenOpt::Level OptLevel,
00413                                          bool GVsWithCode) {
00414 
00415   EngineBuilder EB =
00416       EngineBuilder(M)
00417           .setEngineKind(ForceInterpreter ? EngineKind::Interpreter
00418                                           : EngineKind::Either)
00419           .setErrorStr(ErrorStr)
00420           .setOptLevel(OptLevel)
00421           .setAllocateGVsWithCode(GVsWithCode);
00422 
00423   return EB.create();
00424 }
00425 
00426 /// createJIT - This is the factory method for creating a JIT for the current
00427 /// machine, it does not fall back to the interpreter.  This takes ownership
00428 /// of the module.
00429 ExecutionEngine *ExecutionEngine::createJIT(Module *M,
00430                                             std::string *ErrorStr,
00431                                             JITMemoryManager *JMM,
00432                                             CodeGenOpt::Level OL,
00433                                             bool GVsWithCode,
00434                                             Reloc::Model RM,
00435                                             CodeModel::Model CMM) {
00436   if (!ExecutionEngine::JITCtor) {
00437     if (ErrorStr)
00438       *ErrorStr = "JIT has not been linked in.";
00439     return nullptr;
00440   }
00441 
00442   // Use the defaults for extra parameters.  Users can use EngineBuilder to
00443   // set them.
00444   EngineBuilder EB(M);
00445   EB.setEngineKind(EngineKind::JIT);
00446   EB.setErrorStr(ErrorStr);
00447   EB.setRelocationModel(RM);
00448   EB.setCodeModel(CMM);
00449   EB.setAllocateGVsWithCode(GVsWithCode);
00450   EB.setOptLevel(OL);
00451   EB.setJITMemoryManager(JMM);
00452 
00453   // TODO: permit custom TargetOptions here
00454   TargetMachine *TM = EB.selectTarget();
00455   if (!TM || (ErrorStr && ErrorStr->length() > 0)) return nullptr;
00456 
00457   return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM);
00458 }
00459 
00460 void EngineBuilder::InitEngine() {
00461   WhichEngine = EngineKind::Either;
00462   ErrorStr = nullptr;
00463   OptLevel = CodeGenOpt::Default;
00464   MCJMM = nullptr;
00465   JMM = nullptr;
00466   Options = TargetOptions();
00467   AllocateGVsWithCode = false;
00468   RelocModel = Reloc::Default;
00469   CMModel = CodeModel::JITDefault;
00470   UseMCJIT = false;
00471 
00472 // IR module verification is enabled by default in debug builds, and disabled
00473 // by default in release builds.
00474 #ifndef NDEBUG
00475   VerifyModules = true;
00476 #else
00477   VerifyModules = false;
00478 #endif
00479 }
00480 
00481 ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
00482   std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
00483 
00484   // Make sure we can resolve symbols in the program as well. The zero arg
00485   // to the function tells DynamicLibrary to load the program, not a library.
00486   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
00487     return nullptr;
00488 
00489   assert(!(JMM && MCJMM));
00490   
00491   // If the user specified a memory manager but didn't specify which engine to
00492   // create, we assume they only want the JIT, and we fail if they only want
00493   // the interpreter.
00494   if (JMM || MCJMM) {
00495     if (WhichEngine & EngineKind::JIT)
00496       WhichEngine = EngineKind::JIT;
00497     else {
00498       if (ErrorStr)
00499         *ErrorStr = "Cannot create an interpreter with a memory manager.";
00500       return nullptr;
00501     }
00502   }
00503   
00504   if (MCJMM && ! UseMCJIT) {
00505     if (ErrorStr)
00506       *ErrorStr =
00507         "Cannot create a legacy JIT with a runtime dyld memory "
00508         "manager.";
00509     return nullptr;
00510   }
00511 
00512   // Unless the interpreter was explicitly selected or the JIT is not linked,
00513   // try making a JIT.
00514   if ((WhichEngine & EngineKind::JIT) && TheTM) {
00515     Triple TT(M->getTargetTriple());
00516     if (!TM->getTarget().hasJIT()) {
00517       errs() << "WARNING: This target JIT is not designed for the host"
00518              << " you are running.  If bad things happen, please choose"
00519              << " a different -march switch.\n";
00520     }
00521 
00522     ExecutionEngine *EE = nullptr;
00523     if (UseMCJIT && ExecutionEngine::MCJITCtor)
00524       EE = ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
00525                                       AllocateGVsWithCode, TheTM.release());
00526     else if (ExecutionEngine::JITCtor)
00527       EE = ExecutionEngine::JITCtor(M, ErrorStr, JMM,
00528                                     AllocateGVsWithCode, TheTM.release());
00529 
00530     if (EE) {
00531       EE->setVerifyModules(VerifyModules);
00532       return EE;
00533     }
00534   }
00535 
00536   // If we can't make a JIT and we didn't request one specifically, try making
00537   // an interpreter instead.
00538   if (WhichEngine & EngineKind::Interpreter) {
00539     if (ExecutionEngine::InterpCtor)
00540       return ExecutionEngine::InterpCtor(M, ErrorStr);
00541     if (ErrorStr)
00542       *ErrorStr = "Interpreter has not been linked in.";
00543     return nullptr;
00544   }
00545 
00546   if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor &&
00547       !ExecutionEngine::MCJITCtor) {
00548     if (ErrorStr)
00549       *ErrorStr = "JIT has not been linked in.";
00550   }
00551 
00552   return nullptr;
00553 }
00554 
00555 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
00556   if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
00557     return getPointerToFunction(F);
00558 
00559   MutexGuard locked(lock);
00560   if (void *P = EEState.getGlobalAddressMap()[GV])
00561     return P;
00562 
00563   // Global variable might have been added since interpreter started.
00564   if (GlobalVariable *GVar =
00565           const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
00566     EmitGlobalVariable(GVar);
00567   else
00568     llvm_unreachable("Global hasn't had an address allocated yet!");
00569 
00570   return EEState.getGlobalAddressMap()[GV];
00571 }
00572 
00573 /// \brief Converts a Constant* into a GenericValue, including handling of
00574 /// ConstantExpr values.
00575 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
00576   // If its undefined, return the garbage.
00577   if (isa<UndefValue>(C)) {
00578     GenericValue Result;
00579     switch (C->getType()->getTypeID()) {
00580     default:
00581       break;
00582     case Type::IntegerTyID:
00583     case Type::X86_FP80TyID:
00584     case Type::FP128TyID:
00585     case Type::PPC_FP128TyID:
00586       // Although the value is undefined, we still have to construct an APInt
00587       // with the correct bit width.
00588       Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
00589       break;
00590     case Type::StructTyID: {
00591       // if the whole struct is 'undef' just reserve memory for the value.
00592       if(StructType *STy = dyn_cast<StructType>(C->getType())) {
00593         unsigned int elemNum = STy->getNumElements();
00594         Result.AggregateVal.resize(elemNum);
00595         for (unsigned int i = 0; i < elemNum; ++i) {
00596           Type *ElemTy = STy->getElementType(i);
00597           if (ElemTy->isIntegerTy())
00598             Result.AggregateVal[i].IntVal = 
00599               APInt(ElemTy->getPrimitiveSizeInBits(), 0);
00600           else if (ElemTy->isAggregateType()) {
00601               const Constant *ElemUndef = UndefValue::get(ElemTy);
00602               Result.AggregateVal[i] = getConstantValue(ElemUndef);
00603             }
00604           }
00605         }
00606       }
00607       break;
00608     case Type::VectorTyID:
00609       // if the whole vector is 'undef' just reserve memory for the value.
00610       const VectorType* VTy = dyn_cast<VectorType>(C->getType());
00611       const Type *ElemTy = VTy->getElementType();
00612       unsigned int elemNum = VTy->getNumElements();
00613       Result.AggregateVal.resize(elemNum);
00614       if (ElemTy->isIntegerTy())
00615         for (unsigned int i = 0; i < elemNum; ++i)
00616           Result.AggregateVal[i].IntVal =
00617             APInt(ElemTy->getPrimitiveSizeInBits(), 0);
00618       break;
00619     }
00620     return Result;
00621   }
00622 
00623   // Otherwise, if the value is a ConstantExpr...
00624   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
00625     Constant *Op0 = CE->getOperand(0);
00626     switch (CE->getOpcode()) {
00627     case Instruction::GetElementPtr: {
00628       // Compute the index
00629       GenericValue Result = getConstantValue(Op0);
00630       APInt Offset(DL->getPointerSizeInBits(), 0);
00631       cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
00632 
00633       char* tmp = (char*) Result.PointerVal;
00634       Result = PTOGV(tmp + Offset.getSExtValue());
00635       return Result;
00636     }
00637     case Instruction::Trunc: {
00638       GenericValue GV = getConstantValue(Op0);
00639       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
00640       GV.IntVal = GV.IntVal.trunc(BitWidth);
00641       return GV;
00642     }
00643     case Instruction::ZExt: {
00644       GenericValue GV = getConstantValue(Op0);
00645       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
00646       GV.IntVal = GV.IntVal.zext(BitWidth);
00647       return GV;
00648     }
00649     case Instruction::SExt: {
00650       GenericValue GV = getConstantValue(Op0);
00651       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
00652       GV.IntVal = GV.IntVal.sext(BitWidth);
00653       return GV;
00654     }
00655     case Instruction::FPTrunc: {
00656       // FIXME long double
00657       GenericValue GV = getConstantValue(Op0);
00658       GV.FloatVal = float(GV.DoubleVal);
00659       return GV;
00660     }
00661     case Instruction::FPExt:{
00662       // FIXME long double
00663       GenericValue GV = getConstantValue(Op0);
00664       GV.DoubleVal = double(GV.FloatVal);
00665       return GV;
00666     }
00667     case Instruction::UIToFP: {
00668       GenericValue GV = getConstantValue(Op0);
00669       if (CE->getType()->isFloatTy())
00670         GV.FloatVal = float(GV.IntVal.roundToDouble());
00671       else if (CE->getType()->isDoubleTy())
00672         GV.DoubleVal = GV.IntVal.roundToDouble();
00673       else if (CE->getType()->isX86_FP80Ty()) {
00674         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
00675         (void)apf.convertFromAPInt(GV.IntVal,
00676                                    false,
00677                                    APFloat::rmNearestTiesToEven);
00678         GV.IntVal = apf.bitcastToAPInt();
00679       }
00680       return GV;
00681     }
00682     case Instruction::SIToFP: {
00683       GenericValue GV = getConstantValue(Op0);
00684       if (CE->getType()->isFloatTy())
00685         GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
00686       else if (CE->getType()->isDoubleTy())
00687         GV.DoubleVal = GV.IntVal.signedRoundToDouble();
00688       else if (CE->getType()->isX86_FP80Ty()) {
00689         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
00690         (void)apf.convertFromAPInt(GV.IntVal,
00691                                    true,
00692                                    APFloat::rmNearestTiesToEven);
00693         GV.IntVal = apf.bitcastToAPInt();
00694       }
00695       return GV;
00696     }
00697     case Instruction::FPToUI: // double->APInt conversion handles sign
00698     case Instruction::FPToSI: {
00699       GenericValue GV = getConstantValue(Op0);
00700       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
00701       if (Op0->getType()->isFloatTy())
00702         GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
00703       else if (Op0->getType()->isDoubleTy())
00704         GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
00705       else if (Op0->getType()->isX86_FP80Ty()) {
00706         APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
00707         uint64_t v;
00708         bool ignored;
00709         (void)apf.convertToInteger(&v, BitWidth,
00710                                    CE->getOpcode()==Instruction::FPToSI,
00711                                    APFloat::rmTowardZero, &ignored);
00712         GV.IntVal = v; // endian?
00713       }
00714       return GV;
00715     }
00716     case Instruction::PtrToInt: {
00717       GenericValue GV = getConstantValue(Op0);
00718       uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
00719       assert(PtrWidth <= 64 && "Bad pointer width");
00720       GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
00721       uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
00722       GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
00723       return GV;
00724     }
00725     case Instruction::IntToPtr: {
00726       GenericValue GV = getConstantValue(Op0);
00727       uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
00728       GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
00729       assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
00730       GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
00731       return GV;
00732     }
00733     case Instruction::BitCast: {
00734       GenericValue GV = getConstantValue(Op0);
00735       Type* DestTy = CE->getType();
00736       switch (Op0->getType()->getTypeID()) {
00737         default: llvm_unreachable("Invalid bitcast operand");
00738         case Type::IntegerTyID:
00739           assert(DestTy->isFloatingPointTy() && "invalid bitcast");
00740           if (DestTy->isFloatTy())
00741             GV.FloatVal = GV.IntVal.bitsToFloat();
00742           else if (DestTy->isDoubleTy())
00743             GV.DoubleVal = GV.IntVal.bitsToDouble();
00744           break;
00745         case Type::FloatTyID:
00746           assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
00747           GV.IntVal = APInt::floatToBits(GV.FloatVal);
00748           break;
00749         case Type::DoubleTyID:
00750           assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
00751           GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
00752           break;
00753         case Type::PointerTyID:
00754           assert(DestTy->isPointerTy() && "Invalid bitcast");
00755           break; // getConstantValue(Op0)  above already converted it
00756       }
00757       return GV;
00758     }
00759     case Instruction::Add:
00760     case Instruction::FAdd:
00761     case Instruction::Sub:
00762     case Instruction::FSub:
00763     case Instruction::Mul:
00764     case Instruction::FMul:
00765     case Instruction::UDiv:
00766     case Instruction::SDiv:
00767     case Instruction::URem:
00768     case Instruction::SRem:
00769     case Instruction::And:
00770     case Instruction::Or:
00771     case Instruction::Xor: {
00772       GenericValue LHS = getConstantValue(Op0);
00773       GenericValue RHS = getConstantValue(CE->getOperand(1));
00774       GenericValue GV;
00775       switch (CE->getOperand(0)->getType()->getTypeID()) {
00776       default: llvm_unreachable("Bad add type!");
00777       case Type::IntegerTyID:
00778         switch (CE->getOpcode()) {
00779           default: llvm_unreachable("Invalid integer opcode");
00780           case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
00781           case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
00782           case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
00783           case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
00784           case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
00785           case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
00786           case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
00787           case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
00788           case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
00789           case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
00790         }
00791         break;
00792       case Type::FloatTyID:
00793         switch (CE->getOpcode()) {
00794           default: llvm_unreachable("Invalid float opcode");
00795           case Instruction::FAdd:
00796             GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
00797           case Instruction::FSub:
00798             GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
00799           case Instruction::FMul:
00800             GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
00801           case Instruction::FDiv:
00802             GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
00803           case Instruction::FRem:
00804             GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
00805         }
00806         break;
00807       case Type::DoubleTyID:
00808         switch (CE->getOpcode()) {
00809           default: llvm_unreachable("Invalid double opcode");
00810           case Instruction::FAdd:
00811             GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
00812           case Instruction::FSub:
00813             GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
00814           case Instruction::FMul:
00815             GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
00816           case Instruction::FDiv:
00817             GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
00818           case Instruction::FRem:
00819             GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
00820         }
00821         break;
00822       case Type::X86_FP80TyID:
00823       case Type::PPC_FP128TyID:
00824       case Type::FP128TyID: {
00825         const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
00826         APFloat apfLHS = APFloat(Sem, LHS.IntVal);
00827         switch (CE->getOpcode()) {
00828           default: llvm_unreachable("Invalid long double opcode");
00829           case Instruction::FAdd:
00830             apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
00831             GV.IntVal = apfLHS.bitcastToAPInt();
00832             break;
00833           case Instruction::FSub:
00834             apfLHS.subtract(APFloat(Sem, RHS.IntVal),
00835                             APFloat::rmNearestTiesToEven);
00836             GV.IntVal = apfLHS.bitcastToAPInt();
00837             break;
00838           case Instruction::FMul:
00839             apfLHS.multiply(APFloat(Sem, RHS.IntVal),
00840                             APFloat::rmNearestTiesToEven);
00841             GV.IntVal = apfLHS.bitcastToAPInt();
00842             break;
00843           case Instruction::FDiv:
00844             apfLHS.divide(APFloat(Sem, RHS.IntVal),
00845                           APFloat::rmNearestTiesToEven);
00846             GV.IntVal = apfLHS.bitcastToAPInt();
00847             break;
00848           case Instruction::FRem:
00849             apfLHS.mod(APFloat(Sem, RHS.IntVal),
00850                        APFloat::rmNearestTiesToEven);
00851             GV.IntVal = apfLHS.bitcastToAPInt();
00852             break;
00853           }
00854         }
00855         break;
00856       }
00857       return GV;
00858     }
00859     default:
00860       break;
00861     }
00862 
00863     SmallString<256> Msg;
00864     raw_svector_ostream OS(Msg);
00865     OS << "ConstantExpr not handled: " << *CE;
00866     report_fatal_error(OS.str());
00867   }
00868 
00869   // Otherwise, we have a simple constant.
00870   GenericValue Result;
00871   switch (C->getType()->getTypeID()) {
00872   case Type::FloatTyID:
00873     Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
00874     break;
00875   case Type::DoubleTyID:
00876     Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
00877     break;
00878   case Type::X86_FP80TyID:
00879   case Type::FP128TyID:
00880   case Type::PPC_FP128TyID:
00881     Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
00882     break;
00883   case Type::IntegerTyID:
00884     Result.IntVal = cast<ConstantInt>(C)->getValue();
00885     break;
00886   case Type::PointerTyID:
00887     if (isa<ConstantPointerNull>(C))
00888       Result.PointerVal = nullptr;
00889     else if (const Function *F = dyn_cast<Function>(C))
00890       Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
00891     else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
00892       Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
00893     else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
00894       Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
00895                                                         BA->getBasicBlock())));
00896     else
00897       llvm_unreachable("Unknown constant pointer type!");
00898     break;
00899   case Type::VectorTyID: {
00900     unsigned elemNum;
00901     Type* ElemTy;
00902     const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
00903     const ConstantVector *CV = dyn_cast<ConstantVector>(C);
00904     const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
00905 
00906     if (CDV) {
00907         elemNum = CDV->getNumElements();
00908         ElemTy = CDV->getElementType();
00909     } else if (CV || CAZ) {
00910         VectorType* VTy = dyn_cast<VectorType>(C->getType());
00911         elemNum = VTy->getNumElements();
00912         ElemTy = VTy->getElementType();
00913     } else {
00914         llvm_unreachable("Unknown constant vector type!");
00915     }
00916 
00917     Result.AggregateVal.resize(elemNum);
00918     // Check if vector holds floats.
00919     if(ElemTy->isFloatTy()) {
00920       if (CAZ) {
00921         GenericValue floatZero;
00922         floatZero.FloatVal = 0.f;
00923         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
00924                   floatZero);
00925         break;
00926       }
00927       if(CV) {
00928         for (unsigned i = 0; i < elemNum; ++i)
00929           if (!isa<UndefValue>(CV->getOperand(i)))
00930             Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
00931               CV->getOperand(i))->getValueAPF().convertToFloat();
00932         break;
00933       }
00934       if(CDV)
00935         for (unsigned i = 0; i < elemNum; ++i)
00936           Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
00937 
00938       break;
00939     }
00940     // Check if vector holds doubles.
00941     if (ElemTy->isDoubleTy()) {
00942       if (CAZ) {
00943         GenericValue doubleZero;
00944         doubleZero.DoubleVal = 0.0;
00945         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
00946                   doubleZero);
00947         break;
00948       }
00949       if(CV) {
00950         for (unsigned i = 0; i < elemNum; ++i)
00951           if (!isa<UndefValue>(CV->getOperand(i)))
00952             Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
00953               CV->getOperand(i))->getValueAPF().convertToDouble();
00954         break;
00955       }
00956       if(CDV)
00957         for (unsigned i = 0; i < elemNum; ++i)
00958           Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
00959 
00960       break;
00961     }
00962     // Check if vector holds integers.
00963     if (ElemTy->isIntegerTy()) {
00964       if (CAZ) {
00965         GenericValue intZero;     
00966         intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
00967         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
00968                   intZero);
00969         break;
00970       }
00971       if(CV) {
00972         for (unsigned i = 0; i < elemNum; ++i)
00973           if (!isa<UndefValue>(CV->getOperand(i)))
00974             Result.AggregateVal[i].IntVal = cast<ConstantInt>(
00975                                             CV->getOperand(i))->getValue();
00976           else {
00977             Result.AggregateVal[i].IntVal =
00978               APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
00979           }
00980         break;
00981       }
00982       if(CDV)
00983         for (unsigned i = 0; i < elemNum; ++i)
00984           Result.AggregateVal[i].IntVal = APInt(
00985             CDV->getElementType()->getPrimitiveSizeInBits(),
00986             CDV->getElementAsInteger(i));
00987 
00988       break;
00989     }
00990     llvm_unreachable("Unknown constant pointer type!");
00991   }
00992   break;
00993 
00994   default:
00995     SmallString<256> Msg;
00996     raw_svector_ostream OS(Msg);
00997     OS << "ERROR: Constant unimplemented for type: " << *C->getType();
00998     report_fatal_error(OS.str());
00999   }
01000 
01001   return Result;
01002 }
01003 
01004 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
01005 /// with the integer held in IntVal.
01006 static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
01007                              unsigned StoreBytes) {
01008   assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
01009   const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
01010 
01011   if (sys::IsLittleEndianHost) {
01012     // Little-endian host - the source is ordered from LSB to MSB.  Order the
01013     // destination from LSB to MSB: Do a straight copy.
01014     memcpy(Dst, Src, StoreBytes);
01015   } else {
01016     // Big-endian host - the source is an array of 64 bit words ordered from
01017     // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
01018     // from MSB to LSB: Reverse the word order, but not the bytes in a word.
01019     while (StoreBytes > sizeof(uint64_t)) {
01020       StoreBytes -= sizeof(uint64_t);
01021       // May not be aligned so use memcpy.
01022       memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
01023       Src += sizeof(uint64_t);
01024     }
01025 
01026     memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
01027   }
01028 }
01029 
01030 void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
01031                                          GenericValue *Ptr, Type *Ty) {
01032   const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
01033 
01034   switch (Ty->getTypeID()) {
01035   default:
01036     dbgs() << "Cannot store value of type " << *Ty << "!\n";
01037     break;
01038   case Type::IntegerTyID:
01039     StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
01040     break;
01041   case Type::FloatTyID:
01042     *((float*)Ptr) = Val.FloatVal;
01043     break;
01044   case Type::DoubleTyID:
01045     *((double*)Ptr) = Val.DoubleVal;
01046     break;
01047   case Type::X86_FP80TyID:
01048     memcpy(Ptr, Val.IntVal.getRawData(), 10);
01049     break;
01050   case Type::PointerTyID:
01051     // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
01052     if (StoreBytes != sizeof(PointerTy))
01053       memset(&(Ptr->PointerVal), 0, StoreBytes);
01054 
01055     *((PointerTy*)Ptr) = Val.PointerVal;
01056     break;
01057   case Type::VectorTyID:
01058     for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
01059       if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
01060         *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
01061       if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
01062         *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
01063       if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
01064         unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
01065         StoreIntToMemory(Val.AggregateVal[i].IntVal, 
01066           (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
01067       }
01068     }
01069     break;
01070   }
01071 
01072   if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
01073     // Host and target are different endian - reverse the stored bytes.
01074     std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
01075 }
01076 
01077 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
01078 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
01079 static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
01080   assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
01081   uint8_t *Dst = reinterpret_cast<uint8_t *>(
01082                    const_cast<uint64_t *>(IntVal.getRawData()));
01083 
01084   if (sys::IsLittleEndianHost)
01085     // Little-endian host - the destination must be ordered from LSB to MSB.
01086     // The source is ordered from LSB to MSB: Do a straight copy.
01087     memcpy(Dst, Src, LoadBytes);
01088   else {
01089     // Big-endian - the destination is an array of 64 bit words ordered from
01090     // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
01091     // ordered from MSB to LSB: Reverse the word order, but not the bytes in
01092     // a word.
01093     while (LoadBytes > sizeof(uint64_t)) {
01094       LoadBytes -= sizeof(uint64_t);
01095       // May not be aligned so use memcpy.
01096       memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
01097       Dst += sizeof(uint64_t);
01098     }
01099 
01100     memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
01101   }
01102 }
01103 
01104 /// FIXME: document
01105 ///
01106 void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
01107                                           GenericValue *Ptr,
01108                                           Type *Ty) {
01109   const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
01110 
01111   switch (Ty->getTypeID()) {
01112   case Type::IntegerTyID:
01113     // An APInt with all words initially zero.
01114     Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
01115     LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
01116     break;
01117   case Type::FloatTyID:
01118     Result.FloatVal = *((float*)Ptr);
01119     break;
01120   case Type::DoubleTyID:
01121     Result.DoubleVal = *((double*)Ptr);
01122     break;
01123   case Type::PointerTyID:
01124     Result.PointerVal = *((PointerTy*)Ptr);
01125     break;
01126   case Type::X86_FP80TyID: {
01127     // This is endian dependent, but it will only work on x86 anyway.
01128     // FIXME: Will not trap if loading a signaling NaN.
01129     uint64_t y[2];
01130     memcpy(y, Ptr, 10);
01131     Result.IntVal = APInt(80, y);
01132     break;
01133   }
01134   case Type::VectorTyID: {
01135     const VectorType *VT = cast<VectorType>(Ty);
01136     const Type *ElemT = VT->getElementType();
01137     const unsigned numElems = VT->getNumElements();
01138     if (ElemT->isFloatTy()) {
01139       Result.AggregateVal.resize(numElems);
01140       for (unsigned i = 0; i < numElems; ++i)
01141         Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
01142     }
01143     if (ElemT->isDoubleTy()) {
01144       Result.AggregateVal.resize(numElems);
01145       for (unsigned i = 0; i < numElems; ++i)
01146         Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
01147     }
01148     if (ElemT->isIntegerTy()) {
01149       GenericValue intZero;
01150       const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
01151       intZero.IntVal = APInt(elemBitWidth, 0);
01152       Result.AggregateVal.resize(numElems, intZero);
01153       for (unsigned i = 0; i < numElems; ++i)
01154         LoadIntFromMemory(Result.AggregateVal[i].IntVal,
01155           (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
01156     }
01157   break;
01158   }
01159   default:
01160     SmallString<256> Msg;
01161     raw_svector_ostream OS(Msg);
01162     OS << "Cannot load value of type " << *Ty << "!";
01163     report_fatal_error(OS.str());
01164   }
01165 }
01166 
01167 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
01168   DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
01169   DEBUG(Init->dump());
01170   if (isa<UndefValue>(Init))
01171     return;
01172   
01173   if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
01174     unsigned ElementSize =
01175       getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
01176     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
01177       InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
01178     return;
01179   }
01180   
01181   if (isa<ConstantAggregateZero>(Init)) {
01182     memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
01183     return;
01184   }
01185   
01186   if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
01187     unsigned ElementSize =
01188       getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
01189     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
01190       InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
01191     return;
01192   }
01193   
01194   if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
01195     const StructLayout *SL =
01196       getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
01197     for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
01198       InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
01199     return;
01200   }
01201 
01202   if (const ConstantDataSequential *CDS =
01203                dyn_cast<ConstantDataSequential>(Init)) {
01204     // CDS is already laid out in host memory order.
01205     StringRef Data = CDS->getRawDataValues();
01206     memcpy(Addr, Data.data(), Data.size());
01207     return;
01208   }
01209 
01210   if (Init->getType()->isFirstClassType()) {
01211     GenericValue Val = getConstantValue(Init);
01212     StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
01213     return;
01214   }
01215 
01216   DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
01217   llvm_unreachable("Unknown constant type to initialize memory with!");
01218 }
01219 
01220 /// EmitGlobals - Emit all of the global variables to memory, storing their
01221 /// addresses into GlobalAddress.  This must make sure to copy the contents of
01222 /// their initializers into the memory.
01223 void ExecutionEngine::emitGlobals() {
01224   // Loop over all of the global variables in the program, allocating the memory
01225   // to hold them.  If there is more than one module, do a prepass over globals
01226   // to figure out how the different modules should link together.
01227   std::map<std::pair<std::string, Type*>,
01228            const GlobalValue*> LinkedGlobalsMap;
01229 
01230   if (Modules.size() != 1) {
01231     for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
01232       Module &M = *Modules[m];
01233       for (const auto &GV : M.globals()) {
01234         if (GV.hasLocalLinkage() || GV.isDeclaration() ||
01235             GV.hasAppendingLinkage() || !GV.hasName())
01236           continue;// Ignore external globals and globals with internal linkage.
01237 
01238         const GlobalValue *&GVEntry =
01239           LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
01240 
01241         // If this is the first time we've seen this global, it is the canonical
01242         // version.
01243         if (!GVEntry) {
01244           GVEntry = &GV;
01245           continue;
01246         }
01247 
01248         // If the existing global is strong, never replace it.
01249         if (GVEntry->hasExternalLinkage())
01250           continue;
01251 
01252         // Otherwise, we know it's linkonce/weak, replace it if this is a strong
01253         // symbol.  FIXME is this right for common?
01254         if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
01255           GVEntry = &GV;
01256       }
01257     }
01258   }
01259 
01260   std::vector<const GlobalValue*> NonCanonicalGlobals;
01261   for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
01262     Module &M = *Modules[m];
01263     for (const auto &GV : M.globals()) {
01264       // In the multi-module case, see what this global maps to.
01265       if (!LinkedGlobalsMap.empty()) {
01266         if (const GlobalValue *GVEntry =
01267               LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
01268           // If something else is the canonical global, ignore this one.
01269           if (GVEntry != &GV) {
01270             NonCanonicalGlobals.push_back(&GV);
01271             continue;
01272           }
01273         }
01274       }
01275 
01276       if (!GV.isDeclaration()) {
01277         addGlobalMapping(&GV, getMemoryForGV(&GV));
01278       } else {
01279         // External variable reference. Try to use the dynamic loader to
01280         // get a pointer to it.
01281         if (void *SymAddr =
01282             sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
01283           addGlobalMapping(&GV, SymAddr);
01284         else {
01285           report_fatal_error("Could not resolve external global address: "
01286                             +GV.getName());
01287         }
01288       }
01289     }
01290 
01291     // If there are multiple modules, map the non-canonical globals to their
01292     // canonical location.
01293     if (!NonCanonicalGlobals.empty()) {
01294       for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
01295         const GlobalValue *GV = NonCanonicalGlobals[i];
01296         const GlobalValue *CGV =
01297           LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
01298         void *Ptr = getPointerToGlobalIfAvailable(CGV);
01299         assert(Ptr && "Canonical global wasn't codegen'd!");
01300         addGlobalMapping(GV, Ptr);
01301       }
01302     }
01303 
01304     // Now that all of the globals are set up in memory, loop through them all
01305     // and initialize their contents.
01306     for (const auto &GV : M.globals()) {
01307       if (!GV.isDeclaration()) {
01308         if (!LinkedGlobalsMap.empty()) {
01309           if (const GlobalValue *GVEntry =
01310                 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
01311             if (GVEntry != &GV)  // Not the canonical variable.
01312               continue;
01313         }
01314         EmitGlobalVariable(&GV);
01315       }
01316     }
01317   }
01318 }
01319 
01320 // EmitGlobalVariable - This method emits the specified global variable to the
01321 // address specified in GlobalAddresses, or allocates new memory if it's not
01322 // already in the map.
01323 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
01324   void *GA = getPointerToGlobalIfAvailable(GV);
01325 
01326   if (!GA) {
01327     // If it's not already specified, allocate memory for the global.
01328     GA = getMemoryForGV(GV);
01329 
01330     // If we failed to allocate memory for this global, return.
01331     if (!GA) return;
01332 
01333     addGlobalMapping(GV, GA);
01334   }
01335 
01336   // Don't initialize if it's thread local, let the client do it.
01337   if (!GV->isThreadLocal())
01338     InitializeMemory(GV->getInitializer(), GA);
01339 
01340   Type *ElTy = GV->getType()->getElementType();
01341   size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
01342   NumInitBytes += (unsigned)GVSize;
01343   ++NumGlobals;
01344 }
01345 
01346 ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
01347   : EE(EE), GlobalAddressMap(this) {
01348 }
01349 
01350 sys::Mutex *
01351 ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
01352   return &EES->EE.lock;
01353 }
01354 
01355 void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
01356                                                       const GlobalValue *Old) {
01357   void *OldVal = EES->GlobalAddressMap.lookup(Old);
01358   EES->GlobalAddressReverseMap.erase(OldVal);
01359 }
01360 
01361 void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
01362                                                     const GlobalValue *,
01363                                                     const GlobalValue *) {
01364   llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
01365                    " RAUW on a value it has a global mapping for.");
01366 }