2020-03-17 14:11:51 +01:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_apfloat::Float;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir;
|
|
|
|
use rustc_middle::mir::interpret::{InterpResult, Scalar};
|
2021-08-30 17:38:27 +03:00
|
|
|
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
|
|
|
|
use rustc_middle::ty::{self, FloatTy, Ty};
|
2016-03-13 01:43:28 -06:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
use super::{ImmTy, Immediate, InterpCx, Machine, PlaceTy};
|
2016-10-20 04:42:19 -06:00
|
|
|
|
2020-03-16 15:12:42 -07:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2016-12-10 17:03:12 -08:00
|
|
|
/// Applies the binary operation `op` to the two operands and writes a tuple of the result
|
|
|
|
/// and a boolean signifying the potential overflow to the destination.
|
2018-08-13 16:14:22 +02:00
|
|
|
pub fn binop_with_overflow(
|
2016-12-10 17:03:12 -08:00
|
|
|
&mut self,
|
|
|
|
op: mir::BinOp,
|
2021-02-15 00:00:00 +00:00
|
|
|
left: &ImmTy<'tcx, M::PointerTag>,
|
|
|
|
right: &ImmTy<'tcx, M::PointerTag>,
|
2021-02-15 00:00:00 +00:00
|
|
|
dest: &PlaceTy<'tcx, M::PointerTag>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2021-02-15 00:00:00 +00:00
|
|
|
let (val, overflowed, ty) = self.overflowing_binary_op(op, &left, &right)?;
|
2019-08-10 19:40:56 +02:00
|
|
|
debug_assert_eq!(
|
|
|
|
self.tcx.intern_tup(&[ty, self.tcx.types.bool]),
|
|
|
|
dest.layout.ty,
|
2019-12-22 17:42:04 -05:00
|
|
|
"type mismatch for result of {:?}",
|
|
|
|
op,
|
2019-08-10 19:40:56 +02:00
|
|
|
);
|
2018-10-26 12:33:26 +02:00
|
|
|
let val = Immediate::ScalarPair(val.into(), Scalar::from_bool(overflowed).into());
|
|
|
|
self.write_immediate(val, dest)
|
2016-12-10 17:03:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Applies the binary operation `op` to the arguments and writes the result to the
|
2018-08-13 16:14:22 +02:00
|
|
|
/// destination.
|
|
|
|
pub fn binop_ignore_overflow(
|
2016-12-10 17:03:12 -08:00
|
|
|
&mut self,
|
|
|
|
op: mir::BinOp,
|
2021-02-15 00:00:00 +00:00
|
|
|
left: &ImmTy<'tcx, M::PointerTag>,
|
|
|
|
right: &ImmTy<'tcx, M::PointerTag>,
|
2021-02-15 00:00:00 +00:00
|
|
|
dest: &PlaceTy<'tcx, M::PointerTag>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-08-10 19:40:56 +02:00
|
|
|
let (val, _overflowed, ty) = self.overflowing_binary_op(op, left, right)?;
|
|
|
|
assert_eq!(ty, dest.layout.ty, "type mismatch for result of {:?}", op);
|
2018-08-13 16:14:22 +02:00
|
|
|
self.write_scalar(val, dest)
|
2016-12-10 17:03:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 15:12:42 -07:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2018-08-26 14:22:59 +02:00
|
|
|
fn binary_char_op(
|
2017-06-04 17:26:19 -07:00
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
2018-08-26 14:22:59 +02:00
|
|
|
l: char,
|
|
|
|
r: char,
|
2019-08-10 19:40:56 +02:00
|
|
|
) -> (Scalar<M::PointerTag>, bool, Ty<'tcx>) {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::BinOp::*;
|
2016-10-20 04:42:19 -06:00
|
|
|
|
2018-08-26 14:22:59 +02:00
|
|
|
let res = match bin_op {
|
|
|
|
Eq => l == r,
|
|
|
|
Ne => l != r,
|
|
|
|
Lt => l < r,
|
|
|
|
Le => l <= r,
|
|
|
|
Gt => l > r,
|
|
|
|
Ge => l >= r,
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "Invalid operation on char: {:?}", bin_op),
|
2018-08-26 14:22:59 +02:00
|
|
|
};
|
2020-03-20 15:03:11 +01:00
|
|
|
(Scalar::from_bool(res), false, self.tcx.types.bool)
|
2018-08-26 14:22:59 +02:00
|
|
|
}
|
2018-05-24 11:21:23 +02:00
|
|
|
|
2018-08-26 14:22:59 +02:00
|
|
|
fn binary_bool_op(
|
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
|
|
|
l: bool,
|
|
|
|
r: bool,
|
2019-08-10 19:40:56 +02:00
|
|
|
) -> (Scalar<M::PointerTag>, bool, Ty<'tcx>) {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::BinOp::*;
|
2018-08-24 18:36:52 +02:00
|
|
|
|
2018-08-26 14:22:59 +02:00
|
|
|
let res = match bin_op {
|
|
|
|
Eq => l == r,
|
|
|
|
Ne => l != r,
|
|
|
|
Lt => l < r,
|
|
|
|
Le => l <= r,
|
|
|
|
Gt => l > r,
|
|
|
|
Ge => l >= r,
|
|
|
|
BitAnd => l & r,
|
|
|
|
BitOr => l | r,
|
|
|
|
BitXor => l ^ r,
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "Invalid operation on bool: {:?}", bin_op),
|
2018-08-26 14:22:59 +02:00
|
|
|
};
|
2020-03-20 15:03:11 +01:00
|
|
|
(Scalar::from_bool(res), false, self.tcx.types.bool)
|
2018-08-26 14:22:59 +02:00
|
|
|
}
|
|
|
|
|
2019-06-09 00:41:20 +02:00
|
|
|
fn binary_float_op<F: Float + Into<Scalar<M::PointerTag>>>(
|
2018-08-26 14:22:59 +02:00
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
2019-08-10 19:40:56 +02:00
|
|
|
ty: Ty<'tcx>,
|
2019-06-09 00:41:20 +02:00
|
|
|
l: F,
|
|
|
|
r: F,
|
2019-08-10 19:40:56 +02:00
|
|
|
) -> (Scalar<M::PointerTag>, bool, Ty<'tcx>) {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::BinOp::*;
|
2018-08-26 14:22:59 +02:00
|
|
|
|
2019-08-10 19:40:56 +02:00
|
|
|
let (val, ty) = match bin_op {
|
|
|
|
Eq => (Scalar::from_bool(l == r), self.tcx.types.bool),
|
|
|
|
Ne => (Scalar::from_bool(l != r), self.tcx.types.bool),
|
|
|
|
Lt => (Scalar::from_bool(l < r), self.tcx.types.bool),
|
|
|
|
Le => (Scalar::from_bool(l <= r), self.tcx.types.bool),
|
|
|
|
Gt => (Scalar::from_bool(l > r), self.tcx.types.bool),
|
|
|
|
Ge => (Scalar::from_bool(l >= r), self.tcx.types.bool),
|
|
|
|
Add => ((l + r).value.into(), ty),
|
|
|
|
Sub => ((l - r).value.into(), ty),
|
|
|
|
Mul => ((l * r).value.into(), ty),
|
|
|
|
Div => ((l / r).value.into(), ty),
|
|
|
|
Rem => ((l % r).value.into(), ty),
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "invalid float op: `{:?}`", bin_op),
|
2019-06-09 00:41:20 +02:00
|
|
|
};
|
2020-03-20 15:03:11 +01:00
|
|
|
(val, false, ty)
|
2018-08-26 14:22:59 +02:00
|
|
|
}
|
2016-10-20 04:42:19 -06:00
|
|
|
|
2018-08-26 14:22:59 +02:00
|
|
|
fn binary_int_op(
|
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
|
|
|
// passing in raw bits
|
|
|
|
l: u128,
|
2020-03-04 14:50:21 +00:00
|
|
|
left_layout: TyAndLayout<'tcx>,
|
2018-08-26 14:22:59 +02:00
|
|
|
r: u128,
|
2020-03-04 14:50:21 +00:00
|
|
|
right_layout: TyAndLayout<'tcx>,
|
2019-08-10 19:40:56 +02:00
|
|
|
) -> InterpResult<'tcx, (Scalar<M::PointerTag>, bool, Ty<'tcx>)> {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::BinOp::*;
|
2018-02-22 14:59:18 +01:00
|
|
|
|
2018-08-24 18:36:52 +02:00
|
|
|
// Shift ops can have an RHS with a different numeric type.
|
|
|
|
if bin_op == Shl || bin_op == Shr {
|
2018-02-22 14:59:18 +01:00
|
|
|
let signed = left_layout.abi.is_signed();
|
2020-03-17 14:11:51 +01:00
|
|
|
let size = u128::from(left_layout.size.bits());
|
|
|
|
let overflow = r >= size;
|
2021-11-23 14:12:14 -05:00
|
|
|
// The shift offset is implicitly masked to the type size, to make sure this operation
|
|
|
|
// is always defined. This is the one MIR operator that does *not* directly map to a
|
|
|
|
// single LLVM operation. See
|
|
|
|
// <https://github.com/rust-lang/rust/blob/a3b9405ae7bb6ab4e8103b414e75c44598a10fd2/compiler/rustc_codegen_ssa/src/common.rs#L131-L158>
|
|
|
|
// for the corresponding truncation in our codegen backends.
|
|
|
|
let r = r % size;
|
2020-03-17 14:11:51 +01:00
|
|
|
let r = u32::try_from(r).unwrap(); // we masked so this will always fit
|
2018-02-22 14:59:18 +01:00
|
|
|
let result = if signed {
|
2018-07-24 18:28:53 +02:00
|
|
|
let l = self.sign_extend(l, left_layout) as i128;
|
2018-02-22 14:59:18 +01:00
|
|
|
let result = match bin_op {
|
2020-03-17 14:11:51 +01:00
|
|
|
Shl => l.checked_shl(r).unwrap(),
|
|
|
|
Shr => l.checked_shr(r).unwrap(),
|
2018-02-22 14:59:18 +01:00
|
|
|
_ => bug!("it has already been checked that this is a shift op"),
|
|
|
|
};
|
|
|
|
result as u128
|
2018-02-21 22:02:52 +01:00
|
|
|
} else {
|
2018-02-22 14:59:18 +01:00
|
|
|
match bin_op {
|
2020-03-17 14:11:51 +01:00
|
|
|
Shl => l.checked_shl(r).unwrap(),
|
|
|
|
Shr => l.checked_shr(r).unwrap(),
|
2018-02-22 14:59:18 +01:00
|
|
|
_ => bug!("it has already been checked that this is a shift op"),
|
|
|
|
}
|
2018-02-21 22:02:52 +01:00
|
|
|
};
|
2018-07-24 18:28:53 +02:00
|
|
|
let truncated = self.truncate(result, left_layout);
|
2020-03-17 14:11:51 +01:00
|
|
|
return Ok((Scalar::from_uint(truncated, left_layout.size), overflow, left_layout.ty));
|
2017-06-04 17:26:19 -07:00
|
|
|
}
|
|
|
|
|
2018-08-24 18:36:52 +02:00
|
|
|
// For the remaining ops, the types must be the same on both sides
|
|
|
|
if left_layout.ty != right_layout.ty {
|
2020-06-21 16:13:31 +02:00
|
|
|
span_bug!(
|
|
|
|
self.cur_span(),
|
2019-08-02 23:41:24 +02:00
|
|
|
"invalid asymmetric binary op {:?}: {:?} ({:?}), {:?} ({:?})",
|
2017-08-10 08:48:38 -07:00
|
|
|
bin_op,
|
2019-12-22 17:42:04 -05:00
|
|
|
l,
|
|
|
|
left_layout.ty,
|
|
|
|
r,
|
|
|
|
right_layout.ty,
|
2019-08-02 23:41:24 +02:00
|
|
|
)
|
2016-10-20 04:42:19 -06:00
|
|
|
}
|
2016-03-18 23:03:46 -06:00
|
|
|
|
2020-02-09 15:23:34 +01:00
|
|
|
let size = left_layout.size;
|
|
|
|
|
2018-08-24 18:36:52 +02:00
|
|
|
// Operations that need special treatment for signed integers
|
2018-02-22 14:59:18 +01:00
|
|
|
if left_layout.abi.is_signed() {
|
2018-02-21 22:02:52 +01:00
|
|
|
let op: Option<fn(&i128, &i128) -> bool> = match bin_op {
|
|
|
|
Lt => Some(i128::lt),
|
|
|
|
Le => Some(i128::le),
|
|
|
|
Gt => Some(i128::gt),
|
|
|
|
Ge => Some(i128::ge),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(op) = op {
|
2018-07-24 18:28:53 +02:00
|
|
|
let l = self.sign_extend(l, left_layout) as i128;
|
|
|
|
let r = self.sign_extend(r, right_layout) as i128;
|
2019-08-10 19:40:56 +02:00
|
|
|
return Ok((Scalar::from_bool(op(&l, &r)), false, self.tcx.types.bool));
|
2018-02-21 22:02:52 +01:00
|
|
|
}
|
|
|
|
let op: Option<fn(i128, i128) -> (i128, bool)> = match bin_op {
|
2019-12-01 12:08:05 +01:00
|
|
|
Div if r == 0 => throw_ub!(DivisionByZero),
|
|
|
|
Rem if r == 0 => throw_ub!(RemainderByZero),
|
2018-02-21 22:02:52 +01:00
|
|
|
Div => Some(i128::overflowing_div),
|
|
|
|
Rem => Some(i128::overflowing_rem),
|
|
|
|
Add => Some(i128::overflowing_add),
|
|
|
|
Sub => Some(i128::overflowing_sub),
|
|
|
|
Mul => Some(i128::overflowing_mul),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(op) = op {
|
2022-03-01 20:02:59 -05:00
|
|
|
let l = self.sign_extend(l, left_layout) as i128;
|
2018-07-24 18:28:53 +02:00
|
|
|
let r = self.sign_extend(r, right_layout) as i128;
|
2022-03-01 20:02:59 -05:00
|
|
|
|
|
|
|
// We need a special check for overflowing Rem and Div since they are *UB*
|
|
|
|
// on overflow, which can happen with "int_min $OP -1".
|
|
|
|
if matches!(bin_op, Rem | Div) {
|
|
|
|
if l == size.signed_int_min() && r == -1 {
|
|
|
|
if bin_op == Rem {
|
|
|
|
throw_ub!(RemainderOverflow)
|
|
|
|
} else {
|
|
|
|
throw_ub!(DivisionOverflow)
|
|
|
|
}
|
2020-02-09 15:41:40 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-09 15:23:34 +01:00
|
|
|
|
2020-03-17 14:11:51 +01:00
|
|
|
let (result, oflo) = op(l, r);
|
2020-02-09 15:23:34 +01:00
|
|
|
// This may be out-of-bounds for the result type, so we have to truncate ourselves.
|
|
|
|
// If that truncation loses any information, we have an overflow.
|
2018-02-21 22:02:52 +01:00
|
|
|
let result = result as u128;
|
2018-07-24 18:28:53 +02:00
|
|
|
let truncated = self.truncate(result, left_layout);
|
2020-02-09 15:23:34 +01:00
|
|
|
return Ok((
|
|
|
|
Scalar::from_uint(truncated, size),
|
|
|
|
oflo || self.sign_extend(truncated, left_layout) != result,
|
|
|
|
left_layout.ty,
|
|
|
|
));
|
2018-02-21 22:02:52 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-04 17:26:19 -07:00
|
|
|
|
2019-08-10 19:40:56 +02:00
|
|
|
let (val, ty) = match bin_op {
|
|
|
|
Eq => (Scalar::from_bool(l == r), self.tcx.types.bool),
|
|
|
|
Ne => (Scalar::from_bool(l != r), self.tcx.types.bool),
|
2018-02-21 22:02:52 +01:00
|
|
|
|
2019-08-10 19:40:56 +02:00
|
|
|
Lt => (Scalar::from_bool(l < r), self.tcx.types.bool),
|
|
|
|
Le => (Scalar::from_bool(l <= r), self.tcx.types.bool),
|
|
|
|
Gt => (Scalar::from_bool(l > r), self.tcx.types.bool),
|
|
|
|
Ge => (Scalar::from_bool(l >= r), self.tcx.types.bool),
|
2018-02-21 22:02:52 +01:00
|
|
|
|
2019-08-10 19:40:56 +02:00
|
|
|
BitOr => (Scalar::from_uint(l | r, size), left_layout.ty),
|
|
|
|
BitAnd => (Scalar::from_uint(l & r, size), left_layout.ty),
|
|
|
|
BitXor => (Scalar::from_uint(l ^ r, size), left_layout.ty),
|
2018-02-21 22:02:52 +01:00
|
|
|
|
|
|
|
Add | Sub | Mul | Rem | Div => {
|
2020-02-28 22:54:10 +01:00
|
|
|
assert!(!left_layout.abi.is_signed());
|
2018-02-21 22:02:52 +01:00
|
|
|
let op: fn(u128, u128) -> (u128, bool) = match bin_op {
|
|
|
|
Add => u128::overflowing_add,
|
|
|
|
Sub => u128::overflowing_sub,
|
|
|
|
Mul => u128::overflowing_mul,
|
2019-12-01 12:08:05 +01:00
|
|
|
Div if r == 0 => throw_ub!(DivisionByZero),
|
|
|
|
Rem if r == 0 => throw_ub!(RemainderByZero),
|
2018-02-21 22:02:52 +01:00
|
|
|
Div => u128::overflowing_div,
|
|
|
|
Rem => u128::overflowing_rem,
|
|
|
|
_ => bug!(),
|
|
|
|
};
|
|
|
|
let (result, oflo) = op(l, r);
|
2020-02-09 15:23:34 +01:00
|
|
|
// Truncate to target type.
|
|
|
|
// If that truncation loses any information, we have an overflow.
|
2018-07-24 18:28:53 +02:00
|
|
|
let truncated = self.truncate(result, left_layout);
|
2019-08-10 19:40:56 +02:00
|
|
|
return Ok((
|
|
|
|
Scalar::from_uint(truncated, size),
|
|
|
|
oflo || truncated != result,
|
|
|
|
left_layout.ty,
|
|
|
|
));
|
2018-02-21 22:02:52 +01:00
|
|
|
}
|
2017-06-04 17:26:19 -07:00
|
|
|
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(
|
|
|
|
self.cur_span(),
|
2019-12-22 17:42:04 -05:00
|
|
|
"invalid binary op {:?}: {:?}, {:?} (both {:?})",
|
|
|
|
bin_op,
|
|
|
|
l,
|
|
|
|
r,
|
|
|
|
right_layout.ty,
|
|
|
|
),
|
2017-06-04 17:26:19 -07:00
|
|
|
};
|
|
|
|
|
2019-08-10 19:40:56 +02:00
|
|
|
Ok((val, false, ty))
|
2017-06-04 17:26:19 -07:00
|
|
|
}
|
2016-10-20 04:42:19 -06:00
|
|
|
|
2019-08-10 19:40:56 +02:00
|
|
|
/// Returns the result of the specified operation, whether it overflowed, and
|
|
|
|
/// the result type.
|
|
|
|
pub fn overflowing_binary_op(
|
2018-08-28 01:14:29 +02:00
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
2021-02-15 00:00:00 +00:00
|
|
|
left: &ImmTy<'tcx, M::PointerTag>,
|
|
|
|
right: &ImmTy<'tcx, M::PointerTag>,
|
2019-08-10 19:40:56 +02:00
|
|
|
) -> InterpResult<'tcx, (Scalar<M::PointerTag>, bool, Ty<'tcx>)> {
|
2019-12-22 17:42:04 -05:00
|
|
|
trace!(
|
|
|
|
"Running binary op {:?}: {:?} ({:?}), {:?} ({:?})",
|
|
|
|
bin_op,
|
|
|
|
*left,
|
|
|
|
left.layout.ty,
|
|
|
|
*right,
|
|
|
|
right.layout.ty
|
|
|
|
);
|
2018-08-26 14:22:59 +02:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
match left.layout.ty.kind() {
|
2018-08-26 14:22:59 +02:00
|
|
|
ty::Char => {
|
2019-02-08 14:00:52 +01:00
|
|
|
assert_eq!(left.layout.ty, right.layout.ty);
|
2019-06-09 00:41:20 +02:00
|
|
|
let left = left.to_scalar()?;
|
|
|
|
let right = right.to_scalar()?;
|
|
|
|
Ok(self.binary_char_op(bin_op, left.to_char()?, right.to_char()?))
|
2018-08-26 14:22:59 +02:00
|
|
|
}
|
|
|
|
ty::Bool => {
|
2019-02-08 14:00:52 +01:00
|
|
|
assert_eq!(left.layout.ty, right.layout.ty);
|
2019-06-09 00:41:20 +02:00
|
|
|
let left = left.to_scalar()?;
|
|
|
|
let right = right.to_scalar()?;
|
|
|
|
Ok(self.binary_bool_op(bin_op, left.to_bool()?, right.to_bool()?))
|
2018-08-26 14:22:59 +02:00
|
|
|
}
|
|
|
|
ty::Float(fty) => {
|
2019-02-08 14:00:52 +01:00
|
|
|
assert_eq!(left.layout.ty, right.layout.ty);
|
2019-08-10 19:40:56 +02:00
|
|
|
let ty = left.layout.ty;
|
2019-06-09 00:41:20 +02:00
|
|
|
let left = left.to_scalar()?;
|
|
|
|
let right = right.to_scalar()?;
|
|
|
|
Ok(match fty {
|
2019-12-22 17:42:04 -05:00
|
|
|
FloatTy::F32 => {
|
|
|
|
self.binary_float_op(bin_op, ty, left.to_f32()?, right.to_f32()?)
|
|
|
|
}
|
|
|
|
FloatTy::F64 => {
|
|
|
|
self.binary_float_op(bin_op, ty, left.to_f64()?, right.to_f64()?)
|
|
|
|
}
|
2019-06-09 00:41:20 +02:00
|
|
|
})
|
2018-08-26 14:22:59 +02:00
|
|
|
}
|
2019-07-24 16:08:50 +02:00
|
|
|
_ if left.layout.ty.is_integral() => {
|
|
|
|
// the RHS type can be different, e.g. for shifts -- but it has to be integral, too
|
2019-06-09 12:31:19 +02:00
|
|
|
assert!(
|
2019-07-24 16:08:50 +02:00
|
|
|
right.layout.ty.is_integral(),
|
|
|
|
"Unexpected types for BinOp: {:?} {:?} {:?}",
|
2019-12-22 17:42:04 -05:00
|
|
|
left.layout.ty,
|
|
|
|
bin_op,
|
|
|
|
right.layout.ty
|
2019-07-24 16:08:50 +02:00
|
|
|
);
|
2018-08-26 14:22:59 +02:00
|
|
|
|
2021-07-12 18:22:15 +02:00
|
|
|
let l = left.to_scalar()?.to_bits(left.layout.size)?;
|
|
|
|
let r = right.to_scalar()?.to_bits(right.layout.size)?;
|
2019-02-08 14:00:52 +01:00
|
|
|
self.binary_int_op(bin_op, l, left.layout, r, right.layout)
|
2018-08-26 14:22:59 +02:00
|
|
|
}
|
2019-07-25 01:02:41 +02:00
|
|
|
_ if left.layout.ty.is_any_ptr() => {
|
2021-12-14 00:15:50 +03:00
|
|
|
// The RHS type must be a `pointer` *or an integer type* (for `Offset`).
|
2021-12-14 19:29:29 +03:00
|
|
|
// (Even when both sides are pointers, their type might differ, see issue #91636)
|
2019-07-24 16:08:50 +02:00
|
|
|
assert!(
|
2021-12-13 12:59:31 +03:00
|
|
|
right.layout.ty.is_any_ptr() || right.layout.ty.is_integral(),
|
2019-07-24 16:08:50 +02:00
|
|
|
"Unexpected types for BinOp: {:?} {:?} {:?}",
|
2019-12-22 17:42:04 -05:00
|
|
|
left.layout.ty,
|
|
|
|
bin_op,
|
|
|
|
right.layout.ty
|
2019-07-24 16:08:50 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
M::binary_ptr_op(self, bin_op, left, right)
|
|
|
|
}
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(
|
|
|
|
self.cur_span(),
|
|
|
|
"Invalid MIR: bad LHS type for binop: {:?}",
|
|
|
|
left.layout.ty
|
|
|
|
),
|
2018-08-26 14:22:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 23:27:29 +01:00
|
|
|
/// Typed version of `overflowing_binary_op`, returning an `ImmTy`. Also ignores overflows.
|
2019-08-10 19:40:56 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn binary_op(
|
|
|
|
&self,
|
|
|
|
bin_op: mir::BinOp,
|
2021-02-15 00:00:00 +00:00
|
|
|
left: &ImmTy<'tcx, M::PointerTag>,
|
|
|
|
right: &ImmTy<'tcx, M::PointerTag>,
|
2019-08-10 19:40:56 +02:00
|
|
|
) -> InterpResult<'tcx, ImmTy<'tcx, M::PointerTag>> {
|
|
|
|
let (val, _overflow, ty) = self.overflowing_binary_op(bin_op, left, right)?;
|
|
|
|
Ok(ImmTy::from_scalar(val, self.layout_of(ty)?))
|
|
|
|
}
|
|
|
|
|
2020-02-08 23:27:29 +01:00
|
|
|
/// Returns the result of the specified operation, whether it overflowed, and
|
|
|
|
/// the result type.
|
|
|
|
pub fn overflowing_unary_op(
|
2018-02-21 22:02:52 +01:00
|
|
|
&self,
|
|
|
|
un_op: mir::UnOp,
|
2021-02-15 00:00:00 +00:00
|
|
|
val: &ImmTy<'tcx, M::PointerTag>,
|
2020-02-08 23:27:29 +01:00
|
|
|
) -> InterpResult<'tcx, (Scalar<M::PointerTag>, bool, Ty<'tcx>)> {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::UnOp::*;
|
2016-12-17 03:09:57 -08:00
|
|
|
|
2019-02-08 14:00:52 +01:00
|
|
|
let layout = val.layout;
|
|
|
|
let val = val.to_scalar()?;
|
2018-12-07 19:14:30 +02:00
|
|
|
trace!("Running unary op {:?}: {:?} ({:?})", un_op, val, layout.ty);
|
2016-10-20 04:42:19 -06:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
match layout.ty.kind() {
|
2018-08-26 15:13:01 +02:00
|
|
|
ty::Bool => {
|
|
|
|
let val = val.to_bool()?;
|
|
|
|
let res = match un_op {
|
|
|
|
Not => !val,
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "Invalid bool op {:?}", un_op),
|
2018-08-26 15:13:01 +02:00
|
|
|
};
|
2020-02-08 23:27:29 +01:00
|
|
|
Ok((Scalar::from_bool(res), false, self.tcx.types.bool))
|
2018-08-26 15:13:01 +02:00
|
|
|
}
|
|
|
|
ty::Float(fty) => {
|
|
|
|
let res = match (un_op, fty) {
|
2019-06-09 00:51:47 +02:00
|
|
|
(Neg, FloatTy::F32) => Scalar::from_f32(-val.to_f32()?),
|
|
|
|
(Neg, FloatTy::F64) => Scalar::from_f64(-val.to_f64()?),
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "Invalid float op {:?}", un_op),
|
2018-08-26 15:13:01 +02:00
|
|
|
};
|
2020-02-08 23:27:29 +01:00
|
|
|
Ok((res, false, layout.ty))
|
2018-08-26 15:13:01 +02:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
assert!(layout.ty.is_integral());
|
2021-07-12 18:22:15 +02:00
|
|
|
let val = val.to_bits(layout.size)?;
|
2020-02-08 23:27:29 +01:00
|
|
|
let (res, overflow) = match un_op {
|
|
|
|
Not => (self.truncate(!val, layout), false), // bitwise negation, then truncate
|
2018-08-26 15:13:01 +02:00
|
|
|
Neg => {
|
2020-02-08 23:27:29 +01:00
|
|
|
// arithmetic negation
|
2018-08-26 15:13:01 +02:00
|
|
|
assert!(layout.abi.is_signed());
|
2020-02-08 23:27:29 +01:00
|
|
|
let val = self.sign_extend(val, layout) as i128;
|
|
|
|
let (res, overflow) = val.overflowing_neg();
|
|
|
|
let res = res as u128;
|
|
|
|
// Truncate to target type.
|
|
|
|
// If that truncation loses any information, we have an overflow.
|
|
|
|
let truncated = self.truncate(res, layout);
|
|
|
|
(truncated, overflow || self.sign_extend(truncated, layout) != res)
|
2018-08-26 15:13:01 +02:00
|
|
|
}
|
|
|
|
};
|
2020-02-08 23:27:29 +01:00
|
|
|
Ok((Scalar::from_uint(res, layout.size), overflow, layout.ty))
|
2018-08-26 15:13:01 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-21 22:02:52 +01:00
|
|
|
}
|
2020-02-08 23:27:29 +01:00
|
|
|
|
|
|
|
pub fn unary_op(
|
|
|
|
&self,
|
|
|
|
un_op: mir::UnOp,
|
2021-02-15 00:00:00 +00:00
|
|
|
val: &ImmTy<'tcx, M::PointerTag>,
|
2020-02-08 23:27:29 +01:00
|
|
|
) -> InterpResult<'tcx, ImmTy<'tcx, M::PointerTag>> {
|
|
|
|
let (val, _overflow, ty) = self.overflowing_unary_op(un_op, val)?;
|
|
|
|
Ok(ImmTy::from_scalar(val, self.layout_of(ty)?))
|
|
|
|
}
|
2016-03-13 01:43:28 -06:00
|
|
|
}
|