Skip to content

Commit

Permalink
Fixed some bugs in FSTP and gen_fpu_exp_post_check
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo720 committed Feb 3, 2024
1 parent c982044 commit f713340
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
20 changes: 11 additions & 9 deletions lib86cpu/core/emitter/x64/jit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ static_assert((LOCAL_VARS_off(0) & 15) == 0); // must be 16 byte aligned so that
#define TEST(dst, src) m_a.test(dst, src)
#define ADD(dst, src) m_a.add(dst, src)
#define SUB(dst, src) m_a.sub(dst, src)
#define INC(dst) m_a.inc(dst)
#define DEC(dst) m_a.dec(dst)
#define CMP(dst, src) m_a.cmp(dst, src)
#define MUL(op) m_a.mul(op)
Expand Down Expand Up @@ -2378,9 +2379,9 @@ lc86_jit::gen_fpu_exp_post_check()
FNSTSW(AX);
TEST(AX, FPU_EXP_ALL);
BR_EQ(no_exp);
LD_R16(DX, CPU_CTX_FCTRL);
AND(DX, FPU_EXP_ALL);
CMP(DX, FPU_EXP_ALL);
LD_R16(R9W, CPU_CTX_FCTRL);
AND(R9W, FPU_EXP_ALL);
CMP(R9W, FPU_EXP_ALL);
BR_EQ(no_exp);
static const char *abort_msg = "Unmasked fpu exceptions are not supported";
MOV(RCX, abort_msg);
Expand Down Expand Up @@ -5389,11 +5390,12 @@ lc86_jit::fstp(decoded_instr *instr)
MOV(MEMD64(RSP, LOCAL_VARS_off(0)), 0);
gen_fpu_stack_fault_check<false, fpu_instr_t::float_>();
gen_set_host_fpu_ctx();
MOV(R9D, EAX);
DEC(R9D);
MOV(EDX, EAX);
MOV(MEMD32(RSP, LOCAL_VARS_off(4)), EAX);
INC(EBX);
AND(EBX, 7);
TEST(MEMD64(RSP, LOCAL_VARS_off(0)), 0);
BR_NE(stack_fault);
MOV(EDX, R9D);
MOV(EAX, sizeof(uint80_t));
MUL(DX);
FLD(MEMSD80(RCX, RAX, 0, CPU_CTX_R0));
Expand Down Expand Up @@ -5427,8 +5429,8 @@ lc86_jit::fstp(decoded_instr *instr)
case 0xDD:
FSTP(MEMD64(RSP, LOCAL_VARS_off(0)));
gen_fpu_exp_post_check();
MOV(R8D, MEMD64(RSP, LOCAL_VARS_off(0)));
ST_MEMs(R8D, SIZE64);
MOV(R8, MEMD64(RSP, LOCAL_VARS_off(0)));
ST_MEMs(R8, SIZE64);
break;

case 0xDB:
Expand All @@ -5447,7 +5449,7 @@ lc86_jit::fstp(decoded_instr *instr)
});

RESTORE_FPU_CTX();
MOV(EDX, R9D);
MOV(EDX, MEMD32(RSP, LOCAL_VARS_off(4)));
CALL_F(&fpu_update_tag<false>);
}
}
Expand Down
6 changes: 2 additions & 4 deletions lib86cpu/core/fpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,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)
{
// this function returns the fpu stack top after 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
// 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
// NOTE: we only support masked stack exceptions for now

uint32_t ftop, fstatus = cpu_ctx->regs.fstatus;
Expand All @@ -64,8 +64,6 @@ uint32_t fpu_stack_check(cpu_ctx_t *cpu_ctx, uint32_t *sw, uint80_t *inv_val)
// detect stack underflow
ftop = cpu_ctx->fpu_data.ftop;
no_stack_fault = cpu_ctx->regs.ftags[ftop] != FPU_TAG_EMPTY;
ftop += 1;
ftop &= 7;
}

if (!no_stack_fault) {
Expand Down

0 comments on commit f713340

Please sign in to comment.