use std::fmt;
use std::rc::Rc;
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Op {
And,
Or,
Xor,
Add,
Sub,
Mul,
Udiv,
Urem,
Sdiv,
Srem,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Cmp {
Eq,
Ne,
Ult,
Ule,
Slt,
Sle,
}
pub(crate) enum Node {
Const(u32, u128),
Var(u32, usize),
Not(Bv),
Neg(Bv),
Bin(Op, Bv, Bv),
ShlC(Bv, u32),
ShrC(Bv, u32, bool), ShlV(Bv, Bv), ShrV(Bv, Bv, bool), RotC(Bv, u32, bool), RotV(Bv, Bv, bool), Compare(Cmp, Bv, Bv),
Zext(Bv, u32),
Sext(Bv, u32),
Extract(Bv, u32, u32), Concat(Bv, Bv), Ite(Bv, Bv, Bv), }
#[derive(Clone)]
pub struct Bv(pub(crate) Rc<Node>);
impl Bv {
pub(crate) fn wrap(n: Node) -> Bv {
match fold(&n) {
Some(folded) => folded,
None => Bv(Rc::new(n)),
}
}
#[must_use]
pub fn ptr_eq(&self, other: &Bv) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
#[must_use]
pub(crate) fn ptr_key(&self) -> usize {
Rc::as_ptr(&self.0) as usize
}
#[must_use]
pub fn width(&self) -> u32 {
let mut node = &*self.0;
loop {
match node {
Node::Const(w, _) | Node::Var(w, _) => return *w,
Node::Not(a)
| Node::Neg(a)
| Node::Bin(_, a, _)
| Node::ShlC(a, _)
| Node::ShrC(a, _, _)
| Node::ShlV(a, _)
| Node::ShrV(a, _, _)
| Node::RotC(a, _, _)
| Node::RotV(a, _, _) => node = &*a.0,
Node::Compare(_, _, _) => return 1,
Node::Zext(_, w) | Node::Sext(_, w) => return *w,
Node::Extract(_, hi, lo) => return hi - lo + 1,
Node::Concat(a, b) => return a.width() + b.width(),
Node::Ite(_, a, _) => return a.width(),
}
}
}
#[must_use]
pub fn val(v: u64, w: u32) -> Bv {
Bv::val128(u128::from(v), w)
}
#[must_use]
pub fn val128(v: u128, w: u32) -> Bv {
assert!(
(1..=128).contains(&w),
"constant width must be 1..=128, got {w}"
);
Bv::wrap(Node::Const(w, mask128(v, w)))
}
#[must_use]
pub fn as_const(&self) -> Option<u64> {
self.as_const128().and_then(|v| u64::try_from(v).ok())
}
#[must_use]
pub fn as_const128(&self) -> Option<u128> {
if let Node::Const(_, v) = &*self.0 {
Some(*v)
} else {
None
}
}
#[must_use]
pub fn var_ids(&self) -> Vec<usize> {
let mut seen = std::collections::HashSet::new();
let mut ids = Vec::new();
let mut stack = vec![self.clone()];
while let Some(cur) = stack.pop() {
if !seen.insert(Rc::as_ptr(&cur.0) as usize) {
continue;
}
match &*cur.0 {
Node::Const(_, _) => {}
Node::Var(_, id) => ids.push(*id),
Node::Not(a)
| Node::Neg(a)
| Node::ShlC(a, _)
| Node::ShrC(a, _, _)
| Node::RotC(a, _, _)
| Node::Zext(a, _)
| Node::Sext(a, _)
| Node::Extract(a, _, _) => stack.push(a.clone()),
Node::Bin(_, a, b)
| Node::Compare(_, a, b)
| Node::Concat(a, b)
| Node::ShlV(a, b)
| Node::ShrV(a, b, _)
| Node::RotV(a, b, _) => {
stack.push(a.clone());
stack.push(b.clone());
}
Node::Ite(a, b, c) => {
stack.push(a.clone());
stack.push(b.clone());
stack.push(c.clone());
}
}
}
ids
}
#[must_use]
pub fn not(&self) -> Bv {
Bv::wrap(Node::Not(self.clone()))
}
#[must_use]
pub fn neg(&self) -> Bv {
Bv::wrap(Node::Neg(self.clone()))
}
#[must_use]
pub fn and(&self, o: &Bv) -> Bv {
self.bin(Op::And, o)
}
#[must_use]
pub fn or(&self, o: &Bv) -> Bv {
self.bin(Op::Or, o)
}
#[must_use]
pub fn xor(&self, o: &Bv) -> Bv {
self.bin(Op::Xor, o)
}
#[must_use]
pub fn add(&self, o: &Bv) -> Bv {
self.bin(Op::Add, o)
}
#[must_use]
pub fn sub(&self, o: &Bv) -> Bv {
self.bin(Op::Sub, o)
}
#[must_use]
pub fn mul(&self, o: &Bv) -> Bv {
self.bin(Op::Mul, o)
}
#[must_use]
pub fn udiv(&self, o: &Bv) -> Bv {
self.bin(Op::Udiv, o)
}
#[must_use]
pub fn urem(&self, o: &Bv) -> Bv {
self.bin(Op::Urem, o)
}
#[must_use]
pub fn sdiv(&self, o: &Bv) -> Bv {
self.bin(Op::Sdiv, o)
}
#[must_use]
pub fn srem(&self, o: &Bv) -> Bv {
self.bin(Op::Srem, o)
}
fn bin(&self, op: Op, o: &Bv) -> Bv {
assert_eq!(
self.width(),
o.width(),
"bitvector op needs equal widths, got {} and {}",
self.width(),
o.width()
);
Bv::wrap(Node::Bin(op, self.clone(), o.clone()))
}
#[must_use]
pub fn shl(&self, k: u32) -> Bv {
Bv::wrap(Node::ShlC(self.clone(), k))
}
#[must_use]
pub fn lshr(&self, k: u32) -> Bv {
Bv::wrap(Node::ShrC(self.clone(), k, false))
}
#[must_use]
pub fn ashr(&self, k: u32) -> Bv {
Bv::wrap(Node::ShrC(self.clone(), k, true))
}
#[must_use]
pub fn shl_var(&self, k: &Bv) -> Bv {
Bv::wrap(Node::ShlV(self.clone(), k.clone()))
}
#[must_use]
pub fn lshr_var(&self, k: &Bv) -> Bv {
Bv::wrap(Node::ShrV(self.clone(), k.clone(), false))
}
#[must_use]
pub fn ashr_var(&self, k: &Bv) -> Bv {
Bv::wrap(Node::ShrV(self.clone(), k.clone(), true))
}
#[must_use]
pub fn rotl(&self, k: u32) -> Bv {
Bv::wrap(Node::RotC(self.clone(), k, true))
}
#[must_use]
pub fn rotr(&self, k: u32) -> Bv {
Bv::wrap(Node::RotC(self.clone(), k, false))
}
#[must_use]
pub fn rotl_var(&self, k: &Bv) -> Bv {
Bv::wrap(Node::RotV(self.clone(), k.clone(), true))
}
#[must_use]
pub fn rotr_var(&self, k: &Bv) -> Bv {
Bv::wrap(Node::RotV(self.clone(), k.clone(), false))
}
#[must_use]
pub fn eq(&self, o: &Bv) -> Bv {
self.cmp(Cmp::Eq, o)
}
#[must_use]
pub fn ne(&self, o: &Bv) -> Bv {
self.cmp(Cmp::Ne, o)
}
#[must_use]
pub fn ult(&self, o: &Bv) -> Bv {
self.cmp(Cmp::Ult, o)
}
#[must_use]
pub fn ule(&self, o: &Bv) -> Bv {
self.cmp(Cmp::Ule, o)
}
#[must_use]
pub fn ugt(&self, o: &Bv) -> Bv {
o.cmp(Cmp::Ult, self)
}
#[must_use]
pub fn uge(&self, o: &Bv) -> Bv {
o.cmp(Cmp::Ule, self)
}
#[must_use]
pub fn slt(&self, o: &Bv) -> Bv {
self.cmp(Cmp::Slt, o)
}
#[must_use]
pub fn sle(&self, o: &Bv) -> Bv {
self.cmp(Cmp::Sle, o)
}
#[must_use]
pub fn sgt(&self, o: &Bv) -> Bv {
o.cmp(Cmp::Slt, self)
}
#[must_use]
pub fn sge(&self, o: &Bv) -> Bv {
o.cmp(Cmp::Sle, self)
}
fn cmp(&self, c: Cmp, o: &Bv) -> Bv {
assert_eq!(
self.width(),
o.width(),
"comparison needs equal widths, got {} and {}",
self.width(),
o.width()
);
Bv::wrap(Node::Compare(c, self.clone(), o.clone()))
}
#[must_use]
pub fn zext(&self, new_w: u32) -> Bv {
assert!(
new_w >= self.width(),
"zext must widen: {} to {new_w}",
self.width()
);
assert!(new_w <= 128, "width must be 1..=128, got {new_w}");
Bv::wrap(Node::Zext(self.clone(), new_w))
}
#[must_use]
pub fn sext(&self, new_w: u32) -> Bv {
assert!(
new_w >= self.width(),
"sext must widen: {} to {new_w}",
self.width()
);
assert!(new_w <= 128, "width must be 1..=128, got {new_w}");
Bv::wrap(Node::Sext(self.clone(), new_w))
}
#[must_use]
pub fn extract(&self, hi: u32, lo: u32) -> Bv {
assert!(
hi >= lo && hi < self.width(),
"extract[{hi}:{lo}] out of range for width {}",
self.width()
);
Bv::wrap(Node::Extract(self.clone(), hi, lo))
}
#[must_use]
pub fn concat(&self, low: &Bv) -> Bv {
let w = self.width() + low.width();
assert!(
w <= 128,
"concatenation is {w} bits wide, over the 128-bit limit"
);
Bv::wrap(Node::Concat(self.clone(), low.clone()))
}
#[must_use]
pub fn ite(cond: &Bv, then: &Bv, els: &Bv) -> Bv {
assert_eq!(
cond.width(),
1,
"ite condition must be 1 bit, got {}",
cond.width()
);
assert_eq!(
then.width(),
els.width(),
"ite branches must match: {} and {}",
then.width(),
els.width()
);
Bv::wrap(Node::Ite(cond.clone(), then.clone(), els.clone()))
}
#[must_use]
pub fn land(&self, o: &Bv) -> Bv {
assert_eq!(
self.width(),
1,
"land operand must be 1 bit, got {}",
self.width()
);
assert_eq!(
o.width(),
1,
"land operand must be 1 bit, got {}",
o.width()
);
self.and(o)
}
#[must_use]
pub fn implies(&self, o: &Bv) -> Bv {
assert_eq!(
self.width(),
1,
"implies antecedent must be 1 bit, got {}",
self.width()
);
assert_eq!(
o.width(),
1,
"implies consequent must be 1 bit, got {}",
o.width()
);
self.not().or(o)
}
}
#[must_use]
pub fn mask(v: u64, w: u32) -> u64 {
if w >= 64 { v } else { v & ((1u64 << w) - 1) }
}
#[must_use]
pub(crate) fn mask128(v: u128, w: u32) -> u128 {
if w >= 128 { v } else { v & ((1u128 << w) - 1) }
}
fn fold(n: &Node) -> Option<Bv> {
match n {
Node::Const(_, _) | Node::Var(_, _) => None,
Node::Not(a) => {
if let Some(v) = a.as_const128() {
return Some(const_of(a.width(), mask128(!v, a.width())));
}
if let Node::Not(b) = &*a.0 {
return Some(b.clone());
}
None
}
Node::Neg(a) => {
if let Some(v) = a.as_const128() {
return Some(const_of(
a.width(),
mask128(0u128.wrapping_sub(v), a.width()),
));
}
if let Node::Neg(b) = &*a.0 {
return Some(b.clone());
}
None
}
Node::Bin(op, a, b) => fold_bin(*op, a, b),
Node::ShlC(a, k) => {
if let Some(v) = a.as_const128() {
return Some(const_of(a.width(), shl_(v, u128::from(*k), a.width())));
}
if *k == 0 {
return Some(a.clone());
}
None
}
Node::ShrC(a, k, arith) => {
if let Some(v) = a.as_const128() {
let w = a.width();
return Some(const_of(w, shr_(v, u128::from(*k), w, *arith)));
}
if *k == 0 {
return Some(a.clone());
}
None
}
Node::ShlV(a, k) => {
if let Some(kv) = k.as_const128() {
if let Some(v) = a.as_const128() {
return Some(const_of(a.width(), shl_(v, kv, a.width())));
}
if kv == 0 {
return Some(a.clone());
}
}
None
}
Node::ShrV(a, k, arith) => {
if let Some(kv) = k.as_const128() {
if let Some(v) = a.as_const128() {
let w = a.width();
return Some(const_of(w, shr_(v, kv, w, *arith)));
}
if kv == 0 {
return Some(a.clone());
}
}
None
}
Node::RotC(a, k, left) => {
if let Some(v) = a.as_const128() {
let w = a.width();
return Some(const_of(w, rot_(v, u128::from(*k), w, *left)));
}
if *k == 0 {
return Some(a.clone());
}
None
}
Node::RotV(a, k, left) => {
if let Some(kv) = k.as_const128() {
if let Some(v) = a.as_const128() {
let w = a.width();
return Some(const_of(w, rot_(v, kv, w, *left)));
}
if kv == 0 {
return Some(a.clone());
}
}
None
}
Node::Compare(c, a, b) => fold_cmp(*c, a, b),
Node::Zext(a, w) => {
if let Some(v) = a.as_const128() {
return Some(const_of(*w, v));
}
if *w == a.width() {
return Some(a.clone());
}
None
}
Node::Sext(a, w) => {
if let Some(v) = a.as_const128() {
return Some(const_of(*w, mask128(signed(v, a.width()) as u128, *w)));
}
if *w == a.width() {
return Some(a.clone());
}
None
}
Node::Extract(a, hi, lo) => {
if let Some(v) = a.as_const128() {
return Some(const_of(*hi - *lo + 1, mask128(v >> *lo, *hi - *lo + 1)));
}
if *lo == 0 && *hi == a.width() - 1 {
return Some(a.clone());
}
None
}
Node::Concat(a, b) => {
if let (Some(x), Some(y)) = (a.as_const128(), b.as_const128()) {
let w = a.width() + b.width();
return Some(const_of(w, mask128((x << b.width()) | y, w)));
}
None
}
Node::Ite(c, a, b) => {
if a.ptr_eq(b) {
return Some(a.clone());
}
if let Some(cv) = c.as_const128() {
return Some(if cv != 0 { a.clone() } else { b.clone() });
}
None
}
}
}
fn fold_bin(op: Op, a: &Bv, b: &Bv) -> Option<Bv> {
let w = a.width();
if let (Some(x), Some(y)) = (a.as_const128(), b.as_const128()) {
return Some(const_of(w, eval_bin(op, x, y, w)));
}
let all = mask128(u128::MAX, w);
match op {
Op::Add => {
if a.as_const128() == Some(0) {
return Some(b.clone());
}
if b.as_const128() == Some(0) {
return Some(a.clone());
}
}
Op::Sub => {
if a.ptr_eq(b) {
return Some(const_of(w, 0));
}
if b.as_const128() == Some(0) {
return Some(a.clone());
}
}
Op::Mul => {
if a.as_const128() == Some(0) || b.as_const128() == Some(0) {
return Some(const_of(w, 0));
}
if a.as_const128() == Some(1) {
return Some(b.clone());
}
if b.as_const128() == Some(1) {
return Some(a.clone());
}
}
Op::And => {
if a.ptr_eq(b) {
return Some(a.clone());
}
if a.as_const128() == Some(0) || b.as_const128() == Some(0) {
return Some(const_of(w, 0));
}
if a.as_const128() == Some(all) {
return Some(b.clone());
}
if b.as_const128() == Some(all) {
return Some(a.clone());
}
}
Op::Or => {
if a.ptr_eq(b) {
return Some(a.clone());
}
if a.as_const128() == Some(0) {
return Some(b.clone());
}
if b.as_const128() == Some(0) {
return Some(a.clone());
}
if a.as_const128() == Some(all) || b.as_const128() == Some(all) {
return Some(const_of(w, all));
}
}
Op::Xor => {
if a.ptr_eq(b) {
return Some(const_of(w, 0));
}
if a.as_const128() == Some(0) {
return Some(b.clone());
}
if b.as_const128() == Some(0) {
return Some(a.clone());
}
if a.as_const128() == Some(all) {
return Some(b.not());
}
if b.as_const128() == Some(all) {
return Some(a.not());
}
}
Op::Udiv => {
if b.as_const128() == Some(1) {
return Some(a.clone());
}
if b.as_const128() == Some(0) {
return Some(const_of(w, all));
}
}
Op::Urem => {
if a.ptr_eq(b) {
return Some(const_of(w, 0));
}
if b.as_const128() == Some(1) {
return Some(const_of(w, 0));
}
if b.as_const128() == Some(0) {
return Some(a.clone());
}
}
Op::Sdiv => {
if b.as_const128() == Some(1) {
return Some(a.clone());
}
}
Op::Srem => {
if a.ptr_eq(b) {
return Some(const_of(w, 0));
}
if b.as_const128() == Some(1) {
return Some(const_of(w, 0));
}
if b.as_const128() == Some(0) {
return Some(a.clone());
}
}
}
None
}
fn fold_cmp(c: Cmp, a: &Bv, b: &Bv) -> Option<Bv> {
if let (Some(x), Some(y)) = (a.as_const128(), b.as_const128()) {
let w = a.width();
let bit = match c {
Cmp::Eq => x == y,
Cmp::Ne => x != y,
Cmp::Ult => x < y,
Cmp::Ule => x <= y,
Cmp::Slt => signed(x, w) < signed(y, w),
Cmp::Sle => signed(x, w) <= signed(y, w),
};
return Some(const_of(1, u128::from(bit)));
}
if a.ptr_eq(b) {
return Some(const_of(
1,
match c {
Cmp::Eq | Cmp::Ule | Cmp::Sle => 1,
Cmp::Ne | Cmp::Ult | Cmp::Slt => 0,
},
));
}
if matches!(c, Cmp::Eq | Cmp::Ne) {
if let (Node::Bin(op1, p, q), Node::Bin(op2, r, s)) = (&*a.0, &*b.0) {
if op1 == op2
&& is_commutative(*op1)
&& ((p.ptr_eq(r) && q.ptr_eq(s)) || (p.ptr_eq(s) && q.ptr_eq(r)))
{
return Some(const_of(1, u128::from(c == Cmp::Eq)));
}
}
}
None
}
fn is_commutative(op: Op) -> bool {
matches!(op, Op::And | Op::Or | Op::Xor | Op::Add | Op::Mul)
}
fn const_of(w: u32, v: u128) -> Bv {
Bv(Rc::new(Node::Const(w, mask128(v, w))))
}
pub(crate) fn eval_bin(op: Op, x: u128, y: u128, w: u32) -> u128 {
match op {
Op::And => mask128(x & y, w),
Op::Or => mask128(x | y, w),
Op::Xor => mask128(x ^ y, w),
Op::Add => mask128(x.wrapping_add(y), w),
Op::Sub => mask128(x.wrapping_sub(y), w),
Op::Mul => mask128(x.wrapping_mul(y), w),
Op::Udiv => udiv_(x, y, w),
Op::Urem => urem_(x, y, w),
Op::Sdiv => sdiv_(x, y, w),
Op::Srem => srem_(x, y, w),
}
}
pub(crate) fn signed(x: u128, w: u32) -> i128 {
let x = mask128(x, w);
if w == 128 {
x as i128
} else if (x >> (w - 1)) & 1 == 1 {
x as i128 - (1i128 << w)
} else {
x as i128
}
}
pub(crate) fn shl_(x: u128, k: u128, w: u32) -> u128 {
if k >= u128::from(w) {
0
} else {
mask128(x << k, w)
}
}
fn lshr_(x: u128, k: u128, w: u32) -> u128 {
if k >= u128::from(w) {
0
} else {
mask128(x, w) >> k
}
}
fn ashr_(x: u128, k: u128, w: u32) -> u128 {
let x = mask128(x, w);
if k >= u128::from(w) {
if (x >> (w - 1)) & 1 == 1 {
mask128(u128::MAX, w)
} else {
0
}
} else {
mask128((signed(x, w) >> k) as u128, w)
}
}
pub(crate) fn shr_(x: u128, k: u128, w: u32, arith: bool) -> u128 {
if arith {
ashr_(x, k, w)
} else {
lshr_(x, k, w)
}
}
fn rotl_(x: u128, k: u128, w: u32) -> u128 {
let x = mask128(x, w);
let k = (k % u128::from(w)) as u32;
if k == 0 {
x
} else {
mask128((x << k) | (x >> (w - k)), w)
}
}
fn rotr_(x: u128, k: u128, w: u32) -> u128 {
let x = mask128(x, w);
let k = (k % u128::from(w)) as u32;
if k == 0 {
x
} else {
mask128((x >> k) | (x << (w - k)), w)
}
}
pub(crate) fn rot_(x: u128, k: u128, w: u32, left: bool) -> u128 {
if left { rotl_(x, k, w) } else { rotr_(x, k, w) }
}
fn udiv_(x: u128, y: u128, w: u32) -> u128 {
x.checked_div(y).unwrap_or_else(|| mask128(u128::MAX, w))
}
fn urem_(x: u128, y: u128, _w: u32) -> u128 {
x.checked_rem(y).unwrap_or(x)
}
fn sdiv_(x: u128, y: u128, w: u32) -> u128 {
let (a, b) = (signed(x, w), signed(y, w));
if b == 0 {
return if a >= 0 { mask128(u128::MAX, w) } else { 1 };
}
if w == 128 && a == i128::MIN && b == -1 {
return mask128(a as u128, w);
}
mask128((a / b) as u128, w)
}
fn srem_(x: u128, y: u128, w: u32) -> u128 {
let (a, b) = (signed(x, w), signed(y, w));
if b == 0 {
return mask128(x, w);
}
if w == 128 && a == i128::MIN && b == -1 {
return 0;
}
mask128((a % b) as u128, w)
}
impl fmt::Debug for Bv {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Bv({self})")
}
}
impl fmt::Display for Bv {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &*self.0 {
Node::Const(w, v) => write!(f, "#{v:x}:{w}"),
Node::Var(w, id) => write!(f, "v{id}:{w}"),
Node::Not(a) => write!(f, "(~ {a})"),
Node::Neg(a) => write!(f, "(- {a})"),
Node::Bin(op, a, b) => {
let s = match op {
Op::And => "&",
Op::Or => "|",
Op::Xor => "^",
Op::Add => "+",
Op::Sub => "-",
Op::Mul => "*",
Op::Udiv => "/u",
Op::Urem => "%u",
Op::Sdiv => "/s",
Op::Srem => "%s",
};
write!(f, "({s} {a} {b})")
}
Node::ShlC(a, k) => write!(f, "(<< {a} {k})"),
Node::ShrC(a, k, arith) => {
write!(f, "({} {a} {k})", if *arith { ">>s" } else { ">>u" })
}
Node::ShlV(a, k) => write!(f, "(<<v {a} {k})"),
Node::ShrV(a, k, arith) => {
write!(f, "({}v {a} {k})", if *arith { ">>s" } else { ">>u" })
}
Node::RotC(a, k, left) => {
write!(f, "({} {a} {k})", if *left { "rotl" } else { "rotr" })
}
Node::RotV(a, k, left) => {
write!(f, "({}v {a} {k})", if *left { "rotl" } else { "rotr" })
}
Node::Compare(c, a, b) => {
let s = match c {
Cmp::Eq => "==",
Cmp::Ne => "!=",
Cmp::Ult => "<u",
Cmp::Ule => "<=u",
Cmp::Slt => "<s",
Cmp::Sle => "<=s",
};
write!(f, "({s} {a} {b})")
}
Node::Zext(a, w) => write!(f, "(zext{w} {a})"),
Node::Sext(a, w) => write!(f, "(sext{w} {a})"),
Node::Extract(a, hi, lo) => write!(f, "(ext[{hi}:{lo}] {a})"),
Node::Concat(a, b) => write!(f, "(cat {a} {b})"),
Node::Ite(c, a, b) => write!(f, "(ite {c} {a} {b})"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn var(w: u32) -> Bv {
Bv::wrap(Node::Var(w, 0))
}
#[test]
fn constant_arithmetic_folds() {
assert_eq!(Bv::val(1, 8).add(&Bv::val(1, 8)).as_const(), Some(2));
assert_eq!(Bv::val(250, 8).add(&Bv::val(10, 8)).as_const(), Some(4)); assert_eq!(Bv::val(9, 8).udiv(&Bv::val(0, 8)).as_const(), Some(0xff));
assert_eq!(Bv::val(9, 8).urem(&Bv::val(0, 8)).as_const(), Some(9));
assert_eq!(Bv::val(9, 8).sdiv(&Bv::val(0, 8)).as_const(), Some(0xff));
assert_eq!(Bv::val(0x80, 8).sdiv(&Bv::val(0, 8)).as_const(), Some(1));
assert_eq!(Bv::val(0x80, 8).ashr(2).as_const(), Some(0xe0));
assert_eq!(Bv::val(3, 8).ult(&Bv::val(4, 8)).as_const(), Some(1));
assert_eq!(Bv::val(3, 8).slt(&Bv::val(0xff, 8)).as_const(), Some(0)); }
#[test]
fn identities_return_the_existing_child() {
let x = var(8);
assert!(x.add(&Bv::val(0, 8)).ptr_eq(&x));
assert!(Bv::val(0, 8).add(&x).ptr_eq(&x));
assert!(x.sub(&Bv::val(0, 8)).ptr_eq(&x));
assert!(x.mul(&Bv::val(1, 8)).ptr_eq(&x));
assert!(x.and(&Bv::val(0xff, 8)).ptr_eq(&x));
assert!(x.or(&Bv::val(0, 8)).ptr_eq(&x));
assert!(x.xor(&Bv::val(0, 8)).ptr_eq(&x));
assert!(x.shl(0).ptr_eq(&x));
assert!(x.rotl(0).ptr_eq(&x));
assert!(x.neg().neg().ptr_eq(&x));
assert!(x.not().not().ptr_eq(&x));
}
#[test]
fn identities_return_constants() {
let x = var(8);
assert_eq!(x.sub(&x).as_const(), Some(0));
assert_eq!(x.xor(&x).as_const(), Some(0));
assert_eq!(x.mul(&Bv::val(0, 8)).as_const(), Some(0));
assert_eq!(x.and(&Bv::val(0, 8)).as_const(), Some(0));
assert_eq!(x.or(&Bv::val(0xff, 8)).as_const(), Some(0xff));
assert_eq!(x.udiv(&Bv::val(0, 8)).as_const(), Some(0xff));
assert_eq!(x.urem(&Bv::val(1, 8)).as_const(), Some(0));
}
#[test]
fn a_value_compared_with_itself_folds() {
let x = var(8);
assert_eq!(x.eq(&x).as_const(), Some(1));
assert_eq!(x.ne(&x).as_const(), Some(0));
assert_eq!(x.ult(&x).as_const(), Some(0));
assert_eq!(x.ule(&x).as_const(), Some(1));
assert_eq!(x.slt(&x).as_const(), Some(0));
assert_eq!(x.sle(&x).as_const(), Some(1));
}
#[test]
fn a_symmetric_operation_equals_its_commuted_twin() {
let a = var(8);
let b = var(8);
assert_eq!(a.add(&b).eq(&b.add(&a)).as_const(), Some(1));
assert_eq!(a.mul(&b).eq(&b.mul(&a)).as_const(), Some(1));
assert_eq!(a.and(&b).eq(&b.and(&a)).as_const(), Some(1));
assert_eq!(a.add(&b).ne(&b.add(&a)).as_const(), Some(0));
}
}