2010-11-18 00:06:43 +08:00
|
|
|
//===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H
|
|
|
|
#define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
|
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2012-11-07 02:51:59 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-11-18 00:06:43 +08:00
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
2013-04-26 05:02:36 +08:00
|
|
|
#include "llvm/ExecutionEngine/ObjectCache.h"
|
2013-10-01 09:47:35 +08:00
|
|
|
#include "llvm/ExecutionEngine/ObjectImage.h"
|
2011-03-22 09:06:42 +08:00
|
|
|
#include "llvm/ExecutionEngine/RuntimeDyld.h"
|
2012-12-04 15:12:27 +08:00
|
|
|
#include "llvm/PassManager.h"
|
2010-11-18 00:06:43 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
class MCJIT;
|
|
|
|
|
|
|
|
// This is a helper class that the MCJIT execution engine uses for linking
|
|
|
|
// functions across modules that it owns. It aggregates the memory manager
|
|
|
|
// that is passed in to the MCJIT constructor and defers most functionality
|
|
|
|
// to that object.
|
|
|
|
class LinkingMemoryManager : public RTDyldMemoryManager {
|
|
|
|
public:
|
|
|
|
LinkingMemoryManager(MCJIT *Parent, RTDyldMemoryManager *MM)
|
|
|
|
: ParentEngine(Parent), ClientMM(MM) {}
|
|
|
|
|
|
|
|
virtual uint64_t getSymbolAddress(const std::string &Name);
|
|
|
|
|
|
|
|
// Functions deferred to client memory manager
|
|
|
|
virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned SectionID, StringRef SectionName) {
|
|
|
|
return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
|
2013-10-01 09:47:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned SectionID, StringRef SectionName,
|
|
|
|
bool IsReadOnly) {
|
2013-10-01 09:47:35 +08:00
|
|
|
return ClientMM->allocateDataSection(Size, Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
SectionID, SectionName, IsReadOnly);
|
2013-10-01 09:47:35 +08:00
|
|
|
}
|
|
|
|
|
2013-10-04 08:49:38 +08:00
|
|
|
virtual void notifyObjectLoaded(ExecutionEngine *EE,
|
|
|
|
const ObjectImage *Obj) {
|
|
|
|
ClientMM->notifyObjectLoaded(EE, Obj);
|
|
|
|
}
|
|
|
|
|
2013-10-12 05:25:48 +08:00
|
|
|
virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
|
|
|
|
ClientMM->registerEHFrames(Addr, LoadAddr, Size);
|
2013-10-01 09:47:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool finalizeMemory(std::string *ErrMsg = 0) {
|
|
|
|
return ClientMM->finalizeMemory(ErrMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MCJIT *ParentEngine;
|
|
|
|
OwningPtr<RTDyldMemoryManager> ClientMM;
|
|
|
|
};
|
2012-10-03 05:18:39 +08:00
|
|
|
|
2011-03-19 06:48:41 +08:00
|
|
|
// FIXME: This makes all kinds of horrible assumptions for the time being,
|
|
|
|
// like only having one module, not needing to worry about multi-threading,
|
|
|
|
// blah blah. Purely in get-it-up-and-limping mode for now.
|
|
|
|
|
2010-11-18 00:06:43 +08:00
|
|
|
class MCJIT : public ExecutionEngine {
|
2012-08-21 23:42:49 +08:00
|
|
|
MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
|
|
|
|
bool AllocateGVsWithCode);
|
2011-03-19 06:48:41 +08:00
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
enum ModuleState {
|
|
|
|
ModuleAdded,
|
|
|
|
ModuleEmitted,
|
|
|
|
ModuleLoading,
|
|
|
|
ModuleLoaded,
|
|
|
|
ModuleFinalizing,
|
|
|
|
ModuleFinalized
|
|
|
|
};
|
|
|
|
|
|
|
|
class MCJITModuleState {
|
|
|
|
public:
|
|
|
|
MCJITModuleState() : State(ModuleAdded) {}
|
|
|
|
|
|
|
|
MCJITModuleState & operator=(ModuleState s) { State = s; return *this; }
|
|
|
|
bool hasBeenEmitted() { return State != ModuleAdded; }
|
|
|
|
bool hasBeenLoaded() { return State != ModuleAdded &&
|
|
|
|
State != ModuleEmitted; }
|
|
|
|
bool hasBeenFinalized() { return State == ModuleFinalized; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ModuleState State;
|
|
|
|
};
|
|
|
|
|
2011-03-19 06:48:41 +08:00
|
|
|
TargetMachine *TM;
|
|
|
|
MCContext *Ctx;
|
2013-10-01 09:47:35 +08:00
|
|
|
LinkingMemoryManager MemMgr;
|
2012-08-08 02:33:00 +08:00
|
|
|
RuntimeDyld Dyld;
|
2012-11-07 02:51:59 +08:00
|
|
|
SmallVector<JITEventListener*, 2> EventListeners;
|
2011-03-19 06:48:41 +08:00
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
typedef DenseMap<Module *, MCJITModuleState> ModuleStateMap;
|
|
|
|
ModuleStateMap ModuleStates;
|
|
|
|
|
|
|
|
typedef DenseMap<Module *, ObjectImage *> LoadedObjectMap;
|
|
|
|
LoadedObjectMap LoadedObjects;
|
2011-03-19 06:48:41 +08:00
|
|
|
|
2013-04-26 05:02:36 +08:00
|
|
|
// An optional ObjectCache to be notified of compiled objects and used to
|
|
|
|
// perform lookup of pre-compiled code to avoid re-compilation.
|
|
|
|
ObjectCache *ObjCache;
|
|
|
|
|
2010-11-18 00:06:43 +08:00
|
|
|
public:
|
|
|
|
~MCJIT();
|
|
|
|
|
|
|
|
/// @name ExecutionEngine interface implementation
|
|
|
|
/// @{
|
2013-10-01 09:47:35 +08:00
|
|
|
virtual void addModule(Module *M);
|
2010-11-18 00:06:43 +08:00
|
|
|
|
2013-04-26 05:02:36 +08:00
|
|
|
/// Sets the object manager that MCJIT should use to avoid compilation.
|
|
|
|
virtual void setObjectCache(ObjectCache *manager);
|
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
virtual void generateCodeForModule(Module *M);
|
|
|
|
|
2013-05-17 18:01:46 +08:00
|
|
|
/// finalizeObject - ensure the module is fully processed and is usable.
|
|
|
|
///
|
|
|
|
/// It is the user-level function for completing the process of making the
|
|
|
|
/// object usable for execution. It should be called after sections within an
|
|
|
|
/// object have been relocated using mapSectionAddress. When this method is
|
|
|
|
/// called the MCJIT execution engine will reapply relocations for a loaded
|
|
|
|
/// object.
|
2013-10-01 09:47:35 +08:00
|
|
|
/// FIXME: Do we really need both of these?
|
2012-11-06 04:57:16 +08:00
|
|
|
virtual void finalizeObject();
|
2013-10-01 09:47:35 +08:00
|
|
|
virtual void finalizeModule(Module *);
|
|
|
|
void finalizeLoadedModules();
|
2012-11-06 04:57:16 +08:00
|
|
|
|
2010-11-18 00:06:43 +08:00
|
|
|
virtual void *getPointerToBasicBlock(BasicBlock *BB);
|
|
|
|
|
|
|
|
virtual void *getPointerToFunction(Function *F);
|
|
|
|
|
|
|
|
virtual void *recompileAndRelinkFunction(Function *F);
|
|
|
|
|
|
|
|
virtual void freeMachineCodeForFunction(Function *F);
|
|
|
|
|
|
|
|
virtual GenericValue runFunction(Function *F,
|
|
|
|
const std::vector<GenericValue> &ArgValues);
|
|
|
|
|
2011-03-23 02:05:27 +08:00
|
|
|
/// getPointerToNamedFunction - This method returns the address of the
|
|
|
|
/// specified function by using the dlsym function call. As such it is only
|
|
|
|
/// useful for resolving library symbols, not code generated symbols.
|
|
|
|
///
|
|
|
|
/// If AbortOnFailure is false and no function with the given name is
|
|
|
|
/// found, this function silently returns a null pointer. Otherwise,
|
|
|
|
/// it prints a message to stderr and aborts.
|
|
|
|
///
|
2012-01-06 05:16:14 +08:00
|
|
|
virtual void *getPointerToNamedFunction(const std::string &Name,
|
|
|
|
bool AbortOnFailure = true);
|
2012-03-29 05:46:36 +08:00
|
|
|
|
2012-01-17 07:50:55 +08:00
|
|
|
/// mapSectionAddress - map a section to its target address space value.
|
|
|
|
/// Map the address of a JIT section as returned from the memory manager
|
|
|
|
/// to the address in the target process as the running code will see it.
|
|
|
|
/// This is the address which will be used for relocation resolution.
|
2012-09-14 05:50:06 +08:00
|
|
|
virtual void mapSectionAddress(const void *LocalAddress,
|
|
|
|
uint64_t TargetAddress) {
|
2012-01-17 07:50:55 +08:00
|
|
|
Dyld.mapSectionAddress(LocalAddress, TargetAddress);
|
|
|
|
}
|
2012-11-07 02:51:59 +08:00
|
|
|
virtual void RegisterJITEventListener(JITEventListener *L);
|
|
|
|
virtual void UnregisterJITEventListener(JITEventListener *L);
|
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
// If successful, these function will implicitly finalize all loaded objects.
|
|
|
|
// To get a function address within MCJIT without causing a finalize, use
|
|
|
|
// getSymbolAddress.
|
|
|
|
virtual uint64_t getGlobalValueAddress(const std::string &Name);
|
|
|
|
virtual uint64_t getFunctionAddress(const std::string &Name);
|
|
|
|
|
2010-11-18 00:06:43 +08:00
|
|
|
/// @}
|
|
|
|
/// @name (Private) Registration Interfaces
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
static void Register() {
|
|
|
|
MCJITCtor = createJIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExecutionEngine *createJIT(Module *M,
|
|
|
|
std::string *ErrorStr,
|
2013-05-15 03:29:00 +08:00
|
|
|
RTDyldMemoryManager *MemMgr,
|
2010-11-18 00:06:43 +08:00
|
|
|
bool GVsWithCode,
|
2011-05-14 05:51:29 +08:00
|
|
|
TargetMachine *TM);
|
2010-11-18 00:06:43 +08:00
|
|
|
|
|
|
|
// @}
|
2012-08-08 02:33:00 +08:00
|
|
|
|
2013-10-01 09:47:35 +08:00
|
|
|
// This is not directly exposed via the ExecutionEngine API, but it is
|
|
|
|
// used by the LinkingMemoryManager.
|
|
|
|
uint64_t getSymbolAddress(const std::string &Name,
|
|
|
|
bool CheckFunctionsOnly);
|
|
|
|
|
2012-08-08 02:33:00 +08:00
|
|
|
protected:
|
|
|
|
/// 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. The module
|
|
|
|
/// is passed as a parameter here to prepare for multiple module support in
|
|
|
|
/// the future.
|
2013-04-26 05:02:36 +08:00
|
|
|
ObjectBufferStream* emitObject(Module *M);
|
|
|
|
|
2012-11-07 02:51:59 +08:00
|
|
|
void NotifyObjectEmitted(const ObjectImage& Obj);
|
|
|
|
void NotifyFreeingObject(const ObjectImage& Obj);
|
2013-10-01 09:47:35 +08:00
|
|
|
|
|
|
|
uint64_t getExistingSymbolAddress(const std::string &Name);
|
|
|
|
Module *findModuleForSymbol(const std::string &Name,
|
|
|
|
bool CheckFunctionsOnly);
|
2010-11-18 00:06:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|