Factor out operator analysis and simplify output

This commit is contained in:
Rerumu
2022-04-29 23:30:55 -04:00
parent 2e5890f466
commit 7913dd507a
14 changed files with 121 additions and 92 deletions
+14 -5
View File
@@ -5,9 +5,11 @@ use wasm_ast::node::{
UnOp, Value,
};
use crate::analyzer::operator::bin_symbol_of;
use super::manager::{
write_bin_call, write_cmp_op, write_condition, write_f32, write_f64, write_separated,
write_variable, Driver, Manager,
write_cmp_op, write_condition, write_f32, write_f64, write_separated, write_variable, Driver,
Manager,
};
impl Driver for Recall {
@@ -73,6 +75,7 @@ impl Driver for Value {
}
}
// TODO: Implement context dependent infix comparisons
impl Driver for UnOp {
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
let (a, b) = self.op.as_name();
@@ -85,14 +88,20 @@ impl Driver for UnOp {
impl Driver for BinOp {
fn write(&self, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
if let Some(op) = self.op.as_operator() {
if let Some(symbol) = bin_symbol_of(self.op) {
write!(w, "(")?;
self.lhs.write(mng, w)?;
write!(w, "{op} ")?;
write!(w, "{symbol} ")?;
self.rhs.write(mng, w)?;
write!(w, ")")
} else {
write_bin_call(self.op.as_name(), &self.lhs, &self.rhs, mng, w)
let (head, tail) = self.op.as_name();
write!(w, "{head}_{tail}(")?;
self.lhs.write(mng, w)?;
write!(w, ", ")?;
self.rhs.write(mng, w)?;
write!(w, ")")
}
}
}
+11 -17
View File
@@ -5,6 +5,8 @@ use std::{
use wasm_ast::node::{CmpOp, Expression};
use crate::analyzer::operator::cmp_symbol_of;
#[derive(Default)]
pub struct Manager {
label_list: Vec<usize>,
@@ -85,27 +87,19 @@ pub fn write_variable(var: usize, mng: &Manager, w: &mut dyn Write) -> Result<()
}
}
pub fn write_bin_call(
name: (&str, &str),
lhs: &Expression,
rhs: &Expression,
mng: &mut Manager,
w: &mut dyn Write,
) -> Result<()> {
write!(w, "{}_{}(", name.0, name.1)?;
lhs.write(mng, w)?;
write!(w, ", ")?;
rhs.write(mng, w)?;
write!(w, ")")
}
pub fn write_cmp_op(cmp: &CmpOp, mng: &mut Manager, w: &mut dyn Write) -> Result<()> {
if let Some(op) = cmp.op.as_operator() {
if let Some(symbol) = cmp_symbol_of(cmp.op) {
cmp.lhs.write(mng, w)?;
write!(w, "{op} ")?;
write!(w, "{symbol} ")?;
cmp.rhs.write(mng, w)
} else {
write_bin_call(cmp.op.as_name(), &cmp.lhs, &cmp.rhs, mng, w)
let (head, tail) = cmp.op.as_name();
write!(w, "{head}_{tail}(")?;
cmp.lhs.write(mng, w)?;
write!(w, ", ")?;
cmp.rhs.write(mng, w)?;
write!(w, ")")
}
}
+9 -2
View File
@@ -3,6 +3,7 @@ use std::{
ops::Range,
};
use parity_wasm::elements::ValueType;
use wasm_ast::node::{
Backward, Br, BrIf, BrTable, Call, CallIndirect, Else, Forward, If, Intermediate, Memorize,
Return, SetGlobal, SetLocal, Statement, StoreAt,
@@ -247,14 +248,20 @@ fn write_variable_list(ir: &Intermediate, w: &mut dyn Write) -> Result<()> {
for data in &ir.local_data {
let range = total..total + usize::try_from(data.count()).unwrap();
let typed = data.value_type();
let typed = if data.value_type() == ValueType::I64 {
"0LL"
} else {
"0"
}
.as_bytes();
total = range.end;
write!(w, "local ")?;
write_ascending("loc", range.clone(), w)?;
write!(w, " = ")?;
write_separated(range, |_, w| write!(w, "ZERO_{typed} "), w)?;
write_separated(range, |_, w| w.write_all(typed), w)?;
write!(w, " ")?;
}
if ir.num_stack != 0 {