3 fn codegen_print(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: &str) {
10 call_conv: CallConv::triple_default(fx.triple()),
11 params: vec![AbiParam::new(pointer_ty(fx.tcx))],
16 let puts = fx.module.declare_func_in_func(puts, &mut fx.bcx.func);
17 #[cfg(debug_assertions)]
19 fx.add_entity_comment(puts, "puts");
22 let symbol_name = fx.tcx.symbol_name(fx.instance);
23 let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg);
24 let mut data_ctx = DataContext::new();
25 data_ctx.define(real_msg.as_bytes().to_vec().into_boxed_slice());
29 &(symbol_name.name.as_str().to_string() + msg),
36 // Ignore DuplicateDefinition error, as the data will be the same
37 let _ = fx.module.define_data(msg_id, &data_ctx);
39 let local_msg_id = fx.module.declare_data_in_func(msg_id, fx.bcx.func);
40 #[cfg(debug_assertions)]
42 fx.add_entity_comment(local_msg_id, msg);
44 let msg_ptr = fx.bcx.ins().global_value(pointer_ty(fx.tcx), local_msg_id);
45 fx.bcx.ins().call(puts, &[msg_ptr]);
48 /// Use this when `rustc_codegen_llvm` would insert a call to the panic handler.
52 fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
55 codegen_print(fx, msg.as_ref());
56 fx.bcx.ins().trap(TrapCode::User(0));
59 /// Use this for example when a function call should never return. This will fill the current block,
60 /// so you can **not** add instructions to it afterwards.
62 /// Trap code: user65535
63 pub fn trap_unreachable(
64 fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
67 codegen_print(fx, msg.as_ref());
68 fx.bcx.ins().trap(TrapCode::User(!0));
71 /// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
72 /// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
75 /// Trap code: user65535
76 pub fn trap_unimplemented(
77 fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
80 codegen_print(fx, msg.as_ref());
81 let true_ = fx.bcx.ins().iconst(types::I32, 1);
82 fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
85 /// Like `trap_unreachable` but returns a fake value of the specified type.
87 /// Trap code: user65535
88 pub fn trap_unreachable_ret_value<'tcx>(
89 fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>,
90 dest_layout: TyLayout<'tcx>,
93 trap_unimplemented(fx, msg);
94 CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)