From a379fb2e91cfd2550b00aac6fe3b4cf939639055 Mon Sep 17 00:00:00 2001 From: Rerumu Date: Sun, 12 Jun 2022 04:02:46 -0400 Subject: [PATCH] Fix `BrIf` reachability and simplify block adds --- wasm-ast/src/builder.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/wasm-ast/src/builder.rs b/wasm-ast/src/builder.rs index a6f1978..bac0ddc 100644 --- a/wasm-ast/src/builder.rs +++ b/wasm-ast/src/builder.rs @@ -382,7 +382,7 @@ impl<'a> Builder<'a> { } } - fn start_block(&mut self, typ: BlockType) { + fn start_block(&mut self, typ: BlockType, stat: Statement) { let (num_param, num_result) = match typ { BlockType::NoResult => (0, 0), BlockType::Value(_) => (0, 1), @@ -397,6 +397,7 @@ impl<'a> Builder<'a> { let mut old = std::mem::take(&mut self.target); old.leak_all(); + old.code.push(stat); self.target.stack = old.pop_len(num_param); self.target.num_result = num_result; @@ -540,26 +541,23 @@ impl<'a> Builder<'a> { } Inst::Nop => {} Inst::Block(typ) => { - let data = Statement::Forward(Forward::default()); + let stat = Statement::Forward(Forward::default()); - self.start_block(typ); - self.pending.last_mut().unwrap().code.push(data); + self.start_block(typ, stat); } Inst::Loop(typ) => { - let data = Statement::Backward(Backward::default()); + let stat = Statement::Backward(Backward::default()); - self.start_block(typ); - self.pending.last_mut().unwrap().code.push(data); + self.start_block(typ, stat); } Inst::If(typ) => { - let data = Statement::If(If { + let stat = Statement::If(If { cond: self.target.pop_required(), truthy: Forward::default(), falsey: None, }); - self.start_block(typ); - self.pending.last_mut().unwrap().code.push(data); + self.start_block(typ, stat); } Inst::Else => { self.set_return_data(0); @@ -576,16 +574,17 @@ impl<'a> Builder<'a> { } Inst::BrIf(v) => { let target: usize = v.try_into().unwrap(); - let data = Statement::If(If { + let stat = Statement::If(If { cond: self.target.pop_required(), truthy: Forward::default(), falsey: None, }); - self.start_block(BlockType::NoResult); - self.pending.last_mut().unwrap().code.push(data); + self.start_block(BlockType::NoResult, stat); self.set_br_to_block(target + 1); self.end_block(); + + self.nested_unreachable -= 1; } Inst::BrTable(ref v) => { self.nested_unreachable += 1;