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]);
28 pub fn trap_panic(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: impl AsRef<str>) {
29 codegen_print(fx, msg.as_ref());
30 fx.bcx.ins().trap(TrapCode::User(0));
33 /// Trap code: user65535
34 pub fn trap_unreachable(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 /// Trap code: user65535
40 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> {
41 codegen_print(fx, msg.as_ref());
42 let true_ = fx.bcx.ins().iconst(types::I32, 1);
43 fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
44 let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
45 CValue::by_ref(zero, dest_layout)
48 /// Trap code: user65535
49 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> {
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 let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
54 CPlace::for_addr(zero, dest_layout)