rust/src/operator.rs

199 lines
8.0 KiB
Rust
Raw Normal View History

use rustc::ty;
2018-05-26 10:07:34 -05:00
use rustc::ty::layout::Primitive;
use rustc::mir;
2017-12-14 04:03:55 -06:00
use super::*;
use helpers::EvalContextExt as HelperEvalContextExt;
pub trait EvalContextExt<'tcx> {
fn ptr_op(
&self,
bin_op: mir::BinOp,
2018-05-26 10:07:34 -05:00
left: Scalar,
left_ty: ty::Ty<'tcx>,
2018-05-26 10:07:34 -05:00
right: Scalar,
right_ty: ty::Ty<'tcx>,
2018-05-26 10:07:34 -05:00
) -> EvalResult<'tcx, Option<(Scalar, bool)>>;
fn ptr_int_arithmetic(
&self,
bin_op: mir::BinOp,
2018-05-26 10:07:34 -05:00
left: Pointer,
right: i128,
signed: bool,
2018-05-26 10:07:34 -05:00
) -> EvalResult<'tcx, (Scalar, bool)>;
}
2018-01-14 11:59:13 -06:00
impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super::Evaluator<'tcx>> {
fn ptr_op(
&self,
bin_op: mir::BinOp,
2018-05-26 10:07:34 -05:00
left: Scalar,
left_ty: ty::Ty<'tcx>,
2018-05-26 10:07:34 -05:00
right: Scalar,
right_ty: ty::Ty<'tcx>,
2018-05-26 10:07:34 -05:00
) -> EvalResult<'tcx, Option<(Scalar, bool)>> {
use rustc::mir::BinOp::*;
2018-05-26 10:07:34 -05:00
use rustc::ty::layout::Integer::*;
let usize = Primitive::Int(match self.memory.pointer_size().bytes() {
1 => I8,
2 => I16,
4 => I32,
8 => I64,
16 => I128,
_ => unreachable!(),
}, false);
let isize = Primitive::Int(match self.memory.pointer_size().bytes() {
1 => I8,
2 => I16,
4 => I32,
8 => I64,
16 => I128,
_ => unreachable!(),
}, true);
let left_layout = self.layout_of(left_ty)?;
let left_kind = match left_layout.abi {
2018-05-26 10:07:34 -05:00
ty::layout::Abi::Scalar(ref scalar) => scalar.value,
_ => Err(EvalErrorKind::TypeNotPrimitive(left_ty))?,
};
let right_layout = self.layout_of(right_ty)?;
let right_kind = match right_layout.abi {
2018-05-26 10:07:34 -05:00
ty::layout::Abi::Scalar(ref scalar) => scalar.value,
_ => Err(EvalErrorKind::TypeNotPrimitive(right_ty))?,
};
match bin_op {
2018-05-26 10:07:34 -05:00
Offset if left_kind == Primitive::Pointer && right_kind == usize => {
let pointee_ty = left_ty
2018-01-14 11:59:13 -06:00
.builtin_deref(true)
.expect("Offset called on non-ptr type")
.ty;
let ptr = self.pointer_offset(
2018-07-10 10:32:38 -05:00
left,
pointee_ty,
2018-05-26 10:07:34 -05:00
right.to_bits(self.memory.pointer_size())? as i64,
)?;
2018-05-26 10:07:34 -05:00
Ok(Some((ptr, false)))
}
// These work on anything
Eq if left_kind == right_kind => {
let result = match (left, right) {
2018-05-26 10:07:34 -05:00
(Scalar::Bits { .. }, Scalar::Bits { .. }) => {
left.to_bits(left_layout.size)? == right.to_bits(right_layout.size)?
2018-05-26 10:07:34 -05:00
},
(Scalar::Ptr(left), Scalar::Ptr(right)) => left == right,
_ => false,
};
2018-05-26 10:07:34 -05:00
Ok(Some((Scalar::from_bool(result), false)))
}
Ne if left_kind == right_kind => {
let result = match (left, right) {
2018-05-26 10:07:34 -05:00
(Scalar::Bits { .. }, Scalar::Bits { .. }) => {
left.to_bits(left_layout.size)? != right.to_bits(right_layout.size)?
2018-05-26 10:07:34 -05:00
},
(Scalar::Ptr(left), Scalar::Ptr(right)) => left != right,
_ => true,
};
2018-05-26 10:07:34 -05:00
Ok(Some((Scalar::from_bool(result), false)))
}
// These need both pointers to be in the same allocation
Lt | Le | Gt | Ge | Sub
if left_kind == right_kind &&
2018-05-26 10:07:34 -05:00
(left_kind == Primitive::Pointer || left_kind == usize || left_kind == isize) &&
left.is_ptr() && right.is_ptr() => {
let left = left.to_ptr()?;
let right = right.to_ptr()?;
if left.alloc_id == right.alloc_id {
let res = match bin_op {
Lt => left.offset < right.offset,
Le => left.offset <= right.offset,
Gt => left.offset > right.offset,
Ge => left.offset >= right.offset,
Sub => {
return self.binary_op(
Sub,
2018-05-26 10:07:34 -05:00
Scalar::Bits { bits: left.offset.bytes() as u128, defined: self.memory.pointer_size().bits() as u8 },
self.tcx.types.usize,
2018-05-26 10:07:34 -05:00
Scalar::Bits { bits: right.offset.bytes() as u128, defined: self.memory.pointer_size().bits() as u8 },
self.tcx.types.usize,
).map(Some)
}
_ => bug!("We already established it has to be one of these operators."),
};
2018-05-26 10:07:34 -05:00
Ok(Some((Scalar::from_bool(res), false)))
} else {
// Both are pointers, but from different allocations.
2017-08-02 09:59:01 -05:00
err!(InvalidPointerMath)
}
}
// These work if one operand is a pointer, the other an integer
Add | BitAnd | Sub
if left_kind == right_kind && (left_kind == usize || left_kind == isize) &&
2018-05-26 10:07:34 -05:00
left.is_ptr() && right.is_bits() => {
// Cast to i128 is fine as we checked the kind to be ptr-sized
self.ptr_int_arithmetic(
bin_op,
left.to_ptr()?,
2018-05-26 10:07:34 -05:00
right.to_bits(self.memory.pointer_size())? as i128,
left_kind == isize,
).map(Some)
}
Add | BitAnd
if left_kind == right_kind && (left_kind == usize || left_kind == isize) &&
2018-05-26 10:07:34 -05:00
left.is_bits() && right.is_ptr() => {
// This is a commutative operation, just swap the operands
self.ptr_int_arithmetic(
bin_op,
right.to_ptr()?,
2018-05-26 10:07:34 -05:00
left.to_bits(self.memory.pointer_size())? as i128,
left_kind == isize,
).map(Some)
}
_ => Ok(None),
}
}
fn ptr_int_arithmetic(
&self,
bin_op: mir::BinOp,
2018-05-26 10:07:34 -05:00
left: Pointer,
right: i128,
signed: bool,
2018-05-26 10:07:34 -05:00
) -> EvalResult<'tcx, (Scalar, bool)> {
use rustc::mir::BinOp::*;
2018-05-26 10:07:34 -05:00
fn map_to_primval((res, over): (Pointer, bool)) -> (Scalar, bool) {
(Scalar::Ptr(res), over)
}
Ok(match bin_op {
Sub =>
// The only way this can overflow is by underflowing, so signdeness of the right operands does not matter
map_to_primval(left.overflowing_signed_offset(-right, self)),
Add if signed =>
map_to_primval(left.overflowing_signed_offset(right, self)),
Add if !signed =>
map_to_primval(left.overflowing_offset(Size::from_bytes(right as u64), self)),
BitAnd if !signed => {
2018-01-02 16:43:03 -06:00
let base_mask : u64 = !(self.memory.get(left.alloc_id)?.align.abi() - 1);
let right = right as u64;
if right & base_mask == base_mask {
// Case 1: The base address bits are all preserved, i.e., right is all-1 there
2018-05-26 10:07:34 -05:00
(Scalar::Ptr(Pointer::new(left.alloc_id, Size::from_bytes(left.offset.bytes() & right))), false)
} else if right & base_mask == 0 {
// Case 2: The base address bits are all taken away, i.e., right is all-0 there
2018-05-26 10:07:34 -05:00
(Scalar::Bits { bits: (left.offset.bytes() & right) as u128, defined: 128 }, false)
} else {
2017-08-02 09:59:01 -05:00
return err!(ReadPointerAsBytes);
}
}
_ => {
let msg = format!("unimplemented binary op on pointer {:?}: {:?}, {:?} ({})", bin_op, left, right, if signed { "signed" } else { "unsigned" });
2017-08-02 09:59:01 -05:00
return err!(Unimplemented(msg));
}
})
}
}