Skip to content

Commit fa6480e

Browse files
committed
Remove most trap functions and remove all trapnz usages
1 parent 526553e commit fa6480e

File tree

8 files changed

+45
-60
lines changed

8 files changed

+45
-60
lines changed

src/base.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ pub(crate) fn codegen_fn<'tcx>(
9090
if !crate::constant::check_constants(&mut fx) {
9191
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
9292
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
93-
crate::trap::trap_unreachable(&mut fx, "compilation should have been aborted");
93+
// compilation should have been aborted
94+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
9495
} else if arg_uninhabited {
9596
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
9697
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
@@ -457,17 +458,8 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) {
457458
template,
458459
operands,
459460
*options,
461+
*destination,
460462
);
461-
462-
match *destination {
463-
Some(destination) => {
464-
let destination_block = fx.get_block(destination);
465-
fx.bcx.ins().jump(destination_block, &[]);
466-
}
467-
None => {
468-
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
469-
}
470-
}
471463
}
472464
TerminatorKind::Resume | TerminatorKind::Abort => {
473465
// FIXME implement unwinding
@@ -711,9 +703,7 @@ fn codegen_stmt<'tcx>(
711703
Rvalue::Discriminant(place) => {
712704
let place = codegen_place(fx, place);
713705
let value = place.to_cvalue(fx);
714-
let discr =
715-
crate::discriminant::codegen_get_discriminant(fx, value, dest_layout);
716-
lval.write_cvalue(fx, discr);
706+
crate::discriminant::codegen_get_discriminant(fx, lval, value, dest_layout);
717707
}
718708
Rvalue::Repeat(ref operand, times) => {
719709
let operand = codegen_operand(fx, operand);

src/discriminant.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,14 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
6262

6363
pub(crate) fn codegen_get_discriminant<'tcx>(
6464
fx: &mut FunctionCx<'_, '_, 'tcx>,
65+
dest: CPlace<'tcx>,
6566
value: CValue<'tcx>,
6667
dest_layout: TyAndLayout<'tcx>,
67-
) -> CValue<'tcx> {
68+
) {
6869
let layout = value.layout();
6970

70-
if layout.abi == Abi::Uninhabited {
71-
let true_ = fx.bcx.ins().iconst(types::I32, 1);
72-
fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached);
73-
// Return a dummy value
74-
return CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout);
71+
if layout.abi.is_uninhabited() {
72+
return;
7573
}
7674

7775
let (tag_scalar, tag_field, tag_encoding) = match &layout.variants {
@@ -89,7 +87,9 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
8987
} else {
9088
ty::ScalarInt::try_from_uint(discr_val, dest_layout.size).unwrap()
9189
};
92-
return CValue::const_val(fx, dest_layout, discr_val);
90+
let res = CValue::const_val(fx, dest_layout, discr_val);
91+
dest.write_cvalue(fx, res);
92+
return;
9393
}
9494
Variants::Multiple { tag, tag_field, tag_encoding, variants: _ } => {
9595
(tag, *tag_field, tag_encoding)
@@ -110,7 +110,8 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
110110
_ => false,
111111
};
112112
let val = clif_intcast(fx, tag, cast_to, signed);
113-
CValue::by_val(val, dest_layout)
113+
let res = CValue::by_val(val, dest_layout);
114+
dest.write_cvalue(fx, res);
114115
}
115116
TagEncoding::Niche { dataful_variant, ref niche_variants, niche_start } => {
116117
// Rebase from niche values to discriminants, and check
@@ -170,7 +171,8 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
170171

171172
let dataful_variant = fx.bcx.ins().iconst(cast_to, i64::from(dataful_variant.as_u32()));
172173
let discr = fx.bcx.ins().select(is_niche, niche_discr, dataful_variant);
173-
CValue::by_val(discr, dest_layout)
174+
let res = CValue::by_val(discr, dest_layout);
175+
dest.write_cvalue(fx, res);
174176
}
175177
}
176178
}

src/inline_asm.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ pub(crate) fn codegen_inline_asm<'tcx>(
1515
template: &[InlineAsmTemplatePiece],
1616
operands: &[InlineAsmOperand<'tcx>],
1717
options: InlineAsmOptions,
18+
destination: Option<mir::BasicBlock>,
1819
) {
1920
// FIXME add .eh_frame unwind info directives
2021

2122
if !template.is_empty() {
2223
if template[0] == InlineAsmTemplatePiece::String("int $$0x29".to_string()) {
23-
let true_ = fx.bcx.ins().iconst(types::I32, 1);
24-
fx.bcx.ins().trapnz(true_, TrapCode::User(1));
24+
fx.bcx.ins().trap(TrapCode::User(1));
2525
return;
2626
} else if template[0] == InlineAsmTemplatePiece::String("movq %rbx, ".to_string())
2727
&& matches!(
@@ -101,12 +101,16 @@ pub(crate) fn codegen_inline_asm<'tcx>(
101101
ebx_place.write_cvalue(fx, CValue::by_val(ebx, fx.layout_of(fx.tcx.types.u32)));
102102
ecx_place.write_cvalue(fx, CValue::by_val(ecx, fx.layout_of(fx.tcx.types.u32)));
103103
edx_place.write_cvalue(fx, CValue::by_val(edx, fx.layout_of(fx.tcx.types.u32)));
104+
let destination_block = fx.get_block(destination.unwrap());
105+
fx.bcx.ins().jump(destination_block, &[]);
104106
return;
105107
} else if fx.tcx.symbol_name(fx.instance).name.starts_with("___chkstk") {
106108
// ___chkstk, ___chkstk_ms and __alloca are only used on Windows
107109
crate::trap::trap_unimplemented(fx, "Stack probes are not supported");
110+
return;
108111
} else if fx.tcx.symbol_name(fx.instance).name == "__alloca" {
109112
crate::trap::trap_unimplemented(fx, "Alloca is not supported");
113+
return;
110114
}
111115
}
112116

@@ -175,6 +179,16 @@ pub(crate) fn codegen_inline_asm<'tcx>(
175179
}
176180

177181
call_inline_asm(fx, &asm_name, asm_gen.stack_slot_size, inputs, outputs);
182+
183+
match destination {
184+
Some(destination) => {
185+
let destination_block = fx.get_block(destination);
186+
fx.bcx.ins().jump(destination_block, &[]);
187+
}
188+
None => {
189+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
190+
}
191+
}
178192
}
179193

180194
struct InlineAssemblyGenerator<'a, 'tcx> {

src/intrinsics/cpuid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) fn codegen_cpuid_call<'tcx>(
6262
fx.bcx.ins().jump(dest, &[zero, zero, proc_info_ecx, proc_info_edx]);
6363

6464
fx.bcx.switch_to_block(unsupported_leaf);
65-
crate::trap::trap_unreachable(
65+
crate::trap::trap_unimplemented(
6666
fx,
6767
"__cpuid_count arch intrinsic doesn't yet support specified leaf",
6868
);

src/intrinsics/llvm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
139139
.sess
140140
.warn(&format!("unsupported llvm intrinsic {}; replacing with trap", intrinsic));
141141
crate::trap::trap_unimplemented(fx, intrinsic);
142+
return;
142143
}
143144
}
144145

src/intrinsics/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn report_atomic_type_validation_error<'tcx>(
4444
),
4545
);
4646
// Prevent verifier error
47-
crate::trap::trap_unreachable(fx, "compilation should not have succeeded");
47+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
4848
}
4949

5050
pub(crate) fn clif_vector_type<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx>) -> Option<Type> {
@@ -849,8 +849,6 @@ fn codegen_regular_intrinsic_call<'tcx>(
849849
if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
850850
// special case for compiler-builtins to avoid having to patch it
851851
crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
852-
let ret_block = fx.get_block(destination.unwrap());
853-
fx.bcx.ins().jump(ret_block, &[]);
854852
return;
855853
} else {
856854
fx.tcx
@@ -882,8 +880,6 @@ fn codegen_regular_intrinsic_call<'tcx>(
882880
if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
883881
// special case for compiler-builtins to avoid having to patch it
884882
crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
885-
let ret_block = fx.get_block(destination.unwrap());
886-
fx.bcx.ins().jump(ret_block, &[]);
887883
return;
888884
} else {
889885
fx.tcx

src/intrinsics/simd.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn report_simd_type_validation_error(
1414
) {
1515
fx.tcx.sess.span_err(span, &format!("invalid monomorphization of `{}` intrinsic: expected SIMD input type, found non-SIMD `{}`", intrinsic, ty));
1616
// Prevent verifier error
17-
crate::trap::trap_unreachable(fx, "compilation should not have succeeded");
17+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
1818
}
1919

2020
pub(super) fn codegen_simd_intrinsic_call<'tcx>(
@@ -157,7 +157,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
157157
),
158158
);
159159
// Prevent verifier error
160-
crate::trap::trap_unreachable(fx, "compilation should not have succeeded");
160+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
161161
return;
162162
}
163163
}
@@ -274,12 +274,17 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
274274
idx_const
275275
} else {
276276
fx.tcx.sess.span_warn(span, "Index argument for `simd_extract` is not a constant");
277-
let res = crate::trap::trap_unimplemented_ret_value(
277+
let trap_block = fx.bcx.create_block();
278+
let dummy_block = fx.bcx.create_block();
279+
let true_ = fx.bcx.ins().iconst(types::I8, 1);
280+
fx.bcx.ins().brnz(true_, trap_block, &[]);
281+
fx.bcx.ins().jump(dummy_block, &[]);
282+
fx.bcx.switch_to_block(trap_block);
283+
crate::trap::trap_unimplemented(
278284
fx,
279-
ret.layout(),
280285
"Index argument for `simd_extract` is not a constant",
281286
);
282-
ret.write_cvalue(fx, res);
287+
fx.bcx.switch_to_block(dummy_block);
283288
return;
284289
};
285290

src/trap.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,10 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) {
2525
fx.bcx.ins().call(puts, &[msg_ptr]);
2626
}
2727

28-
/// Use this for example when a function call should never return. This will fill the current block,
29-
/// so you can **not** add instructions to it afterwards.
30-
///
31-
/// Trap code: user65535
32-
pub(crate) fn trap_unreachable(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
33-
codegen_print(fx, msg.as_ref());
34-
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
35-
}
3628
/// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
37-
/// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
38-
/// to it afterwards.
3929
///
4030
/// Trap code: user65535
4131
pub(crate) fn trap_unimplemented(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
4232
codegen_print(fx, msg.as_ref());
43-
let true_ = fx.bcx.ins().iconst(types::I32, 1);
44-
fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
45-
}
46-
47-
/// Like `trap_unimplemented` but returns a fake value of the specified type.
48-
///
49-
/// Trap code: user65535
50-
pub(crate) fn trap_unimplemented_ret_value<'tcx>(
51-
fx: &mut FunctionCx<'_, '_, 'tcx>,
52-
dest_layout: TyAndLayout<'tcx>,
53-
msg: impl AsRef<str>,
54-
) -> CValue<'tcx> {
55-
trap_unimplemented(fx, msg);
56-
CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
33+
fx.bcx.ins().trap(TrapCode::User(!0));
5734
}

0 commit comments

Comments
 (0)