type safety

This commit is contained in:
Fabian 2022-11-03 15:02:34 -06:00
parent 58f9902057
commit a11eb20326
3 changed files with 30 additions and 19 deletions

View file

@ -4,7 +4,7 @@ use cpu::cpu::{
};
use cpu::global_pointers;
use cpu::memory;
use jit::{Instruction, InstructionOperand, JitContext};
use jit::{Instruction, InstructionOperand, InstructionOperandDest, JitContext};
use modrm;
use modrm::ModrmByte;
use profiler;
@ -1640,13 +1640,12 @@ pub fn gen_getcf(ctx: &mut JitContext, negate: ConditionNegate) {
}
else {
match dest {
InstructionOperand::WasmLocal(l) => {
InstructionOperandDest::WasmLocal(l) => {
ctx.builder.get_local(l);
},
InstructionOperand::Other => {
InstructionOperandDest::Other => {
gen_get_last_op1(ctx.builder);
},
&InstructionOperand::Immediate(_) => panic!(),
}
match source {
InstructionOperand::WasmLocal(l) => {
@ -1808,7 +1807,7 @@ pub fn gen_test_be(ctx: &mut JitContext, negate: ConditionNegate) {
} => {
gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
match dest {
InstructionOperand::WasmLocal(l) => {
InstructionOperandDest::WasmLocal(l) => {
ctx.builder.get_local(l);
if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
ctx.builder
@ -1816,8 +1815,7 @@ pub fn gen_test_be(ctx: &mut JitContext, negate: ConditionNegate) {
ctx.builder.and_i32();
}
},
&InstructionOperand::Immediate(_) => panic!(),
InstructionOperand::Other => {
InstructionOperandDest::Other => {
gen_get_last_op1(ctx.builder);
},
}
@ -1896,7 +1894,7 @@ pub fn gen_test_l(ctx: &mut JitContext, negate: ConditionNegate) {
} => {
gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
match dest {
InstructionOperand::WasmLocal(l) => {
InstructionOperandDest::WasmLocal(l) => {
ctx.builder.get_local(l);
if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
ctx.builder
@ -1904,7 +1902,7 @@ pub fn gen_test_l(ctx: &mut JitContext, negate: ConditionNegate) {
ctx.builder.shl_i32();
}
},
InstructionOperand::Other => {
InstructionOperandDest::Other => {
gen_get_last_op1(ctx.builder);
if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
ctx.builder
@ -1912,7 +1910,6 @@ pub fn gen_test_l(ctx: &mut JitContext, negate: ConditionNegate) {
ctx.builder.shl_i32();
}
},
&InstructionOperand::Immediate(_) => panic!(),
}
match source {
InstructionOperand::WasmLocal(l) => {
@ -1972,7 +1969,7 @@ pub fn gen_test_le(ctx: &mut JitContext, negate: ConditionNegate) {
} => {
gen_profiler_stat_increment(ctx.builder, profiler::stat::CONDITION_OPTIMISED);
match dest {
InstructionOperand::WasmLocal(l) => {
InstructionOperandDest::WasmLocal(l) => {
ctx.builder.get_local(l);
if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
ctx.builder
@ -1980,7 +1977,7 @@ pub fn gen_test_le(ctx: &mut JitContext, negate: ConditionNegate) {
ctx.builder.shl_i32();
}
},
InstructionOperand::Other => {
InstructionOperandDest::Other => {
gen_get_last_op1(ctx.builder);
if *opsize == OPSIZE_8 || *opsize == OPSIZE_16 {
ctx.builder
@ -1988,7 +1985,6 @@ pub fn gen_test_le(ctx: &mut JitContext, negate: ConditionNegate) {
ctx.builder.shl_i32();
}
},
&InstructionOperand::Immediate(_) => panic!(),
}
match source {
InstructionOperand::WasmLocal(l) => {

View file

@ -233,15 +233,28 @@ impl CachedCode {
};
}
#[derive(PartialEq)]
pub enum InstructionOperandDest {
WasmLocal(WasmLocal),
Other,
}
#[derive(PartialEq)]
pub enum InstructionOperand {
WasmLocal(WasmLocal),
Immediate(i32),
Other,
}
impl Into<InstructionOperand> for InstructionOperandDest {
fn into(self: InstructionOperandDest) -> InstructionOperand {
match self {
InstructionOperandDest::WasmLocal(l) => InstructionOperand::WasmLocal(l),
InstructionOperandDest::Other => InstructionOperand::Other,
}
}
}
pub enum Instruction {
Cmp {
dest: InstructionOperand,
dest: InstructionOperandDest,
source: InstructionOperand,
opsize: i32,
},

View file

@ -7,7 +7,7 @@ use cpu::cpu::{
FLAG_IOPL, FLAG_OVERFLOW, FLAG_SUB, FLAG_VM, FLAG_ZERO, OPSIZE_8, OPSIZE_16, OPSIZE_32,
};
use cpu::global_pointers;
use jit::{Instruction, InstructionOperand, JitContext};
use jit::{Instruction, InstructionOperand, InstructionOperandDest, JitContext};
use modrm::{jit_add_seg_offset, jit_add_seg_offset_no_override, ModrmByte};
use prefix::SEG_PREFIX_ZERO;
use prefix::{PREFIX_66, PREFIX_67, PREFIX_F2, PREFIX_F3};
@ -54,19 +54,21 @@ impl<'a> LocalOrImmediate<'a> {
fn to_instruction_operand(&self, ctx: &mut JitContext) -> InstructionOperand {
match self {
&LocalOrImmediate::WasmLocal(source) => local_to_instruction_operand(ctx, source),
&LocalOrImmediate::WasmLocal(source) => {
local_to_instruction_operand(ctx, source).into()
},
&LocalOrImmediate::Immediate(i) => InstructionOperand::Immediate(i),
}
}
}
fn local_to_instruction_operand(ctx: &mut JitContext, local: &WasmLocal) -> InstructionOperand {
fn local_to_instruction_operand(ctx: &mut JitContext, local: &WasmLocal) -> InstructionOperandDest {
if ctx.register_locals.iter().any(|l| l == local) {
// safe because register locals are alive for the duration of the entire function
InstructionOperand::WasmLocal(local.unsafe_clone())
InstructionOperandDest::WasmLocal(local.unsafe_clone())
}
else {
InstructionOperand::Other
InstructionOperandDest::Other
}
}