LLVM API Documentation
00001 //===-- JITMemoryManager.h - Interface JIT uses to Allocate Mem -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the JITMemoryManagerInterface 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H 00015 #define LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H 00016 00017 #include "llvm/Support/DataTypes.h" 00018 #include <string> 00019 00020 namespace llvm { 00021 00022 class Function; 00023 class GlobalValue; 00024 00025 /// JITMemoryManager - This interface is used by the JIT to allocate and manage 00026 /// memory for the code generated by the JIT. This can be reimplemented by 00027 /// clients that have a strong desire to control how the layout of JIT'd memory 00028 /// works. 00029 class JITMemoryManager { 00030 protected: 00031 bool HasGOT; 00032 bool SizeRequired; 00033 public: 00034 00035 JITMemoryManager() : HasGOT(false), SizeRequired(false) {} 00036 virtual ~JITMemoryManager(); 00037 00038 /// CreateDefaultMemManager - This is used to create the default 00039 /// JIT Memory Manager if the client does not provide one to the JIT. 00040 static JITMemoryManager *CreateDefaultMemManager(); 00041 00042 /// setMemoryWritable - When code generation is in progress, 00043 /// the code pages may need permissions changed. 00044 virtual void setMemoryWritable() = 0; 00045 00046 /// setMemoryExecutable - When code generation is done and we're ready to 00047 /// start execution, the code pages may need permissions changed. 00048 virtual void setMemoryExecutable() = 0; 00049 00050 /// setPoisonMemory - Setting this flag to true makes the memory manager 00051 /// garbage values over freed memory. This is useful for testing and 00052 /// debugging, and is be turned on by default in debug mode. 00053 virtual void setPoisonMemory(bool poison) = 0; 00054 00055 //===--------------------------------------------------------------------===// 00056 // Global Offset Table Management 00057 //===--------------------------------------------------------------------===// 00058 00059 /// AllocateGOT - If the current table requires a Global Offset Table, this 00060 /// method is invoked to allocate it. This method is required to set HasGOT 00061 /// to true. 00062 virtual void AllocateGOT() = 0; 00063 00064 /// isManagingGOT - Return true if the AllocateGOT method is called. 00065 /// 00066 bool isManagingGOT() const { 00067 return HasGOT; 00068 } 00069 00070 /// getGOTBase - If this is managing a Global Offset Table, this method should 00071 /// return a pointer to its base. 00072 virtual uint8_t *getGOTBase() const = 0; 00073 00074 /// SetDlsymTable - If the JIT must be able to relocate stubs after they have 00075 /// been emitted, potentially because they are being copied to a process 00076 /// where external symbols live at different addresses than in the JITing 00077 /// process, allocate a table with sufficient information to do so. 00078 virtual void SetDlsymTable(void *ptr) = 0; 00079 00080 /// getDlsymTable - If this is managing a table of entries so that stubs to 00081 /// external symbols can be later relocated, this method should return a 00082 /// pointer to it. 00083 virtual void *getDlsymTable() const = 0; 00084 00085 /// NeedsExactSize - If the memory manager requires to know the size of the 00086 /// objects to be emitted 00087 bool NeedsExactSize() const { 00088 return SizeRequired; 00089 } 00090 00091 //===--------------------------------------------------------------------===// 00092 // Main Allocation Functions 00093 //===--------------------------------------------------------------------===// 00094 00095 /// startFunctionBody - When we start JITing a function, the JIT calls this 00096 /// method to allocate a block of free RWX memory, which returns a pointer to 00097 /// it. If the JIT wants to request a block of memory of at least a certain 00098 /// size, it passes that value as ActualSize, and this method returns a block 00099 /// with at least that much space. If the JIT doesn't know ahead of time how 00100 /// much space it will need to emit the function, it passes 0 for the 00101 /// ActualSize. In either case, this method is required to pass back the size 00102 /// of the allocated block through ActualSize. The JIT will be careful to 00103 /// not write more than the returned ActualSize bytes of memory. 00104 virtual uint8_t *startFunctionBody(const Function *F, 00105 uintptr_t &ActualSize) = 0; 00106 00107 /// allocateStub - This method is called by the JIT to allocate space for a 00108 /// function stub (used to handle limited branch displacements) while it is 00109 /// JIT compiling a function. For example, if foo calls bar, and if bar 00110 /// either needs to be lazily compiled or is a native function that exists too 00111 /// far away from the call site to work, this method will be used to make a 00112 /// thunk for it. The stub should be "close" to the current function body, 00113 /// but should not be included in the 'actualsize' returned by 00114 /// startFunctionBody. 00115 virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize, 00116 unsigned Alignment) = 0; 00117 00118 /// endFunctionBody - This method is called when the JIT is done codegen'ing 00119 /// the specified function. At this point we know the size of the JIT 00120 /// compiled function. This passes in FunctionStart (which was returned by 00121 /// the startFunctionBody method) and FunctionEnd which is a pointer to the 00122 /// actual end of the function. This method should mark the space allocated 00123 /// and remember where it is in case the client wants to deallocate it. 00124 virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart, 00125 uint8_t *FunctionEnd) = 0; 00126 00127 /// allocateSpace - Allocate a memory block of the given size. This method 00128 /// cannot be called between calls to startFunctionBody and endFunctionBody. 00129 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) = 0; 00130 00131 /// allocateGlobal - Allocate memory for a global. 00132 /// 00133 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0; 00134 00135 /// deallocateMemForFunction - Free JIT memory for the specified function. 00136 /// This is never called when the JIT is currently emitting a function. 00137 virtual void deallocateMemForFunction(const Function *F) = 0; 00138 00139 /// startExceptionTable - When we finished JITing the function, if exception 00140 /// handling is set, we emit the exception table. 00141 virtual uint8_t* startExceptionTable(const Function* F, 00142 uintptr_t &ActualSize) = 0; 00143 00144 /// endExceptionTable - This method is called when the JIT is done emitting 00145 /// the exception table. 00146 virtual void endExceptionTable(const Function *F, uint8_t *TableStart, 00147 uint8_t *TableEnd, uint8_t* FrameRegister) = 0; 00148 00149 /// CheckInvariants - For testing only. Return true if all internal 00150 /// invariants are preserved, or return false and set ErrorStr to a helpful 00151 /// error message. 00152 virtual bool CheckInvariants(std::string &ErrorStr) { 00153 return true; 00154 } 00155 00156 /// GetDefaultCodeSlabSize - For testing only. Returns DefaultCodeSlabSize 00157 /// from DefaultJITMemoryManager. 00158 virtual size_t GetDefaultCodeSlabSize() { 00159 return 0; 00160 } 00161 00162 /// GetDefaultDataSlabSize - For testing only. Returns DefaultCodeSlabSize 00163 /// from DefaultJITMemoryManager. 00164 virtual size_t GetDefaultDataSlabSize() { 00165 return 0; 00166 } 00167 00168 /// GetDefaultStubSlabSize - For testing only. Returns DefaultCodeSlabSize 00169 /// from DefaultJITMemoryManager. 00170 virtual size_t GetDefaultStubSlabSize() { 00171 return 0; 00172 } 00173 00174 /// GetNumCodeSlabs - For testing only. Returns the number of MemoryBlocks 00175 /// allocated for code. 00176 virtual unsigned GetNumCodeSlabs() { 00177 return 0; 00178 } 00179 00180 /// GetNumDataSlabs - For testing only. Returns the number of MemoryBlocks 00181 /// allocated for data. 00182 virtual unsigned GetNumDataSlabs() { 00183 return 0; 00184 } 00185 00186 /// GetNumStubSlabs - For testing only. Returns the number of MemoryBlocks 00187 /// allocated for function stubs. 00188 virtual unsigned GetNumStubSlabs() { 00189 return 0; 00190 } 00191 }; 00192 00193 } // end namespace llvm. 00194 00195 #endif