Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
7.4 KiB

  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. #ifndef LLVM_EXECUTIONENGINE_JITMEMORYMANAGER_H
  10. #define LLVM_EXECUTIONENGINE_JITMEMORYMANAGER_H
  11. #include "llvm/ExecutionEngine/RuntimeDyld.h"
  12. #include "llvm/Support/DataTypes.h"
  13. #include <string>
  14. namespace llvm {
  15. class Function;
  16. class GlobalValue;
  17. /// JITMemoryManager - This interface is used by the JIT to allocate and manage
  18. /// memory for the code generated by the JIT. This can be reimplemented by
  19. /// clients that have a strong desire to control how the layout of JIT'd memory
  20. /// works.
  21. class JITMemoryManager : public RTDyldMemoryManager {
  22. protected:
  23. bool HasGOT;
  24. public:
  25. JITMemoryManager() : HasGOT(false) {}
  26. virtual ~JITMemoryManager();
  27. /// CreateDefaultMemManager - This is used to create the default
  28. /// JIT Memory Manager if the client does not provide one to the JIT.
  29. static JITMemoryManager *CreateDefaultMemManager();
  30. /// setMemoryWritable - When code generation is in progress,
  31. /// the code pages may need permissions changed.
  32. virtual void setMemoryWritable() = 0;
  33. /// setMemoryExecutable - When code generation is done and we're ready to
  34. /// start execution, the code pages may need permissions changed.
  35. virtual void setMemoryExecutable() = 0;
  36. /// setPoisonMemory - Setting this flag to true makes the memory manager
  37. /// garbage values over freed memory. This is useful for testing and
  38. /// debugging, and may be turned on by default in debug mode.
  39. virtual void setPoisonMemory(bool poison) = 0;
  40. //===--------------------------------------------------------------------===//
  41. // Global Offset Table Management
  42. //===--------------------------------------------------------------------===//
  43. /// AllocateGOT - If the current table requires a Global Offset Table, this
  44. /// method is invoked to allocate it. This method is required to set HasGOT
  45. /// to true.
  46. virtual void AllocateGOT() = 0;
  47. /// isManagingGOT - Return true if the AllocateGOT method is called.
  48. bool isManagingGOT() const {
  49. return HasGOT;
  50. }
  51. /// getGOTBase - If this is managing a Global Offset Table, this method should
  52. /// return a pointer to its base.
  53. virtual uint8_t *getGOTBase() const = 0;
  54. //===--------------------------------------------------------------------===//
  55. // Main Allocation Functions
  56. //===--------------------------------------------------------------------===//
  57. /// startFunctionBody - When we start JITing a function, the JIT calls this
  58. /// method to allocate a block of free RWX memory, which returns a pointer to
  59. /// it. If the JIT wants to request a block of memory of at least a certain
  60. /// size, it passes that value as ActualSize, and this method returns a block
  61. /// with at least that much space. If the JIT doesn't know ahead of time how
  62. /// much space it will need to emit the function, it passes 0 for the
  63. /// ActualSize. In either case, this method is required to pass back the size
  64. /// of the allocated block through ActualSize. The JIT will be careful to
  65. /// not write more than the returned ActualSize bytes of memory.
  66. virtual uint8_t *startFunctionBody(const Function *F,
  67. uintptr_t &ActualSize) = 0;
  68. /// allocateStub - This method is called by the JIT to allocate space for a
  69. /// function stub (used to handle limited branch displacements) while it is
  70. /// JIT compiling a function. For example, if foo calls bar, and if bar
  71. /// either needs to be lazily compiled or is a native function that exists too
  72. /// far away from the call site to work, this method will be used to make a
  73. /// thunk for it. The stub should be "close" to the current function body,
  74. /// but should not be included in the 'actualsize' returned by
  75. /// startFunctionBody.
  76. virtual uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
  77. unsigned Alignment) = 0;
  78. /// endFunctionBody - This method is called when the JIT is done codegen'ing
  79. /// the specified function. At this point we know the size of the JIT
  80. /// compiled function. This passes in FunctionStart (which was returned by
  81. /// the startFunctionBody method) and FunctionEnd which is a pointer to the
  82. /// actual end of the function. This method should mark the space allocated
  83. /// and remember where it is in case the client wants to deallocate it.
  84. virtual void endFunctionBody(const Function *F, uint8_t *FunctionStart,
  85. uint8_t *FunctionEnd) = 0;
  86. /// allocateSpace - Allocate a memory block of the given size. This method
  87. /// cannot be called between calls to startFunctionBody and endFunctionBody.
  88. virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) = 0;
  89. /// allocateGlobal - Allocate memory for a global.
  90. virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
  91. /// deallocateFunctionBody - Free the specified function body. The argument
  92. /// must be the return value from a call to startFunctionBody() that hasn't
  93. /// been deallocated yet. This is never called when the JIT is currently
  94. /// emitting a function.
  95. virtual void deallocateFunctionBody(void *Body) = 0;
  96. /// startExceptionTable - When we finished JITing the function, if exception
  97. /// handling is set, we emit the exception table.
  98. virtual uint8_t* startExceptionTable(const Function* F,
  99. uintptr_t &ActualSize) = 0;
  100. /// endExceptionTable - This method is called when the JIT is done emitting
  101. /// the exception table.
  102. virtual void endExceptionTable(const Function *F, uint8_t *TableStart,
  103. uint8_t *TableEnd, uint8_t* FrameRegister) = 0;
  104. /// deallocateExceptionTable - Free the specified exception table's memory.
  105. /// The argument must be the return value from a call to startExceptionTable()
  106. /// that hasn't been deallocated yet. This is never called when the JIT is
  107. /// currently emitting an exception table.
  108. virtual void deallocateExceptionTable(void *ET) = 0;
  109. /// CheckInvariants - For testing only. Return true if all internal
  110. /// invariants are preserved, or return false and set ErrorStr to a helpful
  111. /// error message.
  112. virtual bool CheckInvariants(std::string &) {
  113. return true;
  114. }
  115. /// GetDefaultCodeSlabSize - For testing only. Returns DefaultCodeSlabSize
  116. /// from DefaultJITMemoryManager.
  117. virtual size_t GetDefaultCodeSlabSize() {
  118. return 0;
  119. }
  120. /// GetDefaultDataSlabSize - For testing only. Returns DefaultCodeSlabSize
  121. /// from DefaultJITMemoryManager.
  122. virtual size_t GetDefaultDataSlabSize() {
  123. return 0;
  124. }
  125. /// GetDefaultStubSlabSize - For testing only. Returns DefaultCodeSlabSize
  126. /// from DefaultJITMemoryManager.
  127. virtual size_t GetDefaultStubSlabSize() {
  128. return 0;
  129. }
  130. /// GetNumCodeSlabs - For testing only. Returns the number of MemoryBlocks
  131. /// allocated for code.
  132. virtual unsigned GetNumCodeSlabs() {
  133. return 0;
  134. }
  135. /// GetNumDataSlabs - For testing only. Returns the number of MemoryBlocks
  136. /// allocated for data.
  137. virtual unsigned GetNumDataSlabs() {
  138. return 0;
  139. }
  140. /// GetNumStubSlabs - For testing only. Returns the number of MemoryBlocks
  141. /// allocated for function stubs.
  142. virtual unsigned GetNumStubSlabs() {
  143. return 0;
  144. }
  145. };
  146. } // end namespace llvm.
  147. #endif