diff --git a/compiler/gen_wasm/src/backend.rs b/compiler/gen_wasm/src/backend.rs index 5410a041f8..1236c514b0 100644 --- a/compiler/gen_wasm/src/backend.rs +++ b/compiler/gen_wasm/src/backend.rs @@ -26,7 +26,7 @@ use crate::wasm_module::sections::{ Import, ImportDesc, ImportSection, MemorySection, TypeSection, WasmModule, }; use crate::wasm_module::{ - code_builder, BlockType, CodeBuilder, ConstExpr, Export, ExportType, Global, GlobalType, + code_builder, CodeBuilder, ConstExpr, Export, ExportType, Global, GlobalType, LinkingSubSection, LocalId, Signature, SymInfo, ValueType, }; use crate::{ @@ -312,12 +312,12 @@ impl<'a> WasmBackend<'a> { // The rules are confusing, and implementing them would add complexity and slow down code gen. // Instead we use local variables to move a value from an inner block to an outer one. self.block_depth += 1; - self.code_builder.block(BlockType::NoResult); + self.code_builder.block(); } fn start_loop(&mut self) { self.block_depth += 1; - self.code_builder.loop_(BlockType::NoResult); + self.code_builder.loop_(); } fn end_block(&mut self) { @@ -893,7 +893,7 @@ impl<'a> WasmBackend<'a> { // null check self.code_builder.i32_eqz(); - self.code_builder.if_(BlockType::NoResult); + self.code_builder.if_(); self.code_builder.i32_const(*nullable_id as i32); self.code_builder.set_local(local_id); self.code_builder.else_(); diff --git a/compiler/gen_wasm/src/wasm_module/code_builder.rs b/compiler/gen_wasm/src/wasm_module/code_builder.rs index c8fe3edbb9..6c6afc04ad 100644 --- a/compiler/gen_wasm/src/wasm_module/code_builder.rs +++ b/compiler/gen_wasm/src/wasm_module/code_builder.rs @@ -36,29 +36,7 @@ impl Serialize for ValueType { } } -#[derive(PartialEq, Eq, Debug)] -pub enum BlockType { - NoResult, - Value(ValueType), -} - -impl BlockType { - pub fn as_byte(&self) -> u8 { - match self { - Self::NoResult => 0x40, - Self::Value(t) => *t as u8, - } - } -} - -impl From> for BlockType { - fn from(opt: Option) -> Self { - match opt { - Some(ty) => BlockType::Value(ty), - None => BlockType::NoResult, - } - } -} +const BLOCK_NO_RESULT: u8 = 0x40; /// A control block in our model of the VM /// Child blocks cannot "see" values from their parent block @@ -579,9 +557,11 @@ impl<'a> CodeBuilder<'a> { } /// Block instruction - fn inst_block(&mut self, opcode: OpCode, pops: usize, block_type: BlockType) { + fn inst_block(&mut self, opcode: OpCode, pops: usize) { self.inst_base(opcode, pops, false); - self.code.push(block_type.as_byte()); + + // We don't support block result types. Too hard to track types through arbitrary control flow. + self.code.push(BLOCK_NO_RESULT); // Start a new block with a fresh value stack self.vm_block_stack.push(VmBlock { @@ -589,12 +569,7 @@ impl<'a> CodeBuilder<'a> { value_stack: Vec::with_capacity_in(8, self.arena), }); - log_instruction!( - "{:10} {:?}\t{:?}", - format!("{:?}", opcode), - block_type, - &self.vm_block_stack - ); + log_instruction!("{:10}\t{:?}", format!("{:?}", opcode), &self.vm_block_stack); } fn inst_imm32(&mut self, opcode: OpCode, pops: usize, push: bool, immediate: u32) { @@ -647,14 +622,14 @@ impl<'a> CodeBuilder<'a> { instruction_no_args!(unreachable_, UNREACHABLE, 0, false); instruction_no_args!(nop, NOP, 0, false); - pub fn block(&mut self, ty: BlockType) { - self.inst_block(BLOCK, 0, ty); + pub fn block(&mut self) { + self.inst_block(BLOCK, 0); } - pub fn loop_(&mut self, ty: BlockType) { - self.inst_block(LOOP, 0, ty); + pub fn loop_(&mut self) { + self.inst_block(LOOP, 0); } - pub fn if_(&mut self, ty: BlockType) { - self.inst_block(IF, 1, ty); + pub fn if_(&mut self) { + self.inst_block(IF, 1); } pub fn else_(&mut self) { // Reuse the 'then' block but clear its value stack @@ -947,7 +922,7 @@ impl<'a> CodeBuilder<'a> { self.i32_const(expected); self.i32_eq(); self.i32_eqz(); - self.if_(BlockType::NoResult); + self.if_(); self.unreachable_(); // Tell Wasm runtime to throw an exception self.end(); // It matches. Restore the original value to the VM stack and continue the program. diff --git a/compiler/gen_wasm/src/wasm_module/mod.rs b/compiler/gen_wasm/src/wasm_module/mod.rs index 2a404e9b48..5ff36428ed 100644 --- a/compiler/gen_wasm/src/wasm_module/mod.rs +++ b/compiler/gen_wasm/src/wasm_module/mod.rs @@ -4,6 +4,6 @@ pub mod opcodes; pub mod sections; pub mod serialize; -pub use code_builder::{Align, BlockType, CodeBuilder, LocalId, ValueType, VmSymbolState}; +pub use code_builder::{Align, CodeBuilder, LocalId, ValueType, VmSymbolState}; pub use linking::{LinkingSubSection, SymInfo}; pub use sections::{ConstExpr, Export, ExportType, Global, GlobalType, Signature, WasmModule};