1 //===-- JITMemoryManager.h - Interface JIT uses to Allocate Mem -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H
11 #define LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H
13 #include "llvm/ExecutionEngine/RuntimeDyld.h"
14 #include "llvm/Support/DataTypes.h"
22 /// JITMemoryManager - This interface is used by the JIT to allocate and manage
23 /// memory for the code generated by the JIT. This can be reimplemented by
24 /// clients that have a strong desire to control how the layout of JIT'd memory
26 class JITMemoryManager : public RTDyldMemoryManager {
31 JITMemoryManager() : HasGOT(false) {}
32 virtual ~JITMemoryManager();
34 /// CreateDefaultMemManager - This is used to create the default
35 /// JIT Memory Manager if the client does not provide one to the JIT.
36 static JITMemoryManager *CreateDefaultMemManager();
38 /// setMemoryWritable - When code generation is in progress,
39 /// the code pages may need permissions changed.
40 virtual void setMemoryWritable() = 0;
42 /// setMemoryExecutable - When code generation is done and we're ready to
43 /// start execution, the code pages may need permissions changed.
44 virtual void setMemoryExecutable() = 0;
46 /// setPoisonMemory - Setting this flag to true makes the memory manager
47 /// garbage values over freed memory. This is useful for testing and
48 /// debugging, and may be turned on by default in debug mode.
49 virtual void setPoisonMemory(bool poison) = 0;
51 //===--------------------------------------------------------------------===//
52 // Global Offset Table Management
53 //===--------------------------------------------------------------------===//
55 /// AllocateGOT - If the current table requires a Global Offset Table, this
56 /// method is invoked to allocate it. This method is required to set HasGOT
58 virtual void AllocateGOT() = 0;
60 /// isManagingGOT - Return true if the AllocateGOT method is called.
61 bool isManagingGOT() const {
65 /// getGOTBase - If this is managing a Global Offset Table, this method should
66 /// return a pointer to its base.
67 virtual uint8_t *getGOTBase() const = 0;
69 //===--------------------------------------------------------------------===//
70 // Main Allocation Functions
71 //===--------------------------------------------------------------------===//
73 /// startFunctionBody - When we start JITing a function, the JIT calls this
74 /// method to allocate a block of free RWX memory, which returns a pointer to
75 /// it. If the JIT wants to request a block of memory of at least a certain
76 /// size, it passes that value as ActualSize, and this method returns a block
77 /// with at least that much space. If the JIT doesn't know ahead of time how
78 /// much space it will need to emit the function, it passes 0 for the
79 /// ActualSize. In either case, this method is required to pass back the size
80 /// of the allocated block through ActualSize. The JIT will be careful to
81 /// not write more than the returned ActualSize bytes of memory.
82 virtual uint8_t *startFunctionBody(const Function *F,
83 uintptr_t &ActualSize) = 0;
85 /// allocateStub - This method is called by the JIT to allocate space for a
86 /// function stub (used to handle limited branch displacements) while it is
87 /// JIT compiling a function. For example, if foo calls bar, and if bar
88 /// either needs to be lazily compiled or is a native function that exists too
89 /// far away from the call site to work, this method will be used to make a
90 /// thunk for it. The stub should be "close" to the current function body,
91 /// but should not be included in the 'actualsize' returned by
92 /// startFunctionBody.
93 virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
94 unsigned Alignment) = 0;
96 /// endFunctionBody - This method is called when the JIT is done codegen'ing
97 /// the specified function. At this point we know the size of the JIT
98 /// compiled function. This passes in FunctionStart (which was returned by
99 /// the startFunctionBody method) and FunctionEnd which is a pointer to the
100 /// actual end of the function. This method should mark the space allocated
101 /// and remember where it is in case the client wants to deallocate it.
102 virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
103 uint8_t *FunctionEnd) = 0;
105 /// allocateSpace - Allocate a memory block of the given size. This method
106 /// cannot be called between calls to startFunctionBody and endFunctionBody.
107 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) = 0;
109 /// allocateGlobal - Allocate memory for a global.
110 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
112 /// deallocateFunctionBody - Free the specified function body. The argument
113 /// must be the return value from a call to startFunctionBody() that hasn't
114 /// been deallocated yet. This is never called when the JIT is currently
115 /// emitting a function.
116 virtual void deallocateFunctionBody(void *Body) = 0;
118 /// startExceptionTable - When we finished JITing the function, if exception
119 /// handling is set, we emit the exception table.
120 virtual uint8_t* startExceptionTable(const Function* F,
121 uintptr_t &ActualSize) = 0;
123 /// endExceptionTable - This method is called when the JIT is done emitting
124 /// the exception table.
125 virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
126 uint8_t *TableEnd, uint8_t* FrameRegister) = 0;
128 /// deallocateExceptionTable - Free the specified exception table's memory.
129 /// The argument must be the return value from a call to startExceptionTable()
130 /// that hasn't been deallocated yet. This is never called when the JIT is
131 /// currently emitting an exception table.
132 virtual void deallocateExceptionTable(void *ET) = 0;
134 /// CheckInvariants - For testing only. Return true if all internal
135 /// invariants are preserved, or return false and set ErrorStr to a helpful
137 virtual bool CheckInvariants(std::string &) {
141 /// GetDefaultCodeSlabSize - For testing only. Returns DefaultCodeSlabSize
142 /// from DefaultJITMemoryManager.
143 virtual size_t GetDefaultCodeSlabSize() {
147 /// GetDefaultDataSlabSize - For testing only. Returns DefaultCodeSlabSize
148 /// from DefaultJITMemoryManager.
149 virtual size_t GetDefaultDataSlabSize() {
153 /// GetDefaultStubSlabSize - For testing only. Returns DefaultCodeSlabSize
154 /// from DefaultJITMemoryManager.
155 virtual size_t GetDefaultStubSlabSize() {
159 /// GetNumCodeSlabs - For testing only. Returns the number of MemoryBlocks
160 /// allocated for code.
161 virtual unsigned GetNumCodeSlabs() {
165 /// GetNumDataSlabs - For testing only. Returns the number of MemoryBlocks
166 /// allocated for data.
167 virtual unsigned GetNumDataSlabs() {
171 /// GetNumStubSlabs - For testing only. Returns the number of MemoryBlocks
172 /// allocated for function stubs.
173 virtual unsigned GetNumStubSlabs() {
178 } // end namespace llvm.