From df198206933247acea4cd84900d3215eb5bb0235 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Mon, 13 Feb 2023 18:10:34 -0800 Subject: [PATCH] Show srclocs --- src/backend/mod.rs | 2 +- src/frontend.rs | 2 +- src/ir/display.rs | 21 +++++++++++++++++---- src/ir/func.rs | 18 +++++++++++++----- src/passes/resolve_aliases.rs | 2 +- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 79db37b..97cbec0 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -31,7 +31,7 @@ macro_rules! op { impl<'a> WasmFuncBackend<'a> { pub fn new(body: &'a FunctionBody) -> Result> { 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); diff --git a/src/frontend.rs b/src/frontend.rs index d31c49f..b58d73b 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -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 { diff --git a/src/ir/display.rs b/src/ir/display.rs index ef85f0d..d0cb7ec 100644 --- a/src/ir/display.rs +++ b/src/ir/display.rs @@ -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::>(); let tys = tys.iter().map(|&ty| format!("{}", ty)).collect::>(); + 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!( diff --git a/src/ir/func.rs b/src/ir/func.rs index c1537d8..a790a2b 100644 --- a/src/ir/func.rs +++ b/src/ir/func.rs @@ -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 ); } diff --git a/src/passes/resolve_aliases.rs b/src/passes/resolve_aliases.rs index 370a7c5..5aa64c3 100644 --- a/src/passes/resolve_aliases.rs +++ b/src/passes/resolve_aliases.rs @@ -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]);