Skip to content

Commit

Permalink
Rollup merge of rust-lang#110558 - spastorino:smir-call-terminator, r…
Browse files Browse the repository at this point in the history
…=oli-obk

Add Call terminator to SMIR

This adds internal MIR `TerminatorKind::Call` to SMIR `Terminator::Call` conversion.

r? `@oli-obk`
  • Loading branch information
matthiaskrgr authored Apr 20, 2023
2 parents 7dc211f + 2f50334 commit 4624616
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
22 changes: 21 additions & 1 deletion compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}

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(
terminator: &rustc_middle::mir::Terminator<'_>,
) -> stable_mir::mir::Terminator {
Expand All @@ -151,7 +163,15 @@ fn rustc_terminator_to_terminator(
Return => Terminator::Return,
Unreachable => Terminator::Unreachable,
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!(),
Yield { .. } => todo!(),
GeneratorDrop => todo!(),
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_smir/src/stable_mir/mir/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum Terminator {
args: Vec<Operand>,
destination: Place,
target: Option<usize>,
cleanup: Option<usize>,
unwind: UnwindAction,
},
Assert {
cond: Operand,
Expand All @@ -44,6 +44,14 @@ pub enum Terminator {
},
}

#[derive(Clone, Debug)]
pub enum UnwindAction {
Continue,
Unreachable,
Terminate,
Cleanup(usize),
}

#[derive(Clone, Debug)]
pub enum Statement {
Assign(Place, Operand),
Expand Down
10 changes: 9 additions & 1 deletion tests/ui-fulldeps/stable-mir/crate-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {

// Find items in the local crate.
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());

// Find the `std` crate.
Expand All @@ -52,6 +51,15 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
stable_mir::mir::Terminator::Return => {}
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.
Expand Down

0 comments on commit 4624616

Please sign in to comment.