1 // AsmJit - Complete JIT Assembler for C++ Language.
3 // Copyright (c) 2008-2010, Petr Kobalicek <kobalicek.petr@gmail.com>
5 // Permission is hereby granted, free of charge, to any person
6 // obtaining a copy of this software and associated documentation
7 // files (the "Software"), to deal in the Software without
8 // restriction, including without limitation the rights to use,
9 // copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 // OTHER DEALINGS IN THE SOFTWARE.
27 #ifndef _ASMJIT_CODEGENERATOR_H
28 #define _ASMJIT_CODEGENERATOR_H
35 // ============================================================================
36 // [Forward Declarations]
37 // ============================================================================
40 struct JitCodeGenerator;
43 // ============================================================================
44 // [AsmJit::CodeGenerator]
45 // ============================================================================
47 //! @brief Code generator is core class for changing behavior of code generated
48 //! by @c Assembler or @c Compiler.
49 struct ASMJIT_API CodeGenerator
51 // --------------------------------------------------------------------------
52 // [Construction / Destruction]
53 // --------------------------------------------------------------------------
55 //! @brief Create a @c CodeGenerator instance.
57 //! @brief Destroy the @c CodeGenerator instance.
58 virtual ~CodeGenerator();
60 // --------------------------------------------------------------------------
62 // --------------------------------------------------------------------------
64 //! @brief Allocate memory for code generated in @a assembler and reloc it
65 //! to target location.
67 //! This method is universal allowing any pre-process / post-process work
68 //! with code generated by @c Assembler or @c Compiler. Because @c Compiler
69 //! always uses @c Assembler it's allowed to access only the @c Assembler
72 //! This method is always last step when using code generation. You can use
73 //! it to allocate memory for JIT code, saving code to remote process or a
76 //! @retrurn Error value, see @c ERROR_CODE.
77 virtual uint32_t generate(void** dest, Assembler* assembler) = 0;
79 // --------------------------------------------------------------------------
81 // --------------------------------------------------------------------------
83 static CodeGenerator* getGlobal();
86 ASMJIT_DISABLE_COPY(CodeGenerator)
89 // ============================================================================
90 // [AsmJit::JitCodeGenerator]
91 // ============================================================================
93 struct JitCodeGenerator : public CodeGenerator
95 // --------------------------------------------------------------------------
96 // [Construction / Destruction]
97 // --------------------------------------------------------------------------
99 //! @brief Create a @c JitCodeGenerator instance.
101 //! @brief Destroy the @c JitCodeGenerator instance.
102 virtual ~JitCodeGenerator();
104 // --------------------------------------------------------------------------
105 // [Memory Manager and Alloc Type]
106 // --------------------------------------------------------------------------
108 // Note: These members can be ignored by all derived classes. They are here
109 // only to privide default implementation. All other implementations (remote
110 // code patching or making dynamic loadable libraries/executables) ignore
111 // members accessed by these accessors.
113 //! @brief Get the @c MemoryManager instance.
114 inline MemoryManager* getMemoryManager() const { return _memoryManager; }
115 //! @brief Set the @c MemoryManager instance.
116 inline void setMemoryManager(MemoryManager* memoryManager) { _memoryManager = memoryManager; }
118 //! @brief Get the type of allocation.
119 inline uint32_t getAllocType() const { return _allocType; }
120 //! @brief Set the type of allocation.
121 inline void setAllocType(uint32_t allocType) { _allocType = allocType; }
123 // --------------------------------------------------------------------------
125 // --------------------------------------------------------------------------
127 virtual uint32_t generate(void** dest, Assembler* assembler);
129 // --------------------------------------------------------------------------
131 // --------------------------------------------------------------------------
134 //! @brief Memory manager.
135 MemoryManager* _memoryManager;
136 //! @brief Type of allocation.
140 ASMJIT_DISABLE_COPY(JitCodeGenerator)
143 } // AsmJit namespace
146 #endif // _ASMJIT_CODEGENERATOR_H