blob: 39326920ee0eba84e1d5228fab7a89a5ef7755d5 [file] [log] [blame]
Daniel Dunbar6aec2982010-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 Kaylor8e9ec012013-10-01 01:47:35 +000013#include "llvm/ADT/DenseMap.h"
Andrew Kaylor776054d2012-11-06 18:51:59 +000014#include "llvm/ADT/SmallVector.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000015#include "llvm/ExecutionEngine/ExecutionEngine.h"
Andrew Kaylor1c489452013-04-25 21:02:36 +000016#include "llvm/ExecutionEngine/ObjectCache.h"
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000017#include "llvm/ExecutionEngine/ObjectImage.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000018#include "llvm/ExecutionEngine/RuntimeDyld.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000019#include "llvm/PassManager.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000020
21namespace llvm {
22
Andrew Kaylor8e9ec012013-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 Pizlo6eb43d22013-10-02 00:59:25 +000038 unsigned SectionID, StringRef SectionName) {
39 return ClientMM->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000040 }
41
42 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
Filip Pizlo6eb43d22013-10-02 00:59:25 +000043 unsigned SectionID, StringRef SectionName,
44 bool IsReadOnly) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000045 return ClientMM->allocateDataSection(Size, Alignment,
Filip Pizlo6eb43d22013-10-02 00:59:25 +000046 SectionID, SectionName, IsReadOnly);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000047 }
48
Andrew Kaylorb868e912013-10-04 00:49:38 +000049 virtual void notifyObjectLoaded(ExecutionEngine *EE,
50 const ObjectImage *Obj) {
51 ClientMM->notifyObjectLoaded(EE, Obj);
52 }
53
Andrew Kaylor528f6d72013-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 Kaylor8e9ec012013-10-01 01:47:35 +000056 }
57
Andrew Kaylor43507d02013-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 Kaylor8e9ec012013-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 Kaylor3f23cef2012-10-02 21:18:39 +000072
Jim Grosbach31649e62011-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
Daniel Dunbar6aec2982010-11-17 16:06:43 +000077class MCJIT : public ExecutionEngine {
Jim Grosbach8005bcd2012-08-21 15:42:49 +000078 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
79 bool AllocateGVsWithCode);
Jim Grosbach31649e62011-03-18 22:48:41 +000080
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000081 enum ModuleState {
82 ModuleAdded,
83 ModuleEmitted,
84 ModuleLoading,
85 ModuleLoaded,
86 ModuleFinalizing,
87 ModuleFinalized
88 };
89
90 class MCJITModuleState {
91 public:
92 MCJITModuleState() : State(ModuleAdded) {}
93
94 MCJITModuleState & operator=(ModuleState s) { State = s; return *this; }
95 bool hasBeenEmitted() { return State != ModuleAdded; }
96 bool hasBeenLoaded() { return State != ModuleAdded &&
97 State != ModuleEmitted; }
98 bool hasBeenFinalized() { return State == ModuleFinalized; }
99
100 private:
101 ModuleState State;
102 };
103
Jim Grosbach31649e62011-03-18 22:48:41 +0000104 TargetMachine *TM;
105 MCContext *Ctx;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000106 LinkingMemoryManager MemMgr;
Andrew Kaylorea708d12012-08-07 18:33:00 +0000107 RuntimeDyld Dyld;
Andrew Kaylor776054d2012-11-06 18:51:59 +0000108 SmallVector<JITEventListener*, 2> EventListeners;
Jim Grosbach31649e62011-03-18 22:48:41 +0000109
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000110 typedef DenseMap<Module *, MCJITModuleState> ModuleStateMap;
111 ModuleStateMap ModuleStates;
112
113 typedef DenseMap<Module *, ObjectImage *> LoadedObjectMap;
114 LoadedObjectMap LoadedObjects;
Jim Grosbach31649e62011-03-18 22:48:41 +0000115
Andrew Kaylor1c489452013-04-25 21:02:36 +0000116 // An optional ObjectCache to be notified of compiled objects and used to
117 // perform lookup of pre-compiled code to avoid re-compilation.
118 ObjectCache *ObjCache;
119
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000120public:
121 ~MCJIT();
122
123 /// @name ExecutionEngine interface implementation
124 /// @{
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000125 virtual void addModule(Module *M);
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000126
Andrew Kaylor1c489452013-04-25 21:02:36 +0000127 /// Sets the object manager that MCJIT should use to avoid compilation.
128 virtual void setObjectCache(ObjectCache *manager);
129
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000130 virtual void generateCodeForModule(Module *M);
131
David Tweedabb38fe2013-05-17 10:01:46 +0000132 /// finalizeObject - ensure the module is fully processed and is usable.
133 ///
134 /// It is the user-level function for completing the process of making the
135 /// object usable for execution. It should be called after sections within an
136 /// object have been relocated using mapSectionAddress. When this method is
137 /// called the MCJIT execution engine will reapply relocations for a loaded
138 /// object.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000139 /// FIXME: Do we really need both of these?
Andrew Kaylor28989882012-11-05 20:57:16 +0000140 virtual void finalizeObject();
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000141 virtual void finalizeModule(Module *);
142 void finalizeLoadedModules();
Andrew Kaylor28989882012-11-05 20:57:16 +0000143
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000144 virtual void *getPointerToBasicBlock(BasicBlock *BB);
145
146 virtual void *getPointerToFunction(Function *F);
147
148 virtual void *recompileAndRelinkFunction(Function *F);
149
150 virtual void freeMachineCodeForFunction(Function *F);
151
152 virtual GenericValue runFunction(Function *F,
153 const std::vector<GenericValue> &ArgValues);
154
Jim Grosbach34714a02011-03-22 18:05:27 +0000155 /// getPointerToNamedFunction - This method returns the address of the
156 /// specified function by using the dlsym function call. As such it is only
157 /// useful for resolving library symbols, not code generated symbols.
158 ///
159 /// If AbortOnFailure is false and no function with the given name is
160 /// found, this function silently returns a null pointer. Otherwise,
161 /// it prints a message to stderr and aborts.
162 ///
Danil Malyshev45a93d62012-01-05 21:16:14 +0000163 virtual void *getPointerToNamedFunction(const std::string &Name,
164 bool AbortOnFailure = true);
Danil Malyshev30b9e322012-03-28 21:46:36 +0000165
Jim Grosbach020f4e82012-01-16 23:50:55 +0000166 /// mapSectionAddress - map a section to its target address space value.
167 /// Map the address of a JIT section as returned from the memory manager
168 /// to the address in the target process as the running code will see it.
169 /// This is the address which will be used for relocation resolution.
Jim Grosbache940c1b2012-09-13 21:50:06 +0000170 virtual void mapSectionAddress(const void *LocalAddress,
171 uint64_t TargetAddress) {
Jim Grosbach020f4e82012-01-16 23:50:55 +0000172 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
173 }
Andrew Kaylor776054d2012-11-06 18:51:59 +0000174 virtual void RegisterJITEventListener(JITEventListener *L);
175 virtual void UnregisterJITEventListener(JITEventListener *L);
176
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000177 // If successful, these function will implicitly finalize all loaded objects.
178 // To get a function address within MCJIT without causing a finalize, use
179 // getSymbolAddress.
180 virtual uint64_t getGlobalValueAddress(const std::string &Name);
181 virtual uint64_t getFunctionAddress(const std::string &Name);
182
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000183 /// @}
184 /// @name (Private) Registration Interfaces
185 /// @{
186
187 static void Register() {
188 MCJITCtor = createJIT;
189 }
190
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000191 static ExecutionEngine *createJIT(Module *M,
192 std::string *ErrorStr,
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000193 RTDyldMemoryManager *MemMgr,
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000194 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +0000195 TargetMachine *TM);
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000196
197 // @}
Andrew Kaylorea708d12012-08-07 18:33:00 +0000198
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000199 // This is not directly exposed via the ExecutionEngine API, but it is
200 // used by the LinkingMemoryManager.
201 uint64_t getSymbolAddress(const std::string &Name,
202 bool CheckFunctionsOnly);
203
Andrew Kaylorea708d12012-08-07 18:33:00 +0000204protected:
205 /// emitObject -- Generate a JITed object in memory from the specified module
206 /// Currently, MCJIT only supports a single module and the module passed to
207 /// this function call is expected to be the contained module. The module
208 /// is passed as a parameter here to prepare for multiple module support in
209 /// the future.
Andrew Kaylor1c489452013-04-25 21:02:36 +0000210 ObjectBufferStream* emitObject(Module *M);
211
Andrew Kaylor776054d2012-11-06 18:51:59 +0000212 void NotifyObjectEmitted(const ObjectImage& Obj);
213 void NotifyFreeingObject(const ObjectImage& Obj);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000214
215 uint64_t getExistingSymbolAddress(const std::string &Name);
216 Module *findModuleForSymbol(const std::string &Name,
217 bool CheckFunctionsOnly);
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000218};
219
220} // End llvm namespace
221
222#endif