2016-05-17 02:26:18 +03:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use rustc::ty::Ty;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::*;
|
2016-06-07 17:28:36 +03:00
|
|
|
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
|
2016-05-17 02:26:18 +03:00
|
|
|
|
|
|
|
/// This struct represents a patch to MIR, which can add
|
|
|
|
/// new statements and basic blocks and patch over block
|
|
|
|
/// terminators.
|
|
|
|
pub struct MirPatch<'tcx> {
|
2016-06-07 17:28:36 +03:00
|
|
|
patch_map: IndexVec<BasicBlock, Option<TerminatorKind<'tcx>>>,
|
2016-05-17 02:26:18 +03:00
|
|
|
new_blocks: Vec<BasicBlockData<'tcx>>,
|
|
|
|
new_statements: Vec<(Location, StatementKind<'tcx>)>,
|
2016-09-25 01:38:27 +02:00
|
|
|
new_locals: Vec<LocalDecl<'tcx>>,
|
2016-05-17 02:26:18 +03:00
|
|
|
resume_block: BasicBlock,
|
2016-09-25 01:38:27 +02:00
|
|
|
next_local: usize,
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MirPatch<'tcx> {
|
|
|
|
pub fn new(mir: &Mir<'tcx>) -> Self {
|
|
|
|
let mut result = MirPatch {
|
2016-06-07 21:20:50 +03:00
|
|
|
patch_map: IndexVec::from_elem(None, mir.basic_blocks()),
|
2016-05-17 02:26:18 +03:00
|
|
|
new_blocks: vec![],
|
|
|
|
new_statements: vec![],
|
2016-09-25 01:38:27 +02:00
|
|
|
new_locals: vec![],
|
|
|
|
next_local: mir.local_decls.len(),
|
2016-05-17 02:26:18 +03:00
|
|
|
resume_block: START_BLOCK
|
|
|
|
};
|
|
|
|
|
|
|
|
// make sure the MIR we create has a resume block. It is
|
|
|
|
// completely legal to convert jumps to the resume block
|
|
|
|
// to jumps to None, but we occasionally have to add
|
|
|
|
// instructions just before that.
|
|
|
|
|
|
|
|
let mut resume_block = None;
|
|
|
|
let mut resume_stmt_block = None;
|
2016-06-07 21:20:50 +03:00
|
|
|
for (bb, block) in mir.basic_blocks().iter_enumerated() {
|
|
|
|
if let TerminatorKind::Resume = block.terminator().kind {
|
|
|
|
if block.statements.len() > 0 {
|
|
|
|
resume_stmt_block = Some(bb);
|
2016-05-17 02:26:18 +03:00
|
|
|
} else {
|
2016-06-07 21:20:50 +03:00
|
|
|
resume_block = Some(bb);
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let resume_block = resume_block.unwrap_or_else(|| {
|
|
|
|
result.new_block(BasicBlockData {
|
|
|
|
statements: vec![],
|
|
|
|
terminator: Some(Terminator {
|
2016-06-07 19:21:56 +03:00
|
|
|
source_info: SourceInfo {
|
|
|
|
span: mir.span,
|
|
|
|
scope: ARGUMENT_VISIBILITY_SCOPE
|
|
|
|
},
|
2016-05-17 02:26:18 +03:00
|
|
|
kind: TerminatorKind::Resume
|
|
|
|
}),
|
|
|
|
is_cleanup: true
|
|
|
|
})});
|
|
|
|
result.resume_block = resume_block;
|
|
|
|
if let Some(resume_stmt_block) = resume_stmt_block {
|
|
|
|
result.patch_terminator(resume_stmt_block, TerminatorKind::Goto {
|
|
|
|
target: resume_block
|
|
|
|
});
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resume_block(&self) -> BasicBlock {
|
|
|
|
self.resume_block
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_patched(&self, bb: BasicBlock) -> bool {
|
2016-06-07 17:28:36 +03:00
|
|
|
self.patch_map[bb].is_some()
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn terminator_loc(&self, mir: &Mir<'tcx>, bb: BasicBlock) -> Location {
|
2016-06-07 21:20:50 +03:00
|
|
|
let offset = match bb.index().checked_sub(mir.basic_blocks().len()) {
|
2016-05-17 02:26:18 +03:00
|
|
|
Some(index) => self.new_blocks[index].statements.len(),
|
2016-06-07 21:20:50 +03:00
|
|
|
None => mir[bb].statements.len()
|
2016-05-17 02:26:18 +03:00
|
|
|
};
|
|
|
|
Location {
|
|
|
|
block: bb,
|
2016-08-08 18:46:06 -07:00
|
|
|
statement_index: offset
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
pub fn new_temp(&mut self, ty: Ty<'tcx>) -> Local {
|
|
|
|
let index = self.next_local;
|
|
|
|
self.next_local += 1;
|
|
|
|
self.new_locals.push(LocalDecl::new_temp(ty));
|
|
|
|
Local::new(index as usize)
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_block(&mut self, data: BasicBlockData<'tcx>) -> BasicBlock {
|
|
|
|
let block = BasicBlock::new(self.patch_map.len());
|
|
|
|
debug!("MirPatch: new_block: {:?}: {:?}", block, data);
|
|
|
|
self.new_blocks.push(data);
|
|
|
|
self.patch_map.push(None);
|
|
|
|
block
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn patch_terminator(&mut self, block: BasicBlock, new: TerminatorKind<'tcx>) {
|
2016-06-07 17:28:36 +03:00
|
|
|
assert!(self.patch_map[block].is_none());
|
2016-05-17 02:26:18 +03:00
|
|
|
debug!("MirPatch: patch_terminator({:?}, {:?})", block, new);
|
2016-06-07 17:28:36 +03:00
|
|
|
self.patch_map[block] = Some(new);
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_statement(&mut self, loc: Location, stmt: StatementKind<'tcx>) {
|
|
|
|
debug!("MirPatch: add_statement({:?}, {:?})", loc, stmt);
|
|
|
|
self.new_statements.push((loc, stmt));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_assign(&mut self, loc: Location, lv: Lvalue<'tcx>, rv: Rvalue<'tcx>) {
|
|
|
|
self.add_statement(loc, StatementKind::Assign(lv, rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn apply(self, mir: &mut Mir<'tcx>) {
|
|
|
|
debug!("MirPatch: {:?} new temps, starting from index {}: {:?}",
|
2016-09-25 01:38:27 +02:00
|
|
|
self.new_locals.len(), mir.local_decls.len(), self.new_locals);
|
2016-05-17 02:26:18 +03:00
|
|
|
debug!("MirPatch: {} new blocks, starting from index {}",
|
2016-06-07 21:20:50 +03:00
|
|
|
self.new_blocks.len(), mir.basic_blocks().len());
|
|
|
|
mir.basic_blocks_mut().extend(self.new_blocks);
|
2016-09-25 01:38:27 +02:00
|
|
|
mir.local_decls.extend(self.new_locals);
|
2016-06-07 17:28:36 +03:00
|
|
|
for (src, patch) in self.patch_map.into_iter_enumerated() {
|
2016-05-17 02:26:18 +03:00
|
|
|
if let Some(patch) = patch {
|
|
|
|
debug!("MirPatch: patching block {:?}", src);
|
2016-06-07 21:20:50 +03:00
|
|
|
mir[src].terminator_mut().kind = patch;
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut new_statements = self.new_statements;
|
|
|
|
new_statements.sort_by(|u,v| u.0.cmp(&v.0));
|
|
|
|
|
|
|
|
let mut delta = 0;
|
|
|
|
let mut last_bb = START_BLOCK;
|
|
|
|
for (mut loc, stmt) in new_statements {
|
|
|
|
if loc.block != last_bb {
|
|
|
|
delta = 0;
|
|
|
|
last_bb = loc.block;
|
|
|
|
}
|
|
|
|
debug!("MirPatch: adding statement {:?} at loc {:?}+{}",
|
|
|
|
stmt, loc, delta);
|
2016-08-08 18:46:06 -07:00
|
|
|
loc.statement_index += delta;
|
2016-06-07 19:21:56 +03:00
|
|
|
let source_info = Self::source_info_for_index(
|
2016-06-07 21:20:50 +03:00
|
|
|
&mir[loc.block], loc
|
2016-05-17 02:26:18 +03:00
|
|
|
);
|
2016-06-07 21:20:50 +03:00
|
|
|
mir[loc.block].statements.insert(
|
2016-08-08 18:46:06 -07:00
|
|
|
loc.statement_index, Statement {
|
2016-06-07 19:21:56 +03:00
|
|
|
source_info: source_info,
|
2016-05-17 02:26:18 +03:00
|
|
|
kind: stmt
|
|
|
|
});
|
|
|
|
delta += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
pub fn source_info_for_index(data: &BasicBlockData, loc: Location) -> SourceInfo {
|
2016-08-08 18:46:06 -07:00
|
|
|
match data.statements.get(loc.statement_index) {
|
2016-06-07 19:21:56 +03:00
|
|
|
Some(stmt) => stmt.source_info,
|
|
|
|
None => data.terminator().source_info
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
pub fn source_info_for_location(&self, mir: &Mir, loc: Location) -> SourceInfo {
|
2016-06-07 21:20:50 +03:00
|
|
|
let data = match loc.block.index().checked_sub(mir.basic_blocks().len()) {
|
2016-05-17 02:26:18 +03:00
|
|
|
Some(new) => &self.new_blocks[new],
|
2016-06-07 21:20:50 +03:00
|
|
|
None => &mir[loc.block]
|
2016-05-17 02:26:18 +03:00
|
|
|
};
|
2016-06-07 19:21:56 +03:00
|
|
|
Self::source_info_for_index(data, loc)
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
}
|