Skip to content

Commit

Permalink
[INTERPRETER] Some cleanup on base logic/math/shift operations
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Nov 15, 2024
1 parent 27745ce commit a39c8aa
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 302 deletions.
6 changes: 0 additions & 6 deletions src/dynarec/dynarec.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ void DynaCall(x64emu_t* emu, uintptr_t addr)
multiuint_t old_op1 = emu->op1;
multiuint_t old_op2 = emu->op2;
multiuint_t old_res = emu->res;
multiuint_t old_op1_sav= emu->op1_sav;
multiuint_t old_res_sav= emu->res_sav;
deferred_flags_t old_df_sav= emu->df_sav;
// uc_link
void* old_uc_link = emu->uc_link;
emu->uc_link = NULL;
Expand All @@ -134,9 +131,6 @@ void DynaCall(x64emu_t* emu, uintptr_t addr)
emu->op1 = old_op1;
emu->op2 = old_op2;
emu->res = old_res;
emu->op1_sav = old_op1_sav;
emu->res_sav = old_res_sav;
emu->df_sav = old_df_sav;
// and the old registers
emu->eflags = old_eflags;
R_RBX = old_rbx;
Expand Down
12 changes: 0 additions & 12 deletions src/emu/x64emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,9 @@ void CloneEmu(x64emu_t *newemu, const x64emu_t* emu)
memcpy(newemu->xmm, emu->xmm, sizeof(emu->xmm));
memcpy(newemu->ymm, emu->ymm, sizeof(emu->ymm));
newemu->df = emu->df;
newemu->df_sav = emu->df_sav;
newemu->op1 = emu->op1;
newemu->op2 = emu->op2;
newemu->res = emu->res;
newemu->op1_sav = emu->op1_sav;
newemu->res_sav = emu->res_sav;
newemu->mxcsr = emu->mxcsr;
newemu->quit = emu->quit;
newemu->error = emu->error;
Expand Down Expand Up @@ -286,12 +283,9 @@ void CopyEmu(x64emu_t *newemu, const x64emu_t* emu)
newemu->top = emu->top;
newemu->fpu_stack = emu->fpu_stack;
newemu->df = emu->df;
newemu->df_sav = emu->df_sav;
newemu->op1 = emu->op1;
newemu->op2 = emu->op2;
newemu->res = emu->res;
newemu->op1_sav = emu->op1_sav;
newemu->res_sav = emu->res_sav;
newemu->mxcsr = emu->mxcsr;
newemu->quit = emu->quit;
newemu->error = emu->error;
Expand Down Expand Up @@ -609,9 +603,6 @@ void EmuCall(x64emu_t* emu, uintptr_t addr)
multiuint_t old_op1 = emu->op1;
multiuint_t old_op2 = emu->op2;
multiuint_t old_res = emu->res;
multiuint_t old_op1_sav= emu->op1_sav;
multiuint_t old_res_sav= emu->res_sav;
deferred_flags_t old_df_sav= emu->df_sav;
// uc_link
void* old_uc_link = emu->uc_link;
emu->uc_link = NULL;
Expand Down Expand Up @@ -640,9 +631,6 @@ void EmuCall(x64emu_t* emu, uintptr_t addr)
emu->op1 = old_op1;
emu->op2 = old_op2;
emu->res = old_res;
emu->op1_sav = old_op1_sav;
emu->res_sav = old_res_sav;
emu->df_sav = old_df_sav;
// and the old registers
emu->eflags = old_eflags;
R_RBX = old_rbx;
Expand Down
3 changes: 0 additions & 3 deletions src/emu/x64emu_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ typedef struct x64emu_s {
multiuint_t op1;
multiuint_t op2;
multiuint_t res;
multiuint_t op1_sav; // for dec/inc deferred flags, to be able to compute CF
multiuint_t res_sav;
deferred_flags_t df_sav;
uint32_t *x64emu_parity_tab; // helper
// segments
uint16_t segs[6]; // only 32bits value?
Expand Down
131 changes: 83 additions & 48 deletions src/emu/x64primop.c
Original file line number Diff line number Diff line change
Expand Up @@ -1189,9 +1189,8 @@ uint16_t sbb16(x64emu_t *emu, uint16_t d, uint16_t s)

if (ACCESS_FLAG(F_CF))
res = d - s - 1;
else {
else
res = d - s;
}
CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
CONDITIONAL_SET_FLAG((res & 0xffff) == 0, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
Expand All @@ -1212,9 +1211,8 @@ uint32_t sbb32(x64emu_t *emu, uint32_t d, uint32_t s)

if (ACCESS_FLAG(F_CF))
res = d - s - 1;
else {
else
res = d - s;
}
CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
CONDITIONAL_SET_FLAG(!res, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
Expand All @@ -1235,9 +1233,8 @@ uint64_t sbb64(x64emu_t *emu, uint64_t d, uint64_t s)

if (ACCESS_FLAG(F_CF))
res = d - s - 1;
else {
else
res = d - s;
}
CONDITIONAL_SET_FLAG(res & 0x8000000000000000LL, F_SF);
CONDITIONAL_SET_FLAG(!res, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
Expand All @@ -1256,32 +1253,32 @@ Implements the TEST instruction and side effects.
****************************************************************************/
void test8(x64emu_t *emu, uint8_t d, uint8_t s)
{
uint32_t res; /* all operands in native machine order */
uint8_t res; /* all operands in native machine order */
RESET_FLAGS(emu);

res = d & s;

CLEAR_FLAG(F_OF);
CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
CONDITIONAL_SET_FLAG(res == 0, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
CLEAR_FLAG(F_AF); /* AF == dont care */
CLEAR_FLAG(F_CF);
CONDITIONAL_SET_FLAG(res & 0x80, F_SF);
CONDITIONAL_SET_FLAG(!res, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
}

void test16(x64emu_t *emu, uint16_t d, uint16_t s)
{
uint32_t res; /* all operands in native machine order */
uint16_t res; /* all operands in native machine order */
RESET_FLAGS(emu);

res = d & s;

CLEAR_FLAG(F_OF);
CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
CONDITIONAL_SET_FLAG(res == 0, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
CLEAR_FLAG(F_AF); /* AF == dont care */
CLEAR_FLAG(F_CF);
CONDITIONAL_SET_FLAG(res & 0x8000, F_SF);
CONDITIONAL_SET_FLAG(!res, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
}

void test32(x64emu_t *emu, uint32_t d, uint32_t s)
Expand All @@ -1292,11 +1289,11 @@ void test32(x64emu_t *emu, uint32_t d, uint32_t s)
res = d & s;

CLEAR_FLAG(F_OF);
CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
CONDITIONAL_SET_FLAG(res == 0, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
CLEAR_FLAG(F_AF); /* AF == dont care */
CLEAR_FLAG(F_CF);
CONDITIONAL_SET_FLAG(res & 0x80000000, F_SF);
CONDITIONAL_SET_FLAG(!res, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
}

void test64(x64emu_t *emu, uint64_t d, uint64_t s)
Expand All @@ -1307,21 +1304,30 @@ void test64(x64emu_t *emu, uint64_t d, uint64_t s)
res = d & s;

CLEAR_FLAG(F_OF);
CONDITIONAL_SET_FLAG(res & 0x8000000000000000LL, F_SF);
CONDITIONAL_SET_FLAG(res == 0, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
CLEAR_FLAG(F_AF); /* AF == dont care */
CLEAR_FLAG(F_CF);
CONDITIONAL_SET_FLAG(res & 0x8000000000000000LL, F_SF);
CONDITIONAL_SET_FLAG(!res, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(res & 0xff), F_PF);
}

/****************************************************************************
REMARKS:
Implements the IDIV instruction and side effects.
****************************************************************************/
extern int box64_dynarec_test;
void idiv8(x64emu_t *emu, uint8_t s)
{
int32_t dvd, quot, mod;
RESET_FLAGS(emu);
if(box64_dynarec_test) {
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_PF);
CLEAR_FLAG(F_ZF);
CLEAR_FLAG(F_SF);
CLEAR_FLAG(F_OF);
}

dvd = (int16_t)R_AX;
if (s == 0) {
Expand All @@ -1342,6 +1348,15 @@ void idiv8(x64emu_t *emu, uint8_t s)
void idiv16(x64emu_t *emu, uint16_t s)
{
int32_t dvd, quot, mod;
RESET_FLAGS(emu);
if(box64_dynarec_test) {
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_PF);
CLEAR_FLAG(F_ZF);
CLEAR_FLAG(F_SF);
CLEAR_FLAG(F_OF);
}

dvd = (((int32_t)R_DX) << 16) | R_AX;
if (s == 0) {
Expand All @@ -1355,10 +1370,6 @@ void idiv16(x64emu_t *emu, uint16_t s)
INTR_RAISE_DIV0(emu);
return;
}
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_SF);
CONDITIONAL_SET_FLAG(quot == 0, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);

R_AX = (uint16_t)quot;
R_DX = (uint16_t)mod;
Expand All @@ -1368,6 +1379,14 @@ void idiv32(x64emu_t *emu, uint32_t s)
{
int64_t dvd, quot, mod;
RESET_FLAGS(emu);
if(box64_dynarec_test) {
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_PF);
CLEAR_FLAG(F_ZF);
CLEAR_FLAG(F_SF);
CLEAR_FLAG(F_OF);
}

dvd = (((int64_t)R_EDX) << 32) | R_EAX;
if (s == 0) {
Expand All @@ -1381,11 +1400,6 @@ void idiv32(x64emu_t *emu, uint32_t s)
INTR_RAISE_DIV0(emu);
return;
}
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_SF);
SET_FLAG(F_ZF);
CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);

R_RAX = (uint32_t)quot;
R_RDX = (uint32_t)mod;
Expand All @@ -1395,6 +1409,14 @@ void idiv64(x64emu_t *emu, uint64_t s)
{
__int128 dvd, quot, mod;
RESET_FLAGS(emu);
if(box64_dynarec_test) {
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_PF);
CLEAR_FLAG(F_ZF);
CLEAR_FLAG(F_SF);
CLEAR_FLAG(F_OF);
}

dvd = (((__int128)R_RDX) << 64) | R_RAX;
if (s == 0) {
Expand All @@ -1407,11 +1429,6 @@ void idiv64(x64emu_t *emu, uint64_t s)
INTR_RAISE_DIV0(emu);
return;
}
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_SF);
SET_FLAG(F_ZF);
CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);

R_RAX = (uint64_t)quot;
R_RDX = (uint64_t)mod;
Expand All @@ -1425,6 +1442,14 @@ void div8(x64emu_t *emu, uint8_t s)
{
uint32_t dvd, div, mod;
RESET_FLAGS(emu);
if(box64_dynarec_test) {
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_PF);
CLEAR_FLAG(F_ZF);
CLEAR_FLAG(F_SF);
CLEAR_FLAG(F_OF);
}

dvd = R_AX;
if (s == 0) {
Expand All @@ -1445,6 +1470,14 @@ void div16(x64emu_t *emu, uint16_t s)
{
uint32_t dvd, div, mod;
RESET_FLAGS(emu);
if(box64_dynarec_test) {
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_PF);
CLEAR_FLAG(F_ZF);
CLEAR_FLAG(F_SF);
CLEAR_FLAG(F_OF);
}

dvd = (((uint32_t)R_DX) << 16) | R_AX;
if (s == 0) {
Expand All @@ -1457,10 +1490,6 @@ void div16(x64emu_t *emu, uint16_t s)
INTR_RAISE_DIV0(emu);
return;
}
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_SF);
CONDITIONAL_SET_FLAG(div == 0, F_ZF);
CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);

R_AX = (uint16_t)div;
R_DX = (uint16_t)mod;
Expand All @@ -1470,6 +1499,14 @@ void div32(x64emu_t *emu, uint32_t s)
{
uint64_t dvd, div, mod;
RESET_FLAGS(emu);
if(box64_dynarec_test) {
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_PF);
CLEAR_FLAG(F_ZF);
CLEAR_FLAG(F_SF);
CLEAR_FLAG(F_OF);
}

dvd = (((uint64_t)R_EDX) << 32) | R_EAX;
if (s == 0) {
Expand All @@ -1482,11 +1519,6 @@ void div32(x64emu_t *emu, uint32_t s)
INTR_RAISE_DIV0(emu);
return;
}
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_SF);
SET_FLAG(F_ZF);
CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);

R_RAX = (uint32_t)div;
R_RDX = (uint32_t)mod;
Expand All @@ -1496,6 +1528,14 @@ void div64(x64emu_t *emu, uint64_t s)
{
__int128 dvd, div, mod;
RESET_FLAGS(emu);
if(box64_dynarec_test) {
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_PF);
CLEAR_FLAG(F_ZF);
CLEAR_FLAG(F_SF);
CLEAR_FLAG(F_OF);
}

dvd = (((__int128)R_RDX) << 64) | R_RAX;
if (s == 0) {
Expand All @@ -1508,11 +1548,6 @@ void div64(x64emu_t *emu, uint64_t s)
INTR_RAISE_DIV0(emu);
return;
}
CLEAR_FLAG(F_CF);
CLEAR_FLAG(F_AF);
CLEAR_FLAG(F_SF);
SET_FLAG(F_ZF);
CONDITIONAL_SET_FLAG(PARITY(mod & 0xff), F_PF);

R_RAX = (uint64_t)div;
R_RDX = (uint64_t)mod;
Expand Down
Loading

0 comments on commit a39c8aa

Please sign in to comment.