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::hir::def_id::DefId;
14 use rustc::mir::transform::MirSource;
15 use rustc::ty::TyCtxt;
16 use rustc_data_structures::fx::FxHashMap;
17 use rustc_data_structures::indexed_vec::{Idx};
18 use std::fmt::Display;
20 use std::io::{self, Write};
21 use std::path::{PathBuf, Path};
23 const INDENT: &'static str = " ";
24 /// Alignment for lining up comments following MIR statements
25 const ALIGN: usize = 40;
27 /// If the session is properly configured, dumps a human-readable
28 /// representation of the mir into:
31 /// rustc.node<node_id>.<pass_name>.<disambiguator>
34 /// Output from this function is controlled by passing `-Z dump-mir=<filter>`,
35 /// where `<filter>` takes the following forms:
37 /// - `all` -- dump MIR for all fns, all passes, all everything
38 /// - `substring1&substring2,...` -- `&`-separated list of substrings
39 /// that can appear in the pass-name or the `item_path_str` for the given
40 /// node-id. If any one of the substrings match, the data is dumped out.
41 pub fn dump_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
43 disambiguator: &Display,
46 let filters = match tcx.sess.opts.debugging_opts.dump_mir {
48 Some(ref filters) => filters,
50 let node_id = src.item_id();
51 let node_path = tcx.item_path_str(tcx.hir.local_def_id(node_id));
56 pass_name.contains(filter) ||
57 node_path.contains(filter)
63 let promotion_id = match src {
64 MirSource::Promoted(_, id) => format!("-{:?}", id),
68 let mut file_path = PathBuf::new();
69 if let Some(ref file_dir) = tcx.sess.opts.debugging_opts.dump_mir_dir {
70 let p = Path::new(file_dir);
73 let file_name = format!("rustc.node{}{}.{}.{}.mir",
74 node_id, promotion_id, pass_name, disambiguator);
75 file_path.push(&file_name);
76 let _ = fs::File::create(&file_path).and_then(|mut file| {
77 writeln!(file, "// MIR for `{}`", node_path)?;
78 writeln!(file, "// node_id = {}", node_id)?;
79 writeln!(file, "// pass_name = {}", pass_name)?;
80 writeln!(file, "// disambiguator = {}", disambiguator)?;
82 write_mir_fn(tcx, src, mir, &mut file)?;
87 /// Write out a human-readable textual representation for the given MIR.
88 pub fn write_mir_pretty<'a, 'b, 'tcx, I>(tcx: TyCtxt<'b, 'tcx, 'tcx>,
92 where I: Iterator<Item=DefId>, 'tcx: 'a
94 writeln!(w, "// WARNING: This output format is intended for human consumers only")?;
95 writeln!(w, "// and is subject to change without notice. Knock yourself out.")?;
98 for def_id in iter.filter(DefId::is_local) {
99 let mir = &tcx.item_mir(def_id);
104 // Put empty lines between all items
108 let id = tcx.hir.as_local_node_id(def_id).unwrap();
109 let src = MirSource::from_node(tcx, id);
110 write_mir_fn(tcx, src, mir, w)?;
112 for (i, mir) in mir.promoted.iter_enumerated() {
114 write_mir_fn(tcx, MirSource::Promoted(id, i), mir, w)?;
120 pub fn write_mir_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
125 write_mir_intro(tcx, src, mir, w)?;
126 for block in mir.basic_blocks().indices() {
127 write_basic_block(tcx, block, mir, w)?;
128 if block.index() + 1 != mir.basic_blocks().len() {
137 /// Write out a human-readable textual representation for the given basic block.
138 fn write_basic_block(tcx: TyCtxt,
143 let data = &mir[block];
145 // Basic block label at the top.
146 writeln!(w, "{}{:?}: {{", INDENT, block)?;
148 // List of statements in the middle.
149 let mut current_location = Location { block: block, statement_index: 0 };
150 for statement in &data.statements {
151 let indented_mir = format!("{0}{0}{1:?};", INDENT, statement);
152 writeln!(w, "{0:1$} // {2}",
155 comment(tcx, statement.source_info))?;
157 current_location.statement_index += 1;
160 // Terminator at the bottom.
161 let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
162 writeln!(w, "{0:1$} // {2}",
165 comment(tcx, data.terminator().source_info))?;
167 writeln!(w, "{}}}", INDENT)
170 fn comment(tcx: TyCtxt, SourceInfo { span, scope }: SourceInfo) -> String {
171 format!("scope {} at {}", scope.index(), tcx.sess.codemap().span_to_string(span))
174 /// Prints user-defined variables in a scope tree.
176 /// Returns the total number of variables printed.
177 fn write_scope_tree(tcx: TyCtxt,
179 scope_tree: &FxHashMap<VisibilityScope, Vec<VisibilityScope>>,
181 parent: VisibilityScope,
184 let indent = depth * INDENT.len();
186 let children = match scope_tree.get(&parent) {
187 Some(childs) => childs,
188 None => return Ok(()),
191 for &child in children {
192 let data = &mir.visibility_scopes[child];
193 assert_eq!(data.parent_scope, Some(parent));
194 writeln!(w, "{0:1$}scope {2} {{", "", indent, child.index())?;
196 // User variable types (including the user's name in a comment).
197 for local in mir.vars_iter() {
198 let var = &mir.local_decls[local];
199 let (name, source_info) = if var.source_info.scope == child {
200 (var.name.unwrap(), var.source_info)
202 // Not a variable or not declared in this scope.
206 let mut_str = if var.mutability == Mutability::Mut {
212 let indent = indent + INDENT.len();
213 let indented_var = format!("{0:1$}let {2}{3:?}: {4};",
219 writeln!(w, "{0:1$} // \"{2}\" in {3}",
223 comment(tcx, source_info))?;
226 write_scope_tree(tcx, mir, scope_tree, w, child, depth + 1)?;
228 writeln!(w, "{0:1$}}}", "", depth * INDENT.len())?;
234 /// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
235 /// local variables (both user-defined bindings and compiler temporaries).
236 fn write_mir_intro<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
241 write_mir_sig(tcx, src, mir, w)?;
244 // construct a scope tree and write it out
245 let mut scope_tree: FxHashMap<VisibilityScope, Vec<VisibilityScope>> = FxHashMap();
246 for (index, scope_data) in mir.visibility_scopes.iter().enumerate() {
247 if let Some(parent) = scope_data.parent_scope {
248 scope_tree.entry(parent)
250 .push(VisibilityScope::new(index));
252 // Only the argument scope has no parent, because it's the root.
253 assert_eq!(index, ARGUMENT_VISIBILITY_SCOPE.index());
257 // Print return pointer
258 let indented_retptr = format!("{}let mut {:?}: {};",
262 writeln!(w, "{0:1$} // return pointer",
266 write_scope_tree(tcx, mir, &scope_tree, w, ARGUMENT_VISIBILITY_SCOPE, 1)?;
268 write_temp_decls(mir, w)?;
270 // Add an empty line before the first block is printed.
276 fn write_mir_sig(tcx: TyCtxt, src: MirSource, mir: &Mir, w: &mut Write)
280 MirSource::Fn(_) => write!(w, "fn")?,
281 MirSource::Const(_) => write!(w, "const")?,
282 MirSource::Static(_, hir::MutImmutable) => write!(w, "static")?,
283 MirSource::Static(_, hir::MutMutable) => write!(w, "static mut")?,
284 MirSource::Promoted(_, i) => write!(w, "{:?} in", i)?
287 write!(w, " {}", tcx.node_path_str(src.item_id()))?;
289 if let MirSource::Fn(_) = src {
292 // fn argument types.
293 for (i, arg) in mir.args_iter().enumerate() {
297 write!(w, "{:?}: {}", Lvalue::Local(arg), mir.local_decls[arg].ty)?;
300 write!(w, ") -> {}", mir.return_ty)
302 assert_eq!(mir.arg_count, 0);
303 write!(w, ": {} =", mir.return_ty)
307 fn write_temp_decls(mir: &Mir, w: &mut Write) -> io::Result<()> {
308 // Compiler-introduced temporary types.
309 for temp in mir.temps_iter() {
310 writeln!(w, "{}let mut {:?}: {};", INDENT, temp, mir.local_decls[temp].ty)?;