1 // Copyright 2016 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.
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.
13 use rustc_data_structures::indexed_vec::{IndexVec, Idx};
16 /// This struct represents a patch to MIR, which can add
17 /// new statements and basic blocks and patch over block
19 pub struct MirPatch<'tcx> {
20 patch_map: IndexVec<BasicBlock, Option<TerminatorKind<'tcx>>>,
21 new_blocks: Vec<BasicBlockData<'tcx>>,
22 new_statements: Vec<(Location, StatementKind<'tcx>)>,
23 new_locals: Vec<LocalDecl<'tcx>>,
24 resume_block: BasicBlock,
26 make_nop: Vec<Location>,
29 impl<'tcx> MirPatch<'tcx> {
30 pub fn new(mir: &Mir<'tcx>) -> Self {
31 let mut result = MirPatch {
32 patch_map: IndexVec::from_elem(None, mir.basic_blocks()),
34 new_statements: vec![],
36 next_local: mir.local_decls.len(),
37 resume_block: START_BLOCK,
41 // make sure the MIR we create has a resume block. It is
42 // completely legal to convert jumps to the resume block
43 // to jumps to None, but we occasionally have to add
44 // instructions just before that.
46 let mut resume_block = None;
47 let mut resume_stmt_block = None;
48 for (bb, block) in mir.basic_blocks().iter_enumerated() {
49 if let TerminatorKind::Resume = block.terminator().kind {
50 if block.statements.len() > 0 {
51 assert!(resume_stmt_block.is_none());
52 resume_stmt_block = Some(bb);
54 resume_block = Some(bb);
59 let resume_block = resume_block.unwrap_or_else(|| {
60 result.new_block(BasicBlockData {
62 terminator: Some(Terminator {
63 source_info: SourceInfo {
65 scope: OUTERMOST_SOURCE_SCOPE
67 kind: TerminatorKind::Resume
71 result.resume_block = resume_block;
72 if let Some(resume_stmt_block) = resume_stmt_block {
73 result.patch_terminator(resume_stmt_block, TerminatorKind::Goto {
80 pub fn resume_block(&self) -> BasicBlock {
84 pub fn is_patched(&self, bb: BasicBlock) -> bool {
85 self.patch_map[bb].is_some()
88 pub fn terminator_loc(&self, mir: &Mir<'tcx>, bb: BasicBlock) -> Location {
89 let offset = match bb.index().checked_sub(mir.basic_blocks().len()) {
90 Some(index) => self.new_blocks[index].statements.len(),
91 None => mir[bb].statements.len()
95 statement_index: offset
99 pub fn new_temp(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
100 let index = self.next_local;
101 self.next_local += 1;
102 self.new_locals.push(LocalDecl::new_temp(ty, span));
103 Local::new(index as usize)
106 pub fn new_internal(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
107 let index = self.next_local;
108 self.next_local += 1;
109 self.new_locals.push(LocalDecl::new_internal(ty, span));
110 Local::new(index as usize)
113 pub fn new_block(&mut self, data: BasicBlockData<'tcx>) -> BasicBlock {
114 let block = BasicBlock::new(self.patch_map.len());
115 debug!("MirPatch: new_block: {:?}: {:?}", block, data);
116 self.new_blocks.push(data);
117 self.patch_map.push(None);
121 pub fn patch_terminator(&mut self, block: BasicBlock, new: TerminatorKind<'tcx>) {
122 assert!(self.patch_map[block].is_none());
123 debug!("MirPatch: patch_terminator({:?}, {:?})", block, new);
124 self.patch_map[block] = Some(new);
127 pub fn add_statement(&mut self, loc: Location, stmt: StatementKind<'tcx>) {
128 debug!("MirPatch: add_statement({:?}, {:?})", loc, stmt);
129 self.new_statements.push((loc, stmt));
132 pub fn add_assign(&mut self, loc: Location, place: Place<'tcx>, rv: Rvalue<'tcx>) {
133 self.add_statement(loc, StatementKind::Assign(place, rv));
136 pub fn make_nop(&mut self, loc: Location) {
137 self.make_nop.push(loc);
140 pub fn apply(self, mir: &mut Mir<'tcx>) {
141 debug!("MirPatch: make nops at: {:?}", self.make_nop);
142 for loc in self.make_nop {
143 mir.make_statement_nop(loc);
145 debug!("MirPatch: {:?} new temps, starting from index {}: {:?}",
146 self.new_locals.len(), mir.local_decls.len(), self.new_locals);
147 debug!("MirPatch: {} new blocks, starting from index {}",
148 self.new_blocks.len(), mir.basic_blocks().len());
149 mir.basic_blocks_mut().extend(self.new_blocks);
150 mir.local_decls.extend(self.new_locals);
151 for (src, patch) in self.patch_map.into_iter_enumerated() {
152 if let Some(patch) = patch {
153 debug!("MirPatch: patching block {:?}", src);
154 mir[src].terminator_mut().kind = patch;
158 let mut new_statements = self.new_statements;
159 new_statements.sort_by(|u,v| u.0.cmp(&v.0));
162 let mut last_bb = START_BLOCK;
163 for (mut loc, stmt) in new_statements {
164 if loc.block != last_bb {
168 debug!("MirPatch: adding statement {:?} at loc {:?}+{}",
170 loc.statement_index += delta;
171 let source_info = Self::source_info_for_index(
174 mir[loc.block].statements.insert(
175 loc.statement_index, Statement {
183 pub fn source_info_for_index(data: &BasicBlockData, loc: Location) -> SourceInfo {
184 match data.statements.get(loc.statement_index) {
185 Some(stmt) => stmt.source_info,
186 None => data.terminator().source_info
190 pub fn source_info_for_location(&self, mir: &Mir, loc: Location) -> SourceInfo {
191 let data = match loc.block.index().checked_sub(mir.basic_blocks().len()) {
192 Some(new) => &self.new_blocks[new],
193 None => &mir[loc.block]
195 Self::source_info_for_index(data, loc)