LCOV - code coverage report
Current view: top level - lib/ExecutionEngine - ExecutionEngine.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 335 604 55.5 %
Date: 2018-02-22 04:41:24 Functions: 35 46 76.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file defines the common interface used by the various execution engine
      11             : // subclasses.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/ExecutionEngine/ExecutionEngine.h"
      16             : #include "llvm/ADT/STLExtras.h"
      17             : #include "llvm/ADT/SmallString.h"
      18             : #include "llvm/ADT/Statistic.h"
      19             : #include "llvm/ExecutionEngine/GenericValue.h"
      20             : #include "llvm/ExecutionEngine/JITEventListener.h"
      21             : #include "llvm/ExecutionEngine/ObjectCache.h"
      22             : #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
      23             : #include "llvm/IR/Constants.h"
      24             : #include "llvm/IR/DataLayout.h"
      25             : #include "llvm/IR/DerivedTypes.h"
      26             : #include "llvm/IR/Mangler.h"
      27             : #include "llvm/IR/Module.h"
      28             : #include "llvm/IR/Operator.h"
      29             : #include "llvm/IR/ValueHandle.h"
      30             : #include "llvm/Object/Archive.h"
      31             : #include "llvm/Object/ObjectFile.h"
      32             : #include "llvm/Support/Debug.h"
      33             : #include "llvm/Support/DynamicLibrary.h"
      34             : #include "llvm/Support/ErrorHandling.h"
      35             : #include "llvm/Support/Host.h"
      36             : #include "llvm/Support/MutexGuard.h"
      37             : #include "llvm/Support/TargetRegistry.h"
      38             : #include "llvm/Support/raw_ostream.h"
      39             : #include "llvm/Target/TargetMachine.h"
      40             : #include <cmath>
      41             : #include <cstring>
      42             : using namespace llvm;
      43             : 
      44             : #define DEBUG_TYPE "jit"
      45             : 
      46             : STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
      47             : STATISTIC(NumGlobals  , "Number of global vars initialized");
      48             : 
      49             : ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
      50             :     std::unique_ptr<Module> M, std::string *ErrorStr,
      51             :     std::shared_ptr<MCJITMemoryManager> MemMgr,
      52             :     std::shared_ptr<LegacyJITSymbolResolver> Resolver,
      53             :     std::unique_ptr<TargetMachine> TM) = nullptr;
      54             : 
      55             : ExecutionEngine *(*ExecutionEngine::OrcMCJITReplacementCtor)(
      56             :     std::string *ErrorStr, std::shared_ptr<MCJITMemoryManager> MemMgr,
      57             :     std::shared_ptr<LegacyJITSymbolResolver> Resolver,
      58             :     std::unique_ptr<TargetMachine> TM) = nullptr;
      59             : 
      60             : ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
      61             :                                                 std::string *ErrorStr) =nullptr;
      62             : 
      63           0 : void JITEventListener::anchor() {}
      64             : 
      65           0 : void ObjectCache::anchor() {}
      66             : 
      67         144 : void ExecutionEngine::Init(std::unique_ptr<Module> M) {
      68         144 :   CompilingLazily         = false;
      69         144 :   GVCompilationDisabled   = false;
      70         144 :   SymbolSearchingDisabled = false;
      71             : 
      72             :   // IR module verification is enabled by default in debug builds, and disabled
      73             :   // by default in release builds.
      74             : #ifndef NDEBUG
      75             :   VerifyModules = true;
      76             : #else
      77         144 :   VerifyModules = false;
      78             : #endif
      79             : 
      80             :   assert(M && "Module is null?");
      81         144 :   Modules.push_back(std::move(M));
      82         144 : }
      83             : 
      84          23 : ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
      85          69 :     : DL(M->getDataLayout()), LazyFunctionCreator(nullptr) {
      86          23 :   Init(std::move(M));
      87          23 : }
      88             : 
      89         121 : ExecutionEngine::ExecutionEngine(DataLayout DL, std::unique_ptr<Module> M)
      90         242 :     : DL(std::move(DL)), LazyFunctionCreator(nullptr) {
      91         121 :   Init(std::move(M));
      92         121 : }
      93             : 
      94         195 : ExecutionEngine::~ExecutionEngine() {
      95          65 :   clearAllGlobalMappings();
      96          65 : }
      97             : 
      98             : namespace {
      99             : /// \brief Helper class which uses a value handler to automatically deletes the
     100             : /// memory block when the GlobalVariable is destroyed.
     101           1 : class GVMemoryBlock final : public CallbackVH {
     102             :   GVMemoryBlock(const GlobalVariable *GV)
     103          13 :     : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
     104             : 
     105             : public:
     106             :   /// \brief Returns the address the GlobalVariable should be written into.  The
     107             :   /// GVMemoryBlock object prefixes that.
     108          13 :   static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
     109          13 :     Type *ElTy = GV->getValueType();
     110          13 :     size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
     111          13 :     void *RawMemory = ::operator new(
     112          26 :         alignTo(sizeof(GVMemoryBlock), TD.getPreferredAlignment(GV)) + GVSize);
     113             :     new(RawMemory) GVMemoryBlock(GV);
     114          13 :     return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
     115             :   }
     116             : 
     117           1 :   void deleted() override {
     118             :     // We allocated with operator new and with some extra memory hanging off the
     119             :     // end, so don't just delete this.  I'm not sure if this is actually
     120             :     // required.
     121             :     this->~GVMemoryBlock();
     122           1 :     ::operator delete(this);
     123           1 :   }
     124             : };
     125             : }  // anonymous namespace
     126             : 
     127          13 : char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
     128          13 :   return GVMemoryBlock::Create(GV, getDataLayout());
     129             : }
     130             : 
     131           0 : void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
     132           0 :   llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
     133             : }
     134             : 
     135             : void
     136           0 : ExecutionEngine::addObjectFile(object::OwningBinary<object::ObjectFile> O) {
     137           0 :   llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
     138             : }
     139             : 
     140           0 : void ExecutionEngine::addArchive(object::OwningBinary<object::Archive> A) {
     141           0 :   llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
     142             : }
     143             : 
     144           0 : bool ExecutionEngine::removeModule(Module *M) {
     145           0 :   for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
     146             :     Module *Found = I->get();
     147             :     if (Found == M) {
     148             :       I->release();
     149           0 :       Modules.erase(I);
     150           0 :       clearGlobalMappingsFromModule(M);
     151           0 :       return true;
     152             :     }
     153             :   }
     154             :   return false;
     155             : }
     156             : 
     157           0 : Function *ExecutionEngine::FindFunctionNamed(StringRef FnName) {
     158           0 :   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
     159           0 :     Function *F = Modules[i]->getFunction(FnName);
     160           0 :     if (F && !F->isDeclaration())
     161             :       return F;
     162             :   }
     163             :   return nullptr;
     164             : }
     165             : 
     166           0 : GlobalVariable *ExecutionEngine::FindGlobalVariableNamed(StringRef Name, bool AllowInternal) {
     167           0 :   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
     168           0 :     GlobalVariable *GV = Modules[i]->getGlobalVariable(Name,AllowInternal);
     169           0 :     if (GV && !GV->isDeclaration())
     170             :       return GV;
     171             :   }
     172             :   return nullptr;
     173             : }
     174             : 
     175           3 : uint64_t ExecutionEngineState::RemoveMapping(StringRef Name) {
     176           3 :   GlobalAddressMapTy::iterator I = GlobalAddressMap.find(Name);
     177             :   uint64_t OldVal;
     178             : 
     179             :   // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
     180             :   // GlobalAddressMap.
     181           6 :   if (I == GlobalAddressMap.end())
     182             :     OldVal = 0;
     183             :   else {
     184           3 :     GlobalAddressReverseMap.erase(I->second);
     185           3 :     OldVal = I->second;
     186             :     GlobalAddressMap.erase(I);
     187             :   }
     188             : 
     189           3 :   return OldVal;
     190             : }
     191             : 
     192         138 : std::string ExecutionEngine::getMangledName(const GlobalValue *GV) {
     193             :   assert(GV->hasName() && "Global must have name.");
     194             : 
     195             :   MutexGuard locked(lock);
     196             :   SmallString<128> FullName;
     197             : 
     198             :   const DataLayout &DL =
     199         138 :     GV->getParent()->getDataLayout().isDefault()
     200         233 :       ? getDataLayout()
     201          95 :       : GV->getParent()->getDataLayout();
     202             : 
     203         276 :   Mangler::getNameWithPrefix(FullName, GV->getName(), DL);
     204         138 :   return FullName.str();
     205             : }
     206             : 
     207          19 : void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
     208             :   MutexGuard locked(lock);
     209          57 :   addGlobalMapping(getMangledName(GV), (uint64_t) Addr);
     210          19 : }
     211             : 
     212          19 : void ExecutionEngine::addGlobalMapping(StringRef Name, uint64_t Addr) {
     213             :   MutexGuard locked(lock);
     214             : 
     215             :   assert(!Name.empty() && "Empty GlobalMapping symbol name!");
     216             : 
     217             :   DEBUG(dbgs() << "JIT: Map \'" << Name  << "\' to [" << Addr << "]\n";);
     218             :   uint64_t &CurVal = EEState.getGlobalAddressMap()[Name];
     219             :   assert((!CurVal || !Addr) && "GlobalMapping already established!");
     220          19 :   CurVal = Addr;
     221             : 
     222             :   // If we are using the reverse mapping, add it too.
     223          19 :   if (!EEState.getGlobalAddressReverseMap().empty()) {
     224           0 :     std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
     225             :     assert((!V.empty() || !Name.empty()) &&
     226             :            "GlobalMapping already established!");
     227           0 :     V = Name;
     228             :   }
     229          19 : }
     230             : 
     231          65 : void ExecutionEngine::clearAllGlobalMappings() {
     232             :   MutexGuard locked(lock);
     233             : 
     234          65 :   EEState.getGlobalAddressMap().clear();
     235             :   EEState.getGlobalAddressReverseMap().clear();
     236          65 : }
     237             : 
     238           1 : void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
     239             :   MutexGuard locked(lock);
     240             : 
     241             :   for (GlobalObject &GO : M->global_objects())
     242           3 :     EEState.RemoveMapping(getMangledName(&GO));
     243           1 : }
     244             : 
     245          80 : uint64_t ExecutionEngine::updateGlobalMapping(const GlobalValue *GV,
     246             :                                               void *Addr) {
     247             :   MutexGuard locked(lock);
     248         320 :   return updateGlobalMapping(getMangledName(GV), (uint64_t) Addr);
     249             : }
     250             : 
     251          80 : uint64_t ExecutionEngine::updateGlobalMapping(StringRef Name, uint64_t Addr) {
     252             :   MutexGuard locked(lock);
     253             : 
     254             :   ExecutionEngineState::GlobalAddressMapTy &Map =
     255             :     EEState.getGlobalAddressMap();
     256             : 
     257             :   // Deleting from the mapping?
     258          80 :   if (!Addr)
     259           2 :     return EEState.RemoveMapping(Name);
     260             : 
     261             :   uint64_t &CurVal = Map[Name];
     262          78 :   uint64_t OldVal = CurVal;
     263             : 
     264          82 :   if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
     265             :     EEState.getGlobalAddressReverseMap().erase(CurVal);
     266          78 :   CurVal = Addr;
     267             : 
     268             :   // If we are using the reverse mapping, add it too.
     269          78 :   if (!EEState.getGlobalAddressReverseMap().empty()) {
     270           1 :     std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
     271             :     assert((!V.empty() || !Name.empty()) &&
     272             :            "GlobalMapping already established!");
     273           2 :     V = Name;
     274             :   }
     275             :   return OldVal;
     276             : }
     277             : 
     278         273 : uint64_t ExecutionEngine::getAddressToGlobalIfAvailable(StringRef S) {
     279             :   MutexGuard locked(lock);
     280             :   uint64_t Address = 0;
     281             :   ExecutionEngineState::GlobalAddressMapTy::iterator I =
     282         273 :     EEState.getGlobalAddressMap().find(S);
     283         546 :   if (I != EEState.getGlobalAddressMap().end())
     284          37 :     Address = I->second;
     285         273 :   return Address;
     286             : }
     287             : 
     288             : 
     289         273 : void *ExecutionEngine::getPointerToGlobalIfAvailable(StringRef S) {
     290             :   MutexGuard locked(lock);
     291         273 :   if (void* Address = (void *) getAddressToGlobalIfAvailable(S))
     292             :     return Address;
     293         236 :   return nullptr;
     294             : }
     295             : 
     296          38 : void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
     297             :   MutexGuard locked(lock);
     298         152 :   return getPointerToGlobalIfAvailable(getMangledName(GV));
     299             : }
     300             : 
     301          14 : const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
     302             :   MutexGuard locked(lock);
     303             : 
     304             :   // If we haven't computed the reverse mapping yet, do so first.
     305          14 :   if (EEState.getGlobalAddressReverseMap().empty()) {
     306             :     for (ExecutionEngineState::GlobalAddressMapTy::iterator
     307           7 :            I = EEState.getGlobalAddressMap().begin(),
     308          13 :            E = EEState.getGlobalAddressMap().end(); I != E; ++I) {
     309             :       StringRef Name = I->first();
     310           6 :       uint64_t Addr = I->second;
     311           6 :       EEState.getGlobalAddressReverseMap().insert(std::make_pair(
     312             :                                                           Addr, Name));
     313             :     }
     314             :   }
     315             : 
     316             :   std::map<uint64_t, std::string>::iterator I =
     317          14 :     EEState.getGlobalAddressReverseMap().find((uint64_t) Addr);
     318             : 
     319          14 :   if (I != EEState.getGlobalAddressReverseMap().end()) {
     320             :     StringRef Name = I->second;
     321          11 :     for (unsigned i = 0, e = Modules.size(); i != e; ++i)
     322          20 :       if (GlobalValue *GV = Modules[i]->getNamedValue(Name))
     323           9 :         return GV;
     324             :   }
     325             :   return nullptr;
     326             : }
     327             : 
     328             : namespace {
     329         608 : class ArgvArray {
     330             :   std::unique_ptr<char[]> Array;
     331             :   std::vector<std::unique_ptr<char[]>> Values;
     332             : public:
     333             :   /// Turn a vector of strings into a nice argv style array of pointers to null
     334             :   /// terminated strings.
     335             :   void *reset(LLVMContext &C, ExecutionEngine *EE,
     336             :               const std::vector<std::string> &InputArgv);
     337             : };
     338             : }  // anonymous namespace
     339          15 : void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
     340             :                        const std::vector<std::string> &InputArgv) {
     341          15 :   Values.clear();  // Free the old contents.
     342          30 :   Values.reserve(InputArgv.size());
     343          15 :   unsigned PtrSize = EE->getDataLayout().getPointerSize();
     344          30 :   Array = make_unique<char[]>((InputArgv.size()+1)*PtrSize);
     345             : 
     346             :   DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array.get() << "\n");
     347          15 :   Type *SBytePtr = Type::getInt8PtrTy(C);
     348             : 
     349         165 :   for (unsigned i = 0; i != InputArgv.size(); ++i) {
     350          45 :     unsigned Size = InputArgv[i].size()+1;
     351          45 :     auto Dest = make_unique<char[]>(Size);
     352             :     DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest.get() << "\n");
     353             : 
     354          45 :     std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
     355          45 :     Dest[Size-1] = 0;
     356             : 
     357             :     // Endian safe: Array[i] = (PointerTy)Dest;
     358          45 :     EE->StoreValueToMemory(PTOGV(Dest.get()),
     359          45 :                            (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
     360             :     Values.push_back(std::move(Dest));
     361             :   }
     362             : 
     363             :   // Null terminate it
     364          15 :   EE->StoreValueToMemory(PTOGV(nullptr),
     365          15 :                          (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
     366             :                          SBytePtr);
     367          15 :   return Array.get();
     368             : }
     369             : 
     370         346 : void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
     371             :                                                        bool isDtors) {
     372         346 :   StringRef Name(isDtors ? "llvm.global_dtors" : "llvm.global_ctors");
     373             :   GlobalVariable *GV = module.getNamedGlobal(Name);
     374             : 
     375             :   // If this global has internal linkage, or if it has a use, then it must be
     376             :   // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
     377             :   // this is the case, don't execute any of the global ctors, __main will do
     378             :   // it.
     379         346 :   if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
     380             : 
     381             :   // Should be an array of '{ i32, void ()* }' structs.  The first value is
     382             :   // the init priority, which we ignore.
     383             :   ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
     384             :   if (!InitList)
     385             :     return;
     386          12 :   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
     387           4 :     ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
     388           0 :     if (!CS) continue;
     389             : 
     390           4 :     Constant *FP = CS->getOperand(1);
     391           4 :     if (FP->isNullValue())
     392           0 :       continue;  // Found a sentinal value, ignore.
     393             : 
     394             :     // Strip off constant expression casts.
     395             :     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
     396           0 :       if (CE->isCast())
     397             :         FP = CE->getOperand(0);
     398             : 
     399             :     // Execute the ctor/dtor function!
     400             :     if (Function *F = dyn_cast<Function>(FP))
     401           8 :       runFunction(F, None);
     402             : 
     403             :     // FIXME: It is marginally lame that we just do nothing here if we see an
     404             :     // entry we don't recognize. It might not be unreasonable for the verifier
     405             :     // to not even allow this and just assert here.
     406             :   }
     407             : }
     408             : 
     409          36 : void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
     410             :   // Execute global ctors/dtors for each module in the program.
     411         108 :   for (std::unique_ptr<Module> &M : Modules)
     412          72 :     runStaticConstructorsDestructors(*M, isDtors);
     413          36 : }
     414             : 
     415             : #ifndef NDEBUG
     416             : /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
     417             : static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
     418             :   unsigned PtrSize = EE->getDataLayout().getPointerSize();
     419             :   for (unsigned i = 0; i < PtrSize; ++i)
     420             :     if (*(i + (uint8_t*)Loc))
     421             :       return false;
     422             :   return true;
     423             : }
     424             : #endif
     425             : 
     426         154 : int ExecutionEngine::runFunctionAsMain(Function *Fn,
     427             :                                        const std::vector<std::string> &argv,
     428             :                                        const char * const * envp) {
     429         152 :   std::vector<GenericValue> GVArgs;
     430         152 :   GenericValue GVArgc;
     431         308 :   GVArgc.IntVal = APInt(32, argv.size());
     432             : 
     433             :   // Check main() type
     434         154 :   unsigned NumArgs = Fn->getFunctionType()->getNumParams();
     435             :   FunctionType *FTy = Fn->getFunctionType();
     436         154 :   Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
     437             : 
     438             :   // Check the argument types.
     439         154 :   if (NumArgs > 3)
     440           0 :     report_fatal_error("Invalid number of arguments of main() supplied");
     441         156 :   if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
     442           0 :     report_fatal_error("Invalid type for third argument of main() supplied");
     443         167 :   if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
     444           0 :     report_fatal_error("Invalid type for second argument of main() supplied");
     445         188 :   if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
     446           0 :     report_fatal_error("Invalid type for first argument of main() supplied");
     447         308 :   if (!FTy->getReturnType()->isIntegerTy() &&
     448             :       !FTy->getReturnType()->isVoidTy())
     449           0 :     report_fatal_error("Invalid return type of main() supplied");
     450             : 
     451         152 :   ArgvArray CArgv;
     452         152 :   ArgvArray CEnv;
     453         154 :   if (NumArgs) {
     454          17 :     GVArgs.push_back(GVArgc); // Arg #0 = argc.
     455          17 :     if (NumArgs > 1) {
     456             :       // Arg #1 = argv.
     457          26 :       GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
     458             :       assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
     459             :              "argv[0] was null after CreateArgv");
     460          13 :       if (NumArgs > 2) {
     461           2 :         std::vector<std::string> EnvVars;
     462          62 :         for (unsigned i = 0; envp[i]; ++i)
     463          30 :           EnvVars.emplace_back(envp[i]);
     464             :         // Arg #2 = envp.
     465           4 :         GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
     466             :       }
     467             :     }
     468             :   }
     469             : 
     470         612 :   return runFunction(Fn, GVArgs).IntVal.getZExtValue();
     471             : }
     472             : 
     473          46 : EngineBuilder::EngineBuilder() : EngineBuilder(nullptr) {}
     474             : 
     475         244 : EngineBuilder::EngineBuilder(std::unique_ptr<Module> M)
     476             :     : M(std::move(M)), WhichEngine(EngineKind::Either), ErrorStr(nullptr),
     477             :       OptLevel(CodeGenOpt::Default), MemMgr(nullptr), Resolver(nullptr),
     478         732 :       UseOrcMCJITReplacement(false) {
     479             : // IR module verification is enabled by default in debug builds, and disabled
     480             : // by default in release builds.
     481             : #ifndef NDEBUG
     482             :   VerifyModules = true;
     483             : #else
     484         244 :   VerifyModules = false;
     485             : #endif
     486         244 : }
     487             : 
     488             : EngineBuilder::~EngineBuilder() = default;
     489             : 
     490         193 : EngineBuilder &EngineBuilder::setMCJITMemoryManager(
     491             :                                    std::unique_ptr<RTDyldMemoryManager> mcjmm) {
     492             :   auto SharedMM = std::shared_ptr<RTDyldMemoryManager>(std::move(mcjmm));
     493             :   MemMgr = SharedMM;
     494             :   Resolver = SharedMM;
     495         193 :   return *this;
     496             : }
     497             : 
     498             : EngineBuilder&
     499           0 : EngineBuilder::setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM) {
     500           0 :   MemMgr = std::shared_ptr<MCJITMemoryManager>(std::move(MM));
     501           0 :   return *this;
     502             : }
     503             : 
     504             : EngineBuilder &
     505           0 : EngineBuilder::setSymbolResolver(std::unique_ptr<LegacyJITSymbolResolver> SR) {
     506           0 :   Resolver = std::shared_ptr<LegacyJITSymbolResolver>(std::move(SR));
     507           0 :   return *this;
     508             : }
     509             : 
     510         221 : ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
     511             :   std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
     512             : 
     513             :   // Make sure we can resolve symbols in the program as well. The zero arg
     514             :   // to the function tells DynamicLibrary to load the program, not a library.
     515         442 :   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
     516             :     return nullptr;
     517             : 
     518             :   // If the user specified a memory manager but didn't specify which engine to
     519             :   // create, we assume they only want the JIT, and we fail if they only want
     520             :   // the interpreter.
     521         221 :   if (MemMgr) {
     522         193 :     if (WhichEngine & EngineKind::JIT)
     523         193 :       WhichEngine = EngineKind::JIT;
     524             :     else {
     525           0 :       if (ErrorStr)
     526             :         *ErrorStr = "Cannot create an interpreter with a memory manager.";
     527             :       return nullptr;
     528             :     }
     529             :   }
     530             : 
     531             :   // Unless the interpreter was explicitly selected or the JIT is not linked,
     532             :   // try making a JIT.
     533         221 :   if ((WhichEngine & EngineKind::JIT) && TheTM) {
     534         198 :     if (!TM->getTarget().hasJIT()) {
     535           0 :       errs() << "WARNING: This target JIT is not designed for the host"
     536           0 :              << " you are running.  If bad things happen, please choose"
     537           0 :              << " a different -march switch.\n";
     538             :     }
     539             : 
     540             :     ExecutionEngine *EE = nullptr;
     541         198 :     if (ExecutionEngine::OrcMCJITReplacementCtor && UseOrcMCJITReplacement) {
     542         308 :       EE = ExecutionEngine::OrcMCJITReplacementCtor(ErrorStr, std::move(MemMgr),
     543             :                                                     std::move(Resolver),
     544             :                                                     std::move(TheTM));
     545         154 :       EE->addModule(std::move(M));
     546         121 :     } else if (ExecutionEngine::MCJITCtor)
     547         605 :       EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MemMgr),
     548             :                                       std::move(Resolver), std::move(TheTM));
     549             : 
     550         198 :     if (EE) {
     551         198 :       EE->setVerifyModules(VerifyModules);
     552         198 :       return EE;
     553             :     }
     554             :   }
     555             : 
     556             :   // If we can't make a JIT and we didn't request one specifically, try making
     557             :   // an interpreter instead.
     558          23 :   if (WhichEngine & EngineKind::Interpreter) {
     559          23 :     if (ExecutionEngine::InterpCtor)
     560          46 :       return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
     561           0 :     if (ErrorStr)
     562             :       *ErrorStr = "Interpreter has not been linked in.";
     563             :     return nullptr;
     564             :   }
     565             : 
     566           0 :   if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::MCJITCtor) {
     567           0 :     if (ErrorStr)
     568             :       *ErrorStr = "JIT has not been linked in.";
     569             :   }
     570             : 
     571             :   return nullptr;
     572             : }
     573             : 
     574          23 : void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
     575             :   if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
     576           6 :     return getPointerToFunction(F);
     577             : 
     578             :   MutexGuard locked(lock);
     579          17 :   if (void* P = getPointerToGlobalIfAvailable(GV))
     580             :     return P;
     581             : 
     582             :   // Global variable might have been added since interpreter started.
     583             :   if (GlobalVariable *GVar =
     584             :           const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
     585           1 :     EmitGlobalVariable(GVar);
     586             :   else
     587           0 :     llvm_unreachable("Global hasn't had an address allocated yet!");
     588             : 
     589           1 :   return getPointerToGlobalIfAvailable(GV);
     590             : }
     591             : 
     592             : /// \brief Converts a Constant* into a GenericValue, including handling of
     593             : /// ConstantExpr values.
     594         708 : GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
     595             :   // If its undefined, return the garbage.
     596         708 :   if (isa<UndefValue>(C)) {
     597          35 :     GenericValue Result;
     598          70 :     switch (C->getType()->getTypeID()) {
     599             :     default:
     600             :       break;
     601           1 :     case Type::IntegerTyID:
     602             :     case Type::X86_FP80TyID:
     603             :     case Type::FP128TyID:
     604             :     case Type::PPC_FP128TyID:
     605             :       // Although the value is undefined, we still have to construct an APInt
     606             :       // with the correct bit width.
     607           2 :       Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
     608           1 :       break;
     609           2 :     case Type::StructTyID: {
     610             :       // if the whole struct is 'undef' just reserve memory for the value.
     611             :       if(StructType *STy = dyn_cast<StructType>(C->getType())) {
     612           2 :         unsigned int elemNum = STy->getNumElements();
     613           2 :         Result.AggregateVal.resize(elemNum);
     614          10 :         for (unsigned int i = 0; i < elemNum; ++i) {
     615           4 :           Type *ElemTy = STy->getElementType(i);
     616           4 :           if (ElemTy->isIntegerTy())
     617           1 :             Result.AggregateVal[i].IntVal =
     618           2 :               APInt(ElemTy->getPrimitiveSizeInBits(), 0);
     619             :           else if (ElemTy->isAggregateType()) {
     620           1 :               const Constant *ElemUndef = UndefValue::get(ElemTy);
     621           2 :               Result.AggregateVal[i] = getConstantValue(ElemUndef);
     622             :             }
     623             :           }
     624             :         }
     625             :       }
     626             :       break;
     627          30 :     case Type::VectorTyID:
     628             :       // if the whole vector is 'undef' just reserve memory for the value.
     629             :       auto* VTy = dyn_cast<VectorType>(C->getType());
     630          30 :       Type *ElemTy = VTy->getElementType();
     631          30 :       unsigned int elemNum = VTy->getNumElements();
     632          30 :       Result.AggregateVal.resize(elemNum);
     633          30 :       if (ElemTy->isIntegerTy())
     634         284 :         for (unsigned int i = 0; i < elemNum; ++i)
     635         132 :           Result.AggregateVal[i].IntVal =
     636         264 :             APInt(ElemTy->getPrimitiveSizeInBits(), 0);
     637             :       break;
     638             :     }
     639             :     return Result;
     640             :   }
     641             : 
     642             :   // Otherwise, if the value is a ConstantExpr...
     643             :   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
     644             :     Constant *Op0 = CE->getOperand(0);
     645           0 :     switch (CE->getOpcode()) {
     646           0 :     case Instruction::GetElementPtr: {
     647             :       // Compute the index
     648           0 :       GenericValue Result = getConstantValue(Op0);
     649           0 :       APInt Offset(DL.getPointerSizeInBits(), 0);
     650           0 :       cast<GEPOperator>(CE)->accumulateConstantOffset(DL, Offset);
     651             : 
     652           0 :       char* tmp = (char*) Result.PointerVal;
     653           0 :       Result = PTOGV(tmp + Offset.getSExtValue());
     654             :       return Result;
     655             :     }
     656           0 :     case Instruction::Trunc: {
     657           0 :       GenericValue GV = getConstantValue(Op0);
     658           0 :       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
     659           0 :       GV.IntVal = GV.IntVal.trunc(BitWidth);
     660             :       return GV;
     661             :     }
     662           0 :     case Instruction::ZExt: {
     663           0 :       GenericValue GV = getConstantValue(Op0);
     664           0 :       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
     665           0 :       GV.IntVal = GV.IntVal.zext(BitWidth);
     666             :       return GV;
     667             :     }
     668           0 :     case Instruction::SExt: {
     669           0 :       GenericValue GV = getConstantValue(Op0);
     670           0 :       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
     671           0 :       GV.IntVal = GV.IntVal.sext(BitWidth);
     672             :       return GV;
     673             :     }
     674           0 :     case Instruction::FPTrunc: {
     675             :       // FIXME long double
     676           0 :       GenericValue GV = getConstantValue(Op0);
     677           0 :       GV.FloatVal = float(GV.DoubleVal);
     678             :       return GV;
     679             :     }
     680           0 :     case Instruction::FPExt:{
     681             :       // FIXME long double
     682           0 :       GenericValue GV = getConstantValue(Op0);
     683           0 :       GV.DoubleVal = double(GV.FloatVal);
     684             :       return GV;
     685             :     }
     686           0 :     case Instruction::UIToFP: {
     687           0 :       GenericValue GV = getConstantValue(Op0);
     688           0 :       if (CE->getType()->isFloatTy())
     689           0 :         GV.FloatVal = float(GV.IntVal.roundToDouble());
     690           0 :       else if (CE->getType()->isDoubleTy())
     691           0 :         GV.DoubleVal = GV.IntVal.roundToDouble();
     692           0 :       else if (CE->getType()->isX86_FP80Ty()) {
     693           0 :         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended());
     694           0 :         (void)apf.convertFromAPInt(GV.IntVal,
     695             :                                    false,
     696             :                                    APFloat::rmNearestTiesToEven);
     697           0 :         GV.IntVal = apf.bitcastToAPInt();
     698             :       }
     699             :       return GV;
     700             :     }
     701           0 :     case Instruction::SIToFP: {
     702           0 :       GenericValue GV = getConstantValue(Op0);
     703           0 :       if (CE->getType()->isFloatTy())
     704           0 :         GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
     705           0 :       else if (CE->getType()->isDoubleTy())
     706           0 :         GV.DoubleVal = GV.IntVal.signedRoundToDouble();
     707           0 :       else if (CE->getType()->isX86_FP80Ty()) {
     708           0 :         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended());
     709           0 :         (void)apf.convertFromAPInt(GV.IntVal,
     710             :                                    true,
     711             :                                    APFloat::rmNearestTiesToEven);
     712           0 :         GV.IntVal = apf.bitcastToAPInt();
     713             :       }
     714             :       return GV;
     715             :     }
     716           0 :     case Instruction::FPToUI: // double->APInt conversion handles sign
     717             :     case Instruction::FPToSI: {
     718           0 :       GenericValue GV = getConstantValue(Op0);
     719           0 :       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
     720           0 :       if (Op0->getType()->isFloatTy())
     721           0 :         GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
     722           0 :       else if (Op0->getType()->isDoubleTy())
     723           0 :         GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
     724           0 :       else if (Op0->getType()->isX86_FP80Ty()) {
     725           0 :         APFloat apf = APFloat(APFloat::x87DoubleExtended(), GV.IntVal);
     726             :         uint64_t v;
     727             :         bool ignored;
     728           0 :         (void)apf.convertToInteger(makeMutableArrayRef(v), BitWidth,
     729             :                                    CE->getOpcode()==Instruction::FPToSI,
     730             :                                    APFloat::rmTowardZero, &ignored);
     731           0 :         GV.IntVal = v; // endian?
     732             :       }
     733             :       return GV;
     734             :     }
     735           0 :     case Instruction::PtrToInt: {
     736           0 :       GenericValue GV = getConstantValue(Op0);
     737           0 :       uint32_t PtrWidth = DL.getTypeSizeInBits(Op0->getType());
     738             :       assert(PtrWidth <= 64 && "Bad pointer width");
     739           0 :       GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
     740           0 :       uint32_t IntWidth = DL.getTypeSizeInBits(CE->getType());
     741           0 :       GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
     742             :       return GV;
     743             :     }
     744           0 :     case Instruction::IntToPtr: {
     745           0 :       GenericValue GV = getConstantValue(Op0);
     746           0 :       uint32_t PtrWidth = DL.getTypeSizeInBits(CE->getType());
     747           0 :       GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
     748             :       assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
     749           0 :       GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
     750             :       return GV;
     751             :     }
     752           0 :     case Instruction::BitCast: {
     753           0 :       GenericValue GV = getConstantValue(Op0);
     754           0 :       Type* DestTy = CE->getType();
     755           0 :       switch (Op0->getType()->getTypeID()) {
     756           0 :         default: llvm_unreachable("Invalid bitcast operand");
     757             :         case Type::IntegerTyID:
     758             :           assert(DestTy->isFloatingPointTy() && "invalid bitcast");
     759           0 :           if (DestTy->isFloatTy())
     760           0 :             GV.FloatVal = GV.IntVal.bitsToFloat();
     761           0 :           else if (DestTy->isDoubleTy())
     762           0 :             GV.DoubleVal = GV.IntVal.bitsToDouble();
     763             :           break;
     764           0 :         case Type::FloatTyID:
     765             :           assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
     766           0 :           GV.IntVal = APInt::floatToBits(GV.FloatVal);
     767           0 :           break;
     768           0 :         case Type::DoubleTyID:
     769             :           assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
     770           0 :           GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
     771           0 :           break;
     772             :         case Type::PointerTyID:
     773             :           assert(DestTy->isPointerTy() && "Invalid bitcast");
     774             :           break; // getConstantValue(Op0)  above already converted it
     775             :       }
     776             :       return GV;
     777             :     }
     778           0 :     case Instruction::Add:
     779             :     case Instruction::FAdd:
     780             :     case Instruction::Sub:
     781             :     case Instruction::FSub:
     782             :     case Instruction::Mul:
     783             :     case Instruction::FMul:
     784             :     case Instruction::UDiv:
     785             :     case Instruction::SDiv:
     786             :     case Instruction::URem:
     787             :     case Instruction::SRem:
     788             :     case Instruction::And:
     789             :     case Instruction::Or:
     790             :     case Instruction::Xor: {
     791           0 :       GenericValue LHS = getConstantValue(Op0);
     792           0 :       GenericValue RHS = getConstantValue(CE->getOperand(1));
     793           0 :       GenericValue GV;
     794           0 :       switch (CE->getOperand(0)->getType()->getTypeID()) {
     795           0 :       default: llvm_unreachable("Bad add type!");
     796             :       case Type::IntegerTyID:
     797           0 :         switch (CE->getOpcode()) {
     798           0 :           default: llvm_unreachable("Invalid integer opcode");
     799           0 :           case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
     800           0 :           case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
     801           0 :           case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
     802           0 :           case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
     803           0 :           case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
     804           0 :           case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
     805           0 :           case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
     806           0 :           case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
     807           0 :           case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
     808           0 :           case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
     809             :         }
     810             :         break;
     811             :       case Type::FloatTyID:
     812           0 :         switch (CE->getOpcode()) {
     813           0 :           default: llvm_unreachable("Invalid float opcode");
     814           0 :           case Instruction::FAdd:
     815           0 :             GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
     816           0 :           case Instruction::FSub:
     817           0 :             GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
     818           0 :           case Instruction::FMul:
     819           0 :             GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
     820           0 :           case Instruction::FDiv:
     821           0 :             GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
     822           0 :           case Instruction::FRem:
     823           0 :             GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
     824             :         }
     825             :         break;
     826             :       case Type::DoubleTyID:
     827           0 :         switch (CE->getOpcode()) {
     828           0 :           default: llvm_unreachable("Invalid double opcode");
     829           0 :           case Instruction::FAdd:
     830           0 :             GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
     831           0 :           case Instruction::FSub:
     832           0 :             GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
     833           0 :           case Instruction::FMul:
     834           0 :             GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
     835           0 :           case Instruction::FDiv:
     836           0 :             GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
     837           0 :           case Instruction::FRem:
     838           0 :             GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
     839             :         }
     840             :         break;
     841             :       case Type::X86_FP80TyID:
     842             :       case Type::PPC_FP128TyID:
     843             :       case Type::FP128TyID: {
     844           0 :         const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
     845             :         APFloat apfLHS = APFloat(Sem, LHS.IntVal);
     846           0 :         switch (CE->getOpcode()) {
     847           0 :           default: llvm_unreachable("Invalid long double opcode");
     848             :           case Instruction::FAdd:
     849           0 :             apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
     850           0 :             GV.IntVal = apfLHS.bitcastToAPInt();
     851           0 :             break;
     852             :           case Instruction::FSub:
     853           0 :             apfLHS.subtract(APFloat(Sem, RHS.IntVal),
     854             :                             APFloat::rmNearestTiesToEven);
     855           0 :             GV.IntVal = apfLHS.bitcastToAPInt();
     856           0 :             break;
     857             :           case Instruction::FMul:
     858           0 :             apfLHS.multiply(APFloat(Sem, RHS.IntVal),
     859             :                             APFloat::rmNearestTiesToEven);
     860           0 :             GV.IntVal = apfLHS.bitcastToAPInt();
     861           0 :             break;
     862             :           case Instruction::FDiv:
     863           0 :             apfLHS.divide(APFloat(Sem, RHS.IntVal),
     864             :                           APFloat::rmNearestTiesToEven);
     865           0 :             GV.IntVal = apfLHS.bitcastToAPInt();
     866           0 :             break;
     867             :           case Instruction::FRem:
     868           0 :             apfLHS.mod(APFloat(Sem, RHS.IntVal));
     869           0 :             GV.IntVal = apfLHS.bitcastToAPInt();
     870           0 :             break;
     871           0 :           }
     872             :         }
     873           0 :         break;
     874             :       }
     875             :       return GV;
     876             :     }
     877             :     default:
     878             :       break;
     879             :     }
     880             : 
     881             :     SmallString<256> Msg;
     882             :     raw_svector_ostream OS(Msg);
     883           0 :     OS << "ConstantExpr not handled: " << *CE;
     884           0 :     report_fatal_error(OS.str());
     885             :   }
     886             : 
     887             :   // Otherwise, we have a simple constant.
     888         673 :   GenericValue Result;
     889        1346 :   switch (C->getType()->getTypeID()) {
     890          19 :   case Type::FloatTyID:
     891          19 :     Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
     892          19 :     break;
     893          26 :   case Type::DoubleTyID:
     894          26 :     Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
     895          26 :     break;
     896           0 :   case Type::X86_FP80TyID:
     897             :   case Type::FP128TyID:
     898             :   case Type::PPC_FP128TyID:
     899           0 :     Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
     900           0 :     break;
     901         191 :   case Type::IntegerTyID:
     902         191 :     Result.IntVal = cast<ConstantInt>(C)->getValue();
     903         191 :     break;
     904          49 :   case Type::PointerTyID:
     905          49 :     if (isa<ConstantPointerNull>(C))
     906           0 :       Result.PointerVal = nullptr;
     907             :     else if (const Function *F = dyn_cast<Function>(C))
     908          66 :       Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
     909             :     else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
     910          32 :       Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
     911             :     else
     912           0 :       llvm_unreachable("Unknown constant pointer type!");
     913             :     break;
     914         388 :   case Type::VectorTyID: {
     915             :     unsigned elemNum;
     916             :     Type* ElemTy;
     917             :     const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
     918             :     const ConstantVector *CV = dyn_cast<ConstantVector>(C);
     919             :     const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
     920             : 
     921         388 :     if (CDV) {
     922         217 :         elemNum = CDV->getNumElements();
     923         217 :         ElemTy = CDV->getElementType();
     924         171 :     } else if (CV || CAZ) {
     925             :         VectorType* VTy = dyn_cast<VectorType>(C->getType());
     926         171 :         elemNum = VTy->getNumElements();
     927         171 :         ElemTy = VTy->getElementType();
     928             :     } else {
     929           0 :         llvm_unreachable("Unknown constant vector type!");
     930             :     }
     931             : 
     932         388 :     Result.AggregateVal.resize(elemNum);
     933             :     // Check if vector holds floats.
     934         388 :     if(ElemTy->isFloatTy()) {
     935          38 :       if (CAZ) {
     936          20 :         GenericValue floatZero;
     937          20 :         floatZero.FloatVal = 0.f;
     938             :         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
     939             :                   floatZero);
     940             :         break;
     941             :       }
     942          18 :       if(CV) {
     943           0 :         for (unsigned i = 0; i < elemNum; ++i)
     944           0 :           if (!isa<UndefValue>(CV->getOperand(i)))
     945           0 :             Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
     946           0 :               CV->getOperand(i))->getValueAPF().convertToFloat();
     947             :         break;
     948             :       }
     949          18 :       if(CDV)
     950         188 :         for (unsigned i = 0; i < elemNum; ++i)
     951         170 :           Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
     952             : 
     953             :       break;
     954             :     }
     955             :     // Check if vector holds doubles.
     956         350 :     if (ElemTy->isDoubleTy()) {
     957          49 :       if (CAZ) {
     958          20 :         GenericValue doubleZero;
     959          20 :         doubleZero.DoubleVal = 0.0;
     960             :         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
     961             :                   doubleZero);
     962             :         break;
     963             :       }
     964          29 :       if(CV) {
     965           0 :         for (unsigned i = 0; i < elemNum; ++i)
     966           0 :           if (!isa<UndefValue>(CV->getOperand(i)))
     967           0 :             Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
     968           0 :               CV->getOperand(i))->getValueAPF().convertToDouble();
     969             :         break;
     970             :       }
     971          29 :       if(CDV)
     972         259 :         for (unsigned i = 0; i < elemNum; ++i)
     973         230 :           Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
     974             : 
     975             :       break;
     976             :     }
     977             :     // Check if vector holds integers.
     978         301 :     if (ElemTy->isIntegerTy()) {
     979         301 :       if (CAZ) {
     980          81 :         GenericValue intZero;
     981         162 :         intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
     982             :         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
     983             :                   intZero);
     984             :         break;
     985             :       }
     986         220 :       if(CV) {
     987         546 :         for (unsigned i = 0; i < elemNum; ++i)
     988         496 :           if (!isa<UndefValue>(CV->getOperand(i)))
     989         496 :             Result.AggregateVal[i].IntVal = cast<ConstantInt>(
     990         248 :                                             CV->getOperand(i))->getValue();
     991             :           else {
     992           0 :             Result.AggregateVal[i].IntVal =
     993           0 :               APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
     994             :           }
     995             :         break;
     996             :       }
     997         170 :       if(CDV)
     998        1642 :         for (unsigned i = 0; i < elemNum; ++i)
     999        2208 :           Result.AggregateVal[i].IntVal = APInt(
    1000             :             CDV->getElementType()->getPrimitiveSizeInBits(),
    1001             :             CDV->getElementAsInteger(i));
    1002             : 
    1003             :       break;
    1004             :     }
    1005           0 :     llvm_unreachable("Unknown constant pointer type!");
    1006             :   }
    1007             :   break;
    1008             : 
    1009             :   default:
    1010             :     SmallString<256> Msg;
    1011             :     raw_svector_ostream OS(Msg);
    1012           0 :     OS << "ERROR: Constant unimplemented for type: " << *C->getType();
    1013           0 :     report_fatal_error(OS.str());
    1014             :   }
    1015             : 
    1016             :   return Result;
    1017             : }
    1018             : 
    1019             : /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
    1020             : /// with the integer held in IntVal.
    1021             : static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
    1022             :                              unsigned StoreBytes) {
    1023             :   assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
    1024             :   const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
    1025             : 
    1026             :   if (sys::IsLittleEndianHost) {
    1027             :     // Little-endian host - the source is ordered from LSB to MSB.  Order the
    1028             :     // destination from LSB to MSB: Do a straight copy.
    1029          26 :     memcpy(Dst, Src, StoreBytes);
    1030             :   } else {
    1031             :     // Big-endian host - the source is an array of 64 bit words ordered from
    1032             :     // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
    1033             :     // from MSB to LSB: Reverse the word order, but not the bytes in a word.
    1034             :     while (StoreBytes > sizeof(uint64_t)) {
    1035             :       StoreBytes -= sizeof(uint64_t);
    1036             :       // May not be aligned so use memcpy.
    1037             :       memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
    1038             :       Src += sizeof(uint64_t);
    1039             :     }
    1040             : 
    1041             :     memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
    1042             :   }
    1043             : }
    1044             : 
    1045          93 : void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
    1046             :                                          GenericValue *Ptr, Type *Ty) {
    1047          93 :   const unsigned StoreBytes = getDataLayout().getTypeStoreSize(Ty);
    1048             : 
    1049          93 :   switch (Ty->getTypeID()) {
    1050           0 :   default:
    1051           0 :     dbgs() << "Cannot store value of type " << *Ty << "!\n";
    1052           0 :     break;
    1053          22 :   case Type::IntegerTyID:
    1054             :     StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
    1055             :     break;
    1056           4 :   case Type::FloatTyID:
    1057           4 :     *((float*)Ptr) = Val.FloatVal;
    1058           4 :     break;
    1059           4 :   case Type::DoubleTyID:
    1060           4 :     *((double*)Ptr) = Val.DoubleVal;
    1061           4 :     break;
    1062           0 :   case Type::X86_FP80TyID:
    1063           0 :     memcpy(Ptr, Val.IntVal.getRawData(), 10);
    1064           0 :     break;
    1065          60 :   case Type::PointerTyID:
    1066             :     // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
    1067          60 :     if (StoreBytes != sizeof(PointerTy))
    1068           0 :       memset(&(Ptr->PointerVal), 0, StoreBytes);
    1069             : 
    1070          60 :     *((PointerTy*)Ptr) = Val.PointerVal;
    1071          60 :     break;
    1072             :   case Type::VectorTyID:
    1073          42 :     for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
    1074          24 :       if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
    1075           4 :         *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
    1076          12 :       if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
    1077           4 :         *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
    1078          12 :       if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
    1079           4 :         unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
    1080           4 :         StoreIntToMemory(Val.AggregateVal[i].IntVal,
    1081           4 :           (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
    1082             :       }
    1083           3 :     }
    1084             :     break;
    1085             :   }
    1086             : 
    1087          93 :   if (sys::IsLittleEndianHost != getDataLayout().isLittleEndian())
    1088             :     // Host and target are different endian - reverse the stored bytes.
    1089           0 :     std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
    1090          93 : }
    1091             : 
    1092             : /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
    1093             : /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
    1094             : static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
    1095             :   assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
    1096             :   uint8_t *Dst = reinterpret_cast<uint8_t *>(
    1097             :                    const_cast<uint64_t *>(IntVal.getRawData()));
    1098             : 
    1099             :   if (sys::IsLittleEndianHost)
    1100             :     // Little-endian host - the destination must be ordered from LSB to MSB.
    1101             :     // The source is ordered from LSB to MSB: Do a straight copy.
    1102          23 :     memcpy(Dst, Src, LoadBytes);
    1103             :   else {
    1104             :     // Big-endian - the destination is an array of 64 bit words ordered from
    1105             :     // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
    1106             :     // ordered from MSB to LSB: Reverse the word order, but not the bytes in
    1107             :     // a word.
    1108             :     while (LoadBytes > sizeof(uint64_t)) {
    1109             :       LoadBytes -= sizeof(uint64_t);
    1110             :       // May not be aligned so use memcpy.
    1111             :       memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
    1112             :       Dst += sizeof(uint64_t);
    1113             :     }
    1114             : 
    1115             :     memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
    1116             :   }
    1117             : }
    1118             : 
    1119             : /// FIXME: document
    1120             : ///
    1121          30 : void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
    1122             :                                           GenericValue *Ptr,
    1123             :                                           Type *Ty) {
    1124             :   const unsigned LoadBytes = getDataLayout().getTypeStoreSize(Ty);
    1125             : 
    1126          30 :   switch (Ty->getTypeID()) {
    1127             :   case Type::IntegerTyID:
    1128             :     // An APInt with all words initially zero.
    1129          19 :     Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
    1130             :     LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
    1131             :     break;
    1132           4 :   case Type::FloatTyID:
    1133           4 :     Result.FloatVal = *((float*)Ptr);
    1134           4 :     break;
    1135           4 :   case Type::DoubleTyID:
    1136           4 :     Result.DoubleVal = *((double*)Ptr);
    1137           4 :     break;
    1138           0 :   case Type::PointerTyID:
    1139           0 :     Result.PointerVal = *((PointerTy*)Ptr);
    1140           0 :     break;
    1141           0 :   case Type::X86_FP80TyID: {
    1142             :     // This is endian dependent, but it will only work on x86 anyway.
    1143             :     // FIXME: Will not trap if loading a signaling NaN.
    1144             :     uint64_t y[2];
    1145           0 :     memcpy(y, Ptr, 10);
    1146           0 :     Result.IntVal = APInt(80, y);
    1147             :     break;
    1148             :   }
    1149             :   case Type::VectorTyID: {
    1150             :     auto *VT = cast<VectorType>(Ty);
    1151           3 :     Type *ElemT = VT->getElementType();
    1152           3 :     const unsigned numElems = VT->getNumElements();
    1153           3 :     if (ElemT->isFloatTy()) {
    1154           1 :       Result.AggregateVal.resize(numElems);
    1155           9 :       for (unsigned i = 0; i < numElems; ++i)
    1156           8 :         Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
    1157             :     }
    1158           3 :     if (ElemT->isDoubleTy()) {
    1159           1 :       Result.AggregateVal.resize(numElems);
    1160           9 :       for (unsigned i = 0; i < numElems; ++i)
    1161           8 :         Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
    1162             :     }
    1163           3 :     if (ElemT->isIntegerTy()) {
    1164           1 :       GenericValue intZero;
    1165             :       const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
    1166           1 :       intZero.IntVal = APInt(elemBitWidth, 0);
    1167           1 :       Result.AggregateVal.resize(numElems, intZero);
    1168           9 :       for (unsigned i = 0; i < numElems; ++i)
    1169           8 :         LoadIntFromMemory(Result.AggregateVal[i].IntVal,
    1170           8 :           (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
    1171             :     }
    1172             :   break;
    1173             :   }
    1174             :   default:
    1175             :     SmallString<256> Msg;
    1176             :     raw_svector_ostream OS(Msg);
    1177           0 :     OS << "Cannot load value of type " << *Ty << "!";
    1178           0 :     report_fatal_error(OS.str());
    1179             :   }
    1180          30 : }
    1181             : 
    1182          13 : void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
    1183             :   DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
    1184             :   DEBUG(Init->dump());
    1185          13 :   if (isa<UndefValue>(Init))
    1186             :     return;
    1187             : 
    1188             :   if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
    1189             :     unsigned ElementSize =
    1190           0 :         getDataLayout().getTypeAllocSize(CP->getType()->getElementType());
    1191           0 :     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
    1192           0 :       InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
    1193             :     return;
    1194             :   }
    1195             : 
    1196          13 :   if (isa<ConstantAggregateZero>(Init)) {
    1197           1 :     memset(Addr, 0, (size_t)getDataLayout().getTypeAllocSize(Init->getType()));
    1198           1 :     return;
    1199             :   }
    1200             : 
    1201             :   if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
    1202             :     unsigned ElementSize =
    1203           0 :         getDataLayout().getTypeAllocSize(CPA->getType()->getElementType());
    1204           0 :     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
    1205           0 :       InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
    1206             :     return;
    1207             :   }
    1208             : 
    1209             :   if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
    1210             :     const StructLayout *SL =
    1211           0 :         getDataLayout().getStructLayout(cast<StructType>(CPS->getType()));
    1212           0 :     for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
    1213           0 :       InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
    1214             :     return;
    1215             :   }
    1216             : 
    1217             :   if (const ConstantDataSequential *CDS =
    1218             :                dyn_cast<ConstantDataSequential>(Init)) {
    1219             :     // CDS is already laid out in host memory order.
    1220          11 :     StringRef Data = CDS->getRawDataValues();
    1221          11 :     memcpy(Addr, Data.data(), Data.size());
    1222             :     return;
    1223             :   }
    1224             : 
    1225           1 :   if (Init->getType()->isFirstClassType()) {
    1226           2 :     GenericValue Val = getConstantValue(Init);
    1227           1 :     StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
    1228             :     return;
    1229             :   }
    1230             : 
    1231             :   DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
    1232           0 :   llvm_unreachable("Unknown constant type to initialize memory with!");
    1233             : }
    1234             : 
    1235             : /// EmitGlobals - Emit all of the global variables to memory, storing their
    1236             : /// addresses into GlobalAddress.  This must make sure to copy the contents of
    1237             : /// their initializers into the memory.
    1238          23 : void ExecutionEngine::emitGlobals() {
    1239             :   // Loop over all of the global variables in the program, allocating the memory
    1240             :   // to hold them.  If there is more than one module, do a prepass over globals
    1241             :   // to figure out how the different modules should link together.
    1242             :   std::map<std::pair<std::string, Type*>,
    1243             :            const GlobalValue*> LinkedGlobalsMap;
    1244             : 
    1245          23 :   if (Modules.size() != 1) {
    1246           0 :     for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
    1247           0 :       Module &M = *Modules[m];
    1248           0 :       for (const auto &GV : M.globals()) {
    1249           0 :         if (GV.hasLocalLinkage() || GV.isDeclaration() ||
    1250           0 :             GV.hasAppendingLinkage() || !GV.hasName())
    1251           0 :           continue;// Ignore external globals and globals with internal linkage.
    1252             : 
    1253             :         const GlobalValue *&GVEntry =
    1254           0 :           LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
    1255             : 
    1256             :         // If this is the first time we've seen this global, it is the canonical
    1257             :         // version.
    1258           0 :         if (!GVEntry) {
    1259           0 :           GVEntry = &GV;
    1260           0 :           continue;
    1261             :         }
    1262             : 
    1263             :         // If the existing global is strong, never replace it.
    1264           0 :         if (GVEntry->hasExternalLinkage())
    1265           0 :           continue;
    1266             : 
    1267             :         // Otherwise, we know it's linkonce/weak, replace it if this is a strong
    1268             :         // symbol.  FIXME is this right for common?
    1269           0 :         if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
    1270           0 :           GVEntry = &GV;
    1271             :       }
    1272             :     }
    1273             :   }
    1274             : 
    1275             :   std::vector<const GlobalValue*> NonCanonicalGlobals;
    1276          46 :   for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
    1277          23 :     Module &M = *Modules[m];
    1278          35 :     for (const auto &GV : M.globals()) {
    1279             :       // In the multi-module case, see what this global maps to.
    1280          12 :       if (!LinkedGlobalsMap.empty()) {
    1281           0 :         if (const GlobalValue *GVEntry =
    1282           0 :               LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
    1283             :           // If something else is the canonical global, ignore this one.
    1284           0 :           if (GVEntry != &GV) {
    1285           0 :             NonCanonicalGlobals.push_back(&GV);
    1286           0 :             continue;
    1287             :           }
    1288             :         }
    1289             :       }
    1290             : 
    1291          12 :       if (!GV.isDeclaration()) {
    1292          12 :         addGlobalMapping(&GV, getMemoryForGV(&GV));
    1293             :       } else {
    1294             :         // External variable reference. Try to use the dynamic loader to
    1295             :         // get a pointer to it.
    1296           0 :         if (void *SymAddr =
    1297           0 :             sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
    1298           0 :           addGlobalMapping(&GV, SymAddr);
    1299             :         else {
    1300           0 :           report_fatal_error("Could not resolve external global address: "
    1301           0 :                             +GV.getName());
    1302             :         }
    1303             :       }
    1304             :     }
    1305             : 
    1306             :     // If there are multiple modules, map the non-canonical globals to their
    1307             :     // canonical location.
    1308          23 :     if (!NonCanonicalGlobals.empty()) {
    1309           0 :       for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
    1310           0 :         const GlobalValue *GV = NonCanonicalGlobals[i];
    1311             :         const GlobalValue *CGV =
    1312           0 :           LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
    1313           0 :         void *Ptr = getPointerToGlobalIfAvailable(CGV);
    1314             :         assert(Ptr && "Canonical global wasn't codegen'd!");
    1315           0 :         addGlobalMapping(GV, Ptr);
    1316             :       }
    1317             :     }
    1318             : 
    1319             :     // Now that all of the globals are set up in memory, loop through them all
    1320             :     // and initialize their contents.
    1321          35 :     for (const auto &GV : M.globals()) {
    1322          12 :       if (!GV.isDeclaration()) {
    1323          12 :         if (!LinkedGlobalsMap.empty()) {
    1324           0 :           if (const GlobalValue *GVEntry =
    1325           0 :                 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
    1326           0 :             if (GVEntry != &GV)  // Not the canonical variable.
    1327           0 :               continue;
    1328             :         }
    1329          12 :         EmitGlobalVariable(&GV);
    1330             :       }
    1331             :     }
    1332             :   }
    1333          23 : }
    1334             : 
    1335             : // EmitGlobalVariable - This method emits the specified global variable to the
    1336             : // address specified in GlobalAddresses, or allocates new memory if it's not
    1337             : // already in the map.
    1338          13 : void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
    1339          13 :   void *GA = getPointerToGlobalIfAvailable(GV);
    1340             : 
    1341          13 :   if (!GA) {
    1342             :     // If it's not already specified, allocate memory for the global.
    1343           1 :     GA = getMemoryForGV(GV);
    1344             : 
    1345             :     // If we failed to allocate memory for this global, return.
    1346           1 :     if (!GA) return;
    1347             : 
    1348           1 :     addGlobalMapping(GV, GA);
    1349             :   }
    1350             : 
    1351             :   // Don't initialize if it's thread local, let the client do it.
    1352          13 :   if (!GV->isThreadLocal())
    1353          13 :     InitializeMemory(GV->getInitializer(), GA);
    1354             : 
    1355          13 :   Type *ElTy = GV->getValueType();
    1356          13 :   size_t GVSize = (size_t)getDataLayout().getTypeAllocSize(ElTy);
    1357             :   NumInitBytes += (unsigned)GVSize;
    1358             :   ++NumGlobals;
    1359             : }

Generated by: LCOV version 1.13