Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Call terminator to SMIR #110558

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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