Index: include/llvm/ExecutionEngine/ExecutionEngine.h =================================================================== --- include/llvm/ExecutionEngine/ExecutionEngine.h +++ include/llvm/ExecutionEngine/ExecutionEngine.h @@ -197,11 +197,16 @@ /// M is found. virtual bool removeModule(Module *M); - /// FindFunctionNamed - Search all of the active modules to find the one that + /// FindFunctionNamed - Search all of the active modules to find the function that /// defines FnName. This is very slow operation and shouldn't be used for /// general code. virtual Function *FindFunctionNamed(const char *FnName); + /// FindGlobalVariableNamed - Search all of the active modules to find the global variable + /// that defines Name. This is very slow operation and shouldn't be used for + /// general code. + virtual GlobalVariable *FindGlobalVariableNamed(const char *Name, bool AllowInternal = false); + /// runFunction - Execute the specified function with the specified arguments, /// and return the result. virtual GenericValue runFunction(Function *F, Index: lib/ExecutionEngine/ExecutionEngine.cpp =================================================================== --- lib/ExecutionEngine/ExecutionEngine.cpp +++ lib/ExecutionEngine/ExecutionEngine.cpp @@ -153,6 +153,14 @@ return nullptr; } +GlobalVariable *ExecutionEngine::FindGlobalVariableNamed(const char *Name, bool AllowInternal) { + for (unsigned i = 0, e = Modules.size(); i != e; ++i) { + GlobalVariable *GV = Modules[i]->getGlobalVariable(Name,AllowInternal); + if (GV && !GV->isDeclaration()) + return GV; + } + return nullptr; +} uint64_t ExecutionEngineState::RemoveMapping(StringRef Name) { GlobalAddressMapTy::iterator I = GlobalAddressMap.find(Name); Index: lib/ExecutionEngine/MCJIT/MCJIT.h =================================================================== --- lib/ExecutionEngine/MCJIT/MCJIT.h +++ lib/ExecutionEngine/MCJIT/MCJIT.h @@ -200,6 +200,11 @@ ModulePtrSet::iterator I, ModulePtrSet::iterator E); + GlobalVariable *FindGlobalVariableNamedInModulePtrSet(const char *Name, + bool AllowInternal, + ModulePtrSet::iterator I, + ModulePtrSet::iterator E); + void runStaticConstructorsDestructorsInModulePtrSet(bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E); @@ -215,10 +220,21 @@ void addArchive(object::OwningBinary O) override; bool removeModule(Module *M) override; - /// FindFunctionNamed - Search all of the active modules to find the one that + // Like addObjectFile, but loads the object now and returns the LoadedObjectInfo + std::unique_ptr + LoadObjectFile(object::OwningBinary O); + std::unique_ptr + LoadObjectFile(std::unique_ptr O); + + /// FindFunctionNamed - Search all of the active modules to find the function that /// defines FnName. This is very slow operation and shouldn't be used for /// general code. - Function *FindFunctionNamed(const char *FnName) override; + virtual Function *FindFunctionNamed(const char *FnName) override; + + /// FindGlobalVariableNamed - Search all of the active modules to find the global variable + /// that defines Name. This is very slow operation and shouldn't be used for + /// general code. + virtual GlobalVariable *FindGlobalVariableNamed(const char *Name, bool AllowInternal = false) override; /// Sets the object manager that MCJIT should use to avoid compilation. void setObjectCache(ObjectCache *manager) override; Index: lib/ExecutionEngine/MCJIT/MCJIT.cpp =================================================================== --- lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -111,7 +111,8 @@ return OwnedModules.removeModule(M); } -void MCJIT::addObjectFile(std::unique_ptr Obj) { +std::unique_ptr +MCJIT::LoadObjectFile(std::unique_ptr Obj) { std::unique_ptr L = Dyld.loadObject(*Obj); if (Dyld.hasError()) report_fatal_error(Dyld.getErrorString()); @@ -119,14 +120,26 @@ NotifyObjectEmitted(*Obj, *L); LoadedObjects.push_back(std::move(Obj)); + return L; } -void MCJIT::addObjectFile(object::OwningBinary Obj) { +std::unique_ptr +MCJIT::LoadObjectFile(object::OwningBinary Obj) +{ std::unique_ptr ObjFile; std::unique_ptr MemBuf; std::tie(ObjFile, MemBuf) = Obj.takeBinary(); - addObjectFile(std::move(ObjFile)); + std::unique_ptr L = LoadObjectFile(std::move(ObjFile)); Buffers.push_back(std::move(MemBuf)); + return L; +} + +void MCJIT::addObjectFile(std::unique_ptr Obj) { + LoadObjectFile(std::move(Obj)); +} + +void MCJIT::addObjectFile(object::OwningBinary Obj) { + LoadObjectFile(std::move(Obj)); } void MCJIT::addArchive(object::OwningBinary A) { @@ -429,6 +442,19 @@ return nullptr; } +GlobalVariable *MCJIT::FindGlobalVariableNamedInModulePtrSet(const char *Name, + bool AllowInternal, + ModulePtrSet::iterator I, + ModulePtrSet::iterator E) { + for (; I != E; ++I) { + GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal); + if (GV && !GV->isDeclaration()) + return GV; + } + return nullptr; +} + + Function *MCJIT::FindFunctionNamed(const char *FnName) { Function *F = FindFunctionNamedInModulePtrSet( FnName, OwnedModules.begin_added(), OwnedModules.end_added()); @@ -441,6 +467,18 @@ return F; } +GlobalVariable *MCJIT::FindGlobalVariableNamed(const char *Name, bool AllowInternal) { + GlobalVariable *GV = FindGlobalVariableNamedInModulePtrSet( + Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added()); + if (!GV) + GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(), + OwnedModules.end_loaded()); + if (!GV) + GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(), + OwnedModules.end_finalized()); + return GV; +} + GenericValue MCJIT::runFunction(Function *F, const std::vector &ArgValues) { assert(F && "Function *F was null at entry to run()");