]> git.lizzy.rs Git - rust.git/blob - src/trap.rs
Fix release builds
[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.module.declare_function("puts", Linkage::Import, &Signature {
5         call_conv: CallConv::SystemV,
6         params: vec![AbiParam::new(pointer_ty(fx.tcx))],
7         returns: vec![],
8     }).unwrap();
9     let puts = fx.module.declare_func_in_func(puts, &mut fx.bcx.func);
10     #[cfg(debug_assertions)] {
11         fx.add_entity_comment(puts, "puts");
12     }
13
14     let symbol_name = fx.tcx.symbol_name(fx.instance);
15     let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg);
16     let mut data_ctx = DataContext::new();
17     data_ctx.define(real_msg.as_bytes().to_vec().into_boxed_slice());
18     let msg_id = fx.module.declare_data(&(symbol_name.as_str().to_string() + msg), Linkage::Local, false, None).unwrap();
19
20     // Ignore DuplicateDefinition error, as the data will be the same
21     let _ = fx.module.define_data(msg_id, &data_ctx);
22
23     let local_msg_id = fx.module.declare_data_in_func(msg_id, fx.bcx.func);
24     #[cfg(debug_assertions)] {
25         fx.add_entity_comment(local_msg_id, msg);
26     }
27     let msg_ptr = fx.bcx.ins().global_value(pointer_ty(fx.tcx), local_msg_id);
28     fx.bcx.ins().call(puts, &[msg_ptr]);
29 }
30
31 /// Use this when `rustc_codegen_llvm` would insert a call to the panic handler.
32 ///
33 /// Trap code: user0
34 pub fn trap_panic(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: impl AsRef<str>) {
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 fn trap_unreachable(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: impl AsRef<str>) {
44     codegen_print(fx, msg.as_ref());
45     fx.bcx.ins().trap(TrapCode::User(!0));
46 }
47
48 /// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
49 /// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
50 /// to it afterwards.
51 ///
52 /// Trap code: user65535
53 pub fn trap_unimplemented(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: impl AsRef<str>) {
54     codegen_print(fx, msg.as_ref());
55     let true_ = fx.bcx.ins().iconst(types::I32, 1);
56     fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
57 }
58
59 /// Like `trap_unreachable` but returns a fake value of the specified type.
60 ///
61 /// Trap code: user65535
62 pub fn trap_unreachable_ret_value<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>, dest_layout: TyLayout<'tcx>, msg: impl AsRef<str>) -> CValue<'tcx> {
63     trap_unimplemented(fx, msg);
64     let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
65     CValue::by_ref(zero, dest_layout)
66 }
67
68 /// Like `trap_unreachable` but returns a fake place for the specified type.
69 ///
70 /// Trap code: user65535
71 pub fn trap_unreachable_ret_place<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>, dest_layout: TyLayout<'tcx>, msg: impl AsRef<str>) -> CPlace<'tcx> {
72     trap_unimplemented(fx, msg);
73     let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
74     CPlace::for_addr(zero, dest_layout)
75 }