Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2008-2017, Petr Kobalicek 3 : 4 : This software is provided 'as-is', without any express or implied 5 : warranty. In no event will the authors be held liable for any damages 6 : arising from the use of this software. 7 : 8 : Permission is granted to anyone to use this software for any purpose, 9 : including commercial applications, and to alter it and redistribute it 10 : freely, subject to the following restrictions: 11 : 12 : 1. The origin of this software must not be misrepresented; you must not 13 : claim that you wrote the original software. If you use this software 14 : in a product, an acknowledgment in the product documentation would be 15 : appreciated but is not required. 16 : 2. Altered source versions must be plainly marked as such, and must not be 17 : misrepresented as being the original software. 18 : 3. This notice may not be removed or altered from any source distribution. 19 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 20 : #ifndef __PLUMED_asmjit_runtime_h 21 : #define __PLUMED_asmjit_runtime_h 22 : #ifdef __PLUMED_HAS_ASMJIT 23 : #pragma GCC diagnostic push 24 : #pragma GCC diagnostic ignored "-Wpedantic" 25 : // [AsmJit] 26 : // Complete x86/x64 JIT and Remote Assembler for C++. 27 : // 28 : // [License] 29 : // Zlib - See LICENSE.md file in the package. 30 : 31 : // [Guard] 32 : #ifndef _ASMJIT_BASE_RUNTIME_H 33 : #define _ASMJIT_BASE_RUNTIME_H 34 : 35 : // [Dependencies] 36 : #include "./codeholder.h" 37 : #include "./vmem.h" 38 : 39 : // [Api-Begin] 40 : #include "./asmjit_apibegin.h" 41 : 42 : namespace PLMD { 43 : namespace asmjit { 44 : 45 : // ============================================================================ 46 : // [Forward Declarations] 47 : // ============================================================================ 48 : 49 : class CodeHolder; 50 : 51 : //! \addtogroup asmjit_base 52 : //! \{ 53 : 54 : // ============================================================================ 55 : // [asmjit::Runtime] 56 : // ============================================================================ 57 : 58 : //! Base runtime. 59 : class ASMJIT_VIRTAPI Runtime { 60 : public: 61 : ASMJIT_NONCOPYABLE(Runtime) 62 : 63 : ASMJIT_ENUM(RuntimeType) { 64 : kRuntimeNone = 0, 65 : kRuntimeJit = 1, 66 : kRuntimeRemote = 2 67 : }; 68 : 69 : // -------------------------------------------------------------------------- 70 : // [Construction / Destruction] 71 : // -------------------------------------------------------------------------- 72 : 73 : //! Create a `Runtime` instance. 74 : ASMJIT_API Runtime() noexcept; 75 : //! Destroy the `Runtime` instance. 76 : ASMJIT_API virtual ~Runtime() noexcept; 77 : 78 : // -------------------------------------------------------------------------- 79 : // [Accessors] 80 : // -------------------------------------------------------------------------- 81 : 82 : //! Get CodeInfo of this runtime. 83 : //! 84 : //! CodeInfo can be used to setup a CodeHolder in case you plan to generate a 85 : //! code compatible and executable by this Runtime. 86 1944 : ASMJIT_INLINE const CodeInfo& getCodeInfo() const noexcept { return _codeInfo; } 87 : 88 : //! Get the Runtime's architecture type, see \ref ArchInfo::Type. 89 : ASMJIT_INLINE uint32_t getArchType() const noexcept { return _codeInfo.getArchType(); } 90 : //! Get the Runtime's architecture sub-type, see \ref ArchInfo::SubType. 91 : ASMJIT_INLINE uint32_t getArchSubType() const noexcept { return _codeInfo.getArchSubType(); } 92 : 93 : //! Get the runtime type, see \ref Type. 94 : ASMJIT_INLINE uint32_t getRuntimeType() const noexcept { return _runtimeType; } 95 : 96 : // -------------------------------------------------------------------------- 97 : // [Interface] 98 : // -------------------------------------------------------------------------- 99 : 100 : // NOTE: To allow passing function pointers to `add()` and `release()` the 101 : // virtual methods are prefixed with `_` and called from templates. 102 : 103 : template<typename Func> 104 : ASMJIT_INLINE Error add(Func* dst, CodeHolder* code) noexcept { 105 1944 : return _add(Internal::ptr_cast<void**, Func*>(dst), code); 106 : } 107 : 108 : template<typename Func> 109 : ASMJIT_INLINE Error release(Func dst) noexcept { 110 : return _release(Internal::ptr_cast<void*, Func>(dst)); 111 : } 112 : 113 : //! Allocate a memory needed for a code stored in the \ref CodeHolder and 114 : //! relocate it to the target location. 115 : //! 116 : //! The beginning of the memory allocated for the function is returned in 117 : //! `dst`. If failed the \ref Error code is returned and `dst` is set to null 118 : //! (this means that you don't have to set it to null before calling `add()`). 119 : virtual Error _add(void** dst, CodeHolder* code) noexcept = 0; 120 : 121 : //! Release `p` allocated by `add()`. 122 : virtual Error _release(void* p) noexcept = 0; 123 : 124 : // -------------------------------------------------------------------------- 125 : // [Members] 126 : // -------------------------------------------------------------------------- 127 : 128 : CodeInfo _codeInfo; //!< Basic information about the Runtime's code. 129 : uint8_t _runtimeType; //!< Type of the runtime. 130 : uint8_t _allocType; //!< Type of the allocator the Runtime uses. 131 : uint8_t _reserved[6]; //!< \internal 132 : }; 133 : 134 : // ============================================================================ 135 : // [asmjit::HostRuntime] 136 : // ============================================================================ 137 : 138 : //! Runtime designed to be used in the same process the code is generated in. 139 : class ASMJIT_VIRTAPI HostRuntime : public Runtime { 140 : public: 141 : ASMJIT_NONCOPYABLE(HostRuntime) 142 : 143 : // -------------------------------------------------------------------------- 144 : // [Construction / Destruction] 145 : // -------------------------------------------------------------------------- 146 : 147 : //! Create a `HostRuntime` instance. 148 : ASMJIT_API HostRuntime() noexcept; 149 : //! Destroy the `HostRuntime` instance. 150 : ASMJIT_API virtual ~HostRuntime() noexcept; 151 : 152 : // -------------------------------------------------------------------------- 153 : // [Interface] 154 : // -------------------------------------------------------------------------- 155 : 156 : //! Flush an instruction cache. 157 : //! 158 : //! This member function is called after the code has been copied to the 159 : //! destination buffer. It is only useful for JIT code generation as it 160 : //! causes a flush of the processor's cache. 161 : //! 162 : //! Flushing is basically a NOP under X86/X64, but is needed by architectures 163 : //! that do not have a transparent instruction cache like ARM. 164 : //! 165 : //! This function can also be overridden to improve compatibility with tools 166 : //! such as Valgrind, however, it's not an official part of AsmJit. 167 : ASMJIT_API virtual void flush(const void* p, size_t size) noexcept; 168 : }; 169 : 170 : // ============================================================================ 171 : // [asmjit::JitRuntime] 172 : // ============================================================================ 173 : 174 : //! Runtime designed to store and execute code generated at runtime (JIT). 175 : class ASMJIT_VIRTAPI JitRuntime : public HostRuntime { 176 : public: 177 : ASMJIT_NONCOPYABLE(JitRuntime) 178 : 179 : // -------------------------------------------------------------------------- 180 : // [Construction / Destruction] 181 : // -------------------------------------------------------------------------- 182 : 183 : //! Create a `JitRuntime` instance. 184 : ASMJIT_API JitRuntime() noexcept; 185 : //! Destroy the `JitRuntime` instance. 186 : ASMJIT_API virtual ~JitRuntime() noexcept; 187 : 188 : // -------------------------------------------------------------------------- 189 : // [Accessors] 190 : // -------------------------------------------------------------------------- 191 : 192 : //! Get the type of allocation. 193 1944 : ASMJIT_INLINE uint32_t getAllocType() const noexcept { return _allocType; } 194 : //! Set the type of allocation. 195 : ASMJIT_INLINE void setAllocType(uint32_t allocType) noexcept { _allocType = allocType; } 196 : 197 : //! Get the virtual memory manager. 198 : ASMJIT_INLINE VMemMgr* getMemMgr() const noexcept { return const_cast<VMemMgr*>(&_memMgr); } 199 : 200 : // -------------------------------------------------------------------------- 201 : // [Interface] 202 : // -------------------------------------------------------------------------- 203 : 204 : ASMJIT_API Error _add(void** dst, CodeHolder* code) noexcept override; 205 : ASMJIT_API Error _release(void* p) noexcept override; 206 : 207 : // -------------------------------------------------------------------------- 208 : // [Members] 209 : // -------------------------------------------------------------------------- 210 : 211 : //! Virtual memory manager. 212 : VMemMgr _memMgr; 213 : }; 214 : 215 : //! \} 216 : 217 : } // asmjit namespace 218 : } // namespace PLMD 219 : 220 : // [Api-End] 221 : #include "./asmjit_apiend.h" 222 : 223 : // [Guard] 224 : #endif // _ASMJIT_BASE_RUNTIME_H 225 : #pragma GCC diagnostic pop 226 : #endif // __PLUMED_HAS_ASMJIT 227 : #endif