Skip to content

Commit

Permalink
FILD
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo720 committed Aug 8, 2024
1 parent b288e1b commit 3a68109
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib86cpu/core/emitter/emitter_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,7 @@ inline constexpr auto all_callable_funcs = std::make_tuple(
fpu_stack_check<true, fpu_instr_t::float_>,
fpu_stack_check<false, fpu_instr_t::float_>,
fpu_stack_check<true, fpu_instr_t::bcd>,
fpu_stack_check<false, fpu_instr_t::bcd>
fpu_stack_check<false, fpu_instr_t::bcd>,
fpu_stack_check<true>,
fpu_stack_check<false>
);
39 changes: 39 additions & 0 deletions lib86cpu/core/emitter/x64/jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ static_assert((LOCAL_VARS_off(0) & 15) == 0); // must be 16 byte aligned so that
#define FNSTSW(dst) m_a.fnstsw(dst)
#define FNCLEX() m_a.fnclex()
#define FLD(src) m_a.fld(src)
#define FILD(src) m_a.fild(src)
#define FSTP(dst) m_a.fstp(dst)
#define FLD1(dst) m_a.fld1(dst)
#define FLDL2T(dst) m_a.fldl2t(dst)
Expand Down Expand Up @@ -5123,6 +5124,44 @@ lc86_jit::enter(decoded_instr *instr)
ST_REG_val(rax_host_reg, CPU_CTX_EBP, m_cpu->size_mode);
}

void
lc86_jit::fild(decoded_instr *instr)
{
if (m_cpu->cpu_ctx.hflags & (HFLG_CR0_EM | HFLG_CR0_TS)) {
RAISEin0_t(EXP_NM);
}
else {
get_rm<OPNUM_SINGLE>(instr,
[](const op_info rm)
{
assert(0);
},
[this, instr](const op_info rm)
{
uint8_t size_mode = instr->i.opcode == 0xDB ? SIZE32 : (instr->i.raw.modrm.reg == 5 ? SIZE64 : SIZE16);
fpu_instr_t fpu_instr = instr->i.opcode == 0xDB ? fpu_instr_t::integer32 : (instr->i.raw.modrm.reg == 5 ? fpu_instr_t::integer64 : fpu_instr_t::integer16);
LD_MEMs(size_mode);
MOV(MEMD32(RSP, LOCAL_VARS_off(0)), EAX);
MOV(R9D, fpu_instr);
LEA(R8, MEMD64(RSP, LOCAL_VARS_off(0)));
LEA(RDX, MEMD64(RSP, LOCAL_VARS_off(1)));
CALL_F((&fpu_stack_check<true>));
MOV(EBX, EAX);
MOV(EDX, EAX);
MOV(EAX, sizeof(uint80_t));
MUL(DX);
EMMS();
FILD(MEMD(RSP, LOCAL_VARS_off(0), size_mode));
FSTP(MEMSD80(RCX, RAX, 0, CPU_CTX_R0));
MOV(AX, MEMD16(RSP, LOCAL_VARS_off(1)));
ST_R16(CPU_CTX_FSTATUS, AX);
ST_R16(FPU_DATA_FTOP, BX);
MOV(EDX, EBX);
CALL_F(&fpu_update_tag<true>);
});
}
}

void
lc86_jit::fld(decoded_instr *instr)
{
Expand Down
1 change: 1 addition & 0 deletions lib86cpu/core/emitter/x64/jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class lc86_jit : public Target {
void dec(decoded_instr *instr);
void div(decoded_instr *instr);
void enter(decoded_instr *instr);
void fild(decoded_instr *instr);
void fld(decoded_instr *instr);
void fld1(decoded_instr *instr);
void fldcw(decoded_instr *instr);
Expand Down
12 changes: 10 additions & 2 deletions lib86cpu/core/fpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ void fpu_update_tag(cpu_ctx_t *cpu_ctx, uint32_t idx)
}
}

template<bool is_push, fpu_instr_t instr_type>
uint32_t fpu_stack_check(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val)
template<bool is_push>
uint32_t fpu_stack_check(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val, fpu_instr_t instr_type)
{
// this function returns the fpu stack pointer to the value modified by the push/pop, and the flags of the status word following a stack fault.
// It also writes an appropriate indefinite value when it detects a masked stack exception
Expand Down Expand Up @@ -113,6 +113,12 @@ uint32_t fpu_stack_check(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val)
return ftop;
}

template<bool is_push, fpu_instr_t instr_type>
uint32_t fpu_stack_check(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val)
{
return fpu_stack_check<is_push>(cpu_ctx, sw, inv_val, instr_type);
}

template JIT_API uint32_t fpu_stack_check<true, fpu_instr_t::integer8>(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val);
template JIT_API uint32_t fpu_stack_check<false, fpu_instr_t::integer8>(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val);
template JIT_API uint32_t fpu_stack_check<true, fpu_instr_t::integer16>(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val);
Expand All @@ -125,5 +131,7 @@ template JIT_API uint32_t fpu_stack_check<true, fpu_instr_t::float_>(cpu_ctx_t *
template JIT_API uint32_t fpu_stack_check<false, fpu_instr_t::float_>(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val);
template JIT_API uint32_t fpu_stack_check<true, fpu_instr_t::bcd>(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val);
template JIT_API uint32_t fpu_stack_check<false, fpu_instr_t::bcd>(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val);
template JIT_API uint32_t fpu_stack_check<true>(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val, fpu_instr_t instr_type);
template JIT_API uint32_t fpu_stack_check<false>(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val, fpu_instr_t instr_type);
template JIT_API void fpu_update_tag<true>(cpu_ctx_t *cpu_ctx, uint32_t idx);
template JIT_API void fpu_update_tag<false>(cpu_ctx_t *cpu_ctx, uint32_t idx);
4 changes: 3 additions & 1 deletion lib86cpu/core/fpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "lib86cpu_priv.h"


enum class fpu_instr_t : int {
enum class fpu_instr_t : uint32_t {
integer8 = 0,
integer16,
integer32,
Expand All @@ -21,3 +21,5 @@ template<bool is_push>
JIT_API void fpu_update_tag(cpu_ctx_t *cpu_ctx, uint32_t idx);
template<bool is_push, fpu_instr_t instr_type>
JIT_API uint32_t fpu_stack_check(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val);
template<bool is_push>
JIT_API uint32_t fpu_stack_check(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val, fpu_instr_t instr_type);
4 changes: 4 additions & 0 deletions lib86cpu/core/translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,10 @@ cpu_translate(cpu_t *cpu)
cpu->jit->enter(&instr);
break;

case ZYDIS_MNEMONIC_FILD:
cpu->jit->fild(&instr);
break;

case ZYDIS_MNEMONIC_FLD:
cpu->jit->fld(&instr);
break;
Expand Down

0 comments on commit 3a68109

Please sign in to comment.