Skip to content

Commit ee0dc45

Browse files
committed
Move binop functions to operator module.
1 parent e0013b2 commit ee0dc45

File tree

2 files changed

+51
-45
lines changed

2 files changed

+51
-45
lines changed

Diff for: src/eval_context.rs

-45
Original file line numberDiff line numberDiff line change
@@ -359,51 +359,6 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
359359
Ok(())
360360
}
361361

362-
fn binop_with_overflow(
363-
&mut self,
364-
op: mir::BinOp,
365-
left: &mir::Operand<'tcx>,
366-
right: &mir::Operand<'tcx>,
367-
) -> EvalResult<'tcx, (PrimVal, bool)> {
368-
let left_ty = self.operand_ty(left);
369-
let right_ty = self.operand_ty(right);
370-
let left_kind = self.ty_to_primval_kind(left_ty)?;
371-
let right_kind = self.ty_to_primval_kind(right_ty)?;
372-
let left_val = self.eval_operand_to_primval(left)?;
373-
let right_val = self.eval_operand_to_primval(right)?;
374-
operator::binary_op(op, left_val, left_kind, right_val, right_kind)
375-
}
376-
377-
/// Applies the binary operation `op` to the two operands and writes a tuple of the result
378-
/// and a boolean signifying the potential overflow to the destination.
379-
pub(super) fn intrinsic_with_overflow(
380-
&mut self,
381-
op: mir::BinOp,
382-
left: &mir::Operand<'tcx>,
383-
right: &mir::Operand<'tcx>,
384-
dest: Lvalue<'tcx>,
385-
dest_ty: Ty<'tcx>,
386-
) -> EvalResult<'tcx, ()> {
387-
let (val, overflowed) = self.binop_with_overflow(op, left, right)?;
388-
let val = Value::ByValPair(val, PrimVal::from_bool(overflowed));
389-
self.write_value(val, dest, dest_ty)
390-
}
391-
392-
/// Applies the binary operation `op` to the arguments and writes the result to the
393-
/// destination. Returns `true` if the operation overflowed.
394-
pub(super) fn intrinsic_overflowing(
395-
&mut self,
396-
op: mir::BinOp,
397-
left: &mir::Operand<'tcx>,
398-
right: &mir::Operand<'tcx>,
399-
dest: Lvalue<'tcx>,
400-
dest_ty: Ty<'tcx>,
401-
) -> EvalResult<'tcx, bool> {
402-
let (val, overflowed) = self.binop_with_overflow(op, left, right)?;
403-
self.write_primval(dest, val, dest_ty)?;
404-
Ok(overflowed)
405-
}
406-
407362
fn assign_fields<I: IntoIterator<Item = u64>>(
408363
&mut self,
409364
dest: Lvalue<'tcx>,

Diff for: src/operator.rs

+51
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,68 @@
11
use rustc::mir;
2+
use rustc::ty::Ty;
23

34
use error::{EvalError, EvalResult};
5+
use eval_context::EvalContext;
6+
use lvalue::Lvalue;
47
use memory::Pointer;
58
use value::{
69
PrimVal,
710
PrimValKind,
11+
Value,
812
bits_to_f32,
913
bits_to_f64,
1014
f32_to_bits,
1115
f64_to_bits,
1216
bits_to_bool,
1317
};
1418

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+
1566
macro_rules! overflow {
1667
($op:ident, $l:expr, $r:expr) => ({
1768
let (val, overflowed) = $l.$op($r);

0 commit comments

Comments
 (0)