2016-06-22 13:37:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-11-29 23:11:28 +00:00
|
|
|
// Include asmjit with warnings ignored
|
2018-06-12 18:03:53 +00:00
|
|
|
#define ASMJIT_EMBED
|
2018-05-14 20:06:17 +00:00
|
|
|
#define ASMJIT_DEBUG
|
|
|
|
|
2019-11-29 23:11:28 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 0)
|
|
|
|
#include <asmjit/asmjit.h>
|
|
|
|
#pragma warning(pop)
|
|
|
|
#else
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wall"
|
|
|
|
#pragma GCC diagnostic ignored "-Wextra"
|
|
|
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
2021-03-05 19:05:37 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
2021-03-08 20:41:23 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
2021-03-23 19:32:50 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wredundant-decls"
|
2021-04-03 16:38:02 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
2021-03-30 15:31:46 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Weffc++"
|
2021-03-13 15:03:08 +00:00
|
|
|
#ifndef __clang__
|
|
|
|
#pragma GCC diagnostic ignored "-Wduplicated-branches"
|
|
|
|
#endif
|
2018-06-12 18:03:53 +00:00
|
|
|
#include <asmjit/asmjit.h>
|
2019-11-29 23:11:28 +00:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2018-05-16 23:38:14 +00:00
|
|
|
#include <array>
|
2018-05-14 20:06:17 +00:00
|
|
|
#include <functional>
|
|
|
|
|
2019-01-28 17:23:26 +00:00
|
|
|
enum class jit_class
|
|
|
|
{
|
|
|
|
ppu_code,
|
|
|
|
ppu_data,
|
|
|
|
spu_code,
|
|
|
|
spu_data,
|
|
|
|
};
|
|
|
|
|
2019-01-26 20:15:45 +00:00
|
|
|
// ASMJIT runtime for emitting code in a single 2G region
|
|
|
|
struct jit_runtime final : asmjit::HostRuntime
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Allocate memory
|
2020-12-18 07:39:54 +00:00
|
|
|
static u8* alloc(usz size, uint align, bool exec = true) noexcept;
|
2019-01-26 20:15:45 +00:00
|
|
|
|
|
|
|
// Should be called at least once after global initialization
|
|
|
|
static void initialize();
|
|
|
|
|
|
|
|
// Deallocate all memory
|
|
|
|
static void finalize() noexcept;
|
|
|
|
};
|
|
|
|
|
2018-05-14 20:06:17 +00:00
|
|
|
namespace asmjit
|
|
|
|
{
|
|
|
|
// Should only be used to build global functions
|
2020-10-17 16:28:21 +00:00
|
|
|
asmjit::Runtime& get_global_runtime();
|
2018-05-14 20:06:17 +00:00
|
|
|
|
2020-10-20 05:22:25 +00:00
|
|
|
// Emit xbegin and adjacent loop, return label at xbegin (don't use xabort please)
|
2020-10-29 02:01:45 +00:00
|
|
|
template <typename F>
|
2020-10-30 22:52:24 +00:00
|
|
|
[[nodiscard]] inline asmjit::Label build_transaction_enter(asmjit::X86Assembler& c, asmjit::Label fallback, F func)
|
2020-10-29 02:01:45 +00:00
|
|
|
{
|
|
|
|
Label fall = c.newLabel();
|
|
|
|
Label begin = c.newLabel();
|
|
|
|
c.jmp(begin);
|
|
|
|
c.bind(fall);
|
|
|
|
|
|
|
|
// Don't repeat on zero status (may indicate syscall or interrupt)
|
|
|
|
c.test(x86::eax, x86::eax);
|
|
|
|
c.jz(fallback);
|
|
|
|
|
2020-10-30 22:52:24 +00:00
|
|
|
// First invoked after failure (can fallback to proceed, or jump anywhere else)
|
|
|
|
func();
|
|
|
|
|
2020-10-29 02:01:45 +00:00
|
|
|
// Other bad statuses are ignored regardless of repeat flag (TODO)
|
|
|
|
c.align(kAlignCode, 16);
|
|
|
|
c.bind(begin);
|
|
|
|
return fall;
|
|
|
|
|
|
|
|
// xbegin should be issued manually, allows to add more check before entering transaction
|
|
|
|
}
|
2020-10-30 22:52:24 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
c.xchg(args[1], with);
|
|
|
|
args[1] = with;
|
|
|
|
#else
|
|
|
|
c.xchg(args[2], with);
|
|
|
|
args[2] = with;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
if (&to != &x86::rax && &to != &x86::rdx)
|
|
|
|
{
|
|
|
|
// Swap to save its contents
|
|
|
|
c.xchg(x86::rax, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
c.rdtsc();
|
|
|
|
c.shl(x86::rdx, 32);
|
|
|
|
|
|
|
|
if (&to == &x86::rax)
|
|
|
|
{
|
|
|
|
c.or_(x86::rax, x86::rdx);
|
|
|
|
}
|
|
|
|
else if (&to == &x86::rdx)
|
|
|
|
{
|
|
|
|
c.or_(x86::rdx, x86::rax);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Swap back, maybe there is more effective way to do it
|
|
|
|
c.xchg(x86::rax, to);
|
|
|
|
c.mov(to.r32(), to.r32());
|
|
|
|
c.or_(to.r64(), x86::rdx);
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build runtime function with asmjit::X86Assembler
|
|
|
|
template <typename FT, typename F>
|
2020-10-17 16:28:21 +00:00
|
|
|
inline FT build_function_asm(F&& builder)
|
2018-05-14 20:06:17 +00:00
|
|
|
{
|
|
|
|
using namespace asmjit;
|
|
|
|
|
|
|
|
auto& rt = get_global_runtime();
|
|
|
|
|
|
|
|
CodeHolder code;
|
|
|
|
code.init(rt.getCodeInfo());
|
|
|
|
code._globalHints = asmjit::CodeEmitter::kHintOptimizedAlign;
|
|
|
|
|
|
|
|
std::array<X86Gp, 4> args;
|
|
|
|
#ifdef _WIN32
|
|
|
|
args[0] = x86::rcx;
|
|
|
|
args[1] = x86::rdx;
|
|
|
|
args[2] = x86::r8;
|
|
|
|
args[3] = x86::r9;
|
|
|
|
#else
|
|
|
|
args[0] = x86::rdi;
|
|
|
|
args[1] = x86::rsi;
|
|
|
|
args[2] = x86::rdx;
|
|
|
|
args[3] = x86::rcx;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
X86Assembler compiler(&code);
|
|
|
|
builder(std::ref(compiler), args);
|
2020-12-09 15:04:52 +00:00
|
|
|
ensure(compiler.getLastError() == 0);
|
2018-05-14 20:06:17 +00:00
|
|
|
|
|
|
|
FT result;
|
|
|
|
|
|
|
|
if (rt.add(&result, &code))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-22 13:37:51 +00:00
|
|
|
#ifdef LLVM_AVAILABLE
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2019-01-21 18:04:32 +00:00
|
|
|
#include <string_view>
|
2016-06-22 13:37:51 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2020-12-12 12:01:29 +00:00
|
|
|
#include "util/types.hpp"
|
2016-06-22 13:37:51 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 0)
|
2019-11-29 23:11:28 +00:00
|
|
|
#else
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wall"
|
|
|
|
#pragma GCC diagnostic ignored "-Wextra"
|
|
|
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
2021-02-15 15:45:54 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wsuggest-override"
|
2021-03-05 19:05:37 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
2021-03-08 20:41:23 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
2021-03-30 15:31:46 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Weffc++"
|
2021-04-07 21:52:18 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wmissing-noreturn"
|
2021-02-15 15:45:54 +00:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic ignored "-Winconsistent-missing-override"
|
|
|
|
#endif
|
2016-06-22 13:37:51 +00:00
|
|
|
#endif
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
2019-11-29 23:11:28 +00:00
|
|
|
#else
|
|
|
|
#pragma GCC diagnostic pop
|
2016-06-22 13:37:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Temporary compiler interface
|
|
|
|
class jit_compiler final
|
|
|
|
{
|
2017-06-24 15:36:49 +00:00
|
|
|
// Local LLVM context
|
2021-04-03 16:38:02 +00:00
|
|
|
llvm::LLVMContext m_context{};
|
2017-06-24 15:36:49 +00:00
|
|
|
|
2016-06-22 13:37:51 +00:00
|
|
|
// Execution instance
|
2021-04-03 16:38:02 +00:00
|
|
|
std::unique_ptr<llvm::ExecutionEngine> m_engine{};
|
2016-06-22 13:37:51 +00:00
|
|
|
|
2017-02-26 15:56:31 +00:00
|
|
|
// Arch
|
2021-04-03 16:38:02 +00:00
|
|
|
std::string m_cpu{};
|
2017-02-26 15:56:31 +00:00
|
|
|
|
2016-06-22 13:37:51 +00:00
|
|
|
public:
|
2019-03-18 16:24:55 +00:00
|
|
|
jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, u32 flags = 0);
|
2016-06-22 13:37:51 +00:00
|
|
|
~jit_compiler();
|
|
|
|
|
2017-06-24 15:36:49 +00:00
|
|
|
// Get LLVM context
|
|
|
|
auto& get_context()
|
|
|
|
{
|
|
|
|
return m_context;
|
|
|
|
}
|
|
|
|
|
2018-05-01 10:20:36 +00:00
|
|
|
auto& get_engine() const
|
|
|
|
{
|
|
|
|
return *m_engine;
|
|
|
|
}
|
|
|
|
|
2017-07-15 09:20:40 +00:00
|
|
|
// Add module (path to obj cache dir)
|
2020-05-06 15:18:30 +00:00
|
|
|
void add(std::unique_ptr<llvm::Module> _module, const std::string& path);
|
2017-06-22 21:52:09 +00:00
|
|
|
|
2018-05-01 10:20:36 +00:00
|
|
|
// Add module (not cached)
|
2020-05-06 15:18:30 +00:00
|
|
|
void add(std::unique_ptr<llvm::Module> _module);
|
2018-05-01 10:20:36 +00:00
|
|
|
|
2017-07-15 09:20:40 +00:00
|
|
|
// Add object (path to obj file)
|
|
|
|
void add(const std::string& path);
|
|
|
|
|
2020-04-07 13:09:47 +00:00
|
|
|
// Check object file
|
|
|
|
static bool check(const std::string& path);
|
|
|
|
|
2017-06-22 21:52:09 +00:00
|
|
|
// Finalize
|
2017-06-24 15:36:49 +00:00
|
|
|
void fin();
|
2017-02-26 15:56:31 +00:00
|
|
|
|
2016-06-22 13:37:51 +00:00
|
|
|
// Get compiled function address
|
2017-06-24 15:36:49 +00:00
|
|
|
u64 get(const std::string& name);
|
|
|
|
|
2017-02-26 15:56:31 +00:00
|
|
|
// Get CPU info
|
2018-03-17 17:41:35 +00:00
|
|
|
static std::string cpu(const std::string& _cpu);
|
2016-06-22 13:37:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|