Refactor BrIf behavior

This commit is contained in:
Rerumu 2022-06-12 02:28:56 -04:00
parent 183db977f3
commit 32031c8c2c
5 changed files with 15 additions and 50 deletions

View File

@ -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<usize>, 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),

View File

@ -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<usize>, 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),

View File

@ -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;

View File

@ -693,11 +693,6 @@ pub struct If {
pub falsey: Option<Forward>,
}
pub struct BrIf {
pub cond: Expression,
pub target: usize,
}
pub struct Call {
pub func: usize,
pub result: Range<usize>,
@ -737,7 +732,6 @@ pub enum Statement {
Forward(Forward),
Backward(Backward),
If(If),
BrIf(BrIf),
Call(Call),
CallIndirect(CallIndirect),
SetTemporary(SetTemporary),

View File

@ -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<T: Visitor> Driver<T> for If {
}
}
impl<T: Visitor> Driver<T> for BrIf {
fn accept(&self, visitor: &mut T) {
self.cond.accept(visitor);
visitor.visit_br_if(self);
}
}
impl<T: Visitor> Driver<T> for Call {
fn accept(&self, visitor: &mut T) {
for v in &self.param_list {
@ -302,7 +292,6 @@ impl<T: Visitor> Driver<T> 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),