1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use rustc::mir::repr::*;
13 use rustc::middle::ty;
15 use std::io::{self, Write};
17 /// Write a graphviz DOT graph for the given MIR.
18 pub fn write_mir_graphviz<W: Write>(mir: &Mir, w: &mut W) -> io::Result<()> {
19 try!(writeln!(w, "digraph Mir {{"));
21 // Global graph properties
22 try!(writeln!(w, r#" graph [fontname="monospace"];"#));
23 try!(writeln!(w, r#" node [fontname="monospace"];"#));
24 try!(writeln!(w, r#" edge [fontname="monospace"];"#));
27 try!(write_graph_label(mir, w));
30 for block in mir.all_basic_blocks() {
31 try!(write_node(block, mir, w));
35 for source in mir.all_basic_blocks() {
36 try!(write_edges(source, mir, w));
42 /// Write a graphviz DOT node for the given basic block.
43 fn write_node<W: Write>(block: BasicBlock, mir: &Mir, w: &mut W) -> io::Result<()> {
44 let data = mir.basic_block_data(block);
46 // Start a new node with the label to follow, in one of DOT's pseudo-HTML tables.
47 try!(write!(w, r#" {} [shape="none", label=<"#, node(block)));
48 try!(write!(w, r#"<table border="0" cellborder="1" cellspacing="0">"#));
50 // Basic block number at the top.
51 try!(write!(w, r#"<tr><td bgcolor="gray" align="center">{}</td></tr>"#, block.index()));
53 // List of statements in the middle.
54 if !data.statements.is_empty() {
55 try!(write!(w, r#"<tr><td align="left" balign="left">"#));
56 for statement in &data.statements {
57 try!(write!(w, "{}<br/>", escape(statement)));
59 try!(write!(w, "</td></tr>"));
62 // Terminator head at the bottom, not including the list of successor blocks. Those will be
63 // displayed as labels on the edges between blocks.
64 let mut terminator_head = String::new();
65 data.terminator().fmt_head(&mut terminator_head).unwrap();
66 try!(write!(w, r#"<tr><td align="left">{}</td></tr>"#, dot::escape_html(&terminator_head)));
68 // Close the table, node label, and the node itself.
69 writeln!(w, "</table>>];")
72 /// Write graphviz DOT edges with labels between the given basic block and all of its successors.
73 fn write_edges<W: Write>(source: BasicBlock, mir: &Mir, w: &mut W) -> io::Result<()> {
74 let terminator = &mir.basic_block_data(source).terminator();
75 let labels = terminator.fmt_successor_labels();
77 for (&target, label) in terminator.successors().iter().zip(labels) {
78 try!(writeln!(w, r#" {} -> {} [label="{}"];"#, node(source), node(target), label));
84 /// Write the graphviz DOT label for the overall graph. This is essentially a block of text that
85 /// will appear below the graph, showing the type of the `fn` this MIR represents and the types of
86 /// all the variables and temporaries.
87 fn write_graph_label<W: Write>(mir: &Mir, w: &mut W) -> io::Result<()> {
88 try!(write!(w, " label=<fn("));
91 for (i, arg) in mir.arg_decls.iter().enumerate() {
93 try!(write!(w, ", "));
95 try!(write!(w, "{:?}: {}", Lvalue::Arg(i as u32), escape(&arg.ty)));
98 try!(write!(w, ") -> "));
101 match mir.return_ty {
102 ty::FnOutput::FnConverging(ty) => try!(write!(w, "{}", escape(ty))),
103 ty::FnOutput::FnDiverging => try!(write!(w, "!")),
106 try!(write!(w, r#"<br align="left"/>"#));
108 // User variable types (including the user's name in a comment).
109 for (i, var) in mir.var_decls.iter().enumerate() {
110 try!(write!(w, "let "));
111 if var.mutability == Mutability::Mut {
112 try!(write!(w, "mut "));
114 try!(write!(w, r#"{:?}: {}; // {}<br align="left"/>"#,
115 Lvalue::Var(i as u32), escape(&var.ty), var.name));
118 // Compiler-introduced temporary types.
119 for (i, temp) in mir.temp_decls.iter().enumerate() {
120 try!(write!(w, r#"let mut {:?}: {};<br align="left"/>"#,
121 Lvalue::Temp(i as u32), escape(&temp.ty)));
127 fn node(block: BasicBlock) -> String {
128 format!("bb{}", block.index())
131 fn escape<T: Debug>(t: &T) -> String {
132 dot::escape_html(&format!("{:?}", t))