2016-06-22 15:37:51 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-11-30 00:11:28 +01:00
|
|
|
// Include asmjit with warnings ignored
|
2018-06-12 20:03:53 +02:00
|
|
|
#define ASMJIT_EMBED
|
2018-05-14 22:06:17 +02:00
|
|
|
#define ASMJIT_DEBUG
|
|
|
|
|
2019-11-30 00:11:28 +01: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"
|
2018-06-12 20:03:53 +02:00
|
|
|
#include <asmjit/asmjit.h>
|
2019-11-30 00:11:28 +01:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2018-05-17 01:38:14 +02:00
|
|
|
#include <array>
|
2018-05-14 22:06:17 +02:00
|
|
|
#include <functional>
|
|
|
|
|
2019-01-28 18:23:26 +01:00
|
|
|
enum class jit_class
|
|
|
|
{
|
|
|
|
ppu_code,
|
|
|
|
ppu_data,
|
|
|
|
spu_code,
|
|
|
|
spu_data,
|
|
|
|
};
|
|
|
|
|
2019-01-26 21:15:45 +01: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
|
|
|
|
static u8* alloc(std::size_t size, uint align, bool exec = true) noexcept;
|
|
|
|
|
|
|
|
// Should be called at least once after global initialization
|
|
|
|
static void initialize();
|
|
|
|
|
|
|
|
// Deallocate all memory
|
|
|
|
static void finalize() noexcept;
|
|
|
|
};
|
|
|
|
|
2018-05-14 22:06:17 +02:00
|
|
|
namespace asmjit
|
|
|
|
{
|
|
|
|
// Should only be used to build global functions
|
2020-10-17 18:28:21 +02:00
|
|
|
asmjit::Runtime& get_global_runtime();
|
2018-05-14 22:06:17 +02:00
|
|
|
|
2018-05-18 17:51:48 +02:00
|
|
|
// Emit xbegin and adjacent loop, return label at xbegin
|
2020-10-09 19:33:12 +02:00
|
|
|
[[nodiscard]] asmjit::Label build_transaction_enter(X86Assembler& c, Label fallback, const X86Gp& ctr, uint less_than);
|
2018-05-14 22:06:17 +02:00
|
|
|
|
2018-05-14 22:07:36 +02:00
|
|
|
// Emit xabort
|
2018-05-14 22:06:17 +02:00
|
|
|
void build_transaction_abort(X86Assembler& c, unsigned char code);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build runtime function with asmjit::X86Assembler
|
|
|
|
template <typename FT, typename F>
|
2020-10-17 18:28:21 +02:00
|
|
|
inline FT build_function_asm(F&& builder)
|
2018-05-14 22:06:17 +02: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-10-17 20:55:22 +02:00
|
|
|
ASSERT(compiler.getLastError() == 0);
|
2018-05-14 22:06:17 +02:00
|
|
|
|
|
|
|
FT result;
|
|
|
|
|
|
|
|
if (rt.add(&result, &code))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
#ifdef LLVM_AVAILABLE
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2019-01-21 19:04:32 +01:00
|
|
|
#include <string_view>
|
2016-06-22 15:37:51 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "types.h"
|
2017-06-24 17:36:49 +02:00
|
|
|
#include "mutex.h"
|
2016-06-22 15:37:51 +02:00
|
|
|
|
2017-01-28 13:32:45 +01:00
|
|
|
#include "restore_new.h"
|
2016-06-22 15:37:51 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 0)
|
2019-11-30 00:11:28 +01:00
|
|
|
#else
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wall"
|
|
|
|
#pragma GCC diagnostic ignored "-Wextra"
|
|
|
|
#pragma GCC diagnostic ignored "-Wold-style-cast"
|
2016-06-22 15:37:51 +02: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-30 00:11:28 +01:00
|
|
|
#else
|
|
|
|
#pragma GCC diagnostic pop
|
2016-06-22 15:37:51 +02:00
|
|
|
#endif
|
2017-01-28 13:32:45 +01:00
|
|
|
#include "define_new_memleakdetect.h"
|
2016-06-22 15:37:51 +02:00
|
|
|
|
|
|
|
// Temporary compiler interface
|
|
|
|
class jit_compiler final
|
|
|
|
{
|
2017-06-24 17:36:49 +02:00
|
|
|
// Local LLVM context
|
|
|
|
llvm::LLVMContext m_context;
|
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
// Execution instance
|
|
|
|
std::unique_ptr<llvm::ExecutionEngine> m_engine;
|
|
|
|
|
2017-02-26 16:56:31 +01:00
|
|
|
// Arch
|
|
|
|
std::string m_cpu;
|
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
public:
|
2019-03-18 17:24:55 +01:00
|
|
|
jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, u32 flags = 0);
|
2016-06-22 15:37:51 +02:00
|
|
|
~jit_compiler();
|
|
|
|
|
2017-06-24 17:36:49 +02:00
|
|
|
// Get LLVM context
|
|
|
|
auto& get_context()
|
|
|
|
{
|
|
|
|
return m_context;
|
|
|
|
}
|
|
|
|
|
2018-05-01 12:20:36 +02:00
|
|
|
auto& get_engine() const
|
|
|
|
{
|
|
|
|
return *m_engine;
|
|
|
|
}
|
|
|
|
|
2017-07-15 11:20:40 +02:00
|
|
|
// Add module (path to obj cache dir)
|
2020-05-06 17:18:30 +02:00
|
|
|
void add(std::unique_ptr<llvm::Module> _module, const std::string& path);
|
2017-06-22 23:52:09 +02:00
|
|
|
|
2018-05-01 12:20:36 +02:00
|
|
|
// Add module (not cached)
|
2020-05-06 17:18:30 +02:00
|
|
|
void add(std::unique_ptr<llvm::Module> _module);
|
2018-05-01 12:20:36 +02:00
|
|
|
|
2017-07-15 11:20:40 +02:00
|
|
|
// Add object (path to obj file)
|
|
|
|
void add(const std::string& path);
|
|
|
|
|
2020-04-07 15:09:47 +02:00
|
|
|
// Check object file
|
|
|
|
static bool check(const std::string& path);
|
|
|
|
|
2017-06-22 23:52:09 +02:00
|
|
|
// Finalize
|
2017-06-24 17:36:49 +02:00
|
|
|
void fin();
|
2017-02-26 16:56:31 +01:00
|
|
|
|
2016-06-22 15:37:51 +02:00
|
|
|
// Get compiled function address
|
2017-06-24 17:36:49 +02:00
|
|
|
u64 get(const std::string& name);
|
|
|
|
|
2017-02-26 16:56:31 +01:00
|
|
|
// Get CPU info
|
2018-03-17 18:41:35 +01:00
|
|
|
static std::string cpu(const std::string& _cpu);
|
2016-06-22 15:37:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|