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