diff --git a/codegen-luajit/src/analyzer/localize.rs b/codegen-luajit/src/analyzer/localize.rs index ff75881..5f1a255 100644 --- a/codegen-luajit/src/analyzer/localize.rs +++ b/codegen-luajit/src/analyzer/localize.rs @@ -1,7 +1,7 @@ use std::collections::BTreeSet; use wasm_ast::{ - node::{AnyBinOp, AnyCmpOp, AnyLoad, AnyStore, AnyUnOp, Function}, + node::{BinOp, CmpOp, Intermediate, LoadAt, StoreAt, UnOp}, visit::{Driver, Visitor}, }; @@ -10,25 +10,25 @@ struct Visit { } impl Visitor for Visit { - fn visit_any_load(&mut self, v: &AnyLoad) { - let name = v.op.as_name(); + fn visit_load_at(&mut self, v: &LoadAt) { + let name = v.what.as_name(); self.result.insert(("load", name)); } - fn visit_any_store(&mut self, v: &AnyStore) { - let name = v.op.as_name(); + fn visit_store_at(&mut self, v: &StoreAt) { + let name = v.what.as_name(); self.result.insert(("store", name)); } - fn visit_any_unop(&mut self, v: &AnyUnOp) { + fn visit_un_op(&mut self, v: &UnOp) { let name = v.op.as_name(); self.result.insert(name); } - fn visit_any_binop(&mut self, v: &AnyBinOp) { + fn visit_bin_op(&mut self, v: &BinOp) { if v.op.as_operator().is_some() { return; } @@ -38,7 +38,7 @@ impl Visitor for Visit { self.result.insert(name); } - fn visit_any_cmpop(&mut self, v: &AnyCmpOp) { + fn visit_cmp_op(&mut self, v: &CmpOp) { if v.op.as_operator().is_some() { return; } @@ -49,12 +49,12 @@ impl Visitor for Visit { } } -pub fn visit(func: &Function) -> BTreeSet<(&'static str, &'static str)> { +pub fn visit(ir: &Intermediate) -> BTreeSet<(&'static str, &'static str)> { let mut visit = Visit { result: BTreeSet::new(), }; - func.accept(&mut visit); + ir.accept(&mut visit); visit.result } diff --git a/codegen-luajit/src/analyzer/memory.rs b/codegen-luajit/src/analyzer/memory.rs index 551d6e4..1c70f65 100644 --- a/codegen-luajit/src/analyzer/memory.rs +++ b/codegen-luajit/src/analyzer/memory.rs @@ -1,7 +1,7 @@ use std::collections::BTreeSet; use wasm_ast::{ - node::{AnyLoad, AnyStore, Function, MemoryGrow, MemorySize}, + node::{Intermediate, LoadAt, MemoryGrow, MemorySize, StoreAt}, visit::{Driver, Visitor}, }; @@ -10,11 +10,11 @@ struct Visit { } impl Visitor for Visit { - fn visit_any_store(&mut self, _: &AnyStore) { + fn visit_store_at(&mut self, _: &StoreAt) { self.result.insert(0); } - fn visit_any_load(&mut self, _: &AnyLoad) { + fn visit_load_at(&mut self, _: &LoadAt) { self.result.insert(0); } @@ -27,12 +27,12 @@ impl Visitor for Visit { } } -pub fn visit(func: &Function) -> BTreeSet { +pub fn visit(ir: &Intermediate) -> BTreeSet { let mut visit = Visit { result: BTreeSet::new(), }; - func.accept(&mut visit); + ir.accept(&mut visit); visit.result } diff --git a/codegen-luajit/src/gen.rs b/codegen-luajit/src/gen.rs index 9ffeeb0..74d4908 100644 --- a/codegen-luajit/src/gen.rs +++ b/codegen-luajit/src/gen.rs @@ -7,9 +7,9 @@ use parity_wasm::elements::{ use wasm_ast::{ builder::{Builder, TypeInfo}, node::{ - AnyBinOp, AnyCmpOp, AnyLoad, AnyStore, AnyUnOp, Backward, Br, BrIf, BrTable, Call, - CallIndirect, Else, Expression, Forward, Function, GetGlobal, GetLocal, If, Memorize, - MemoryGrow, MemorySize, Recall, Return, Select, SetGlobal, SetLocal, Statement, Value, + Backward, BinOp, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, Else, Expression, Forward, + GetGlobal, GetLocal, If, Intermediate, LoadAt, Memorize, MemoryGrow, MemorySize, Recall, + Return, Select, SetGlobal, SetLocal, Statement, StoreAt, UnOp, Value, }, writer::{Transpiler, Writer}, }; @@ -109,9 +109,9 @@ fn write_named_array(name: &str, len: usize, w: Writer) -> Result<()> { write!(w, "local {name} = table_new({len}, {hash})") } -fn write_parameter_list(func: &Function, w: Writer) -> Result<()> { +fn write_parameter_list(ir: &Intermediate, w: Writer) -> Result<()> { write!(w, "function(")?; - write_ascending("param", 0..func.num_param, w)?; + write_ascending("param", 0..ir.num_param, w)?; write!(w, ")") } @@ -124,10 +124,10 @@ fn write_call_store(result: Range, w: Writer) -> Result<()> { write!(w, " = ") } -fn write_variable_list(func: &Function, w: Writer) -> Result<()> { +fn write_variable_list(ir: &Intermediate, w: Writer) -> Result<()> { let mut total = 0; - for data in &func.local_data { + for data in &ir.local_data { let range = total..total + usize::try_from(data.count()).unwrap(); let typed = data.value_type(); @@ -139,9 +139,9 @@ fn write_variable_list(func: &Function, w: Writer) -> Result<()> { write_separated(range, |_, w| write!(w, "ZERO_{typed} "), w)?; } - if func.num_stack != 0 { + if ir.num_stack != 0 { write!(w, "local ")?; - write_ascending("reg", 0..func.num_stack, w)?; + write_ascending("reg", 0..ir.num_stack, w)?; write!(w, " ")?; } @@ -252,9 +252,9 @@ impl Driver for GetGlobal { } } -impl Driver for AnyLoad { +impl Driver for LoadAt { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { - write!(w, "load_{}(memory_at_0, ", self.op.as_name())?; + write!(w, "load_{}(memory_at_0, ", self.what.as_name())?; self.pointer.visit(v, w)?; write!(w, "+ {})", self.offset) } @@ -285,7 +285,7 @@ impl Driver for Value { } } -impl Driver for AnyUnOp { +impl Driver for UnOp { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { let (a, b) = self.op.as_name(); @@ -309,7 +309,7 @@ fn write_bin_call( write!(w, ")") } -impl Driver for AnyBinOp { +impl Driver for BinOp { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { if let Some(op) = self.op.as_operator() { write!(w, "(")?; @@ -323,7 +323,7 @@ impl Driver for AnyBinOp { } } -fn write_any_cmp(cmp: &AnyCmpOp, v: &mut Visitor, w: Writer) -> Result<()> { +fn write_any_cmp(cmp: &CmpOp, v: &mut Visitor, w: Writer) -> Result<()> { if let Some(op) = cmp.op.as_operator() { cmp.lhs.visit(v, w)?; write!(w, "{op} ")?; @@ -333,7 +333,7 @@ fn write_any_cmp(cmp: &AnyCmpOp, v: &mut Visitor, w: Writer) -> Result<()> { } } -impl Driver for AnyCmpOp { +impl Driver for CmpOp { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { write!(w, "(")?; write_any_cmp(self, v, w)?; @@ -343,7 +343,7 @@ impl Driver for AnyCmpOp { // Removes the boolean to integer conversion fn write_as_condition(data: &Expression, v: &mut Visitor, w: Writer) -> Result<()> { - if let Expression::AnyCmpOp(o) = data { + if let Expression::CmpOp(o) = data { write_any_cmp(o, v, w) } else { data.visit(v, w)?; @@ -362,13 +362,13 @@ impl Driver for Expression { Self::Select(e) => e.visit(v, w), Self::GetLocal(e) => e.visit(v, w), Self::GetGlobal(e) => e.visit(v, w), - Self::AnyLoad(e) => e.visit(v, w), + Self::LoadAt(e) => e.visit(v, w), Self::MemorySize(e) => e.visit(v, w), Self::MemoryGrow(e) => e.visit(v, w), Self::Value(e) => e.visit(v, w), - Self::AnyUnOp(e) => e.visit(v, w), - Self::AnyBinOp(e) => e.visit(v, w), - Self::AnyCmpOp(e) => e.visit(v, w), + Self::UnOp(e) => e.visit(v, w), + Self::BinOp(e) => e.visit(v, w), + Self::CmpOp(e) => e.visit(v, w), } } } @@ -544,9 +544,9 @@ impl Driver for SetGlobal { } } -impl Driver for AnyStore { +impl Driver for StoreAt { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { - write!(w, "store_{}(memory_at_0, ", self.op.as_name())?; + write!(w, "store_{}(memory_at_0, ", self.what.as_name())?; self.pointer.visit(v, w)?; write!(w, "+ {}, ", self.offset)?; self.value.visit(v, w)?; @@ -570,12 +570,12 @@ impl Driver for Statement { Self::CallIndirect(s) => s.visit(v, w), Self::SetLocal(s) => s.visit(v, w), Self::SetGlobal(s) => s.visit(v, w), - Self::AnyStore(s) => s.visit(v, w), + Self::StoreAt(s) => s.visit(v, w), } } } -impl Driver for Function { +impl Driver for Intermediate { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { write_parameter_list(self, w)?; @@ -815,7 +815,7 @@ impl<'a> Generator<'a> { write!(w, "}} end ") } - fn gen_localize(func_list: &[Function], w: Writer) -> Result<()> { + fn gen_localize(func_list: &[Intermediate], w: Writer) -> Result<()> { let mut loc_set = BTreeSet::new(); for func in func_list { @@ -829,7 +829,7 @@ impl<'a> Generator<'a> { // FIXME: Make `pub` only for fuzzing. #[must_use] - pub fn build_func_list(&self) -> Vec { + pub fn build_func_list(&self) -> Vec { let list = self.wasm.code_section().unwrap().bodies(); let iter = list.iter().enumerate(); @@ -842,7 +842,7 @@ impl<'a> Generator<'a> { /// /// # Panics /// If the number of functions overflows 32 bits. - pub fn gen_func_list(&self, func_list: &[Function], w: Writer) -> Result<()> { + pub fn gen_func_list(&self, func_list: &[Intermediate], w: Writer) -> Result<()> { let offset = self.type_info.len_ex().try_into().unwrap(); func_list.iter().enumerate().try_for_each(|(i, v)| { diff --git a/codegen-luau/src/analyzer/localize.rs b/codegen-luau/src/analyzer/localize.rs index ff75881..5f1a255 100644 --- a/codegen-luau/src/analyzer/localize.rs +++ b/codegen-luau/src/analyzer/localize.rs @@ -1,7 +1,7 @@ use std::collections::BTreeSet; use wasm_ast::{ - node::{AnyBinOp, AnyCmpOp, AnyLoad, AnyStore, AnyUnOp, Function}, + node::{BinOp, CmpOp, Intermediate, LoadAt, StoreAt, UnOp}, visit::{Driver, Visitor}, }; @@ -10,25 +10,25 @@ struct Visit { } impl Visitor for Visit { - fn visit_any_load(&mut self, v: &AnyLoad) { - let name = v.op.as_name(); + fn visit_load_at(&mut self, v: &LoadAt) { + let name = v.what.as_name(); self.result.insert(("load", name)); } - fn visit_any_store(&mut self, v: &AnyStore) { - let name = v.op.as_name(); + fn visit_store_at(&mut self, v: &StoreAt) { + let name = v.what.as_name(); self.result.insert(("store", name)); } - fn visit_any_unop(&mut self, v: &AnyUnOp) { + fn visit_un_op(&mut self, v: &UnOp) { let name = v.op.as_name(); self.result.insert(name); } - fn visit_any_binop(&mut self, v: &AnyBinOp) { + fn visit_bin_op(&mut self, v: &BinOp) { if v.op.as_operator().is_some() { return; } @@ -38,7 +38,7 @@ impl Visitor for Visit { self.result.insert(name); } - fn visit_any_cmpop(&mut self, v: &AnyCmpOp) { + fn visit_cmp_op(&mut self, v: &CmpOp) { if v.op.as_operator().is_some() { return; } @@ -49,12 +49,12 @@ impl Visitor for Visit { } } -pub fn visit(func: &Function) -> BTreeSet<(&'static str, &'static str)> { +pub fn visit(ir: &Intermediate) -> BTreeSet<(&'static str, &'static str)> { let mut visit = Visit { result: BTreeSet::new(), }; - func.accept(&mut visit); + ir.accept(&mut visit); visit.result } diff --git a/codegen-luau/src/analyzer/memory.rs b/codegen-luau/src/analyzer/memory.rs index 551d6e4..1c70f65 100644 --- a/codegen-luau/src/analyzer/memory.rs +++ b/codegen-luau/src/analyzer/memory.rs @@ -1,7 +1,7 @@ use std::collections::BTreeSet; use wasm_ast::{ - node::{AnyLoad, AnyStore, Function, MemoryGrow, MemorySize}, + node::{Intermediate, LoadAt, MemoryGrow, MemorySize, StoreAt}, visit::{Driver, Visitor}, }; @@ -10,11 +10,11 @@ struct Visit { } impl Visitor for Visit { - fn visit_any_store(&mut self, _: &AnyStore) { + fn visit_store_at(&mut self, _: &StoreAt) { self.result.insert(0); } - fn visit_any_load(&mut self, _: &AnyLoad) { + fn visit_load_at(&mut self, _: &LoadAt) { self.result.insert(0); } @@ -27,12 +27,12 @@ impl Visitor for Visit { } } -pub fn visit(func: &Function) -> BTreeSet { +pub fn visit(ir: &Intermediate) -> BTreeSet { let mut visit = Visit { result: BTreeSet::new(), }; - func.accept(&mut visit); + ir.accept(&mut visit); visit.result } diff --git a/codegen-luau/src/gen.rs b/codegen-luau/src/gen.rs index 9c951b5..a78103d 100644 --- a/codegen-luau/src/gen.rs +++ b/codegen-luau/src/gen.rs @@ -7,9 +7,9 @@ use parity_wasm::elements::{ use wasm_ast::{ builder::{Builder, TypeInfo}, node::{ - AnyBinOp, AnyCmpOp, AnyLoad, AnyStore, AnyUnOp, Backward, Br, BrIf, BrTable, Call, - CallIndirect, Else, Expression, Forward, Function, GetGlobal, GetLocal, If, Memorize, - MemoryGrow, MemorySize, Recall, Return, Select, SetGlobal, SetLocal, Statement, Value, + Backward, BinOp, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, Else, Expression, Forward, + GetGlobal, GetLocal, If, Intermediate, LoadAt, Memorize, MemoryGrow, MemorySize, Recall, + Return, Select, SetGlobal, SetLocal, Statement, StoreAt, UnOp, Value, }, writer::{Transpiler, Writer}, }; @@ -108,9 +108,9 @@ fn write_named_array(name: &str, len: usize, w: Writer) -> Result<()> { write!(w, "local {name} = table.create({len})") } -fn write_parameter_list(func: &Function, w: Writer) -> Result<()> { +fn write_parameter_list(ir: &Intermediate, w: Writer) -> Result<()> { write!(w, "function(")?; - write_ascending("param", 0..func.num_param, w)?; + write_ascending("param", 0..ir.num_param, w)?; write!(w, ")") } @@ -123,10 +123,10 @@ fn write_call_store(result: Range, w: Writer) -> Result<()> { write!(w, " = ") } -fn write_variable_list(func: &Function, w: Writer) -> Result<()> { +fn write_variable_list(ir: &Intermediate, w: Writer) -> Result<()> { let mut total = 0; - for data in &func.local_data { + for data in &ir.local_data { let range = total..total + usize::try_from(data.count()).unwrap(); let typed = data.value_type(); @@ -138,9 +138,9 @@ fn write_variable_list(func: &Function, w: Writer) -> Result<()> { write_separated(range, |_, w| write!(w, "ZERO_{typed} "), w)?; } - if func.num_stack != 0 { + if ir.num_stack != 0 { write!(w, "local ")?; - write_ascending("reg", 0..func.num_stack, w)?; + write_ascending("reg", 0..ir.num_stack, w)?; write!(w, " ")?; } @@ -256,9 +256,9 @@ impl Driver for GetGlobal { } } -impl Driver for AnyLoad { +impl Driver for LoadAt { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { - write!(w, "load_{}(memory_at_0, ", self.op.as_name())?; + write!(w, "load_{}(memory_at_0, ", self.what.as_name())?; self.pointer.visit(v, w)?; write!(w, "+ {})", self.offset) } @@ -289,7 +289,7 @@ impl Driver for Value { } } -impl Driver for AnyUnOp { +impl Driver for UnOp { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { let (a, b) = self.op.as_name(); @@ -299,7 +299,7 @@ impl Driver for AnyUnOp { } } -fn write_bin_op(bin_op: &AnyBinOp, v: &mut Visitor, w: Writer) -> Result<()> { +fn write_bin_op(bin_op: &BinOp, v: &mut Visitor, w: Writer) -> Result<()> { let op = bin_op.op.as_operator().unwrap(); write!(w, "(")?; @@ -309,7 +309,7 @@ fn write_bin_op(bin_op: &AnyBinOp, v: &mut Visitor, w: Writer) -> Result<()> { write!(w, ")") } -fn write_bin_op_call(bin_op: &AnyBinOp, v: &mut Visitor, w: Writer) -> Result<()> { +fn write_bin_op_call(bin_op: &BinOp, v: &mut Visitor, w: Writer) -> Result<()> { let (a, b) = bin_op.op.as_name(); write!(w, "{a}_{b}(")?; @@ -319,7 +319,7 @@ fn write_bin_op_call(bin_op: &AnyBinOp, v: &mut Visitor, w: Writer) -> Result<() write!(w, ")") } -impl Driver for AnyBinOp { +impl Driver for BinOp { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { if self.op.as_operator().is_some() { write_bin_op(self, v, w) @@ -329,7 +329,7 @@ impl Driver for AnyBinOp { } } -impl Driver for AnyCmpOp { +impl Driver for CmpOp { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { let (a, b) = self.op.as_name(); @@ -352,13 +352,13 @@ impl Driver for Expression { Self::Select(e) => e.visit(v, w), Self::GetLocal(e) => e.visit(v, w), Self::GetGlobal(e) => e.visit(v, w), - Self::AnyLoad(e) => e.visit(v, w), + Self::LoadAt(e) => e.visit(v, w), Self::MemorySize(e) => e.visit(v, w), Self::MemoryGrow(e) => e.visit(v, w), Self::Value(e) => e.visit(v, w), - Self::AnyUnOp(e) => e.visit(v, w), - Self::AnyBinOp(e) => e.visit(v, w), - Self::AnyCmpOp(e) => e.visit(v, w), + Self::UnOp(e) => e.visit(v, w), + Self::BinOp(e) => e.visit(v, w), + Self::CmpOp(e) => e.visit(v, w), } } } @@ -548,9 +548,9 @@ impl Driver for SetGlobal { } } -impl Driver for AnyStore { +impl Driver for StoreAt { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { - write!(w, "store_{}(memory_at_0, ", self.op.as_name())?; + write!(w, "store_{}(memory_at_0, ", self.what.as_name())?; self.pointer.visit(v, w)?; write!(w, "+ {}, ", self.offset)?; self.value.visit(v, w)?; @@ -574,12 +574,12 @@ impl Driver for Statement { Statement::CallIndirect(s) => s.visit(v, w), Statement::SetLocal(s) => s.visit(v, w), Statement::SetGlobal(s) => s.visit(v, w), - Statement::AnyStore(s) => s.visit(v, w), + Statement::StoreAt(s) => s.visit(v, w), } } } -impl Driver for Function { +impl Driver for Intermediate { fn visit(&self, v: &mut Visitor, w: Writer) -> Result<()> { write_parameter_list(self, w)?; @@ -817,7 +817,7 @@ impl<'a> Generator<'a> { write!(w, "}} end ") } - fn gen_localize(func_list: &[Function], w: Writer) -> Result<()> { + fn gen_localize(func_list: &[Intermediate], w: Writer) -> Result<()> { let mut loc_set = BTreeSet::new(); for func in func_list { @@ -829,7 +829,7 @@ impl<'a> Generator<'a> { .try_for_each(|(a, b)| write!(w, "local {a}_{b} = rt.{a}.{b} ")) } - fn build_func_list(&self) -> Vec { + fn build_func_list(&self) -> Vec { let list = self.wasm.code_section().unwrap().bodies(); let iter = list.iter().enumerate(); @@ -837,7 +837,7 @@ impl<'a> Generator<'a> { .collect() } - fn gen_func_list(&self, func_list: &[Function], w: Writer) -> Result<()> { + fn gen_func_list(&self, func_list: &[Intermediate], w: Writer) -> Result<()> { let offset = self.type_info.len_ex().try_into().unwrap(); func_list.iter().enumerate().try_for_each(|(i, v)| { diff --git a/wasm-ast/src/builder.rs b/wasm-ast/src/builder.rs index 8d3faea..c4a4922 100644 --- a/wasm-ast/src/builder.rs +++ b/wasm-ast/src/builder.rs @@ -4,10 +4,10 @@ use parity_wasm::elements::{ }; use crate::node::{ - AnyBinOp, AnyCmpOp, AnyLoad, AnyStore, AnyUnOp, Backward, BinOp, Br, BrIf, BrTable, Call, - CallIndirect, CmpOp, Else, Expression, Forward, Function, GetGlobal, GetLocal, If, Load, - Memorize, MemoryGrow, MemorySize, Recall, Return, Select, SetGlobal, SetLocal, Statement, - Store, UnOp, Value, + Backward, BinOp, BinOpType, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, CmpOpType, Else, + Expression, Forward, GetGlobal, GetLocal, If, Intermediate, LoadAt, LoadType, Memorize, + MemoryGrow, MemorySize, Recall, Return, Select, SetGlobal, SetLocal, Statement, StoreAt, + StoreType, UnOp, UnOpType, Value, }; struct Arity { @@ -175,50 +175,48 @@ impl Stacked { } } - fn push_load(&mut self, op: Load, offset: u32) { + fn push_load(&mut self, what: LoadType, offset: u32) { let pointer = Box::new(self.pop()); - self.stack.push(Expression::AnyLoad(AnyLoad { - op, + self.stack.push(Expression::LoadAt(LoadAt { + what, offset, pointer, })); } - fn gen_store(&mut self, op: Store, offset: u32, stat: &mut Vec) { + fn gen_store(&mut self, what: StoreType, offset: u32, stat: &mut Vec) { let value = self.pop(); let pointer = self.pop(); self.gen_leak_pending(stat); - stat.push(Statement::AnyStore(AnyStore { - op, + stat.push(Statement::StoreAt(StoreAt { + what, offset, pointer, value, })); } - fn push_un_op(&mut self, op: UnOp) { + fn push_un_op(&mut self, op: UnOpType) { let rhs = Box::new(self.pop()); - self.stack.push(Expression::AnyUnOp(AnyUnOp { op, rhs })); + self.stack.push(Expression::UnOp(UnOp { op, rhs })); } - fn push_bin_op(&mut self, op: BinOp) { + fn push_bin_op(&mut self, op: BinOpType) { let rhs = Box::new(self.pop()); let lhs = Box::new(self.pop()); - self.stack - .push(Expression::AnyBinOp(AnyBinOp { op, lhs, rhs })); + self.stack.push(Expression::BinOp(BinOp { op, lhs, rhs })); } - fn push_cmp_op(&mut self, op: CmpOp) { + fn push_cmp_op(&mut self, op: CmpOpType) { let rhs = Box::new(self.pop()); let lhs = Box::new(self.pop()); - self.stack - .push(Expression::AnyCmpOp(AnyCmpOp { op, lhs, rhs })); + self.stack.push(Expression::CmpOp(CmpOp { op, lhs, rhs })); } // Since Eqz is the only unary comparison it's cleaner to @@ -227,13 +225,13 @@ impl Stacked { match inst { Instruction::I32Eqz => { self.push_constant(0_i32); - self.push_cmp_op(CmpOp::Eq_I32); + self.push_cmp_op(CmpOpType::Eq_I32); true } Instruction::I64Eqz => { self.push_constant(0_i64); - self.push_cmp_op(CmpOp::Eq_I64); + self.push_cmp_op(CmpOpType::Eq_I64); true } @@ -242,15 +240,15 @@ impl Stacked { } fn try_operation(&mut self, inst: &Instruction) -> bool { - if let Ok(op) = UnOp::try_from(inst) { + if let Ok(op) = UnOpType::try_from(inst) { self.push_un_op(op); true - } else if let Ok(op) = BinOp::try_from(inst) { + } else if let Ok(op) = BinOpType::try_from(inst) { self.push_bin_op(op); true - } else if let Ok(op) = CmpOp::try_from(inst) { + } else if let Ok(op) = CmpOpType::try_from(inst) { self.push_cmp_op(op); true @@ -291,7 +289,7 @@ impl<'a> Builder<'a> { } #[must_use] - pub fn consume(mut self, index: usize, func: &'a FuncBody) -> Function { + pub fn consume(mut self, index: usize, func: &'a FuncBody) -> Intermediate { let arity = &self.type_info.arity_of(self.type_info.len_ex() + index); self.num_result = arity.num_result; @@ -299,7 +297,7 @@ impl<'a> Builder<'a> { let code = self.new_forward(&mut func.code().elements()); let num_stack = self.data.last_stack; - Function { + Intermediate { local_data: func.locals().to_vec(), num_param: arity.num_param, num_stack, @@ -533,29 +531,29 @@ impl<'a> Builder<'a> { stat.push(Statement::SetGlobal(SetGlobal { var, value })); } - Inst::I32Load(_, o) => self.data.push_load(Load::I32, o), - Inst::I64Load(_, o) => self.data.push_load(Load::I64, o), - Inst::F32Load(_, o) => self.data.push_load(Load::F32, o), - Inst::F64Load(_, o) => self.data.push_load(Load::F64, o), - Inst::I32Load8S(_, o) => self.data.push_load(Load::I32_I8, o), - Inst::I32Load8U(_, o) => self.data.push_load(Load::I32_U8, o), - Inst::I32Load16S(_, o) => self.data.push_load(Load::I32_I16, o), - Inst::I32Load16U(_, o) => self.data.push_load(Load::I32_U16, o), - Inst::I64Load8S(_, o) => self.data.push_load(Load::I64_I8, o), - Inst::I64Load8U(_, o) => self.data.push_load(Load::I64_U8, o), - Inst::I64Load16S(_, o) => self.data.push_load(Load::I64_I16, o), - Inst::I64Load16U(_, o) => self.data.push_load(Load::I64_U16, o), - Inst::I64Load32S(_, o) => self.data.push_load(Load::I64_I32, o), - Inst::I64Load32U(_, o) => self.data.push_load(Load::I64_U32, o), - Inst::I32Store(_, o) => self.data.gen_store(Store::I32, o, &mut stat), - Inst::I64Store(_, o) => self.data.gen_store(Store::I64, o, &mut stat), - Inst::F32Store(_, o) => self.data.gen_store(Store::F32, o, &mut stat), - Inst::F64Store(_, o) => self.data.gen_store(Store::F64, o, &mut stat), - Inst::I32Store8(_, o) => self.data.gen_store(Store::I32_N8, o, &mut stat), - Inst::I32Store16(_, o) => self.data.gen_store(Store::I32_N16, o, &mut stat), - Inst::I64Store8(_, o) => self.data.gen_store(Store::I64_N8, o, &mut stat), - Inst::I64Store16(_, o) => self.data.gen_store(Store::I64_N16, o, &mut stat), - Inst::I64Store32(_, o) => self.data.gen_store(Store::I64_N32, o, &mut stat), + Inst::I32Load(_, o) => self.data.push_load(LoadType::I32, o), + Inst::I64Load(_, o) => self.data.push_load(LoadType::I64, o), + Inst::F32Load(_, o) => self.data.push_load(LoadType::F32, o), + Inst::F64Load(_, o) => self.data.push_load(LoadType::F64, o), + Inst::I32Load8S(_, o) => self.data.push_load(LoadType::I32_I8, o), + Inst::I32Load8U(_, o) => self.data.push_load(LoadType::I32_U8, o), + Inst::I32Load16S(_, o) => self.data.push_load(LoadType::I32_I16, o), + Inst::I32Load16U(_, o) => self.data.push_load(LoadType::I32_U16, o), + Inst::I64Load8S(_, o) => self.data.push_load(LoadType::I64_I8, o), + Inst::I64Load8U(_, o) => self.data.push_load(LoadType::I64_U8, o), + Inst::I64Load16S(_, o) => self.data.push_load(LoadType::I64_I16, o), + Inst::I64Load16U(_, o) => self.data.push_load(LoadType::I64_U16, o), + Inst::I64Load32S(_, o) => self.data.push_load(LoadType::I64_I32, o), + Inst::I64Load32U(_, o) => self.data.push_load(LoadType::I64_U32, o), + Inst::I32Store(_, o) => self.data.gen_store(StoreType::I32, o, &mut stat), + Inst::I64Store(_, o) => self.data.gen_store(StoreType::I64, o, &mut stat), + Inst::F32Store(_, o) => self.data.gen_store(StoreType::F32, o, &mut stat), + Inst::F64Store(_, o) => self.data.gen_store(StoreType::F64, o, &mut stat), + Inst::I32Store8(_, o) => self.data.gen_store(StoreType::I32_N8, o, &mut stat), + Inst::I32Store16(_, o) => self.data.gen_store(StoreType::I32_N16, o, &mut stat), + Inst::I64Store8(_, o) => self.data.gen_store(StoreType::I64_N8, o, &mut stat), + Inst::I64Store16(_, o) => self.data.gen_store(StoreType::I64_N16, o, &mut stat), + Inst::I64Store32(_, o) => self.data.gen_store(StoreType::I64_N32, o, &mut stat), Inst::CurrentMemory(memory) => { let memory = memory.try_into().unwrap(); diff --git a/wasm-ast/src/node.rs b/wasm-ast/src/node.rs index 7ff365a..72b0a02 100644 --- a/wasm-ast/src/node.rs +++ b/wasm-ast/src/node.rs @@ -4,7 +4,7 @@ use parity_wasm::elements::{BrTableData, Instruction, Local, SignExtInstruction} #[allow(non_camel_case_types)] #[derive(Clone, Copy)] -pub enum Load { +pub enum LoadType { I32, I64, F32, @@ -21,7 +21,7 @@ pub enum Load { I64_U32, } -impl Load { +impl LoadType { #[must_use] pub fn as_name(self) -> &'static str { match self { @@ -43,7 +43,7 @@ impl Load { } } -impl TryFrom<&Instruction> for Load { +impl TryFrom<&Instruction> for LoadType { type Error = (); fn try_from(inst: &Instruction) -> Result { @@ -73,7 +73,7 @@ impl TryFrom<&Instruction> for Load { #[allow(non_camel_case_types)] #[derive(Clone, Copy)] -pub enum Store { +pub enum StoreType { I32, I64, F32, @@ -85,7 +85,7 @@ pub enum Store { I64_N32, } -impl Store { +impl StoreType { #[must_use] pub fn as_name(self) -> &'static str { match self { @@ -102,7 +102,7 @@ impl Store { } } -impl TryFrom<&Instruction> for Store { +impl TryFrom<&Instruction> for StoreType { type Error = (); fn try_from(inst: &Instruction) -> Result { @@ -129,7 +129,7 @@ impl TryFrom<&Instruction> for Store { // operation_result_parameter #[allow(non_camel_case_types)] #[derive(Clone, Copy)] -pub enum UnOp { +pub enum UnOpType { Clz_I32, Ctz_I32, Popcnt_I32, @@ -174,7 +174,7 @@ pub enum UnOp { Reinterpret_F64_I64, } -impl UnOp { +impl UnOpType { #[must_use] pub fn as_name(self) -> (&'static str, &'static str) { match self { @@ -224,7 +224,7 @@ impl UnOp { } } -impl TryFrom<&Instruction> for UnOp { +impl TryFrom<&Instruction> for UnOpType { type Error = (); fn try_from(inst: &Instruction) -> Result { @@ -285,7 +285,7 @@ impl TryFrom<&Instruction> for UnOp { #[allow(non_camel_case_types)] #[derive(Clone, Copy)] -pub enum BinOp { +pub enum BinOpType { Add_I32, Sub_I32, Mul_I32, @@ -325,7 +325,7 @@ pub enum BinOp { Copysign_FN, } -impl BinOp { +impl BinOpType { #[must_use] pub fn as_operator(self) -> Option<&'static str> { let op = match self { @@ -384,7 +384,7 @@ impl BinOp { } } -impl TryFrom<&Instruction> for BinOp { +impl TryFrom<&Instruction> for BinOpType { type Error = (); fn try_from(inst: &Instruction) -> Result { @@ -439,7 +439,7 @@ impl TryFrom<&Instruction> for BinOp { #[allow(non_camel_case_types)] #[derive(Clone, Copy)] -pub enum CmpOp { +pub enum CmpOpType { Eq_I32, Ne_I32, LtS_I32, @@ -468,7 +468,7 @@ pub enum CmpOp { Ge_FN, } -impl CmpOp { +impl CmpOpType { #[must_use] pub fn as_operator(self) -> Option<&'static str> { let op = match self { @@ -517,7 +517,7 @@ impl CmpOp { } } -impl TryFrom<&Instruction> for CmpOp { +impl TryFrom<&Instruction> for CmpOpType { type Error = (); fn try_from(inst: &Instruction) -> Result { @@ -578,8 +578,8 @@ pub struct GetGlobal { pub var: usize, } -pub struct AnyLoad { - pub op: Load, +pub struct LoadAt { + pub what: LoadType, pub offset: u32, pub pointer: Box, } @@ -625,19 +625,19 @@ impl From for Value { } } -pub struct AnyUnOp { - pub op: UnOp, +pub struct UnOp { + pub op: UnOpType, pub rhs: Box, } -pub struct AnyBinOp { - pub op: BinOp, +pub struct BinOp { + pub op: BinOpType, pub lhs: Box, pub rhs: Box, } -pub struct AnyCmpOp { - pub op: CmpOp, +pub struct CmpOp { + pub op: CmpOpType, pub lhs: Box, pub rhs: Box, } @@ -647,13 +647,13 @@ pub enum Expression { Select(Select), GetLocal(GetLocal), GetGlobal(GetGlobal), - AnyLoad(AnyLoad), + LoadAt(LoadAt), MemorySize(MemorySize), MemoryGrow(MemoryGrow), Value(Value), - AnyUnOp(AnyUnOp), - AnyBinOp(AnyBinOp), - AnyCmpOp(AnyCmpOp), + UnOp(UnOp), + BinOp(BinOp), + CmpOp(CmpOp), } impl Expression { @@ -738,8 +738,8 @@ pub struct SetGlobal { pub value: Expression, } -pub struct AnyStore { - pub op: Store, +pub struct StoreAt { + pub what: StoreType, pub offset: u32, pub pointer: Expression, pub value: Expression, @@ -759,10 +759,10 @@ pub enum Statement { CallIndirect(CallIndirect), SetLocal(SetLocal), SetGlobal(SetGlobal), - AnyStore(AnyStore), + StoreAt(StoreAt), } -pub struct Function { +pub struct Intermediate { pub local_data: Vec, pub num_param: usize, pub num_stack: usize, diff --git a/wasm-ast/src/visit.rs b/wasm-ast/src/visit.rs index d1ddca2..31a8cbd 100644 --- a/wasm-ast/src/visit.rs +++ b/wasm-ast/src/visit.rs @@ -1,7 +1,7 @@ use crate::node::{ - AnyBinOp, AnyCmpOp, AnyLoad, AnyStore, AnyUnOp, Backward, Br, BrIf, BrTable, Call, - CallIndirect, Else, Expression, Forward, Function, GetGlobal, GetLocal, If, Memorize, - MemoryGrow, MemorySize, Recall, Return, Select, SetGlobal, SetLocal, Statement, Value, + Backward, BinOp, Br, BrIf, BrTable, Call, CallIndirect, CmpOp, Else, Expression, Forward, + GetGlobal, GetLocal, If, Intermediate, LoadAt, Memorize, MemoryGrow, MemorySize, Recall, + Return, Select, SetGlobal, SetLocal, Statement, StoreAt, UnOp, Value, }; pub trait Visitor { @@ -13,7 +13,7 @@ pub trait Visitor { fn visit_get_global(&mut self, _: &GetGlobal) {} - fn visit_any_load(&mut self, _: &AnyLoad) {} + fn visit_load_at(&mut self, _: &LoadAt) {} fn visit_memory_size(&mut self, _: &MemorySize) {} @@ -21,11 +21,11 @@ pub trait Visitor { fn visit_value(&mut self, _: &Value) {} - fn visit_any_unop(&mut self, _: &AnyUnOp) {} + fn visit_un_op(&mut self, _: &UnOp) {} - fn visit_any_binop(&mut self, _: &AnyBinOp) {} + fn visit_bin_op(&mut self, _: &BinOp) {} - fn visit_any_cmpop(&mut self, _: &AnyCmpOp) {} + fn visit_cmp_op(&mut self, _: &CmpOp) {} fn visit_expression(&mut self, _: &Expression) {} @@ -57,7 +57,7 @@ pub trait Visitor { fn visit_set_global(&mut self, _: &SetGlobal) {} - fn visit_any_store(&mut self, _: &AnyStore) {} + fn visit_store_at(&mut self, _: &StoreAt) {} fn visit_statement(&mut self, _: &Statement) {} } @@ -94,11 +94,11 @@ impl Driver for GetGlobal { } } -impl Driver for AnyLoad { +impl Driver for LoadAt { fn accept(&self, visitor: &mut T) { self.pointer.accept(visitor); - visitor.visit_any_load(self); + visitor.visit_load_at(self); } } @@ -122,29 +122,29 @@ impl Driver for Value { } } -impl Driver for AnyUnOp { +impl Driver for UnOp { fn accept(&self, visitor: &mut T) { self.rhs.accept(visitor); - visitor.visit_any_unop(self); + visitor.visit_un_op(self); } } -impl Driver for AnyBinOp { +impl Driver for BinOp { fn accept(&self, visitor: &mut T) { self.lhs.accept(visitor); self.rhs.accept(visitor); - visitor.visit_any_binop(self); + visitor.visit_bin_op(self); } } -impl Driver for AnyCmpOp { +impl Driver for CmpOp { fn accept(&self, visitor: &mut T) { self.lhs.accept(visitor); self.rhs.accept(visitor); - visitor.visit_any_cmpop(self); + visitor.visit_cmp_op(self); } } @@ -155,13 +155,13 @@ impl Driver for Expression { Self::Select(v) => v.accept(visitor), Self::GetLocal(v) => v.accept(visitor), Self::GetGlobal(v) => v.accept(visitor), - Self::AnyLoad(v) => v.accept(visitor), + Self::LoadAt(v) => v.accept(visitor), Self::MemorySize(v) => v.accept(visitor), Self::MemoryGrow(v) => v.accept(visitor), Self::Value(v) => v.accept(visitor), - Self::AnyUnOp(v) => v.accept(visitor), - Self::AnyBinOp(v) => v.accept(visitor), - Self::AnyCmpOp(v) => v.accept(visitor), + Self::UnOp(v) => v.accept(visitor), + Self::BinOp(v) => v.accept(visitor), + Self::CmpOp(v) => v.accept(visitor), } visitor.visit_expression(self); @@ -292,12 +292,12 @@ impl Driver for SetGlobal { } } -impl Driver for AnyStore { +impl Driver for StoreAt { fn accept(&self, visitor: &mut T) { self.pointer.accept(visitor); self.value.accept(visitor); - visitor.visit_any_store(self); + visitor.visit_store_at(self); } } @@ -317,12 +317,12 @@ impl Driver for Statement { Self::CallIndirect(v) => v.accept(visitor), Self::SetLocal(v) => v.accept(visitor), Self::SetGlobal(v) => v.accept(visitor), - Self::AnyStore(v) => v.accept(visitor), + Self::StoreAt(v) => v.accept(visitor), } } } -impl Driver for Function { +impl Driver for Intermediate { fn accept(&self, visitor: &mut T) { self.code.accept(visitor); }