Factor out debugging code into the common base class.
[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   //===--------------------------------------------------------------------===//
39   // Global Offset Table Management
40   //===--------------------------------------------------------------------===//
41
42   /// AllocateGOT - If the current table requires a Global Offset Table, this
43   /// method is invoked to allocate it.  This method is required to set HasGOT
44   /// to true.
45   virtual void AllocateGOT() = 0;
46   
47   /// isManagingGOT - Return true if the AllocateGOT method is called.
48   ///
49   bool isManagingGOT() const {
50     return HasGOT;
51   }
52   
53   /// getGOTBase - If this is managing a Global Offset Table, this method should
54   /// return a pointer to its base.
55   virtual unsigned char *getGOTBase() const = 0;
56   
57   /// NeedsExactSize - If the memory manager requires to know the size of the
58   /// objects to be emitted
59   bool NeedsExactSize() const {
60     return SizeRequired;
61   }
62
63   //===--------------------------------------------------------------------===//
64   // Main Allocation Functions
65   //===--------------------------------------------------------------------===//
66   
67   /// startFunctionBody - When we start JITing a function, the JIT calls this 
68   /// method to allocate a block of free RWX memory, which returns a pointer to
69   /// it.  The JIT doesn't know ahead of time how much space it will need to
70   /// emit the function, so it doesn't pass in the size.  Instead, this method
71   /// is required to pass back a "valid size".  The JIT will be careful to not
72   /// write more than the returned ActualSize bytes of memory. 
73   virtual unsigned char *startFunctionBody(const Function *F, 
74                                            uintptr_t &ActualSize) = 0;
75   
76   /// allocateStub - This method is called by the JIT to allocate space for a
77   /// function stub (used to handle limited branch displacements) while it is
78   /// JIT compiling a function.  For example, if foo calls bar, and if bar
79   /// either needs to be lazily compiled or is a native function that exists too
80   /// far away from the call site to work, this method will be used to make a
81   /// thunk for it.  The stub should be "close" to the current function body,
82   /// but should not be included in the 'actualsize' returned by
83   /// startFunctionBody.
84   virtual unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
85                                       unsigned Alignment) =0;
86   
87   
88   /// endFunctionBody - This method is called when the JIT is done codegen'ing
89   /// the specified function.  At this point we know the size of the JIT
90   /// compiled function.  This passes in FunctionStart (which was returned by
91   /// the startFunctionBody method) and FunctionEnd which is a pointer to the 
92   /// actual end of the function.  This method should mark the space allocated
93   /// and remember where it is in case the client wants to deallocate it.
94   virtual void endFunctionBody(const Function *F, unsigned char *FunctionStart,
95                                unsigned char *FunctionEnd) = 0;
96   
97   /// deallocateMemForFunction - Free JIT memory for the specified function.
98   /// This is never called when the JIT is currently emitting a function.
99   virtual void deallocateMemForFunction(const Function *F) = 0;
100   
101   /// startExceptionTable - When we finished JITing the function, if exception
102   /// handling is set, we emit the exception table.
103   virtual unsigned char* startExceptionTable(const Function* F,
104                                              uintptr_t &ActualSize) = 0;
105   
106   /// endExceptionTable - This method is called when the JIT is done emitting
107   /// the exception table.
108   virtual void endExceptionTable(const Function *F, unsigned char *TableStart,
109                                  unsigned char *TableEnd, 
110                                  unsigned char* FrameRegister) = 0;
111 };
112
113 } // end namespace llvm.
114
115 #endif