Update ASMJIT (new upstream API)

This commit is contained in:
Nekotekina 2021-12-28 22:25:36 +03:00
parent 900d7df40f
commit cb2748ae08
15 changed files with 550 additions and 458 deletions

View file

@ -4,7 +4,9 @@
// Include asmjit with warnings ignored
#define ASMJIT_EMBED
#define ASMJIT_DEBUG
#define ASMJIT_STATIC
#define ASMJIT_BUILD_DEBUG
#undef Bool
#ifdef _MSC_VER
#pragma warning(push, 0)
@ -49,17 +51,27 @@ enum class jit_class
spu_data,
};
struct jit_runtime_base
{
jit_runtime_base() noexcept = default;
virtual ~jit_runtime_base() = default;
jit_runtime_base(const jit_runtime_base&) = delete;
jit_runtime_base& operator=(const jit_runtime_base&) = delete;
const asmjit::Environment& environment() const noexcept;
void* _add(asmjit::CodeHolder* code) noexcept;
virtual uchar* _alloc(usz size, usz align) noexcept = 0;
};
// ASMJIT runtime for emitting code in a single 2G region
struct jit_runtime final : asmjit::HostRuntime
struct jit_runtime final : jit_runtime_base
{
jit_runtime();
~jit_runtime() override;
// Allocate executable memory
asmjit::Error _add(void** dst, asmjit::CodeHolder* code) noexcept override;
// Do nothing (deallocation is delayed)
asmjit::Error _release(void* p) noexcept override;
uchar* _alloc(usz size, usz align) noexcept override;
// Allocate memory
static u8* alloc(usz size, uint align, bool exec = true) noexcept;
@ -74,35 +86,25 @@ struct jit_runtime final : asmjit::HostRuntime
namespace asmjit
{
// Should only be used to build global functions
asmjit::Runtime& get_global_runtime();
jit_runtime_base& get_global_runtime();
// Don't use directly
class inline_runtime : public HostRuntime
class inline_runtime : public jit_runtime_base
{
uchar* m_data;
usz m_size;
public:
inline_runtime(const inline_runtime&) = delete;
inline_runtime& operator=(const inline_runtime&) = delete;
inline_runtime(uchar* data, usz size)
: m_data(data)
, m_size(size)
{
}
asmjit::Error _add(void** dst, asmjit::CodeHolder* code) noexcept override;
asmjit::Error _release(void*) noexcept override;
inline_runtime(uchar* data, usz size);
~inline_runtime();
uchar* _alloc(usz size, usz align) noexcept override;
};
// Emit xbegin and adjacent loop, return label at xbegin (don't use xabort please)
template <typename F>
[[nodiscard]] inline asmjit::Label build_transaction_enter(asmjit::X86Assembler& c, asmjit::Label fallback, F func)
[[nodiscard]] inline asmjit::Label build_transaction_enter(asmjit::x86::Assembler& c, asmjit::Label fallback, F func)
{
Label fall = c.newLabel();
Label begin = c.newLabel();
@ -117,7 +119,7 @@ namespace asmjit
func();
// Other bad statuses are ignored regardless of repeat flag (TODO)
c.align(kAlignCode, 16);
c.align(AlignMode::kCode, 16);
c.bind(begin);
return fall;
@ -125,7 +127,7 @@ namespace asmjit
}
// Helper to spill RDX (EDX) register for RDTSC
inline void build_swap_rdx_with(asmjit::X86Assembler& c, std::array<X86Gp, 4>& args, const asmjit::X86Gp& with)
inline void build_swap_rdx_with(asmjit::x86::Assembler& c, std::array<x86::Gp, 4>& args, const asmjit::x86::Gp& with)
{
#ifdef _WIN32
c.xchg(args[1], with);
@ -137,7 +139,7 @@ namespace asmjit
}
// Get full RDTSC value into chosen register (clobbers rax/rdx or saves only rax with other target)
inline void build_get_tsc(asmjit::X86Assembler& c, const asmjit::X86Gp& to = asmjit::x86::rax)
inline void build_get_tsc(asmjit::x86::Assembler& c, const asmjit::x86::Gp& to = asmjit::x86::rax)
{
if (&to != &x86::rax && &to != &x86::rdx)
{
@ -164,6 +166,8 @@ namespace asmjit
c.or_(to.r64(), x86::rdx);
}
}
using imm_ptr = Imm;
}
// Build runtime function with asmjit::X86Assembler
@ -175,10 +179,9 @@ inline FT build_function_asm(std::string_view name, F&& builder)
auto& rt = get_global_runtime();
CodeHolder code;
code.init(rt.getCodeInfo());
code._globalHints = asmjit::CodeEmitter::kHintOptimizedAlign;
code.init(rt.environment());
std::array<X86Gp, 4> args;
std::array<x86::Gp, 4> args;
#ifdef _WIN32
args[0] = x86::rcx;
args[1] = x86::rdx;
@ -191,19 +194,12 @@ inline FT build_function_asm(std::string_view name, F&& builder)
args[3] = x86::rcx;
#endif
X86Assembler compiler(&code);
x86::Assembler compiler(&code);
compiler.addEncodingOptions(EncodingOptions::kOptimizedAlign);
builder(std::ref(compiler), args);
ensure(compiler.getLastError() == 0);
FT result;
if (rt.add(&result, &code))
{
return nullptr;
}
jit_announce(result, code.getCodeSize(), name);
return result;
const auto result = rt._add(&code);
jit_announce(result, code.codeSize(), name);
return reinterpret_cast<FT>(uptr(result));
}
#ifdef __APPLE__
@ -253,10 +249,9 @@ public:
inline_runtime rt(m_data, Size);
CodeHolder code;
code.init(rt.getCodeInfo());
code._globalHints = asmjit::CodeEmitter::kHintOptimizedAlign;
code.init(rt.environment());
std::array<X86Gp, 4> args;
std::array<x86::Gp, 4> args;
#ifdef _WIN32
args[0] = x86::rcx;
args[1] = x86::rdx;
@ -269,19 +264,10 @@ public:
args[3] = x86::rcx;
#endif
X86Assembler compiler(&code);
x86::Assembler compiler(&code);
compiler.addEncodingOptions(EncodingOptions::kOptimizedAlign);
builder(std::ref(compiler), args);
FT result;
if (compiler.getLastError() || rt.add(&result, &code))
{
ensure(false);
}
else
{
jit_announce(result, code.getCodeSize(), name);
}
jit_announce(rt._add(&code), code.codeSize(), name);
}
operator FT() const noexcept