blob: a40a9e429bed41ecfb67af43c7686ec9d9a7d862 [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
49 virtual void registerEHFrames(StringRef SectionData) {
50 ClientMM->registerEHFrames(SectionData);
51 }
52
53 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
54 return ClientMM->finalizeMemory(ErrMsg);
55 }
56
57private:
58 MCJIT *ParentEngine;
59 OwningPtr<RTDyldMemoryManager> ClientMM;
60};
Andrew Kayloradc70562012-10-02 21:18:39 +000061
Jim Grosbach7b162492011-03-18 22:48:41 +000062// 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.
65
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000066class MCJIT : public ExecutionEngine {
Jim Grosbachbea67532012-08-21 15:42:49 +000067 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
68 bool AllocateGVsWithCode);
Jim Grosbach7b162492011-03-18 22:48:41 +000069
Andrew Kaylorea395922013-10-01 01:47:35 +000070 enum ModuleState {
71 ModuleAdded,
72 ModuleEmitted,
73 ModuleLoading,
74 ModuleLoaded,
75 ModuleFinalizing,
76 ModuleFinalized
77 };
78
79 class MCJITModuleState {
80 public:
81 MCJITModuleState() : State(ModuleAdded) {}
82
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; }
88
89 private:
90 ModuleState State;
91 };
92
Jim Grosbach7b162492011-03-18 22:48:41 +000093 TargetMachine *TM;
94 MCContext *Ctx;
Andrew Kaylorea395922013-10-01 01:47:35 +000095 LinkingMemoryManager MemMgr;
Andrew Kaylor1a568c32012-08-07 18:33:00 +000096 RuntimeDyld Dyld;
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +000097 SmallVector<JITEventListener*, 2> EventListeners;
Jim Grosbach7b162492011-03-18 22:48:41 +000098
Andrew Kaylorea395922013-10-01 01:47:35 +000099 typedef DenseMap<Module *, MCJITModuleState> ModuleStateMap;
100 ModuleStateMap ModuleStates;
101
102 typedef DenseMap<Module *, ObjectImage *> LoadedObjectMap;
103 LoadedObjectMap LoadedObjects;
Jim Grosbach7b162492011-03-18 22:48:41 +0000104
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000105 // 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;
108
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000109public:
110 ~MCJIT();
111
112 /// @name ExecutionEngine interface implementation
113 /// @{
Andrew Kaylorea395922013-10-01 01:47:35 +0000114 virtual void addModule(Module *M);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000115
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000116 /// Sets the object manager that MCJIT should use to avoid compilation.
117 virtual void setObjectCache(ObjectCache *manager);
118
Andrew Kaylorea395922013-10-01 01:47:35 +0000119 virtual void generateCodeForModule(Module *M);
120
David Tweed2e7efed2013-05-17 10:01:46 +0000121 /// finalizeObject - ensure the module is fully processed and is usable.
122 ///
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
127 /// object.
Andrew Kaylorea395922013-10-01 01:47:35 +0000128 /// FIXME: Do we really need both of these?
Andrew Kaylora714efc2012-11-05 20:57:16 +0000129 virtual void finalizeObject();
Andrew Kaylorea395922013-10-01 01:47:35 +0000130 virtual void finalizeModule(Module *);
131 void finalizeLoadedModules();
Andrew Kaylora714efc2012-11-05 20:57:16 +0000132
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000133 virtual void *getPointerToBasicBlock(BasicBlock *BB);
134
135 virtual void *getPointerToFunction(Function *F);
136
137 virtual void *recompileAndRelinkFunction(Function *F);
138
139 virtual void freeMachineCodeForFunction(Function *F);
140
141 virtual GenericValue runFunction(Function *F,
142 const std::vector<GenericValue> &ArgValues);
143
Jim Grosbachd5274402011-03-22 18:05:27 +0000144 /// 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.
147 ///
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.
151 ///
Danil Malyshev7e325782012-01-05 21:16:14 +0000152 virtual void *getPointerToNamedFunction(const std::string &Name,
153 bool AbortOnFailure = true);
Danil Malyshevbfee5422012-03-28 21:46:36 +0000154
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000155 /// 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.
Jim Grosbach6d613972012-09-13 21:50:06 +0000159 virtual void mapSectionAddress(const void *LocalAddress,
160 uint64_t TargetAddress) {
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000161 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
162 }
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000163 virtual void RegisterJITEventListener(JITEventListener *L);
164 virtual void UnregisterJITEventListener(JITEventListener *L);
165
Andrew Kaylorea395922013-10-01 01:47:35 +0000166 // If successful, these function will implicitly finalize all loaded objects.
167 // To get a function address within MCJIT without causing a finalize, use
168 // getSymbolAddress.
169 virtual uint64_t getGlobalValueAddress(const std::string &Name);
170 virtual uint64_t getFunctionAddress(const std::string &Name);
171
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000172 /// @}
173 /// @name (Private) Registration Interfaces
174 /// @{
175
176 static void Register() {
177 MCJITCtor = createJIT;
178 }
179
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000180 static ExecutionEngine *createJIT(Module *M,
181 std::string *ErrorStr,
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000182 RTDyldMemoryManager *MemMgr,
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000183 bool GVsWithCode,
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +0000184 TargetMachine *TM);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000185
186 // @}
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000187
Andrew Kaylorea395922013-10-01 01:47:35 +0000188 // 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);
192
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000193protected:
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
198 /// the future.
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000199 ObjectBufferStream* emitObject(Module *M);
200
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000201 void NotifyObjectEmitted(const ObjectImage& Obj);
202 void NotifyFreeingObject(const ObjectImage& Obj);
Andrew Kaylorea395922013-10-01 01:47:35 +0000203
204 uint64_t getExistingSymbolAddress(const std::string &Name);
205 Module *findModuleForSymbol(const std::string &Name,
206 bool CheckFunctionsOnly);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000207};
208
209} // End llvm namespace
210
211#endif