rust/src/trap.rs

110 lines
3.4 KiB
Rust
Raw Normal View History

use crate::prelude::*;
2018-11-16 10:35:47 -06:00
2019-03-23 07:06:35 -05:00
fn codegen_print(fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>, msg: &str) {
2019-08-31 12:28:09 -05:00
let puts = fx
.module
.declare_function(
"puts",
Linkage::Import,
&Signature {
call_conv: CallConv::SystemV,
params: vec![AbiParam::new(pointer_ty(fx.tcx))],
returns: vec![],
},
)
.unwrap();
2019-03-23 07:06:35 -05:00
let puts = fx.module.declare_func_in_func(puts, &mut fx.bcx.func);
2019-08-31 12:28:09 -05:00
#[cfg(debug_assertions)]
{
2019-07-30 08:00:15 -05:00
fx.add_entity_comment(puts, "puts");
}
2019-03-23 07:06:35 -05:00
let symbol_name = fx.tcx.symbol_name(fx.instance);
let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg);
2019-03-23 07:06:35 -05:00
let mut data_ctx = DataContext::new();
data_ctx.define(real_msg.as_bytes().to_vec().into_boxed_slice());
2019-08-31 12:28:09 -05:00
let msg_id = fx
.module
.declare_data(
&(symbol_name.as_str().to_string() + msg),
Linkage::Local,
false,
None,
)
.unwrap();
2019-03-23 07:06:35 -05:00
// Ignore DuplicateDefinition error, as the data will be the same
let _ = fx.module.define_data(msg_id, &data_ctx);
let local_msg_id = fx.module.declare_data_in_func(msg_id, fx.bcx.func);
2019-08-31 12:28:09 -05:00
#[cfg(debug_assertions)]
{
2019-07-30 08:00:15 -05:00
fx.add_entity_comment(local_msg_id, msg);
}
2019-03-23 07:06:35 -05:00
let msg_ptr = fx.bcx.ins().global_value(pointer_ty(fx.tcx), local_msg_id);
fx.bcx.ins().call(puts, &[msg_ptr]);
}
/// Use this when `rustc_codegen_llvm` would insert a call to the panic handler.
///
2018-11-16 10:35:47 -06:00
/// Trap code: user0
2019-08-31 12:28:09 -05:00
pub fn trap_panic(
fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
msg: impl AsRef<str>,
) {
2019-03-23 07:06:35 -05:00
codegen_print(fx, msg.as_ref());
fx.bcx.ins().trap(TrapCode::User(0));
2018-11-16 10:35:47 -06: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
2019-08-31 12:28:09 -05:00
pub fn trap_unreachable(
fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
msg: impl AsRef<str>,
) {
2019-03-23 07:06:35 -05:00
codegen_print(fx, msg.as_ref());
fx.bcx.ins().trap(TrapCode::User(!0));
2018-11-16 10:35:47 -06: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
2019-08-31 12:28:09 -05:00
pub fn trap_unimplemented(
fx: &mut FunctionCx<'_, '_, impl cranelift_module::Backend>,
msg: impl AsRef<str>,
) {
2019-03-23 07:06:35 -05:00
codegen_print(fx, msg.as_ref());
let true_ = fx.bcx.ins().iconst(types::I32, 1);
fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
}
/// Like `trap_unreachable` but returns a fake value of the specified type.
///
/// Trap code: user65535
2019-08-31 12:28:09 -05:00
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> {
trap_unimplemented(fx, msg);
let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
2019-06-11 08:32:30 -05:00
CValue::by_ref(zero, dest_layout)
}
/// Like `trap_unreachable` but returns a fake place for the specified type.
///
2019-03-23 07:06:35 -05:00
/// Trap code: user65535
2019-08-31 12:28:09 -05:00
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> {
trap_unimplemented(fx, msg);
let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
2019-06-11 09:30:47 -05:00
CPlace::for_addr(zero, dest_layout)
}