From 32031c8c2cc92b4d09c538ae9d2b8d96d402737b Mon Sep 17 00:00:00 2001 From: Rerumu Date: Sun, 12 Jun 2022 02:28:56 -0400 Subject: [PATCH] Refactor `BrIf` behavior --- codegen-luajit/src/backend/statement.rs | 13 +------------ codegen-luau/src/backend/statement.rs | 13 +------------ wasm-ast/src/builder.rs | 20 ++++++++++++-------- wasm-ast/src/node.rs | 6 ------ wasm-ast/src/visit.rs | 13 +------------ 5 files changed, 15 insertions(+), 50 deletions(-) diff --git a/codegen-luajit/src/backend/statement.rs b/codegen-luajit/src/backend/statement.rs index aa46d7d..4963a09 100644 --- a/codegen-luajit/src/backend/statement.rs +++ b/codegen-luajit/src/backend/statement.rs @@ -5,7 +5,7 @@ use std::{ use parity_wasm::elements::ValueType; use wasm_ast::node::{ - Backward, Br, BrIf, BrTable, Call, CallIndirect, Forward, FuncData, If, SetGlobal, SetLocal, + Backward, Br, BrTable, Call, CallIndirect, Forward, FuncData, If, SetGlobal, SetLocal, SetTemporary, Statement, StoreAt, Terminator, }; @@ -152,16 +152,6 @@ impl Driver for If { } } -impl Driver for BrIf { - fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> { - write!(w, "if ")?; - write_condition(&self.cond, mng, w)?; - write!(w, "then ")?; - write_br_at(self.target, mng, w)?; - write!(w, "end ") - } -} - fn write_call_store(result: Range, w: &mut dyn Write) -> Result<()> { if result.is_empty() { return Ok(()); @@ -236,7 +226,6 @@ impl Driver for Statement { Self::Forward(s) => s.write(mng, w), Self::Backward(s) => s.write(mng, w), Self::If(s) => s.write(mng, w), - Self::BrIf(s) => s.write(mng, w), Self::Call(s) => s.write(mng, w), Self::CallIndirect(s) => s.write(mng, w), Self::SetTemporary(s) => s.write(mng, w), diff --git a/codegen-luau/src/backend/statement.rs b/codegen-luau/src/backend/statement.rs index 6f8e3c4..6932a3f 100644 --- a/codegen-luau/src/backend/statement.rs +++ b/codegen-luau/src/backend/statement.rs @@ -5,7 +5,7 @@ use std::{ use parity_wasm::elements::ValueType; use wasm_ast::node::{ - Backward, Br, BrIf, BrTable, Call, CallIndirect, Forward, FuncData, If, SetGlobal, SetLocal, + Backward, Br, BrTable, Call, CallIndirect, Forward, FuncData, If, SetGlobal, SetLocal, SetTemporary, Statement, StoreAt, Terminator, }; @@ -159,16 +159,6 @@ impl Driver for If { } } -impl Driver for BrIf { - fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> { - write!(w, "if ")?; - write_condition(&self.cond, mng, w)?; - write!(w, "then ")?; - write_br_at(self.target, mng, w)?; - write!(w, "end ") - } -} - fn write_call_store(result: Range, w: &mut dyn Write) -> Result<()> { if result.is_empty() { return Ok(()); @@ -243,7 +233,6 @@ impl Driver for Statement { Self::Forward(s) => s.write(mng, w), Self::Backward(s) => s.write(mng, w), Self::If(s) => s.write(mng, w), - Self::BrIf(s) => s.write(mng, w), Self::Call(s) => s.write(mng, w), Self::CallIndirect(s) => s.write(mng, w), Self::SetTemporary(s) => s.write(mng, w), diff --git a/wasm-ast/src/builder.rs b/wasm-ast/src/builder.rs index 7e9a118..b8671ef 100644 --- a/wasm-ast/src/builder.rs +++ b/wasm-ast/src/builder.rs @@ -4,10 +4,10 @@ use parity_wasm::elements::{ }; use crate::node::{ - Backward, BinOp, BinOpType, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, CmpOpType, - Expression, Forward, FuncData, GetGlobal, GetLocal, GetTemporary, If, LoadAt, LoadType, - MemoryGrow, MemorySize, Select, SetGlobal, SetLocal, SetTemporary, Statement, StoreAt, - StoreType, Terminator, UnOp, UnOpType, Value, + Backward, BinOp, BinOpType, Br, BrTable, Call, CallIndirect, CmpOp, CmpOpType, Expression, + Forward, FuncData, GetGlobal, GetLocal, GetTemporary, If, LoadAt, LoadType, MemoryGrow, + MemorySize, Select, SetGlobal, SetLocal, SetTemporary, Statement, StoreAt, StoreType, + Terminator, UnOp, UnOpType, Value, }; macro_rules! leak_with_predicate { @@ -574,13 +574,17 @@ impl<'a> Builder<'a> { self.set_br_to_block(target); } Inst::BrIf(v) => { - let data = Statement::BrIf(BrIf { + let target: usize = v.try_into().unwrap(); + let data = Statement::If(If { cond: self.target.pop_required(), - target: v.try_into().unwrap(), + truthy: Forward::default(), + falsey: None, }); - // FIXME: Does not push results unless true - self.target.code.push(data); + self.start_block(BlockType::NoResult); + self.pending.last_mut().unwrap().code.push(data); + self.set_br_to_block(target + 1); + self.end_block(); } Inst::BrTable(ref v) => { self.nested_unreachable += 1; diff --git a/wasm-ast/src/node.rs b/wasm-ast/src/node.rs index 9837481..1df0cbd 100644 --- a/wasm-ast/src/node.rs +++ b/wasm-ast/src/node.rs @@ -693,11 +693,6 @@ pub struct If { pub falsey: Option, } -pub struct BrIf { - pub cond: Expression, - pub target: usize, -} - pub struct Call { pub func: usize, pub result: Range, @@ -737,7 +732,6 @@ pub enum Statement { Forward(Forward), Backward(Backward), If(If), - BrIf(BrIf), Call(Call), CallIndirect(CallIndirect), SetTemporary(SetTemporary), diff --git a/wasm-ast/src/visit.rs b/wasm-ast/src/visit.rs index 1edda7f..4a4d758 100644 --- a/wasm-ast/src/visit.rs +++ b/wasm-ast/src/visit.rs @@ -1,5 +1,5 @@ use crate::node::{ - Backward, BinOp, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, Expression, Forward, FuncData, + Backward, BinOp, Br, BrTable, Call, CallIndirect, CmpOp, Expression, Forward, FuncData, GetGlobal, GetLocal, GetTemporary, If, LoadAt, MemoryGrow, MemorySize, Select, SetGlobal, SetLocal, SetTemporary, Statement, StoreAt, Terminator, UnOp, Value, }; @@ -43,8 +43,6 @@ pub trait Visitor { fn visit_if(&mut self, _: &If) {} - fn visit_br_if(&mut self, _: &BrIf) {} - fn visit_call(&mut self, _: &Call) {} fn visit_call_indirect(&mut self, _: &CallIndirect) {} @@ -233,14 +231,6 @@ impl Driver for If { } } -impl Driver for BrIf { - fn accept(&self, visitor: &mut T) { - self.cond.accept(visitor); - - visitor.visit_br_if(self); - } -} - impl Driver for Call { fn accept(&self, visitor: &mut T) { for v in &self.param_list { @@ -302,7 +292,6 @@ impl Driver for Statement { Self::Forward(v) => v.accept(visitor), Self::Backward(v) => v.accept(visitor), Self::If(v) => v.accept(visitor), - Self::BrIf(v) => v.accept(visitor), Self::Call(v) => v.accept(visitor), Self::CallIndirect(v) => v.accept(visitor), Self::SetTemporary(v) => v.accept(visitor),