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