2015-12-29 20:06:19 -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 rustc::mir::repr::*;
|
|
|
|
use rustc::middle::ty;
|
|
|
|
use std::io::{self, Write};
|
2016-03-12 19:07:00 +02:00
|
|
|
use syntax::ast::NodeId;
|
2015-12-29 20:06:19 -06:00
|
|
|
|
|
|
|
const INDENT: &'static str = " ";
|
|
|
|
|
|
|
|
/// Write out a human-readable textual representation for the given MIR.
|
2016-03-12 19:07:00 +02:00
|
|
|
pub fn write_mir_pretty<'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 {
|
|
|
|
try!(write_mir_intro(tcx, nodeid, mir, w));
|
|
|
|
// Nodes
|
|
|
|
for block in mir.all_basic_blocks() {
|
|
|
|
try!(write_basic_block(block, mir, w));
|
|
|
|
}
|
|
|
|
try!(writeln!(w, "}}"))
|
2015-12-29 20:06:19 -06:00
|
|
|
}
|
2016-03-12 19:07:00 +02:00
|
|
|
Ok(())
|
2015-12-29 20:06:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Write out a human-readable textual representation for the given basic block.
|
|
|
|
fn write_basic_block<W: Write>(block: BasicBlock, mir: &Mir, w: &mut W) -> io::Result<()> {
|
|
|
|
let data = mir.basic_block_data(block);
|
|
|
|
|
|
|
|
// Basic block label at the top.
|
|
|
|
try!(writeln!(w, "\n{}{:?}: {{", INDENT, block));
|
|
|
|
|
|
|
|
// List of statements in the middle.
|
|
|
|
for statement in &data.statements {
|
|
|
|
try!(writeln!(w, "{0}{0}{1:?};", INDENT, statement));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminator at the bottom.
|
2016-01-07 15:16:07 -06:00
|
|
|
try!(writeln!(w, "{0}{0}{1:?};", INDENT, data.terminator()));
|
2015-12-29 20:06:19 -06:00
|
|
|
|
|
|
|
writeln!(w, "{}}}", INDENT)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
|
|
|
|
/// local variables (both user-defined bindings and compiler temporaries).
|
2016-03-12 19:07:00 +02:00
|
|
|
fn write_mir_intro<W: Write>(tcx: &ty::TyCtxt, nid: NodeId, mir: &Mir, w: &mut W)
|
|
|
|
-> io::Result<()> {
|
|
|
|
|
|
|
|
try!(write!(w, "fn {}(", tcx.map.path_to_string(nid)));
|
2015-12-29 20:06:19 -06:00
|
|
|
|
|
|
|
// fn argument types.
|
|
|
|
for (i, arg) in mir.arg_decls.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
try!(write!(w, ", "));
|
|
|
|
}
|
|
|
|
try!(write!(w, "{:?}: {}", Lvalue::Arg(i as u32), arg.ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
try!(write!(w, ") -> "));
|
|
|
|
|
|
|
|
// fn return type.
|
|
|
|
match mir.return_ty {
|
|
|
|
ty::FnOutput::FnConverging(ty) => try!(write!(w, "{}", ty)),
|
|
|
|
ty::FnOutput::FnDiverging => try!(write!(w, "!")),
|
|
|
|
}
|
|
|
|
|
|
|
|
try!(writeln!(w, " {{"));
|
|
|
|
|
|
|
|
// User variable types (including the user's name in a comment).
|
|
|
|
for (i, var) in mir.var_decls.iter().enumerate() {
|
|
|
|
try!(write!(w, "{}let ", INDENT));
|
|
|
|
if var.mutability == Mutability::Mut {
|
|
|
|
try!(write!(w, "mut "));
|
|
|
|
}
|
|
|
|
try!(writeln!(w, "{:?}: {}; // {}", Lvalue::Var(i as u32), var.ty, var.name));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compiler-introduced temporary types.
|
|
|
|
for (i, temp) in mir.temp_decls.iter().enumerate() {
|
2016-01-02 07:10:25 -06:00
|
|
|
try!(writeln!(w, "{}let mut {:?}: {};", INDENT, Lvalue::Temp(i as u32), temp.ty));
|
2015-12-29 20:06:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|