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