rust/src/operator.rs

83 lines
3.0 KiB
Rust
Raw Normal View History

use log::trace;
2020-04-02 17:05:35 -05:00
use rustc_middle::{mir, ty::Ty};
2018-11-01 02:56:41 -05:00
use crate::*;
pub trait EvalContextExt<'tcx> {
fn binary_ptr_op(
&self,
bin_op: mir::BinOp,
left: &ImmTy<'tcx, Tag>,
right: &ImmTy<'tcx, Tag>,
2019-08-10 14:19:25 -05:00
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)>;
2019-12-23 05:56:23 -06:00
fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool>;
}
2019-06-13 01:52:04 -05:00
impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriEvalContext<'mir, 'tcx> {
fn binary_ptr_op(
&self,
bin_op: mir::BinOp,
left: &ImmTy<'tcx, Tag>,
right: &ImmTy<'tcx, Tag>,
2019-08-10 14:19:25 -05:00
) -> InterpResult<'tcx, (Scalar<Tag>, bool, Ty<'tcx>)> {
use rustc_middle::mir::BinOp::*;
2019-02-08 09:27:00 -06:00
trace!("ptr_op: {:?} {:?} {:?}", *left, bin_op, *right);
Ok(match bin_op {
2019-02-08 09:27:00 -06:00
Eq | Ne => {
// This supports fat pointers.
2019-12-23 05:56:23 -06:00
#[rustfmt::skip]
let eq = match (**left, **right) {
2019-12-23 05:56:23 -06:00
(Immediate::Scalar(left), Immediate::Scalar(right)) => {
self.ptr_eq(left.check_init()?, right.check_init()?)?
2019-12-23 05:56:23 -06:00
}
(Immediate::ScalarPair(left1, left2), Immediate::ScalarPair(right1, right2)) => {
self.ptr_eq(left1.check_init()?, right1.check_init()?)?
&& self.ptr_eq(left2.check_init()?, right2.check_init()?)?
2019-12-23 05:56:23 -06:00
}
2019-02-08 09:27:00 -06:00
_ => bug!("Type system should not allow comparing Scalar with ScalarPair"),
};
2019-08-10 14:19:25 -05:00
(Scalar::from_bool(if bin_op == Eq { eq } else { !eq }), false, self.tcx.types.bool)
2019-02-08 09:27:00 -06:00
}
Lt | Le | Gt | Ge => {
// Just compare the integers.
2021-07-15 13:33:08 -05:00
let left = left.to_scalar()?.to_bits(left.layout.size)?;
let right = right.to_scalar()?.to_bits(right.layout.size)?;
let res = match bin_op {
Lt => left < right,
Le => left <= right,
Gt => left > right,
Ge => left >= right,
_ => bug!("We already established it has to be one of these operators."),
};
2019-08-10 14:19:25 -05:00
(Scalar::from_bool(res), false, self.tcx.types.bool)
}
Offset => {
2019-12-23 05:56:23 -06:00
let pointee_ty =
left.layout.ty.builtin_deref(true).expect("Offset called on non-ptr type").ty;
let ptr = self.ptr_offset_inbounds(
2021-07-15 13:33:08 -05:00
self.scalar_to_ptr(left.to_scalar()?),
pointee_ty,
2019-11-08 15:07:52 -06:00
right.to_scalar()?.to_machine_isize(self)?,
)?;
2021-07-15 13:33:08 -05:00
(Scalar::from_maybe_pointer(ptr, self), false, left.layout.ty)
}
2019-12-23 05:56:23 -06:00
_ => bug!("Invalid operator on pointers: {:?}", bin_op),
2019-08-03 03:25:55 -05:00
})
}
2019-12-23 05:56:23 -06:00
fn ptr_eq(&self, left: Scalar<Tag>, right: Scalar<Tag>) -> InterpResult<'tcx, bool> {
2019-02-08 09:27:00 -06:00
let size = self.pointer_size();
2019-07-23 14:38:53 -05:00
// Just compare the integers.
2021-07-15 13:33:08 -05:00
let left = left.to_bits(size)?;
let right = right.to_bits(size)?;
2019-07-23 14:38:53 -05:00
Ok(left == right)
}
}