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 fx.add_entity_comment(puts, "puts");
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();
18 // Ignore DuplicateDefinition error, as the data will be the same
19 let _ = fx.module.define_data(msg_id, &data_ctx);
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]);
27 /// Use this when `rustc_codegen_llvm` would insert a call to the panic handler.
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));
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.
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));
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
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));
55 /// Like `trap_unreachable` but returns a fake value of the specified type.
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)
64 /// Like `trap_unreachable` but returns a fake place for the specified type.
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)