use super::budget::Meter;
use super::{Budget, Engine, Runner, Stop};
use crate::BitVec;
use crate::error::Error;
use crate::expr::{Context, Expr};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LinearForm {
pub constant: BitVec,
pub terms: Vec<(BitVec, Expr)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct XorForm {
pub constant: BitVec,
pub terms: Vec<(BitVec, Expr)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BitwiseForm {
pub terms: Vec<Expr>,
pub classes: Vec<(BitVec, u8)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PredicateForm {
Constant(bool),
Values {
of: Expr,
intervals: Vec<(BitVec, BitVec)>,
},
Relation {
left: Expr,
right: Expr,
relations: u8,
},
Opaque,
}
impl Engine {
pub fn predicate_form(&self, cx: &mut Context, e: Expr) -> Result<PredicateForm, Error> {
use super::pass::compares::Info;
let id = cx.id(e)?;
let mut runner = self.analyst(id);
Ok(match super::pass::compares::info_of(&mut runner, cx, id) {
Ok(Info::Const(b)) => PredicateForm::Constant(b),
Ok(Info::Values(x, set, _)) => PredicateForm::Values {
of: cx.handle(x),
intervals: set,
},
Ok(Info::Pair(x, y, relations)) => PredicateForm::Relation {
left: cx.handle(x),
right: cx.handle(y),
relations,
},
Ok(_) | Err(Stop::Exhausted(_)) => PredicateForm::Opaque,
Err(Stop::Error(e)) => return Err(e),
})
}
pub fn bitwise_form(&self, cx: &mut Context, e: Expr) -> Result<Option<BitwiseForm>, Error> {
let id = cx.id(e)?;
let mut runner = self.analyst(id);
match super::pass::bitwise::masked(&mut runner, cx, &[id]) {
Ok(Some(m)) => {
let (atoms, classes) = m.atoms_classes();
Ok(Some(BitwiseForm {
terms: atoms.iter().map(|&a| cx.handle(a)).collect(),
classes: classes.map(|(m, t)| (*m, t)).collect(),
}))
}
Ok(None) | Err(Stop::Exhausted(_)) => Ok(None),
Err(Stop::Error(e)) => Err(e),
}
}
pub fn linear_form(&self, cx: &mut Context, e: Expr) -> Result<LinearForm, Error> {
let id = cx.id(e)?;
let mut runner = self.analyst(id);
let (constant, terms) = match super::pass::linear::form_of(&mut runner, cx, id) {
Ok(form) => {
let (k, ts) = form.konst_terms();
(*k, ts.to_vec())
}
Err(Stop::Error(e)) => return Err(e),
Err(Stop::Exhausted(_)) => {
let w = cx.width(e)?;
(BitVec::zero(w), vec![(id, BitVec::wrapping_from_u64(w, 1))])
}
};
Ok(LinearForm {
constant,
terms: (terms.into_iter())
.map(|(t, c)| (c, cx.handle(t)))
.collect(),
})
}
pub fn xor_form(&self, cx: &mut Context, e: Expr) -> Result<XorForm, Error> {
let id = cx.id(e)?;
let mut runner = self.analyst(id);
let (constant, terms) = match super::pass::xor::form_of(&mut runner, cx, id) {
Ok(form) => {
let (k, ts) = form.konst_terms();
(*k, ts.to_vec())
}
Err(Stop::Error(e)) => return Err(e),
Err(Stop::Exhausted(_)) => {
let w = cx.width(e)?;
(BitVec::zero(w), vec![(id, BitVec::ones(w))])
}
};
Ok(XorForm {
constant,
terms: (terms.into_iter())
.map(|(t, m)| (m, cx.handle(t)))
.collect(),
})
}
pub fn under_mask(&self, cx: &mut Context, e: Expr, mask: &BitVec) -> Result<Expr, Error> {
let id = cx.id(e)?;
if cx.width(e)? != mask.width() {
return Err(Error::Contract(
"the mask is not of the expression's width".into(),
));
}
let mut runner = self.analyst(id);
match super::pass::demanded::under_mask(&mut runner, cx, id, mask) {
Ok(out) => Ok(cx.handle(out)),
Err(Stop::Exhausted(_)) => Ok(e),
Err(Stop::Error(e)) => Err(e),
}
}
fn analyst(&self, root: u32) -> Runner<'_, 'static> {
let meter = Meter::new(Budget::default(), None);
Runner::new(&self.inner, meter, None, None, None, &[root])
}
}