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 // This file defines the JITMemoryManagerInterface
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H
15 #define LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H
17 #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 {
32 JITMemoryManager() : HasGOT(false), SizeRequired(false) {}
33 virtual ~JITMemoryManager();
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();
39 /// setMemoryWritable - When code generation is in progress,
40 /// the code pages may need permissions changed.
41 virtual void setMemoryWritable(void) = 0;
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(void) = 0;
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 is be turned on by default in debug mode.
50 virtual void setPoisonMemory(bool poison) = 0;
52 //===--------------------------------------------------------------------===//
53 // Global Offset Table Management
54 //===--------------------------------------------------------------------===//
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
59 virtual void AllocateGOT() = 0;
61 /// isManagingGOT - Return true if the AllocateGOT method is called.
63 bool isManagingGOT() const {
67 /// getGOTBase - If this is managing a Global Offset Table, this method should
68 /// return a pointer to its base.
69 virtual uint8_t *getGOTBase() const = 0;
71 /// SetDlsymTable - If the JIT must be able to relocate stubs after they have
72 /// been emitted, potentially because they are being copied to a process
73 /// where external symbols live at different addresses than in the JITing
74 /// process, allocate a table with sufficient information to do so.
75 virtual void SetDlsymTable(void *ptr) = 0;
77 /// getDlsymTable - If this is managing a table of entries so that stubs to
78 /// external symbols can be later relocated, this method should return a
80 virtual void *getDlsymTable() const = 0;
82 /// NeedsExactSize - If the memory manager requires to know the size of the
83 /// objects to be emitted
84 bool NeedsExactSize() const {
88 //===--------------------------------------------------------------------===//
89 // Main Allocation Functions
90 //===--------------------------------------------------------------------===//
92 /// startFunctionBody - When we start JITing a function, the JIT calls this
93 /// method to allocate a block of free RWX memory, which returns a pointer to
94 /// it. The JIT doesn't know ahead of time how much space it will need to
95 /// emit the function, so it doesn't pass in the size. Instead, this method
96 /// is required to pass back a "valid size". The JIT will be careful to not
97 /// write more than the returned ActualSize bytes of memory.
98 virtual uint8_t *startFunctionBody(const Function *F,
99 uintptr_t &ActualSize) = 0;
101 /// allocateStub - This method is called by the JIT to allocate space for a
102 /// function stub (used to handle limited branch displacements) while it is
103 /// JIT compiling a function. For example, if foo calls bar, and if bar
104 /// either needs to be lazily compiled or is a native function that exists too
105 /// far away from the call site to work, this method will be used to make a
106 /// thunk for it. The stub should be "close" to the current function body,
107 /// but should not be included in the 'actualsize' returned by
108 /// startFunctionBody.
109 virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
110 unsigned Alignment) = 0;
112 /// endFunctionBody - This method is called when the JIT is done codegen'ing
113 /// the specified function. At this point we know the size of the JIT
114 /// compiled function. This passes in FunctionStart (which was returned by
115 /// the startFunctionBody method) and FunctionEnd which is a pointer to the
116 /// actual end of the function. This method should mark the space allocated
117 /// and remember where it is in case the client wants to deallocate it.
118 virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
119 uint8_t *FunctionEnd) = 0;
121 /// allocateSpace - Allocate a memory block of the given size.
122 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) = 0;
124 /// allocateGlobal - Allocate memory for a global.
125 virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
127 /// deallocateMemForFunction - Free JIT memory for the specified function.
128 /// This is never called when the JIT is currently emitting a function.
129 virtual void deallocateMemForFunction(const Function *F) = 0;
131 /// startExceptionTable - When we finished JITing the function, if exception
132 /// handling is set, we emit the exception table.
133 virtual uint8_t* startExceptionTable(const Function* F,
134 uintptr_t &ActualSize) = 0;
136 /// endExceptionTable - This method is called when the JIT is done emitting
137 /// the exception table.
138 virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
139 uint8_t *TableEnd, uint8_t* FrameRegister) = 0;
142 } // end namespace llvm.