rust/src/trap.rs

35 lines
1.1 KiB
Rust
Raw Normal View History

//! Helpers used to print a message and abort in case of certain panics and some detected UB.
use crate::prelude::*;
2018-11-16 17:35:47 +01:00
fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) {
2019-08-31 22:58:09 +05:30
let puts = fx
.module
2019-08-31 22:58:09 +05:30
.declare_function(
"puts",
Linkage::Import,
&Signature {
call_conv: fx.target_config.default_call_conv,
params: vec![AbiParam::new(fx.pointer_type)],
2020-04-17 19:33:57 +02:00
returns: vec![AbiParam::new(types::I32)],
2019-08-31 22:58:09 +05:30
},
)
.unwrap();
let puts = fx.module.declare_func_in_func(puts, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
2020-03-20 12:18:40 +01:00
fx.add_comment(puts, "puts");
2019-07-30 15:00:15 +02:00
}
2019-03-23 13:06:35 +01:00
let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, fx.symbol_name, msg);
let msg_ptr = fx.anonymous_str(&real_msg);
2019-03-23 13:06:35 +01:00
fx.bcx.ins().call(puts, &[msg_ptr]);
}
/// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
///
2019-03-23 13:06:35 +01:00
/// Trap code: user65535
pub(crate) fn trap_unimplemented(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
2019-03-23 13:06:35 +01:00
codegen_print(fx, msg.as_ref());
fx.bcx.ins().trap(TrapCode::User(!0));
}