Skip to content

Commit 74bb917

Browse files
committed
add location table
This will be used in fact generation.
1 parent 53eb9e5 commit 74bb917

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright 2017 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+
#![allow(dead_code)] // TODO -- will be used in a later commit, remove then
12+
13+
use rustc::mir::{BasicBlock, Location, Mir};
14+
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
15+
16+
/// Maps between a MIR Location, which identifies the a particular
17+
/// statement within a basic block, to a "rich location", which
18+
/// identifies at a finer granularity. In particular, we distinguish
19+
/// the *start* of a statement and the *mid-point*. The mid-point is
20+
/// the point *just* before the statement takes effect; in particular,
21+
/// for an assignment `A = B`, it is the point where B is about to be
22+
/// written into A. This mid-point is a kind of hack to work around
23+
/// our inability to track the position information at sufficient
24+
/// granularity through outlives relations; however, the rich location
25+
/// table serves another purpose: it compresses locations from
26+
/// multiple words into a single u32.
27+
crate struct LocationTable {
28+
num_points: usize,
29+
statements_before_block: IndexVec<BasicBlock, usize>,
30+
}
31+
32+
newtype_index!(LocationIndex { DEBUG_FORMAT = "LocationIndex({})" });
33+
34+
#[derive(Copy, Clone, Debug)]
35+
crate enum RichLocation {
36+
Start(Location),
37+
Mid(Location),
38+
}
39+
40+
impl LocationTable {
41+
crate fn new(mir: &Mir<'_>) -> Self {
42+
let mut num_points = 0;
43+
let statements_before_block = mir.basic_blocks()
44+
.iter()
45+
.map(|block_data| {
46+
let v = num_points;
47+
num_points += (block_data.statements.len() + 1) * 2;
48+
v
49+
})
50+
.collect();
51+
52+
debug!(
53+
"LocationTable(statements_before_block={:#?})",
54+
statements_before_block
55+
);
56+
debug!("LocationTable: num_points={:#?}", num_points);
57+
58+
Self {
59+
num_points,
60+
statements_before_block,
61+
}
62+
}
63+
64+
#[allow(dead_code)] // TODO
65+
crate fn all_points(&self) -> impl Iterator<Item = LocationIndex> {
66+
(0..self.num_points).map(LocationIndex::new)
67+
}
68+
69+
crate fn start_index(&self, location: Location) -> LocationIndex {
70+
let Location {
71+
block,
72+
statement_index,
73+
} = location;
74+
let start_index = self.statements_before_block[block];
75+
LocationIndex::new(start_index + statement_index * 2)
76+
}
77+
78+
crate fn mid_index(&self, location: Location) -> LocationIndex {
79+
let Location {
80+
block,
81+
statement_index,
82+
} = location;
83+
let start_index = self.statements_before_block[block];
84+
LocationIndex::new(start_index + statement_index * 2 + 1)
85+
}
86+
87+
crate fn to_location(&self, index: LocationIndex) -> RichLocation {
88+
let point_index = index.index();
89+
90+
// Find the basic block. We have a vector with the
91+
// starting index of the statement in each block. Imagine
92+
// we have statement #22, and we have a vector like:
93+
//
94+
// [0, 10, 20]
95+
//
96+
// In that case, this represents point_index 2 of
97+
// basic block BB2. We know this because BB0 accounts for
98+
// 0..10, BB1 accounts for 11..20, and BB2 accounts for
99+
// 20...
100+
//
101+
// To compute this, we could do a binary search, but
102+
// because I am lazy we instead iterate through to find
103+
// the last point where the "first index" (0, 10, or 20)
104+
// was less than the statement index (22). In our case, this will
105+
// be (BB2, 20).
106+
let (block, &first_index) = self.statements_before_block
107+
.iter_enumerated()
108+
.filter(|(_, first_index)| **first_index <= point_index)
109+
.last()
110+
.unwrap();
111+
112+
let statement_index = (point_index - first_index) / 2;
113+
if index.is_start() {
114+
RichLocation::Start(Location { block, statement_index })
115+
} else {
116+
RichLocation::Mid(Location { block, statement_index })
117+
}
118+
}
119+
}
120+
121+
impl LocationIndex {
122+
fn is_start(&self) -> bool {
123+
// even indices are start points; odd indices are mid points
124+
(self.index() % 2) == 0
125+
}
126+
}

src/librustc_mir/borrow_check/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ use std::iter;
5050

5151
use self::borrow_set::{BorrowSet, BorrowData};
5252
use self::flows::Flows;
53+
use self::location::LocationTable;
5354
use self::prefixes::PrefixSet;
5455
use self::MutateMode::{JustWrite, WriteAndRead};
5556

5657
crate mod borrow_set;
5758
mod error_reporting;
5859
mod flows;
60+
mod location;
5961
crate mod place_ext;
6062
mod prefixes;
6163

@@ -110,6 +112,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
110112
let mut mir: Mir<'tcx> = input_mir.clone();
111113
let free_regions = nll::replace_regions_in_mir(infcx, def_id, param_env, &mut mir);
112114
let mir = &mir; // no further changes
115+
let location_table = &LocationTable::new(mir);
113116

114117
let move_data: MoveData<'tcx> = match MoveData::gather_moves(mir, tcx) {
115118
Ok(move_data) => move_data,
@@ -199,6 +202,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
199202
def_id,
200203
free_regions,
201204
mir,
205+
location_table,
202206
param_env,
203207
&mut flow_inits,
204208
&mdpe.move_data,

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use borrow_check::borrow_set::BorrowSet;
12+
use borrow_check::location::LocationTable;
1213
use rustc::hir::def_id::DefId;
1314
use rustc::mir::{ClosureRegionRequirements, ClosureOutlivesSubject, Mir};
1415
use rustc::infer::InferCtxt;
@@ -70,6 +71,7 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
7071
def_id: DefId,
7172
universal_regions: UniversalRegions<'tcx>,
7273
mir: &Mir<'tcx>,
74+
_location_table: &LocationTable,
7375
param_env: ty::ParamEnv<'gcx>,
7476
flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'cx, 'gcx, 'tcx>>,
7577
move_data: &MoveData<'tcx>,

0 commit comments

Comments
 (0)