blob: 5fb2eebd92ae0f95b8fa128d6072269e1f254299 [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,
38 unsigned SectionID) {
39 return ClientMM->allocateCodeSection(Size, Alignment, SectionID);
40 }
41
42 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
43 unsigned SectionID, bool IsReadOnly) {
44 return ClientMM->allocateDataSection(Size, Alignment,
45 SectionID, IsReadOnly);
46 }
47
48 virtual void registerEHFrames(StringRef SectionData) {
49 ClientMM->registerEHFrames(SectionData);
50 }
51
52 virtual bool finalizeMemory(std::string *ErrMsg = 0) {
53 return ClientMM->finalizeMemory(ErrMsg);
54 }
55
56private:
57 MCJIT *ParentEngine;
58 OwningPtr<RTDyldMemoryManager> ClientMM;
59};
Andrew Kayloradc70562012-10-02 21:18:39 +000060
Jim Grosbach7b162492011-03-18 22:48:41 +000061// FIXME: This makes all kinds of horrible assumptions for the time being,
62// like only having one module, not needing to worry about multi-threading,
63// blah blah. Purely in get-it-up-and-limping mode for now.
64
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000065class MCJIT : public ExecutionEngine {
Jim Grosbachbea67532012-08-21 15:42:49 +000066 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
67 bool AllocateGVsWithCode);
Jim Grosbach7b162492011-03-18 22:48:41 +000068
Andrew Kaylorea395922013-10-01 01:47:35 +000069 enum ModuleState {
70 ModuleAdded,
71 ModuleEmitted,
72 ModuleLoading,
73 ModuleLoaded,
74 ModuleFinalizing,
75 ModuleFinalized
76 };
77
78 class MCJITModuleState {
79 public:
80 MCJITModuleState() : State(ModuleAdded) {}
81
82 MCJITModuleState & operator=(ModuleState s) { State = s; return *this; }
83 bool hasBeenEmitted() { return State != ModuleAdded; }
84 bool hasBeenLoaded() { return State != ModuleAdded &&
85 State != ModuleEmitted; }
86 bool hasBeenFinalized() { return State == ModuleFinalized; }
87
88 private:
89 ModuleState State;
90 };
91
Jim Grosbach7b162492011-03-18 22:48:41 +000092 TargetMachine *TM;
93 MCContext *Ctx;
Andrew Kaylorea395922013-10-01 01:47:35 +000094 LinkingMemoryManager MemMgr;
Andrew Kaylor1a568c32012-08-07 18:33:00 +000095 RuntimeDyld Dyld;
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +000096 SmallVector<JITEventListener*, 2> EventListeners;
Jim Grosbach7b162492011-03-18 22:48:41 +000097
Andrew Kaylorea395922013-10-01 01:47:35 +000098 typedef DenseMap<Module *, MCJITModuleState> ModuleStateMap;
99 ModuleStateMap ModuleStates;
100
101 typedef DenseMap<Module *, ObjectImage *> LoadedObjectMap;
102 LoadedObjectMap LoadedObjects;
Jim Grosbach7b162492011-03-18 22:48:41 +0000103
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000104 // An optional ObjectCache to be notified of compiled objects and used to
105 // perform lookup of pre-compiled code to avoid re-compilation.
106 ObjectCache *ObjCache;
107
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000108public:
109 ~MCJIT();
110
111 /// @name ExecutionEngine interface implementation
112 /// @{
Andrew Kaylorea395922013-10-01 01:47:35 +0000113 virtual void addModule(Module *M);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000114
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000115 /// Sets the object manager that MCJIT should use to avoid compilation.
116 virtual void setObjectCache(ObjectCache *manager);
117
Andrew Kaylorea395922013-10-01 01:47:35 +0000118 virtual void generateCodeForModule(Module *M);
119
David Tweed2e7efed2013-05-17 10:01:46 +0000120 /// finalizeObject - ensure the module is fully processed and is usable.
121 ///
122 /// It is the user-level function for completing the process of making the
123 /// object usable for execution. It should be called after sections within an
124 /// object have been relocated using mapSectionAddress. When this method is
125 /// called the MCJIT execution engine will reapply relocations for a loaded
126 /// object.
Andrew Kaylorea395922013-10-01 01:47:35 +0000127 /// FIXME: Do we really need both of these?
Andrew Kaylora714efc2012-11-05 20:57:16 +0000128 virtual void finalizeObject();
Andrew Kaylorea395922013-10-01 01:47:35 +0000129 virtual void finalizeModule(Module *);
130 void finalizeLoadedModules();
Andrew Kaylora714efc2012-11-05 20:57:16 +0000131
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000132 virtual void *getPointerToBasicBlock(BasicBlock *BB);
133
134 virtual void *getPointerToFunction(Function *F);
135
136 virtual void *recompileAndRelinkFunction(Function *F);
137
138 virtual void freeMachineCodeForFunction(Function *F);
139
140 virtual GenericValue runFunction(Function *F,
141 const std::vector<GenericValue> &ArgValues);
142
Jim Grosbachd5274402011-03-22 18:05:27 +0000143 /// getPointerToNamedFunction - This method returns the address of the
144 /// specified function by using the dlsym function call. As such it is only
145 /// useful for resolving library symbols, not code generated symbols.
146 ///
147 /// If AbortOnFailure is false and no function with the given name is
148 /// found, this function silently returns a null pointer. Otherwise,
149 /// it prints a message to stderr and aborts.
150 ///
Danil Malyshev7e325782012-01-05 21:16:14 +0000151 virtual void *getPointerToNamedFunction(const std::string &Name,
152 bool AbortOnFailure = true);
Danil Malyshevbfee5422012-03-28 21:46:36 +0000153
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000154 /// mapSectionAddress - map a section to its target address space value.
155 /// Map the address of a JIT section as returned from the memory manager
156 /// to the address in the target process as the running code will see it.
157 /// This is the address which will be used for relocation resolution.
Jim Grosbach6d613972012-09-13 21:50:06 +0000158 virtual void mapSectionAddress(const void *LocalAddress,
159 uint64_t TargetAddress) {
Jim Grosbach0ddb3a42012-01-16 23:50:55 +0000160 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
161 }
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000162 virtual void RegisterJITEventListener(JITEventListener *L);
163 virtual void UnregisterJITEventListener(JITEventListener *L);
164
Andrew Kaylorea395922013-10-01 01:47:35 +0000165 // If successful, these function will implicitly finalize all loaded objects.
166 // To get a function address within MCJIT without causing a finalize, use
167 // getSymbolAddress.
168 virtual uint64_t getGlobalValueAddress(const std::string &Name);
169 virtual uint64_t getFunctionAddress(const std::string &Name);
170
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000171 /// @}
172 /// @name (Private) Registration Interfaces
173 /// @{
174
175 static void Register() {
176 MCJITCtor = createJIT;
177 }
178
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000179 static ExecutionEngine *createJIT(Module *M,
180 std::string *ErrorStr,
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000181 RTDyldMemoryManager *MemMgr,
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000182 bool GVsWithCode,
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +0000183 TargetMachine *TM);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000184
185 // @}
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000186
Andrew Kaylorea395922013-10-01 01:47:35 +0000187 // This is not directly exposed via the ExecutionEngine API, but it is
188 // used by the LinkingMemoryManager.
189 uint64_t getSymbolAddress(const std::string &Name,
190 bool CheckFunctionsOnly);
191
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000192protected:
193 /// emitObject -- Generate a JITed object in memory from the specified module
194 /// Currently, MCJIT only supports a single module and the module passed to
195 /// this function call is expected to be the contained module. The module
196 /// is passed as a parameter here to prepare for multiple module support in
197 /// the future.
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000198 ObjectBufferStream* emitObject(Module *M);
199
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000200 void NotifyObjectEmitted(const ObjectImage& Obj);
201 void NotifyFreeingObject(const ObjectImage& Obj);
Andrew Kaylorea395922013-10-01 01:47:35 +0000202
203 uint64_t getExistingSymbolAddress(const std::string &Name);
204 Module *findModuleForSymbol(const std::string &Name,
205 bool CheckFunctionsOnly);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000206};
207
208} // End llvm namespace
209
210#endif