32 void ObjectCache::anchor() {}
36 static struct RegisterJIT {
47 std::string *ErrorStr,
48 std::shared_ptr<MCJITMemoryManager> MemMgr,
49 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
50 std::unique_ptr<TargetMachine>
TM) {
56 if (!MemMgr || !Resolver) {
57 auto RTDyldMM = std::make_shared<SectionMemoryManager>();
64 return new MCJIT(std::move(M), std::move(TM), std::move(MemMgr),
68 MCJIT::MCJIT(std::unique_ptr<Module> M, std::unique_ptr<TargetMachine> tm,
69 std::shared_ptr<MCJITMemoryManager> MemMgr,
70 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver)
72 MemMgr(std::move(MemMgr)), Resolver(*this, std::move(Resolver)),
73 Dyld(*this->MemMgr, this->Resolver), ObjCache(nullptr) {
84 std::unique_ptr<Module> First = std::move(
Modules[0]);
87 OwnedModules.addModule(std::move(First));
97 for (
auto &Obj : LoadedObjects)
106 OwnedModules.addModule(std::move(M));
111 return OwnedModules.removeModule(M);
115 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L = Dyld.
loadObject(*Obj);
121 LoadedObjects.push_back(std::move(Obj));
125 std::unique_ptr<object::ObjectFile> ObjFile;
126 std::unique_ptr<MemoryBuffer> MemBuf;
129 Buffers.push_back(std::move(MemBuf));
133 Archives.push_back(std::move(A));
164 std::unique_ptr<MemoryBuffer> CompiledObjBuffer(
176 return CompiledObjBuffer;
184 assert(OwnedModules.ownsModule(M) &&
185 "MCJIT::generateCodeForModule: Unknown module.");
188 if (OwnedModules.hasModuleBeenLoaded(M))
191 std::unique_ptr<MemoryBuffer> ObjectToLoad;
201 assert(ObjectToLoad &&
"Compilation did not produce an object.");
208 std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
216 Buffers.push_back(std::move(ObjectToLoad));
217 LoadedObjects.push_back(std::move(*LoadedObject));
219 OwnedModules.markModuleAsLoaded(M);
228 OwnedModules.markAllLoadedModulesAsFinalized();
234 MemMgr->finalizeMemory();
244 for (
auto M : OwnedModules.added())
247 for (
auto M : ModsToAdd)
257 assert(OwnedModules.ownsModule(M) &&
"MCJIT::finalizeModule: Unknown module.");
260 if (!OwnedModules.hasModuleBeenLoaded(M))
272 reinterpret_cast<uintptr_t>(Addr)),
279 bool CheckFunctionsOnly) {
284 E = OwnedModules.end_added();
290 if (!CheckFunctionsOnly) {
302 bool CheckFunctionsOnly) {
307 bool CheckFunctionsOnly) {
324 std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.
get();
325 if (ChildBin->isObject()) {
326 std::unique_ptr<object::ObjectFile> OF(
327 static_cast<object::ObjectFile *>(ChildBin.release()));
349 auto Addr =
static_cast<uint64_t
>(
379 TM->getNameWithPrefix(Name, F, Mang);
389 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
392 if (HasBeenAddedButNotLoaded)
394 else if (!OwnedModules.hasModuleBeenLoaded(M)) {
409 void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
410 bool isDtors, ModulePtrSet::iterator
I, ModulePtrSet::iterator E) {
411 for (; I != E; ++
I) {
418 runStaticConstructorsDestructorsInModulePtrSet(
419 isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
420 runStaticConstructorsDestructorsInModulePtrSet(
421 isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
422 runStaticConstructorsDestructorsInModulePtrSet(
423 isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
426 Function *MCJIT::FindFunctionNamedInModulePtrSet(
const char *FnName,
427 ModulePtrSet::iterator I,
428 ModulePtrSet::iterator E) {
429 for (; I != E; ++
I) {
439 ModulePtrSet::iterator I,
440 ModulePtrSet::iterator E) {
441 for (; I != E; ++
I) {
442 GlobalVariable *GV = (*I)->getGlobalVariable(Name, AllowInternal);
451 Function *F = FindFunctionNamedInModulePtrSet(
452 FnName, OwnedModules.begin_added(), OwnedModules.end_added());
454 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
455 OwnedModules.end_loaded());
457 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
458 OwnedModules.end_finalized());
464 Name, AllowInternal, OwnedModules.begin_added(), OwnedModules.end_added());
466 GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_loaded(),
467 OwnedModules.end_loaded());
469 GV = FindGlobalVariableNamedInModulePtrSet(Name, AllowInternal, OwnedModules.begin_finalized(),
470 OwnedModules.end_finalized());
475 assert(F &&
"Function *F was null at entry to run()");
478 assert(FPtr &&
"Pointer to fn's code was null after getPointerToFunction");
484 "Wrong number of arguments passed into function!");
486 "This doesn't support passing arguments through varargs (yet)!");
491 switch (ArgValues.
size()) {
496 int (*PF)(
int,
char **,
const char **) =
497 (
int(*)(
int,
char **,
const char **))(
intptr_t)FPtr;
502 (
char **)
GVTOP(ArgValues[1]),
503 (
const char **)
GVTOP(ArgValues[2])));
515 (
char **)
GVTOP(ArgValues[1])));
532 if (ArgValues.
empty()) {
537 unsigned BitWidth = cast<IntegerType>(RetTy)->
getBitWidth();
540 else if (BitWidth <= 8)
542 else if (BitWidth <= 16)
544 else if (BitWidth <= 32)
546 else if (BitWidth <= 64)
576 reinterpret_cast<void*
>(
577 static_cast<uintptr_t
>(Resolver.findSymbol(Name).getAddress()));
587 if (AbortOnFailure) {
589 "' which could not be resolved!");
598 EventListeners.push_back(L);
605 auto I = std::find(EventListeners.rbegin(), EventListeners.rend(), L);
606 if (I != EventListeners.rend()) {
608 EventListeners.pop_back();
615 MemMgr->notifyObjectLoaded(
this, Obj);
616 for (
unsigned I = 0, S = EventListeners.size(); I < S; ++
I) {
617 EventListeners[
I]->NotifyObjectEmitted(Obj, L);
624 L->NotifyFreeingObject(Obj);
629 auto Result = ParentEngine.
findSymbol(Name,
false);
632 if (!Result && Name[0] ==
'_')
633 Result = ParentEngine.
findSymbol(Name.substr(1),
false);
638 return ClientResolver->findSymbol(Name);
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
Information about the loaded object.
void setObjectCache(ObjectCache *manager) override
Sets the object manager that MCJIT should use to avoid compilation.
void push_back(const T &Elt)
std::error_code getError() const
Represents either an error or a value T.
virtual Function * FindFunctionNamed(const char *FnName) override
FindFunctionNamed - Search all of the active modules to find the function that defines FnName...
std::unique_ptr< LoadedObjectInfo > loadObject(const object::ObjectFile &O)
Add the referenced object file to the list of objects to be loaded and relocated. ...
void registerEHFrames()
Register any EH frame sections that have been loaded but not previously registered with the memory ma...
ErrorOr< std::unique_ptr< Binary > > getAsBinary(LLVMContext *Context=nullptr) const
A Module instance is used to store all the information related to an LLVM module. ...
unsigned getNumParams() const
getNumParams - Return the number of fixed parameters this function type requires. ...
2: 32-bit floating point type
virtual std::unique_ptr< MemoryBuffer > getObject(const Module *M)=0
Returns a pointer to a newly allocated MemoryBuffer that contains the object which corresponds with M...
void * getPointerToGlobalIfAvailable(StringRef S)
getPointerToGlobalIfAvailable - This returns the address of the specified global value if it is has a...
sys::Mutex lock
lock - This lock protects the ExecutionEngine and MCJIT classes.
uint64_t updateGlobalMapping(const GlobalValue *GV, void *Addr)
updateGlobalMapping - Replace an existing mapping for GV with a new address.
JITEventListener - Abstract interface for use by the JIT to notify clients about significant events d...
StringRef getErrorString()
uint64_t getAddress() const
static JITEventListener * createGDBRegistrationListener()
This class is the base class for all object file types.
bool hasAvailableExternallyLinkage() const
void setDataLayout(StringRef Desc)
Set the data layout.
A raw_ostream that writes to an SmallVector or SmallString.
void generateCodeForModule(Module *M) override
generateCodeForModule - Run code generation for the specified module and load it into memory...
4: 80-bit floating point type (X87)
void UnregisterJITEventListener(JITEventListener *L) override
void deregisterEHFrames()
SymbolInfo getSymbol(StringRef Name) const
Get the target address and flags for the named symbol.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
virtual void runStaticConstructorsDestructors(bool isDtors)
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
void runStaticConstructorsDestructors(bool isDtors) override
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
uint64_t getGlobalValueAddress(const std::string &Name) override
getGlobalValueAddress - Return the address of the specified global value.
FunctionType - Class to represent function types.
virtual void finalizeModule(Module *)
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name, bool CheckFunctionsOnly)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
TypeID getTypeID() const
getTypeID - Return the type id for the type.
void * getPointerToFunction(Function *F) override
getPointerToFunction - The different EE's represent function bodies in different ways.
size_t size() const
size - Get the array size.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
10: Arbitrary bit width integers
PassManager manages ModulePassManagers.
static ExecutionEngine * createJIT(std::unique_ptr< Module > M, std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MemMgr, std::shared_ptr< RuntimeDyld::SymbolResolver > Resolver, std::unique_ptr< TargetMachine > TM)
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
Type * getParamType(unsigned i) const
Parameter type accessors.
void addModule(std::unique_ptr< Module > M) override
Add a Module to the list of modules that we can JIT from.
The instances of the Type class are immutable: once they are created, they are never changed...
child_iterator child_end() const
bool removeModule(Module *M) override
removeModule - Remove a Module from the list of modules.
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
bool isSymbolSearchingDisabled() const
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
6: 128-bit floating point type (two 64-bits, PowerPC)
bool empty() const
empty - Check if the array is empty.
bool getVerifyModules() const
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
void setDataLayout(const DataLayout *Val)
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
virtual GlobalVariable * FindGlobalVariableNamed(const char *Name, bool AllowInternal=false) override
FindGlobalVariableNamed - Search all of the active modules to find the global variable that defines N...
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
virtual void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj)=0
notifyObjectCompiled - Provides a pointer to compiled code for Module M.
child_iterator findSym(StringRef name) const
void * GVTOP(const GenericValue &GV)
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
void addArchive(object::OwningBinary< object::Archive > O) override
addArchive - Add an Archive to the execution engine.
SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
bool hasExternalWeakLinkage() const
RuntimeDyld::SymbolInfo findExistingSymbol(const std::string &Name)
Module.h This file contains the declarations for the Module class.
uint64_t getSymbolAddress(const std::string &Name, bool CheckFunctionsOnly)
void RegisterJITEventListener(JITEventListener *L) override
Registers a listener to be called back on various events within the JIT.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
GenericValue PTOGV(void *P)
Class for arbitrary precision integers.
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
Module * findModuleForSymbol(const std::string &Name, bool CheckFunctionsOnly)
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
runFunction - Execute the specified function with the specified arguments, and return the result...
std::unique_ptr< MemoryBuffer > emitObject(Module *M)
emitObject – Generate a JITed object in memory from the specified module Currently, MCJIT only supports a single module and the module passed to this function call is expected to be the contained module.
SmallVector< std::unique_ptr< Module >, 1 > Modules
The list of Modules that we are JIT'ing from.
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override
This method returns the address of the specified function or variable.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
FunctionType * getFunctionType() const
SmallVector-backed MemoryBuffer instance.
Information about a named symbol.
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
void resolveRelocations()
Resolve the relocations for all symbols we currently know about.
3: 64-bit floating point type
Type * getReturnType() const
FunctionCreator LazyFunctionCreator
LazyFunctionCreator - If an unknown function is needed, this function pointer is invoked to create it...
Module * getParent()
Get the module that this global value is contained inside of...
void NotifyFreeingObject(const object::ObjectFile &Obj)
static ErrorOr< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
StringRef - Represent a constant reference to a string, i.e.
void NotifyObjectEmitted(const object::ObjectFile &Obj, const RuntimeDyld::LoadedObjectInfo &L)
void finalizeLoadedModules()
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
5: 128-bit floating point type (112-bit mantissa)
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.