3 use rustc_index::vec::{IndexVec, Idx};
6 /// This struct represents a patch to MIR, which can add
7 /// new statements and basic blocks and patch over block
9 pub struct MirPatch<'tcx> {
10 patch_map: IndexVec<BasicBlock, Option<TerminatorKind<'tcx>>>,
11 new_blocks: Vec<BasicBlockData<'tcx>>,
12 new_statements: Vec<(Location, StatementKind<'tcx>)>,
13 new_locals: Vec<LocalDecl<'tcx>>,
14 resume_block: BasicBlock,
16 make_nop: Vec<Location>,
19 impl<'tcx> MirPatch<'tcx> {
20 pub fn new(body: &Body<'tcx>) -> Self {
21 let mut result = MirPatch {
22 patch_map: IndexVec::from_elem(None, body.basic_blocks()),
24 new_statements: vec![],
26 next_local: body.local_decls.len(),
27 resume_block: START_BLOCK,
31 // make sure the MIR we create has a resume block. It is
32 // completely legal to convert jumps to the resume block
33 // to jumps to None, but we occasionally have to add
34 // instructions just before that.
36 let mut resume_block = None;
37 let mut resume_stmt_block = None;
38 for (bb, block) in body.basic_blocks().iter_enumerated() {
39 if let TerminatorKind::Resume = block.terminator().kind {
40 if block.statements.len() > 0 {
41 assert!(resume_stmt_block.is_none());
42 resume_stmt_block = Some(bb);
44 resume_block = Some(bb);
49 let resume_block = resume_block.unwrap_or_else(|| {
50 result.new_block(BasicBlockData {
52 terminator: Some(Terminator {
53 source_info: SourceInfo {
55 scope: OUTERMOST_SOURCE_SCOPE
57 kind: TerminatorKind::Resume
61 result.resume_block = resume_block;
62 if let Some(resume_stmt_block) = resume_stmt_block {
63 result.patch_terminator(resume_stmt_block, TerminatorKind::Goto {
70 pub fn resume_block(&self) -> BasicBlock {
74 pub fn is_patched(&self, bb: BasicBlock) -> bool {
75 self.patch_map[bb].is_some()
78 pub fn terminator_loc(&self, body: &Body<'tcx>, bb: BasicBlock) -> Location {
79 let offset = match bb.index().checked_sub(body.basic_blocks().len()) {
80 Some(index) => self.new_blocks[index].statements.len(),
81 None => body[bb].statements.len()
85 statement_index: offset
89 pub fn new_temp(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
90 let index = self.next_local;
92 self.new_locals.push(LocalDecl::new_temp(ty, span));
93 Local::new(index as usize)
96 pub fn new_internal(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
97 let index = self.next_local;
99 self.new_locals.push(LocalDecl::new_internal(ty, span));
100 Local::new(index as usize)
103 pub fn new_block(&mut self, data: BasicBlockData<'tcx>) -> BasicBlock {
104 let block = BasicBlock::new(self.patch_map.len());
105 debug!("MirPatch: new_block: {:?}: {:?}", block, data);
106 self.new_blocks.push(data);
107 self.patch_map.push(None);
111 pub fn patch_terminator(&mut self, block: BasicBlock, new: TerminatorKind<'tcx>) {
112 assert!(self.patch_map[block].is_none());
113 debug!("MirPatch: patch_terminator({:?}, {:?})", block, new);
114 self.patch_map[block] = Some(new);
117 pub fn add_statement(&mut self, loc: Location, stmt: StatementKind<'tcx>) {
118 debug!("MirPatch: add_statement({:?}, {:?})", loc, stmt);
119 self.new_statements.push((loc, stmt));
122 pub fn add_assign(&mut self, loc: Location, place: Place<'tcx>, rv: Rvalue<'tcx>) {
123 self.add_statement(loc, StatementKind::Assign(box(place, rv)));
126 pub fn make_nop(&mut self, loc: Location) {
127 self.make_nop.push(loc);
130 pub fn apply(self, body: &mut Body<'tcx>) {
131 debug!("MirPatch: make nops at: {:?}", self.make_nop);
132 for loc in self.make_nop {
133 body.make_statement_nop(loc);
135 debug!("MirPatch: {:?} new temps, starting from index {}: {:?}",
136 self.new_locals.len(), body.local_decls.len(), self.new_locals);
137 debug!("MirPatch: {} new blocks, starting from index {}",
138 self.new_blocks.len(), body.basic_blocks().len());
139 body.basic_blocks_mut().extend(self.new_blocks);
140 body.local_decls.extend(self.new_locals);
141 for (src, patch) in self.patch_map.into_iter_enumerated() {
142 if let Some(patch) = patch {
143 debug!("MirPatch: patching block {:?}", src);
144 body[src].terminator_mut().kind = patch;
148 let mut new_statements = self.new_statements;
149 new_statements.sort_by_key(|s| s.0);
152 let mut last_bb = START_BLOCK;
153 for (mut loc, stmt) in new_statements {
154 if loc.block != last_bb {
158 debug!("MirPatch: adding statement {:?} at loc {:?}+{}",
160 loc.statement_index += delta;
161 let source_info = Self::source_info_for_index(
162 &body[loc.block], loc
164 body[loc.block].statements.insert(
165 loc.statement_index, Statement {
173 pub fn source_info_for_index(data: &BasicBlockData<'_>, loc: Location) -> SourceInfo {
174 match data.statements.get(loc.statement_index) {
175 Some(stmt) => stmt.source_info,
176 None => data.terminator().source_info
180 pub fn source_info_for_location(&self, body: &Body<'_>, loc: Location) -> SourceInfo {
181 let data = match loc.block.index().checked_sub(body.basic_blocks().len()) {
182 Some(new) => &self.new_blocks[new],
183 None => &body[loc.block]
185 Self::source_info_for_index(data, loc)