Skip to content

Commit 2bc8d87

Browse files
committed
---
yaml --- r: 273520 b: refs/heads/beta c: d32bde3 h: refs/heads/master
1 parent a55c327 commit 2bc8d87

File tree

4 files changed

+92
-13
lines changed

4 files changed

+92
-13
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: caac0b969fd888014c3ff80238d8a00c6eae4feb
26+
refs/heads/beta: d32bde3311a035f2a0d7c26cf3170cf98860d701
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: e06d2ad9fcd5027bcaac5b08fc9aa39a49d0ecd3
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/src/librustc/mir/repr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub struct ArgDecl<'tcx> {
197197
/// list of the `Mir`.
198198
///
199199
/// (We use a `u32` internally just to save memory.)
200-
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)]
200+
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
201201
pub struct BasicBlock(u32);
202202

203203
impl BasicBlock {
@@ -668,7 +668,7 @@ impl IndexMut<ScopeId> for ScopeDataVec {
668668
}
669669
}
670670

671-
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
671+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
672672
pub struct ScopeId(u32);
673673

674674
impl ScopeId {

branches/beta/src/librustc_mir/build/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub struct ScopeAuxiliary {
7373
pub postdoms: Vec<Location>,
7474
}
7575

76+
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
7677
pub struct Location {
7778
/// the location is within this block
7879
pub block: BasicBlock,

branches/beta/src/librustc_mir/pretty.rs

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,138 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use build::Location;
1112
use rustc::mir::repr::*;
12-
use rustc::middle::ty;
13+
use rustc::middle::ty::{self, TyCtxt};
14+
use rustc_data_structures::fnv::FnvHashMap;
1315
use std::io::{self, Write};
1416
use syntax::ast::NodeId;
17+
use syntax::codemap::Span;
1518

1619
const INDENT: &'static str = " ";
1720

1821
/// Write out a human-readable textual representation for the given MIR.
19-
pub fn write_mir_pretty<'a, 'tcx, I>(tcx: &ty::TyCtxt<'tcx>,
22+
pub fn write_mir_pretty<'a, 'tcx, I>(tcx: &TyCtxt<'tcx>,
2023
iter: I,
2124
w: &mut Write)
2225
-> io::Result<()>
2326
where I: Iterator<Item=(&'a NodeId, &'a Mir<'tcx>)>, 'tcx: 'a
2427
{
28+
let no_annotations = FnvHashMap();
2529
for (&node_id, mir) in iter {
26-
write_mir_fn(tcx, node_id, mir, w)?;
30+
write_mir_fn(tcx, node_id, mir, w, &no_annotations)?;
2731
}
2832
Ok(())
2933
}
3034

31-
pub fn write_mir_fn<'tcx>(tcx: &ty::TyCtxt<'tcx>,
35+
pub enum Annotation {
36+
EnterScope(ScopeId),
37+
ExitScope(ScopeId),
38+
}
39+
40+
pub fn write_mir_fn<'tcx>(tcx: &TyCtxt<'tcx>,
3241
node_id: NodeId,
3342
mir: &Mir<'tcx>,
34-
w: &mut Write)
43+
w: &mut Write,
44+
annotations: &FnvHashMap<Location, Vec<Annotation>>)
3545
-> io::Result<()> {
3646
write_mir_intro(tcx, node_id, mir, w)?;
3747
for block in mir.all_basic_blocks() {
38-
write_basic_block(block, mir, w)?;
48+
write_basic_block(tcx, block, mir, w, annotations)?;
49+
}
50+
51+
// construct a scope tree
52+
let mut scope_tree: FnvHashMap<Option<ScopeId>, Vec<ScopeId>> = FnvHashMap();
53+
for (index, scope_data) in mir.scopes.vec.iter().enumerate() {
54+
scope_tree.entry(scope_data.parent_scope)
55+
.or_insert(vec![])
56+
.push(ScopeId::new(index));
3957
}
58+
write_scope_tree(tcx, mir, &scope_tree, w, None, 1)?;
59+
4060
writeln!(w, "}}")?;
4161
Ok(())
4262
}
4363

4464
/// Write out a human-readable textual representation for the given basic block.
45-
fn write_basic_block(block: BasicBlock, mir: &Mir, w: &mut Write) -> io::Result<()> {
65+
fn write_basic_block(tcx: &TyCtxt,
66+
block: BasicBlock,
67+
mir: &Mir,
68+
w: &mut Write,
69+
annotations: &FnvHashMap<Location, Vec<Annotation>>)
70+
-> io::Result<()> {
4671
let data = mir.basic_block_data(block);
4772

4873
// Basic block label at the top.
4974
writeln!(w, "\n{}{:?}: {{", INDENT, block)?;
5075

5176
// List of statements in the middle.
77+
let mut current_location = Location { block: block, statement_index: 0 };
5278
for statement in &data.statements {
53-
writeln!(w, "{0}{0}{1:?};", INDENT, statement)?;
79+
if let Some(ref annotations) = annotations.get(&current_location) {
80+
for annotation in annotations.iter() {
81+
match *annotation {
82+
Annotation::EnterScope(id) =>
83+
writeln!(w, "{0}{0}// Enter Scope({1})",
84+
INDENT, id.index())?,
85+
Annotation::ExitScope(id) =>
86+
writeln!(w, "{0}{0}// Exit Scope({1})",
87+
INDENT, id.index())?,
88+
}
89+
}
90+
}
91+
92+
writeln!(w, "{0}{0}{1:?}; // {2}",
93+
INDENT,
94+
statement,
95+
comment(tcx, statement.scope, statement.span))?;
96+
97+
current_location.statement_index += 1;
5498
}
5599

56100
// Terminator at the bottom.
57-
writeln!(w, "{0}{0}{1:?};", INDENT, data.terminator())?;
101+
writeln!(w, "{0}{0}{1:?}; // {2}",
102+
INDENT,
103+
data.terminator(),
104+
comment(tcx, data.terminator().scope, data.terminator().span))?;
58105

59106
writeln!(w, "{}}}", INDENT)
60107
}
61108

109+
fn comment(tcx: &TyCtxt,
110+
scope: ScopeId,
111+
span: Span)
112+
-> String {
113+
format!("Scope({}) at {}", scope.index(), tcx.sess.codemap().span_to_string(span))
114+
}
115+
116+
fn write_scope_tree(tcx: &TyCtxt,
117+
mir: &Mir,
118+
scope_tree: &FnvHashMap<Option<ScopeId>, Vec<ScopeId>>,
119+
w: &mut Write,
120+
parent: Option<ScopeId>,
121+
depth: usize)
122+
-> io::Result<()> {
123+
for &child in scope_tree.get(&parent).unwrap_or(&vec![]) {
124+
let indent = depth * INDENT.len();
125+
let data = &mir.scopes[child];
126+
assert_eq!(data.parent_scope, parent);
127+
writeln!(w, "{0:1$}Scope({2}) {{", "", indent, child.index())?;
128+
let indent = indent + INDENT.len();
129+
if let Some(parent) = parent {
130+
writeln!(w, "{0:1$}Parent: Scope({2})", "", indent, parent.index())?;
131+
}
132+
writeln!(w, "{0:1$}Extent: {2:?}",
133+
"", indent,
134+
tcx.region_maps.code_extent_data(data.extent))?;
135+
write_scope_tree(tcx, mir, scope_tree, w, Some(child), depth + 1)?;
136+
}
137+
Ok(())
138+
}
139+
62140
/// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
63141
/// local variables (both user-defined bindings and compiler temporaries).
64-
fn write_mir_intro(tcx: &ty::TyCtxt, nid: NodeId, mir: &Mir, w: &mut Write)
142+
fn write_mir_intro(tcx: &TyCtxt, nid: NodeId, mir: &Mir, w: &mut Write)
65143
-> io::Result<()> {
66144
write!(w, "fn {}(", tcx.map.path_to_string(nid))?;
67145

0 commit comments

Comments
 (0)