Add Call terminator to SMIR

This commit is contained in:
Santiago Pastorino 2023-04-19 17:10:35 -03:00
parent 7e23d180c1
commit 2f503345b4
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
3 changed files with 39 additions and 3 deletions

View File

@ -128,6 +128,18 @@ fn rustc_place_to_place(place: &rustc_middle::mir::Place<'_>) -> stable_mir::mir
stable_mir::mir::Place { local: place.local.as_usize() } stable_mir::mir::Place { local: place.local.as_usize() }
} }
fn rustc_unwind_to_unwind(
unwind: &rustc_middle::mir::UnwindAction,
) -> stable_mir::mir::UnwindAction {
use rustc_middle::mir::UnwindAction;
match unwind {
UnwindAction::Continue => stable_mir::mir::UnwindAction::Continue,
UnwindAction::Unreachable => stable_mir::mir::UnwindAction::Unreachable,
UnwindAction::Terminate => stable_mir::mir::UnwindAction::Terminate,
UnwindAction::Cleanup(bb) => stable_mir::mir::UnwindAction::Cleanup(bb.as_usize()),
}
}
fn rustc_terminator_to_terminator( fn rustc_terminator_to_terminator(
terminator: &rustc_middle::mir::Terminator<'_>, terminator: &rustc_middle::mir::Terminator<'_>,
) -> stable_mir::mir::Terminator { ) -> stable_mir::mir::Terminator {
@ -151,7 +163,15 @@ fn rustc_terminator_to_terminator(
Return => Terminator::Return, Return => Terminator::Return,
Unreachable => Terminator::Unreachable, Unreachable => Terminator::Unreachable,
Drop { .. } => todo!(), Drop { .. } => todo!(),
Call { .. } => todo!(), Call { func, args, destination, target, unwind, from_hir_call: _, fn_span: _ } => {
Terminator::Call {
func: rustc_op_to_op(func),
args: args.iter().map(|arg| rustc_op_to_op(arg)).collect(),
destination: rustc_place_to_place(destination),
target: target.map(|t| t.as_usize()),
unwind: rustc_unwind_to_unwind(unwind),
}
}
Assert { .. } => todo!(), Assert { .. } => todo!(),
Yield { .. } => todo!(), Yield { .. } => todo!(),
GeneratorDrop => todo!(), GeneratorDrop => todo!(),

View File

@ -33,7 +33,7 @@ pub enum Terminator {
args: Vec<Operand>, args: Vec<Operand>,
destination: Place, destination: Place,
target: Option<usize>, target: Option<usize>,
cleanup: Option<usize>, unwind: UnwindAction,
}, },
Assert { Assert {
cond: Operand, cond: Operand,
@ -44,6 +44,14 @@ pub enum Terminator {
}, },
} }
#[derive(Clone, Debug)]
pub enum UnwindAction {
Continue,
Unreachable,
Terminate,
Cleanup(usize),
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Statement { pub enum Statement {
Assign(Place, Operand), Assign(Place, Operand),

View File

@ -33,7 +33,6 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
// Find items in the local crate. // Find items in the local crate.
let items = stable_mir::all_local_items(); let items = stable_mir::all_local_items();
assert!(get_item(tcx, &items, (DefKind::Fn, "foo_bar")).is_some());
assert!(get_item(tcx, &items, (DefKind::Fn, "foo::bar")).is_some()); assert!(get_item(tcx, &items, (DefKind::Fn, "foo::bar")).is_some());
// Find the `std` crate. // Find the `std` crate.
@ -52,6 +51,15 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
stable_mir::mir::Terminator::Return => {} stable_mir::mir::Terminator::Return => {}
other => panic!("{other:?}"), other => panic!("{other:?}"),
} }
let foo_bar = get_item(tcx, &items, (DefKind::Fn, "foo_bar")).unwrap();
let body = foo_bar.body();
assert_eq!(body.blocks.len(), 4);
let block = &body.blocks[0];
match &block.terminator {
stable_mir::mir::Terminator::Call { .. } => {}
other => panic!("{other:?}"),
}
} }
// Use internal API to find a function in a crate. // Use internal API to find a function in a crate.