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