blob: fe59288aeab85086c1eb88d7eaf8d373cc0da72d [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
58 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
59 return ClientMM->finalizeMemory(ErrMsg);
60 }
61
62private:
63 MCJIT *ParentEngine;
64 OwningPtr<RTDyldMemoryManager> ClientMM;
65};
Andrew Kayloradc70562012-10-02 21:18:39 +000066
Jim Grosbach7b162492011-03-18 22:48:41 +000067// 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.
70
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000071class MCJIT : public ExecutionEngine {
Jim Grosbachbea67532012-08-21 15:42:49 +000072 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
73 bool AllocateGVsWithCode);
Jim Grosbach7b162492011-03-18 22:48:41 +000074
Andrew Kaylorea395922013-10-01 01:47:35 +000075 enum ModuleState {
76 ModuleAdded,
77 ModuleEmitted,
78 ModuleLoading,
79 ModuleLoaded,
80 ModuleFinalizing,
81 ModuleFinalized
82 };
83
84 class MCJITModuleState {
85 public:
86 MCJITModuleState() : State(ModuleAdded) {}
87
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; }
93
94 private:
95 ModuleState State;
96 };
97
Jim Grosbach7b162492011-03-18 22:48:41 +000098 TargetMachine *TM;
99 MCContext *Ctx;
Andrew Kaylorea395922013-10-01 01:47:35 +0000100 LinkingMemoryManager MemMgr;
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000101 RuntimeDyld Dyld;
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000102 SmallVector<JITEventListener*, 2> EventListeners;
Jim Grosbach7b162492011-03-18 22:48:41 +0000103
Andrew Kaylorea395922013-10-01 01:47:35 +0000104 typedef DenseMap<Module *, MCJITModuleState> ModuleStateMap;
105 ModuleStateMap ModuleStates;
106
107 typedef DenseMap<Module *, ObjectImage *> LoadedObjectMap;
108 LoadedObjectMap LoadedObjects;
Jim Grosbach7b162492011-03-18 22:48:41 +0000109
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000110 // 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;
113
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000114public:
115 ~MCJIT();
116
117 /// @name ExecutionEngine interface implementation
118 /// @{
Andrew Kaylorea395922013-10-01 01:47:35 +0000119 virtual void addModule(Module *M);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000120
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000121 /// Sets the object manager that MCJIT should use to avoid compilation.
122 virtual void setObjectCache(ObjectCache *manager);
123
Andrew Kaylorea395922013-10-01 01:47:35 +0000124 virtual void generateCodeForModule(Module *M);
125
David Tweed2e7efed2013-05-17 10:01:46 +0000126 /// finalizeObject - ensure the module is fully processed and is usable.
127 ///
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
132 /// object.
Andrew Kaylorea395922013-10-01 01:47:35 +0000133 /// FIXME: Do we really need both of these?
Andrew Kaylora714efc2012-11-05 20:57:16 +0000134 virtual void finalizeObject();
Andrew Kaylorea395922013-10-01 01:47:35 +0000135 virtual void finalizeModule(Module *);
136 void finalizeLoadedModules();
Andrew Kaylora714efc2012-11-05 20:57:16 +0000137
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000138 virtual void *getPointerToBasicBlock(BasicBlock *BB);
139
140 virtual void *getPointerToFunction(Function *F);
141
142 virtual void *recompileAndRelinkFunction(Function *F);
143
144 virtual void freeMachineCodeForFunction(Function *F);
145
146 virtual GenericValue runFunction(Function *F,
147 const std::vector<GenericValue> &ArgValues);
148
Jim Grosbachd5274402011-03-22 18:05:27 +0000149 /// 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.
152 ///
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.
156 ///
Danil Malyshev7e325782012-01-05 21:16:14 +0000157 virtual void *getPointerToNamedFunction(const std::string &Name,
158 bool AbortOnFailure = true);
Danil Malyshevbfee5422012-03-28 21:46:36 +0000159
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000160 /// 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.
Jim Grosbach6d613972012-09-13 21:50:06 +0000164 virtual void mapSectionAddress(const void *LocalAddress,
165 uint64_t TargetAddress) {
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000166 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
167 }
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000168 virtual void RegisterJITEventListener(JITEventListener *L);
169 virtual void UnregisterJITEventListener(JITEventListener *L);
170
Andrew Kaylorea395922013-10-01 01:47:35 +0000171 // If successful, these function will implicitly finalize all loaded objects.
172 // To get a function address within MCJIT without causing a finalize, use
173 // getSymbolAddress.
174 virtual uint64_t getGlobalValueAddress(const std::string &Name);
175 virtual uint64_t getFunctionAddress(const std::string &Name);
176
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000177 /// @}
178 /// @name (Private) Registration Interfaces
179 /// @{
180
181 static void Register() {
182 MCJITCtor = createJIT;
183 }
184
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000185 static ExecutionEngine *createJIT(Module *M,
186 std::string *ErrorStr,
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000187 RTDyldMemoryManager *MemMgr,
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000188 bool GVsWithCode,
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +0000189 TargetMachine *TM);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000190
191 // @}
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000192
Andrew Kaylorea395922013-10-01 01:47:35 +0000193 // 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);
197
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000198protected:
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
203 /// the future.
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000204 ObjectBufferStream* emitObject(Module *M);
205
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000206 void NotifyObjectEmitted(const ObjectImage& Obj);
207 void NotifyFreeingObject(const ObjectImage& Obj);
Andrew Kaylorea395922013-10-01 01:47:35 +0000208
209 uint64_t getExistingSymbolAddress(const std::string &Name);
210 Module *findModuleForSymbol(const std::string &Name,
211 bool CheckFunctionsOnly);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000212};
213
214} // End llvm namespace
215
216#endif