]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/graphviz.rs
Rollup merge of #31295 - steveklabnik:gh31266, r=alexcrichton
[rust.git] / src / librustc_mir / graphviz.rs
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.
4 //
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.
10
11 use dot;
12 use rustc::mir::repr::*;
13 use rustc::middle::ty;
14 use std::fmt::Debug;
15 use std::io::{self, Write};
16
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 {{"));
20
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"];"#));
25
26     // Graph label
27     try!(write_graph_label(mir, w));
28
29     // Nodes
30     for block in mir.all_basic_blocks() {
31         try!(write_node(block, mir, w));
32     }
33
34     // Edges
35     for source in mir.all_basic_blocks() {
36         try!(write_edges(source, mir, w));
37     }
38
39     writeln!(w, "}}")
40 }
41
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);
45
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">"#));
49
50     // Basic block number at the top.
51     try!(write!(w, r#"<tr><td bgcolor="gray" align="center">{}</td></tr>"#, block.index()));
52
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)));
58         }
59         try!(write!(w, "</td></tr>"));
60     }
61
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)));
67
68     // Close the table, node label, and the node itself.
69     writeln!(w, "</table>>];")
70 }
71
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();
76
77     for (&target, label) in terminator.successors().iter().zip(labels) {
78         try!(writeln!(w, r#"    {} -> {} [label="{}"];"#, node(source), node(target), label));
79     }
80
81     Ok(())
82 }
83
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("));
89
90     // fn argument types.
91     for (i, arg) in mir.arg_decls.iter().enumerate() {
92         if i > 0 {
93             try!(write!(w, ", "));
94         }
95         try!(write!(w, "{:?}: {}", Lvalue::Arg(i as u32), escape(&arg.ty)));
96     }
97
98     try!(write!(w, ") -&gt; "));
99
100     // fn return type.
101     match mir.return_ty {
102         ty::FnOutput::FnConverging(ty) => try!(write!(w, "{}", escape(ty))),
103         ty::FnOutput::FnDiverging => try!(write!(w, "!")),
104     }
105
106     try!(write!(w, r#"<br align="left"/>"#));
107
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 "));
113         }
114         try!(write!(w, r#"{:?}: {}; // {}<br align="left"/>"#,
115                     Lvalue::Var(i as u32), escape(&var.ty), var.name));
116     }
117
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)));
122     }
123
124     writeln!(w, ">;")
125 }
126
127 fn node(block: BasicBlock) -> String {
128     format!("bb{}", block.index())
129 }
130
131 fn escape<T: Debug>(t: &T) -> String {
132     dot::escape_html(&format!("{:?}", t))
133 }