]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_cranelift/src/trap.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / compiler / rustc_codegen_cranelift / src / trap.rs
CommitLineData
29967ef6
XL
1//! Helpers used to print a message and abort in case of certain panics and some detected UB.
2
3use crate::prelude::*;
4
6a06907d 5fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) {
29967ef6
XL
6 let puts = fx
7 .cx
8 .module
9 .declare_function(
10 "puts",
11 Linkage::Import,
12 &Signature {
13 call_conv: CallConv::triple_default(fx.triple()),
14 params: vec![AbiParam::new(pointer_ty(fx.tcx))],
15 returns: vec![AbiParam::new(types::I32)],
16 },
17 )
18 .unwrap();
19 let puts = fx.cx.module.declare_func_in_func(puts, &mut fx.bcx.func);
cdc7bbd5 20 if fx.clif_comments.enabled() {
29967ef6
XL
21 fx.add_comment(puts, "puts");
22 }
23
24 let symbol_name = fx.tcx.symbol_name(fx.instance);
25 let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg);
26 let msg_ptr = fx.anonymous_str("trap", &real_msg);
27 fx.bcx.ins().call(puts, &[msg_ptr]);
28}
29
30/// Trap code: user1
6a06907d 31pub(crate) fn trap_abort(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
29967ef6
XL
32 codegen_print(fx, msg.as_ref());
33 fx.bcx.ins().trap(TrapCode::User(1));
34}
35
36/// Use this for example when a function call should never return. This will fill the current block,
37/// so you can **not** add instructions to it afterwards.
38///
39/// Trap code: user65535
6a06907d 40pub(crate) fn trap_unreachable(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
29967ef6
XL
41 codegen_print(fx, msg.as_ref());
42 fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
43}
44
45/// Like `trap_unreachable` but returns a fake value of the specified type.
46///
47/// Trap code: user65535
48pub(crate) fn trap_unreachable_ret_value<'tcx>(
6a06907d 49 fx: &mut FunctionCx<'_, '_, 'tcx>,
29967ef6
XL
50 dest_layout: TyAndLayout<'tcx>,
51 msg: impl AsRef<str>,
52) -> CValue<'tcx> {
53 codegen_print(fx, msg.as_ref());
54 let true_ = fx.bcx.ins().iconst(types::I32, 1);
55 fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached);
56 CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
57}
58
59/// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
60/// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
61/// to it afterwards.
62///
63/// Trap code: user65535
6a06907d 64pub(crate) fn trap_unimplemented(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
29967ef6
XL
65 codegen_print(fx, msg.as_ref());
66 let true_ = fx.bcx.ins().iconst(types::I32, 1);
67 fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
68}
fc512014
XL
69
70/// Like `trap_unimplemented` but returns a fake value of the specified type.
71///
72/// Trap code: user65535
73pub(crate) fn trap_unimplemented_ret_value<'tcx>(
6a06907d 74 fx: &mut FunctionCx<'_, '_, 'tcx>,
fc512014
XL
75 dest_layout: TyAndLayout<'tcx>,
76 msg: impl AsRef<str>,
77) -> CValue<'tcx> {
78 trap_unimplemented(fx, msg);
79 CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
80}