use super::expr::{MNode, MOp, MbaExpr};
use crate::error::Error;
use crate::expr::{Context, Expr, OpCode};
use crate::hash::IdMap;
use crate::ops::{BinOp, UnOp};
use crate::{BitVec, Width};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct MbaLimits {
pub max_vars: u32,
pub max_nodes: u32,
pub max_width: u16,
pub min_nodes: u32,
}
impl Default for MbaLimits {
fn default() -> Self {
MbaLimits {
max_vars: 8,
max_nodes: 2048,
max_width: 512,
min_nodes: 4,
}
}
}
setters!(MbaLimits {
with_max_vars: max_vars: u32,
with_max_nodes: max_nodes: u32,
with_max_width: max_width: u16,
with_min_nodes: min_nodes: u32,
});
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Refusal {
TooManyVars,
TooLarge,
TooWide,
TooSmall,
NotMixed,
Unsupported(String),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Bindings {
pub(crate) atoms: Vec<u32>,
}
impl Bindings {
pub fn len(&self) -> usize {
self.atoms.len()
}
pub fn is_empty(&self) -> bool {
self.atoms.is_empty()
}
}
fn in_fragment(cx: &Context, i: u32) -> bool {
let n = cx.node(i);
match n.op {
OpCode::Const
| OpCode::Add
| OpCode::Sub
| OpCode::Mul
| OpCode::Neg
| OpCode::And
| OpCode::Or
| OpCode::Xor
| OpCode::Not
| OpCode::Zext
| OpCode::Sext
| OpCode::Concat => true,
OpCode::Extract => n.b + u32::from(n.width) <= u32::from(cx.width_of(n.a).bits()),
OpCode::Shl | OpCode::LShr => cx
.const_val(n.b)
.and_then(|k| k.to_u64())
.is_some_and(|k| k < u64::from(n.width)),
_ => false,
}
}
pub(crate) fn fits_nodes(cx: &Context, root: u32, lim: &MbaLimits) -> Result<Vec<u32>, Refusal> {
let (_, order) = scan(cx, root, lim)?;
Ok(order)
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum Reading {
SemiLinear,
Shallow,
Linear,
Cut(u32),
CutWhole(u32),
}
fn in_reading(cx: &Context, i: u32, reading: Reading) -> bool {
let n = cx.node(i);
let uniform = |k: u32| cx.const_val(k).is_none_or(|v| v.is_zero() || v.is_ones());
match n.op {
OpCode::Const | OpCode::Add | OpCode::Sub | OpCode::Neg => true,
OpCode::Mul => cx.const_val(n.a).is_some() || cx.const_val(n.b).is_some(),
OpCode::Shl => reading == Reading::SemiLinear && in_fragment(cx, i),
OpCode::And | OpCode::Or | OpCode::Xor | OpCode::Not => {
reading == Reading::SemiLinear || n.children().all(uniform)
}
_ => false,
}
}
fn scan(cx: &Context, root: u32, lim: &MbaLimits) -> Result<(Vec<u32>, Vec<u32>), Refusal> {
scan_in(cx, root, lim, &in_fragment)
}
fn scan_in(
cx: &Context,
root: u32,
lim: &MbaLimits,
in_fragment: &dyn Fn(&Context, u32) -> bool,
) -> Result<(Vec<u32>, Vec<u32>), Refusal> {
let mut atoms: Vec<u32> = Vec::new();
let mut order: Vec<u32> = Vec::new();
let mut seen: IdMap<u32, ()> = IdMap::default();
let mut stack: Vec<(u32, bool)> = vec![(root, false)];
while let Some((i, expanded)) = stack.pop() {
if expanded {
order.push(i);
continue;
}
if seen.insert(i, ()).is_some() {
continue;
}
if cx.node(i).width > lim.max_width {
return Err(Refusal::TooWide);
}
if seen.len() as u32 > lim.max_nodes {
return Err(Refusal::TooLarge);
}
if i != root && !in_fragment(cx, i) {
atoms.push(i);
if atoms.len() as u32 > lim.max_vars {
return Err(Refusal::TooManyVars);
}
continue;
}
if !in_fragment(cx, i) {
return Err(Refusal::Unsupported("not an MBA operator".into()));
}
stack.push((i, true));
for c in cx.node(i).children() {
stack.push((c, false));
}
}
Ok((atoms, order))
}
pub(crate) fn lower_id(
cx: &Context,
root: u32,
lim: &MbaLimits,
) -> Result<(MbaExpr, Bindings), Refusal> {
let (atoms, order) = scan(cx, root, lim)?;
build(cx, atoms, order)
}
pub(crate) fn lower_id_reading(
cx: &Context,
root: u32,
lim: &MbaLimits,
reading: Reading,
) -> Result<(MbaExpr, Bindings), Refusal> {
let (atoms, order) = match reading {
Reading::Shallow => {
let under = under_bitwise(cx, root, lim)?;
scan_in(cx, root, lim, &|cx, i| {
in_reading(cx, i, Reading::SemiLinear)
&& (bitwise_or_const(cx, i) || !under.contains_key(&i))
})?
}
Reading::Cut(cut) => scan_in(cx, root, lim, &|cx, i| {
i != cut && in_reading(cx, i, Reading::SemiLinear)
})?,
Reading::CutWhole(cut) => scan_in(cx, root, lim, &|cx, i| i != cut && in_fragment(cx, i))?,
_ => scan_in(cx, root, lim, &|cx, i| in_reading(cx, i, reading))?,
};
build(cx, atoms, order)
}
pub(crate) fn shared_nodes(cx: &Context, root: u32, lim: &MbaLimits, n: usize) -> Vec<u32> {
let mut users: IdMap<u32, u32> = IdMap::default();
let mut seen: IdMap<u32, ()> = IdMap::default();
let mut stack = vec![root];
while let Some(i) = stack.pop() {
if seen.insert(i, ()).is_some() || seen.len() as u32 > lim.max_nodes {
continue;
}
if !in_reading(cx, i, Reading::SemiLinear) {
continue;
}
for c in cx.node(i).children() {
*users.entry(c).or_default() += 1;
stack.push(c);
}
}
let mut v: Vec<(u32, u32)> = users
.into_iter()
.filter(|&(i, k)| {
k >= 2 && cx.const_val(i).is_none() && in_reading(cx, i, Reading::SemiLinear)
})
.filter(|&(i, _)| cx.node(i).op != OpCode::Not)
.map(|(i, k)| (k, i))
.collect();
v.sort_by(|a, b| b.cmp(a));
v.into_iter().take(n).map(|(_, i)| i).collect()
}
fn bitwise_or_const(cx: &Context, i: u32) -> bool {
matches!(
cx.node(i).op,
OpCode::Const | OpCode::And | OpCode::Or | OpCode::Xor | OpCode::Not
)
}
fn under_bitwise(cx: &Context, root: u32, lim: &MbaLimits) -> Result<IdMap<u32, ()>, Refusal> {
let mut under: IdMap<u32, ()> = IdMap::default();
let mut seen: IdMap<(u32, bool), ()> = IdMap::default();
let mut stack: Vec<(u32, bool)> = vec![(root, false)];
while let Some((i, bw)) = stack.pop() {
if seen.insert((i, bw), ()).is_some() {
continue;
}
if seen.len() as u32 > lim.max_nodes.saturating_mul(2) {
return Err(Refusal::TooLarge);
}
if bw {
under.insert(i, ());
}
let bitwise = bitwise_or_const(cx, i);
if (bw && !bitwise) || !in_reading(cx, i, Reading::SemiLinear) {
continue;
}
for c in cx.node(i).children() {
stack.push((c, bitwise));
}
}
Ok(under)
}
fn build(cx: &Context, atoms: Vec<u32>, order: Vec<u32>) -> Result<(MbaExpr, Bindings), Refusal> {
let mut m = MbaExpr::new(atoms.iter().map(|&a| cx.width_of(a)).collect());
let mut at: IdMap<u32, u32> = IdMap::default();
for (&a, k) in atoms.iter().zip(0u32..) {
let v = m
.push(MOp::Var(k), &[])
.map_err(|e| Refusal::Unsupported(e.to_string()))?;
at.insert(a, v);
}
for i in order {
let n = cx.node(i);
let arg = |k: u32| at[&k];
let bad = |e: super::expr::MbaError| Refusal::Unsupported(e.to_string());
let v = match n.op {
OpCode::Const => m.push(
MOp::Const(cx.const_val(i).unwrap_or(BitVec::zero(Width::W1))),
&[],
),
OpCode::Add => m.push(MOp::Add, &[arg(n.a), arg(n.b)]),
OpCode::Sub => m.push(MOp::Sub, &[arg(n.a), arg(n.b)]),
OpCode::Mul => m.push(MOp::Mul, &[arg(n.a), arg(n.b)]),
OpCode::And => m.push(MOp::And, &[arg(n.a), arg(n.b)]),
OpCode::Or => m.push(MOp::Or, &[arg(n.a), arg(n.b)]),
OpCode::Xor => m.push(MOp::Xor, &[arg(n.a), arg(n.b)]),
OpCode::Neg => m.push(MOp::Neg, &[arg(n.a)]),
OpCode::Not => m.push(MOp::Not, &[arg(n.a)]),
OpCode::Shl | OpCode::LShr => {
let k = cx.const_val(n.b).and_then(|k| k.to_u64()).unwrap_or(0) as u16;
let op = if n.op == OpCode::Shl {
MOp::Shl(k)
} else {
MOp::LShr(k)
};
m.push(op, &[arg(n.a)])
}
OpCode::Zext => m.push_cast(MOp::Zext, arg(n.a), cx.width_of(i)),
OpCode::Sext => m.push_cast(MOp::Sext, arg(n.a), cx.width_of(i)),
OpCode::Extract if n.b == 0 => m.push_cast(MOp::Trunc, arg(n.a), cx.width_of(i)),
OpCode::Extract => (|| {
let s = m.push(MOp::LShr(n.b as u16), &[arg(n.a)])?;
if cx.width_of(i) == cx.width_of(n.a) {
return Ok(s);
}
m.push_cast(MOp::Trunc, s, cx.width_of(i))
})(),
OpCode::Concat => (|| {
let w = cx.width_of(i);
let lw = cx.width_of(n.b);
let hn = cx.node(n.a);
let hi = if hn.op == OpCode::Extract && hn.b == 0 && cx.width_of(hn.a) == w {
arg(hn.a)
} else {
m.push_cast(MOp::Zext, arg(n.a), w)?
};
let hi = m.push(MOp::Shl(lw.bits()), &[hi])?;
let lo = match cx.const_val(n.b) {
Some(c) => m.push(MOp::Const(c.zext(w).unwrap_or(BitVec::zero(w))), &[])?,
None => m.push_cast(MOp::Zext, arg(n.b), w)?,
};
m.push(MOp::Add, &[hi, lo])
})(),
_ => return Err(Refusal::Unsupported("not an MBA operator".into())),
}
.map_err(bad)?;
at.insert(i, v);
}
Ok((m, Bindings { atoms }))
}
pub fn lower(cx: &Context, e: Expr, lim: &MbaLimits) -> Result<(MbaExpr, Bindings), Error> {
let i = cx.id(e)?;
lower_id(cx, i, lim).map_err(|r| Error::Unsupported(format!("{r:?}")))
}
pub(crate) fn lift_id(cx: &mut Context, m: &MbaExpr, b: &Bindings) -> Result<u32, Error> {
let mut v: Vec<u32> = Vec::with_capacity(m.nodes().len());
for n in m.nodes() {
let a = |k: usize| v[n.args[k] as usize];
let r = match n.op {
MOp::Const(c) => cx.mk_const(&c)?,
MOp::Var(i) => *b
.atoms
.get(i as usize)
.ok_or(Error::Unsupported("unbound MBA variable".into()))?,
MOp::Add => cx.c_bin(BinOp::Add, a(0), a(1))?,
MOp::Sub => cx.c_bin(BinOp::Sub, a(0), a(1))?,
MOp::Mul => cx.c_bin(BinOp::Mul, a(0), a(1))?,
MOp::And => cx.c_bin(BinOp::And, a(0), a(1))?,
MOp::Or => cx.c_bin(BinOp::Or, a(0), a(1))?,
MOp::Xor => cx.c_bin(BinOp::Xor, a(0), a(1))?,
MOp::Neg => cx.c_un(UnOp::Neg, a(0))?,
MOp::Not => cx.c_un(UnOp::Not, a(0))?,
MOp::Shl(k) | MOp::LShr(k) => {
let kc = cx.mk_const(&BitVec::wrapping_from_u64(n.width, u64::from(k)))?;
let op = if matches!(n.op, MOp::Shl(_)) {
BinOp::Shl
} else {
BinOp::LShr
};
cx.c_bin(op, a(0), kc)?
}
MOp::Zext => cx.c_zext(a(0), n.width.bits())?,
MOp::Sext => cx.c_sext(a(0), n.width.bits())?,
MOp::Trunc => match m.nodes()[n.args[0] as usize] {
MNode {
op: MOp::LShr(k),
width,
args,
} if k + n.width.bits() <= width.bits() => {
cx.c_extract(v[args[0] as usize], k, n.width.bits())?
}
_ => cx.c_extract(a(0), 0, n.width.bits())?,
},
};
v.push(r);
}
v.last()
.copied()
.ok_or(Error::Unsupported("empty MBA expression".into()))
}
pub fn lift(cx: &mut Context, m: &MbaExpr, b: &Bindings) -> Result<Expr, Error> {
let i = lift_id(cx, m, b)?;
Ok(cx.handle(i))
}