runtime.cpp 5.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// [AsmJit]
// Complete x86/x64 JIT and Remote Assembler for C++.
//
// [License]
// Zlib - See LICENSE.md file in the package.

// [Export]
#define ASMJIT_EXPORTS

// [Dependencies - AsmJit]
#include "../base/assembler.h"
#include "../base/cpuinfo.h"
#include "../base/error.h"
#include "../base/runtime.h"

// [Api-Begin]
#include "../apibegin.h"

namespace asmjit {

// ============================================================================
// [asmjit::Runtime - Construction / Destruction]
// ============================================================================

Runtime::Runtime() {
  _sizeLimit = 0;

  _runtimeType = kRuntimeTypeNone;
  _allocType = kVMemAllocFreeable;
  ::memset(_reserved, 0, sizeof(_reserved));

  _baseAddress = kNoBaseAddress;
}

Runtime::~Runtime() {}

// ============================================================================
// [asmjit::HostRuntime - Construction / Destruction]
// ============================================================================

HostRuntime::HostRuntime() {
  _runtimeType = kRuntimeTypeJit;
}

HostRuntime::~HostRuntime() {}

// ============================================================================
// [asmjit::HostRuntime - Interface]
// ============================================================================

const CpuInfo* HostRuntime::getCpuInfo() {
  return CpuInfo::getHost();
}

uint32_t HostRuntime::getStackAlignment() {
  uint32_t alignment = sizeof(intptr_t);

#if defined(ASMJIT_HOST_X86)
  // Modern Linux, APPLE and UNIX guarantees 16-byte stack alignment, but I'm
  // not sure about all other UNIX operating systems, because 16-byte alignment
  // is addition to an older specification.
# if (defined(__linux__)   || \
      defined(__linux)     || \
      defined(__unix__)    || \
      defined(__FreeBSD__) || \
      defined(__NetBSD__)  || \
      defined(__OpenBSD__) || \
      defined(__DARWIN__)  || \
      defined(__APPLE__)   )
  alignment = 16;
# endif
#elif defined(ASMJIT_HOST_X64)
  alignment = 16;
#endif

  return alignment;
}

void HostRuntime::flush(void* p, size_t size) {
  // Only useful on non-x86 architectures.
#if !defined(ASMJIT_HOST_X86) && !defined(ASMJIT_HOST_X64)

  // Windows has built-in support in kernel32.dll.
#if defined(ASMJIT_OS_WINDOWS)
  ::FlushInstructionCache(_memMgr.getProcessHandle(), p, size);
#endif // ASMJIT_OS_WINDOWS

#endif // !ASMJIT_HOST_X86 && !ASMJIT_HOST_X64
}

// ============================================================================
// [asmjit::StaticRuntime - Construction / Destruction]
// ============================================================================

StaticRuntime::StaticRuntime(void* baseAddress, size_t sizeLimit) {
  _sizeLimit = sizeLimit;
  _baseAddress = static_cast<Ptr>((uintptr_t)baseAddress);
}

StaticRuntime::~StaticRuntime() {}

// ============================================================================
// [asmjit::StaticRuntime - Interface]
// ============================================================================

Error StaticRuntime::add(void** dst, Assembler* assembler) {
  size_t codeSize = assembler->getCodeSize();
  size_t sizeLimit = _sizeLimit;

  if (codeSize == 0) {
    *dst = NULL;
    return kErrorNoCodeGenerated;
  }

  if (sizeLimit != 0 && sizeLimit < codeSize) {
    *dst = NULL;
    return kErrorCodeTooLarge;
  }

  Ptr baseAddress = _baseAddress;
  uint8_t* p = static_cast<uint8_t*>((void*)static_cast<uintptr_t>(baseAddress));

  // Since the base address is known the `relocSize` returned should be equal
  // to `codeSize`. It's better to fail if they don't match instead of passsing
  // silently.
  size_t relocSize = assembler->relocCode(p, baseAddress);
  if (relocSize == 0 || codeSize != relocSize) {
    *dst = NULL;
    return kErrorInvalidState;
  }

  _baseAddress += codeSize;
  if (sizeLimit)
    sizeLimit -= codeSize;

  flush(p, codeSize);
  *dst = p;

  return kErrorOk;
}

Error StaticRuntime::release(void* p) {
  // There is nothing to release as `StaticRuntime` doesn't manage any memory.
  ASMJIT_UNUSED(p);
  return kErrorOk;
}

// ============================================================================
// [asmjit::JitRuntime - Construction / Destruction]
// ============================================================================

JitRuntime::JitRuntime() {}
JitRuntime::~JitRuntime() {}

// ============================================================================
// [asmjit::JitRuntime - Interface]
// ============================================================================

Error JitRuntime::add(void** dst, Assembler* assembler) {
  size_t codeSize = assembler->getCodeSize();
  if (codeSize == 0) {
    *dst = NULL;
    return kErrorNoCodeGenerated;
  }

  void* p = _memMgr.alloc(codeSize, getAllocType());
  if (p == NULL) {
    *dst = NULL;
    return kErrorNoVirtualMemory;
  }

  // Relocate the code and release the unused memory back to `VMemMgr`.
  size_t relocSize = assembler->relocCode(p);
  if (relocSize < codeSize) {
    _memMgr.shrink(p, relocSize);
  }

  flush(p, relocSize);
  *dst = p;

  return kErrorOk;
}

Error JitRuntime::release(void* p) {
  return _memMgr.release(p);
}

} // asmjit namespace

// [Api-End]
#include "../apiend.h"