LCOV - code coverage report
Current view: top level - lib/ExecutionEngine/MCJIT - MCJIT.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 234 279 83.9 %
Date: 2018-06-17 00:07:59 Functions: 34 38 89.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
       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             : #include "MCJIT.h"
      11             : #include "llvm/ADT/STLExtras.h"
      12             : #include "llvm/ExecutionEngine/GenericValue.h"
      13             : #include "llvm/ExecutionEngine/JITEventListener.h"
      14             : #include "llvm/ExecutionEngine/MCJIT.h"
      15             : #include "llvm/ExecutionEngine/SectionMemoryManager.h"
      16             : #include "llvm/IR/DataLayout.h"
      17             : #include "llvm/IR/DerivedTypes.h"
      18             : #include "llvm/IR/Function.h"
      19             : #include "llvm/IR/LegacyPassManager.h"
      20             : #include "llvm/IR/Mangler.h"
      21             : #include "llvm/IR/Module.h"
      22             : #include "llvm/Object/Archive.h"
      23             : #include "llvm/Object/ObjectFile.h"
      24             : #include "llvm/Support/DynamicLibrary.h"
      25             : #include "llvm/Support/ErrorHandling.h"
      26             : #include "llvm/Support/MemoryBuffer.h"
      27             : #include "llvm/Support/MutexGuard.h"
      28             : 
      29             : using namespace llvm;
      30             : 
      31             : namespace {
      32             : 
      33             : static struct RegisterJIT {
      34             :   RegisterJIT() { MCJIT::Register(); }
      35             : } JITRegistrator;
      36             : 
      37             : }
      38             : 
      39           0 : extern "C" void LLVMLinkInMCJIT() {
      40           0 : }
      41             : 
      42             : ExecutionEngine *
      43         121 : MCJIT::createJIT(std::unique_ptr<Module> M, std::string *ErrorStr,
      44             :                  std::shared_ptr<MCJITMemoryManager> MemMgr,
      45             :                  std::shared_ptr<LegacyJITSymbolResolver> Resolver,
      46             :                  std::unique_ptr<TargetMachine> TM) {
      47             :   // Try to register the program as a source of symbols to resolve against.
      48             :   //
      49             :   // FIXME: Don't do this here.
      50             :   sys::DynamicLibrary::LoadLibraryPermanently(nullptr, nullptr);
      51             : 
      52         121 :   if (!MemMgr || !Resolver) {
      53             :     auto RTDyldMM = std::make_shared<SectionMemoryManager>();
      54           5 :     if (!MemMgr)
      55             :       MemMgr = RTDyldMM;
      56           5 :     if (!Resolver)
      57             :       Resolver = RTDyldMM;
      58             :   }
      59             : 
      60             :   return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
      61         484 :                    std::move(Resolver));
      62             : }
      63             : 
      64         121 : MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> TM,
      65             :              std::shared_ptr<MCJITMemoryManager> MemMgr,
      66         121 :              std::shared_ptr<LegacyJITSymbolResolver> Resolver)
      67         121 :     : ExecutionEngine(TM->createDataLayout(), std::move(M)), TM(std::move(TM)),
      68             :       Ctx(nullptr), MemMgr(std::move(MemMgr)),
      69             :       Resolver(*this, std::move(Resolver)), Dyld(*this->MemMgr, this->Resolver),
      70         726 :       ObjCache(nullptr) {
      71             :   // FIXME: We are managing our modules, so we do not want the base class
      72             :   // ExecutionEngine to manage them as well. To avoid double destruction
      73             :   // of the first (and only) module added in ExecutionEngine constructor
      74             :   // we remove it from EE and will destruct it ourselves.
      75             :   //
      76             :   // It may make sense to move our module manager (based on SmallStPtr) back
      77             :   // into EE if the JIT and Interpreter can live with it.
      78             :   // If so, additional functions: addModule, removeModule, FindFunctionNamed,
      79             :   // runStaticConstructorsDestructors could be moved back to EE as well.
      80             :   //
      81         121 :   std::unique_ptr<Module> First = std::move(Modules[0]);
      82             :   Modules.clear();
      83             : 
      84         242 :   if (First->getDataLayout().isDefault())
      85         121 :     First->setDataLayout(getDataLayout());
      86             : 
      87         121 :   OwnedModules.addModule(std::move(First));
      88         121 :   RegisterJITEventListener(JITEventListener::createGDBRegistrationListener());
      89         121 : }
      90             : 
      91         245 : MCJIT::~MCJIT() {
      92             :   MutexGuard locked(lock);
      93             : 
      94          49 :   Dyld.deregisterEHFrames();
      95             : 
      96         175 :   for (auto &Obj : LoadedObjects)
      97          63 :     if (Obj)
      98          63 :       NotifyFreeingObject(*Obj);
      99             : 
     100             :   Archives.clear();
     101          98 : }
     102             : 
     103          31 : void MCJIT::addModule(std::unique_ptr<Module> M) {
     104             :   MutexGuard locked(lock);
     105             : 
     106          62 :   if (M->getDataLayout().isDefault())
     107          31 :     M->setDataLayout(getDataLayout());
     108             : 
     109          31 :   OwnedModules.addModule(std::move(M));
     110          31 : }
     111             : 
     112           0 : bool MCJIT::removeModule(Module *M) {
     113             :   MutexGuard locked(lock);
     114           0 :   return OwnedModules.removeModule(M);
     115             : }
     116             : 
     117           4 : void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
     118           8 :   std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.loadObject(*Obj);
     119           4 :   if (Dyld.hasError())
     120           0 :     report_fatal_error(Dyld.getErrorString());
     121             : 
     122           4 :   NotifyObjectEmitted(*Obj, *L);
     123             : 
     124           4 :   LoadedObjects.push_back(std::move(Obj));
     125           4 : }
     126             : 
     127           2 : void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) {
     128           2 :   std::unique_ptr<object::ObjectFile> ObjFile;
     129           2 :   std::unique_ptr<MemoryBuffer> MemBuf;
     130           2 :   std::tie(ObjFile, MemBuf) = Obj.takeBinary();
     131           6 :   addObjectFile(std::move(ObjFile));
     132           2 :   Buffers.push_back(std::move(MemBuf));
     133           2 : }
     134             : 
     135           1 : void MCJIT::addArchive(object::OwningBinary<object::Archive> A) {
     136           1 :   Archives.push_back(std::move(A));
     137           1 : }
     138             : 
     139           7 : void MCJIT::setObjectCache(ObjectCache* NewCache) {
     140             :   MutexGuard locked(lock);
     141           7 :   ObjCache = NewCache;
     142           7 : }
     143             : 
     144         143 : std::unique_ptr<MemoryBuffer> MCJIT::emitObject(Module *M) {
     145             :   assert(M && "Can not emit a null module");
     146             : 
     147             :   MutexGuard locked(lock);
     148             : 
     149             :   // Materialize all globals in the module if they have not been
     150             :   // materialized already.
     151         286 :   cantFail(M->materializeAll());
     152             : 
     153             :   // This must be a module which has already been added but not loaded to this
     154             :   // MCJIT instance, since these conditions are tested by our caller,
     155             :   // generateCodeForModule.
     156             : 
     157         286 :   legacy::PassManager PM;
     158             : 
     159             :   // The RuntimeDyld will take ownership of this shortly
     160             :   SmallVector<char, 4096> ObjBufferSV;
     161             :   raw_svector_ostream ObjStream(ObjBufferSV);
     162             : 
     163             :   // Turn the machine code intermediate representation into bytes in memory
     164             :   // that may be executed.
     165         143 :   if (TM->addPassesToEmitMC(PM, Ctx, ObjStream, !getVerifyModules()))
     166           0 :     report_fatal_error("Target does not support MC emission!");
     167             : 
     168             :   // Initialize passes.
     169         143 :   PM.run(*M);
     170             :   // Flush the output buffer to get the generated code into memory
     171             : 
     172             :   std::unique_ptr<MemoryBuffer> CompiledObjBuffer(
     173         143 :       new SmallVectorMemoryBuffer(std::move(ObjBufferSV)));
     174             : 
     175             :   // If we have an object cache, tell it about the new object.
     176             :   // Note that we're using the compiled image, not the loaded image (as below).
     177         143 :   if (ObjCache) {
     178             :     // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
     179             :     // to create a temporary object here and delete it after the call.
     180           7 :     MemoryBufferRef MB = CompiledObjBuffer->getMemBufferRef();
     181           7 :     ObjCache->notifyObjectCompiled(M, MB);
     182             :   }
     183             : 
     184         143 :   return CompiledObjBuffer;
     185             : }
     186             : 
     187         144 : void MCJIT::generateCodeForModule(Module *M) {
     188             :   // Get a thread lock to make sure we aren't trying to load multiple times
     189             :   MutexGuard locked(lock);
     190             : 
     191             :   // This must be a module which has already been added to this MCJIT instance.
     192             :   assert(OwnedModules.ownsModule(M) &&
     193             :          "MCJIT::generateCodeForModule: Unknown module.");
     194             : 
     195             :   // Re-compilation is not supported
     196         144 :   if (OwnedModules.hasModuleBeenLoaded(M))
     197             :     return;
     198             : 
     199         144 :   std::unique_ptr<MemoryBuffer> ObjectToLoad;
     200             :   // Try to load the pre-compiled object from cache if possible
     201         144 :   if (ObjCache)
     202          16 :     ObjectToLoad = ObjCache->getObject(M);
     203             : 
     204             :   assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
     205             : 
     206             :   // If the cache did not contain a suitable object, compile the object
     207         144 :   if (!ObjectToLoad) {
     208         286 :     ObjectToLoad = emitObject(M);
     209             :     assert(ObjectToLoad && "Compilation did not produce an object.");
     210             :   }
     211             : 
     212             :   // Load the object into the dynamic linker.
     213             :   // MCJIT now owns the ObjectImage pointer (via its LoadedObjects list).
     214             :   Expected<std::unique_ptr<object::ObjectFile>> LoadedObject =
     215         288 :     object::ObjectFile::createObjectFile(ObjectToLoad->getMemBufferRef());
     216         144 :   if (!LoadedObject) {
     217             :     std::string Buf;
     218             :     raw_string_ostream OS(Buf);
     219           0 :     logAllUnhandledErrors(LoadedObject.takeError(), OS, "");
     220             :     OS.flush();
     221           0 :     report_fatal_error(Buf);
     222             :   }
     223             :   std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
     224         288 :     Dyld.loadObject(*LoadedObject.get());
     225             : 
     226         144 :   if (Dyld.hasError())
     227           0 :     report_fatal_error(Dyld.getErrorString());
     228             : 
     229         144 :   NotifyObjectEmitted(*LoadedObject.get(), *L);
     230             : 
     231         144 :   Buffers.push_back(std::move(ObjectToLoad));
     232         144 :   LoadedObjects.push_back(std::move(*LoadedObject));
     233             : 
     234         144 :   OwnedModules.markModuleAsLoaded(M);
     235             : }
     236             : 
     237         295 : void MCJIT::finalizeLoadedModules() {
     238             :   MutexGuard locked(lock);
     239             : 
     240             :   // Resolve any outstanding relocations.
     241         295 :   Dyld.resolveRelocations();
     242             : 
     243         295 :   OwnedModules.markAllLoadedModulesAsFinalized();
     244             : 
     245             :   // Register EH frame data for any module we own which has been loaded
     246         295 :   Dyld.registerEHFrames();
     247             : 
     248             :   // Set page permissions.
     249         295 :   MemMgr->finalizeMemory();
     250         295 : }
     251             : 
     252             : // FIXME: Rename this.
     253         102 : void MCJIT::finalizeObject() {
     254             :   MutexGuard locked(lock);
     255             : 
     256             :   // Generate code for module is going to move objects out of the 'added' list,
     257             :   // so we need to copy that out before using it:
     258             :   SmallVector<Module*, 16> ModsToAdd;
     259         199 :   for (auto M : OwnedModules.added())
     260          97 :     ModsToAdd.push_back(M);
     261             : 
     262         296 :   for (auto M : ModsToAdd)
     263          97 :     generateCodeForModule(M);
     264             : 
     265         102 :   finalizeLoadedModules();
     266         102 : }
     267             : 
     268         143 : void MCJIT::finalizeModule(Module *M) {
     269             :   MutexGuard locked(lock);
     270             : 
     271             :   // This must be a module which has already been added to this MCJIT instance.
     272             :   assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
     273             : 
     274             :   // If the module hasn't been compiled, just do that.
     275         143 :   if (!OwnedModules.hasModuleBeenLoaded(M))
     276           0 :     generateCodeForModule(M);
     277             : 
     278         143 :   finalizeLoadedModules();
     279         143 : }
     280             : 
     281         234 : JITSymbol MCJIT::findExistingSymbol(const std::string &Name) {
     282         468 :   if (void *Addr = getPointerToGlobalIfAvailable(Name))
     283             :     return JITSymbol(static_cast<uint64_t>(
     284             :                          reinterpret_cast<uintptr_t>(Addr)),
     285           2 :                      JITSymbolFlags::Exported);
     286             : 
     287         696 :   return Dyld.getSymbol(Name);
     288             : }
     289             : 
     290         168 : Module *MCJIT::findModuleForSymbol(const std::string &Name,
     291             :                                    bool CheckFunctionsOnly) {
     292             :   StringRef DemangledName = Name;
     293         336 :   if (DemangledName[0] == getDataLayout().getGlobalPrefix())
     294           0 :     DemangledName = DemangledName.substr(1);
     295             : 
     296             :   MutexGuard locked(lock);
     297             : 
     298             :   // If it hasn't already been generated, see if it's in one of our modules.
     299             :   for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
     300             :                               E = OwnedModules.end_added();
     301         177 :        I != E; ++I) {
     302             :     Module *M = *I;
     303          56 :     Function *F = M->getFunction(DemangledName);
     304          56 :     if (F && !F->isDeclaration())
     305             :       return M;
     306          10 :     if (!CheckFunctionsOnly) {
     307             :       GlobalVariable *G = M->getGlobalVariable(DemangledName);
     308           3 :       if (G && !G->isDeclaration())
     309             :         return M;
     310             :       // FIXME: Do we need to worry about global aliases?
     311             :     }
     312             :   }
     313             :   // We didn't find the symbol in any of our modules.
     314             :   return nullptr;
     315             : }
     316             : 
     317          50 : uint64_t MCJIT::getSymbolAddress(const std::string &Name,
     318             :                                  bool CheckFunctionsOnly) {
     319             :   std::string MangledName;
     320             :   {
     321          50 :     raw_string_ostream MangledNameStream(MangledName);
     322          50 :     Mangler::getNameWithPrefix(MangledNameStream, Name, getDataLayout());
     323             :   }
     324         100 :   if (auto Sym = findSymbol(MangledName, CheckFunctionsOnly)) {
     325         100 :     if (auto AddrOrErr = Sym.getAddress())
     326         100 :       return *AddrOrErr;
     327             :     else
     328           0 :       report_fatal_error(AddrOrErr.takeError());
     329             :   } else
     330           0 :     report_fatal_error(Sym.takeError());
     331             : }
     332             : 
     333         185 : JITSymbol MCJIT::findSymbol(const std::string &Name,
     334             :                             bool CheckFunctionsOnly) {
     335             :   MutexGuard locked(lock);
     336             : 
     337             :   // First, check to see if we already have this symbol.
     338         355 :   if (auto Sym = findExistingSymbol(Name))
     339          15 :     return Sym;
     340             : 
     341         172 :   for (object::OwningBinary<object::Archive> &OB : Archives) {
     342             :     object::Archive *A = OB.getBinary();
     343             :     // Look for our symbols in each Archive
     344           3 :     auto OptionalChildOrErr = A->findSym(Name);
     345           3 :     if (!OptionalChildOrErr)
     346           0 :       report_fatal_error(OptionalChildOrErr.takeError());
     347             :     auto &OptionalChild = *OptionalChildOrErr;
     348           3 :     if (OptionalChild) {
     349             :       // FIXME: Support nested archives?
     350             :       Expected<std::unique_ptr<object::Binary>> ChildBinOrErr =
     351           2 :           OptionalChild->getAsBinary();
     352           2 :       if (!ChildBinOrErr) {
     353             :         // TODO: Actually report errors helpfully.
     354           0 :         consumeError(ChildBinOrErr.takeError());
     355           0 :         continue;
     356             :       }
     357             :       std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
     358           4 :       if (ChildBin->isObject()) {
     359             :         std::unique_ptr<object::ObjectFile> OF(
     360             :             static_cast<object::ObjectFile *>(ChildBin.release()));
     361             :         // This causes the object file to be loaded.
     362           6 :         addObjectFile(std::move(OF));
     363             :         // The address should be here now.
     364           2 :         if (auto Sym = findExistingSymbol(Name))
     365           2 :           return Sym;
     366             :       }
     367             :     }
     368             :   }
     369             : 
     370             :   // If it hasn't already been generated, see if it's in one of our modules.
     371         168 :   Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
     372         168 :   if (M) {
     373          47 :     generateCodeForModule(M);
     374             : 
     375             :     // Check the RuntimeDyld table again, it should be there now.
     376          47 :     return findExistingSymbol(Name);
     377             :   }
     378             : 
     379             :   // If a LazyFunctionCreator is installed, use it to get/create the function.
     380             :   // FIXME: Should we instead have a LazySymbolCreator callback?
     381         121 :   if (LazyFunctionCreator) {
     382             :     auto Addr = static_cast<uint64_t>(
     383           3 :                   reinterpret_cast<uintptr_t>(LazyFunctionCreator(Name)));
     384             :     return JITSymbol(Addr, JITSymbolFlags::Exported);
     385             :   }
     386             : 
     387             :   return nullptr;
     388             : }
     389             : 
     390           1 : uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
     391             :   MutexGuard locked(lock);
     392           1 :   uint64_t Result = getSymbolAddress(Name, false);
     393           1 :   if (Result != 0)
     394           1 :     finalizeLoadedModules();
     395           1 :   return Result;
     396             : }
     397             : 
     398          49 : uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
     399             :   MutexGuard locked(lock);
     400          49 :   uint64_t Result = getSymbolAddress(Name, true);
     401          49 :   if (Result != 0)
     402          49 :     finalizeLoadedModules();
     403          49 :   return Result;
     404             : }
     405             : 
     406             : // Deprecated.  Use getFunctionAddress instead.
     407         226 : void *MCJIT::getPointerToFunction(Function *F) {
     408             :   MutexGuard locked(lock);
     409             : 
     410             :   Mangler Mang;
     411             :   SmallString<128> Name;
     412         226 :   TM->getNameWithPrefix(Name, F, Mang);
     413             : 
     414         380 :   if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
     415          72 :     bool AbortOnFailure = !F->hasExternalWeakLinkage();
     416         144 :     void *Addr = getPointerToNamedFunction(Name, AbortOnFailure);
     417          72 :     updateGlobalMapping(F, Addr);
     418          72 :     return Addr;
     419             :   }
     420             : 
     421         154 :   Module *M = F->getParent();
     422             :   bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
     423             : 
     424             :   // Make sure the relevant module has been compiled and loaded.
     425         154 :   if (HasBeenAddedButNotLoaded)
     426           0 :     generateCodeForModule(M);
     427         154 :   else if (!OwnedModules.hasModuleBeenLoaded(M)) {
     428             :     // If this function doesn't belong to one of our modules, we're done.
     429             :     // FIXME: Asking for the pointer to a function that hasn't been registered,
     430             :     //        and isn't a declaration (which is handled above) should probably
     431             :     //        be an assertion.
     432             :     return nullptr;
     433             :   }
     434             : 
     435             :   // FIXME: Should the Dyld be retaining module information? Probably not.
     436             :   //
     437             :   // This is the accessor for the target address, so make sure to check the
     438             :   // load address of the symbol, not the local address.
     439         308 :   return (void*)Dyld.getSymbol(Name).getAddress();
     440             : }
     441             : 
     442         423 : void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
     443             :     bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
     444         584 :   for (; I != E; ++I) {
     445         322 :     ExecutionEngine::runStaticConstructorsDestructors(**I, isDtors);
     446             :   }
     447         423 : }
     448             : 
     449         141 : void MCJIT::runStaticConstructorsDestructors(bool isDtors) {
     450             :   // Execute global ctors/dtors for each module in the program.
     451         141 :   runStaticConstructorsDestructorsInModulePtrSet(
     452             :       isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
     453         141 :   runStaticConstructorsDestructorsInModulePtrSet(
     454             :       isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
     455         141 :   runStaticConstructorsDestructorsInModulePtrSet(
     456             :       isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
     457         141 : }
     458             : 
     459           2 : Function *MCJIT::FindFunctionNamedInModulePtrSet(StringRef FnName,
     460             :                                                  ModulePtrSet::iterator I,
     461             :                                                  ModulePtrSet::iterator E) {
     462           3 :   for (; I != E; ++I) {
     463           3 :     Function *F = (*I)->getFunction(FnName);
     464           3 :     if (F && !F->isDeclaration())
     465             :       return F;
     466             :   }
     467             :   return nullptr;
     468             : }
     469             : 
     470           6 : GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(StringRef Name,
     471             :                                                              bool AllowInternal,
     472             :                                                              ModulePtrSet::iterator I,
     473             :                                                              ModulePtrSet::iterator E) {
     474          10 :   for (; I != E; ++I) {
     475           7 :     GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal);
     476           7 :     if (GV && !GV->isDeclaration())
     477             :       return GV;
     478             :   }
     479             :   return nullptr;
     480             : }
     481             : 
     482             : 
     483           2 : Function *MCJIT::FindFunctionNamed(StringRef FnName) {
     484             :   Function *F = FindFunctionNamedInModulePtrSet(
     485           2 :       FnName, OwnedModules.begin_added(), OwnedModules.end_added());
     486           2 :   if (!F)
     487           0 :     F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
     488             :                                         OwnedModules.end_loaded());
     489           2 :   if (!F)
     490           0 :     F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
     491             :                                         OwnedModules.end_finalized());
     492           2 :   return F;
     493             : }
     494             : 
     495           4 : GlobalVariable *MCJIT::FindGlobalVariableNamed(StringRef Name, bool AllowInternal) {
     496           4 :   GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet(
     497           4 :       Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added());
     498           4 :   if (!GV)
     499           1 :     GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(),
     500             :                                         OwnedModules.end_loaded());
     501           4 :   if (!GV)
     502           1 :     GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(),
     503             :                                         OwnedModules.end_finalized());
     504           4 :   return GV;
     505             : }
     506             : 
     507         143 : GenericValue MCJIT::runFunction(Function *F, ArrayRef<GenericValue> ArgValues) {
     508             :   assert(F && "Function *F was null at entry to run()");
     509             : 
     510         143 :   void *FPtr = getPointerToFunction(F);
     511         143 :   finalizeModule(F->getParent());
     512             :   assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
     513             :   FunctionType *FTy = F->getFunctionType();
     514         143 :   Type *RetTy = FTy->getReturnType();
     515             : 
     516             :   assert((FTy->getNumParams() == ArgValues.size() ||
     517             :           (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
     518             :          "Wrong number of arguments passed into function!");
     519             :   assert(FTy->getNumParams() == ArgValues.size() &&
     520             :          "This doesn't support passing arguments through varargs (yet)!");
     521             : 
     522             :   // Handle some common cases first.  These cases correspond to common `main'
     523             :   // prototypes.
     524         215 :   if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
     525         143 :     switch (ArgValues.size()) {
     526           1 :     case 3:
     527           3 :       if (FTy->getParamType(0)->isIntegerTy(32) &&
     528           2 :           FTy->getParamType(1)->isPointerTy() &&
     529             :           FTy->getParamType(2)->isPointerTy()) {
     530           1 :         int (*PF)(int, char **, const char **) =
     531             :           (int(*)(int, char **, const char **))(intptr_t)FPtr;
     532             : 
     533             :         // Call the function.
     534           1 :         GenericValue rv;
     535           2 :         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
     536           1 :                                  (char **)GVTOP(ArgValues[1]),
     537           1 :                                  (const char **)GVTOP(ArgValues[2])));
     538             :         return rv;
     539             :       }
     540             :       break;
     541           5 :     case 2:
     542          15 :       if (FTy->getParamType(0)->isIntegerTy(32) &&
     543           5 :           FTy->getParamType(1)->isPointerTy()) {
     544           5 :         int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
     545             : 
     546             :         // Call the function.
     547           5 :         GenericValue rv;
     548          10 :         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
     549           5 :                                  (char **)GVTOP(ArgValues[1])));
     550             :         return rv;
     551             :       }
     552             :       break;
     553          72 :     case 1:
     554         144 :       if (FTy->getNumParams() == 1 &&
     555         144 :           FTy->getParamType(0)->isIntegerTy(32)) {
     556           2 :         GenericValue rv;
     557          72 :         int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
     558          74 :         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
     559             :         return rv;
     560             :       }
     561             :       break;
     562             :     }
     563             :   }
     564             : 
     565             :   // Handle cases where no arguments are passed first.
     566          65 :   if (ArgValues.empty()) {
     567          64 :     GenericValue rv;
     568          65 :     switch (RetTy->getTypeID()) {
     569           0 :     default: llvm_unreachable("Unknown return type for function call!");
     570             :     case Type::IntegerTyID: {
     571             :       unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
     572          63 :       if (BitWidth == 1)
     573           0 :         rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
     574          63 :       else if (BitWidth <= 8)
     575           0 :         rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
     576          63 :       else if (BitWidth <= 16)
     577           0 :         rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
     578          63 :       else if (BitWidth <= 32)
     579         125 :         rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
     580           0 :       else if (BitWidth <= 64)
     581           0 :         rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
     582             :       else
     583           0 :         llvm_unreachable("Integer types > 64 bits not supported");
     584             :       return rv;
     585             :     }
     586           2 :     case Type::VoidTyID:
     587           4 :       rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
     588             :       return rv;
     589           0 :     case Type::FloatTyID:
     590           0 :       rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
     591             :       return rv;
     592           0 :     case Type::DoubleTyID:
     593           0 :       rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
     594             :       return rv;
     595           0 :     case Type::X86_FP80TyID:
     596             :     case Type::FP128TyID:
     597             :     case Type::PPC_FP128TyID:
     598           0 :       llvm_unreachable("long double not supported yet");
     599           0 :     case Type::PointerTyID:
     600           0 :       return PTOGV(((void*(*)())(intptr_t)FPtr)());
     601             :     }
     602             :   }
     603             : 
     604           0 :   report_fatal_error("MCJIT::runFunction does not support full-featured "
     605             :                      "argument passing. Please use "
     606             :                      "ExecutionEngine::getFunctionAddress and cast the result "
     607             :                      "to the desired function pointer type.");
     608             : }
     609             : 
     610          72 : void *MCJIT::getPointerToNamedFunction(StringRef Name, bool AbortOnFailure) {
     611          72 :   if (!isSymbolSearchingDisabled()) {
     612         216 :     if (auto Sym = Resolver.findSymbol(Name)) {
     613         144 :       if (auto AddrOrErr = Sym.getAddress())
     614             :         return reinterpret_cast<void*>(
     615          72 :                  static_cast<uintptr_t>(*AddrOrErr));
     616           0 :     } else if (auto Err = Sym.takeError())
     617           0 :       report_fatal_error(std::move(Err));
     618             :   }
     619             : 
     620             :   /// If a LazyFunctionCreator is installed, use it to get/create the function.
     621           0 :   if (LazyFunctionCreator)
     622           0 :     if (void *RP = LazyFunctionCreator(Name))
     623             :       return RP;
     624             : 
     625           0 :   if (AbortOnFailure) {
     626           0 :     report_fatal_error("Program used external function '"+Name+
     627             :                        "' which could not be resolved!");
     628             :   }
     629             :   return nullptr;
     630             : }
     631             : 
     632         291 : void MCJIT::RegisterJITEventListener(JITEventListener *L) {
     633         291 :   if (!L)
     634             :     return;
     635             :   MutexGuard locked(lock);
     636         121 :   EventListeners.push_back(L);
     637             : }
     638             : 
     639           0 : void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
     640           0 :   if (!L)
     641           0 :     return;
     642             :   MutexGuard locked(lock);
     643             :   auto I = find(reverse(EventListeners), L);
     644           0 :   if (I != EventListeners.rend()) {
     645             :     std::swap(*I, EventListeners.back());
     646             :     EventListeners.pop_back();
     647             :   }
     648             : }
     649             : 
     650         148 : void MCJIT::NotifyObjectEmitted(const object::ObjectFile& Obj,
     651             :                                 const RuntimeDyld::LoadedObjectInfo &L) {
     652             :   MutexGuard locked(lock);
     653         148 :   MemMgr->notifyObjectLoaded(this, Obj);
     654         444 :   for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
     655         296 :     EventListeners[I]->NotifyObjectEmitted(Obj, L);
     656             :   }
     657         148 : }
     658             : 
     659          63 : void MCJIT::NotifyFreeingObject(const object::ObjectFile& Obj) {
     660             :   MutexGuard locked(lock);
     661         126 :   for (JITEventListener *L : EventListeners)
     662          63 :     L->NotifyFreeingObject(Obj);
     663          63 : }
     664             : 
     665             : JITSymbol
     666         135 : LinkingSymbolResolver::findSymbol(const std::string &Name) {
     667         270 :   auto Result = ParentEngine.findSymbol(Name, false);
     668             :   if (Result)
     669          17 :     return Result;
     670         118 :   if (ParentEngine.isSymbolSearchingDisabled())
     671             :     return nullptr;
     672         118 :   return ClientResolver->findSymbol(Name);
     673             : }
     674             : 
     675      202338 : void LinkingSymbolResolver::anchor() {}

Generated by: LCOV version 1.13