Add comment support (#379)
* Add comment support * Don't print empty comments * Add nop instruction * Add test and note * Add FuncWriter trait * Remove comment support * Add write_preamble to FuncWriter * Fix test * Some changes
This commit is contained in:
@@ -3,18 +3,19 @@
|
||||
use ir;
|
||||
use ir::entities::Inst;
|
||||
use ir::function::Function;
|
||||
use isa::TargetIsa;
|
||||
use isa::{RegInfo, TargetIsa};
|
||||
use result::CodegenError;
|
||||
use std::fmt;
|
||||
use std::fmt::Write;
|
||||
use std::string::{String, ToString};
|
||||
use verifier::VerifierError;
|
||||
use write::decorate_function;
|
||||
use write::{decorate_function, FuncWriter, PlainWriter};
|
||||
|
||||
/// Pretty-print a verifier error.
|
||||
pub fn pretty_verifier_error(
|
||||
pub fn pretty_verifier_error<'a>(
|
||||
func: &ir::Function,
|
||||
isa: Option<&TargetIsa>,
|
||||
func_w: Option<Box<FuncWriter + 'a>>,
|
||||
err: &VerifierError,
|
||||
) -> String {
|
||||
let mut w = String::new();
|
||||
@@ -29,7 +30,7 @@ pub fn pretty_verifier_error(
|
||||
}
|
||||
|
||||
decorate_function(
|
||||
&mut |w, func, isa, inst, indent| pretty_function_error(w, func, isa, inst, indent, err),
|
||||
&mut PrettyVerifierError(func_w.unwrap_or(Box::new(PlainWriter)), err),
|
||||
&mut w,
|
||||
func,
|
||||
isa,
|
||||
@@ -37,6 +38,30 @@ pub fn pretty_verifier_error(
|
||||
w
|
||||
}
|
||||
|
||||
struct PrettyVerifierError<'a>(Box<FuncWriter + 'a>, &'a VerifierError);
|
||||
|
||||
impl<'a> FuncWriter for PrettyVerifierError<'a> {
|
||||
fn write_instruction(
|
||||
&mut self,
|
||||
w: &mut Write,
|
||||
func: &Function,
|
||||
isa: Option<&TargetIsa>,
|
||||
inst: Inst,
|
||||
indent: usize,
|
||||
) -> fmt::Result {
|
||||
pretty_function_error(w, func, isa, inst, indent, &mut *self.0, self.1)
|
||||
}
|
||||
|
||||
fn write_preamble(
|
||||
&mut self,
|
||||
w: &mut Write,
|
||||
func: &Function,
|
||||
regs: Option<&RegInfo>,
|
||||
) -> Result<bool, fmt::Error> {
|
||||
self.0.write_preamble(w, func, regs)
|
||||
}
|
||||
}
|
||||
|
||||
/// Pretty-print a function verifier error.
|
||||
fn pretty_function_error(
|
||||
w: &mut Write,
|
||||
@@ -44,17 +69,12 @@ fn pretty_function_error(
|
||||
isa: Option<&TargetIsa>,
|
||||
cur_inst: Inst,
|
||||
indent: usize,
|
||||
func_w: &mut FuncWriter,
|
||||
err: &VerifierError,
|
||||
) -> fmt::Result {
|
||||
match err.location {
|
||||
ir::entities::AnyEntity::Inst(inst) if inst == cur_inst => {
|
||||
writeln!(
|
||||
w,
|
||||
"{1:0$}{2}",
|
||||
indent,
|
||||
"",
|
||||
func.dfg.display_inst(cur_inst, isa)
|
||||
)?;
|
||||
func_w.write_instruction(w, func, isa, cur_inst, indent)?;
|
||||
write!(w, "{1:0$}^", indent, "")?;
|
||||
for _c in cur_inst.to_string().chars() {
|
||||
write!(w, "~")?;
|
||||
@@ -74,7 +94,7 @@ fn pretty_function_error(
|
||||
/// Pretty-print a Cranelift error.
|
||||
pub fn pretty_error(func: &ir::Function, isa: Option<&TargetIsa>, err: CodegenError) -> String {
|
||||
if let CodegenError::Verifier(e) = err {
|
||||
pretty_verifier_error(func, isa, &e)
|
||||
pretty_verifier_error(func, isa, None, &e)
|
||||
} else {
|
||||
err.to_string()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user