|
1 | 1 | use rustc::mir;
|
| 2 | +use rustc::ty::Ty; |
2 | 3 |
|
3 | 4 | use error::{EvalError, EvalResult};
|
| 5 | +use eval_context::EvalContext; |
| 6 | +use lvalue::Lvalue; |
4 | 7 | use memory::Pointer;
|
5 | 8 | use value::{
|
6 | 9 | PrimVal,
|
7 | 10 | PrimValKind,
|
| 11 | + Value, |
8 | 12 | bits_to_f32,
|
9 | 13 | bits_to_f64,
|
10 | 14 | f32_to_bits,
|
11 | 15 | f64_to_bits,
|
12 | 16 | bits_to_bool,
|
13 | 17 | };
|
14 | 18 |
|
| 19 | +impl<'a, 'tcx> EvalContext<'a, 'tcx> { |
| 20 | + fn binop_with_overflow( |
| 21 | + &mut self, |
| 22 | + op: mir::BinOp, |
| 23 | + left: &mir::Operand<'tcx>, |
| 24 | + right: &mir::Operand<'tcx>, |
| 25 | + ) -> EvalResult<'tcx, (PrimVal, bool)> { |
| 26 | + let left_ty = self.operand_ty(left); |
| 27 | + let right_ty = self.operand_ty(right); |
| 28 | + let left_kind = self.ty_to_primval_kind(left_ty)?; |
| 29 | + let right_kind = self.ty_to_primval_kind(right_ty)?; |
| 30 | + let left_val = self.eval_operand_to_primval(left)?; |
| 31 | + let right_val = self.eval_operand_to_primval(right)?; |
| 32 | + binary_op(op, left_val, left_kind, right_val, right_kind) |
| 33 | + } |
| 34 | + |
| 35 | + /// Applies the binary operation `op` to the two operands and writes a tuple of the result |
| 36 | + /// and a boolean signifying the potential overflow to the destination. |
| 37 | + pub(super) fn intrinsic_with_overflow( |
| 38 | + &mut self, |
| 39 | + op: mir::BinOp, |
| 40 | + left: &mir::Operand<'tcx>, |
| 41 | + right: &mir::Operand<'tcx>, |
| 42 | + dest: Lvalue<'tcx>, |
| 43 | + dest_ty: Ty<'tcx>, |
| 44 | + ) -> EvalResult<'tcx, ()> { |
| 45 | + let (val, overflowed) = self.binop_with_overflow(op, left, right)?; |
| 46 | + let val = Value::ByValPair(val, PrimVal::from_bool(overflowed)); |
| 47 | + self.write_value(val, dest, dest_ty) |
| 48 | + } |
| 49 | + |
| 50 | + /// Applies the binary operation `op` to the arguments and writes the result to the |
| 51 | + /// destination. Returns `true` if the operation overflowed. |
| 52 | + pub(super) fn intrinsic_overflowing( |
| 53 | + &mut self, |
| 54 | + op: mir::BinOp, |
| 55 | + left: &mir::Operand<'tcx>, |
| 56 | + right: &mir::Operand<'tcx>, |
| 57 | + dest: Lvalue<'tcx>, |
| 58 | + dest_ty: Ty<'tcx>, |
| 59 | + ) -> EvalResult<'tcx, bool> { |
| 60 | + let (val, overflowed) = self.binop_with_overflow(op, left, right)?; |
| 61 | + self.write_primval(dest, val, dest_ty)?; |
| 62 | + Ok(overflowed) |
| 63 | + } |
| 64 | +} |
| 65 | + |
15 | 66 | macro_rules! overflow {
|
16 | 67 | ($op:ident, $l:expr, $r:expr) => ({
|
17 | 68 | let (val, overflowed) = $l.$op($r);
|
|
0 commit comments