]> git.lizzy.rs Git - rust.git/blob - src/trap.rs
FunctionCx: WIP: Replace .tcx with .codegen_cx.tcx
[rust.git] / src / trap.rs
1 use crate::prelude::*;
2
3 fn codegen_print(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: &str) {
4     let puts = fx
5         .module
6         .declare_function(
7             "puts",
8             Linkage::Import,
9             &Signature {
10                 call_conv: CallConv::triple_default(fx.triple()),
11                 params: vec![AbiParam::new(pointer_ty(fxcodegen_cx.tcx))],
12                 returns: vec![AbiParam::new(types::I32)],
13             },
14         )
15         .unwrap();
16     let puts = fx.module.declare_func_in_func(puts, &mut fx.bcx.func);
17     #[cfg(debug_assertions)]
18     {
19         fx.add_comment(puts, "puts");
20     }
21
22     let symbol_name = fxcodegen_cx.tcx.symbol_name(fx.instance);
23     let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg);
24     let msg_ptr = fx.anonymous_str("trap", &real_msg);
25     fx.bcx.ins().call(puts, &[msg_ptr]);
26 }
27
28 /// Use this when `rustc_codegen_llvm` would insert a call to the panic handler.
29 ///
30 /// Trap code: user0
31 pub(crate) fn trap_panic(
32     fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
33     msg: impl AsRef<str>,
34 ) {
35     codegen_print(fx, msg.as_ref());
36     fx.bcx.ins().trap(TrapCode::User(0));
37 }
38
39 /// Use this for example when a function call should never return. This will fill the current block,
40 /// so you can **not** add instructions to it afterwards.
41 ///
42 /// Trap code: user65535
43 pub(crate) fn trap_unreachable(
44     fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
45     msg: impl AsRef<str>,
46 ) {
47     codegen_print(fx, msg.as_ref());
48     fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
49 }
50
51 /// Like `trap_unreachable` but returns a fake value of the specified type.
52 ///
53 /// Trap code: user65535
54 pub(crate) fn trap_unreachable_ret_value<'tcx>(
55     fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>,
56     dest_layout: TyAndLayout<'tcx>,
57     msg: impl AsRef<str>,
58 ) -> CValue<'tcx> {
59     codegen_print(fx, msg.as_ref());
60     let true_ = fx.bcx.ins().iconst(types::I32, 1);
61     fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached);
62     CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
63 }
64
65 /// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
66 /// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
67 /// to it afterwards.
68 ///
69 /// Trap code: user65535
70 pub(crate) fn trap_unimplemented(
71     fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
72     msg: impl AsRef<str>,
73 ) {
74     codegen_print(fx, msg.as_ref());
75     let true_ = fx.bcx.ins().iconst(types::I32, 1);
76     fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
77 }
78
79 /// Like `trap_unimplemented` but returns a fake value of the specified type.
80 ///
81 /// Trap code: user65535
82 pub(crate) fn trap_unimplemented_ret_value<'tcx>(
83     fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>,
84     dest_layout: TyAndLayout<'tcx>,
85     msg: impl AsRef<str>,
86 ) -> CValue<'tcx> {
87     trap_unimplemented(fx, msg);
88     CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
89 }