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,
38 unsigned SectionID, StringRef SectionName) {
39 return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
42 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
43 unsigned SectionID, StringRef SectionName,
45 return ClientMM->allocateDataSection(Size, Alignment,
46 SectionID, SectionName, IsReadOnly);
49 virtual void notifyObjectLoaded(ExecutionEngine *EE,
50 const ObjectImage *Obj) {
51 ClientMM->notifyObjectLoaded(EE, Obj);
54 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
55 ClientMM->registerEHFrames(Addr, LoadAddr, Size);
58 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
59 return ClientMM->finalizeMemory(ErrMsg);
64 OwningPtr<RTDyldMemoryManager> ClientMM;
67 // FIXME: This makes all kinds of horrible assumptions for the time being,
68 // like only having one module, not needing to worry about multi-threading,
69 // blah blah. Purely in get-it-up-and-limping mode for now.
71 class MCJIT : public ExecutionEngine {
72 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
73 bool AllocateGVsWithCode);
84 class MCJITModuleState {
86 MCJITModuleState() : State(ModuleAdded) {}
88 MCJITModuleState & operator=(ModuleState s) { State = s; return *this; }
89 bool hasBeenEmitted() { return State != ModuleAdded; }
90 bool hasBeenLoaded() { return State != ModuleAdded &&
91 State != ModuleEmitted; }
92 bool hasBeenFinalized() { return State == ModuleFinalized; }
100 LinkingMemoryManager MemMgr;
102 SmallVector<JITEventListener*, 2> EventListeners;
104 typedef DenseMap<Module *, MCJITModuleState> ModuleStateMap;
105 ModuleStateMap ModuleStates;
107 typedef DenseMap<Module *, ObjectImage *> LoadedObjectMap;
108 LoadedObjectMap LoadedObjects;
110 // An optional ObjectCache to be notified of compiled objects and used to
111 // perform lookup of pre-compiled code to avoid re-compilation.
112 ObjectCache *ObjCache;
117 /// @name ExecutionEngine interface implementation
119 virtual void addModule(Module *M);
121 /// Sets the object manager that MCJIT should use to avoid compilation.
122 virtual void setObjectCache(ObjectCache *manager);
124 virtual void generateCodeForModule(Module *M);
126 /// finalizeObject - ensure the module is fully processed and is usable.
128 /// It is the user-level function for completing the process of making the
129 /// object usable for execution. It should be called after sections within an
130 /// object have been relocated using mapSectionAddress. When this method is
131 /// called the MCJIT execution engine will reapply relocations for a loaded
133 /// FIXME: Do we really need both of these?
134 virtual void finalizeObject();
135 virtual void finalizeModule(Module *);
136 void finalizeLoadedModules();
138 virtual void *getPointerToBasicBlock(BasicBlock *BB);
140 virtual void *getPointerToFunction(Function *F);
142 virtual void *recompileAndRelinkFunction(Function *F);
144 virtual void freeMachineCodeForFunction(Function *F);
146 virtual GenericValue runFunction(Function *F,
147 const std::vector<GenericValue> &ArgValues);
149 /// getPointerToNamedFunction - This method returns the address of the
150 /// specified function by using the dlsym function call. As such it is only
151 /// useful for resolving library symbols, not code generated symbols.
153 /// If AbortOnFailure is false and no function with the given name is
154 /// found, this function silently returns a null pointer. Otherwise,
155 /// it prints a message to stderr and aborts.
157 virtual void *getPointerToNamedFunction(const std::string &Name,
158 bool AbortOnFailure = true);
160 /// mapSectionAddress - map a section to its target address space value.
161 /// Map the address of a JIT section as returned from the memory manager
162 /// to the address in the target process as the running code will see it.
163 /// This is the address which will be used for relocation resolution.
164 virtual void mapSectionAddress(const void *LocalAddress,
165 uint64_t TargetAddress) {
166 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
168 virtual void RegisterJITEventListener(JITEventListener *L);
169 virtual void UnregisterJITEventListener(JITEventListener *L);
171 // If successful, these function will implicitly finalize all loaded objects.
172 // To get a function address within MCJIT without causing a finalize, use
174 virtual uint64_t getGlobalValueAddress(const std::string &Name);
175 virtual uint64_t getFunctionAddress(const std::string &Name);
178 /// @name (Private) Registration Interfaces
181 static void Register() {
182 MCJITCtor = createJIT;
185 static ExecutionEngine *createJIT(Module *M,
186 std::string *ErrorStr,
187 RTDyldMemoryManager *MemMgr,
193 // This is not directly exposed via the ExecutionEngine API, but it is
194 // used by the LinkingMemoryManager.
195 uint64_t getSymbolAddress(const std::string &Name,
196 bool CheckFunctionsOnly);
199 /// emitObject -- Generate a JITed object in memory from the specified module
200 /// Currently, MCJIT only supports a single module and the module passed to
201 /// this function call is expected to be the contained module. The module
202 /// is passed as a parameter here to prepare for multiple module support in
204 ObjectBufferStream* emitObject(Module *M);
206 void NotifyObjectEmitted(const ObjectImage& Obj);
207 void NotifyFreeingObject(const ObjectImage& Obj);
209 uint64_t getExistingSymbolAddress(const std::string &Name);
210 Module *findModuleForSymbol(const std::string &Name,
211 bool CheckFunctionsOnly);
214 } // End llvm namespace