2020-09-23 08:13:49 -05:00
|
|
|
//! Helpers used to print a message and abort in case of certain panics and some detected UB.
|
|
|
|
|
2019-02-24 10:38:31 -06:00
|
|
|
use crate::prelude::*;
|
2018-11-16 10:35:47 -06:00
|
|
|
|
2021-02-22 09:00:20 -06:00
|
|
|
fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) {
|
2019-08-31 12:28:09 -05:00
|
|
|
let puts = fx
|
2020-08-28 05:10:48 -05:00
|
|
|
.module
|
2019-08-31 12:28:09 -05:00
|
|
|
.declare_function(
|
|
|
|
"puts",
|
|
|
|
Linkage::Import,
|
|
|
|
&Signature {
|
2021-12-18 08:46:30 -06:00
|
|
|
call_conv: fx.target_config.default_call_conv,
|
2021-07-26 12:21:45 -05:00
|
|
|
params: vec![AbiParam::new(fx.pointer_type)],
|
2020-04-17 12:33:57 -05:00
|
|
|
returns: vec![AbiParam::new(types::I32)],
|
2019-08-31 12:28:09 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-04-08 15:01:39 -05:00
|
|
|
let puts = fx.module.declare_func_in_func(puts, &mut fx.bcx.func);
|
2021-03-06 11:45:04 -06:00
|
|
|
if fx.clif_comments.enabled() {
|
2020-03-20 06:18:40 -05:00
|
|
|
fx.add_comment(puts, "puts");
|
2019-07-30 08:00:15 -05:00
|
|
|
}
|
2019-03-23 07:06:35 -05:00
|
|
|
|
2021-04-07 07:54:59 -05:00
|
|
|
let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, fx.symbol_name, msg);
|
2021-05-11 07:25:56 -05:00
|
|
|
let msg_ptr = fx.anonymous_str(&real_msg);
|
2019-03-23 07:06:35 -05:00
|
|
|
fx.bcx.ins().call(puts, &[msg_ptr]);
|
|
|
|
}
|
|
|
|
|
2019-07-20 08:33:57 -05:00
|
|
|
/// Use this for example when a function call should never return. This will fill the current block,
|
|
|
|
/// so you can **not** add instructions to it afterwards.
|
|
|
|
///
|
2018-11-16 10:35:47 -06:00
|
|
|
/// Trap code: user65535
|
2021-02-22 09:00:20 -06:00
|
|
|
pub(crate) fn trap_unreachable(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
|
2019-03-23 07:06:35 -05:00
|
|
|
codegen_print(fx, msg.as_ref());
|
2020-02-07 14:38:30 -06:00
|
|
|
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
|
2018-11-16 10:35:47 -06:00
|
|
|
}
|
2019-07-20 08:33:57 -05:00
|
|
|
/// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
|
|
|
|
/// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
|
|
|
|
/// to it afterwards.
|
|
|
|
///
|
2019-03-23 07:06:35 -05:00
|
|
|
/// Trap code: user65535
|
2021-02-22 09:00:20 -06:00
|
|
|
pub(crate) fn trap_unimplemented(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
|
2019-03-23 07:06:35 -05:00
|
|
|
codegen_print(fx, msg.as_ref());
|
2019-02-24 10:38:31 -06:00
|
|
|
let true_ = fx.bcx.ins().iconst(types::I32, 1);
|
|
|
|
fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
|
2019-07-20 08:33:57 -05:00
|
|
|
}
|
2020-11-23 04:48:53 -06:00
|
|
|
|
|
|
|
/// Like `trap_unimplemented` but returns a fake value of the specified type.
|
|
|
|
///
|
|
|
|
/// Trap code: user65535
|
|
|
|
pub(crate) fn trap_unimplemented_ret_value<'tcx>(
|
2021-02-22 09:00:20 -06:00
|
|
|
fx: &mut FunctionCx<'_, '_, 'tcx>,
|
2020-11-23 04:48:53 -06:00
|
|
|
dest_layout: TyAndLayout<'tcx>,
|
|
|
|
msg: impl AsRef<str>,
|
|
|
|
) -> CValue<'tcx> {
|
|
|
|
trap_unimplemented(fx, msg);
|
|
|
|
CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
|
|
|
|
}
|