This commit is contained in:
Chris Fallin 2022-11-02 19:46:15 -07:00
parent 3dc1356669
commit a73dacd5df
3 changed files with 47 additions and 8 deletions

View file

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

View file

@ -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::<Vec<_>>()
.join(", "),
tys.iter()
.map(|arg| format!("{}", arg))
.collect::<Vec<_>>()
.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!(),
_ => {}
}
}

View file

@ -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)
}
}