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 #ifndef LLVM_EXECUTIONENGINE_JITMEMORYMANAGER_H 00011 #define LLVM_EXECUTIONENGINE_JITMEMORYMANAGER_H 00012 00013 #include "llvm/ExecutionEngine/RuntimeDyld.h" 00014 #include "llvm/Support/DataTypes.h" 00015 #include <string> 00016 00017 namespace llvm { 00018 00019 class Function; 00020 class GlobalValue; 00021 00022 /// JITMemoryManager - This interface is used by the JIT to allocate and manage 00023 /// memory for the code generated by the JIT. This can be reimplemented by 00024 /// clients that have a strong desire to control how the layout of JIT'd memory 00025 /// works. 00026 class JITMemoryManager : public RTDyldMemoryManager { 00027 protected: 00028 bool HasGOT; 00029 00030 public: 00031 JITMemoryManager() : HasGOT(false) {} 00032 virtual ~JITMemoryManager(); 00033 00034 /// CreateDefaultMemManager - This is used to create the default 00035 /// JIT Memory Manager if the client does not provide one to the JIT. 00036 static JITMemoryManager *CreateDefaultMemManager(); 00037 00038 /// setMemoryWritable - When code generation is in progress, 00039 /// the code pages may need permissions changed. 00040 virtual void setMemoryWritable() = 0; 00041 00042 /// setMemoryExecutable - When code generation is done and we're ready to 00043 /// start execution, the code pages may need permissions changed. 00044 virtual void setMemoryExecutable() = 0; 00045 00046 /// setPoisonMemory - Setting this flag to true makes the memory manager 00047 /// garbage values over freed memory. This is useful for testing and 00048 /// debugging, and may be turned on by default in debug mode. 00049 virtual void setPoisonMemory(bool poison) = 0; 00050 00051 //===--------------------------------------------------------------------===// 00052 // Global Offset Table Management 00053 //===--------------------------------------------------------------------===// 00054 00055 /// AllocateGOT - If the current table requires a Global Offset Table, this 00056 /// method is invoked to allocate it. This method is required to set HasGOT 00057 /// to true. 00058 virtual void AllocateGOT() = 0; 00059 00060 /// isManagingGOT - Return true if the AllocateGOT method is called. 00061 bool isManagingGOT() const { 00062 return HasGOT; 00063 } 00064 00065 /// getGOTBase - If this is managing a Global Offset Table, this method should 00066 /// return a pointer to its base. 00067 virtual uint8_t *getGOTBase() const = 0; 00068 00069 //===--------------------------------------------------------------------===// 00070 // Main Allocation Functions 00071 //===--------------------------------------------------------------------===// 00072 00073 /// startFunctionBody - When we start JITing a function, the JIT calls this 00074 /// method to allocate a block of free RWX memory, which returns a pointer to 00075 /// it. If the JIT wants to request a block of memory of at least a certain 00076 /// size, it passes that value as ActualSize, and this method returns a block 00077 /// with at least that much space. If the JIT doesn't know ahead of time how 00078 /// much space it will need to emit the function, it passes 0 for the 00079 /// ActualSize. In either case, this method is required to pass back the size 00080 /// of the allocated block through ActualSize. The JIT will be careful to 00081 /// not write more than the returned ActualSize bytes of memory. 00082 virtual uint8_t *startFunctionBody(const Function *F, 00083 uintptr_t &ActualSize) = 0; 00084 00085 /// allocateStub - This method is called by the JIT to allocate space for a 00086 /// function stub (used to handle limited branch displacements) while it is 00087 /// JIT compiling a function. For example, if foo calls bar, and if bar 00088 /// either needs to be lazily compiled or is a native function that exists too 00089 /// far away from the call site to work, this method will be used to make a 00090 /// thunk for it. The stub should be "close" to the current function body, 00091 /// but should not be included in the 'actualsize' returned by 00092 /// startFunctionBody. 00093 virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize, 00094 unsigned Alignment) = 0; 00095 00096 /// endFunctionBody - This method is called when the JIT is done codegen'ing 00097 /// the specified function. At this point we know the size of the JIT 00098 /// compiled function. This passes in FunctionStart (which was returned by 00099 /// the startFunctionBody method) and FunctionEnd which is a pointer to the 00100 /// actual end of the function. This method should mark the space allocated 00101 /// and remember where it is in case the client wants to deallocate it. 00102 virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart, 00103 uint8_t *FunctionEnd) = 0; 00104 00105 /// allocateSpace - Allocate a memory block of the given size. This method 00106 /// cannot be called between calls to startFunctionBody and endFunctionBody. 00107 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) = 0; 00108 00109 /// allocateGlobal - Allocate memory for a global. 00110 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0; 00111 00112 /// deallocateFunctionBody - Free the specified function body. The argument 00113 /// must be the return value from a call to startFunctionBody() that hasn't 00114 /// been deallocated yet. This is never called when the JIT is currently 00115 /// emitting a function. 00116 virtual void deallocateFunctionBody(void *Body) = 0; 00117 00118 /// CheckInvariants - For testing only. Return true if all internal 00119 /// invariants are preserved, or return false and set ErrorStr to a helpful 00120 /// error message. 00121 virtual bool CheckInvariants(std::string &) { 00122 return true; 00123 } 00124 00125 /// GetDefaultCodeSlabSize - For testing only. Returns DefaultCodeSlabSize 00126 /// from DefaultJITMemoryManager. 00127 virtual size_t GetDefaultCodeSlabSize() { 00128 return 0; 00129 } 00130 00131 /// GetDefaultDataSlabSize - For testing only. Returns DefaultCodeSlabSize 00132 /// from DefaultJITMemoryManager. 00133 virtual size_t GetDefaultDataSlabSize() { 00134 return 0; 00135 } 00136 00137 /// GetDefaultStubSlabSize - For testing only. Returns DefaultCodeSlabSize 00138 /// from DefaultJITMemoryManager. 00139 virtual size_t GetDefaultStubSlabSize() { 00140 return 0; 00141 } 00142 00143 /// GetNumCodeSlabs - For testing only. Returns the number of MemoryBlocks 00144 /// allocated for code. 00145 virtual unsigned GetNumCodeSlabs() { 00146 return 0; 00147 } 00148 00149 /// GetNumDataSlabs - For testing only. Returns the number of MemoryBlocks 00150 /// allocated for data. 00151 virtual unsigned GetNumDataSlabs() { 00152 return 0; 00153 } 00154 00155 /// GetNumStubSlabs - For testing only. Returns the number of MemoryBlocks 00156 /// allocated for function stubs. 00157 virtual unsigned GetNumStubSlabs() { 00158 return 0; 00159 } 00160 }; 00161 00162 } // end namespace llvm. 00163 00164 #endif