1
Fork 0
rust/src/librustc_mir/interpret/operator.rs

418 lines
16 KiB
Rust
Raw Normal View History

use rustc::mir;
use rustc::mir::interpret::{InterpResult, Scalar};
2019-12-22 17:42:04 -05:00
use rustc::ty::{
self,
layout::{LayoutOf, TyLayout},
Ty,
};
use rustc_apfloat::Float;
use rustc_ast::ast::FloatTy;
2019-12-22 17:42:04 -05:00
use super::{ImmTy, Immediate, InterpCx, Machine, PlaceTy};
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
/// 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.
pub fn binop_with_overflow(
&mut self,
op: mir::BinOp,
2018-10-26 12:33:26 +02:00
left: ImmTy<'tcx, M::PointerTag>,
right: ImmTy<'tcx, M::PointerTag>,
dest: PlaceTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx> {
let (val, overflowed, ty) = self.overflowing_binary_op(op, left, right)?;
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,
);
2018-10-26 12:33:26 +02:00
let val = Immediate::ScalarPair(val.into(), Scalar::from_bool(overflowed).into());
self.write_immediate(val, dest)
}
/// Applies the binary operation `op` to the arguments and writes the result to the
/// destination.
pub fn binop_ignore_overflow(
&mut self,
op: mir::BinOp,
2018-10-26 12:33:26 +02:00
left: ImmTy<'tcx, M::PointerTag>,
right: ImmTy<'tcx, M::PointerTag>,
dest: PlaceTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx> {
let (val, _overflowed, ty) = self.overflowing_binary_op(op, left, right)?;
assert_eq!(ty, dest.layout.ty, "type mismatch for result of {:?}", op);
self.write_scalar(val, dest)
}
}
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2018-08-26 14:22:59 +02:00
fn binary_char_op(
&self,
bin_op: mir::BinOp,
2018-08-26 14:22:59 +02:00
l: char,
r: char,
) -> (Scalar<M::PointerTag>, bool, Ty<'tcx>) {
use rustc::mir::BinOp::*;
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,
_ => bug!("Invalid operation on char: {:?}", bin_op),
};
return (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,
) -> (Scalar<M::PointerTag>, bool, Ty<'tcx>) {
2018-08-26 14:22:59 +02:00
use rustc::mir::BinOp::*;
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,
_ => bug!("Invalid operation on bool: {:?}", bin_op),
};
return (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,
ty: Ty<'tcx>,
2019-06-09 00:41:20 +02:00
l: F,
r: F,
) -> (Scalar<M::PointerTag>, bool, Ty<'tcx>) {
2018-08-26 14:22:59 +02:00
use rustc::mir::BinOp::*;
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),
2019-06-09 00:41:20 +02:00
_ => bug!("invalid float op: `{:?}`", bin_op),
};
return (val, false, ty);
2018-08-26 14:22:59 +02:00
}
2018-08-26 14:22:59 +02:00
fn binary_int_op(
&self,
bin_op: mir::BinOp,
// passing in raw bits
l: u128,
left_layout: TyLayout<'tcx>,
r: u128,
right_layout: TyLayout<'tcx>,
) -> InterpResult<'tcx, (Scalar<M::PointerTag>, bool, Ty<'tcx>)> {
2018-08-26 14:22:59 +02:00
use rustc::mir::BinOp::*;
// Shift ops can have an RHS with a different numeric type.
if bin_op == Shl || bin_op == Shr {
let signed = left_layout.abi.is_signed();
2018-06-27 13:47:19 +02:00
let mut oflo = (r as u32 as u128) != r;
let mut r = r as u32;
let size = left_layout.size;
oflo |= r >= size.bits() as u32;
r %= size.bits() as u32;
let result = if signed {
let l = self.sign_extend(l, left_layout) as i128;
let result = match bin_op {
Shl => l << r,
Shr => l >> r,
_ => bug!("it has already been checked that this is a shift op"),
};
result as u128
} else {
match bin_op {
Shl => l << r,
Shr => l >> r,
_ => bug!("it has already been checked that this is a shift op"),
}
};
let truncated = self.truncate(result, left_layout);
return Ok((Scalar::from_uint(truncated, size), oflo, left_layout.ty));
}
// For the remaining ops, the types must be the same on both sides
if left_layout.ty != right_layout.ty {
bug!(
"invalid asymmetric binary op {:?}: {:?} ({:?}), {:?} ({:?})",
bin_op,
2019-12-22 17:42:04 -05:00
l,
left_layout.ty,
r,
right_layout.ty,
)
}
let size = left_layout.size;
// Operations that need special treatment for signed integers
if left_layout.abi.is_signed() {
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 {
let l = self.sign_extend(l, left_layout) as i128;
let r = self.sign_extend(r, right_layout) as i128;
return Ok((Scalar::from_bool(op(&l, &r)), false, self.tcx.types.bool));
}
let op: Option<fn(i128, i128) -> (i128, bool)> = match bin_op {
Div if r == 0 => throw_ub!(DivisionByZero),
Rem if r == 0 => throw_ub!(RemainderByZero),
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 {
let l128 = self.sign_extend(l, left_layout) as i128;
let r = self.sign_extend(r, right_layout) as i128;
2020-02-09 15:41:40 +01:00
// We need a special check for overflowing remainder:
// "int_min % -1" overflows and returns 0, but after casting things to a larger int
// type it does *not* overflow nor give an unrepresentable result!
match bin_op {
Rem => {
if r == -1 && l == (1 << (size.bits() - 1)) {
return Ok((Scalar::from_int(0, size), true, left_layout.ty));
}
}
_ => {}
}
let (result, oflo) = op(l128, r);
// 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.
let result = result as u128;
let truncated = self.truncate(result, left_layout);
return Ok((
Scalar::from_uint(truncated, size),
oflo || self.sign_extend(truncated, left_layout) != result,
left_layout.ty,
));
}
}
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),
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),
Add | Sub | Mul | Rem | Div => {
assert!(!left_layout.abi.is_signed());
let op: fn(u128, u128) -> (u128, bool) = match bin_op {
Add => u128::overflowing_add,
Sub => u128::overflowing_sub,
Mul => u128::overflowing_mul,
Div if r == 0 => throw_ub!(DivisionByZero),
Rem if r == 0 => throw_ub!(RemainderByZero),
Div => u128::overflowing_div,
Rem => u128::overflowing_rem,
_ => bug!(),
};
let (result, oflo) = op(l, r);
// Truncate to target type.
// If that truncation loses any information, we have an overflow.
let truncated = self.truncate(result, left_layout);
return Ok((
Scalar::from_uint(truncated, size),
oflo || truncated != result,
left_layout.ty,
));
}
2019-12-22 17:42:04 -05:00
_ => bug!(
"invalid binary op {:?}: {:?}, {:?} (both {:?})",
bin_op,
l,
r,
right_layout.ty,
),
};
Ok((val, false, ty))
}
/// Returns the result of the specified operation, whether it overflowed, and
/// the result type.
pub fn overflowing_binary_op(
&self,
bin_op: mir::BinOp,
2018-10-26 12:33:26 +02:00
left: ImmTy<'tcx, M::PointerTag>,
right: ImmTy<'tcx, M::PointerTag>,
) -> 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
2019-09-16 19:08:35 +01:00
match left.layout.ty.kind {
2018-08-26 14:22:59 +02:00
ty::Char => {
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 => {
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) => {
assert_eq!(left.layout.ty, right.layout.ty);
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
}
_ if left.layout.ty.is_integral() => {
// the RHS type can be different, e.g. for shifts -- but it has to be integral, too
assert!(
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
);
2018-08-26 14:22:59 +02:00
let l = self.force_bits(left.to_scalar()?, left.layout.size)?;
let r = self.force_bits(right.to_scalar()?, right.layout.size)?;
self.binary_int_op(bin_op, l, left.layout, r, right.layout)
2018-08-26 14:22:59 +02:00
}
_ if left.layout.ty.is_any_ptr() => {
// The RHS type must be the same *or an integer type* (for `Offset`).
assert!(
right.layout.ty == left.layout.ty || 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
);
M::binary_ptr_op(self, bin_op, left, right)
}
_ => bug!("Invalid MIR: bad LHS type for binop: {:?}", left.layout.ty),
2018-08-26 14:22:59 +02:00
}
}
/// Typed version of `overflowing_binary_op`, returning an `ImmTy`. Also ignores overflows.
#[inline]
pub fn binary_op(
&self,
bin_op: mir::BinOp,
left: ImmTy<'tcx, M::PointerTag>,
right: ImmTy<'tcx, M::PointerTag>,
) -> 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)?))
}
/// Returns the result of the specified operation, whether it overflowed, and
/// the result type.
pub fn overflowing_unary_op(
&self,
un_op: mir::UnOp,
val: ImmTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx, (Scalar<M::PointerTag>, bool, Ty<'tcx>)> {
use rustc::mir::UnOp::*;
let layout = val.layout;
let val = val.to_scalar()?;
trace!("Running unary op {:?}: {:?} ({:?})", un_op, val, layout.ty);
2019-09-16 19:08:35 +01:00
match layout.ty.kind {
ty::Bool => {
let val = val.to_bool()?;
let res = match un_op {
Not => !val,
2019-12-22 17:42:04 -05:00
_ => bug!("Invalid bool op {:?}", un_op),
};
Ok((Scalar::from_bool(res), false, self.tcx.types.bool))
}
ty::Float(fty) => {
let res = match (un_op, fty) {
(Neg, FloatTy::F32) => Scalar::from_f32(-val.to_f32()?),
(Neg, FloatTy::F64) => Scalar::from_f64(-val.to_f64()?),
2019-12-22 17:42:04 -05:00
_ => bug!("Invalid float op {:?}", un_op),
};
Ok((res, false, layout.ty))
}
_ => {
assert!(layout.ty.is_integral());
let val = self.force_bits(val, layout.size)?;
let (res, overflow) = match un_op {
Not => (self.truncate(!val, layout), false), // bitwise negation, then truncate
Neg => {
// arithmetic negation
assert!(layout.abi.is_signed());
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)
}
};
Ok((Scalar::from_uint(res, layout.size), overflow, layout.ty))
}
}
}
pub fn unary_op(
&self,
un_op: mir::UnOp,
val: ImmTy<'tcx, M::PointerTag>,
) -> 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)?))
}
}