]> git.lizzy.rs Git - rust.git/blob - src/trap.rs
Limit publicness to crate where possible and remove unused imports
[rust.git] / src / trap.rs
1 use std::collections::hash_map::DefaultHasher;
2 use std::hash::{Hash, Hasher};
3
4 use crate::prelude::*;
5
6 fn codegen_print(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: &str) {
7     let puts = fx
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![],
16             },
17         )
18         .unwrap();
19     let puts = fx.module.declare_func_in_func(puts, &mut fx.bcx.func);
20     #[cfg(debug_assertions)]
21     {
22         fx.add_comment(puts, "puts");
23     }
24
25     let symbol_name = fx.tcx.symbol_name(fx.instance);
26     let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg);
27     let mut hasher = DefaultHasher::new();
28     real_msg.hash(&mut hasher);
29     let msg_hash = hasher.finish();
30     let mut data_ctx = DataContext::new();
31     data_ctx.define(real_msg.as_bytes().to_vec().into_boxed_slice());
32     let msg_id = fx
33         .module
34         .declare_data(
35             &format!("__trap_{:08x}", msg_hash),
36             Linkage::Local,
37             false,
38             false,
39             None,
40         )
41         .unwrap();
42
43     // Ignore DuplicateDefinition error, as the data will be the same
44     let _ = fx.module.define_data(msg_id, &data_ctx);
45
46     let local_msg_id = fx.module.declare_data_in_func(msg_id, fx.bcx.func);
47     #[cfg(debug_assertions)]
48     {
49         fx.add_comment(local_msg_id, msg);
50     }
51     let msg_ptr = fx.bcx.ins().global_value(pointer_ty(fx.tcx), local_msg_id);
52     fx.bcx.ins().call(puts, &[msg_ptr]);
53 }
54
55 /// Use this when `rustc_codegen_llvm` would insert a call to the panic handler.
56 ///
57 /// Trap code: user0
58 pub(crate) fn trap_panic(
59     fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
60     msg: impl AsRef<str>,
61 ) {
62     codegen_print(fx, msg.as_ref());
63     fx.bcx.ins().trap(TrapCode::User(0));
64 }
65
66 /// Use this for example when a function call should never return. This will fill the current block,
67 /// so you can **not** add instructions to it afterwards.
68 ///
69 /// Trap code: user65535
70 pub(crate) fn trap_unreachable(
71     fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
72     msg: impl AsRef<str>,
73 ) {
74     codegen_print(fx, msg.as_ref());
75     fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
76 }
77
78 /// Like `trap_unreachable` but returns a fake value of the specified type.
79 ///
80 /// Trap code: user65535
81 pub(crate) fn trap_unreachable_ret_value<'tcx>(
82     fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>,
83     dest_layout: TyLayout<'tcx>,
84     msg: impl AsRef<str>,
85 ) -> CValue<'tcx> {
86     trap_unreachable(fx, msg);
87     CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
88 }
89
90 /// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
91 /// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
92 /// to it afterwards.
93 ///
94 /// Trap code: user65535
95 pub(crate) fn trap_unimplemented(
96     fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
97     msg: impl AsRef<str>,
98 ) {
99     codegen_print(fx, msg.as_ref());
100     let true_ = fx.bcx.ins().iconst(types::I32, 1);
101     fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
102 }
103
104 /// Like `trap_unimplemented` but returns a fake value of the specified type.
105 ///
106 /// Trap code: user65535
107 pub(crate) fn trap_unimplemented_ret_value<'tcx>(
108     fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>,
109     dest_layout: TyLayout<'tcx>,
110     msg: impl AsRef<str>,
111 ) -> CValue<'tcx> {
112     trap_unimplemented(fx, msg);
113     CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
114 }