Add support to the JIT for true non-lazy operation. When a call to a function
[oota-llvm.git] / include / llvm / ExecutionEngine / JITMemoryManager.h
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 // This file defines the JITMemoryManagerInterface
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H
15 #define LLVM_EXECUTION_ENGINE_JIT_MEMMANAGER_H
16
17 #include "llvm/Support/DataTypes.h"
18
19 namespace llvm {
20   class Function;
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 {
27 protected:
28   bool HasGOT;
29   bool SizeRequired;
30 public:
31   JITMemoryManager() : HasGOT(false), SizeRequired(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(void) = 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(void) = 0;
45
46   //===--------------------------------------------------------------------===//
47   // Global Offset Table Management
48   //===--------------------------------------------------------------------===//
49
50   /// AllocateGOT - If the current table requires a Global Offset Table, this
51   /// method is invoked to allocate it.  This method is required to set HasGOT
52   /// to true.
53   virtual void AllocateGOT() = 0;
54   
55   /// isManagingGOT - Return true if the AllocateGOT method is called.
56   ///
57   bool isManagingGOT() const {
58     return HasGOT;
59   }
60   
61   /// getGOTBase - If this is managing a Global Offset Table, this method should
62   /// return a pointer to its base.
63   virtual unsigned char *getGOTBase() const = 0;
64   
65   /// SetDlsymTable - If the JIT must be able to relocate stubs after they have
66   /// been emitted, potentially because they are being copied to a process
67   /// where external symbols live at different addresses than in the JITing
68   ///  process, allocate a table with sufficient information to do so.
69   virtual void SetDlsymTable(void *ptr) = 0;
70   
71   /// getDlsymTable - If this is managing a table of entries so that stubs to
72   /// external symbols can be later relocated, this method should return a
73   /// pointer to it.
74   virtual void *getDlsymTable() const = 0;
75   
76   /// NeedsExactSize - If the memory manager requires to know the size of the
77   /// objects to be emitted
78   bool NeedsExactSize() const {
79     return SizeRequired;
80   }
81
82   //===--------------------------------------------------------------------===//
83   // Main Allocation Functions
84   //===--------------------------------------------------------------------===//
85   
86   /// startFunctionBody - When we start JITing a function, the JIT calls this 
87   /// method to allocate a block of free RWX memory, which returns a pointer to
88   /// it.  The JIT doesn't know ahead of time how much space it will need to
89   /// emit the function, so it doesn't pass in the size.  Instead, this method
90   /// is required to pass back a "valid size".  The JIT will be careful to not
91   /// write more than the returned ActualSize bytes of memory. 
92   virtual unsigned char *startFunctionBody(const Function *F, 
93                                            uintptr_t &ActualSize) = 0;
94   
95   /// allocateStub - This method is called by the JIT to allocate space for a
96   /// function stub (used to handle limited branch displacements) while it is
97   /// JIT compiling a function.  For example, if foo calls bar, and if bar
98   /// either needs to be lazily compiled or is a native function that exists too
99   /// far away from the call site to work, this method will be used to make a
100   /// thunk for it.  The stub should be "close" to the current function body,
101   /// but should not be included in the 'actualsize' returned by
102   /// startFunctionBody.
103   virtual unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
104                                       unsigned Alignment) =0;
105   
106   
107   /// endFunctionBody - This method is called when the JIT is done codegen'ing
108   /// the specified function.  At this point we know the size of the JIT
109   /// compiled function.  This passes in FunctionStart (which was returned by
110   /// the startFunctionBody method) and FunctionEnd which is a pointer to the 
111   /// actual end of the function.  This method should mark the space allocated
112   /// and remember where it is in case the client wants to deallocate it.
113   virtual void endFunctionBody(const Function *F, unsigned char *FunctionStart,
114                                unsigned char *FunctionEnd) = 0;
115
116   /// allocateSpace - Allocate a memory block of the given size.
117   virtual unsigned char *allocateSpace(intptr_t Size, unsigned Alignment) = 0;
118   
119   /// deallocateMemForFunction - Free JIT memory for the specified function.
120   /// This is never called when the JIT is currently emitting a function.
121   virtual void deallocateMemForFunction(const Function *F) = 0;
122   
123   /// startExceptionTable - When we finished JITing the function, if exception
124   /// handling is set, we emit the exception table.
125   virtual unsigned char* startExceptionTable(const Function* F,
126                                              uintptr_t &ActualSize) = 0;
127   
128   /// endExceptionTable - This method is called when the JIT is done emitting
129   /// the exception table.
130   virtual void endExceptionTable(const Function *F, unsigned char *TableStart,
131                                  unsigned char *TableEnd, 
132                                  unsigned char* FrameRegister) = 0;
133 };
134
135 } // end namespace llvm.
136
137 #endif