Skip to content

Commit ab514c9

Browse files
committed
Pass UnwindAction to a couple of functions
In preparation for future unwinding support. Part of #1567
1 parent 420e44f commit ab514c9

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

src/abi/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
2020
use rustc_session::Session;
2121
use rustc_span::source_map::Spanned;
2222
use rustc_target::callconv::{Conv, FnAbi, PassMode};
23+
use smallvec::SmallVec;
2324

2425
use self::pass_mode::*;
2526
pub(crate) use self::returning::codegen_return;
@@ -384,6 +385,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
384385
args: &[Spanned<Operand<'tcx>>],
385386
destination: Place<'tcx>,
386387
target: Option<BasicBlock>,
388+
_unwind: UnwindAction,
387389
) {
388390
let func = codegen_operand(fx, func);
389391
let fn_sig = func.layout().ty.fn_sig(fx.tcx);
@@ -588,12 +590,14 @@ pub(crate) fn codegen_terminator_call<'tcx>(
588590
with_no_trimmed_paths!(fx.add_comment(nop_inst, format!("abi: {:?}", fn_abi)));
589591
}
590592

591-
match func_ref {
593+
let call_inst = match func_ref {
592594
CallTarget::Direct(func_ref) => fx.bcx.ins().call(func_ref, &call_args),
593595
CallTarget::Indirect(sig, func_ptr) => {
594596
fx.bcx.ins().call_indirect(sig, func_ptr, &call_args)
595597
}
596-
}
598+
};
599+
600+
fx.bcx.func.dfg.inst_results(call_inst).iter().copied().collect::<SmallVec<[Value; 2]>>()
597601
});
598602

599603
if let Some(dest) = target {
@@ -703,14 +707,17 @@ pub(crate) fn codegen_drop<'tcx>(
703707
source_info: mir::SourceInfo,
704708
drop_place: CPlace<'tcx>,
705709
target: BasicBlock,
710+
_unwind: UnwindAction,
706711
) {
707712
let ty = drop_place.layout().ty;
708713
let drop_instance = Instance::resolve_drop_in_place(fx.tcx, ty);
714+
let ret_block = fx.get_block(target);
709715

710716
if let ty::InstanceKind::DropGlue(_, None) | ty::InstanceKind::AsyncDropGlueCtorShim(_, None) =
711717
drop_instance.def
712718
{
713719
// we don't actually need to drop anything
720+
fx.bcx.ins().jump(ret_block, &[]);
714721
} else {
715722
match ty.kind() {
716723
ty::Dynamic(_, _, ty::Dyn) => {
@@ -747,7 +754,9 @@ pub(crate) fn codegen_drop<'tcx>(
747754

748755
let sig = clif_sig_from_fn_abi(fx.tcx, fx.target_config.default_call_conv, &fn_abi);
749756
let sig = fx.bcx.import_signature(sig);
757+
// FIXME implement cleanup on exceptions
750758
fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
759+
fx.bcx.ins().jump(ret_block, &[]);
751760
}
752761
ty::Dynamic(_, _, ty::DynStar) => {
753762
// IN THIS ARM, WE HAVE:
@@ -791,6 +800,8 @@ pub(crate) fn codegen_drop<'tcx>(
791800
let sig = clif_sig_from_fn_abi(fx.tcx, fx.target_config.default_call_conv, &fn_abi);
792801
let sig = fx.bcx.import_signature(sig);
793802
fx.bcx.ins().call_indirect(sig, drop_fn, &[data]);
803+
// FIXME implement cleanup on exceptions
804+
fx.bcx.ins().jump(ret_block, &[]);
794805
}
795806
_ => {
796807
assert!(!matches!(drop_instance.def, InstanceKind::Virtual(_, _)));
@@ -816,10 +827,9 @@ pub(crate) fn codegen_drop<'tcx>(
816827

817828
let func_ref = fx.get_function_ref(drop_instance);
818829
fx.bcx.ins().call(func_ref, &call_args);
830+
// FIXME implement cleanup on exceptions
831+
fx.bcx.ins().jump(ret_block, &[]);
819832
}
820833
}
821834
}
822-
823-
let target_block = fx.get_block(target);
824-
fx.bcx.ins().jump(target_block, &[]);
825835
}

src/abi/returning.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
4646
fx: &mut FunctionCx<'_, '_, 'tcx>,
4747
ret_arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
4848
ret_place: CPlace<'tcx>,
49-
f: impl FnOnce(&mut FunctionCx<'_, '_, 'tcx>, Option<Value>) -> Inst,
49+
f: impl FnOnce(&mut FunctionCx<'_, '_, 'tcx>, Option<Value>) -> SmallVec<[Value; 2]>,
5050
) {
5151
let (ret_temp_place, return_ptr) = match ret_arg_abi.mode {
5252
PassMode::Ignore => (None, None),
@@ -67,23 +67,21 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
6767
PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast { .. } => (None, None),
6868
};
6969

70-
let call_inst = f(fx, return_ptr);
70+
let results = f(fx, return_ptr);
7171

7272
match ret_arg_abi.mode {
7373
PassMode::Ignore => {}
7474
PassMode::Direct(_) => {
75-
let ret_val = fx.bcx.inst_results(call_inst)[0];
75+
let ret_val = results[0];
7676
ret_place.write_cvalue(fx, CValue::by_val(ret_val, ret_arg_abi.layout));
7777
}
7878
PassMode::Pair(_, _) => {
79-
let ret_val_a = fx.bcx.inst_results(call_inst)[0];
80-
let ret_val_b = fx.bcx.inst_results(call_inst)[1];
79+
let ret_val_a = results[0];
80+
let ret_val_b = results[1];
8181
ret_place
8282
.write_cvalue(fx, CValue::by_val_pair(ret_val_a, ret_val_b, ret_arg_abi.layout));
8383
}
8484
PassMode::Cast { ref cast, .. } => {
85-
let results =
86-
fx.bcx.inst_results(call_inst).iter().copied().collect::<SmallVec<[Value; 2]>>();
8785
let result =
8886
super::pass_mode::from_casted_value(fx, &results, ret_place.layout(), cast);
8987
ret_place.write_cvalue(fx, result);

src/base.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
372372
TerminatorKind::Return => {
373373
crate::abi::codegen_return(fx);
374374
}
375-
TerminatorKind::Assert { cond, expected, msg, target, unwind: _ } => {
375+
TerminatorKind::Assert { cond, expected, msg, target, unwind } => {
376376
if !fx.tcx.sess.overflow_checks() && msg.is_optional_overflow_check() {
377377
let target = fx.get_block(*target);
378378
fx.bcx.ins().jump(target, &[]);
@@ -402,6 +402,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
402402
fx,
403403
rustc_hir::LangItem::PanicBoundsCheck,
404404
&[index, len, location],
405+
*unwind,
405406
Some(source_info.span),
406407
);
407408
}
@@ -414,6 +415,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
414415
fx,
415416
rustc_hir::LangItem::PanicMisalignedPointerDereference,
416417
&[required, found, location],
418+
*unwind,
417419
Some(source_info.span),
418420
);
419421
}
@@ -424,6 +426,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
424426
fx,
425427
rustc_hir::LangItem::PanicNullPointerDereference,
426428
&[location],
429+
*unwind,
427430
Some(source_info.span),
428431
)
429432
}
@@ -434,6 +437,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
434437
fx,
435438
msg.panic_function(),
436439
&[location],
440+
*unwind,
437441
Some(source_info.span),
438442
);
439443
}
@@ -492,7 +496,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
492496
destination,
493497
target,
494498
fn_span,
495-
unwind: _,
499+
unwind,
496500
call_source: _,
497501
} => {
498502
fx.tcx.prof.generic_activity("codegen call").run(|| {
@@ -503,6 +507,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
503507
args,
504508
*destination,
505509
*target,
510+
*unwind,
506511
)
507512
});
508513
}
@@ -565,9 +570,9 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
565570
| TerminatorKind::CoroutineDrop => {
566571
bug!("shouldn't exist at codegen {:?}", bb_data.terminator());
567572
}
568-
TerminatorKind::Drop { place, target, unwind: _, replace: _ } => {
573+
TerminatorKind::Drop { place, target, unwind, replace: _ } => {
569574
let drop_place = codegen_place(fx, *place);
570-
crate::abi::codegen_drop(fx, source_info, drop_place, *target);
575+
crate::abi::codegen_drop(fx, source_info, drop_place, *target, *unwind);
571576
}
572577
};
573578
}
@@ -1089,7 +1094,13 @@ pub(crate) fn codegen_panic_nounwind<'tcx>(
10891094
let msg_len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap());
10901095
let args = [msg_ptr, msg_len];
10911096

1092-
codegen_panic_inner(fx, rustc_hir::LangItem::PanicNounwind, &args, span);
1097+
codegen_panic_inner(
1098+
fx,
1099+
rustc_hir::LangItem::PanicNounwind,
1100+
&args,
1101+
UnwindAction::Terminate(UnwindTerminateReason::Abi),
1102+
span,
1103+
);
10931104
}
10941105

10951106
pub(crate) fn codegen_unwind_terminate<'tcx>(
@@ -1099,13 +1110,20 @@ pub(crate) fn codegen_unwind_terminate<'tcx>(
10991110
) {
11001111
let args = [];
11011112

1102-
codegen_panic_inner(fx, reason.lang_item(), &args, Some(source_info.span));
1113+
codegen_panic_inner(
1114+
fx,
1115+
reason.lang_item(),
1116+
&args,
1117+
UnwindAction::Terminate(UnwindTerminateReason::Abi),
1118+
Some(source_info.span),
1119+
);
11031120
}
11041121

11051122
fn codegen_panic_inner<'tcx>(
11061123
fx: &mut FunctionCx<'_, '_, 'tcx>,
11071124
lang_item: rustc_hir::LangItem,
11081125
args: &[Value],
1126+
_unwind: UnwindAction,
11091127
span: Option<Span>,
11101128
) {
11111129
fx.bcx.set_cold_block(fx.bcx.current_block().unwrap());
@@ -1121,6 +1139,8 @@ fn codegen_panic_inner<'tcx>(
11211139

11221140
let symbol_name = fx.tcx.symbol_name(instance).name;
11231141

1142+
// FIXME implement cleanup on exceptions
1143+
11241144
fx.lib_call(
11251145
symbol_name,
11261146
args.iter().map(|&arg| AbiParam::new(fx.bcx.func.dfg.value_type(arg))).collect(),

0 commit comments

Comments
 (0)