use vlk_base::target::Isa;
use vlk_ir::ir::semantics::{mask, operand_width};
use vlk_ir::ir::{BinOp, Cond, Expr, Operation, Stmt};
use vlk_types::types::Precision;
#[cfg(test)]
mod smt_tests;
#[derive(Default)]
pub(super) struct State {
pub pending: Option<Flags>,
pub touched: bool,
pub written: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Source {
Difference,
Sum,
Logic,
Result,
Zero,
Shift,
Fixed(u8),
Carry,
CarryOverflow,
FloatCompare(Precision),
Stored { overflow: bool },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Flags {
pub source: Source,
pub lhs: Expr,
pub rhs: Expr,
pub carry: bool,
pub low_cleared: bool,
pub chosen: Option<Choice>,
pub width: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Choice {
pub cond: Cond,
pub lhs: Expr,
pub rhs: Expr,
pub otherwise: u8,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum Test {
Always,
Never,
Compare(Cond, Expr, Expr),
Unknown,
}
impl Flags {
pub(super) fn byte_tests(&self, isa: Isa) -> [(Test, u32); 5] {
[
(self.test(Cond::Mi, isa), 7),
(self.test(Cond::Eq, isa), 6),
(self.adjust(), 4),
(self.parity(true), 2),
(self.test(Cond::Cc, isa), 0),
]
}
pub fn difference(lhs: Expr, rhs: Expr) -> Self {
Self::new(Source::Difference, lhs, rhs)
}
pub fn sum(lhs: Expr, rhs: Expr) -> Self {
Self::new(Source::Sum, lhs, rhs)
}
pub fn logic(result: Expr) -> Self {
Self::new(Source::Logic, result, Expr::constant(0))
}
pub fn logic_at(result: Expr, width: u32) -> Self {
Self {
width: Some(width),
..Self::logic(result)
}
}
pub fn result(result: Expr) -> Self {
Self::new(Source::Result, result, Expr::constant(0))
}
pub fn zero(bit: Expr) -> Self {
Self::new(Source::Zero, bit, Expr::constant(0))
}
pub fn shift(result: Expr, carry: Expr) -> Self {
Self::new(Source::Shift, result, carry)
}
pub fn float_compare(lhs: Expr, rhs: Expr, precision: Precision) -> Self {
Self::new(Source::FloatCompare(precision), lhs, rhs)
}
pub fn parity(&self, even: bool) -> Test {
let set = match self.source {
_ if self.chosen.is_some() => Test::Unknown,
Source::FloatCompare(precision) => {
holds(Operation::FloatUnordered(precision), &self.lhs, &self.rhs)
}
_ if self.low_cleared => Test::Never,
Source::Stored { .. } => bit_set(&self.lhs, 2),
Source::Difference | Source::Sum | Source::Logic | Source::Shift => {
let low = Expr::bin(BinOp::And, self.value(), Expr::constant(0xff));
let folded = Expr::bin(
BinOp::And,
Expr::bin(
BinOp::Xor,
low.clone(),
Expr::bin(BinOp::Shr, low, Expr::constant(4)),
),
Expr::constant(0xf),
);
bit_set(&Expr::bin(BinOp::Shr, Expr::constant(0x9669), folded), 0)
}
_ => Test::Unknown,
};
if even { set } else { not(set) }
}
pub fn adjust(&self) -> Test {
match self.source {
_ if self.chosen.is_some() => Test::Unknown,
Source::FloatCompare(_) => Test::Never,
_ if self.low_cleared => Test::Never,
Source::Stored { .. } => bit_set(&self.lhs, 4),
Source::Difference | Source::Sum => {
let operands = Expr::bin(BinOp::Xor, self.lhs.clone(), self.rhs.clone());
bit_set(&Expr::bin(BinOp::Xor, operands, self.value()), 4)
}
_ => Test::Unknown,
}
}
fn value(&self) -> Expr {
let op = match self.source {
Source::Difference => BinOp::Sub,
Source::Sum => BinOp::Add,
_ => return self.lhs.clone(),
};
Expr::bin(op, self.lhs.clone(), self.rhs.clone())
}
pub fn stored(byte: Expr, overflow: Option<Expr>) -> Self {
let known = overflow.is_some();
Self::new(
Source::Stored { overflow: known },
byte,
overflow.unwrap_or(Expr::constant(0)),
)
}
pub fn clearing_low(self) -> Self {
Self {
low_cleared: true,
..self
}
}
pub fn carry(bit: Expr) -> Self {
Self::new(Source::Carry, bit, Expr::constant(0))
}
pub fn carry_overflow(lost: Expr) -> Self {
Self::new(Source::CarryOverflow, lost, Expr::constant(0))
}
pub fn fixed(nzcv: u8) -> Self {
Self::new(Source::Fixed(nzcv), Expr::constant(0), Expr::constant(0))
}
fn new(source: Source, lhs: Expr, rhs: Expr) -> Self {
Self {
source,
lhs,
rhs,
carry: true,
low_cleared: false,
chosen: None,
width: None,
}
}
pub fn chosen_by(self, earlier: Test, otherwise: u8) -> Option<Self> {
match earlier {
Test::Always => Some(self),
Test::Never => Some(Self::fixed(otherwise)),
Test::Compare(cond, lhs, rhs) => Some(Self {
chosen: Some(Choice {
cond,
lhs,
rhs,
otherwise,
}),
..self
}),
Test::Unknown => None,
}
}
pub fn carried(&self) -> (Flags, Vec<Expr>) {
let mut template = self.clone();
let mut values = Vec::new();
for (slot, operand) in vlk_ir::ir::FLAG_CARRIED
.into_iter()
.zip(template.operands_mut())
{
let width = match vlk_ir::ir::semantics::width(operand) {
0 => 64,
bits => bits as u8,
};
values.push(std::mem::replace(operand, Expr::reg(slot, width)));
}
(template, values)
}
pub fn operands_mut(&mut self) -> Vec<&mut Expr> {
let mut operands = vec![&mut self.lhs, &mut self.rhs];
if let Some(choice) = &mut self.chosen {
operands.push(&mut choice.lhs);
operands.push(&mut choice.rhs);
}
operands
}
pub fn keeping_carry(self) -> Self {
Self {
carry: false,
..self
}
}
pub fn test(&self, cond: Cond, isa: Isa) -> Test {
let own = self.own_test(cond, isa);
let Some(choice) = &self.chosen else {
return own;
};
let when = Test::Compare(choice.cond, choice.lhs.clone(), choice.rhs.clone());
if fixed_holds(cond, choice.otherwise) {
not(both(when, not(own)))
} else {
both(when, own)
}
}
fn own_test(&self, cond: Cond, isa: Isa) -> Test {
let (a, b) = (&self.lhs, &self.rhs);
let reads_carry = matches!(cond, Cond::Cs | Cond::Cc | Cond::Hi | Cond::Ls);
if reads_carry && !self.carry {
return Test::Unknown;
}
let bits = operand_width(a, b);
let compare = |cond, lhs: &Expr, rhs: &Expr| Test::Compare(cond, lhs.clone(), rhs.clone());
let zero = Expr::constant(0);
match self.source {
Source::Zero => match cond {
Cond::Eq => compare(Cond::Ne, a, &zero),
Cond::Ne => compare(Cond::Eq, a, &zero),
_ => Test::Unknown,
},
Source::CarryOverflow => match cond {
Cond::Cc | Cond::Vs => compare(Cond::Ne, a, &zero),
Cond::Cs | Cond::Vc => compare(Cond::Eq, a, &zero),
_ => Test::Unknown,
},
Source::Carry => {
let set = if matches!(isa, Isa::I386 | Isa::X86_64) {
Cond::Cc
} else {
Cond::Cs
};
if cond == set {
compare(Cond::Ne, a, &zero)
} else if cond == set.invert() {
compare(Cond::Eq, a, &zero)
} else {
Test::Unknown
}
}
Source::FloatCompare(precision) => float_test(a, b, precision, cond, isa),
Source::Stored { overflow } => stored_test(a, b, overflow, cond),
Source::Fixed(nzcv) => {
if fixed_holds(cond, nzcv) {
Test::Always
} else {
Test::Never
}
}
Source::Difference => {
let difference = || Expr::bin(BinOp::Sub, a.clone(), b.clone());
let overflow = || {
Expr::bin(
BinOp::And,
Expr::bin(BinOp::Xor, a.clone(), b.clone()),
Expr::bin(BinOp::Xor, a.clone(), difference()),
)
};
match (cond, b) {
(Cond::Mi, Expr::Const(0)) => compare(Cond::Lt, a, b),
(Cond::Pl, Expr::Const(0)) => compare(Cond::Ge, a, b),
(Cond::Mi, _) => Test::Compare(Cond::Lt, difference(), zero),
(Cond::Pl, _) => Test::Compare(Cond::Ge, difference(), zero),
(Cond::Vs, _) => Test::Compare(Cond::Lt, overflow(), zero),
(Cond::Vc, _) => Test::Compare(Cond::Ge, overflow(), zero),
_ => compare(cond, a, b),
}
}
Source::Logic
if self.width.is_some_and(|width| width > bits)
&& matches!(
cond,
Cond::Mi | Cond::Pl | Cond::Lt | Cond::Ge | Cond::Gt | Cond::Le
) =>
{
match cond {
Cond::Mi | Cond::Lt => Test::Never,
Cond::Pl | Cond::Ge => Test::Always,
Cond::Gt => compare(Cond::Ne, a, &zero),
_ => compare(Cond::Eq, a, &zero),
}
}
Source::Logic | Source::Result | Source::Shift => {
let sign = |cond| match cond {
Cond::Mi => Cond::Lt,
Cond::Pl => Cond::Ge,
other => other,
};
match cond {
Cond::Eq | Cond::Ne | Cond::Mi | Cond::Pl => compare(sign(cond), a, &zero),
Cond::Cc if self.source == Source::Shift => compare(Cond::Ne, b, &zero),
Cond::Cs if self.source == Source::Shift => compare(Cond::Eq, b, &zero),
Cond::Hi | Cond::Ls if self.source == Source::Shift => {
let above = both(
Test::Compare(Cond::Eq, b.clone(), zero.clone()),
Test::Compare(Cond::Ne, a.clone(), zero),
);
if cond == Cond::Hi { above } else { not(above) }
}
_ if self.source != Source::Logic => Test::Unknown,
Cond::Vs => Test::Never,
Cond::Vc => Test::Always,
Cond::Ge | Cond::Lt | Cond::Gt | Cond::Le => compare(cond, a, &zero),
_ => match (isa, cond) {
(Isa::I386 | Isa::X86_64, Cond::Cs)
| (Isa::AArch64, Cond::Cc | Cond::Ls) => Test::Always,
(Isa::I386 | Isa::X86_64, Cond::Cc)
| (Isa::AArch64, Cond::Cs | Cond::Hi) => Test::Never,
(_, Cond::Hi) => compare(Cond::Ne, a, &zero),
_ => compare(Cond::Eq, a, &zero),
},
}
}
Source::Sum => sum(a, b, bits, cond, isa),
}
}
}
fn bit_set(value: &Expr, bit: u64) -> Test {
let shifted = match bit {
0 => value.clone(),
_ => Expr::bin(BinOp::Shr, value.clone(), Expr::constant(bit)),
};
Test::Compare(
Cond::Ne,
Expr::bin(BinOp::And, shifted, Expr::constant(1)),
Expr::constant(0),
)
}
fn stored_test(byte: &Expr, overflow: &Expr, known: bool, cond: Cond) -> Test {
let zero = || Expr::constant(0);
let sign = || {
Expr::bin(
BinOp::And,
Expr::bin(BinOp::Shr, byte.clone(), Expr::constant(7)),
Expr::constant(1),
)
};
let above = Test::Compare(
Cond::Eq,
Expr::bin(BinOp::And, byte.clone(), Expr::constant(0x41)),
zero(),
);
let sign_is_overflow = || Test::Compare(Cond::Eq, sign(), overflow.clone());
let greater = || both(not(bit_set(byte, 6)), sign_is_overflow());
match cond {
Cond::Eq => bit_set(byte, 6),
Cond::Ne => not(bit_set(byte, 6)),
Cond::Cc => bit_set(byte, 0),
Cond::Cs => not(bit_set(byte, 0)),
Cond::Mi => bit_set(byte, 7),
Cond::Pl => not(bit_set(byte, 7)),
Cond::Hi => above,
Cond::Ls => not(above),
_ if !known => Test::Unknown,
Cond::Vs => Test::Compare(Cond::Ne, overflow.clone(), zero()),
Cond::Vc => Test::Compare(Cond::Eq, overflow.clone(), zero()),
Cond::Ge => sign_is_overflow(),
Cond::Lt => not(sign_is_overflow()),
Cond::Gt => greater(),
Cond::Le => not(greater()),
}
}
fn holds(op: Operation, a: &Expr, b: &Expr) -> Test {
Test::Compare(
Cond::Ne,
Expr::apply(op, vec![a.clone(), b.clone()]),
Expr::constant(0),
)
}
fn float_test(a: &Expr, b: &Expr, precision: Precision, cond: Cond, isa: Isa) -> Test {
let less = |x, y| holds(Operation::FloatLess(precision), x, y);
let less_equal = |x, y| holds(Operation::FloatLessEqual(precision), x, y);
let equal = holds(Operation::FloatEqual(precision), a, b);
let unordered = holds(Operation::FloatUnordered(precision), a, b);
let equal_or_unordered = || not(both(not(equal.clone()), not(unordered.clone())));
match isa {
Isa::I386 | Isa::X86_64 => match cond {
Cond::Eq | Cond::Le => equal_or_unordered(),
Cond::Ne | Cond::Gt => not(equal_or_unordered()),
Cond::Cc => not(less_equal(b, a)),
Cond::Cs => less_equal(b, a),
Cond::Hi => less(b, a),
Cond::Ls => not(less(b, a)),
Cond::Mi | Cond::Vs | Cond::Lt => Test::Never,
Cond::Pl | Cond::Vc | Cond::Ge => Test::Always,
},
_ => match cond {
Cond::Eq => equal,
Cond::Ne => not(equal),
Cond::Cc | Cond::Mi => less(a, b),
Cond::Cs | Cond::Pl => not(less(a, b)),
Cond::Vs => unordered,
Cond::Vc => not(unordered),
Cond::Hi => not(less_equal(a, b)),
Cond::Ls => less_equal(a, b),
Cond::Ge => less_equal(b, a),
Cond::Lt => not(less_equal(b, a)),
Cond::Gt => less(b, a),
Cond::Le => not(less(b, a)),
},
}
}
fn sum(a: &Expr, b: &Expr, bits: u32, cond: Cond, isa: Isa) -> Test {
let all = mask(bits);
let lowest = 1u64 << (bits - 1);
let s = Expr::bin(BinOp::Add, a.clone(), b.clone());
if let Expr::Const(c) = b {
let c = c & all;
if c == 0 {
let flags = match isa {
Isa::I386 | Isa::X86_64 => Flags::difference(a.clone(), Expr::constant(0)),
_ => Flags::logic(a.clone()),
};
return flags.test(cond, isa);
}
if c != lowest {
let negated = Expr::constant(c.wrapping_neg() & all);
let cond = match (isa, cond) {
(_, Cond::Mi) => return Test::Compare(Cond::Lt, s, Expr::constant(0)),
(_, Cond::Pl) => return Test::Compare(Cond::Ge, s, Expr::constant(0)),
(_, Cond::Vs | Cond::Vc) => cond,
(Isa::I386 | Isa::X86_64, Cond::Cs | Cond::Hi) => Cond::Cc,
(Isa::I386 | Isa::X86_64, Cond::Cc | Cond::Ls) => Cond::Cs,
(_, other) => other,
};
if !matches!(cond, Cond::Vs | Cond::Vc) {
return Test::Compare(cond, a.clone(), negated);
}
}
}
let zero = || Expr::constant(0);
let negated = match b {
Expr::Const(c) => Expr::constant(c.wrapping_neg() & all),
_ => Expr::bin(BinOp::Sub, zero(), b.clone()),
};
let is_zero = Test::Compare(Cond::Eq, a.clone(), negated);
let inverted = Expr::bin(BinOp::Xor, b.clone(), Expr::constant(all));
let carry = Test::Compare(Cond::Hi, a.clone(), inverted);
let carry_flag = match isa {
Isa::I386 | Isa::X86_64 => not(carry),
_ => carry,
};
let overflow_bits = Expr::bin(
BinOp::And,
Expr::bin(BinOp::Xor, a.clone(), s.clone()),
Expr::bin(BinOp::Xor, b.clone(), s.clone()),
);
let overflow = Test::Compare(Cond::Lt, overflow_bits.clone(), zero());
let not_negative = Test::Compare(
Cond::Ge,
Expr::bin(BinOp::Xor, s.clone(), overflow_bits),
zero(),
);
match cond {
Cond::Eq => is_zero,
Cond::Ne => not(is_zero),
Cond::Mi => Test::Compare(Cond::Lt, s, zero()),
Cond::Pl => Test::Compare(Cond::Ge, s, zero()),
Cond::Vs => overflow,
Cond::Vc => not(overflow),
Cond::Cs => carry_flag,
Cond::Cc => not(carry_flag),
Cond::Hi => both(carry_flag, not(is_zero)),
Cond::Ls => not(both(carry_flag, not(is_zero))),
Cond::Ge => not_negative,
Cond::Lt => not(not_negative),
Cond::Gt => both(not(is_zero), not_negative),
Cond::Le => not(both(not(is_zero), not_negative)),
}
}
pub(super) fn carry_out(a: &Expr, b: &Expr, carry: Expr, bits: u32) -> Expr {
let inverted = Expr::bin(BinOp::Xor, b.clone(), Expr::constant(mask(bits)));
let beyond = |cond| truth(Test::Compare(cond, a.clone(), inverted.clone()));
choose(carry, beyond(Cond::Cs), beyond(Cond::Hi))
}
pub(super) fn borrow_out(a: &Expr, b: &Expr, borrow: Expr) -> Expr {
let below = |cond| truth(Test::Compare(cond, a.clone(), b.clone()));
choose(borrow, below(Cond::Ls), below(Cond::Cc))
}
fn choose(bit: Expr, then: Expr, els: Expr) -> Expr {
Expr::Select {
cond: Cond::Ne,
lhs: Box::new(bit),
rhs: Box::new(Expr::constant(0)),
then: Box::new(then),
els: Box::new(els),
}
}
fn truth(test: Test) -> Expr {
test.select(Expr::constant(1), Expr::constant(0))
.expect("a decided test")
}
fn not(test: Test) -> Test {
match test {
Test::Always => Test::Never,
Test::Never => Test::Always,
Test::Compare(cond, lhs, rhs) => Test::Compare(cond.invert(), lhs, rhs),
Test::Unknown => Test::Unknown,
}
}
fn both(first: Test, second: Test) -> Test {
match (first, second) {
(Test::Never, _) | (_, Test::Never) => Test::Never,
(Test::Always, other) | (other, Test::Always) => other,
(Test::Unknown, _) | (_, Test::Unknown) => Test::Unknown,
(Test::Compare(c1, l1, r1), Test::Compare(c2, l2, r2)) => {
let inner = Expr::truth(c2, l2, r2);
let outer = Expr::Select {
cond: c1,
lhs: Box::new(l1),
rhs: Box::new(r1),
then: Box::new(inner),
els: Box::new(Expr::constant(0)),
};
Test::Compare(Cond::Ne, outer, Expr::constant(0))
}
}
}
impl Test {
pub fn select(self, then: Expr, els: Expr) -> Option<Expr> {
if let (Test::Compare(Cond::Ne, value, Expr::Const(0)), Expr::Const(1), Expr::Const(0)) =
(&self, &then, &els)
&& is_truth(value)
{
return Some(value.clone());
}
match self {
Test::Always => Some(then),
Test::Never => Some(els),
Test::Compare(cond, lhs, rhs) => Some(Expr::Select {
cond,
lhs: Box::new(lhs),
rhs: Box::new(rhs),
then: Box::new(then),
els: Box::new(els),
}),
Test::Unknown => None,
}
}
pub fn invert(self) -> Test {
not(self)
}
pub fn branch(self, cond: Cond, target: u64) -> Option<Stmt> {
match self {
Test::Always => Some(Stmt::Br {
target: Some(target),
at: None,
}),
Test::Never => None,
Test::Compare(cond, lhs, rhs) => Some(Stmt::Brif {
cond,
lhs,
rhs,
target,
at: None,
}),
Test::Unknown => Some(Stmt::BrifFlags {
cond,
target,
at: None,
}),
}
}
}
fn fixed_holds(cond: Cond, nzcv: u8) -> bool {
let (n, z, c, v) = (nzcv & 8 != 0, nzcv & 4 != 0, nzcv & 2 != 0, nzcv & 1 != 0);
match cond {
Cond::Eq => z,
Cond::Ne => !z,
Cond::Cs => c,
Cond::Cc => !c,
Cond::Mi => n,
Cond::Pl => !n,
Cond::Vs => v,
Cond::Vc => !v,
Cond::Hi => c && !z,
Cond::Ls => !(c && !z),
Cond::Ge => n == v,
Cond::Lt => n != v,
Cond::Gt => !z && n == v,
Cond::Le => !(!z && n == v),
}
}
fn is_truth(expr: &Expr) -> bool {
match expr {
Expr::Const(0 | 1) => true,
Expr::Select { then, els, .. } => is_truth(then) && is_truth(els),
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::{Flags, Test, fixed_holds};
use vlk_base::target::Isa;
use vlk_eval::eval::hash;
use vlk_ir::ir::semantics::{evaluate, holds, mask, operand_width, sign_extend};
use vlk_ir::ir::{Cond, Expr};
const CONDITIONS: [Cond; 14] = [
Cond::Eq,
Cond::Ne,
Cond::Cs,
Cond::Cc,
Cond::Mi,
Cond::Pl,
Cond::Vs,
Cond::Vc,
Cond::Hi,
Cond::Ls,
Cond::Ge,
Cond::Lt,
Cond::Gt,
Cond::Le,
];
fn reference(kind: u8, a: u64, b: u64, bits: u32, isa: Isa) -> u8 {
let all = mask(bits);
let (a, b) = (a & all, b & all);
let (result, carry, overflow) = match kind {
0 => {
let r = a.wrapping_sub(b) & all;
(r, a >= b, ((a ^ b) & (a ^ r)) >> (bits - 1) & 1 == 1)
}
1 => {
let r = a.wrapping_add(b) & all;
let carry = u128::from(a) + u128::from(b) > u128::from(all);
let carry = if matches!(isa, Isa::I386 | Isa::X86_64) {
!carry
} else {
carry
};
(r, carry, ((a ^ r) & (b ^ r)) >> (bits - 1) & 1 == 1)
}
_ => (a, matches!(isa, Isa::I386 | Isa::X86_64), false),
};
let negative = sign_extend(result, bits) < 0;
u8::from(negative) << 3
| u8::from(result == 0) << 2
| u8::from(carry) << 1
| u8::from(overflow)
}
fn decided(test: &Test, registers: &[u64; 2]) -> Option<bool> {
let value =
|expr: &Expr| evaluate(expr, &|index| registers.get(usize::from(index)).copied());
match test {
Test::Always => Some(true),
Test::Never => Some(false),
Test::Compare(cond, lhs, rhs) => Some(holds(
*cond,
value(lhs)?,
value(rhs)?,
operand_width(lhs, rhs),
)),
Test::Unknown => None,
}
}
#[test]
fn every_condition_decides_as_the_flags_do() {
const EDGES: [u64; 7] = [0, 1, 0x7f, 0x80, 0x7fff_ffff, 0x8000_0000, 1 << 63];
for isa in [Isa::AArch64, Isa::X86_64] {
for bits in [8u32, 32, 64] {
for seed in 0..600u64 {
let pick = |key: u64| match hash(seed, key) % 3 {
0 => EDGES[(hash(seed, key + 9) % 7) as usize].wrapping_sub(hash(seed, 5) % 2),
_ => hash(seed, key + 3),
} & mask(bits);
let (a, b) = (pick(1), pick(2));
let lhs = Expr::reg(0, bits as u8);
for rhs in [Expr::reg(1, bits as u8), Expr::constant(b)] {
for kind in 0..3u8 {
let flags = match kind {
0 => Flags::difference(lhs.clone(), rhs.clone()),
1 => Flags::sum(lhs.clone(), rhs.clone()),
_ => Flags::logic(lhs.clone()),
};
let nzcv = reference(kind, a, b, bits, isa);
for cond in CONDITIONS {
let test = flags.test(cond, isa);
assert_eq!(
decided(&test, &[a, b]),
Some(fixed_holds(cond, nzcv)),
"{isa:?} {bits}-bit kind {kind} {cond:?} a={a:#x} b={b:#x} rhs={rhs:?}: {test:?}"
);
}
}
}
}
}
}
}
#[test]
fn a_conditional_compare_tests_both_conditions() {
let earlier = Flags::difference(Expr::reg(0, 64), Expr::reg(1, 64));
let flags = Flags::difference(Expr::reg(2, 64), Expr::reg(3, 64))
.chosen_by(earlier.test(Cond::Eq, Isa::AArch64), 0)
.expect("known");
let test = flags.test(Cond::Eq, Isa::AArch64);
let value = |registers: [u64; 4]| match &test {
Test::Compare(cond, lhs, rhs) => {
let read = |expr: &Expr| {
evaluate(expr, &|index| registers.get(usize::from(index)).copied())
.expect("registers")
};
holds(*cond, read(lhs), read(rhs), operand_width(lhs, rhs))
}
other => panic!("{other:?}"),
};
assert!(value([1, 1, 2, 2]));
assert!(!value([1, 0, 2, 2]));
assert!(!value([1, 1, 2, 3]));
}
}