rust/src/primval.rs

194 lines
6.5 KiB
Rust
Raw Normal View History

use rustc::mir::repr as mir;
use error::{EvalError, EvalResult};
use memory::Pointer;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PrimVal {
Bool(bool),
2016-03-15 00:03:31 -05:00
I8(i8), I16(i16), I32(i32), I64(i64),
U8(u8), U16(u16), U32(u32), U64(u64),
AbstractPtr(Pointer),
2016-06-20 03:35:15 -05:00
FnPtr(Pointer),
IntegerPtr(u64),
}
/// returns the result of the operation and whether the operation overflowed
pub fn binary_op<'tcx>(bin_op: mir::BinOp, left: PrimVal, right: PrimVal) -> EvalResult<'tcx, (PrimVal, bool)> {
2016-04-23 01:39:38 -05:00
use rustc::mir::repr::BinOp::*;
use self::PrimVal::*;
2016-06-17 06:09:20 -05:00
macro_rules! overflow {
($v:ident, $v2:ident, $l:ident, $op:ident, $r:ident) => ({
let (val, of) = $l.$op($r);
if of {
return Ok(($v(val), true));
2016-06-17 06:09:20 -05:00
} else {
$v(val)
}
})
}
macro_rules! int_binops {
($v:ident, $l:ident, $r:ident) => ({
match bin_op {
2016-06-17 06:09:20 -05:00
Add => overflow!($v, $v, $l, overflowing_add, $r),
Sub => overflow!($v, $v, $l, overflowing_sub, $r),
Mul => overflow!($v, $v, $l, overflowing_mul, $r),
Div => overflow!($v, $v, $l, overflowing_div, $r),
Rem => overflow!($v, $v, $l, overflowing_rem, $r),
BitXor => $v($l ^ $r),
BitAnd => $v($l & $r),
BitOr => $v($l | $r),
2016-06-17 06:09:20 -05:00
// these have already been handled
Shl => unreachable!(),
Shr => unreachable!(),
Eq => Bool($l == $r),
Ne => Bool($l != $r),
Lt => Bool($l < $r),
Le => Bool($l <= $r),
Gt => Bool($l > $r),
Ge => Bool($l >= $r),
}
})
}
fn unrelated_ptr_ops<'tcx>(bin_op: mir::BinOp) -> EvalResult<'tcx, PrimVal> {
use rustc::mir::repr::BinOp::*;
match bin_op {
Eq => Ok(Bool(false)),
Ne => Ok(Bool(true)),
Lt | Le | Gt | Ge => Err(EvalError::InvalidPointerMath),
_ => unimplemented!(),
}
}
2016-06-17 06:09:20 -05:00
match bin_op {
// can have rhs with a different numeric type
Shl | Shr => {
let r = match right {
I8(i) if i >= 0 => i as u32,
I16(i) if i >= 0 => i as u32,
I32(i) if i >= 0 => i as u32,
I64(i) if i >= 0 && i as i32 as i64 == i => i as u32,
U8(i) => i as u32,
U16(i) => i as u32,
U32(i) => i,
U64(i) if i as u32 as u64 == i => i as u32,
_ => return Err(EvalError::InvalidBitShiftRhs(right)),
};
macro_rules! shift {
($v:ident, $l:ident, $r:ident) => ({
match bin_op {
Shl => overflow!($v, U32, $l, overflowing_shl, $r),
Shr => overflow!($v, U32, $l, overflowing_shr, $r),
_ => unreachable!(),
}
})
}
let val = match left {
I8(l) => shift!(I8, l, r),
I16(l) => shift!(I16, l, r),
I32(l) => shift!(I32, l, r),
I64(l) => shift!(I64, l, r),
U8(l) => shift!(U8, l, r),
U16(l) => shift!(U16, l, r),
U32(l) => shift!(U32, l, r),
U64(l) => shift!(U64, l, r),
_ => unreachable!(),
};
return Ok((val, false));
2016-06-17 06:09:20 -05:00
},
_ => {},
}
let val = match (left, right) {
2016-03-15 00:03:31 -05:00
(I8(l), I8(r)) => int_binops!(I8, l, r),
(I16(l), I16(r)) => int_binops!(I16, l, r),
(I32(l), I32(r)) => int_binops!(I32, l, r),
(I64(l), I64(r)) => int_binops!(I64, l, r),
2016-03-15 00:03:31 -05:00
(U8(l), U8(r)) => int_binops!(U8, l, r),
(U16(l), U16(r)) => int_binops!(U16, l, r),
(U32(l), U32(r)) => int_binops!(U32, l, r),
(U64(l), U64(r)) => int_binops!(U64, l, r),
2016-04-23 01:39:38 -05:00
(Bool(l), Bool(r)) => {
Bool(match bin_op {
Eq => l == r,
Ne => l != r,
Lt => l < r,
Le => l <= r,
Gt => l > r,
Ge => l >= r,
2016-04-23 21:13:00 -05:00
BitOr => l | r,
BitXor => l ^ r,
BitAnd => l & r,
2016-05-30 08:27:52 -05:00
Add | Sub | Mul | Div | Rem | Shl | Shr => return Err(EvalError::InvalidBoolOp(bin_op)),
2016-04-23 01:39:38 -05:00
})
}
(IntegerPtr(l), IntegerPtr(r)) => int_binops!(IntegerPtr, l, r),
2016-06-20 03:35:15 -05:00
(AbstractPtr(_), IntegerPtr(_)) |
(IntegerPtr(_), AbstractPtr(_)) |
(FnPtr(_), AbstractPtr(_)) |
(AbstractPtr(_), FnPtr(_)) |
(FnPtr(_), IntegerPtr(_)) |
(IntegerPtr(_), FnPtr(_)) =>
unrelated_ptr_ops(bin_op)?,
2016-06-20 03:35:15 -05:00
(FnPtr(l_ptr), FnPtr(r_ptr)) => match bin_op {
Eq => Bool(l_ptr == r_ptr),
Ne => Bool(l_ptr != r_ptr),
_ => return Err(EvalError::Unimplemented(format!("unimplemented fn ptr comparison: {:?}", bin_op))),
},
(AbstractPtr(l_ptr), AbstractPtr(r_ptr)) => {
if l_ptr.alloc_id != r_ptr.alloc_id {
return Ok((unrelated_ptr_ops(bin_op)?, false));
}
let l = l_ptr.offset;
let r = r_ptr.offset;
match bin_op {
Eq => Bool(l == r),
Ne => Bool(l != r),
Lt => Bool(l < r),
Le => Bool(l <= r),
Gt => Bool(l > r),
Ge => Bool(l >= r),
2016-05-30 08:27:52 -05:00
_ => return Err(EvalError::Unimplemented(format!("unimplemented ptr op: {:?}", bin_op))),
}
}
2016-05-30 08:27:52 -05:00
(l, r) => return Err(EvalError::Unimplemented(format!("unimplemented binary op: {:?}, {:?}, {:?}", l, r, bin_op))),
};
Ok((val, false))
}
pub fn unary_op<'tcx>(un_op: mir::UnOp, val: PrimVal) -> EvalResult<'tcx, PrimVal> {
use rustc::mir::repr::UnOp::*;
use self::PrimVal::*;
match (un_op, val) {
2016-05-30 08:27:52 -05:00
(Not, Bool(b)) => Ok(Bool(!b)),
(Not, I8(n)) => Ok(I8(!n)),
(Neg, I8(n)) => Ok(I8(-n)),
(Not, I16(n)) => Ok(I16(!n)),
(Neg, I16(n)) => Ok(I16(-n)),
(Not, I32(n)) => Ok(I32(!n)),
(Neg, I32(n)) => Ok(I32(-n)),
(Not, I64(n)) => Ok(I64(!n)),
(Neg, I64(n)) => Ok(I64(-n)),
(Not, U8(n)) => Ok(U8(!n)),
(Not, U16(n)) => Ok(U16(!n)),
(Not, U32(n)) => Ok(U32(!n)),
(Not, U64(n)) => Ok(U64(!n)),
_ => Err(EvalError::Unimplemented(format!("unimplemented unary op: {:?}, {:?}", un_op, val))),
}
}