4 use rustc_miri::interpret::*;
6 pub trait EvalContextExt<'tcx> {
11 left_ty: ty::Ty<'tcx>,
13 right_ty: ty::Ty<'tcx>,
14 ) -> EvalResult<'tcx, Option<(PrimVal, bool)>>;
16 fn ptr_int_arithmetic(
22 ) -> EvalResult<'tcx, (PrimVal, bool)>;
25 impl<'a, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, super::Evaluator> {
30 left_ty: ty::Ty<'tcx>,
32 right_ty: ty::Ty<'tcx>,
33 ) -> EvalResult<'tcx, Option<(PrimVal, bool)>> {
34 use rustc_miri::interpret::PrimValKind::*;
35 use rustc::mir::BinOp::*;
36 let usize = PrimValKind::from_uint_size(self.memory.pointer_size());
37 let isize = PrimValKind::from_int_size(self.memory.pointer_size());
38 let left_kind = self.ty_to_primval_kind(left_ty)?;
39 let right_kind = self.ty_to_primval_kind(right_ty)?;
41 Offset if left_kind == Ptr && right_kind == usize => {
42 let pointee_ty = left_ty.builtin_deref(true, ty::LvaluePreference::NoPreference).expect("Offset called on non-ptr type").ty;
43 let ptr = self.pointer_offset(left.into(), pointee_ty, right.to_bytes()? as i64)?;
44 Ok(Some((ptr.into_inner_primval(), false)))
46 // These work on anything
47 Eq if left_kind == right_kind => {
48 let result = match (left, right) {
49 (PrimVal::Bytes(left), PrimVal::Bytes(right)) => left == right,
50 (PrimVal::Ptr(left), PrimVal::Ptr(right)) => left == right,
51 (PrimVal::Undef, _) | (_, PrimVal::Undef) => return Err(EvalError::ReadUndefBytes),
54 Ok(Some((PrimVal::from_bool(result), false)))
56 Ne if left_kind == right_kind => {
57 let result = match (left, right) {
58 (PrimVal::Bytes(left), PrimVal::Bytes(right)) => left != right,
59 (PrimVal::Ptr(left), PrimVal::Ptr(right)) => left != right,
60 (PrimVal::Undef, _) | (_, PrimVal::Undef) => return Err(EvalError::ReadUndefBytes),
63 Ok(Some((PrimVal::from_bool(result), false)))
65 // These need both pointers to be in the same allocation
66 Lt | Le | Gt | Ge | Sub
67 if left_kind == right_kind
68 && (left_kind == Ptr || left_kind == usize || left_kind == isize)
69 && left.is_ptr() && right.is_ptr() => {
70 let left = left.to_ptr()?;
71 let right = right.to_ptr()?;
72 if left.alloc_id == right.alloc_id {
73 let res = match bin_op {
74 Lt => left.offset < right.offset,
75 Le => left.offset <= right.offset,
76 Gt => left.offset > right.offset,
77 Ge => left.offset >= right.offset,
78 Sub => return self.binary_op(
80 PrimVal::Bytes(left.offset as u128),
82 PrimVal::Bytes(right.offset as u128),
85 _ => bug!("We already established it has to be one of these operators."),
87 Ok(Some((PrimVal::from_bool(res), false)))
89 // Both are pointers, but from different allocations.
90 Err(EvalError::InvalidPointerMath)
93 // These work if one operand is a pointer, the other an integer
95 if left_kind == right_kind && (left_kind == usize || left_kind == isize)
96 && left.is_ptr() && right.is_bytes() => {
97 // Cast to i128 is fine as we checked the kind to be ptr-sized
98 self.ptr_int_arithmetic(bin_op, left.to_ptr()?, right.to_bytes()? as i128, left_kind == isize).map(Some)
101 if left_kind == right_kind && (left_kind == usize || left_kind == isize)
102 && left.is_bytes() && right.is_ptr() => {
103 // This is a commutative operation, just swap the operands
104 self.ptr_int_arithmetic(bin_op, right.to_ptr()?, left.to_bytes()? as i128, left_kind == isize).map(Some)
110 fn ptr_int_arithmetic(
116 ) -> EvalResult<'tcx, (PrimVal, bool)> {
117 use rustc::mir::BinOp::*;
119 fn map_to_primval((res, over) : (MemoryPointer, bool)) -> (PrimVal, bool) {
120 (PrimVal::Ptr(res), over)
125 // The only way this can overflow is by underflowing, so signdeness of the right operands does not matter
126 map_to_primval(left.overflowing_signed_offset(-right, self)),
128 map_to_primval(left.overflowing_signed_offset(right, self)),
130 map_to_primval(left.overflowing_offset(right as u64, self)),
132 BitAnd if !signed => {
133 let base_mask : u64 = !(self.memory.get(left.alloc_id)?.align - 1);
134 let right = right as u64;
135 if right & base_mask == base_mask {
136 // Case 1: The base address bits are all preserved, i.e., right is all-1 there
137 (PrimVal::Ptr(MemoryPointer::new(left.alloc_id, left.offset & right)), false)
138 } else if right & base_mask == 0 {
139 // Case 2: The base address bits are all taken away, i.e., right is all-0 there
140 (PrimVal::from_u128((left.offset & right) as u128), false)
142 return Err(EvalError::ReadPointerAsBytes);
147 let msg = format!("unimplemented binary op on pointer {:?}: {:?}, {:?} ({})", bin_op, left, right, if signed { "signed" } else { "unsigned" });
148 return Err(EvalError::Unimplemented(msg));