2 use rustc::mir::interpret::{InterpResult, Scalar};
5 layout::{LayoutOf, TyLayout},
8 use rustc_apfloat::Float;
9 use syntax::ast::FloatTy;
11 use super::{ImmTy, Immediate, InterpCx, Machine, PlaceTy};
13 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
14 /// Applies the binary operation `op` to the two operands and writes a tuple of the result
15 /// and a boolean signifying the potential overflow to the destination.
16 pub fn binop_with_overflow(
19 left: ImmTy<'tcx, M::PointerTag>,
20 right: ImmTy<'tcx, M::PointerTag>,
21 dest: PlaceTy<'tcx, M::PointerTag>,
22 ) -> InterpResult<'tcx> {
23 let (val, overflowed, ty) = self.overflowing_binary_op(op, left, right)?;
25 self.tcx.intern_tup(&[ty, self.tcx.types.bool]),
27 "type mismatch for result of {:?}",
30 let val = Immediate::ScalarPair(val.into(), Scalar::from_bool(overflowed).into());
31 self.write_immediate(val, dest)
34 /// Applies the binary operation `op` to the arguments and writes the result to the
36 pub fn binop_ignore_overflow(
39 left: ImmTy<'tcx, M::PointerTag>,
40 right: ImmTy<'tcx, M::PointerTag>,
41 dest: PlaceTy<'tcx, M::PointerTag>,
42 ) -> InterpResult<'tcx> {
43 let (val, _overflowed, ty) = self.overflowing_binary_op(op, left, right)?;
44 assert_eq!(ty, dest.layout.ty, "type mismatch for result of {:?}", op);
45 self.write_scalar(val, dest)
49 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
55 ) -> (Scalar<M::PointerTag>, bool, Ty<'tcx>) {
56 use rustc::mir::BinOp::*;
58 let res = match bin_op {
65 _ => bug!("Invalid operation on char: {:?}", bin_op),
67 return (Scalar::from_bool(res), false, self.tcx.types.bool);
75 ) -> (Scalar<M::PointerTag>, bool, Ty<'tcx>) {
76 use rustc::mir::BinOp::*;
78 let res = match bin_op {
88 _ => bug!("Invalid operation on bool: {:?}", bin_op),
90 return (Scalar::from_bool(res), false, self.tcx.types.bool);
93 fn binary_float_op<F: Float + Into<Scalar<M::PointerTag>>>(
99 ) -> (Scalar<M::PointerTag>, bool, Ty<'tcx>) {
100 use rustc::mir::BinOp::*;
102 let (val, ty) = match bin_op {
103 Eq => (Scalar::from_bool(l == r), self.tcx.types.bool),
104 Ne => (Scalar::from_bool(l != r), self.tcx.types.bool),
105 Lt => (Scalar::from_bool(l < r), self.tcx.types.bool),
106 Le => (Scalar::from_bool(l <= r), self.tcx.types.bool),
107 Gt => (Scalar::from_bool(l > r), self.tcx.types.bool),
108 Ge => (Scalar::from_bool(l >= r), self.tcx.types.bool),
109 Add => ((l + r).value.into(), ty),
110 Sub => ((l - r).value.into(), ty),
111 Mul => ((l * r).value.into(), ty),
112 Div => ((l / r).value.into(), ty),
113 Rem => ((l % r).value.into(), ty),
114 _ => bug!("invalid float op: `{:?}`", bin_op),
116 return (val, false, ty);
122 // passing in raw bits
124 left_layout: TyLayout<'tcx>,
126 right_layout: TyLayout<'tcx>,
127 ) -> InterpResult<'tcx, (Scalar<M::PointerTag>, bool, Ty<'tcx>)> {
128 use rustc::mir::BinOp::*;
130 // Shift ops can have an RHS with a different numeric type.
131 if bin_op == Shl || bin_op == Shr {
132 let signed = left_layout.abi.is_signed();
133 let mut oflo = (r as u32 as u128) != r;
134 let mut r = r as u32;
135 let size = left_layout.size;
136 oflo |= r >= size.bits() as u32;
138 r %= size.bits() as u32;
140 let result = if signed {
141 let l = self.sign_extend(l, left_layout) as i128;
142 let result = match bin_op {
145 _ => bug!("it has already been checked that this is a shift op"),
152 _ => bug!("it has already been checked that this is a shift op"),
155 let truncated = self.truncate(result, left_layout);
156 return Ok((Scalar::from_uint(truncated, size), oflo, left_layout.ty));
159 // For the remaining ops, the types must be the same on both sides
160 if left_layout.ty != right_layout.ty {
162 "invalid asymmetric binary op {:?}: {:?} ({:?}), {:?} ({:?})",
171 // Operations that need special treatment for signed integers
172 if left_layout.abi.is_signed() {
173 let op: Option<fn(&i128, &i128) -> bool> = match bin_op {
174 Lt => Some(i128::lt),
175 Le => Some(i128::le),
176 Gt => Some(i128::gt),
177 Ge => Some(i128::ge),
180 if let Some(op) = op {
181 let l = self.sign_extend(l, left_layout) as i128;
182 let r = self.sign_extend(r, right_layout) as i128;
183 return Ok((Scalar::from_bool(op(&l, &r)), false, self.tcx.types.bool));
185 let op: Option<fn(i128, i128) -> (i128, bool)> = match bin_op {
186 Div if r == 0 => throw_ub!(DivisionByZero),
187 Rem if r == 0 => throw_ub!(RemainderByZero),
188 Div => Some(i128::overflowing_div),
189 Rem => Some(i128::overflowing_rem),
190 Add => Some(i128::overflowing_add),
191 Sub => Some(i128::overflowing_sub),
192 Mul => Some(i128::overflowing_mul),
195 if let Some(op) = op {
196 let l128 = self.sign_extend(l, left_layout) as i128;
197 let r = self.sign_extend(r, right_layout) as i128;
198 let size = left_layout.size;
202 if r == -1 && l == (1 << (size.bits() - 1)) {
203 return Ok((Scalar::from_uint(l, size), true, left_layout.ty));
208 trace!("{}, {}, {}", l, l128, r);
209 let (result, mut oflo) = op(l128, r);
210 trace!("{}, {}", result, oflo);
211 if !oflo && size.bits() != 128 {
212 let max = 1 << (size.bits() - 1);
213 oflo = result >= max || result < -max;
215 // this may be out-of-bounds for the result type, so we have to truncate ourselves
216 let result = result as u128;
217 let truncated = self.truncate(result, left_layout);
218 return Ok((Scalar::from_uint(truncated, size), oflo, left_layout.ty));
222 let size = left_layout.size;
224 let (val, ty) = match bin_op {
225 Eq => (Scalar::from_bool(l == r), self.tcx.types.bool),
226 Ne => (Scalar::from_bool(l != r), self.tcx.types.bool),
228 Lt => (Scalar::from_bool(l < r), self.tcx.types.bool),
229 Le => (Scalar::from_bool(l <= r), self.tcx.types.bool),
230 Gt => (Scalar::from_bool(l > r), self.tcx.types.bool),
231 Ge => (Scalar::from_bool(l >= r), self.tcx.types.bool),
233 BitOr => (Scalar::from_uint(l | r, size), left_layout.ty),
234 BitAnd => (Scalar::from_uint(l & r, size), left_layout.ty),
235 BitXor => (Scalar::from_uint(l ^ r, size), left_layout.ty),
237 Add | Sub | Mul | Rem | Div => {
238 debug_assert!(!left_layout.abi.is_signed());
239 let op: fn(u128, u128) -> (u128, bool) = match bin_op {
240 Add => u128::overflowing_add,
241 Sub => u128::overflowing_sub,
242 Mul => u128::overflowing_mul,
243 Div if r == 0 => throw_ub!(DivisionByZero),
244 Rem if r == 0 => throw_ub!(RemainderByZero),
245 Div => u128::overflowing_div,
246 Rem => u128::overflowing_rem,
249 let (result, oflo) = op(l, r);
250 let truncated = self.truncate(result, left_layout);
252 Scalar::from_uint(truncated, size),
253 oflo || truncated != result,
259 "invalid binary op {:?}: {:?}, {:?} (both {:?})",
270 /// Returns the result of the specified operation, whether it overflowed, and
272 pub fn overflowing_binary_op(
275 left: ImmTy<'tcx, M::PointerTag>,
276 right: ImmTy<'tcx, M::PointerTag>,
277 ) -> InterpResult<'tcx, (Scalar<M::PointerTag>, bool, Ty<'tcx>)> {
279 "Running binary op {:?}: {:?} ({:?}), {:?} ({:?})",
287 match left.layout.ty.kind {
289 assert_eq!(left.layout.ty, right.layout.ty);
290 let left = left.to_scalar()?;
291 let right = right.to_scalar()?;
292 Ok(self.binary_char_op(bin_op, left.to_char()?, right.to_char()?))
295 assert_eq!(left.layout.ty, right.layout.ty);
296 let left = left.to_scalar()?;
297 let right = right.to_scalar()?;
298 Ok(self.binary_bool_op(bin_op, left.to_bool()?, right.to_bool()?))
301 assert_eq!(left.layout.ty, right.layout.ty);
302 let ty = left.layout.ty;
303 let left = left.to_scalar()?;
304 let right = right.to_scalar()?;
307 self.binary_float_op(bin_op, ty, left.to_f32()?, right.to_f32()?)
310 self.binary_float_op(bin_op, ty, left.to_f64()?, right.to_f64()?)
314 _ if left.layout.ty.is_integral() => {
315 // the RHS type can be different, e.g. for shifts -- but it has to be integral, too
317 right.layout.ty.is_integral(),
318 "Unexpected types for BinOp: {:?} {:?} {:?}",
324 let l = self.force_bits(left.to_scalar()?, left.layout.size)?;
325 let r = self.force_bits(right.to_scalar()?, right.layout.size)?;
326 self.binary_int_op(bin_op, l, left.layout, r, right.layout)
328 _ if left.layout.ty.is_any_ptr() => {
329 // The RHS type must be the same *or an integer type* (for `Offset`).
331 right.layout.ty == left.layout.ty || right.layout.ty.is_integral(),
332 "Unexpected types for BinOp: {:?} {:?} {:?}",
338 M::binary_ptr_op(self, bin_op, left, right)
340 _ => bug!("Invalid MIR: bad LHS type for binop: {:?}", left.layout.ty),
344 /// Typed version of `checked_binary_op`, returning an `ImmTy`. Also ignores overflows.
349 left: ImmTy<'tcx, M::PointerTag>,
350 right: ImmTy<'tcx, M::PointerTag>,
351 ) -> InterpResult<'tcx, ImmTy<'tcx, M::PointerTag>> {
352 let (val, _overflow, ty) = self.overflowing_binary_op(bin_op, left, right)?;
353 Ok(ImmTy::from_scalar(val, self.layout_of(ty)?))
359 val: ImmTy<'tcx, M::PointerTag>,
360 ) -> InterpResult<'tcx, ImmTy<'tcx, M::PointerTag>> {
361 use rustc::mir::UnOp::*;
363 let layout = val.layout;
364 let val = val.to_scalar()?;
365 trace!("Running unary op {:?}: {:?} ({:?})", un_op, val, layout.ty);
367 match layout.ty.kind {
369 let val = val.to_bool()?;
370 let res = match un_op {
372 _ => bug!("Invalid bool op {:?}", un_op),
374 Ok(ImmTy::from_scalar(Scalar::from_bool(res), self.layout_of(self.tcx.types.bool)?))
377 let res = match (un_op, fty) {
378 (Neg, FloatTy::F32) => Scalar::from_f32(-val.to_f32()?),
379 (Neg, FloatTy::F64) => Scalar::from_f64(-val.to_f64()?),
380 _ => bug!("Invalid float op {:?}", un_op),
382 Ok(ImmTy::from_scalar(res, layout))
385 assert!(layout.ty.is_integral());
386 let val = self.force_bits(val, layout.size)?;
387 let res = match un_op {
390 assert!(layout.abi.is_signed());
391 (-(val as i128)) as u128
394 // res needs tuncating
395 Ok(ImmTy::from_uint(self.truncate(res, layout), layout))