From a73dacd5dfdfca67bde4614843120a71769426a4 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Wed, 2 Nov 2022 19:46:15 -0700 Subject: [PATCH] WIP. --- src/frontend.rs | 10 +++++++--- src/ir/display.rs | 35 ++++++++++++++++++++++++++++++++--- src/ir/func.rs | 10 ++++++++-- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/frontend.rs b/src/frontend.rs index 3b29716..d5c2890 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -131,9 +131,7 @@ fn parse_body<'a>( for (arg_idx, &arg_ty) in module.signature(my_sig).params.iter().enumerate() { let local_idx = Local::new(arg_idx); builder.body.add_blockparam(entry, arg_ty); - let value = builder - .body - .add_value(ValueDef::BlockParam(entry, arg_idx, arg_ty)); + let value = builder.body.blocks[entry].params.last().unwrap().1; trace!("defining local {} to value {}", local_idx, value); builder.locals.declare(local_idx, arg_ty); builder.locals.set(local_idx, value); @@ -321,6 +319,12 @@ impl LocalTracker { _ => todo!("unsupported type: {:?}", ty), }; body.blocks[at_block].insts.push(val); + log::trace!( + "created default value {} of type {} at block {}", + val, + ty, + at_block + ); val } diff --git a/src/ir/display.rs b/src/ir/display.rs index 73f9701..049aeeb 100644 --- a/src/ir/display.rs +++ b/src/ir/display.rs @@ -4,7 +4,11 @@ use super::{FuncDecl, FunctionBody, Module, ValueDef}; use std::collections::HashMap; use std::fmt::{Display, Formatter, Result as FmtResult}; -pub struct FunctionBodyDisplay<'a>(pub(crate) &'a FunctionBody, pub(crate) &'a str); +pub struct FunctionBodyDisplay<'a>( + pub(crate) &'a FunctionBody, + pub(crate) &'a str, + pub(crate) bool, +); impl<'a> Display for FunctionBodyDisplay<'a> { fn fmt(&self, f: &mut Formatter) -> FmtResult { @@ -29,10 +33,34 @@ impl<'a> Display for FunctionBodyDisplay<'a> { ret_tys.join(", ") )?; + let verbose = self.2; for (value, value_def) in self.0.values.entries() { match value_def { - ValueDef::Operator(..) | ValueDef::BlockParam(..) => {} - ValueDef::Alias(_alias_target) => {} + ValueDef::Operator(op, args, tys) if verbose => writeln!( + f, + "{} {} = {} {} # {}", + self.1, + value, + op, + args.iter() + .map(|arg| format!("{}", arg)) + .collect::>() + .join(", "), + tys.iter() + .map(|arg| format!("{}", arg)) + .collect::>() + .join(", ") + )?, + ValueDef::BlockParam(block, idx, ty) if verbose => writeln!( + f, + "{} {} = blockparam {}, {} # {}", + self.1, value, block, idx, ty + )?, + ValueDef::Alias(alias_target) => { + if verbose { + writeln!(f, "{} {} = {}", self.1, value, alias_target)? + } + } ValueDef::PickOutput(val, idx, ty) => { writeln!(f, "{} {} = {}.{} # {}", self.1, value, val, idx, ty)? } @@ -40,6 +68,7 @@ impl<'a> Display for FunctionBodyDisplay<'a> { writeln!(f, "{} {} = placeholder # {}", self.1, value, ty)? } ValueDef::None => panic!(), + _ => {} } } diff --git a/src/ir/func.rs b/src/ir/func.rs index 1708e42..0238f20 100644 --- a/src/ir/func.rs +++ b/src/ir/func.rs @@ -66,7 +66,9 @@ impl FunctionBody { pub fn add_value(&mut self, value: ValueDef) -> Value { log::trace!("add_value: def {:?}", value); - self.values.push(value) + let value = self.values.push(value); + log::trace!(" -> {}", value); + value } pub fn set_alias(&mut self, value: Value, to: Value) { @@ -140,7 +142,11 @@ impl FunctionBody { } pub fn display<'a>(&'a self, indent: &'a str) -> FunctionBodyDisplay<'a> { - FunctionBodyDisplay(self, indent) + FunctionBodyDisplay(self, indent, /* verbose = */ false) + } + + pub fn display_verbose<'a>(&'a self, indent: &'a str) -> FunctionBodyDisplay<'a> { + FunctionBodyDisplay(self, indent, /* verbose = */ true) } }