1 //===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H
11 #define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ExecutionEngine/ExecutionEngine.h"
16 #include "llvm/ExecutionEngine/ObjectCache.h"
17 #include "llvm/ExecutionEngine/ObjectImage.h"
18 #include "llvm/ExecutionEngine/RuntimeDyld.h"
19 #include "llvm/PassManager.h"
25 // This is a helper class that the MCJIT execution engine uses for linking
26 // functions across modules that it owns. It aggregates the memory manager
27 // that is passed in to the MCJIT constructor and defers most functionality
29 class LinkingMemoryManager : public RTDyldMemoryManager {
31 LinkingMemoryManager(MCJIT *Parent, RTDyldMemoryManager *MM)
32 : ParentEngine(Parent), ClientMM(MM) {}
34 virtual uint64_t getSymbolAddress(const std::string &Name);
36 // Functions deferred to client memory manager
37 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
39 return ClientMM->allocateCodeSection(Size, Alignment, SectionID);
42 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
43 unsigned SectionID, bool IsReadOnly) {
44 return ClientMM->allocateDataSection(Size, Alignment,
45 SectionID, IsReadOnly);
48 virtual void registerEHFrames(StringRef SectionData) {
49 ClientMM->registerEHFrames(SectionData);
52 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
53 return ClientMM->finalizeMemory(ErrMsg);
58 OwningPtr<RTDyldMemoryManager> ClientMM;
61 // FIXME: This makes all kinds of horrible assumptions for the time being,
62 // like only having one module, not needing to worry about multi-threading,
63 // blah blah. Purely in get-it-up-and-limping mode for now.
65 class MCJIT : public ExecutionEngine {
66 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
67 bool AllocateGVsWithCode);
78 class MCJITModuleState {
80 MCJITModuleState() : State(ModuleAdded) {}
82 MCJITModuleState & operator=(ModuleState s) { State = s; return *this; }
83 bool hasBeenEmitted() { return State != ModuleAdded; }
84 bool hasBeenLoaded() { return State != ModuleAdded &&
85 State != ModuleEmitted; }
86 bool hasBeenFinalized() { return State == ModuleFinalized; }
94 LinkingMemoryManager MemMgr;
96 SmallVector<JITEventListener*, 2> EventListeners;
98 typedef DenseMap<Module *, MCJITModuleState> ModuleStateMap;
99 ModuleStateMap ModuleStates;
101 typedef DenseMap<Module *, ObjectImage *> LoadedObjectMap;
102 LoadedObjectMap LoadedObjects;
104 // An optional ObjectCache to be notified of compiled objects and used to
105 // perform lookup of pre-compiled code to avoid re-compilation.
106 ObjectCache *ObjCache;
111 /// @name ExecutionEngine interface implementation
113 virtual void addModule(Module *M);
115 /// Sets the object manager that MCJIT should use to avoid compilation.
116 virtual void setObjectCache(ObjectCache *manager);
118 virtual void generateCodeForModule(Module *M);
120 /// finalizeObject - ensure the module is fully processed and is usable.
122 /// It is the user-level function for completing the process of making the
123 /// object usable for execution. It should be called after sections within an
124 /// object have been relocated using mapSectionAddress. When this method is
125 /// called the MCJIT execution engine will reapply relocations for a loaded
127 /// FIXME: Do we really need both of these?
128 virtual void finalizeObject();
129 virtual void finalizeModule(Module *);
130 void finalizeLoadedModules();
132 virtual void *getPointerToBasicBlock(BasicBlock *BB);
134 virtual void *getPointerToFunction(Function *F);
136 virtual void *recompileAndRelinkFunction(Function *F);
138 virtual void freeMachineCodeForFunction(Function *F);
140 virtual GenericValue runFunction(Function *F,
141 const std::vector<GenericValue> &ArgValues);
143 /// getPointerToNamedFunction - This method returns the address of the
144 /// specified function by using the dlsym function call. As such it is only
145 /// useful for resolving library symbols, not code generated symbols.
147 /// If AbortOnFailure is false and no function with the given name is
148 /// found, this function silently returns a null pointer. Otherwise,
149 /// it prints a message to stderr and aborts.
151 virtual void *getPointerToNamedFunction(const std::string &Name,
152 bool AbortOnFailure = true);
154 /// mapSectionAddress - map a section to its target address space value.
155 /// Map the address of a JIT section as returned from the memory manager
156 /// to the address in the target process as the running code will see it.
157 /// This is the address which will be used for relocation resolution.
158 virtual void mapSectionAddress(const void *LocalAddress,
159 uint64_t TargetAddress) {
160 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
162 virtual void RegisterJITEventListener(JITEventListener *L);
163 virtual void UnregisterJITEventListener(JITEventListener *L);
165 // If successful, these function will implicitly finalize all loaded objects.
166 // To get a function address within MCJIT without causing a finalize, use
168 virtual uint64_t getGlobalValueAddress(const std::string &Name);
169 virtual uint64_t getFunctionAddress(const std::string &Name);
172 /// @name (Private) Registration Interfaces
175 static void Register() {
176 MCJITCtor = createJIT;
179 static ExecutionEngine *createJIT(Module *M,
180 std::string *ErrorStr,
181 RTDyldMemoryManager *MemMgr,
187 // This is not directly exposed via the ExecutionEngine API, but it is
188 // used by the LinkingMemoryManager.
189 uint64_t getSymbolAddress(const std::string &Name,
190 bool CheckFunctionsOnly);
193 /// emitObject -- Generate a JITed object in memory from the specified module
194 /// Currently, MCJIT only supports a single module and the module passed to
195 /// this function call is expected to be the contained module. The module
196 /// is passed as a parameter here to prepare for multiple module support in
198 ObjectBufferStream* emitObject(Module *M);
200 void NotifyObjectEmitted(const ObjectImage& Obj);
201 void NotifyFreeingObject(const ObjectImage& Obj);
203 uint64_t getExistingSymbolAddress(const std::string &Name);
204 Module *findModuleForSymbol(const std::string &Name,
205 bool CheckFunctionsOnly);
208 } // End llvm namespace