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

Generated by: LCOV version 1.13