blob: ebf51d48bd34e59807beb24ffa56d0b1d29a9c4c [file] [log] [blame]
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +00001//===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H
11#define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
12
Andrew Kaylorea395922013-10-01 01:47:35 +000013#include "llvm/ADT/DenseMap.h"
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +000014#include "llvm/ADT/SmallVector.h"
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000015#include "llvm/ExecutionEngine/ExecutionEngine.h"
Andrew Kaylorced4e8f2013-04-25 21:02:36 +000016#include "llvm/ExecutionEngine/ObjectCache.h"
Andrew Kaylorea395922013-10-01 01:47:35 +000017#include "llvm/ExecutionEngine/ObjectImage.h"
Jim Grosbach348a5482011-03-22 01:06:42 +000018#include "llvm/ExecutionEngine/RuntimeDyld.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000019#include "llvm/PassManager.h"
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000020
21namespace llvm {
22
Andrew Kaylorea395922013-10-01 01:47:35 +000023class MCJIT;
24
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
28// to that object.
29class LinkingMemoryManager : public RTDyldMemoryManager {
30public:
31 LinkingMemoryManager(MCJIT *Parent, RTDyldMemoryManager *MM)
32 : ParentEngine(Parent), ClientMM(MM) {}
33
34 virtual uint64_t getSymbolAddress(const std::string &Name);
35
36 // Functions deferred to client memory manager
37 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000038 unsigned SectionID, StringRef SectionName) {
39 return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Andrew Kaylorea395922013-10-01 01:47:35 +000040 }
41
42 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000043 unsigned SectionID, StringRef SectionName,
44 bool IsReadOnly) {
Andrew Kaylorea395922013-10-01 01:47:35 +000045 return ClientMM->allocateDataSection(Size, Alignment,
Filip Pizlo7aa695e02013-10-02 00:59:25 +000046 SectionID, SectionName, IsReadOnly);
Andrew Kaylorea395922013-10-01 01:47:35 +000047 }
48
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +000049 virtual void notifyObjectLoaded(ExecutionEngine *EE,
50 const ObjectImage *Obj) {
51 ClientMM->notifyObjectLoaded(EE, Obj);
52 }
53
Andrew Kaylor7bb13442013-10-11 21:25:48 +000054 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
55 ClientMM->registerEHFrames(Addr, LoadAddr, Size);
Andrew Kaylorea395922013-10-01 01:47:35 +000056 }
57
Andrew Kaylorc442a762013-10-16 00:14:21 +000058 virtual void deregisterEHFrames(uint8_t *Addr,
59 uint64_t LoadAddr,
60 size_t Size) {
61 ClientMM->deregisterEHFrames(Addr, LoadAddr, Size);
62 }
63
Andrew Kaylorea395922013-10-01 01:47:35 +000064 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
65 return ClientMM->finalizeMemory(ErrMsg);
66 }
67
68private:
69 MCJIT *ParentEngine;
70 OwningPtr<RTDyldMemoryManager> ClientMM;
71};
Andrew Kayloradc70562012-10-02 21:18:39 +000072
Jim Grosbach7b162492011-03-18 22:48:41 +000073// FIXME: This makes all kinds of horrible assumptions for the time being,
74// like only having one module, not needing to worry about multi-threading,
75// blah blah. Purely in get-it-up-and-limping mode for now.
76
Yaron Kerenfb955822013-10-19 09:03:20 +000077// About Module states:
78//
79// The purpose of the "added" state is having modules in standby. (added=known
80// but not compiled). The idea is that you can add a module to provide function
81// definitions but if nothing in that module is referenced by a module in which
82// a function is executed (note the wording here because itÂ’s not exactly the
83// ideal case) then the module never gets compiled. This is sort of lazy
84// compilation.
85//
86// The purpose of the "loaded" state (loaded=compiled and required sections
87// copied into local memory but not yet ready for execution) is to have an
88// intermediate state wherein clients can remap the addresses of sections, using
89// MCJIT::mapSectionAddress, (in preparation for later copying to a new location
90// or an external process) before relocations and page permissions are applied.
91//
92// It might not be obvious at first glance, but the "remote-mcjit" case in the
93// lli tool does this. In that case, the intermediate action is taken by the
94// RemoteMemoryManager in response to the notifyObjectLoaded function being
95// called.
96
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000097class MCJIT : public ExecutionEngine {
Jim Grosbachbea67532012-08-21 15:42:49 +000098 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
99 bool AllocateGVsWithCode);
Jim Grosbach7b162492011-03-18 22:48:41 +0000100
Andrew Kaylorea395922013-10-01 01:47:35 +0000101 enum ModuleState {
102 ModuleAdded,
103 ModuleEmitted,
104 ModuleLoading,
105 ModuleLoaded,
106 ModuleFinalizing,
107 ModuleFinalized
108 };
109
110 class MCJITModuleState {
111 public:
112 MCJITModuleState() : State(ModuleAdded) {}
113
114 MCJITModuleState & operator=(ModuleState s) { State = s; return *this; }
115 bool hasBeenEmitted() { return State != ModuleAdded; }
116 bool hasBeenLoaded() { return State != ModuleAdded &&
117 State != ModuleEmitted; }
118 bool hasBeenFinalized() { return State == ModuleFinalized; }
119
120 private:
121 ModuleState State;
122 };
123
Jim Grosbach7b162492011-03-18 22:48:41 +0000124 TargetMachine *TM;
125 MCContext *Ctx;
Andrew Kaylorea395922013-10-01 01:47:35 +0000126 LinkingMemoryManager MemMgr;
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000127 RuntimeDyld Dyld;
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000128 SmallVector<JITEventListener*, 2> EventListeners;
Jim Grosbach7b162492011-03-18 22:48:41 +0000129
Andrew Kaylorea395922013-10-01 01:47:35 +0000130 typedef DenseMap<Module *, MCJITModuleState> ModuleStateMap;
131 ModuleStateMap ModuleStates;
132
133 typedef DenseMap<Module *, ObjectImage *> LoadedObjectMap;
134 LoadedObjectMap LoadedObjects;
Jim Grosbach7b162492011-03-18 22:48:41 +0000135
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000136 // An optional ObjectCache to be notified of compiled objects and used to
137 // perform lookup of pre-compiled code to avoid re-compilation.
138 ObjectCache *ObjCache;
139
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000140public:
141 ~MCJIT();
142
143 /// @name ExecutionEngine interface implementation
144 /// @{
Andrew Kaylorea395922013-10-01 01:47:35 +0000145 virtual void addModule(Module *M);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000146
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000147 /// Sets the object manager that MCJIT should use to avoid compilation.
148 virtual void setObjectCache(ObjectCache *manager);
149
Andrew Kaylorea395922013-10-01 01:47:35 +0000150 virtual void generateCodeForModule(Module *M);
151
David Tweed2e7efed2013-05-17 10:01:46 +0000152 /// finalizeObject - ensure the module is fully processed and is usable.
153 ///
154 /// It is the user-level function for completing the process of making the
155 /// object usable for execution. It should be called after sections within an
156 /// object have been relocated using mapSectionAddress. When this method is
157 /// called the MCJIT execution engine will reapply relocations for a loaded
158 /// object.
Yaron Kerenfb955822013-10-19 09:03:20 +0000159 /// Is it OK to finalize a set of modules, add modules and finalize again.
Andrew Kaylorea395922013-10-01 01:47:35 +0000160 /// FIXME: Do we really need both of these?
Andrew Kaylora714efc2012-11-05 20:57:16 +0000161 virtual void finalizeObject();
Andrew Kaylorea395922013-10-01 01:47:35 +0000162 virtual void finalizeModule(Module *);
163 void finalizeLoadedModules();
Andrew Kaylora714efc2012-11-05 20:57:16 +0000164
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000165 virtual void *getPointerToBasicBlock(BasicBlock *BB);
166
167 virtual void *getPointerToFunction(Function *F);
168
169 virtual void *recompileAndRelinkFunction(Function *F);
170
171 virtual void freeMachineCodeForFunction(Function *F);
172
173 virtual GenericValue runFunction(Function *F,
174 const std::vector<GenericValue> &ArgValues);
175
Jim Grosbachd5274402011-03-22 18:05:27 +0000176 /// getPointerToNamedFunction - This method returns the address of the
177 /// specified function by using the dlsym function call. As such it is only
178 /// useful for resolving library symbols, not code generated symbols.
179 ///
180 /// If AbortOnFailure is false and no function with the given name is
181 /// found, this function silently returns a null pointer. Otherwise,
182 /// it prints a message to stderr and aborts.
183 ///
Danil Malyshev7e325782012-01-05 21:16:14 +0000184 virtual void *getPointerToNamedFunction(const std::string &Name,
185 bool AbortOnFailure = true);
Danil Malyshevbfee5422012-03-28 21:46:36 +0000186
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000187 /// mapSectionAddress - map a section to its target address space value.
188 /// Map the address of a JIT section as returned from the memory manager
189 /// to the address in the target process as the running code will see it.
190 /// This is the address which will be used for relocation resolution.
Jim Grosbach6d613972012-09-13 21:50:06 +0000191 virtual void mapSectionAddress(const void *LocalAddress,
192 uint64_t TargetAddress) {
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000193 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
194 }
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000195 virtual void RegisterJITEventListener(JITEventListener *L);
196 virtual void UnregisterJITEventListener(JITEventListener *L);
197
Andrew Kaylorea395922013-10-01 01:47:35 +0000198 // If successful, these function will implicitly finalize all loaded objects.
199 // To get a function address within MCJIT without causing a finalize, use
200 // getSymbolAddress.
201 virtual uint64_t getGlobalValueAddress(const std::string &Name);
202 virtual uint64_t getFunctionAddress(const std::string &Name);
203
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000204 /// @}
205 /// @name (Private) Registration Interfaces
206 /// @{
207
208 static void Register() {
209 MCJITCtor = createJIT;
210 }
211
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000212 static ExecutionEngine *createJIT(Module *M,
213 std::string *ErrorStr,
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000214 RTDyldMemoryManager *MemMgr,
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000215 bool GVsWithCode,
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +0000216 TargetMachine *TM);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000217
218 // @}
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000219
Andrew Kaylorea395922013-10-01 01:47:35 +0000220 // This is not directly exposed via the ExecutionEngine API, but it is
221 // used by the LinkingMemoryManager.
222 uint64_t getSymbolAddress(const std::string &Name,
223 bool CheckFunctionsOnly);
224
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000225protected:
226 /// emitObject -- Generate a JITed object in memory from the specified module
227 /// Currently, MCJIT only supports a single module and the module passed to
228 /// this function call is expected to be the contained module. The module
229 /// is passed as a parameter here to prepare for multiple module support in
230 /// the future.
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000231 ObjectBufferStream* emitObject(Module *M);
232
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000233 void NotifyObjectEmitted(const ObjectImage& Obj);
234 void NotifyFreeingObject(const ObjectImage& Obj);
Andrew Kaylorea395922013-10-01 01:47:35 +0000235
236 uint64_t getExistingSymbolAddress(const std::string &Name);
237 Module *findModuleForSymbol(const std::string &Name,
238 bool CheckFunctionsOnly);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000239};
240
241} // End llvm namespace
242
243#endif