2018-07-20 11:51:34 +00:00
|
|
|
use std::borrow::Cow;
|
2018-07-14 09:59:42 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fmt;
|
|
|
|
|
2018-07-20 11:51:34 +00:00
|
|
|
use cranelift::codegen::write::{FuncWriter, PlainWriter};
|
|
|
|
|
|
|
|
use prelude::*;
|
2018-07-14 09:59:42 +00:00
|
|
|
|
|
|
|
pub struct CommentWriter(pub HashMap<Inst, String>);
|
|
|
|
|
|
|
|
impl FuncWriter for CommentWriter {
|
|
|
|
fn write_instruction(
|
|
|
|
&mut self,
|
|
|
|
w: &mut dyn fmt::Write,
|
|
|
|
func: &Function,
|
|
|
|
isa: Option<&dyn isa::TargetIsa>,
|
|
|
|
inst: Inst,
|
|
|
|
indent: usize,
|
|
|
|
) -> fmt::Result {
|
2018-07-20 11:51:34 +00:00
|
|
|
PlainWriter.write_instruction(w, func, isa, inst, indent)?;
|
2018-07-14 10:21:45 +00:00
|
|
|
if let Some(comment) = self.0.get(&inst) {
|
|
|
|
writeln!(w, "; {}", comment.replace('\n', "\n; "))?;
|
|
|
|
}
|
2018-07-20 11:51:34 +00:00
|
|
|
Ok(())
|
2018-07-14 09:59:42 +00: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)
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 11:51:34 +00:00
|
|
|
|
|
|
|
impl<'a, 'tcx: 'a> FunctionCx<'a, 'tcx> {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|