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.
11 use rustc::mir::repr::*;
12 use rustc::middle::ty;
13 use std::io::{self, Write};
15 const INDENT: &'static str = " ";
17 /// Write out a human-readable textual representation for the given MIR.
18 pub fn write_mir_pretty<W: Write>(mir: &Mir, w: &mut W) -> io::Result<()> {
19 try!(write_mir_intro(mir, w));
22 for block in mir.all_basic_blocks() {
23 try!(write_basic_block(block, mir, w));
29 /// Write out a human-readable textual representation for the given basic block.
30 fn write_basic_block<W: Write>(block: BasicBlock, mir: &Mir, w: &mut W) -> io::Result<()> {
31 let data = mir.basic_block_data(block);
33 // Basic block label at the top.
34 try!(writeln!(w, "\n{}{:?}: {{", INDENT, block));
36 // List of statements in the middle.
37 for statement in &data.statements {
38 try!(writeln!(w, "{0}{0}{1:?};", INDENT, statement));
41 // Terminator at the bottom.
42 try!(writeln!(w, "{0}{0}{1:?};", INDENT, data.terminator()));
44 writeln!(w, "{}}}", INDENT)
47 /// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
48 /// local variables (both user-defined bindings and compiler temporaries).
49 fn write_mir_intro<W: Write>(mir: &Mir, w: &mut W) -> io::Result<()> {
50 try!(write!(w, "fn("));
53 for (i, arg) in mir.arg_decls.iter().enumerate() {
55 try!(write!(w, ", "));
57 try!(write!(w, "{:?}: {}", Lvalue::Arg(i as u32), arg.ty));
60 try!(write!(w, ") -> "));
64 ty::FnOutput::FnConverging(ty) => try!(write!(w, "{}", ty)),
65 ty::FnOutput::FnDiverging => try!(write!(w, "!")),
68 try!(writeln!(w, " {{"));
70 // User variable types (including the user's name in a comment).
71 for (i, var) in mir.var_decls.iter().enumerate() {
72 try!(write!(w, "{}let ", INDENT));
73 if var.mutability == Mutability::Mut {
74 try!(write!(w, "mut "));
76 try!(writeln!(w, "{:?}: {}; // {}", Lvalue::Var(i as u32), var.ty, var.name));
79 // Compiler-introduced temporary types.
80 for (i, temp) in mir.temp_decls.iter().enumerate() {
81 try!(writeln!(w, "{}let mut {:?}: {};", INDENT, Lvalue::Temp(i as u32), temp.ty));