From a11eb20326d511001ef02ec15240e9555bdf45cf Mon Sep 17 00:00:00 2001 From: Fabian Date: Thu, 3 Nov 2022 15:02:34 -0600 Subject: [PATCH] type safety --- src/rust/codegen.rs | 22 +++++++++------------- src/rust/jit.rs | 15 ++++++++++++++- src/rust/jit_instructions.rs | 12 +++++++----- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/rust/codegen.rs b/src/rust/codegen.rs index 7775f586..12385f3c 100644 --- a/src/rust/codegen.rs +++ b/src/rust/codegen.rs @@ -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) => { diff --git a/src/rust/jit.rs b/src/rust/jit.rs index 32ab67aa..2b09aea6 100644 --- a/src/rust/jit.rs +++ b/src/rust/jit.rs @@ -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 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, }, diff --git a/src/rust/jit_instructions.rs b/src/rust/jit_instructions.rs index 8ff9e9d4..f55a3a80 100644 --- a/src/rust/jit_instructions.rs +++ b/src/rust/jit_instructions.rs @@ -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 } }