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_EXECUTIONENGINE_JITMEMORYMANAGER_H 11 #define LLVM_EXECUTIONENGINE_JITMEMORYMANAGER_H 12 13 #include "llvm/ExecutionEngine/RuntimeDyld.h" 14 #include "llvm/Support/DataTypes.h" 15 #include <string> 16 17 namespace llvm { 18 19 class Function; 20 class GlobalValue; 21 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 25 /// works. 26 class JITMemoryManager : public RTDyldMemoryManager { 27 protected: 28 bool HasGOT; 29 30 public: 31 JITMemoryManager() : HasGOT(false) {} 32 virtual ~JITMemoryManager(); 33 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(); 37 38 /// setMemoryWritable - When code generation is in progress, 39 /// the code pages may need permissions changed. 40 virtual void setMemoryWritable() = 0; 41 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; 45 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; 50 51 //===--------------------------------------------------------------------===// 52 // Global Offset Table Management 53 //===--------------------------------------------------------------------===// 54 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 57 /// to true. 58 virtual void AllocateGOT() = 0; 59 60 /// isManagingGOT - Return true if the AllocateGOT method is called. 61 bool isManagingGOT() const { 62 return HasGOT; 63 } 64 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; 68 69 //===--------------------------------------------------------------------===// 70 // Main Allocation Functions 71 //===--------------------------------------------------------------------===// 72 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; 84 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; 95 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; 104 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; 108 109 /// allocateGlobal - Allocate memory for a global. 110 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0; 111 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; 117 118 /// CheckInvariants - For testing only. Return true if all internal 119 /// invariants are preserved, or return false and set ErrorStr to a helpful 120 /// error message. 121 virtual bool CheckInvariants(std::string &) { 122 return true; 123 } 124 125 /// GetDefaultCodeSlabSize - For testing only. Returns DefaultCodeSlabSize 126 /// from DefaultJITMemoryManager. 127 virtual size_t GetDefaultCodeSlabSize() { 128 return 0; 129 } 130 131 /// GetDefaultDataSlabSize - For testing only. Returns DefaultCodeSlabSize 132 /// from DefaultJITMemoryManager. 133 virtual size_t GetDefaultDataSlabSize() { 134 return 0; 135 } 136 137 /// GetDefaultStubSlabSize - For testing only. Returns DefaultCodeSlabSize 138 /// from DefaultJITMemoryManager. 139 virtual size_t GetDefaultStubSlabSize() { 140 return 0; 141 } 142 143 /// GetNumCodeSlabs - For testing only. Returns the number of MemoryBlocks 144 /// allocated for code. 145 virtual unsigned GetNumCodeSlabs() { 146 return 0; 147 } 148 149 /// GetNumDataSlabs - For testing only. Returns the number of MemoryBlocks 150 /// allocated for data. 151 virtual unsigned GetNumDataSlabs() { 152 return 0; 153 } 154 155 /// GetNumStubSlabs - For testing only. Returns the number of MemoryBlocks 156 /// allocated for function stubs. 157 virtual unsigned GetNumStubSlabs() { 158 return 0; 159 } 160 }; 161 162 } // end namespace llvm. 163 164 #endif 165