rust/src/pretty_clif.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

2018-07-20 13:51:34 +02:00
use std::borrow::Cow;
2018-07-14 11:59:42 +02:00
use std::collections::HashMap;
use std::fmt;
2018-09-27 19:19:16 +02:00
use cranelift::codegen::entity::SecondaryMap;
2018-07-20 13:51:34 +02:00
use cranelift::codegen::write::{FuncWriter, PlainWriter};
use crate::prelude::*;
2018-07-14 11:59:42 +02:00
pub struct CommentWriter(pub HashMap<Inst, String>);
impl FuncWriter for CommentWriter {
2018-11-15 10:55:40 +01:00
fn write_preamble(
&mut self,
w: &mut dyn fmt::Write,
func: &Function,
reg_info: Option<&isa::RegInfo>,
) -> Result<bool, fmt::Error> {
PlainWriter.write_preamble(w, func, reg_info)
}
fn write_ebb_header(
&mut self,
w: &mut dyn fmt::Write,
func: &Function,
isa: Option<&dyn isa::TargetIsa>,
ebb: Ebb,
indent: usize,
) -> fmt::Result {
PlainWriter.write_ebb_header(w, func, isa, ebb, indent)
}
2018-07-14 11:59:42 +02:00
fn write_instruction(
&mut self,
w: &mut dyn fmt::Write,
func: &Function,
2018-09-27 19:19:16 +02:00
aliases: &SecondaryMap<Value, Vec<Value>>,
2018-07-14 11:59:42 +02:00
isa: Option<&dyn isa::TargetIsa>,
inst: Inst,
indent: usize,
) -> fmt::Result {
2018-09-05 19:43:42 +02:00
PlainWriter.write_instruction(w, func, aliases, isa, inst, indent)?;
2018-07-14 12:21:45 +02:00
if let Some(comment) = self.0.get(&inst) {
writeln!(w, "; {}", comment.replace('\n', "\n; "))?;
}
2018-07-20 13:51:34 +02:00
Ok(())
2018-07-14 11:59:42 +02:00
}
}
2018-07-20 13:51:34 +02:00
2018-08-14 20:31:16 +02:00
impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
2018-08-15 16:17:59 +02:00
pub fn add_global_comment<'s, S: Into<Cow<'s, str>>>(&mut self, comment: S) {
self.add_comment(self.top_nop.expect("fx.top_nop not yet set"), comment)
}
2018-07-20 13:51:34 +02:00
pub fn add_comment<'s, S: Into<Cow<'s, str>>>(&mut self, inst: Inst, comment: S) {
use std::collections::hash_map::Entry;
match self.comments.entry(inst) {
Entry::Occupied(mut occ) => {
occ.get_mut().push('\n');
occ.get_mut().push_str(comment.into().as_ref());
}
Entry::Vacant(vac) => {
vac.insert(comment.into().into_owned());
}
}
}
}