This void is implicit in C++.
[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 // This file defines the JITMemoryManagerInterface
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H
15 #define LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H
16
17 #include "llvm/Support/DataTypes.h"
18 #include <string>
19
20 namespace llvm {
21
22   class Function;
23   class GlobalValue;
24
25 /// JITMemoryManager - This interface is used by the JIT to allocate and manage
26 /// memory for the code generated by the JIT.  This can be reimplemented by
27 /// clients that have a strong desire to control how the layout of JIT'd memory
28 /// works.
29 class JITMemoryManager {
30 protected:
31   bool HasGOT;
32   bool SizeRequired;
33 public:
34
35   JITMemoryManager() : HasGOT(false), SizeRequired(false) {}
36   virtual ~JITMemoryManager();
37   
38   /// CreateDefaultMemManager - This is used to create the default
39   /// JIT Memory Manager if the client does not provide one to the JIT.
40   static JITMemoryManager *CreateDefaultMemManager();
41   
42   /// setMemoryWritable - When code generation is in progress,
43   /// the code pages may need permissions changed.
44   virtual void setMemoryWritable() = 0;
45
46   /// setMemoryExecutable - When code generation is done and we're ready to
47   /// start execution, the code pages may need permissions changed.
48   virtual void setMemoryExecutable() = 0;
49
50   /// setPoisonMemory - Setting this flag to true makes the memory manager
51   /// garbage values over freed memory.  This is useful for testing and
52   /// debugging, and is be turned on by default in debug mode.
53   virtual void setPoisonMemory(bool poison) = 0;
54
55   //===--------------------------------------------------------------------===//
56   // Global Offset Table Management
57   //===--------------------------------------------------------------------===//
58
59   /// AllocateGOT - If the current table requires a Global Offset Table, this
60   /// method is invoked to allocate it.  This method is required to set HasGOT
61   /// to true.
62   virtual void AllocateGOT() = 0;
63   
64   /// isManagingGOT - Return true if the AllocateGOT method is called.
65   ///
66   bool isManagingGOT() const {
67     return HasGOT;
68   }
69   
70   /// getGOTBase - If this is managing a Global Offset Table, this method should
71   /// return a pointer to its base.
72   virtual uint8_t *getGOTBase() const = 0;
73   
74   /// SetDlsymTable - If the JIT must be able to relocate stubs after they have
75   /// been emitted, potentially because they are being copied to a process
76   /// where external symbols live at different addresses than in the JITing
77   ///  process, allocate a table with sufficient information to do so.
78   virtual void SetDlsymTable(void *ptr) = 0;
79   
80   /// getDlsymTable - If this is managing a table of entries so that stubs to
81   /// external symbols can be later relocated, this method should return a
82   /// pointer to it.
83   virtual void *getDlsymTable() const = 0;
84   
85   /// NeedsExactSize - If the memory manager requires to know the size of the
86   /// objects to be emitted
87   bool NeedsExactSize() const {
88     return SizeRequired;
89   }
90
91   //===--------------------------------------------------------------------===//
92   // Main Allocation Functions
93   //===--------------------------------------------------------------------===//
94
95   /// startFunctionBody - When we start JITing a function, the JIT calls this
96   /// method to allocate a block of free RWX memory, which returns a pointer to
97   /// it.  If the JIT wants to request a block of memory of at least a certain
98   /// size, it passes that value as ActualSize, and this method returns a block
99   /// with at least that much space.  If the JIT doesn't know ahead of time how
100   /// much space it will need to emit the function, it passes 0 for the
101   /// ActualSize.  In either case, this method is required to pass back the size
102   /// of the allocated block through ActualSize.  The JIT will be careful to
103   /// not write more than the returned ActualSize bytes of memory.
104   virtual uint8_t *startFunctionBody(const Function *F,
105                                      uintptr_t &ActualSize) = 0;
106
107   /// allocateStub - This method is called by the JIT to allocate space for a
108   /// function stub (used to handle limited branch displacements) while it is
109   /// JIT compiling a function.  For example, if foo calls bar, and if bar
110   /// either needs to be lazily compiled or is a native function that exists too
111   /// far away from the call site to work, this method will be used to make a
112   /// thunk for it.  The stub should be "close" to the current function body,
113   /// but should not be included in the 'actualsize' returned by
114   /// startFunctionBody.
115   virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
116                                 unsigned Alignment) = 0;
117   
118   /// endFunctionBody - This method is called when the JIT is done codegen'ing
119   /// the specified function.  At this point we know the size of the JIT
120   /// compiled function.  This passes in FunctionStart (which was returned by
121   /// the startFunctionBody method) and FunctionEnd which is a pointer to the 
122   /// actual end of the function.  This method should mark the space allocated
123   /// and remember where it is in case the client wants to deallocate it.
124   virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
125                                uint8_t *FunctionEnd) = 0;
126
127   /// allocateSpace - Allocate a memory block of the given size.  This method
128   /// cannot be called between calls to startFunctionBody and endFunctionBody.
129   virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) = 0;
130
131   /// allocateGlobal - Allocate memory for a global.
132   ///
133   virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
134
135   /// deallocateMemForFunction - Free JIT memory for the specified function.
136   /// This is never called when the JIT is currently emitting a function.
137   virtual void deallocateMemForFunction(const Function *F) = 0;
138   
139   /// startExceptionTable - When we finished JITing the function, if exception
140   /// handling is set, we emit the exception table.
141   virtual uint8_t* startExceptionTable(const Function* F,
142                                        uintptr_t &ActualSize) = 0;
143   
144   /// endExceptionTable - This method is called when the JIT is done emitting
145   /// the exception table.
146   virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
147                                  uint8_t *TableEnd, uint8_t* FrameRegister) = 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 &ErrorStr) {
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