]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/util/pretty.rs
0811783a9e57f7b75c211652c2398cc84b346c81
[rust.git] / src / librustc_mir / util / pretty.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 rustc::hir;
12 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
13 use rustc::mir::*;
14 use rustc::mir::transform::{MirSuite, MirPassIndex, MirSource};
15 use rustc::ty::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;
20 use std::fs;
21 use std::io::{self, Write};
22 use std::path::{PathBuf, Path};
23
24 const INDENT: &'static str = "    ";
25 /// Alignment for lining up comments following MIR statements
26 const ALIGN: usize = 40;
27
28 /// If the session is properly configured, dumps a human-readable
29 /// representation of the mir into:
30 ///
31 /// ```text
32 /// rustc.node<node_id>.<pass_num>.<pass_name>.<disambiguator>
33 /// ```
34 ///
35 /// Output from this function is controlled by passing `-Z dump-mir=<filter>`,
36 /// where `<filter>` takes the following forms:
37 ///
38 /// - `all` -- dump MIR for all fns, all passes, all everything
39 /// - `substring1&substring2,...` -- `&`-separated list of substrings
40 ///   that can appear in the pass-name or the `item_path_str` for the given
41 ///   node-id. If any one of the substrings match, the data is dumped out.
42 pub fn dump_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
43                           pass_num: Option<(MirSuite, MirPassIndex)>,
44                           pass_name: &str,
45                           disambiguator: &Display,
46                           source: MirSource,
47                           mir: &Mir<'tcx>) {
48     if !dump_enabled(tcx, pass_name, source) {
49         return;
50     }
51
52     let node_path = item_path::with_forced_impl_filename_line(|| { // see notes on #41697 below
53         tcx.item_path_str(tcx.hir.local_def_id(source.item_id()))
54     });
55     dump_matched_mir_node(tcx, pass_num, pass_name, &node_path,
56                           disambiguator, source, mir);
57     for (index, promoted_mir) in mir.promoted.iter_enumerated() {
58         let promoted_source = MirSource::Promoted(source.item_id(), index);
59         dump_matched_mir_node(tcx, pass_num, pass_name, &node_path, disambiguator,
60                               promoted_source, promoted_mir);
61     }
62 }
63
64 pub fn dump_enabled<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
65                               pass_name: &str,
66                               source: MirSource)
67                               -> bool {
68     let filters = match tcx.sess.opts.debugging_opts.dump_mir {
69         None => return false,
70         Some(ref filters) => filters,
71     };
72     let node_id = source.item_id();
73     let node_path = item_path::with_forced_impl_filename_line(|| { // see notes on #41697 below
74         tcx.item_path_str(tcx.hir.local_def_id(node_id))
75     });
76     filters.split("&")
77            .any(|filter| {
78                filter == "all" ||
79                    pass_name.contains(filter) ||
80                    node_path.contains(filter)
81            })
82 }
83
84 // #41697 -- we use `with_forced_impl_filename_line()` because
85 // `item_path_str()` would otherwise trigger `type_of`, and this can
86 // run while we are already attempting to evaluate `type_of`.
87
88 fn dump_matched_mir_node<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
89                                    pass_num: Option<(MirSuite, MirPassIndex)>,
90                                    pass_name: &str,
91                                    node_path: &str,
92                                    disambiguator: &Display,
93                                    source: MirSource,
94                                    mir: &Mir<'tcx>) {
95     let promotion_id = match source {
96         MirSource::Promoted(_, id) => format!("-{:?}", id),
97         MirSource::GeneratorDrop(_) => format!("-drop"),
98         _ => String::new()
99     };
100
101     let pass_num = if tcx.sess.opts.debugging_opts.dump_mir_exclude_pass_number {
102         format!("")
103     } else {
104         match pass_num {
105             None => format!(".-------"),
106             Some((suite, pass_num)) => format!(".{:03}-{:03}", suite.0, pass_num.0),
107         }
108     };
109
110     let mut file_path = PathBuf::new();
111     if let Some(ref file_dir) = tcx.sess.opts.debugging_opts.dump_mir_dir {
112         let p = Path::new(file_dir);
113         file_path.push(p);
114     };
115     let _ = fs::create_dir_all(&file_path);
116     let file_name = format!("rustc.node{}{}{}.{}.{}.mir",
117                             source.item_id(), promotion_id, pass_num, pass_name, disambiguator);
118     file_path.push(&file_name);
119     let _ = fs::File::create(&file_path).and_then(|mut file| {
120         writeln!(file, "// MIR for `{}`", node_path)?;
121         writeln!(file, "// source = {:?}", source)?;
122         writeln!(file, "// pass_name = {}", pass_name)?;
123         writeln!(file, "// disambiguator = {}", disambiguator)?;
124         if let Some(ref layout) = mir.generator_layout {
125             writeln!(file, "// generator_layout = {:?}", layout)?;
126         }
127         writeln!(file, "")?;
128         write_mir_fn(tcx, source, mir, &mut file)?;
129         Ok(())
130     });
131 }
132
133 /// Write out a human-readable textual representation for the given MIR.
134 pub fn write_mir_pretty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
135                                   single: Option<DefId>,
136                                   w: &mut Write)
137                                   -> io::Result<()>
138 {
139     writeln!(w, "// WARNING: This output format is intended for human consumers only")?;
140     writeln!(w, "// and is subject to change without notice. Knock yourself out.")?;
141
142     let mut first = true;
143     for def_id in dump_mir_def_ids(tcx, single) {
144         let mir = &tcx.optimized_mir(def_id);
145
146         if first {
147             first = false;
148         } else {
149             // Put empty lines between all items
150             writeln!(w, "")?;
151         }
152
153         let id = tcx.hir.as_local_node_id(def_id).unwrap();
154         let src = MirSource::from_node(tcx, id);
155         write_mir_fn(tcx, src, mir, w)?;
156
157         for (i, mir) in mir.promoted.iter_enumerated() {
158             writeln!(w, "")?;
159             write_mir_fn(tcx, MirSource::Promoted(id, i), mir, w)?;
160         }
161     }
162     Ok(())
163 }
164
165 pub fn write_mir_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
166                               src: MirSource,
167                               mir: &Mir<'tcx>,
168                               w: &mut Write)
169                               -> io::Result<()> {
170     write_mir_intro(tcx, src, mir, w)?;
171     for block in mir.basic_blocks().indices() {
172         write_basic_block(tcx, block, mir, w)?;
173         if block.index() + 1 != mir.basic_blocks().len() {
174             writeln!(w, "")?;
175         }
176     }
177
178     writeln!(w, "}}")?;
179     Ok(())
180 }
181
182 /// Write out a human-readable textual representation for the given basic block.
183 pub fn write_basic_block(tcx: TyCtxt,
184                      block: BasicBlock,
185                      mir: &Mir,
186                      w: &mut Write)
187                      -> io::Result<()> {
188     let data = &mir[block];
189
190     // Basic block label at the top.
191     writeln!(w, "{}{:?}: {{", INDENT, block)?;
192
193     // List of statements in the middle.
194     let mut current_location = Location { block: block, statement_index: 0 };
195     for statement in &data.statements {
196         let indented_mir = format!("{0}{0}{1:?};", INDENT, statement);
197         writeln!(w, "{0:1$} // {2}",
198                  indented_mir,
199                  ALIGN,
200                  comment(tcx, statement.source_info))?;
201
202         current_location.statement_index += 1;
203     }
204
205     // Terminator at the bottom.
206     let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
207     writeln!(w, "{0:1$} // {2}",
208              indented_terminator,
209              ALIGN,
210              comment(tcx, data.terminator().source_info))?;
211
212     writeln!(w, "{}}}", INDENT)
213 }
214
215 fn comment(tcx: TyCtxt, SourceInfo { span, scope }: SourceInfo) -> String {
216     format!("scope {} at {}", scope.index(), tcx.sess.codemap().span_to_string(span))
217 }
218
219 /// Prints user-defined variables in a scope tree.
220 ///
221 /// Returns the total number of variables printed.
222 fn write_scope_tree(tcx: TyCtxt,
223                     mir: &Mir,
224                     scope_tree: &FxHashMap<VisibilityScope, Vec<VisibilityScope>>,
225                     w: &mut Write,
226                     parent: VisibilityScope,
227                     depth: usize)
228                     -> io::Result<()> {
229     let indent = depth * INDENT.len();
230
231     let children = match scope_tree.get(&parent) {
232         Some(childs) => childs,
233         None => return Ok(()),
234     };
235
236     for &child in children {
237         let data = &mir.visibility_scopes[child];
238         assert_eq!(data.parent_scope, Some(parent));
239         writeln!(w, "{0:1$}scope {2} {{", "", indent, child.index())?;
240
241         // User variable types (including the user's name in a comment).
242         for local in mir.vars_iter() {
243             let var = &mir.local_decls[local];
244             let (name, source_info) = if var.source_info.scope == child {
245                 (var.name.unwrap(), var.source_info)
246             } else {
247                 // Not a variable or not declared in this scope.
248                 continue;
249             };
250
251             let mut_str = if var.mutability == Mutability::Mut {
252                 "mut "
253             } else {
254                 ""
255             };
256
257             let indent = indent + INDENT.len();
258             let indented_var = format!("{0:1$}let {2}{3:?}: {4};",
259                                        INDENT,
260                                        indent,
261                                        mut_str,
262                                        local,
263                                        var.ty);
264             writeln!(w, "{0:1$} // \"{2}\" in {3}",
265                      indented_var,
266                      ALIGN,
267                      name,
268                      comment(tcx, source_info))?;
269         }
270
271         write_scope_tree(tcx, mir, scope_tree, w, child, depth + 1)?;
272
273         writeln!(w, "{0:1$}}}", "", depth * INDENT.len())?;
274     }
275
276     Ok(())
277 }
278
279 /// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
280 /// local variables (both user-defined bindings and compiler temporaries).
281 pub fn write_mir_intro<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
282                              src: MirSource,
283                              mir: &Mir,
284                              w: &mut Write)
285                              -> io::Result<()> {
286     write_mir_sig(tcx, src, mir, w)?;
287     writeln!(w, " {{")?;
288
289     // construct a scope tree and write it out
290     let mut scope_tree: FxHashMap<VisibilityScope, Vec<VisibilityScope>> = FxHashMap();
291     for (index, scope_data) in mir.visibility_scopes.iter().enumerate() {
292         if let Some(parent) = scope_data.parent_scope {
293             scope_tree.entry(parent)
294                       .or_insert(vec![])
295                       .push(VisibilityScope::new(index));
296         } else {
297             // Only the argument scope has no parent, because it's the root.
298             assert_eq!(index, ARGUMENT_VISIBILITY_SCOPE.index());
299         }
300     }
301
302     // Print return pointer
303     let indented_retptr = format!("{}let mut {:?}: {};",
304                                   INDENT,
305                                   RETURN_POINTER,
306                                   mir.return_ty);
307     writeln!(w, "{0:1$} // return pointer",
308              indented_retptr,
309              ALIGN)?;
310
311     write_scope_tree(tcx, mir, &scope_tree, w, ARGUMENT_VISIBILITY_SCOPE, 1)?;
312
313     write_temp_decls(mir, w)?;
314
315     // Add an empty line before the first block is printed.
316     writeln!(w, "")?;
317
318     Ok(())
319 }
320
321 fn write_mir_sig(tcx: TyCtxt, src: MirSource, mir: &Mir, w: &mut Write)
322                  -> io::Result<()>
323 {
324     match src {
325         MirSource::Fn(_) => write!(w, "fn")?,
326         MirSource::Const(_) => write!(w, "const")?,
327         MirSource::Static(_, hir::MutImmutable) => write!(w, "static")?,
328         MirSource::Static(_, hir::MutMutable) => write!(w, "static mut")?,
329         MirSource::Promoted(_, i) => write!(w, "{:?} in", i)?,
330         MirSource::GeneratorDrop(_) => write!(w, "drop_glue")?,
331     }
332
333     item_path::with_forced_impl_filename_line(|| { // see notes on #41697 elsewhere
334         write!(w, " {}", tcx.node_path_str(src.item_id()))
335     })?;
336
337     match src {
338         MirSource::Fn(_) | MirSource::GeneratorDrop(_) => {
339             write!(w, "(")?;
340
341             // fn argument types.
342             for (i, arg) in mir.args_iter().enumerate() {
343                 if i != 0 {
344                     write!(w, ", ")?;
345                 }
346                 write!(w, "{:?}: {}", Lvalue::Local(arg), mir.local_decls[arg].ty)?;
347             }
348
349             write!(w, ") -> {}", mir.return_ty)
350         }
351         MirSource::Const(..) |
352         MirSource::Static(..) |
353         MirSource::Promoted(..) => {
354             assert_eq!(mir.arg_count, 0);
355             write!(w, ": {} =", mir.return_ty)
356         }
357     }
358 }
359
360 fn write_temp_decls(mir: &Mir, w: &mut Write) -> io::Result<()> {
361     // Compiler-introduced temporary types.
362     for temp in mir.temps_iter() {
363         writeln!(w, "{}let mut {:?}: {};", INDENT, temp, mir.local_decls[temp].ty)?;
364     }
365
366     Ok(())
367 }
368
369 pub fn dump_mir_def_ids(tcx: TyCtxt, single: Option<DefId>) -> Vec<DefId> {
370     if let Some(i) = single {
371         vec![i]
372     } else {
373         tcx.mir_keys(LOCAL_CRATE).iter().cloned().collect()
374     }
375 }