Also mask other 16-bit arithmetic (not strictly necessary, but more robust for future changes)

This commit is contained in:
Fabian 2022-09-06 00:15:50 +09:00
parent 21c7a88d16
commit a464c77ec0
3 changed files with 108 additions and 28 deletions

View file

@ -49,9 +49,17 @@ unsafe fn sbb(dest_operand: i32, source_operand: i32, op_size: i32) -> i32 {
| ((source_operand ^ dest_operand) & (res ^ dest_operand)) >> op_size << 11 & FLAG_OVERFLOW;
return res;
}
pub unsafe fn add8(x: i32, y: i32) -> i32 { return add(x, y, OPSIZE_8); }
pub unsafe fn add8(x: i32, y: i32) -> i32 {
dbg_assert!(x >= 0 && x < 0x10000);
dbg_assert!(y >= 0 && y < 0x10000);
return add(x, y, OPSIZE_8);
}
#[no_mangle]
pub unsafe fn add16(x: i32, y: i32) -> i32 { return add(x, y, OPSIZE_16); }
pub unsafe fn add16(x: i32, y: i32) -> i32 {
dbg_assert!(x >= 0 && x < 0x10000);
dbg_assert!(y >= 0 && y < 0x10000);
return add(x, y, OPSIZE_16);
}
pub unsafe fn add32(x: i32, y: i32) -> i32 { return add(x, y, OPSIZE_32); }
pub unsafe fn sub8(x: i32, y: i32) -> i32 { return sub(x, y, OPSIZE_8); }
#[no_mangle]
@ -67,8 +75,16 @@ pub unsafe fn sbb8(x: i32, y: i32) -> i32 { return sbb(x, y, OPSIZE_8); }
#[no_mangle]
pub unsafe fn sbb16(x: i32, y: i32) -> i32 { return sbb(x, y, OPSIZE_16); }
pub unsafe fn sbb32(x: i32, y: i32) -> i32 { return sbb(x, y, OPSIZE_32); }
pub unsafe fn cmp8(x: i32, y: i32) { sub(x, y, OPSIZE_8); }
pub unsafe fn cmp16(x: i32, y: i32) { sub(x, y, OPSIZE_16); }
pub unsafe fn cmp8(x: i32, y: i32) {
dbg_assert!(x >= 0 && x < 0x100);
dbg_assert!(y >= 0 && y < 0x100);
sub(x, y, OPSIZE_8);
}
pub unsafe fn cmp16(x: i32, y: i32) {
dbg_assert!(x >= 0 && x < 0x10000);
dbg_assert!(y >= 0 && y < 0x10000);
sub(x, y, OPSIZE_16);
}
pub unsafe fn cmp32(x: i32, y: i32) { sub(x, y, OPSIZE_32); }
unsafe fn inc(dest_operand: i32, op_size: i32) -> i32 {
*flags = *flags & !1 | getcf() as i32;

View file

@ -761,24 +761,53 @@ pub unsafe fn instr_82_7_reg(r: i32, imm: i32) { cmp8(read_reg8(r), imm); }
pub unsafe fn instr_82_7_mem(addr: i32, imm: i32) {
cmp8(return_on_pagefault!(safe_read8(addr)), imm);
}
pub unsafe fn instr16_83_0_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| add16(x, imm)) }
pub unsafe fn instr16_83_0_reg(r1: i32, imm: i32) { write_reg16(r1, add16(read_reg16(r1), imm)); }
pub unsafe fn instr16_83_1_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| or16(x, imm)) }
pub unsafe fn instr16_83_1_reg(r1: i32, imm: i32) { write_reg16(r1, or16(read_reg16(r1), imm)); }
pub unsafe fn instr16_83_2_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| adc16(x, imm)) }
pub unsafe fn instr16_83_2_reg(r1: i32, imm: i32) { write_reg16(r1, adc16(read_reg16(r1), imm)); }
pub unsafe fn instr16_83_3_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| sbb16(x, imm)) }
pub unsafe fn instr16_83_3_reg(r1: i32, imm: i32) { write_reg16(r1, sbb16(read_reg16(r1), imm)); }
pub unsafe fn instr16_83_4_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| and16(x, imm)) }
pub unsafe fn instr16_83_4_reg(r1: i32, imm: i32) { write_reg16(r1, and16(read_reg16(r1), imm)); }
pub unsafe fn instr16_83_5_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| sub16(x, imm)) }
pub unsafe fn instr16_83_5_reg(r1: i32, imm: i32) { write_reg16(r1, sub16(read_reg16(r1), imm)); }
pub unsafe fn instr16_83_6_mem(addr: i32, imm: i32) { safe_read_write16(addr, &|x| xor16(x, imm)) }
pub unsafe fn instr16_83_6_reg(r1: i32, imm: i32) { write_reg16(r1, xor16(read_reg16(r1), imm)); }
pub unsafe fn instr16_83_7_reg(r: i32, imm: i32) { cmp16(read_reg16(r), imm); }
pub unsafe fn instr16_83_7_mem(addr: i32, imm: i32) {
cmp16(return_on_pagefault!(safe_read16(addr)), imm);
pub unsafe fn instr16_83_0_mem(addr: i32, imm: i32) {
safe_read_write16(addr, &|x| add16(x, imm & 0xFFFF))
}
pub unsafe fn instr16_83_0_reg(r1: i32, imm: i32) {
write_reg16(r1, add16(read_reg16(r1), imm & 0xFFFF));
}
pub unsafe fn instr16_83_1_mem(addr: i32, imm: i32) {
safe_read_write16(addr, &|x| or16(x, imm & 0xFFFF))
}
pub unsafe fn instr16_83_1_reg(r1: i32, imm: i32) {
write_reg16(r1, or16(read_reg16(r1), imm & 0xFFFF));
}
pub unsafe fn instr16_83_2_mem(addr: i32, imm: i32) {
safe_read_write16(addr, &|x| adc16(x, imm & 0xFFFF))
}
pub unsafe fn instr16_83_2_reg(r1: i32, imm: i32) {
write_reg16(r1, adc16(read_reg16(r1), imm & 0xFFFF));
}
pub unsafe fn instr16_83_3_mem(addr: i32, imm: i32) {
safe_read_write16(addr, &|x| sbb16(x, imm & 0xFFFF))
}
pub unsafe fn instr16_83_3_reg(r1: i32, imm: i32) {
write_reg16(r1, sbb16(read_reg16(r1), imm & 0xFFFF));
}
pub unsafe fn instr16_83_4_mem(addr: i32, imm: i32) {
safe_read_write16(addr, &|x| and16(x, imm & 0xFFFF))
}
pub unsafe fn instr16_83_4_reg(r1: i32, imm: i32) {
write_reg16(r1, and16(read_reg16(r1), imm & 0xFFFF));
}
pub unsafe fn instr16_83_5_mem(addr: i32, imm: i32) {
safe_read_write16(addr, &|x| sub16(x, imm & 0xFFFF))
}
pub unsafe fn instr16_83_5_reg(r1: i32, imm: i32) {
write_reg16(r1, sub16(read_reg16(r1), imm & 0xFFFF));
}
pub unsafe fn instr16_83_6_mem(addr: i32, imm: i32) {
safe_read_write16(addr, &|x| xor16(x, imm & 0xFFFF))
}
pub unsafe fn instr16_83_6_reg(r1: i32, imm: i32) {
write_reg16(r1, xor16(read_reg16(r1), imm & 0xFFFF));
}
pub unsafe fn instr16_83_7_reg(r: i32, imm: i32) { cmp16(read_reg16(r), imm & 0xFFFF); }
pub unsafe fn instr16_83_7_mem(addr: i32, imm: i32) {
cmp16(return_on_pagefault!(safe_read16(addr)), imm & 0xFFFF);
}
pub unsafe fn instr32_83_0_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| add32(x, imm)) }
pub unsafe fn instr32_83_0_reg(r1: i32, imm: i32) { write_reg32(r1, add32(read_reg32(r1), imm)); }
pub unsafe fn instr32_83_1_mem(addr: i32, imm: i32) { safe_read_write32(addr, &|x| or32(x, imm)) }

View file

@ -2588,25 +2588,60 @@ define_instruction_read_write_mem32!(gen_sub32, instr32_81_5_mem_jit, instr32_81
define_instruction_read_write_mem16!("xor16", instr16_81_6_mem_jit, instr16_81_6_reg_jit, imm16);
define_instruction_read_write_mem32!(gen_xor32, instr32_81_6_mem_jit, instr32_81_6_reg_jit, imm32);
define_instruction_read_write_mem16!("add16", instr16_83_0_mem_jit, instr16_83_0_reg_jit, imm8s);
define_instruction_read_write_mem16!(
"add16",
instr16_83_0_mem_jit,
instr16_83_0_reg_jit,
imm8s_16bits
);
define_instruction_read_write_mem32!(gen_add32, instr32_83_0_mem_jit, instr32_83_0_reg_jit, imm8s);
define_instruction_read_write_mem16!("or16", instr16_83_1_mem_jit, instr16_83_1_reg_jit, imm8s);
define_instruction_read_write_mem16!(
"or16",
instr16_83_1_mem_jit,
instr16_83_1_reg_jit,
imm8s_16bits
);
define_instruction_read_write_mem32!(gen_or32, instr32_83_1_mem_jit, instr32_83_1_reg_jit, imm8s);
define_instruction_read_write_mem16!("adc16", instr16_83_2_mem_jit, instr16_83_2_reg_jit, imm8s);
define_instruction_read_write_mem16!(
"adc16",
instr16_83_2_mem_jit,
instr16_83_2_reg_jit,
imm8s_16bits
);
define_instruction_read_write_mem32!(gen_adc32, instr32_83_2_mem_jit, instr32_83_2_reg_jit, imm8s);
define_instruction_read_write_mem16!("sbb16", instr16_83_3_mem_jit, instr16_83_3_reg_jit, imm8s);
define_instruction_read_write_mem16!(
"sbb16",
instr16_83_3_mem_jit,
instr16_83_3_reg_jit,
imm8s_16bits
);
define_instruction_read_write_mem32!(gen_sbb32, instr32_83_3_mem_jit, instr32_83_3_reg_jit, imm8s);
define_instruction_read_write_mem16!("and16", instr16_83_4_mem_jit, instr16_83_4_reg_jit, imm8s);
define_instruction_read_write_mem16!(
"and16",
instr16_83_4_mem_jit,
instr16_83_4_reg_jit,
imm8s_16bits
);
define_instruction_read_write_mem32!(gen_and32, instr32_83_4_mem_jit, instr32_83_4_reg_jit, imm8s);
define_instruction_read_write_mem16!("sub16", instr16_83_5_mem_jit, instr16_83_5_reg_jit, imm8s);
define_instruction_read_write_mem16!(
"sub16",
instr16_83_5_mem_jit,
instr16_83_5_reg_jit,
imm8s_16bits
);
define_instruction_read_write_mem32!(gen_sub32, instr32_83_5_mem_jit, instr32_83_5_reg_jit, imm8s);
define_instruction_read_write_mem16!("xor16", instr16_83_6_mem_jit, instr16_83_6_reg_jit, imm8s);
define_instruction_read_write_mem16!(
"xor16",
instr16_83_6_mem_jit,
instr16_83_6_reg_jit,
imm8s_16bits
);
define_instruction_read_write_mem32!(gen_xor32, instr32_83_6_mem_jit, instr32_83_6_reg_jit, imm8s);
define_instruction_read8!(gen_cmp8, instr_80_7_mem_jit, instr_80_7_reg_jit, imm8);