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