Revert a series of commits to MCJIT to get the build working in CMake
[oota-llvm.git] / include / llvm / ExecutionEngine / JITMemoryManager.h
1 //===-- JITMemoryManager.h - Interface JIT uses to Allocate Mem -*- 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_EXECUTION_ENGINE_JIT_MEMMANAGER_H
11 #define LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H
12
13 #include "llvm/Support/DataTypes.h"
14 #include <string>
15
16 namespace llvm {
17
18   class Function;
19   class GlobalValue;
20
21 /// JITMemoryManager - This interface is used by the JIT to allocate and manage
22 /// memory for the code generated by the JIT.  This can be reimplemented by
23 /// clients that have a strong desire to control how the layout of JIT'd memory
24 /// works.
25 class JITMemoryManager {
26 protected:
27   bool HasGOT;
28
29 public:
30   JITMemoryManager() : HasGOT(false) {}
31   virtual ~JITMemoryManager();
32
33   /// CreateDefaultMemManager - This is used to create the default
34   /// JIT Memory Manager if the client does not provide one to the JIT.
35   static JITMemoryManager *CreateDefaultMemManager();
36
37   /// setMemoryWritable - When code generation is in progress,
38   /// the code pages may need permissions changed.
39   virtual void setMemoryWritable() = 0;
40
41   /// setMemoryExecutable - When code generation is done and we're ready to
42   /// start execution, the code pages may need permissions changed.
43   virtual void setMemoryExecutable() = 0;
44
45   /// setPoisonMemory - Setting this flag to true makes the memory manager
46   /// garbage values over freed memory.  This is useful for testing and
47   /// debugging, and may be turned on by default in debug mode.
48   virtual void setPoisonMemory(bool poison) = 0;
49
50   //===--------------------------------------------------------------------===//
51   // Global Offset Table Management
52   //===--------------------------------------------------------------------===//
53
54   /// AllocateGOT - If the current table requires a Global Offset Table, this
55   /// method is invoked to allocate it.  This method is required to set HasGOT
56   /// to true.
57   virtual void AllocateGOT() = 0;
58
59   /// isManagingGOT - Return true if the AllocateGOT method is called.
60   bool isManagingGOT() const {
61     return HasGOT;
62   }
63
64   /// getGOTBase - If this is managing a Global Offset Table, this method should
65   /// return a pointer to its base.
66   virtual uint8_t *getGOTBase() const = 0;
67
68   //===--------------------------------------------------------------------===//
69   // Main Allocation Functions
70   //===--------------------------------------------------------------------===//
71
72   /// startFunctionBody - When we start JITing a function, the JIT calls this
73   /// method to allocate a block of free RWX memory, which returns a pointer to
74   /// it.  If the JIT wants to request a block of memory of at least a certain
75   /// size, it passes that value as ActualSize, and this method returns a block
76   /// with at least that much space.  If the JIT doesn't know ahead of time how
77   /// much space it will need to emit the function, it passes 0 for the
78   /// ActualSize.  In either case, this method is required to pass back the size
79   /// of the allocated block through ActualSize.  The JIT will be careful to
80   /// not write more than the returned ActualSize bytes of memory.
81   virtual uint8_t *startFunctionBody(const Function *F,
82                                      uintptr_t &ActualSize) = 0;
83
84   /// allocateStub - This method is called by the JIT to allocate space for a
85   /// function stub (used to handle limited branch displacements) while it is
86   /// JIT compiling a function.  For example, if foo calls bar, and if bar
87   /// either needs to be lazily compiled or is a native function that exists too
88   /// far away from the call site to work, this method will be used to make a
89   /// thunk for it.  The stub should be "close" to the current function body,
90   /// but should not be included in the 'actualsize' returned by
91   /// startFunctionBody.
92   virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
93                                 unsigned Alignment) = 0;
94
95   /// endFunctionBody - This method is called when the JIT is done codegen'ing
96   /// the specified function.  At this point we know the size of the JIT
97   /// compiled function.  This passes in FunctionStart (which was returned by
98   /// the startFunctionBody method) and FunctionEnd which is a pointer to the
99   /// actual end of the function.  This method should mark the space allocated
100   /// and remember where it is in case the client wants to deallocate it.
101   virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
102                                uint8_t *FunctionEnd) = 0;
103
104   /// allocateCodeSection - Allocate a memory block of (at least) the given
105   /// size suitable for executable code. The SectionID is a unique identifier
106   /// assigned by the JIT and passed through to the memory manager for
107   /// the instance class to use if it needs to communicate to the JIT about
108   /// a given section after the fact.
109   virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
110                                        unsigned SectionID) = 0;
111
112   /// allocateDataSection - Allocate a memory block of (at least) the given
113   /// size suitable for data. The SectionID is a unique identifier
114   /// assigned by the JIT and passed through to the memory manager for
115   /// the instance class to use if it needs to communicate to the JIT about
116   /// a given section after the fact.
117   virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
118                                        unsigned SectionID) = 0;
119
120   /// allocateSpace - Allocate a memory block of the given size.  This method
121   /// cannot be called between calls to startFunctionBody and endFunctionBody.
122   virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) = 0;
123
124   /// allocateGlobal - Allocate memory for a global.
125   virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
126
127   /// deallocateFunctionBody - Free the specified function body.  The argument
128   /// must be the return value from a call to startFunctionBody() that hasn't
129   /// been deallocated yet.  This is never called when the JIT is currently
130   /// emitting a function.
131   virtual void deallocateFunctionBody(void *Body) = 0;
132
133   /// startExceptionTable - When we finished JITing the function, if exception
134   /// handling is set, we emit the exception table.
135   virtual uint8_t* startExceptionTable(const Function* F,
136                                        uintptr_t &ActualSize) = 0;
137
138   /// endExceptionTable - This method is called when the JIT is done emitting
139   /// the exception table.
140   virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
141                                  uint8_t *TableEnd, uint8_t* FrameRegister) = 0;
142
143   /// deallocateExceptionTable - Free the specified exception table's memory.
144   /// The argument must be the return value from a call to startExceptionTable()
145   /// that hasn't been deallocated yet.  This is never called when the JIT is
146   /// currently emitting an exception table.
147   virtual void deallocateExceptionTable(void *ET) = 0;
148
149   /// CheckInvariants - For testing only.  Return true if all internal
150   /// invariants are preserved, or return false and set ErrorStr to a helpful
151   /// error message.
152   virtual bool CheckInvariants(std::string &) {
153     return true;
154   }
155
156   /// GetDefaultCodeSlabSize - For testing only.  Returns DefaultCodeSlabSize
157   /// from DefaultJITMemoryManager.
158   virtual size_t GetDefaultCodeSlabSize() {
159     return 0;
160   }
161
162   /// GetDefaultDataSlabSize - For testing only.  Returns DefaultCodeSlabSize
163   /// from DefaultJITMemoryManager.
164   virtual size_t GetDefaultDataSlabSize() {
165     return 0;
166   }
167
168   /// GetDefaultStubSlabSize - For testing only.  Returns DefaultCodeSlabSize
169   /// from DefaultJITMemoryManager.
170   virtual size_t GetDefaultStubSlabSize() {
171     return 0;
172   }
173
174   /// GetNumCodeSlabs - For testing only.  Returns the number of MemoryBlocks
175   /// allocated for code.
176   virtual unsigned GetNumCodeSlabs() {
177     return 0;
178   }
179
180   /// GetNumDataSlabs - For testing only.  Returns the number of MemoryBlocks
181   /// allocated for data.
182   virtual unsigned GetNumDataSlabs() {
183     return 0;
184   }
185
186   /// GetNumStubSlabs - For testing only.  Returns the number of MemoryBlocks
187   /// allocated for function stubs.
188   virtual unsigned GetNumStubSlabs() {
189     return 0;
190   }
191 };
192
193 } // end namespace llvm.
194
195 #endif