Show srclocs

This commit is contained in:
Chris Fallin 2023-02-13 18:10:34 -08:00
parent cfc758900d
commit df19820693
5 changed files with 33 additions and 12 deletions

View file

@ -31,7 +31,7 @@ macro_rules! op {
impl<'a> WasmFuncBackend<'a> {
pub fn new(body: &'a FunctionBody) -> Result<WasmFuncBackend<'a>> {
body.validate()?;
log::debug!("Backend compiling:\n{}\n", body.display_verbose("| "));
log::debug!("Backend compiling:\n{}\n", body.display_verbose("| ", None));
let cfg = CFGInfo::new(body);
log::debug!("CFG:\n{:?}\n", cfg);
let trees = Trees::compute(body);

View file

@ -373,7 +373,7 @@ impl<'a> DebugLocReader<'a> {
}
fn get_loc(&mut self, offset: usize) -> SourceLoc {
let offset = u32::try_from(offset).unwrap();
let offset = u32::try_from(offset).unwrap() + self.code_offset;
while self.locs.len() > 0 {
let (start, len, loc) = self.locs[0];
if offset < start {

View file

@ -1,6 +1,7 @@
//! Displaying IR.
use super::{FuncDecl, FunctionBody, Module, ValueDef};
use super::{FuncDecl, FunctionBody, Module, SourceLoc, ValueDef};
use crate::entity::EntityRef;
use std::collections::HashMap;
use std::fmt::{Display, Formatter, Result as FmtResult};
@ -8,6 +9,7 @@ pub struct FunctionBodyDisplay<'a>(
pub(crate) &'a FunctionBody,
pub(crate) &'a str,
pub(crate) bool,
pub(crate) Option<&'a Module<'a>>,
);
impl<'a> Display for FunctionBodyDisplay<'a> {
@ -121,15 +123,26 @@ impl<'a> Display for FunctionBodyDisplay<'a> {
ValueDef::Operator(op, args, tys) => {
let args = args.iter().map(|&v| format!("{}", v)).collect::<Vec<_>>();
let tys = tys.iter().map(|&ty| format!("{}", ty)).collect::<Vec<_>>();
let loc = if self.0.source_locs[inst] != SourceLoc::invalid()
&& self.3.is_some()
{
let module = self.3.as_ref().unwrap();
let loc = self.0.source_locs[inst];
let data = &module.debug.source_locs[loc];
let filename = &module.debug.source_files[data.file];
format!("@{} {}:{}:{}", loc, filename, data.line, data.col)
} else {
"".to_owned()
};
writeln!(
f,
"{} {} = {} {} # {} @{}",
"{} {} = {} {} # {} {}",
self.1,
inst,
op,
args.join(", "),
tys.join(", "),
self.0.source_locs[inst],
loc,
)?;
}
ValueDef::PickOutput(val, idx, ty) => {
@ -226,7 +239,7 @@ impl<'a> Display for ModuleDisplay<'a> {
sig,
sig_strs.get(&sig).unwrap()
)?;
writeln!(f, "{}", body.display(" "))?;
writeln!(f, "{}", body.display(" ", Some(self.0)))?;
}
FuncDecl::Lazy(sig, name, reader) => {
writeln!(

View file

@ -265,12 +265,20 @@ impl FunctionBody {
self.locals.push(ty)
}
pub fn display<'a>(&'a self, indent: &'a str) -> FunctionBodyDisplay<'a> {
FunctionBodyDisplay(self, indent, /* verbose = */ false)
pub fn display<'a>(
&'a self,
indent: &'a str,
module: Option<&'a Module>,
) -> FunctionBodyDisplay<'a> {
FunctionBodyDisplay(self, indent, /* verbose = */ false, module)
}
pub fn display_verbose<'a>(&'a self, indent: &'a str) -> FunctionBodyDisplay<'a> {
FunctionBodyDisplay(self, indent, /* verbose = */ true)
pub fn display_verbose<'a>(
&'a self,
indent: &'a str,
module: Option<&'a Module>,
) -> FunctionBodyDisplay<'a> {
FunctionBodyDisplay(self, indent, /* verbose = */ true, module)
}
pub fn validate(&self) -> anyhow::Result<()> {
@ -358,7 +366,7 @@ impl FunctionBody {
if bad.len() > 0 {
anyhow::bail!(
"Body is:\n{}\nError(s) in SSA: {:?}",
self.display_verbose(" | "),
self.display_verbose(" | ", None),
bad
);
}

View file

@ -5,7 +5,7 @@ use crate::{FunctionBody, ValueDef};
pub fn run(body: &mut FunctionBody) {
log::debug!(
"Resolve aliases: running on:\n{}\n",
body.display_verbose("| ")
body.display_verbose("| ", None),
);
for value in body.values.iter() {
let mut value_def = std::mem::take(&mut body.values[value]);