use crate::executor::eval;
use crate::il;
use crate::Error;
use std::fmt::Debug;
pub trait Value: Clone + Debug + Eq + PartialEq {
fn constant(constant: il::Constant) -> Self;
fn bits(&self) -> usize;
fn shl(&self, bits: usize) -> Result<Self, Error>;
fn shr(&self, bits: usize) -> Result<Self, Error>;
fn trun(&self, bits: usize) -> Result<Self, Error>;
fn zext(&self, bits: usize) -> Result<Self, Error>;
fn or(&self, other: &Self) -> Result<Self, Error>;
}
impl Value for il::Constant {
fn constant(constant: il::Constant) -> Self {
constant
}
fn bits(&self) -> usize {
self.bits()
}
fn shl(&self, bits: usize) -> Result<Self, Error> {
eval(&il::Expression::shl(
self.clone().into(),
il::expr_const(bits as u64, self.bits()),
)?)
}
fn shr(&self, bits: usize) -> Result<Self, Error> {
eval(&il::Expression::shr(
self.clone().into(),
il::expr_const(bits as u64, self.bits()),
)?)
}
fn trun(&self, bits: usize) -> Result<Self, Error> {
eval(&il::Expression::trun(bits, self.clone().into())?)
}
fn zext(&self, bits: usize) -> Result<Self, Error> {
eval(&il::Expression::zext(bits, self.clone().into())?)
}
fn or(&self, other: &Self) -> Result<Self, Error> {
eval(&il::Expression::or(
self.clone().into(),
other.clone().into(),
)?)
}
}
impl Value for il::Expression {
fn constant(constant: il::Constant) -> Self {
il::Expression::constant(constant)
}
fn bits(&self) -> usize {
self.bits()
}
fn shl(&self, bits: usize) -> Result<Self, Error> {
il::Expression::shl(self.clone(), il::expr_const(bits as u64, self.bits()))
}
fn shr(&self, bits: usize) -> Result<Self, Error> {
il::Expression::shr(self.clone(), il::expr_const(bits as u64, self.bits()))
}
fn trun(&self, bits: usize) -> Result<Self, Error> {
il::Expression::trun(bits, self.clone())
}
fn zext(&self, bits: usize) -> Result<Self, Error> {
il::Expression::zext(bits, self.clone())
}
fn or(&self, other: &Self) -> Result<Self, Error> {
il::Expression::or(self.clone(), other.clone())
}
}