2020-03-27 12:14:45 +01:00
|
|
|
mod llvm;
|
2019-11-18 20:58:33 +01:00
|
|
|
mod simd;
|
2019-11-18 20:45:32 +01:00
|
|
|
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) use llvm::codegen_llvm_intrinsic_call;
|
|
|
|
|
2018-10-03 18:21:52 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-07-29 12:43:24 +02:00
|
|
|
macro intrinsic_pat {
|
2018-10-05 19:55:06 +02:00
|
|
|
(_) => {
|
|
|
|
_
|
2019-07-29 12:43:24 +02:00
|
|
|
},
|
2018-10-05 19:55:06 +02:00
|
|
|
($name:ident) => {
|
|
|
|
stringify!($name)
|
2019-07-29 12:43:24 +02:00
|
|
|
},
|
|
|
|
($name:literal) => {
|
|
|
|
stringify!($name)
|
|
|
|
},
|
|
|
|
($x:ident . $($xs:tt).*) => {
|
|
|
|
concat!(stringify!($x), ".", intrinsic_pat!($($xs).*))
|
2018-10-05 19:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 12:43:24 +02:00
|
|
|
macro intrinsic_arg {
|
2019-07-28 11:24:33 +02:00
|
|
|
(o $fx:expr, $arg:ident) => {
|
2018-10-05 19:55:06 +02:00
|
|
|
$arg
|
2019-07-29 12:43:24 +02:00
|
|
|
},
|
2019-07-28 11:24:33 +02:00
|
|
|
(c $fx:expr, $arg:ident) => {
|
|
|
|
trans_operand($fx, $arg)
|
2019-07-29 12:43:24 +02:00
|
|
|
},
|
2018-10-05 19:55:06 +02:00
|
|
|
(v $fx:expr, $arg:ident) => {
|
2019-07-28 11:24:33 +02:00
|
|
|
trans_operand($fx, $arg).load_scalar($fx)
|
2019-07-29 12:43:24 +02:00
|
|
|
}
|
2018-10-05 19:55:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 12:43:24 +02:00
|
|
|
macro intrinsic_substs {
|
|
|
|
($substs:expr, $index:expr,) => {},
|
2018-10-05 20:11:47 +02:00
|
|
|
($substs:expr, $index:expr, $first:ident $(,$rest:ident)*) => {
|
|
|
|
let $first = $substs.type_at($index);
|
|
|
|
intrinsic_substs!($substs, $index+1, $($rest),*);
|
2019-07-29 12:43:24 +02:00
|
|
|
}
|
2018-10-05 20:11:47 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 20:45:32 +01:00
|
|
|
macro intrinsic_match {
|
2019-07-29 12:43:24 +02:00
|
|
|
($fx:expr, $intrinsic:expr, $substs:expr, $args:expr,
|
|
|
|
_ => $unknown:block;
|
|
|
|
$(
|
|
|
|
$($($name:tt).*)|+ $(if $cond:expr)?, $(<$($subst:ident),*>)? ($($a:ident $arg:ident),*) $content:block;
|
2018-10-05 19:55:06 +02:00
|
|
|
)*) => {
|
2020-02-22 15:17:30 +01:00
|
|
|
let _ = $substs; // Silence warning when substs is unused.
|
2018-10-05 19:55:06 +02:00
|
|
|
match $intrinsic {
|
|
|
|
$(
|
2019-07-29 12:43:24 +02:00
|
|
|
$(intrinsic_pat!($($name).*))|* $(if $cond)? => {
|
2018-10-05 20:11:47 +02:00
|
|
|
#[allow(unused_parens, non_snake_case)]
|
|
|
|
{
|
|
|
|
$(
|
|
|
|
intrinsic_substs!($substs, 0, $($subst),*);
|
|
|
|
)?
|
2019-07-28 11:24:33 +02:00
|
|
|
if let [$($arg),*] = $args {
|
|
|
|
let ($($arg,)*) = (
|
|
|
|
$(intrinsic_arg!($a $fx, $arg),)*
|
2018-10-05 19:55:06 +02:00
|
|
|
);
|
2018-10-05 20:11:47 +02:00
|
|
|
#[warn(unused_parens, non_snake_case)]
|
|
|
|
{
|
|
|
|
$content
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bug!("wrong number of args for intrinsic {:?}", $intrinsic);
|
2018-10-05 19:55:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
2019-07-29 12:43:24 +02:00
|
|
|
_ => $unknown,
|
2018-10-05 19:55:06 +02:00
|
|
|
}
|
2019-07-29 12:43:24 +02:00
|
|
|
}
|
2018-10-05 19:55:06 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
macro call_intrinsic_match {
|
2019-07-27 16:51:48 +02:00
|
|
|
($fx:expr, $intrinsic:expr, $substs:expr, $ret:expr, $destination:expr, $args:expr, $(
|
|
|
|
$name:ident($($arg:ident),*) -> $ty:ident => $func:ident,
|
|
|
|
)*) => {
|
|
|
|
match $intrinsic {
|
|
|
|
$(
|
|
|
|
stringify!($name) => {
|
|
|
|
assert!($substs.is_noop());
|
2019-07-28 11:24:33 +02:00
|
|
|
if let [$(ref $arg),*] = *$args {
|
|
|
|
let ($($arg,)*) = (
|
|
|
|
$(trans_operand($fx, $arg),)*
|
|
|
|
);
|
2019-07-27 16:51:48 +02:00
|
|
|
let res = $fx.easy_call(stringify!($func), &[$($arg),*], $fx.tcx.types.$ty);
|
|
|
|
$ret.write_cvalue($fx, res);
|
|
|
|
|
|
|
|
if let Some((_, dest)) = $destination {
|
2020-02-14 18:23:29 +01:00
|
|
|
let ret_block = $fx.get_block(dest);
|
|
|
|
$fx.bcx.ins().jump(ret_block, &[]);
|
2019-07-27 16:51:48 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bug!("wrong number of args for intrinsic {:?}", $intrinsic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
macro atomic_binop_return_old($fx:expr, $op:ident<$T:ident>($ptr:ident, $src:ident) -> $ret:ident) {
|
2020-01-18 12:43:31 +01:00
|
|
|
crate::atomic_shim::lock_global_lock($fx);
|
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
let clif_ty = $fx.clif_type($T).unwrap();
|
|
|
|
let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
|
|
|
|
let new = $fx.bcx.ins().$op(old, $src);
|
|
|
|
$fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
|
|
|
|
$ret.write_cvalue($fx, CValue::by_val(old, $fx.layout_of($T)));
|
2020-01-18 12:43:31 +01:00
|
|
|
|
|
|
|
crate::atomic_shim::unlock_global_lock($fx);
|
2018-10-06 10:24:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
macro atomic_minmax($fx:expr, $cc:expr, <$T:ident> ($ptr:ident, $src:ident) -> $ret:ident) {
|
2020-01-18 12:43:31 +01:00
|
|
|
crate::atomic_shim::lock_global_lock($fx);
|
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
// Read old
|
|
|
|
let clif_ty = $fx.clif_type($T).unwrap();
|
|
|
|
let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
|
2018-10-06 10:24:09 +02:00
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
// Compare
|
|
|
|
let is_eq = codegen_icmp($fx, IntCC::SignedGreaterThan, old, $src);
|
|
|
|
let new = $fx.bcx.ins().select(is_eq, old, $src);
|
2018-10-06 10:24:09 +02:00
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
// Write new
|
|
|
|
$fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
|
2018-10-06 10:24:09 +02:00
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
let ret_val = CValue::by_val(old, $ret.layout());
|
|
|
|
$ret.write_cvalue($fx, ret_val);
|
2020-01-18 12:43:31 +01:00
|
|
|
|
|
|
|
crate::atomic_shim::unlock_global_lock($fx);
|
2018-10-06 10:24:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-14 17:11:06 +01:00
|
|
|
fn lane_type_and_count<'tcx>(
|
2019-11-18 21:15:43 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-07-27 17:48:24 +02:00
|
|
|
layout: TyLayout<'tcx>,
|
2019-07-28 11:24:33 +02:00
|
|
|
) -> (TyLayout<'tcx>, u32) {
|
2019-07-28 10:24:57 +02:00
|
|
|
assert!(layout.ty.is_simd());
|
2019-07-27 17:48:24 +02:00
|
|
|
let lane_count = match layout.fields {
|
2019-07-28 11:24:33 +02:00
|
|
|
layout::FieldPlacement::Array { stride: _, count } => u32::try_from(count).unwrap(),
|
2019-11-18 21:15:43 +01:00
|
|
|
_ => unreachable!("lane_type_and_count({:?})", layout),
|
2019-07-27 17:48:24 +02:00
|
|
|
};
|
2019-11-18 21:15:43 +01:00
|
|
|
let lane_layout = layout.field(&ty::layout::LayoutCx {
|
|
|
|
tcx,
|
|
|
|
param_env: ParamEnv::reveal_all(),
|
|
|
|
}, 0).unwrap();
|
2019-07-27 17:48:24 +02:00
|
|
|
(lane_layout, lane_count)
|
|
|
|
}
|
|
|
|
|
2019-11-18 20:45:32 +01:00
|
|
|
fn simd_for_each_lane<'tcx, B: Backend>(
|
2019-11-18 21:29:34 +01:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, B>,
|
|
|
|
val: CValue<'tcx>,
|
|
|
|
ret: CPlace<'tcx>,
|
|
|
|
f: impl Fn(
|
|
|
|
&mut FunctionCx<'_, 'tcx, B>,
|
|
|
|
TyLayout<'tcx>,
|
|
|
|
TyLayout<'tcx>,
|
|
|
|
Value,
|
|
|
|
) -> CValue<'tcx>,
|
|
|
|
) {
|
|
|
|
let layout = val.layout();
|
|
|
|
|
|
|
|
let (lane_layout, lane_count) = lane_type_and_count(fx.tcx, layout);
|
|
|
|
let (ret_lane_layout, ret_lane_count) = lane_type_and_count(fx.tcx, ret.layout());
|
|
|
|
assert_eq!(lane_count, ret_lane_count);
|
|
|
|
|
|
|
|
for lane_idx in 0..lane_count {
|
|
|
|
let lane_idx = mir::Field::new(lane_idx.try_into().unwrap());
|
|
|
|
let lane = val.value_field(fx, lane_idx).load_scalar(fx);
|
|
|
|
|
|
|
|
let res_lane = f(fx, lane_layout, ret_lane_layout, lane);
|
|
|
|
|
|
|
|
ret.place_field(fx, lane_idx).write_cvalue(fx, res_lane);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn simd_pair_for_each_lane<'tcx, B: Backend>(
|
2019-07-27 17:48:24 +02:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, B>,
|
|
|
|
x: CValue<'tcx>,
|
|
|
|
y: CValue<'tcx>,
|
|
|
|
ret: CPlace<'tcx>,
|
2019-08-31 22:58:09 +05:30
|
|
|
f: impl Fn(
|
|
|
|
&mut FunctionCx<'_, 'tcx, B>,
|
|
|
|
TyLayout<'tcx>,
|
|
|
|
TyLayout<'tcx>,
|
|
|
|
Value,
|
|
|
|
Value,
|
|
|
|
) -> CValue<'tcx>,
|
2019-07-27 17:48:24 +02:00
|
|
|
) {
|
|
|
|
assert_eq!(x.layout(), y.layout());
|
|
|
|
let layout = x.layout();
|
|
|
|
|
2019-11-18 21:15:43 +01:00
|
|
|
let (lane_layout, lane_count) = lane_type_and_count(fx.tcx, layout);
|
|
|
|
let (ret_lane_layout, ret_lane_count) = lane_type_and_count(fx.tcx, ret.layout());
|
2019-07-27 17:48:24 +02:00
|
|
|
assert_eq!(lane_count, ret_lane_count);
|
|
|
|
|
|
|
|
for lane in 0..lane_count {
|
2019-07-28 11:24:33 +02:00
|
|
|
let lane = mir::Field::new(lane.try_into().unwrap());
|
2019-07-27 17:48:24 +02:00
|
|
|
let x_lane = x.value_field(fx, lane).load_scalar(fx);
|
|
|
|
let y_lane = y.value_field(fx, lane).load_scalar(fx);
|
|
|
|
|
|
|
|
let res_lane = f(fx, lane_layout, ret_lane_layout, x_lane, y_lane);
|
|
|
|
|
|
|
|
ret.place_field(fx, lane).write_cvalue(fx, res_lane);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 20:45:32 +01:00
|
|
|
fn bool_to_zero_or_max_uint<'tcx>(
|
2019-07-28 09:45:01 +02:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
|
|
|
layout: TyLayout<'tcx>,
|
|
|
|
val: Value,
|
|
|
|
) -> CValue<'tcx> {
|
|
|
|
let ty = fx.clif_type(layout.ty).unwrap();
|
|
|
|
|
2019-07-30 14:37:20 +02:00
|
|
|
let int_ty = match ty {
|
|
|
|
types::F32 => types::I32,
|
|
|
|
types::F64 => types::I64,
|
|
|
|
ty => ty,
|
|
|
|
};
|
|
|
|
|
2020-03-27 16:04:34 +01:00
|
|
|
let val = fx.bcx.ins().bint(int_ty, val);
|
|
|
|
let mut res = fx.bcx.ins().ineg(val);
|
2019-07-30 14:37:20 +02:00
|
|
|
|
|
|
|
if ty.is_float() {
|
|
|
|
res = fx.bcx.ins().bitcast(ty, res);
|
|
|
|
}
|
|
|
|
|
2019-07-28 09:45:01 +02:00
|
|
|
CValue::by_val(res, layout)
|
|
|
|
}
|
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
macro simd_cmp {
|
2019-12-31 15:53:18 +01:00
|
|
|
($fx:expr, $cc:ident($x:ident, $y:ident) -> $ret:ident) => {
|
2019-11-18 21:29:34 +01:00
|
|
|
simd_pair_for_each_lane(
|
2019-08-31 22:58:09 +05:30
|
|
|
$fx,
|
|
|
|
$x,
|
|
|
|
$y,
|
|
|
|
$ret,
|
|
|
|
|fx, lane_layout, res_lane_layout, x_lane, y_lane| {
|
2019-09-28 11:13:40 +02:00
|
|
|
let res_lane = match lane_layout.ty.kind {
|
2019-08-31 22:58:09 +05:30
|
|
|
ty::Uint(_) | ty::Int(_) => codegen_icmp(fx, IntCC::$cc, x_lane, y_lane),
|
|
|
|
_ => unreachable!("{:?}", lane_layout.ty),
|
|
|
|
};
|
|
|
|
bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
|
|
|
|
},
|
|
|
|
);
|
2019-11-18 20:58:33 +01:00
|
|
|
},
|
2019-12-31 15:53:18 +01:00
|
|
|
($fx:expr, $cc_u:ident|$cc_s:ident($x:ident, $y:ident) -> $ret:ident) => {
|
2019-11-18 21:29:34 +01:00
|
|
|
simd_pair_for_each_lane(
|
2019-08-31 22:58:09 +05:30
|
|
|
$fx,
|
|
|
|
$x,
|
|
|
|
$y,
|
|
|
|
$ret,
|
|
|
|
|fx, lane_layout, res_lane_layout, x_lane, y_lane| {
|
2019-09-28 11:13:40 +02:00
|
|
|
let res_lane = match lane_layout.ty.kind {
|
2019-08-31 22:58:09 +05:30
|
|
|
ty::Uint(_) => codegen_icmp(fx, IntCC::$cc_u, x_lane, y_lane),
|
|
|
|
ty::Int(_) => codegen_icmp(fx, IntCC::$cc_s, x_lane, y_lane),
|
|
|
|
_ => unreachable!("{:?}", lane_layout.ty),
|
|
|
|
};
|
|
|
|
bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane)
|
|
|
|
},
|
|
|
|
);
|
2019-11-18 20:58:33 +01:00
|
|
|
},
|
2019-07-28 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
macro simd_int_binop {
|
2019-12-23 17:25:32 +01:00
|
|
|
($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) => {
|
|
|
|
simd_int_binop!($fx, $op|$op($x, $y) -> $ret);
|
2019-11-18 20:58:33 +01:00
|
|
|
},
|
2019-12-23 17:25:32 +01:00
|
|
|
($fx:expr, $op_u:ident|$op_s:ident($x:ident, $y:ident) -> $ret:ident) => {
|
2020-01-04 18:27:00 +01:00
|
|
|
simd_pair_for_each_lane(
|
|
|
|
$fx,
|
|
|
|
$x,
|
|
|
|
$y,
|
|
|
|
$ret,
|
|
|
|
|fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
|
|
|
|
let res_lane = match lane_layout.ty.kind {
|
|
|
|
ty::Uint(_) => fx.bcx.ins().$op_u(x_lane, y_lane),
|
|
|
|
ty::Int(_) => fx.bcx.ins().$op_s(x_lane, y_lane),
|
|
|
|
_ => unreachable!("{:?}", lane_layout.ty),
|
|
|
|
};
|
|
|
|
CValue::by_val(res_lane, ret_lane_layout)
|
|
|
|
},
|
|
|
|
);
|
2019-11-18 20:58:33 +01:00
|
|
|
},
|
2019-07-27 17:48:24 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
macro simd_int_flt_binop {
|
2019-12-23 17:25:32 +01:00
|
|
|
($fx:expr, $op:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
|
|
|
|
simd_int_flt_binop!($fx, $op|$op|$op_f($x, $y) -> $ret);
|
2019-11-18 20:58:33 +01:00
|
|
|
},
|
2019-12-23 17:25:32 +01:00
|
|
|
($fx:expr, $op_u:ident|$op_s:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => {
|
2020-01-04 18:27:00 +01:00
|
|
|
simd_pair_for_each_lane(
|
|
|
|
$fx,
|
|
|
|
$x,
|
|
|
|
$y,
|
|
|
|
$ret,
|
|
|
|
|fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
|
|
|
|
let res_lane = match lane_layout.ty.kind {
|
|
|
|
ty::Uint(_) => fx.bcx.ins().$op_u(x_lane, y_lane),
|
|
|
|
ty::Int(_) => fx.bcx.ins().$op_s(x_lane, y_lane),
|
|
|
|
ty::Float(_) => fx.bcx.ins().$op_f(x_lane, y_lane),
|
|
|
|
_ => unreachable!("{:?}", lane_layout.ty),
|
|
|
|
};
|
|
|
|
CValue::by_val(res_lane, ret_lane_layout)
|
|
|
|
},
|
|
|
|
);
|
2019-11-18 20:58:33 +01:00
|
|
|
},
|
2019-07-30 14:37:20 +02:00
|
|
|
}
|
|
|
|
|
2019-12-23 17:25:32 +01:00
|
|
|
macro simd_flt_binop($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) {
|
2020-01-04 18:27:00 +01:00
|
|
|
simd_pair_for_each_lane(
|
|
|
|
$fx,
|
|
|
|
$x,
|
|
|
|
$y,
|
|
|
|
$ret,
|
|
|
|
|fx, lane_layout, ret_lane_layout, x_lane, y_lane| {
|
|
|
|
let res_lane = match lane_layout.ty.kind {
|
|
|
|
ty::Float(_) => fx.bcx.ins().$op(x_lane, y_lane),
|
|
|
|
_ => unreachable!("{:?}", lane_layout.ty),
|
|
|
|
};
|
|
|
|
CValue::by_val(res_lane, ret_lane_layout)
|
|
|
|
},
|
|
|
|
);
|
2019-07-30 14:37:20 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 12:14:45 +01:00
|
|
|
pub(crate) fn codegen_intrinsic_call<'tcx>(
|
2019-08-18 16:52:07 +02:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-09-21 11:30:29 +02:00
|
|
|
instance: Instance<'tcx>,
|
2019-07-28 11:24:33 +02:00
|
|
|
args: &[mir::Operand<'tcx>],
|
2018-10-03 18:21:52 +02:00
|
|
|
destination: Option<(CPlace<'tcx>, BasicBlock)>,
|
2019-11-09 11:14:18 +01:00
|
|
|
span: Span,
|
2018-10-03 18:21:52 +02:00
|
|
|
) {
|
2019-09-21 11:30:29 +02:00
|
|
|
let def_id = instance.def_id();
|
|
|
|
let substs = instance.substs;
|
|
|
|
|
2018-10-03 18:21:52 +02:00
|
|
|
let intrinsic = fx.tcx.item_name(def_id).as_str();
|
|
|
|
let intrinsic = &intrinsic[..];
|
|
|
|
|
|
|
|
let ret = match destination {
|
|
|
|
Some((place, _)) => place,
|
|
|
|
None => {
|
|
|
|
// Insert non returning intrinsics here
|
|
|
|
match intrinsic {
|
|
|
|
"abort" => {
|
2019-06-17 21:19:08 +02:00
|
|
|
trap_panic(fx, "Called intrinsic::abort.");
|
2018-10-03 18:21:52 +02:00
|
|
|
}
|
|
|
|
"unreachable" => {
|
2019-03-23 13:06:35 +01:00
|
|
|
trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
|
2018-10-03 18:21:52 +02:00
|
|
|
}
|
2019-08-04 12:27:43 +02:00
|
|
|
"transmute" => {
|
2019-08-31 22:58:09 +05:30
|
|
|
trap_unreachable(
|
|
|
|
fx,
|
2020-03-17 14:52:06 +01:00
|
|
|
"[corruption] Transmuting to uninhabited type.",
|
2019-08-31 22:58:09 +05:30
|
|
|
);
|
2019-08-04 12:27:43 +02:00
|
|
|
}
|
2018-10-03 18:21:52 +02:00
|
|
|
_ => unimplemented!("unsupported instrinsic {}", intrinsic),
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-18 20:58:33 +01:00
|
|
|
if intrinsic.starts_with("simd_") {
|
|
|
|
self::simd::codegen_simd_intrinsic_call(fx, instance, args, ret, span);
|
2020-02-14 18:23:29 +01:00
|
|
|
let ret_block = fx.get_block(destination.expect("SIMD intrinsics don't diverge").1);
|
|
|
|
fx.bcx.ins().jump(ret_block, &[]);
|
2019-11-18 20:58:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-03 18:21:52 +02:00
|
|
|
let usize_layout = fx.layout_of(fx.tcx.types.usize);
|
|
|
|
|
2019-07-27 16:51:48 +02:00
|
|
|
call_intrinsic_match! {
|
|
|
|
fx, intrinsic, substs, ret, destination, args,
|
|
|
|
expf32(flt) -> f32 => expf,
|
|
|
|
expf64(flt) -> f64 => exp,
|
|
|
|
exp2f32(flt) -> f32 => exp2f,
|
|
|
|
exp2f64(flt) -> f64 => exp2,
|
|
|
|
sqrtf32(flt) -> f32 => sqrtf,
|
|
|
|
sqrtf64(flt) -> f64 => sqrt,
|
|
|
|
powif32(a, x) -> f32 => __powisf2, // compiler-builtins
|
|
|
|
powif64(a, x) -> f64 => __powidf2, // compiler-builtins
|
2019-08-19 17:16:21 +02:00
|
|
|
powf32(a, x) -> f32 => powf,
|
|
|
|
powf64(a, x) -> f64 => pow,
|
2019-07-27 16:51:48 +02:00
|
|
|
logf32(flt) -> f32 => logf,
|
|
|
|
logf64(flt) -> f64 => log,
|
2019-08-12 15:54:24 +02:00
|
|
|
log2f32(flt) -> f32 => log2f,
|
|
|
|
log2f64(flt) -> f64 => log2,
|
2019-11-24 15:30:28 +01:00
|
|
|
log10f32(flt) -> f32 => log10f,
|
|
|
|
log10f64(flt) -> f64 => log10,
|
2019-07-27 16:51:48 +02:00
|
|
|
fabsf32(flt) -> f32 => fabsf,
|
|
|
|
fabsf64(flt) -> f64 => fabs,
|
|
|
|
fmaf32(x, y, z) -> f32 => fmaf,
|
|
|
|
fmaf64(x, y, z) -> f64 => fma,
|
2019-08-19 15:48:32 +02:00
|
|
|
copysignf32(x, y) -> f32 => copysignf,
|
|
|
|
copysignf64(x, y) -> f64 => copysign,
|
2019-07-27 16:51:48 +02:00
|
|
|
|
|
|
|
// rounding variants
|
2019-08-19 15:48:32 +02:00
|
|
|
// FIXME use clif insts
|
2019-07-27 16:51:48 +02:00
|
|
|
floorf32(flt) -> f32 => floorf,
|
|
|
|
floorf64(flt) -> f64 => floor,
|
|
|
|
ceilf32(flt) -> f32 => ceilf,
|
|
|
|
ceilf64(flt) -> f64 => ceil,
|
|
|
|
truncf32(flt) -> f32 => truncf,
|
|
|
|
truncf64(flt) -> f64 => trunc,
|
|
|
|
roundf32(flt) -> f32 => roundf,
|
|
|
|
roundf64(flt) -> f64 => round,
|
|
|
|
|
|
|
|
// trigonometry
|
|
|
|
sinf32(flt) -> f32 => sinf,
|
|
|
|
sinf64(flt) -> f64 => sin,
|
|
|
|
cosf32(flt) -> f32 => cosf,
|
|
|
|
cosf64(flt) -> f64 => cos,
|
|
|
|
tanf32(flt) -> f32 => tanf,
|
|
|
|
tanf64(flt) -> f64 => tan,
|
|
|
|
}
|
|
|
|
|
2018-10-05 19:55:06 +02:00
|
|
|
intrinsic_match! {
|
2018-10-05 20:11:47 +02:00
|
|
|
fx, intrinsic, substs, args,
|
2019-07-29 12:43:24 +02:00
|
|
|
_ => {
|
2020-03-17 16:26:56 +01:00
|
|
|
unimpl_fatal!(fx.tcx, span, "unsupported intrinsic {}", intrinsic);
|
2019-07-29 12:43:24 +02:00
|
|
|
};
|
2018-10-05 19:55:06 +02:00
|
|
|
|
2018-10-05 20:11:47 +02:00
|
|
|
assume, (c _a) {};
|
|
|
|
likely | unlikely, (c a) {
|
2018-10-05 19:55:06 +02:00
|
|
|
ret.write_cvalue(fx, a);
|
|
|
|
};
|
2019-02-11 18:49:59 +01:00
|
|
|
breakpoint, () {
|
|
|
|
fx.bcx.ins().debugtrap();
|
|
|
|
};
|
2018-10-05 20:11:47 +02:00
|
|
|
copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
|
2018-10-03 18:21:52 +02:00
|
|
|
let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
|
|
|
|
let elem_size = fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
2018-11-03 13:14:28 +01:00
|
|
|
.iconst(fx.pointer_type, elem_size as i64);
|
2018-10-03 18:21:52 +02:00
|
|
|
assert_eq!(args.len(), 3);
|
|
|
|
let byte_amount = fx.bcx.ins().imul(count, elem_size);
|
|
|
|
|
|
|
|
if intrinsic.ends_with("_nonoverlapping") {
|
2020-01-04 12:33:39 +01:00
|
|
|
// FIXME emit_small_memcpy
|
2018-11-09 18:03:47 +01:00
|
|
|
fx.bcx.call_memcpy(fx.module.target_config(), dst, src, byte_amount);
|
2018-10-03 18:21:52 +02:00
|
|
|
} else {
|
2020-01-04 12:33:39 +01:00
|
|
|
// FIXME emit_small_memmove
|
2018-11-09 18:03:47 +01:00
|
|
|
fx.bcx.call_memmove(fx.module.target_config(), dst, src, byte_amount);
|
2018-10-03 18:21:52 +02:00
|
|
|
}
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2019-08-18 16:19:33 +02:00
|
|
|
discriminant_value, (c ptr) {
|
|
|
|
let pointee_layout = fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
|
2019-12-20 16:02:47 +01:00
|
|
|
let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), pointee_layout);
|
2019-08-18 16:19:33 +02:00
|
|
|
let discr = crate::discriminant::codegen_get_discriminant(fx, val, ret.layout());
|
2018-10-03 18:21:52 +02:00
|
|
|
ret.write_cvalue(fx, discr);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2018-10-05 20:11:47 +02:00
|
|
|
size_of_val, <T> (c ptr) {
|
|
|
|
let layout = fx.layout_of(T);
|
2018-12-29 15:33:34 +01:00
|
|
|
let size = if layout.is_unsized() {
|
2019-03-02 21:09:28 +01:00
|
|
|
let (_ptr, info) = ptr.load_scalar_pair(fx);
|
2020-01-15 13:17:09 +01:00
|
|
|
let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout, info);
|
2018-12-29 15:33:34 +01:00
|
|
|
size
|
|
|
|
} else {
|
|
|
|
fx
|
2018-10-03 18:21:52 +02:00
|
|
|
.bcx
|
|
|
|
.ins()
|
2018-12-29 15:33:34 +01:00
|
|
|
.iconst(fx.pointer_type, layout.size.bytes() as i64)
|
2018-10-03 18:21:52 +02:00
|
|
|
};
|
2019-06-11 16:25:07 +02:00
|
|
|
ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2018-10-05 20:11:47 +02:00
|
|
|
min_align_of_val, <T> (c ptr) {
|
|
|
|
let layout = fx.layout_of(T);
|
2018-12-29 15:33:34 +01:00
|
|
|
let align = if layout.is_unsized() {
|
2019-03-02 21:09:28 +01:00
|
|
|
let (_ptr, info) = ptr.load_scalar_pair(fx);
|
2020-01-15 13:17:09 +01:00
|
|
|
let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout, info);
|
2018-12-29 15:33:34 +01:00
|
|
|
align
|
|
|
|
} else {
|
|
|
|
fx
|
2018-10-03 18:21:52 +02:00
|
|
|
.bcx
|
|
|
|
.ins()
|
2018-12-29 15:33:34 +01:00
|
|
|
.iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
|
2018-10-03 18:21:52 +02:00
|
|
|
};
|
2019-06-11 16:25:07 +02:00
|
|
|
ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2019-06-23 16:33:34 +02:00
|
|
|
|
2018-11-13 18:50:45 +01:00
|
|
|
_ if intrinsic.starts_with("unchecked_") || intrinsic == "exact_div", (c x, c y) {
|
2019-03-04 18:57:09 +01:00
|
|
|
// FIXME trap on overflow
|
2018-10-03 18:21:52 +02:00
|
|
|
let bin_op = match intrinsic {
|
2019-06-23 10:47:16 +02:00
|
|
|
"unchecked_sub" => BinOp::Sub,
|
2018-11-13 18:50:45 +01:00
|
|
|
"unchecked_div" | "exact_div" => BinOp::Div,
|
2018-10-03 18:21:52 +02:00
|
|
|
"unchecked_rem" => BinOp::Rem,
|
|
|
|
"unchecked_shl" => BinOp::Shl,
|
|
|
|
"unchecked_shr" => BinOp::Shr,
|
2020-01-25 16:24:48 +01:00
|
|
|
_ => unreachable!("intrinsic {}", intrinsic),
|
2018-10-03 18:21:52 +02:00
|
|
|
};
|
2019-08-14 15:03:52 +02:00
|
|
|
let res = crate::num::trans_int_binop(fx, bin_op, x, y);
|
2018-10-03 18:21:52 +02:00
|
|
|
ret.write_cvalue(fx, res);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2019-08-14 13:01:55 +02:00
|
|
|
_ if intrinsic.ends_with("_with_overflow"), (c x, c y) {
|
2018-10-05 19:55:06 +02:00
|
|
|
assert_eq!(x.layout().ty, y.layout().ty);
|
2018-10-03 18:21:52 +02:00
|
|
|
let bin_op = match intrinsic {
|
|
|
|
"add_with_overflow" => BinOp::Add,
|
|
|
|
"sub_with_overflow" => BinOp::Sub,
|
|
|
|
"mul_with_overflow" => BinOp::Mul,
|
2020-01-25 16:24:48 +01:00
|
|
|
_ => unreachable!("intrinsic {}", intrinsic),
|
2018-10-03 18:21:52 +02:00
|
|
|
};
|
2019-08-08 15:54:13 +02:00
|
|
|
|
2019-08-14 11:52:39 +02:00
|
|
|
let res = crate::num::trans_checked_int_binop(
|
2019-08-08 15:54:13 +02:00
|
|
|
fx,
|
|
|
|
bin_op,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
);
|
2018-10-03 18:21:52 +02:00
|
|
|
ret.write_cvalue(fx, res);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2019-08-18 11:30:13 +02:00
|
|
|
_ if intrinsic.starts_with("wrapping_"), (c x, c y) {
|
2018-10-05 19:55:06 +02:00
|
|
|
assert_eq!(x.layout().ty, y.layout().ty);
|
2018-10-03 18:21:52 +02:00
|
|
|
let bin_op = match intrinsic {
|
2019-08-18 11:30:13 +02:00
|
|
|
"wrapping_add" => BinOp::Add,
|
|
|
|
"wrapping_sub" => BinOp::Sub,
|
|
|
|
"wrapping_mul" => BinOp::Mul,
|
2020-01-25 16:24:48 +01:00
|
|
|
_ => unreachable!("intrinsic {}", intrinsic),
|
2018-10-03 18:21:52 +02:00
|
|
|
};
|
2019-08-14 11:52:39 +02:00
|
|
|
let res = crate::num::trans_int_binop(
|
2019-08-12 17:25:16 +02:00
|
|
|
fx,
|
|
|
|
bin_op,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
);
|
2018-10-03 18:21:52 +02:00
|
|
|
ret.write_cvalue(fx, res);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2019-08-20 10:40:08 +02:00
|
|
|
_ if intrinsic.starts_with("saturating_"), <T> (c lhs, c rhs) {
|
|
|
|
assert_eq!(lhs.layout().ty, rhs.layout().ty);
|
2019-03-04 18:57:09 +01:00
|
|
|
let bin_op = match intrinsic {
|
|
|
|
"saturating_add" => BinOp::Add,
|
|
|
|
"saturating_sub" => BinOp::Sub,
|
2020-01-25 16:24:48 +01:00
|
|
|
_ => unreachable!("intrinsic {}", intrinsic),
|
2019-03-04 18:57:09 +01:00
|
|
|
};
|
2019-08-08 15:54:13 +02:00
|
|
|
|
2019-08-12 17:25:16 +02:00
|
|
|
let signed = type_sign(T);
|
2019-08-08 15:54:13 +02:00
|
|
|
|
2019-08-14 11:52:39 +02:00
|
|
|
let checked_res = crate::num::trans_checked_int_binop(
|
2019-08-08 15:54:13 +02:00
|
|
|
fx,
|
|
|
|
bin_op,
|
2019-08-20 10:40:08 +02:00
|
|
|
lhs,
|
|
|
|
rhs,
|
2019-08-08 15:54:13 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let (val, has_overflow) = checked_res.load_scalar_pair(fx);
|
|
|
|
let clif_ty = fx.clif_type(T).unwrap();
|
|
|
|
|
|
|
|
// `select.i8` is not implemented by Cranelift.
|
|
|
|
let has_overflow = fx.bcx.ins().uextend(types::I32, has_overflow);
|
|
|
|
|
2019-08-12 17:25:16 +02:00
|
|
|
let (min, max) = type_min_max_value(clif_ty, signed);
|
2019-08-08 15:54:13 +02:00
|
|
|
let min = fx.bcx.ins().iconst(clif_ty, min);
|
|
|
|
let max = fx.bcx.ins().iconst(clif_ty, max);
|
|
|
|
|
|
|
|
let val = match (intrinsic, signed) {
|
2019-12-08 11:52:04 +01:00
|
|
|
("saturating_add", false) => fx.bcx.ins().select(has_overflow, max, val),
|
|
|
|
("saturating_sub", false) => fx.bcx.ins().select(has_overflow, min, val),
|
2019-08-20 10:40:08 +02:00
|
|
|
("saturating_add", true) => {
|
|
|
|
let rhs = rhs.load_scalar(fx);
|
|
|
|
let rhs_ge_zero = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
|
2019-12-08 11:52:04 +01:00
|
|
|
let sat_val = fx.bcx.ins().select(rhs_ge_zero, max, min);
|
|
|
|
fx.bcx.ins().select(has_overflow, sat_val, val)
|
2019-08-20 10:40:08 +02:00
|
|
|
}
|
|
|
|
("saturating_sub", true) => {
|
|
|
|
let rhs = rhs.load_scalar(fx);
|
|
|
|
let rhs_ge_zero = fx.bcx.ins().icmp_imm(IntCC::SignedGreaterThanOrEqual, rhs, 0);
|
2019-12-08 11:52:04 +01:00
|
|
|
let sat_val = fx.bcx.ins().select(rhs_ge_zero, min, max);
|
|
|
|
fx.bcx.ins().select(has_overflow, sat_val, val)
|
2019-08-20 10:40:08 +02:00
|
|
|
}
|
2019-08-08 15:54:13 +02:00
|
|
|
_ => unreachable!(),
|
2019-03-04 18:57:09 +01:00
|
|
|
};
|
2019-08-08 15:54:13 +02:00
|
|
|
|
|
|
|
let res = CValue::by_val(val, fx.layout_of(T));
|
|
|
|
|
2019-03-04 18:57:09 +01:00
|
|
|
ret.write_cvalue(fx, res);
|
|
|
|
};
|
2018-11-16 19:50:02 +01:00
|
|
|
rotate_left, <T>(v x, v y) {
|
|
|
|
let layout = fx.layout_of(T);
|
|
|
|
let res = fx.bcx.ins().rotl(x, y);
|
2019-06-11 16:25:07 +02:00
|
|
|
ret.write_cvalue(fx, CValue::by_val(res, layout));
|
2018-11-16 19:50:02 +01:00
|
|
|
};
|
|
|
|
rotate_right, <T>(v x, v y) {
|
|
|
|
let layout = fx.layout_of(T);
|
|
|
|
let res = fx.bcx.ins().rotr(x, y);
|
2019-06-11 16:25:07 +02:00
|
|
|
ret.write_cvalue(fx, CValue::by_val(res, layout));
|
2018-11-16 19:50:02 +01:00
|
|
|
};
|
2019-02-16 13:49:42 +01:00
|
|
|
|
|
|
|
// The only difference between offset and arith_offset is regarding UB. Because Cranelift
|
|
|
|
// doesn't have UB both are codegen'ed the same way
|
|
|
|
offset | arith_offset, (c base, v offset) {
|
2019-02-11 18:49:59 +01:00
|
|
|
let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
|
|
|
|
let pointee_size = fx.layout_of(pointee_ty).size.bytes();
|
|
|
|
let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
|
|
|
|
let base_val = base.load_scalar(fx);
|
|
|
|
let res = fx.bcx.ins().iadd(base_val, ptr_diff);
|
2019-07-28 11:24:33 +02:00
|
|
|
ret.write_cvalue(fx, CValue::by_val(res, base.layout()));
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2019-02-16 13:49:42 +01:00
|
|
|
|
2018-10-05 20:11:47 +02:00
|
|
|
transmute, <src_ty, dst_ty> (c from) {
|
2018-10-05 19:55:06 +02:00
|
|
|
assert_eq!(from.layout().ty, src_ty);
|
2020-01-25 16:24:48 +01:00
|
|
|
let (addr, meta) = from.force_stack(fx);
|
|
|
|
assert!(meta.is_none());
|
2018-10-03 18:21:52 +02:00
|
|
|
let dst_layout = fx.layout_of(dst_ty);
|
2019-06-11 15:32:30 +02:00
|
|
|
ret.write_cvalue(fx, CValue::by_ref(addr, dst_layout))
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2019-02-16 13:49:42 +01:00
|
|
|
write_bytes, (c dst, v val, v count) {
|
|
|
|
let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
|
|
|
|
let pointee_size = fx.layout_of(pointee_ty).size.bytes();
|
|
|
|
let count = fx.bcx.ins().imul_imm(count, pointee_size as i64);
|
|
|
|
let dst_ptr = dst.load_scalar(fx);
|
|
|
|
fx.bcx.call_memset(fx.module.target_config(), dst_ptr, val, count);
|
2018-10-06 10:43:01 +02:00
|
|
|
};
|
2018-10-05 20:11:47 +02:00
|
|
|
ctlz | ctlz_nonzero, <T> (v arg) {
|
2019-08-13 11:46:59 +02:00
|
|
|
// FIXME trap on `ctlz_nonzero` with zero arg.
|
2019-06-29 16:43:20 +02:00
|
|
|
let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
|
|
|
|
// FIXME verify this algorithm is correct
|
|
|
|
let (lsb, msb) = fx.bcx.ins().isplit(arg);
|
|
|
|
let lsb_lz = fx.bcx.ins().clz(lsb);
|
|
|
|
let msb_lz = fx.bcx.ins().clz(msb);
|
2019-07-24 17:16:31 +02:00
|
|
|
let msb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, msb, 0);
|
2019-06-29 16:43:20 +02:00
|
|
|
let lsb_lz_plus_64 = fx.bcx.ins().iadd_imm(lsb_lz, 64);
|
2019-12-20 12:01:55 +01:00
|
|
|
let res = fx.bcx.ins().select(msb_is_zero, lsb_lz_plus_64, msb_lz);
|
|
|
|
fx.bcx.ins().uextend(types::I128, res)
|
2019-06-29 16:43:20 +02:00
|
|
|
} else {
|
|
|
|
fx.bcx.ins().clz(arg)
|
|
|
|
};
|
|
|
|
let res = CValue::by_val(res, fx.layout_of(T));
|
2018-10-03 18:21:52 +02:00
|
|
|
ret.write_cvalue(fx, res);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2018-10-05 20:11:47 +02:00
|
|
|
cttz | cttz_nonzero, <T> (v arg) {
|
2019-08-13 11:46:59 +02:00
|
|
|
// FIXME trap on `cttz_nonzero` with zero arg.
|
2019-06-29 16:43:20 +02:00
|
|
|
let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
|
|
|
|
// FIXME verify this algorithm is correct
|
|
|
|
let (lsb, msb) = fx.bcx.ins().isplit(arg);
|
|
|
|
let lsb_tz = fx.bcx.ins().ctz(lsb);
|
|
|
|
let msb_tz = fx.bcx.ins().ctz(msb);
|
2019-07-24 17:16:31 +02:00
|
|
|
let lsb_is_zero = fx.bcx.ins().icmp_imm(IntCC::Equal, lsb, 0);
|
|
|
|
let msb_tz_plus_64 = fx.bcx.ins().iadd_imm(msb_tz, 64);
|
2019-12-20 12:01:55 +01:00
|
|
|
let res = fx.bcx.ins().select(lsb_is_zero, msb_tz_plus_64, lsb_tz);
|
|
|
|
fx.bcx.ins().uextend(types::I128, res)
|
2019-06-29 16:43:20 +02:00
|
|
|
} else {
|
|
|
|
fx.bcx.ins().ctz(arg)
|
|
|
|
};
|
|
|
|
let res = CValue::by_val(res, fx.layout_of(T));
|
2018-10-03 18:21:52 +02:00
|
|
|
ret.write_cvalue(fx, res);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2018-10-05 20:11:47 +02:00
|
|
|
ctpop, <T> (v arg) {
|
2019-12-08 11:52:04 +01:00
|
|
|
let res = fx.bcx.ins().popcnt(arg);
|
2019-10-06 15:51:43 +02:00
|
|
|
let res = CValue::by_val(res, fx.layout_of(T));
|
2018-10-03 18:21:52 +02:00
|
|
|
ret.write_cvalue(fx, res);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2018-10-05 20:11:47 +02:00
|
|
|
bitreverse, <T> (v arg) {
|
2019-12-08 11:52:04 +01:00
|
|
|
let res = fx.bcx.ins().bitrev(arg);
|
2019-10-06 15:51:43 +02:00
|
|
|
let res = CValue::by_val(res, fx.layout_of(T));
|
2018-10-03 18:21:52 +02:00
|
|
|
ret.write_cvalue(fx, res);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2018-11-17 18:52:47 +01:00
|
|
|
bswap, <T> (v arg) {
|
|
|
|
// FIXME(CraneStation/cranelift#794) add bswap instruction to cranelift
|
|
|
|
fn swap(bcx: &mut FunctionBuilder, v: Value) -> Value {
|
|
|
|
match bcx.func.dfg.value_type(v) {
|
|
|
|
types::I8 => v,
|
|
|
|
|
|
|
|
// https://code.woboq.org/gcc/include/bits/byteswap.h.html
|
|
|
|
types::I16 => {
|
|
|
|
let tmp1 = bcx.ins().ishl_imm(v, 8);
|
|
|
|
let n1 = bcx.ins().band_imm(tmp1, 0xFF00);
|
|
|
|
|
|
|
|
let tmp2 = bcx.ins().ushr_imm(v, 8);
|
|
|
|
let n2 = bcx.ins().band_imm(tmp2, 0x00FF);
|
|
|
|
|
|
|
|
bcx.ins().bor(n1, n2)
|
|
|
|
}
|
|
|
|
types::I32 => {
|
|
|
|
let tmp1 = bcx.ins().ishl_imm(v, 24);
|
|
|
|
let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000);
|
|
|
|
|
|
|
|
let tmp2 = bcx.ins().ishl_imm(v, 8);
|
|
|
|
let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000);
|
|
|
|
|
|
|
|
let tmp3 = bcx.ins().ushr_imm(v, 8);
|
|
|
|
let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00);
|
|
|
|
|
|
|
|
let tmp4 = bcx.ins().ushr_imm(v, 24);
|
|
|
|
let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF);
|
|
|
|
|
|
|
|
let or_tmp1 = bcx.ins().bor(n1, n2);
|
|
|
|
let or_tmp2 = bcx.ins().bor(n3, n4);
|
|
|
|
bcx.ins().bor(or_tmp1, or_tmp2)
|
|
|
|
}
|
|
|
|
types::I64 => {
|
|
|
|
let tmp1 = bcx.ins().ishl_imm(v, 56);
|
|
|
|
let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000_0000_0000u64 as i64);
|
|
|
|
|
|
|
|
let tmp2 = bcx.ins().ishl_imm(v, 40);
|
|
|
|
let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000_0000_0000u64 as i64);
|
|
|
|
|
|
|
|
let tmp3 = bcx.ins().ishl_imm(v, 24);
|
|
|
|
let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00_0000_0000u64 as i64);
|
|
|
|
|
|
|
|
let tmp4 = bcx.ins().ishl_imm(v, 8);
|
|
|
|
let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF_0000_0000u64 as i64);
|
|
|
|
|
|
|
|
let tmp5 = bcx.ins().ushr_imm(v, 8);
|
|
|
|
let n5 = bcx.ins().band_imm(tmp5, 0x0000_0000_FF00_0000u64 as i64);
|
|
|
|
|
|
|
|
let tmp6 = bcx.ins().ushr_imm(v, 24);
|
|
|
|
let n6 = bcx.ins().band_imm(tmp6, 0x0000_0000_00FF_0000u64 as i64);
|
|
|
|
|
|
|
|
let tmp7 = bcx.ins().ushr_imm(v, 40);
|
|
|
|
let n7 = bcx.ins().band_imm(tmp7, 0x0000_0000_0000_FF00u64 as i64);
|
|
|
|
|
|
|
|
let tmp8 = bcx.ins().ushr_imm(v, 56);
|
|
|
|
let n8 = bcx.ins().band_imm(tmp8, 0x0000_0000_0000_00FFu64 as i64);
|
|
|
|
|
|
|
|
let or_tmp1 = bcx.ins().bor(n1, n2);
|
|
|
|
let or_tmp2 = bcx.ins().bor(n3, n4);
|
|
|
|
let or_tmp3 = bcx.ins().bor(n5, n6);
|
|
|
|
let or_tmp4 = bcx.ins().bor(n7, n8);
|
|
|
|
|
|
|
|
let or_tmp5 = bcx.ins().bor(or_tmp1, or_tmp2);
|
|
|
|
let or_tmp6 = bcx.ins().bor(or_tmp3, or_tmp4);
|
|
|
|
bcx.ins().bor(or_tmp5, or_tmp6)
|
|
|
|
}
|
2019-06-29 16:53:20 +02:00
|
|
|
types::I128 => {
|
|
|
|
let (lo, hi) = bcx.ins().isplit(v);
|
|
|
|
let lo = swap(bcx, lo);
|
|
|
|
let hi = swap(bcx, hi);
|
|
|
|
bcx.ins().iconcat(hi, lo)
|
|
|
|
}
|
2020-01-25 16:24:48 +01:00
|
|
|
ty => unreachable!("bswap {}", ty),
|
2018-11-17 18:52:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let res = CValue::by_val(swap(&mut fx.bcx, arg), fx.layout_of(T));
|
|
|
|
ret.write_cvalue(fx, res);
|
|
|
|
};
|
2020-03-17 15:12:15 +01:00
|
|
|
assert_inhabited | assert_zero_valid | assert_uninit_valid, <T> () {
|
2020-03-12 21:04:00 +01:00
|
|
|
let layout = fx.layout_of(T);
|
|
|
|
if layout.abi.is_uninhabited() {
|
|
|
|
crate::trap::trap_panic(fx, &format!("attempted to instantiate uninhabited type `{}`", T));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-17 14:52:06 +01:00
|
|
|
if intrinsic == "assert_zero_valid" && !layout.might_permit_raw_init(fx, /*zero:*/ true).unwrap() {
|
2020-03-12 21:04:00 +01:00
|
|
|
crate::trap::trap_panic(fx, &format!("attempted to zero-initialize type `{}`, which is invalid", T));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-17 15:12:15 +01:00
|
|
|
if intrinsic == "assert_uninit_valid" && !layout.might_permit_raw_init(fx, /*zero:*/ false).unwrap() {
|
2020-03-12 21:04:00 +01:00
|
|
|
crate::trap::trap_panic(fx, &format!("attempted to leave type `{}` uninitialized, which is invalid", T));
|
2019-01-06 18:11:30 +01:00
|
|
|
return;
|
2019-01-06 15:27:20 +01:00
|
|
|
}
|
|
|
|
};
|
2018-10-06 10:24:09 +02:00
|
|
|
|
2019-06-23 16:33:34 +02:00
|
|
|
volatile_load, (c ptr) {
|
|
|
|
// Cranelift treats loads as volatile by default
|
2020-03-21 16:52:02 +01:00
|
|
|
// FIXME ignore during stack2reg optimization
|
2019-06-23 16:33:34 +02:00
|
|
|
let inner_layout =
|
|
|
|
fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
|
2019-12-20 16:02:47 +01:00
|
|
|
let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
|
2019-06-23 16:33:34 +02:00
|
|
|
ret.write_cvalue(fx, val);
|
|
|
|
};
|
|
|
|
volatile_store, (v ptr, c val) {
|
|
|
|
// Cranelift treats stores as volatile by default
|
2020-03-21 16:52:02 +01:00
|
|
|
// FIXME ignore during stack2reg optimization
|
2019-12-20 16:02:47 +01:00
|
|
|
let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
|
2019-06-23 16:33:34 +02:00
|
|
|
dest.write_cvalue(fx, val);
|
|
|
|
};
|
|
|
|
|
2019-09-21 11:30:29 +02:00
|
|
|
size_of | pref_align_of | min_align_of | needs_drop | type_id | type_name, () {
|
2019-12-23 13:25:22 +01:00
|
|
|
let const_val =
|
|
|
|
fx.tcx.const_eval_instance(ParamEnv::reveal_all(), instance, None).unwrap();
|
2020-02-22 14:20:37 +01:00
|
|
|
let val = crate::constant::trans_const_value(
|
|
|
|
fx,
|
|
|
|
ty::Const::from_value(fx.tcx, const_val, ret.layout().ty),
|
|
|
|
);
|
2019-09-21 11:30:29 +02:00
|
|
|
ret.write_cvalue(fx, val);
|
|
|
|
};
|
|
|
|
|
2019-11-09 11:14:18 +01:00
|
|
|
ptr_offset_from, <T> (v ptr, v base) {
|
|
|
|
let isize_layout = fx.layout_of(fx.tcx.types.isize);
|
|
|
|
|
|
|
|
let pointee_size: u64 = fx.layout_of(T).size.bytes();
|
|
|
|
let diff = fx.bcx.ins().isub(ptr, base);
|
|
|
|
// FIXME this can be an exact division.
|
|
|
|
let val = CValue::by_val(fx.bcx.ins().udiv_imm(diff, pointee_size as i64), isize_layout);
|
|
|
|
ret.write_cvalue(fx, val);
|
|
|
|
};
|
|
|
|
|
|
|
|
caller_location, () {
|
|
|
|
let caller_location = fx.get_caller_location(span);
|
|
|
|
ret.write_cvalue(fx, caller_location);
|
|
|
|
};
|
|
|
|
|
2020-01-18 12:43:31 +01:00
|
|
|
_ if intrinsic.starts_with("atomic_fence"), () {
|
|
|
|
crate::atomic_shim::lock_global_lock(fx);
|
|
|
|
crate::atomic_shim::unlock_global_lock(fx);
|
|
|
|
};
|
|
|
|
_ if intrinsic.starts_with("atomic_singlethreadfence"), () {
|
|
|
|
crate::atomic_shim::lock_global_lock(fx);
|
|
|
|
crate::atomic_shim::unlock_global_lock(fx);
|
|
|
|
};
|
2018-10-05 20:11:47 +02:00
|
|
|
_ if intrinsic.starts_with("atomic_load"), (c ptr) {
|
2020-01-18 12:43:31 +01:00
|
|
|
crate::atomic_shim::lock_global_lock(fx);
|
|
|
|
|
2018-10-03 18:21:52 +02:00
|
|
|
let inner_layout =
|
2018-10-05 19:55:06 +02:00
|
|
|
fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
|
2019-12-20 16:02:47 +01:00
|
|
|
let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout);
|
2018-10-03 18:21:52 +02:00
|
|
|
ret.write_cvalue(fx, val);
|
2020-01-18 12:43:31 +01:00
|
|
|
|
|
|
|
crate::atomic_shim::unlock_global_lock(fx);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2018-10-05 20:11:47 +02:00
|
|
|
_ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
|
2020-01-18 12:43:31 +01:00
|
|
|
crate::atomic_shim::lock_global_lock(fx);
|
|
|
|
|
2019-12-20 16:02:47 +01:00
|
|
|
let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout());
|
2018-10-05 19:55:06 +02:00
|
|
|
dest.write_cvalue(fx, val);
|
2020-01-18 12:43:31 +01:00
|
|
|
|
|
|
|
crate::atomic_shim::unlock_global_lock(fx);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2018-10-06 10:24:09 +02:00
|
|
|
_ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
|
2020-01-18 12:43:31 +01:00
|
|
|
crate::atomic_shim::lock_global_lock(fx);
|
|
|
|
|
2018-10-06 10:24:09 +02:00
|
|
|
// Read old
|
2018-11-12 07:23:39 -08:00
|
|
|
let clif_ty = fx.clif_type(T).unwrap();
|
2018-10-03 18:21:52 +02:00
|
|
|
let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
|
2019-06-11 16:25:07 +02:00
|
|
|
ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
|
2018-10-06 10:24:09 +02:00
|
|
|
|
|
|
|
// Write new
|
2019-12-20 16:02:47 +01:00
|
|
|
let dest = CPlace::for_ptr(Pointer::new(ptr), src.layout());
|
2018-10-06 10:24:09 +02:00
|
|
|
dest.write_cvalue(fx, src);
|
2020-01-18 12:43:31 +01:00
|
|
|
|
|
|
|
crate::atomic_shim::unlock_global_lock(fx);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2018-10-06 10:24:09 +02:00
|
|
|
_ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*
|
2020-01-18 12:43:31 +01:00
|
|
|
crate::atomic_shim::lock_global_lock(fx);
|
|
|
|
|
2018-10-06 10:24:09 +02:00
|
|
|
// Read old
|
2018-11-12 07:23:39 -08:00
|
|
|
let clif_ty = fx.clif_type(T).unwrap();
|
2018-10-03 18:21:52 +02:00
|
|
|
let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
|
2018-10-06 10:24:09 +02:00
|
|
|
|
|
|
|
// Compare
|
2019-08-15 11:36:06 +02:00
|
|
|
let is_eq = codegen_icmp(fx, IntCC::Equal, old, test_old);
|
2019-12-08 11:52:04 +01:00
|
|
|
let new = fx.bcx.ins().select(is_eq, new, old); // Keep old if not equal to test_old
|
2018-10-06 10:24:09 +02:00
|
|
|
|
|
|
|
// Write new
|
2018-10-03 18:21:52 +02:00
|
|
|
fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
|
2018-10-06 10:24:09 +02:00
|
|
|
|
2019-06-11 15:32:30 +02:00
|
|
|
let ret_val = CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
|
2018-10-06 10:24:09 +02:00
|
|
|
ret.write_cvalue(fx, ret_val);
|
2020-01-18 12:43:31 +01:00
|
|
|
|
|
|
|
crate::atomic_shim::unlock_global_lock(fx);
|
2018-10-06 10:24:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
_ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
|
|
|
|
atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
|
|
|
|
};
|
|
|
|
_ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
|
|
|
|
atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
|
|
|
|
};
|
|
|
|
_ if intrinsic.starts_with("atomic_and"), <T> (v ptr, v src) {
|
|
|
|
atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
|
|
|
|
};
|
|
|
|
_ if intrinsic.starts_with("atomic_nand"), <T> (v ptr, v src) {
|
2020-01-18 12:43:31 +01:00
|
|
|
crate::atomic_shim::lock_global_lock(fx);
|
|
|
|
|
2019-08-01 10:58:18 +02:00
|
|
|
let clif_ty = fx.clif_type(T).unwrap();
|
|
|
|
let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
|
|
|
|
let and = fx.bcx.ins().band(old, src);
|
|
|
|
let new = fx.bcx.ins().bnot(and);
|
|
|
|
fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
|
|
|
|
ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
|
2020-01-18 12:43:31 +01:00
|
|
|
|
|
|
|
crate::atomic_shim::unlock_global_lock(fx);
|
2018-10-06 10:24:09 +02:00
|
|
|
};
|
|
|
|
_ if intrinsic.starts_with("atomic_or"), <T> (v ptr, v src) {
|
|
|
|
atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
|
|
|
|
};
|
|
|
|
_ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, v src) {
|
|
|
|
atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
|
|
|
|
};
|
|
|
|
|
|
|
|
_ if intrinsic.starts_with("atomic_max"), <T> (v ptr, v src) {
|
|
|
|
atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
|
|
|
|
};
|
|
|
|
_ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, v src) {
|
|
|
|
atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
|
|
|
|
};
|
|
|
|
_ if intrinsic.starts_with("atomic_min"), <T> (v ptr, v src) {
|
|
|
|
atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
|
|
|
|
};
|
|
|
|
_ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, v src) {
|
|
|
|
atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
|
2018-10-05 19:55:06 +02:00
|
|
|
};
|
2019-07-27 17:48:24 +02:00
|
|
|
|
|
|
|
minnumf32, (v a, v b) {
|
|
|
|
let val = fx.bcx.ins().fmin(a, b);
|
|
|
|
let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
|
|
|
|
ret.write_cvalue(fx, val);
|
|
|
|
};
|
|
|
|
minnumf64, (v a, v b) {
|
|
|
|
let val = fx.bcx.ins().fmin(a, b);
|
|
|
|
let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
|
|
|
|
ret.write_cvalue(fx, val);
|
|
|
|
};
|
|
|
|
maxnumf32, (v a, v b) {
|
|
|
|
let val = fx.bcx.ins().fmax(a, b);
|
|
|
|
let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32));
|
|
|
|
ret.write_cvalue(fx, val);
|
|
|
|
};
|
|
|
|
maxnumf64, (v a, v b) {
|
|
|
|
let val = fx.bcx.ins().fmax(a, b);
|
|
|
|
let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64));
|
|
|
|
ret.write_cvalue(fx, val);
|
|
|
|
};
|
|
|
|
|
2020-03-17 14:53:32 +01:00
|
|
|
try, (v f, v data, v _catch_fn) {
|
2019-07-31 14:04:00 +02:00
|
|
|
// FIXME once unwinding is supported, change this to actually catch panics
|
|
|
|
let f_sig = fx.bcx.func.import_signature(Signature {
|
2019-12-17 15:03:32 +01:00
|
|
|
call_conv: CallConv::triple_default(fx.triple()),
|
2019-07-31 14:04:00 +02:00
|
|
|
params: vec![AbiParam::new(fx.bcx.func.dfg.value_type(data))],
|
|
|
|
returns: vec![],
|
|
|
|
});
|
|
|
|
|
|
|
|
fx.bcx.ins().call_indirect(f_sig, f, &[data]);
|
|
|
|
|
2020-01-15 13:17:09 +01:00
|
|
|
let ret_val = CValue::const_val(fx, ret.layout(), 0);
|
2019-07-31 14:04:00 +02:00
|
|
|
ret.write_cvalue(fx, ret_val);
|
|
|
|
};
|
2018-10-03 18:21:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((_, dest)) = destination {
|
2020-02-14 18:23:29 +01:00
|
|
|
let ret_block = fx.get_block(dest);
|
|
|
|
fx.bcx.ins().jump(ret_block, &[]);
|
2018-10-03 18:21:52 +02:00
|
|
|
} else {
|
2019-03-23 13:06:35 +01:00
|
|
|
trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
|
2018-10-03 18:21:52 +02:00
|
|
|
}
|
|
|
|
}
|