2
0
mirror of https://github.com/rehlds/metamod-r.git synced 2025-01-13 15:18:19 +03:00

Align meta globals and local backup for faster access

This commit is contained in:
asmodai 2017-02-14 01:51:47 +03:00
parent efb98cc155
commit 6ea3130c66
3 changed files with 32 additions and 21 deletions

View File

@ -66,22 +66,27 @@ void CForwardCallbackJIT::naked_main()
enum // stack map
{
orig_ret = 0,
over_ret = sizeof(int)
/* META GLOBALS BACKUP */
/* STRING BUFFER */
over_ret = sizeof(int),
orig_ret = 0
};
auto globals = ebx;
auto mg_backup = m_jitdata->has_ret ? sizeof(int) * 2 /* orig + over */ : 0;
auto framesize = align(mg_backup + sizeof(meta_globals_t) + /* for align */m_jitdata->args_count * sizeof(int), xmmreg_size) - m_jitdata->args_count * sizeof(int);
auto locals_size = m_jitdata->has_ret ? sizeof(int) * 2 /* orig + over */ : 0;
auto framesize = align(locals_size + sizeof(meta_globals_t) + /* for align */m_jitdata->args_count * sizeof(int), xmmreg_size) - m_jitdata->args_count * sizeof(int);
if (m_jitdata->has_varargs) {
size_t buf_offset = framesize;
size_t strbuf_offset = locals_size;
sub(esp, framesize += align(MAX_STRBUF_LEN, xmmreg_size));
// format varargs
lea(edx, dword_ptr[ebp + first_arg_offset + m_jitdata->args_count * sizeof(int)]); // varargs ptr
lea(eax, dword_ptr[esp + buf_offset]); // buf ptr
if (strbuf_offset)
lea(eax, dword_ptr[esp + strbuf_offset]); // buf ptr
else
mov(eax, esp);
mov(ecx, size_t(vsnprintf));
push(edx);
@ -94,12 +99,14 @@ void CForwardCallbackJIT::naked_main()
else
sub(esp, framesize);
size_t mg_backup = framesize - xmmreg_size - sizeof(int);
// setup globals ptr and backup old data
mov(globals, size_t(&g_metaGlobals));
movups(xmm0, xmmword_ptr[globals]);
movaps(xmm0, xmmword_ptr[globals]);
mov(eax, dword_ptr[globals + xmmreg_size]);
movups(xmmword_ptr[esp + mg_backup], xmm0);
mov(dword_ptr[esp + mg_backup + xmmreg_size], eax);
movaps(xmmword_ptr[esp + mg_backup + sizeof(int)], xmm0);
mov(dword_ptr[esp + mg_backup], eax);
// call metamod's pre hook if present
if (m_jitdata->mm_hook && m_jitdata->mm_hook_time == P_PRE) {
@ -255,9 +262,9 @@ void CForwardCallbackJIT::naked_main()
call_func(ecx);
}
movups(xmm0, xmmword_ptr[esp + mg_backup]);
mov(eax, dword_ptr[esp + mg_backup + xmmreg_size]);
movups(xmmword_ptr[globals], xmm0);
movaps(xmm0, xmmword_ptr[esp + mg_backup + sizeof(int)]);
mov(eax, dword_ptr[esp + mg_backup]);
movaps(xmmword_ptr[globals], xmm0);
mov(dword_ptr[globals + xmmreg_size], eax);
if (m_jitdata->has_ret) {
@ -276,12 +283,16 @@ void CForwardCallbackJIT::naked_main()
void CForwardCallbackJIT::call_func(jitasm::Frontend::Reg32 addr)
{
const size_t fixed_args_count = m_jitdata->args_count - (m_jitdata->has_varargs ? 1u /* excluding format string */ : 0u);
const size_t strbuf_offset = align((m_jitdata->has_ret ? sizeof(int) * 2 : 0) + sizeof(meta_globals_t) + /* for align */m_jitdata->args_count * sizeof(int), xmmreg_size) - m_jitdata->args_count * sizeof(int);
const size_t strbuf_offset = m_jitdata->has_ret ? sizeof(int) * 2 /* orig + over */ : 0;
// push formatted buf instead of format string
if (m_jitdata->has_varargs) {
lea(eax, dword_ptr[esp + strbuf_offset]);
push(eax);
if (strbuf_offset) {
lea(eax, dword_ptr[esp + strbuf_offset]);
push(eax);
}
else
push(esp);
}
// push normal args

View File

@ -321,7 +321,7 @@ namespace detail
bool operator==(const Opd& rhs) const
{
if ((opdtype_ & O_TYPE_TYPE_MASK) != (rhs.opdtype_ & O_TYPE_TYPE_MASK) || rhs.opdsize_ != opdsize_) {return false;}
if ((opdtype_ & O_TYPE_TYPE_MASK) != (rhs.opdtype_ & O_TYPE_TYPE_MASK) || opdsize_ != opdsize_) {return false;}
if (IsReg()) {return reg_ == rhs.reg_ && reg_assignable_ == rhs.reg_assignable_;}
if (IsMem()) {return base_ == rhs.base_ && index_ == rhs.index_ && scale_ == rhs.scale_ && disp_ == rhs.disp_ && addrsize_ == rhs.addrsize_;}
if (IsImm()) {return imm_ == rhs.imm_;}
@ -1924,7 +1924,6 @@ struct Frontend
#ifndef JITASM64
void call(const Reg16& dst) {AppendInstr(I_CALL, 0xFF, E_OPERAND_SIZE_PREFIX, Imm8(2), R(dst));}
void call(const Reg32& dst) {AppendInstr(I_CALL, 0xFF, 0, Imm8(2), R(dst));}
void call(const Mem32& dst) {AppendInstr(I_CALL, 0xFF, 0, Imm8(2), R(dst));} // Imm8(2) = register/opcode
#else
void call(const Reg64& dst) {AppendInstr(I_CALL, 0xFF, 0, Imm8(2), R(dst));}
#endif
@ -2267,7 +2266,6 @@ struct Frontend
#ifdef JITASM64
void iretq() {AppendInstr(I_IRETQ, 0xCF, E_REXW_PREFIX);}
#endif
void jmp(const Mem32& dst) {AppendInstr(I_JMP, 0xFF, 0, Imm8(4), R(dst));}
void jmp(const std::string& label_name) {AppendJmp(GetLabelID(label_name));}
void ja(const std::string& label_name) {AppendJcc(JCC_A, GetLabelID(label_name));}
void jae(const std::string& label_name) {AppendJcc(JCC_AE, GetLabelID(label_name));}
@ -3996,7 +3994,7 @@ struct Frontend
void vblendvps(const YmmReg& dst, const YmmReg& src1, const Mem256& src2, const YmmReg& mask) {AppendInstr(I_BLENDVPS, 0x4A, E_VEX_256 | E_VEX_66_0F3A, W(dst), R(src2), R(src1), R(mask));}
void vbroadcastss(const XmmReg& dst, const Mem32& src) {AppendInstr(I_VBROADCASTSS, 0x18, E_VEX_128_66_0F38_W0, W(dst), R(src));}
void vbroadcastss(const YmmReg& dst, const Mem32& src) {AppendInstr(I_VBROADCASTSS, 0x18, E_VEX_256_66_0F38_W0, W(dst), R(src));}
void vbroadcastsd(const YmmReg& dst, const Mem64& src) {AppendInstr(I_VBROADCASTSD, 0x19, E_VEX_256_66_0F38_W0, W(dst), R(src));}
void vbroadcastsd(const YmmReg& dst, const Mem64 src) {AppendInstr(I_VBROADCASTSD, 0x19, E_VEX_256_66_0F38_W0, W(dst), R(src));}
void vbroadcastf128(const YmmReg& dst, const Mem128& src) {AppendInstr(I_VBROADCASTF128, 0x1A, E_VEX_256_66_0F38_W0, W(dst), R(src));}
void vcmppd(const XmmReg& dst, const XmmReg& src1, const XmmReg& src2, const Imm8& imm) {AppendInstr(I_CMPPD, 0xC2, E_VEX_128_66_0F_WIG, W(dst), R(src2), R(src1), imm);}
void vcmppd(const XmmReg& dst, const XmmReg& src1, const Mem128& src2, const Imm8& imm) {AppendInstr(I_CMPPD, 0xC2, E_VEX_128_66_0F_WIG, W(dst), R(src2), R(src1), imm);}
@ -4038,8 +4036,8 @@ struct Frontend
void vcvtps2pd(const XmmReg& dst, const Mem64& src) {AppendInstr(I_CVTPS2PD, 0x5A, E_VEX_128_0F_WIG, W(dst), R(src));}
void vcvtps2pd(const YmmReg& dst, const XmmReg& src) {AppendInstr(I_CVTPS2PD, 0x5A, E_VEX_256_0F_WIG, W(dst), R(src));}
void vcvtps2pd(const YmmReg& dst, const Mem128& src) {AppendInstr(I_CVTPS2PD, 0x5A, E_VEX_256_0F_WIG, W(dst), R(src));}
void vcvtsd2si(const Reg32& dst, const XmmReg& src) {AppendInstr(I_CVTSD2SI, 0x2D, E_VEX_128 | E_VEX_F2_0F | E_VEX_W0, W(dst), R(src));}
void vcvtsd2si(const Reg32& dst, const Mem64& src) {AppendInstr(I_CVTSD2SI, 0x2D, E_VEX_128 | E_VEX_F2_0F | E_VEX_W0, W(dst), R(src));}
void vcvtsd2si(const Reg32 dst, const XmmReg& src) {AppendInstr(I_CVTSD2SI, 0x2D, E_VEX_128 | E_VEX_F2_0F | E_VEX_W0, W(dst), R(src));}
void vcvtsd2si(const Reg32 dst, const Mem64& src) {AppendInstr(I_CVTSD2SI, 0x2D, E_VEX_128 | E_VEX_F2_0F | E_VEX_W0, W(dst), R(src));}
#ifdef JITASM64
void vcvtsd2si(const Reg64 dst, const XmmReg& src) {AppendInstr(I_CVTSD2SI, 0x2D, E_VEX_128 | E_VEX_F2_0F | E_VEX_W1, W(dst), R(src));}
void vcvtsd2si(const Reg64 dst, const Mem64& src) {AppendInstr(I_CVTSD2SI, 0x2D, E_VEX_128 | E_VEX_F2_0F | E_VEX_W1, W(dst), R(src));}
@ -6245,6 +6243,7 @@ namespace compiler
InstrID instr_id = f.instrs_[instr_idx].GetID();
if (Frontend::IsJump(instr_id) || instr_id == I_RET || instr_id == I_IRET) {
// Jump instruction always terminate basic block
BlockList::iterator next_block;
if (instr_idx + 1 < cur_block->instr_end) {
// Split basic block
split(blocks_.begin() + block_idx, instr_idx + 1);

View File

@ -16,6 +16,7 @@ option_t g_global_options[] =
gamedll_t g_GameDLL;
ALIGN16
meta_globals_t g_metaGlobals;
meta_enginefuncs_t g_plugin_engfuncs;