"openmmapi/vscode:/vscode.git/clone" did not exist on "c4acc7d209b84282bfb1a2321e900b43fdcb95d9"
runtime.h 7.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
// [AsmJit]
// Complete x86/x64 JIT and Remote Assembler for C++.
//
// [License]
// Zlib - See LICENSE.md file in the package.

// [Guard]
#ifndef _ASMJIT_BASE_RUNTIME_H
#define _ASMJIT_BASE_RUNTIME_H

11
12
// [Dependencies]
#include "../base/codeholder.h"
13
14
15
#include "../base/vmem.h"

// [Api-Begin]
16
#include "../asmjit_apibegin.h"
17
18
19
20
21
22
23

namespace asmjit {

// ============================================================================
// [Forward Declarations]
// ============================================================================

24
class CodeHolder;
25

26
//! \addtogroup asmjit_base
27
28
29
30
31
32
33
//! \{

// ============================================================================
// [asmjit::Runtime]
// ============================================================================

//! Base runtime.
34
35
36
37
38
39
40
41
42
class ASMJIT_VIRTAPI Runtime {
public:
  ASMJIT_NONCOPYABLE(Runtime)

  ASMJIT_ENUM(RuntimeType) {
    kRuntimeNone = 0,
    kRuntimeJit = 1,
    kRuntimeRemote = 2
  };
43
44
45
46
47
48

  // --------------------------------------------------------------------------
  // [Construction / Destruction]
  // --------------------------------------------------------------------------

  //! Create a `Runtime` instance.
49
  ASMJIT_API Runtime() noexcept;
50
  //! Destroy the `Runtime` instance.
51
  ASMJIT_API virtual ~Runtime() noexcept;
52
53
54
55
56

  // --------------------------------------------------------------------------
  // [Accessors]
  // --------------------------------------------------------------------------

57
  //! Get CodeInfo of this runtime.
58
  //!
59
60
61
  //! CodeInfo can be used to setup a CodeHolder in case you plan to generate a
  //! code compatible and executable by this Runtime.
  ASMJIT_INLINE const CodeInfo& getCodeInfo() const noexcept { return _codeInfo; }
62

63
64
65
66
67
68
69
  //! Get the Runtime's architecture type, see \ref ArchInfo::Type.
  ASMJIT_INLINE uint32_t getArchType() const noexcept { return _codeInfo.getArchType(); }
  //! Get the Runtime's architecture sub-type, see \ref ArchInfo::SubType.
  ASMJIT_INLINE uint32_t getArchSubType() const noexcept { return _codeInfo.getArchSubType(); }

  //! Get the runtime type, see \ref Type.
  ASMJIT_INLINE uint32_t getRuntimeType() const noexcept { return _runtimeType; }
70
71
72
73
74

  // --------------------------------------------------------------------------
  // [Interface]
  // --------------------------------------------------------------------------

75
76
  // NOTE: To allow passing function pointers to `add()` and `release()` the
  // virtual methods are prefixed with `_` and called from templates.
77

78
79
80
81
82
83
84
85
86
  template<typename Func>
  ASMJIT_INLINE Error add(Func* dst, CodeHolder* code) noexcept {
    return _add(Internal::ptr_cast<void**, Func*>(dst), code);
  }

  template<typename Func>
  ASMJIT_INLINE Error release(Func dst) noexcept {
    return _release(Internal::ptr_cast<void*, Func>(dst));
  }
87

88
  //! Allocate a memory needed for a code stored in the \ref CodeHolder and
89
90
91
  //! relocate it to the target location.
  //!
  //! The beginning of the memory allocated for the function is returned in
92
93
94
  //! `dst`. If failed the \ref Error code is returned and `dst` is set to null
  //! (this means that you don't have to set it to null before calling `add()`).
  virtual Error _add(void** dst, CodeHolder* code) noexcept = 0;
95

96
97
  //! Release `p` allocated by `add()`.
  virtual Error _release(void* p) noexcept = 0;
98
99
100
101
102

  // --------------------------------------------------------------------------
  // [Members]
  // --------------------------------------------------------------------------

103
104
105
106
  CodeInfo _codeInfo;                    //!< Basic information about the Runtime's code.
  uint8_t _runtimeType;                  //!< Type of the runtime.
  uint8_t _allocType;                    //!< Type of the allocator the Runtime uses.
  uint8_t _reserved[6];                  //!< \internal
107
108
109
110
111
112
};

// ============================================================================
// [asmjit::HostRuntime]
// ============================================================================

113
114
115
116
//! Runtime designed to be used in the same process the code is generated in.
class ASMJIT_VIRTAPI HostRuntime : public Runtime {
public:
  ASMJIT_NONCOPYABLE(HostRuntime)
117
118
119
120
121
122

  // --------------------------------------------------------------------------
  // [Construction / Destruction]
  // --------------------------------------------------------------------------

  //! Create a `HostRuntime` instance.
123
  ASMJIT_API HostRuntime() noexcept;
124
  //! Destroy the `HostRuntime` instance.
125
  ASMJIT_API virtual ~HostRuntime() noexcept;
126
127
128
129
130
131
132
133
134

  // --------------------------------------------------------------------------
  // [Interface]
  // --------------------------------------------------------------------------

  //! Flush an instruction cache.
  //!
  //! This member function is called after the code has been copied to the
  //! destination buffer. It is only useful for JIT code generation as it
135
  //! causes a flush of the processor's cache.
136
137
  //!
  //! Flushing is basically a NOP under X86/X64, but is needed by architectures
138
  //! that do not have a transparent instruction cache like ARM.
139
140
141
  //!
  //! This function can also be overridden to improve compatibility with tools
  //! such as Valgrind, however, it's not an official part of AsmJit.
142
  ASMJIT_API virtual void flush(const void* p, size_t size) noexcept;
143
144
145
146
147
148
};

// ============================================================================
// [asmjit::JitRuntime]
// ============================================================================

149
150
151
152
//! Runtime designed to store and execute code generated at runtime (JIT).
class ASMJIT_VIRTAPI JitRuntime : public HostRuntime {
public:
  ASMJIT_NONCOPYABLE(JitRuntime)
153
154
155
156
157
158

  // --------------------------------------------------------------------------
  // [Construction / Destruction]
  // --------------------------------------------------------------------------

  //! Create a `JitRuntime` instance.
159
  ASMJIT_API JitRuntime() noexcept;
160
  //! Destroy the `JitRuntime` instance.
161
  ASMJIT_API virtual ~JitRuntime() noexcept;
162
163
164
165
166
167

  // --------------------------------------------------------------------------
  // [Accessors]
  // --------------------------------------------------------------------------

  //! Get the type of allocation.
168
  ASMJIT_INLINE uint32_t getAllocType() const noexcept { return _allocType; }
169
  //! Set the type of allocation.
170
  ASMJIT_INLINE void setAllocType(uint32_t allocType) noexcept { _allocType = allocType; }
171
172

  //! Get the virtual memory manager.
173
  ASMJIT_INLINE VMemMgr* getMemMgr() const noexcept { return const_cast<VMemMgr*>(&_memMgr); }
174
175
176
177
178

  // --------------------------------------------------------------------------
  // [Interface]
  // --------------------------------------------------------------------------

179
180
  ASMJIT_API Error _add(void** dst, CodeHolder* code) noexcept override;
  ASMJIT_API Error _release(void* p) noexcept override;
181
182
183
184
185
186
187
188
189
190
191
192
193
194

  // --------------------------------------------------------------------------
  // [Members]
  // --------------------------------------------------------------------------

  //! Virtual memory manager.
  VMemMgr _memMgr;
};

//! \}

} // asmjit namespace

// [Api-End]
195
#include "../asmjit_apiend.h"
196
197
198

// [Guard]
#endif // _ASMJIT_BASE_RUNTIME_H