1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-24 19:52:37 +01:00
rpcs3/Utilities/JIT.h

147 lines
2.7 KiB
C
Raw Normal View History

#pragma once
2018-06-12 20:03:53 +02:00
#define ASMJIT_EMBED
#define ASMJIT_DEBUG
2018-06-12 20:03:53 +02:00
#include <asmjit/asmjit.h>
#include <array>
#include <functional>
namespace asmjit
{
// Should only be used to build global functions
JitRuntime& get_global_runtime();
// Emit xbegin and adjacent loop, return label at xbegin
Label build_transaction_enter(X86Assembler& c, Label fallback);
// Emit xabort
void build_transaction_abort(X86Assembler& c, unsigned char code);
}
// Build runtime function with asmjit::X86Assembler
template <typename FT, typename F>
FT build_function_asm(F&& builder)
{
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);
FT result;
if (rt.add(&result, &code))
{
return nullptr;
}
return result;
}
#ifdef LLVM_AVAILABLE
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
#include "types.h"
2017-06-24 17:36:49 +02:00
#include "mutex.h"
2017-01-28 13:32:45 +01:00
#include "restore_new.h"
#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#ifdef _MSC_VER
#pragma warning(pop)
#endif
2017-01-28 13:32:45 +01:00
#include "define_new_memleakdetect.h"
// Temporary compiler interface
class jit_compiler final
{
2017-06-24 17:36:49 +02:00
// Local LLVM context
llvm::LLVMContext m_context;
// JIT Event Listener
std::unique_ptr<struct EventListener> m_jit_el;
// Execution instance
std::unique_ptr<llvm::ExecutionEngine> m_engine;
2017-06-24 17:36:49 +02:00
// Link table
std::unordered_map<std::string, u64> m_link;
2017-02-26 16:56:31 +01:00
// Arch
std::string m_cpu;
public:
2018-05-01 12:20:36 +02:00
jit_compiler(const std::unordered_map<std::string, u64>& _link, const std::string& _cpu, bool large = false);
~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;
}
// Test SSSE3 feature
bool has_ssse3() const;
2017-07-15 11:20:40 +02:00
// Add module (path to obj cache dir)
void add(std::unique_ptr<llvm::Module> module, const std::string& path);
2018-05-01 12:20:36 +02:00
// Add module (not cached)
void add(std::unique_ptr<llvm::Module> module);
2017-07-15 11:20:40 +02:00
// Add object (path to obj file)
void add(const std::string& path);
// Finalize
2017-06-24 17:36:49 +02:00
void fin();
2017-02-26 16:56:31 +01:00
// Get compiled function address
2017-06-24 17:36:49 +02:00
u64 get(const std::string& name);
// Allocate writable executable memory (alignment is assumed 16)
static u8* alloc(u32 size);
2017-02-26 16:56:31 +01:00
// Get CPU info
static std::string cpu(const std::string& _cpu);
2017-06-29 16:25:39 +02:00
// Check JIT purpose
bool is_primary() const
{
return !m_link.empty();
}
};
#endif