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 registerEHFrames(StringRef SectionData) {
50 ClientMM->registerEHFrames(SectionData);
53 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
54 return ClientMM->finalizeMemory(ErrMsg);
59 OwningPtr<RTDyldMemoryManager> ClientMM;
62 // FIXME: This makes all kinds of horrible assumptions for the time being,
63 // like only having one module, not needing to worry about multi-threading,
64 // blah blah. Purely in get-it-up-and-limping mode for now.
66 class MCJIT : public ExecutionEngine {
67 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
68 bool AllocateGVsWithCode);
79 class MCJITModuleState {
81 MCJITModuleState() : State(ModuleAdded) {}
83 MCJITModuleState & operator=(ModuleState s) { State = s; return *this; }
84 bool hasBeenEmitted() { return State != ModuleAdded; }
85 bool hasBeenLoaded() { return State != ModuleAdded &&
86 State != ModuleEmitted; }
87 bool hasBeenFinalized() { return State == ModuleFinalized; }
95 LinkingMemoryManager MemMgr;
97 SmallVector<JITEventListener*, 2> EventListeners;
99 typedef DenseMap<Module *, MCJITModuleState> ModuleStateMap;
100 ModuleStateMap ModuleStates;
102 typedef DenseMap<Module *, ObjectImage *> LoadedObjectMap;
103 LoadedObjectMap LoadedObjects;
105 // An optional ObjectCache to be notified of compiled objects and used to
106 // perform lookup of pre-compiled code to avoid re-compilation.
107 ObjectCache *ObjCache;
112 /// @name ExecutionEngine interface implementation
114 virtual void addModule(Module *M);
116 /// Sets the object manager that MCJIT should use to avoid compilation.
117 virtual void setObjectCache(ObjectCache *manager);
119 virtual void generateCodeForModule(Module *M);
121 /// finalizeObject - ensure the module is fully processed and is usable.
123 /// It is the user-level function for completing the process of making the
124 /// object usable for execution. It should be called after sections within an
125 /// object have been relocated using mapSectionAddress. When this method is
126 /// called the MCJIT execution engine will reapply relocations for a loaded
128 /// FIXME: Do we really need both of these?
129 virtual void finalizeObject();
130 virtual void finalizeModule(Module *);
131 void finalizeLoadedModules();
133 virtual void *getPointerToBasicBlock(BasicBlock *BB);
135 virtual void *getPointerToFunction(Function *F);
137 virtual void *recompileAndRelinkFunction(Function *F);
139 virtual void freeMachineCodeForFunction(Function *F);
141 virtual GenericValue runFunction(Function *F,
142 const std::vector<GenericValue> &ArgValues);
144 /// getPointerToNamedFunction - This method returns the address of the
145 /// specified function by using the dlsym function call. As such it is only
146 /// useful for resolving library symbols, not code generated symbols.
148 /// If AbortOnFailure is false and no function with the given name is
149 /// found, this function silently returns a null pointer. Otherwise,
150 /// it prints a message to stderr and aborts.
152 virtual void *getPointerToNamedFunction(const std::string &Name,
153 bool AbortOnFailure = true);
155 /// mapSectionAddress - map a section to its target address space value.
156 /// Map the address of a JIT section as returned from the memory manager
157 /// to the address in the target process as the running code will see it.
158 /// This is the address which will be used for relocation resolution.
159 virtual void mapSectionAddress(const void *LocalAddress,
160 uint64_t TargetAddress) {
161 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
163 virtual void RegisterJITEventListener(JITEventListener *L);
164 virtual void UnregisterJITEventListener(JITEventListener *L);
166 // If successful, these function will implicitly finalize all loaded objects.
167 // To get a function address within MCJIT without causing a finalize, use
169 virtual uint64_t getGlobalValueAddress(const std::string &Name);
170 virtual uint64_t getFunctionAddress(const std::string &Name);
173 /// @name (Private) Registration Interfaces
176 static void Register() {
177 MCJITCtor = createJIT;
180 static ExecutionEngine *createJIT(Module *M,
181 std::string *ErrorStr,
182 RTDyldMemoryManager *MemMgr,
188 // This is not directly exposed via the ExecutionEngine API, but it is
189 // used by the LinkingMemoryManager.
190 uint64_t getSymbolAddress(const std::string &Name,
191 bool CheckFunctionsOnly);
194 /// emitObject -- Generate a JITed object in memory from the specified module
195 /// Currently, MCJIT only supports a single module and the module passed to
196 /// this function call is expected to be the contained module. The module
197 /// is passed as a parameter here to prepare for multiple module support in
199 ObjectBufferStream* emitObject(Module *M);
201 void NotifyObjectEmitted(const ObjectImage& Obj);
202 void NotifyFreeingObject(const ObjectImage& Obj);
204 uint64_t getExistingSymbolAddress(const std::string &Name);
205 Module *findModuleForSymbol(const std::string &Name,
206 bool CheckFunctionsOnly);
209 } // End llvm namespace