Skip to content

Commit 85b2dcd

Browse files
committed
Refactor: Move the logic for handling the cleanup place unpacking out of MirFunction.
In order to handle all possible cases of `UnwindAction`, we need to decide outside of `MirFunction` where the cleanup place should point to. Now getting the place references for a function call occurs in the `Translator` and the `MirFunction` just performs simplers tasts without any control flow.
1 parent 9ae2c01 commit 85b2dcd

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

src/translator.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,23 @@ impl<'tcx> Translator<'tcx> {
209209
return;
210210
};
211211

212-
let place_refs_for_function_call =
213-
current_function.get_place_refs_for_function_call(return_block, unwind, &mut self.net);
212+
let start_place = current_function.get_start_place_for_function_call();
213+
let end_place =
214+
current_function.get_end_place_for_function_call(return_block, &mut self.net);
215+
let cleanup_place = if let rustc_middle::mir::UnwindAction::Cleanup(cleanup_block) = unwind
216+
{
217+
Some(current_function.get_end_place_for_function_call(cleanup_block, &mut self.net))
218+
} else {
219+
None
220+
};
214221

215222
let function_call = FunctionCall::new(function_def_id, self.tcx);
216223
self.start_function_call(
217224
&function_call,
218225
function_def_id,
219226
args,
220227
destination,
221-
place_refs_for_function_call,
228+
(start_place, end_place, cleanup_place),
222229
);
223230
}
224231

src/translator/mir_function.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ mod statement;
1818
mod terminator;
1919

2020
use crate::data_structures::petri_net_interface::{PetriNet, PlaceRef};
21-
use crate::translator::function_call::FunctionPlaces;
2221
use basic_block::BasicBlock;
2322
use std::collections::HashMap;
2423

@@ -192,41 +191,22 @@ impl<'tcx> MirFunction<'tcx> {
192191
}
193192

194193
/// Returns the start place for a function call, i.e., the end place of the current active block.
194+
/// Clones the place reference to simplify using it.
195195
pub fn get_start_place_for_function_call(&self) -> PlaceRef {
196196
let active_block = self.get_active_block();
197197
active_block.end_place.clone()
198198
}
199199

200-
/// Returns a 3-tuple of the form `(start_place, end_place, cleanup_place)`
201-
/// where:
202-
/// - `start_place` is the end place of the currently active basic block.
203-
/// - `end_place` is the start place of the block with the given block number,
204-
/// which is added to the function if it is not present already.
205-
/// - `cleanup_place` (optional) is the place for cleanups in case the function call unwinds.
206-
/// Usually foreign function calls have this.
207-
///
208-
/// Clones the references to simplify using them.
209-
pub fn get_place_refs_for_function_call(
200+
/// Returns the end place for a function call, i.e., the start place of the given block number.
201+
/// Adds the basic block it if it is not present already.
202+
/// Clones the place reference to simplify using it.
203+
pub fn get_end_place_for_function_call(
210204
&mut self,
211205
block_number: rustc_middle::mir::BasicBlock,
212-
unwind: rustc_middle::mir::UnwindAction,
213206
net: &mut PetriNet,
214-
) -> FunctionPlaces {
215-
let active_block = self.get_active_block();
216-
let start_place = active_block.end_place.clone();
217-
207+
) -> PlaceRef {
218208
let return_block = self.get_or_add_basic_block(block_number, net);
219-
let end_place = return_block.start_place.clone();
220-
221-
let cleanup_place =
222-
if let rustc_middle::mir::UnwindAction::Cleanup(cleanup_block_number) = unwind {
223-
let cleanup_block = self.get_or_add_basic_block(cleanup_block_number, net);
224-
Some(cleanup_block.start_place.clone())
225-
} else {
226-
None
227-
};
228-
229-
(start_place, end_place, cleanup_place)
209+
return_block.start_place.clone()
230210
}
231211

232212
/// Adds a statement to the active basic block.

0 commit comments

Comments
 (0)