diff --git a/src/eval_context.rs b/src/eval_context.rs index b0c01aed9e13..713c01602c78 100644 --- a/src/eval_context.rs +++ b/src/eval_context.rs @@ -359,51 +359,6 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> { Ok(()) } - fn binop_with_overflow( - &mut self, - op: mir::BinOp, - left: &mir::Operand<'tcx>, - right: &mir::Operand<'tcx>, - ) -> EvalResult<'tcx, (PrimVal, bool)> { - let left_ty = self.operand_ty(left); - let right_ty = self.operand_ty(right); - let left_kind = self.ty_to_primval_kind(left_ty)?; - let right_kind = self.ty_to_primval_kind(right_ty)?; - let left_val = self.eval_operand_to_primval(left)?; - let right_val = self.eval_operand_to_primval(right)?; - operator::binary_op(op, left_val, left_kind, right_val, right_kind) - } - - /// Applies the binary operation `op` to the two operands and writes a tuple of the result - /// and a boolean signifying the potential overflow to the destination. - pub(super) fn intrinsic_with_overflow( - &mut self, - op: mir::BinOp, - left: &mir::Operand<'tcx>, - right: &mir::Operand<'tcx>, - dest: Lvalue<'tcx>, - dest_ty: Ty<'tcx>, - ) -> EvalResult<'tcx, ()> { - let (val, overflowed) = self.binop_with_overflow(op, left, right)?; - let val = Value::ByValPair(val, PrimVal::from_bool(overflowed)); - self.write_value(val, dest, dest_ty) - } - - /// Applies the binary operation `op` to the arguments and writes the result to the - /// destination. Returns `true` if the operation overflowed. - pub(super) fn intrinsic_overflowing( - &mut self, - op: mir::BinOp, - left: &mir::Operand<'tcx>, - right: &mir::Operand<'tcx>, - dest: Lvalue<'tcx>, - dest_ty: Ty<'tcx>, - ) -> EvalResult<'tcx, bool> { - let (val, overflowed) = self.binop_with_overflow(op, left, right)?; - self.write_primval(dest, val, dest_ty)?; - Ok(overflowed) - } - fn assign_fields>( &mut self, dest: Lvalue<'tcx>, diff --git a/src/operator.rs b/src/operator.rs index 5a6182d0be76..6e656d69f655 100644 --- a/src/operator.rs +++ b/src/operator.rs @@ -1,10 +1,14 @@ use rustc::mir; +use rustc::ty::Ty; use error::{EvalError, EvalResult}; +use eval_context::EvalContext; +use lvalue::Lvalue; use memory::Pointer; use value::{ PrimVal, PrimValKind, + Value, bits_to_f32, bits_to_f64, f32_to_bits, @@ -12,6 +16,53 @@ use value::{ bits_to_bool, }; +impl<'a, 'tcx> EvalContext<'a, 'tcx> { + fn binop_with_overflow( + &mut self, + op: mir::BinOp, + left: &mir::Operand<'tcx>, + right: &mir::Operand<'tcx>, + ) -> EvalResult<'tcx, (PrimVal, bool)> { + let left_ty = self.operand_ty(left); + let right_ty = self.operand_ty(right); + let left_kind = self.ty_to_primval_kind(left_ty)?; + let right_kind = self.ty_to_primval_kind(right_ty)?; + let left_val = self.eval_operand_to_primval(left)?; + let right_val = self.eval_operand_to_primval(right)?; + binary_op(op, left_val, left_kind, right_val, right_kind) + } + + /// Applies the binary operation `op` to the two operands and writes a tuple of the result + /// and a boolean signifying the potential overflow to the destination. + pub(super) fn intrinsic_with_overflow( + &mut self, + op: mir::BinOp, + left: &mir::Operand<'tcx>, + right: &mir::Operand<'tcx>, + dest: Lvalue<'tcx>, + dest_ty: Ty<'tcx>, + ) -> EvalResult<'tcx, ()> { + let (val, overflowed) = self.binop_with_overflow(op, left, right)?; + let val = Value::ByValPair(val, PrimVal::from_bool(overflowed)); + self.write_value(val, dest, dest_ty) + } + + /// Applies the binary operation `op` to the arguments and writes the result to the + /// destination. Returns `true` if the operation overflowed. + pub(super) fn intrinsic_overflowing( + &mut self, + op: mir::BinOp, + left: &mir::Operand<'tcx>, + right: &mir::Operand<'tcx>, + dest: Lvalue<'tcx>, + dest_ty: Ty<'tcx>, + ) -> EvalResult<'tcx, bool> { + let (val, overflowed) = self.binop_with_overflow(op, left, right)?; + self.write_primval(dest, val, dest_ty)?; + Ok(overflowed) + } +} + macro_rules! overflow { ($op:ident, $l:expr, $r:expr) => ({ let (val, overflowed) = $l.$op($r);