2020-03-13 05:01:39 -05:00
|
|
|
use std::collections::hash_map::DefaultHasher;
|
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
|
2019-02-24 10:38:31 -06:00
|
|
|
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 {
|
2019-12-17 08:03:32 -06:00
|
|
|
call_conv: CallConv::triple_default(fx.triple()),
|
2019-08-31 12:28:09 -05:00
|
|
|
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)]
|
|
|
|
{
|
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
|
|
|
|
|
|
|
let symbol_name = fx.tcx.symbol_name(fx.instance);
|
2019-06-16 10:40:16 -05:00
|
|
|
let real_msg = format!("trap at {:?} ({}): {}\0", fx.instance, symbol_name, msg);
|
2020-03-13 05:01:39 -05:00
|
|
|
let mut hasher = DefaultHasher::new();
|
|
|
|
real_msg.hash(&mut hasher);
|
|
|
|
let msg_hash = hasher.finish();
|
2019-03-23 07:06:35 -05:00
|
|
|
let mut data_ctx = DataContext::new();
|
2019-06-16 10:40:16 -05:00
|
|
|
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(
|
2020-03-13 05:01:39 -05:00
|
|
|
&format!("__trap_{:08x}", msg_hash),
|
2019-08-31 12:28:09 -05:00
|
|
|
Linkage::Local,
|
|
|
|
false,
|
2019-10-27 10:55:35 -05:00
|
|
|
false,
|
2019-08-31 12:28:09 -05:00
|
|
|
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);
|
|
|
|
|
2019-09-28 10:59:27 -05:00
|
|
|
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)]
|
|
|
|
{
|
2020-03-20 06:18:40 -05:00
|
|
|
fx.add_comment(local_msg_id, msg);
|
2019-07-30 08:00:15 -05:00
|
|
|
}
|
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]);
|
|
|
|
}
|
|
|
|
|
2019-07-20 08:33:57 -05:00
|
|
|
/// 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
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn trap_panic(
|
2019-08-31 12:28:09 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn trap_unreachable(
|
2019-08-31 12:28:09 -05:00
|
|
|
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());
|
2020-02-07 14:38:30 -06:00
|
|
|
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
|
2018-11-16 10:35:47 -06:00
|
|
|
}
|
2019-02-24 10:38:31 -06:00
|
|
|
|
2020-03-14 08:34:07 -05:00
|
|
|
/// Like `trap_unreachable` but returns a fake value of the specified type.
|
|
|
|
///
|
|
|
|
/// Trap code: user65535
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn trap_unreachable_ret_value<'tcx>(
|
2020-03-14 08:34:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>,
|
|
|
|
dest_layout: TyLayout<'tcx>,
|
|
|
|
msg: impl AsRef<str>,
|
|
|
|
) -> CValue<'tcx> {
|
|
|
|
trap_unreachable(fx, msg);
|
|
|
|
CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
|
|
|
|
}
|
|
|
|
|
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
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn trap_unimplemented(
|
2019-08-31 12:28:09 -05:00
|
|
|
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());
|
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-03-14 08:34:07 -05:00
|
|
|
/// Like `trap_unimplemented` but returns a fake value of the specified type.
|
2019-07-20 08:33:57 -05:00
|
|
|
///
|
|
|
|
/// Trap code: user65535
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn trap_unimplemented_ret_value<'tcx>(
|
2019-08-31 12:28:09 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>,
|
|
|
|
dest_layout: TyLayout<'tcx>,
|
|
|
|
msg: impl AsRef<str>,
|
|
|
|
) -> CValue<'tcx> {
|
2019-07-20 08:33:57 -05:00
|
|
|
trap_unimplemented(fx, msg);
|
2019-12-20 09:02:47 -06:00
|
|
|
CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
|
2019-02-24 10:38:31 -06:00
|
|
|
}
|