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))],
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");
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();
20 // Ignore DuplicateDefinition error, as the data will be the same
21 let _ = fx.module.define_data(msg_id, &data_ctx);
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);
27 let msg_ptr = fx.bcx.ins().global_value(pointer_ty(fx.tcx), local_msg_id);
28 fx.bcx.ins().call(puts, &[msg_ptr]);
31 /// Use this when `rustc_codegen_llvm` would insert a call to the panic handler.
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));
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.
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));
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
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));
59 /// Like `trap_unreachable` but returns a fake value of the specified type.
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)
68 /// Like `trap_unreachable` but returns a fake place for the specified type.
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)