LLVM API Documentation

JITMemoryManager.h

Go to the documentation of this file.
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 
00019 namespace llvm {
00020   class Function;
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 {
00027 protected:
00028   bool HasGOT;
00029   bool SizeRequired;
00030 public:
00031   JITMemoryManager() : HasGOT(false), SizeRequired(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(void) = 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(void) = 0;
00045 
00046   //===--------------------------------------------------------------------===//
00047   // Global Offset Table Management
00048   //===--------------------------------------------------------------------===//
00049 
00050   /// AllocateGOT - If the current table requires a Global Offset Table, this
00051   /// method is invoked to allocate it.  This method is required to set HasGOT
00052   /// to true.
00053   virtual void AllocateGOT() = 0;
00054   
00055   /// isManagingGOT - Return true if the AllocateGOT method is called.
00056   ///
00057   bool isManagingGOT() const {
00058     return HasGOT;
00059   }
00060   
00061   /// getGOTBase - If this is managing a Global Offset Table, this method should
00062   /// return a pointer to its base.
00063   virtual unsigned char *getGOTBase() const = 0;
00064   
00065   /// NeedsExactSize - If the memory manager requires to know the size of the
00066   /// objects to be emitted
00067   bool NeedsExactSize() const {
00068     return SizeRequired;
00069   }
00070 
00071   //===--------------------------------------------------------------------===//
00072   // Main Allocation Functions
00073   //===--------------------------------------------------------------------===//
00074   
00075   /// startFunctionBody - When we start JITing a function, the JIT calls this 
00076   /// method to allocate a block of free RWX memory, which returns a pointer to
00077   /// it.  The JIT doesn't know ahead of time how much space it will need to
00078   /// emit the function, so it doesn't pass in the size.  Instead, this method
00079   /// is required to pass back a "valid size".  The JIT will be careful to not
00080   /// write more than the returned ActualSize bytes of memory. 
00081   virtual unsigned char *startFunctionBody(const Function *F, 
00082                                            uintptr_t &ActualSize) = 0;
00083   
00084   /// allocateStub - This method is called by the JIT to allocate space for a
00085   /// function stub (used to handle limited branch displacements) while it is
00086   /// JIT compiling a function.  For example, if foo calls bar, and if bar
00087   /// either needs to be lazily compiled or is a native function that exists too
00088   /// far away from the call site to work, this method will be used to make a
00089   /// thunk for it.  The stub should be "close" to the current function body,
00090   /// but should not be included in the 'actualsize' returned by
00091   /// startFunctionBody.
00092   virtual unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
00093                                       unsigned Alignment) =0;
00094   
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, unsigned char *FunctionStart,
00103                                unsigned char *FunctionEnd) = 0;
00104 
00105   /// allocateSpace - Allocate a memory block of the given size.
00106   virtual unsigned char *allocateSpace(intptr_t Size, unsigned Alignment) = 0;
00107   
00108   /// deallocateMemForFunction - Free JIT memory for the specified function.
00109   /// This is never called when the JIT is currently emitting a function.
00110   virtual void deallocateMemForFunction(const Function *F) = 0;
00111   
00112   /// startExceptionTable - When we finished JITing the function, if exception
00113   /// handling is set, we emit the exception table.
00114   virtual unsigned char* startExceptionTable(const Function* F,
00115                                              uintptr_t &ActualSize) = 0;
00116   
00117   /// endExceptionTable - This method is called when the JIT is done emitting
00118   /// the exception table.
00119   virtual void endExceptionTable(const Function *F, unsigned char *TableStart,
00120                                  unsigned char *TableEnd, 
00121                                  unsigned char* FrameRegister) = 0;
00122 };
00123 
00124 } // end namespace llvm.
00125 
00126 #endif