From 83a768d77ac57f5d8e8884173765e0efc80831f4 Mon Sep 17 00:00:00 2001 From: Henry Gressmann Date: Tue, 27 Feb 2024 01:50:54 +0100 Subject: reduce instruction enum size Signed-off-by: Henry Gressmann --- crates/parser/src/conversion.rs | 2 +- crates/parser/src/visit.rs | 29 ++++++++++++++++------------- 2 files changed, 17 insertions(+), 14 deletions(-) (limited to 'crates/parser') diff --git a/crates/parser/src/conversion.rs b/crates/parser/src/conversion.rs index 53cceb6..c13d08f 100644 --- a/crates/parser/src/conversion.rs +++ b/crates/parser/src/conversion.rs @@ -226,7 +226,7 @@ pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> ValType { } pub(crate) fn convert_memarg(memarg: wasmparser::MemArg) -> MemoryArg { - MemoryArg { offset: memarg.offset, align: memarg.align, align_max: memarg.max_align, mem_addr: memarg.memory } + MemoryArg { offset: memarg.offset, mem_addr: memarg.memory } } pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result { diff --git a/crates/parser/src/visit.rs b/crates/parser/src/visit.rs index f994e7e..3a10a93 100644 --- a/crates/parser/src/visit.rs +++ b/crates/parser/src/visit.rs @@ -338,15 +338,16 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder { } fn visit_local_set(&mut self, idx: u32) -> Self::Output { - if self.instructions.len() < 1 { - return self.visit(Instruction::I64Rotl); + if let Some(instruction) = self.instructions.last_mut() { + match instruction { + // Needs more testing, seems to make performance worse + // Instruction::LocalGet(a) => *instruction = Instruction::LocalGetSet(*a, idx), + _ => return self.visit(Instruction::LocalSet(idx)), + }; + // Ok(()) + } else { + self.visit(Instruction::LocalSet(idx)) } - - // LocalGetSet - match self.instructions[self.instructions.len() - 1..] { - // Instruction::LocalGet(a) => *instruction = Instruction::LocalGetSet(*a, idx), - _ => return self.visit(Instruction::LocalSet(idx)), - }; } fn visit_local_tee(&mut self, idx: u32) -> Self::Output { @@ -413,7 +414,7 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder { match self.instructions[label_pointer] { Instruction::Else(ref mut else_instr_end_offset) => { - *else_instr_end_offset = current_instr_ptr - label_pointer; + *else_instr_end_offset = (current_instr_ptr - label_pointer as usize) as u32; #[cold] fn error() -> crate::ParseError { @@ -430,13 +431,13 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder { return Err(error()); }; - *else_offset = Some(label_pointer - if_label_pointer); - *end_offset = current_instr_ptr - if_label_pointer; + *else_offset = Some((label_pointer - if_label_pointer) as u32); + *end_offset = (current_instr_ptr - if_label_pointer) as u32; } Instruction::Block(_, ref mut end_offset) | Instruction::Loop(_, ref mut end_offset) | Instruction::If(_, _, ref mut end_offset) => { - *end_offset = current_instr_ptr - label_pointer; + *end_offset = (current_instr_ptr - label_pointer) as u32; } _ => { return Err(crate::ParseError::UnsupportedOperator( @@ -456,7 +457,9 @@ impl<'a> wasmparser::VisitOperator<'a> for FunctionBuilder { .collect::, wasmparser::BinaryReaderError>>() .expect("BrTable targets are invalid, this should have been caught by the validator"); - self.instructions.extend(IntoIterator::into_iter([Instruction::BrTable(def, instrs.len())]).chain(instrs)); + self.instructions + .extend(IntoIterator::into_iter([Instruction::BrTable(def, instrs.len() as u32)]).chain(instrs)); + Ok(()) } -- cgit v1.3.1