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, LOCAL_CRATE};
14 use rustc::mir::visit::Visitor;
15 use rustc::ty::{self, TyCtxt};
16 use rustc::ty::item_path;
17 use rustc_data_structures::fx::FxHashMap;
18 use rustc_data_structures::indexed_vec::Idx;
19 use std::fmt::Display;
21 use std::io::{self, Write};
22 use std::path::{Path, PathBuf};
23 use super::graphviz::write_mir_fn_graphviz;
24 use transform::MirSource;
26 const INDENT: &'static str = " ";
27 /// Alignment for lining up comments following MIR statements
28 pub(crate) const ALIGN: usize = 40;
30 /// An indication of where we are in the control flow graph. Used for printing
31 /// extra information in `dump_mir`
33 /// We have not started dumping the control flow graph, but we are about to.
36 /// We just finished dumping the control flow graph. This is right before EOF
39 /// We are about to start dumping the given basic block.
40 BeforeBlock(BasicBlock),
42 /// We are just about to dump the given statement or terminator.
43 BeforeLocation(Location),
45 /// We just dumped the given statement or terminator.
46 AfterLocation(Location),
49 /// If the session is properly configured, dumps a human-readable
50 /// representation of the mir into:
53 /// rustc.node<node_id>.<pass_num>.<pass_name>.<disambiguator>
56 /// Output from this function is controlled by passing `-Z dump-mir=<filter>`,
57 /// where `<filter>` takes the following forms:
59 /// - `all` -- dump MIR for all fns, all passes, all everything
60 /// - a filter defined by a set of substrings combined with `&` and `|`
61 /// (`&` has higher precedence). At least one of the `|`-separated groups
62 /// must match; an `|`-separated group matches if all of its `&`-separated
63 /// substrings are matched.
67 /// - `nll` == match if `nll` appears in the name
68 /// - `foo & nll` == match if `foo` and `nll` both appear in the name
69 /// - `foo & nll | typeck` == match if `foo` and `nll` both appear in the name
70 /// or `typeck` appears in the name.
71 /// - `foo & nll | bar & typeck` == match if `foo` and `nll` both appear in the name
72 /// or `typeck` and `bar` both appear in the name.
73 pub fn dump_mir<'a, 'gcx, 'tcx, F>(
74 tcx: TyCtxt<'a, 'gcx, 'tcx>,
75 pass_num: Option<&Display>,
77 disambiguator: &Display,
82 F: FnMut(PassWhere, &mut Write) -> io::Result<()>,
84 if !dump_enabled(tcx, pass_name, source) {
88 let node_path = item_path::with_forced_impl_filename_line(|| {
89 // see notes on #41697 below
90 tcx.item_path_str(source.def_id)
92 dump_matched_mir_node(
104 pub fn dump_enabled<'a, 'gcx, 'tcx>(
105 tcx: TyCtxt<'a, 'gcx, 'tcx>,
109 let filters = match tcx.sess.opts.debugging_opts.dump_mir {
110 None => return false,
111 Some(ref filters) => filters,
113 let node_path = item_path::with_forced_impl_filename_line(|| {
114 // see notes on #41697 below
115 tcx.item_path_str(source.def_id)
117 filters.split("|").any(|or_filter| {
118 or_filter.split("&").all(|and_filter| {
119 and_filter == "all" || pass_name.contains(and_filter) || node_path.contains(and_filter)
124 // #41697 -- we use `with_forced_impl_filename_line()` because
125 // `item_path_str()` would otherwise trigger `type_of`, and this can
126 // run while we are already attempting to evaluate `type_of`.
128 fn dump_matched_mir_node<'a, 'gcx, 'tcx, F>(
129 tcx: TyCtxt<'a, 'gcx, 'tcx>,
130 pass_num: Option<&Display>,
133 disambiguator: &Display,
138 F: FnMut(PassWhere, &mut Write) -> io::Result<()>,
140 let _: io::Result<()> = do catch {
141 let mut file = create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, source)?;
142 writeln!(file, "// MIR for `{}`", node_path)?;
143 writeln!(file, "// source = {:?}", source)?;
144 writeln!(file, "// pass_name = {}", pass_name)?;
145 writeln!(file, "// disambiguator = {}", disambiguator)?;
146 if let Some(ref layout) = mir.generator_layout {
147 writeln!(file, "// generator_layout = {:?}", layout)?;
150 extra_data(PassWhere::BeforeCFG, &mut file)?;
151 write_mir_fn(tcx, source, mir, &mut extra_data, &mut file)?;
152 extra_data(PassWhere::AfterCFG, &mut file)?;
156 if tcx.sess.opts.debugging_opts.dump_mir_graphviz {
157 let _: io::Result<()> = do catch {
159 create_dump_file(tcx, "dot", pass_num, pass_name, disambiguator, source)?;
160 write_mir_fn_graphviz(tcx, source.def_id, mir, &mut file)?;
166 /// Returns the path to the filename where we should dump a given MIR.
167 /// Also used by other bits of code (e.g., NLL inference) that dump
168 /// graphviz data or other things.
170 tcx: TyCtxt<'_, '_, '_>,
172 pass_num: Option<&Display>,
174 disambiguator: &Display,
177 let promotion_id = match source.promoted {
178 Some(id) => format!("-{:?}", id),
179 None => String::new(),
182 let pass_num = if tcx.sess.opts.debugging_opts.dump_mir_exclude_pass_number {
186 None => format!(".-------"),
187 Some(pass_num) => format!(".{}", pass_num),
191 let mut file_path = PathBuf::new();
192 file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir));
194 let item_name = tcx.hir
195 .def_path(source.def_id)
196 .to_filename_friendly_no_crate();
198 let file_name = format!(
199 "rustc.{}{}{}.{}.{}.{}",
208 file_path.push(&file_name);
213 /// Attempts to open a file where we should dump a given MIR or other
214 /// bit of MIR-related data. Used by `mir-dump`, but also by other
215 /// bits of code (e.g., NLL inference) that dump graphviz data or
216 /// other things, and hence takes the extension as an argument.
217 pub(crate) fn create_dump_file(
218 tcx: TyCtxt<'_, '_, '_>,
220 pass_num: Option<&Display>,
222 disambiguator: &Display,
224 ) -> io::Result<fs::File> {
225 let file_path = dump_path(tcx, extension, pass_num, pass_name, disambiguator, source);
226 if let Some(parent) = file_path.parent() {
227 fs::create_dir_all(parent)?;
229 fs::File::create(&file_path)
232 /// Write out a human-readable textual representation for the given MIR.
233 pub fn write_mir_pretty<'a, 'gcx, 'tcx>(
234 tcx: TyCtxt<'a, 'gcx, 'tcx>,
235 single: Option<DefId>,
237 ) -> io::Result<()> {
240 "// WARNING: This output format is intended for human consumers only"
244 "// and is subject to change without notice. Knock yourself out."
247 let mut first = true;
248 for def_id in dump_mir_def_ids(tcx, single) {
249 let mir = &tcx.optimized_mir(def_id);
254 // Put empty lines between all items
258 write_mir_fn(tcx, MirSource::item(def_id), mir, &mut |_, _| Ok(()), w)?;
260 for (i, mir) in mir.promoted.iter_enumerated() {
262 let src = MirSource {
266 write_mir_fn(tcx, src, mir, &mut |_, _| Ok(()), w)?;
272 pub fn write_mir_fn<'a, 'gcx, 'tcx, F>(
273 tcx: TyCtxt<'a, 'gcx, 'tcx>,
280 F: FnMut(PassWhere, &mut Write) -> io::Result<()>,
282 write_mir_intro(tcx, src, mir, w)?;
283 for block in mir.basic_blocks().indices() {
284 extra_data(PassWhere::BeforeBlock(block), w)?;
285 write_basic_block(tcx, block, mir, extra_data, w)?;
286 if block.index() + 1 != mir.basic_blocks().len() {
295 /// Write out a human-readable textual representation for the given basic block.
296 pub fn write_basic_block<'cx, 'gcx, 'tcx, F>(
297 tcx: TyCtxt<'cx, 'gcx, 'tcx>,
304 F: FnMut(PassWhere, &mut Write) -> io::Result<()>,
306 let data = &mir[block];
308 // Basic block label at the top.
309 let cleanup_text = if data.is_cleanup { " // cleanup" } else { "" };
310 let lbl = format!("{}{:?}: {{", INDENT, block);
311 writeln!(w, "{0:1$}{2}", lbl, ALIGN, cleanup_text)?;
313 // List of statements in the middle.
314 let mut current_location = Location {
318 for statement in &data.statements {
319 extra_data(PassWhere::BeforeLocation(current_location), w)?;
320 let indented_mir = format!("{0}{0}{1:?};", INDENT, statement);
326 comment(tcx, statement.source_info),
330 write_extra(tcx, w, |visitor| {
331 visitor.visit_statement(current_location.block, statement, current_location);
334 extra_data(PassWhere::AfterLocation(current_location), w)?;
336 current_location.statement_index += 1;
339 // Terminator at the bottom.
340 extra_data(PassWhere::BeforeLocation(current_location), w)?;
341 let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
347 comment(tcx, data.terminator().source_info),
351 write_extra(tcx, w, |visitor| {
352 visitor.visit_terminator(current_location.block, data.terminator(), current_location);
355 extra_data(PassWhere::AfterLocation(current_location), w)?;
357 writeln!(w, "{}}}", INDENT)
360 /// After we print the main statement, we sometimes dump extra
361 /// information. There's often a lot of little things "nuzzled up" in
363 fn write_extra<'cx, 'gcx, 'tcx, F>(
364 tcx: TyCtxt<'cx, 'gcx, 'tcx>,
369 F: FnMut(&mut ExtraComments<'cx, 'gcx, 'tcx>),
371 let mut extra_comments = ExtraComments {
375 visit_op(&mut extra_comments);
376 for comment in extra_comments.comments {
377 writeln!(write, "{:A$} // {}", "", comment, A = ALIGN)?;
382 struct ExtraComments<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
383 _tcx: TyCtxt<'cx, 'gcx, 'tcx>, // don't need it now, but bet we will soon
384 comments: Vec<String>,
387 impl<'cx, 'gcx, 'tcx> ExtraComments<'cx, 'gcx, 'tcx> {
388 fn push(&mut self, lines: &str) {
389 for line in lines.split("\n") {
390 self.comments.push(line.to_string());
395 impl<'cx, 'gcx, 'tcx> Visitor<'tcx> for ExtraComments<'cx, 'gcx, 'tcx> {
396 fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
397 self.super_constant(constant, location);
398 let Constant { span, ty, literal } = constant;
399 self.push(&format!("mir::Constant"));
400 self.push(&format!("└ span: {:?}", span));
401 self.push(&format!("└ ty: {:?}", ty));
402 self.push(&format!("└ literal: {:?}", literal));
405 fn visit_const(&mut self, constant: &&'tcx ty::Const<'tcx>, _: Location) {
406 self.super_const(constant);
407 let ty::Const { ty, val } = constant;
408 self.push(&format!("ty::Const"));
409 self.push(&format!("└ ty: {:?}", ty));
410 self.push(&format!("└ val: {:?}", val));
413 fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
414 self.super_rvalue(rvalue, location);
416 Rvalue::Aggregate(kind, _) => match **kind {
417 AggregateKind::Closure(def_id, substs) => {
418 self.push(&format!("closure"));
419 self.push(&format!("└ def_id: {:?}", def_id));
420 self.push(&format!("└ substs: {:#?}", substs));
423 AggregateKind::Generator(def_id, substs, interior) => {
424 self.push(&format!("generator"));
425 self.push(&format!("└ def_id: {:?}", def_id));
426 self.push(&format!("└ substs: {:#?}", substs));
427 self.push(&format!("└ interior: {:?}", interior));
438 fn comment(tcx: TyCtxt, SourceInfo { span, scope }: SourceInfo) -> String {
442 tcx.sess.codemap().span_to_string(span)
446 /// Prints user-defined variables in a scope tree.
448 /// Returns the total number of variables printed.
452 scope_tree: &FxHashMap<VisibilityScope, Vec<VisibilityScope>>,
454 parent: VisibilityScope,
456 ) -> io::Result<()> {
457 let indent = depth * INDENT.len();
459 let children = match scope_tree.get(&parent) {
460 Some(childs) => childs,
461 None => return Ok(()),
464 for &child in children {
465 let data = &mir.visibility_scopes[child];
466 assert_eq!(data.parent_scope, Some(parent));
467 writeln!(w, "{0:1$}scope {2} {{", "", indent, child.index())?;
469 // User variable types (including the user's name in a comment).
470 for local in mir.vars_iter() {
471 let var = &mir.local_decls[local];
472 let (name, source_info) = if var.source_info.scope == child {
473 (var.name.unwrap(), var.source_info)
475 // Not a variable or not declared in this scope.
479 let mut_str = if var.mutability == Mutability::Mut {
485 let indent = indent + INDENT.len();
486 let indented_var = format!(
487 "{0:1$}let {2}{3:?}: {4:?};",
496 "{0:1$} // \"{2}\" in {3}",
500 comment(tcx, source_info)
504 write_scope_tree(tcx, mir, scope_tree, w, child, depth + 1)?;
506 writeln!(w, "{0:1$}}}", "", depth * INDENT.len())?;
512 /// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
513 /// local variables (both user-defined bindings and compiler temporaries).
514 pub fn write_mir_intro<'a, 'gcx, 'tcx>(
515 tcx: TyCtxt<'a, 'gcx, 'tcx>,
519 ) -> io::Result<()> {
520 write_mir_sig(tcx, src, mir, w)?;
523 // construct a scope tree and write it out
524 let mut scope_tree: FxHashMap<VisibilityScope, Vec<VisibilityScope>> = FxHashMap();
525 for (index, scope_data) in mir.visibility_scopes.iter().enumerate() {
526 if let Some(parent) = scope_data.parent_scope {
530 .push(VisibilityScope::new(index));
532 // Only the argument scope has no parent, because it's the root.
533 assert_eq!(index, ARGUMENT_VISIBILITY_SCOPE.index());
537 // Print return place
538 let indented_retptr = format!("{}let mut {:?}: {};",
541 mir.local_decls[RETURN_PLACE].ty);
542 writeln!(w, "{0:1$} // return place",
546 write_scope_tree(tcx, mir, &scope_tree, w, ARGUMENT_VISIBILITY_SCOPE, 1)?;
548 write_temp_decls(mir, w)?;
550 // Add an empty line before the first block is printed.
556 fn write_mir_sig(tcx: TyCtxt, src: MirSource, mir: &Mir, w: &mut Write) -> io::Result<()> {
557 let id = tcx.hir.as_local_node_id(src.def_id).unwrap();
558 let body_owner_kind = tcx.hir.body_owner_kind(id);
559 match (body_owner_kind, src.promoted) {
560 (_, Some(i)) => write!(w, "{:?} in", i)?,
561 (hir::BodyOwnerKind::Fn, _) => write!(w, "fn")?,
562 (hir::BodyOwnerKind::Const, _) => write!(w, "const")?,
563 (hir::BodyOwnerKind::Static(hir::MutImmutable), _) => write!(w, "static")?,
564 (hir::BodyOwnerKind::Static(hir::MutMutable), _) => write!(w, "static mut")?,
567 item_path::with_forced_impl_filename_line(|| {
568 // see notes on #41697 elsewhere
569 write!(w, " {}", tcx.item_path_str(src.def_id))
572 match (body_owner_kind, src.promoted) {
573 (hir::BodyOwnerKind::Fn, None) => {
576 // fn argument types.
577 for (i, arg) in mir.args_iter().enumerate() {
581 write!(w, "{:?}: {}", Place::Local(arg), mir.local_decls[arg].ty)?;
584 write!(w, ") -> {}", mir.return_ty())?;
586 (hir::BodyOwnerKind::Const, _) | (hir::BodyOwnerKind::Static(_), _) | (_, Some(_)) => {
587 assert_eq!(mir.arg_count, 0);
588 write!(w, ": {} =", mir.return_ty())?;
592 if let Some(yield_ty) = mir.yield_ty {
594 writeln!(w, "yields {}", yield_ty)?;
600 fn write_temp_decls(mir: &Mir, w: &mut Write) -> io::Result<()> {
601 // Compiler-introduced temporary types.
602 for temp in mir.temps_iter() {
605 "{}let mut {:?}: {};",
608 mir.local_decls[temp].ty
615 pub fn dump_mir_def_ids(tcx: TyCtxt, single: Option<DefId>) -> Vec<DefId> {
616 if let Some(i) = single {
619 tcx.mir_keys(LOCAL_CRATE).iter().cloned().collect()