2015-12-18 19:29:03 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use dot;
|
|
|
|
use rustc::mir::repr::*;
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty;
|
2015-12-18 21:52:55 -06:00
|
|
|
use std::fmt::Debug;
|
2015-12-18 19:29:03 -06:00
|
|
|
use std::io::{self, Write};
|
2016-03-12 19:07:00 +02:00
|
|
|
use syntax::ast::NodeId;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2016-03-12 19:07:00 +02:00
|
|
|
/// Write a graphviz DOT graph of a list of MIRs.
|
|
|
|
pub fn write_mir_graphviz<'a, 't, W, I>(tcx: &ty::TyCtxt<'t>, iter: I, w: &mut W) -> io::Result<()>
|
|
|
|
where W: Write, I: Iterator<Item=(&'a NodeId, &'a Mir<'a>)> {
|
|
|
|
for (&nodeid, mir) in iter {
|
2016-03-22 22:01:37 -05:00
|
|
|
writeln!(w, "digraph Mir_{} {{", nodeid)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2016-03-12 19:07:00 +02:00
|
|
|
// Global graph properties
|
2016-03-22 22:01:37 -05:00
|
|
|
writeln!(w, r#" graph [fontname="monospace"];"#)?;
|
|
|
|
writeln!(w, r#" node [fontname="monospace"];"#)?;
|
|
|
|
writeln!(w, r#" edge [fontname="monospace"];"#)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2016-03-12 19:07:00 +02:00
|
|
|
// Graph label
|
2016-03-22 22:01:37 -05:00
|
|
|
write_graph_label(tcx, nodeid, mir, w)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2016-03-12 19:07:00 +02:00
|
|
|
// Nodes
|
|
|
|
for block in mir.all_basic_blocks() {
|
2016-03-22 22:01:37 -05:00
|
|
|
write_node(block, mir, w)?;
|
2016-03-12 19:07:00 +02:00
|
|
|
}
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2016-03-12 19:07:00 +02:00
|
|
|
// Edges
|
|
|
|
for source in mir.all_basic_blocks() {
|
2016-03-22 22:01:37 -05:00
|
|
|
write_edges(source, mir, w)?;
|
2016-03-12 19:07:00 +02:00
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
writeln!(w, "}}")?
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
2016-03-12 19:07:00 +02:00
|
|
|
Ok(())
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
|
|
|
|
2016-01-25 14:34:34 +01:00
|
|
|
/// Write a graphviz HTML-styled label for the given basic block, with
|
|
|
|
/// all necessary escaping already performed. (This is suitable for
|
|
|
|
/// emitting directly, as is done in this module, or for use with the
|
|
|
|
/// LabelText::HtmlStr from libgraphviz.)
|
|
|
|
///
|
|
|
|
/// `init` and `fini` are callbacks for emitting additional rows of
|
|
|
|
/// data (using HTML enclosed with `<tr>` in the emitted text).
|
|
|
|
pub fn write_node_label<W: Write, INIT, FINI>(block: BasicBlock,
|
|
|
|
mir: &Mir,
|
|
|
|
w: &mut W,
|
|
|
|
num_cols: u32,
|
|
|
|
init: INIT,
|
|
|
|
fini: FINI) -> io::Result<()>
|
|
|
|
where INIT: Fn(&mut W) -> io::Result<()>,
|
|
|
|
FINI: Fn(&mut W) -> io::Result<()>
|
|
|
|
{
|
2015-12-18 19:29:03 -06:00
|
|
|
let data = mir.basic_block_data(block);
|
|
|
|
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, r#"<table border="0" cellborder="1" cellspacing="0">"#)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
// Basic block number at the top.
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, r#"<tr><td {attrs} colspan="{colspan}">{blk}</td></tr>"#,
|
2016-03-22 17:58:45 -05:00
|
|
|
attrs=r#"bgcolor="gray" align="center""#,
|
|
|
|
colspan=num_cols,
|
|
|
|
blk=block.index())?;
|
2016-01-25 14:34:34 +01:00
|
|
|
|
2016-03-22 22:01:37 -05:00
|
|
|
init(w)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
// List of statements in the middle.
|
2015-12-18 19:29:03 -06:00
|
|
|
if !data.statements.is_empty() {
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, r#"<tr><td align="left" balign="left">"#)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
for statement in &data.statements {
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, "{}<br/>", escape(statement))?;
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, "</td></tr>")?;
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
// Terminator head at the bottom, not including the list of successor blocks. Those will be
|
|
|
|
// displayed as labels on the edges between blocks.
|
2015-12-18 19:29:03 -06:00
|
|
|
let mut terminator_head = String::new();
|
2016-03-10 09:55:15 -05:00
|
|
|
data.terminator().kind.fmt_head(&mut terminator_head).unwrap();
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, r#"<tr><td align="left">{}</td></tr>"#, dot::escape_html(&terminator_head))?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2016-03-22 22:01:37 -05:00
|
|
|
fini(w)?;
|
2016-01-25 14:34:34 +01:00
|
|
|
|
|
|
|
// Close the table
|
|
|
|
writeln!(w, "</table>")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write a graphviz DOT node for the given basic block.
|
|
|
|
fn write_node<W: Write>(block: BasicBlock, mir: &Mir, w: &mut W) -> io::Result<()> {
|
|
|
|
// Start a new node with the label to follow, in one of DOT's pseudo-HTML tables.
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, r#" {} [shape="none", label=<"#, node(block))?;
|
|
|
|
write_node_label(block, mir, w, 1, |_| Ok(()), |_| Ok(()))?;
|
2016-01-25 14:34:34 +01:00
|
|
|
// Close the node label and the node itself.
|
|
|
|
writeln!(w, ">];")
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
/// Write graphviz DOT edges with labels between the given basic block and all of its successors.
|
2015-12-18 19:29:03 -06:00
|
|
|
fn write_edges<W: Write>(source: BasicBlock, mir: &Mir, w: &mut W) -> io::Result<()> {
|
2015-12-19 00:44:32 +02:00
|
|
|
let terminator = &mir.basic_block_data(source).terminator();
|
2016-03-10 09:55:15 -05:00
|
|
|
let labels = terminator.kind.fmt_successor_labels();
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
for (&target, label) in terminator.successors().iter().zip(labels) {
|
2016-03-22 22:01:37 -05:00
|
|
|
writeln!(w, r#" {} -> {} [label="{}"];"#, node(source), node(target), label)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
/// Write the graphviz DOT label for the overall graph. This is essentially a block of text that
|
|
|
|
/// will appear below the graph, showing the type of the `fn` this MIR represents and the types of
|
|
|
|
/// all the variables and temporaries.
|
2016-03-12 19:07:00 +02:00
|
|
|
fn write_graph_label<W: Write>(tcx: &ty::TyCtxt, nid: NodeId, mir: &Mir, w: &mut W)
|
|
|
|
-> io::Result<()> {
|
2016-04-06 13:51:55 +03:00
|
|
|
write!(w, " label=<fn {}(", dot::escape_html(&tcx.node_path_str(nid)))?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
// fn argument types.
|
2015-12-18 19:29:03 -06:00
|
|
|
for (i, arg) in mir.arg_decls.iter().enumerate() {
|
|
|
|
if i > 0 {
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, ", ")?;
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, "{:?}: {}", Lvalue::Arg(i as u32), escape(&arg.ty))?;
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
|
|
|
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, ") -> ")?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
// fn return type.
|
2015-12-18 19:29:03 -06:00
|
|
|
match mir.return_ty {
|
2016-03-22 22:01:37 -05:00
|
|
|
ty::FnOutput::FnConverging(ty) => write!(w, "{}", escape(ty))?,
|
|
|
|
ty::FnOutput::FnDiverging => write!(w, "!")?,
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
|
|
|
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, r#"<br align="left"/>"#)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
// User variable types (including the user's name in a comment).
|
2015-12-18 19:29:03 -06:00
|
|
|
for (i, var) in mir.var_decls.iter().enumerate() {
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, "let ")?;
|
2015-12-18 19:29:03 -06:00
|
|
|
if var.mutability == Mutability::Mut {
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, "mut ")?;
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, r#"{:?}: {}; // {}<br align="left"/>"#,
|
2016-03-22 17:58:45 -05:00
|
|
|
Lvalue::Var(i as u32), escape(&var.ty), var.name)?;
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
|
|
|
|
2015-12-18 21:52:55 -06:00
|
|
|
// Compiler-introduced temporary types.
|
2015-12-18 19:29:03 -06:00
|
|
|
for (i, temp) in mir.temp_decls.iter().enumerate() {
|
2016-03-22 22:01:37 -05:00
|
|
|
write!(w, r#"let mut {:?}: {};<br align="left"/>"#,
|
2016-03-22 17:58:45 -05:00
|
|
|
Lvalue::Temp(i as u32), escape(&temp.ty))?;
|
2015-12-18 19:29:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
writeln!(w, ">;")
|
|
|
|
}
|
2015-12-18 21:52:55 -06:00
|
|
|
|
|
|
|
fn node(block: BasicBlock) -> String {
|
|
|
|
format!("bb{}", block.index())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn escape<T: Debug>(t: &T) -> String {
|
|
|
|
dot::escape_html(&format!("{:?}", t))
|
|
|
|
}
|