6 use helpers::EvalContextExt as HelperEvalContextExt;
8 pub trait EvalContextExt<'tcx> {
13 left_ty: ty::Ty<'tcx>,
15 right_ty: ty::Ty<'tcx>,
16 ) -> EvalResult<'tcx, Option<(PrimVal, bool)>>;
18 fn ptr_int_arithmetic(
24 ) -> EvalResult<'tcx, (PrimVal, bool)>;
27 impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super::Evaluator<'tcx>> {
32 left_ty: ty::Ty<'tcx>,
34 right_ty: ty::Ty<'tcx>,
35 ) -> EvalResult<'tcx, Option<(PrimVal, bool)>> {
36 use rustc::mir::interpret::PrimValKind::*;
37 use rustc::mir::BinOp::*;
38 let usize = PrimValKind::from_uint_size(self.memory.pointer_size());
39 let isize = PrimValKind::from_int_size(self.memory.pointer_size());
40 let left_kind = self.ty_to_primval_kind(left_ty)?;
41 let right_kind = self.ty_to_primval_kind(right_ty)?;
43 Offset if left_kind == Ptr && right_kind == usize => {
44 let pointee_ty = left_ty
46 .expect("Offset called on non-ptr type")
48 let ptr = self.pointer_offset(
51 right.to_bytes()? as i64,
53 Ok(Some((ptr.into_inner_primval(), false)))
55 // These work on anything
56 Eq 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,
61 (_, PrimVal::Undef) => return err!(ReadUndefBytes),
64 Ok(Some((PrimVal::from_bool(result), false)))
66 Ne if left_kind == right_kind => {
67 let result = match (left, right) {
68 (PrimVal::Bytes(left), PrimVal::Bytes(right)) => left != right,
69 (PrimVal::Ptr(left), PrimVal::Ptr(right)) => left != right,
71 (_, PrimVal::Undef) => return err!(ReadUndefBytes),
74 Ok(Some((PrimVal::from_bool(result), false)))
76 // These need both pointers to be in the same allocation
77 Lt | Le | Gt | Ge | Sub
78 if left_kind == right_kind &&
79 (left_kind == Ptr || left_kind == usize || left_kind == isize) &&
80 left.is_ptr() && right.is_ptr() => {
81 let left = left.to_ptr()?;
82 let right = right.to_ptr()?;
83 if left.alloc_id == right.alloc_id {
84 let res = match bin_op {
85 Lt => left.offset < right.offset,
86 Le => left.offset <= right.offset,
87 Gt => left.offset > right.offset,
88 Ge => left.offset >= right.offset,
90 return self.binary_op(
92 PrimVal::Bytes(left.offset as u128),
94 PrimVal::Bytes(right.offset as u128),
98 _ => bug!("We already established it has to be one of these operators."),
100 Ok(Some((PrimVal::from_bool(res), false)))
102 // Both are pointers, but from different allocations.
103 err!(InvalidPointerMath)
106 // These work if one operand is a pointer, the other an integer
108 if left_kind == right_kind && (left_kind == usize || left_kind == isize) &&
109 left.is_ptr() && right.is_bytes() => {
110 // Cast to i128 is fine as we checked the kind to be ptr-sized
111 self.ptr_int_arithmetic(
114 right.to_bytes()? as i128,
119 if left_kind == right_kind && (left_kind == usize || left_kind == isize) &&
120 left.is_bytes() && right.is_ptr() => {
121 // This is a commutative operation, just swap the operands
122 self.ptr_int_arithmetic(
125 left.to_bytes()? as i128,
133 fn ptr_int_arithmetic(
139 ) -> EvalResult<'tcx, (PrimVal, bool)> {
140 use rustc::mir::BinOp::*;
142 fn map_to_primval((res, over): (MemoryPointer, bool)) -> (PrimVal, bool) {
143 (PrimVal::Ptr(res), over)
148 // The only way this can overflow is by underflowing, so signdeness of the right operands does not matter
149 map_to_primval(left.overflowing_signed_offset(-right, self)),
151 map_to_primval(left.overflowing_signed_offset(right, self)),
153 map_to_primval(left.overflowing_offset(right as u64, self)),
155 BitAnd if !signed => {
156 let base_mask : u64 = !(self.memory.get(left.alloc_id)?.align.abi() - 1);
157 let right = right as u64;
158 if right & base_mask == base_mask {
159 // Case 1: The base address bits are all preserved, i.e., right is all-1 there
160 (PrimVal::Ptr(MemoryPointer::new(left.alloc_id, left.offset & right)), false)
161 } else if right & base_mask == 0 {
162 // Case 2: The base address bits are all taken away, i.e., right is all-0 there
163 (PrimVal::from_u128((left.offset & right) as u128), false)
165 return err!(ReadPointerAsBytes);
170 let msg = format!("unimplemented binary op on pointer {:?}: {:?}, {:?} ({})", bin_op, left, right, if signed { "signed" } else { "unsigned" });
171 return err!(Unimplemented(msg));