vlk-analysis 0.3.0

Lifting, SSA and simplification passes of the vlk decompiler
use super::{Emitter, Write, recover_operand};
use crate::lift::flags::{Flags, Test};
use vlk_base::target::Isa;
use vlk_ir::ir::{BinOp, Cond, EXCHANGE, Expr, FLAG_OPERANDS, Stmt};

/// A block whose pending flags compare `lhs` with `rhs`.
fn compared(lhs: Expr, rhs: Expr) -> Emitter {
    Emitter::pending(Some(Flags::difference(lhs, rhs)))
}

fn operands(out: &mut Emitter) -> Vec<Expr> {
    let flags = out.flags.pending.as_mut().expect("pending flags");
    flags
        .operands_mut()
        .into_iter()
        .map(|operand| operand.clone())
        .collect()
}

fn load(addr: Expr) -> Expr {
    Expr::Load {
        addr: Box::new(addr),
        width: 8,
        signed: false,
    }
}

#[test]
fn a_write_elsewhere_leaves_the_compare_alone() {
    let mut out = compared(Expr::reg(1, 64), Expr::Const(5));
    out.preserve(Write::Register(2, None));
    out.preserve(Write::Memory);
    assert!(out.stmts.is_empty());
    assert_eq!(operands(&mut out), [Expr::reg(1, 64), Expr::Const(5)]);
}

#[test]
fn an_operand_whose_register_is_overwritten_moves_to_its_own() {
    let mut out = compared(Expr::reg(1, 64), Expr::reg(2, 64));
    out.preserve(Write::Register(2, None));
    assert_eq!(
        operands(&mut out),
        [Expr::reg(1, 64), Expr::reg(FLAG_OPERANDS[1], 64)]
    );
    assert!(
        matches!(out.stmts.as_slice(), [Stmt::Assign { dest, value, .. }]
            if dest.index == FLAG_OPERANDS[1] && *value == Expr::reg(2, 64)),
        "{:?}",
        out.stmts
    );
}

#[test]
fn an_operand_equal_to_the_value_written_reads_the_register() {
    let sum = Expr::bin(BinOp::Add, Expr::reg(1, 64), Expr::reg(2, 64));
    let mut out = compared(sum.clone(), Expr::Const(0));
    out.preserve(Write::Register(1, Some(&sum)));
    assert!(out.stmts.is_empty());
    assert_eq!(operands(&mut out), [Expr::reg(1, 64), Expr::Const(0)]);
}

#[test]
fn the_operand_of_a_written_difference_is_recovered_from_it() {
    // `cmp r1, r2` and then `r1 = r1 - r2`: r1 before is the result plus r2.
    let mut out = compared(Expr::reg(1, 64), Expr::reg(2, 64));
    let difference = Expr::bin(BinOp::Sub, Expr::reg(1, 64), Expr::reg(2, 64));
    out.preserve(Write::Register(1, Some(&difference)));
    assert!(out.stmts.is_empty());
    assert_eq!(
        operands(&mut out),
        [
            Expr::bin(BinOp::Add, Expr::reg(1, 64), Expr::reg(2, 64)),
            Expr::reg(2, 64)
        ]
    );
}

#[test]
fn a_store_moves_an_operand_read_from_memory() {
    let mut out = compared(load(Expr::reg(3, 64)), Expr::Const(1));
    out.preserve(Write::Memory);
    assert_eq!(out.stmts.len(), 1);
    assert_eq!(
        operands(&mut out),
        [Expr::reg(FLAG_OPERANDS[0], 64), Expr::Const(1)]
    );
}

#[test]
fn recovering_an_operand_needs_the_other_to_survive() {
    let a = Expr::reg(1, 64);
    let recover = |value: &Expr| recover_operand(&a, value, 1, 64);
    assert_eq!(
        recover(&Expr::bin(BinOp::Add, a.clone(), Expr::Const(4))),
        Some(Expr::bin(BinOp::Sub, Expr::reg(1, 64), Expr::Const(4)))
    );
    assert_eq!(
        recover(&Expr::bin(BinOp::Sub, Expr::Const(0), a.clone())),
        Some(Expr::bin(BinOp::Sub, Expr::Const(0), Expr::reg(1, 64)))
    );
    // `r1 = r1 + r1` does not give r1 back, nor does a product.
    assert_eq!(recover(&Expr::bin(BinOp::Add, a.clone(), a.clone())), None);
    assert_eq!(
        recover(&Expr::bin(BinOp::Mul, a.clone(), Expr::Const(3))),
        None
    );
}

#[test]
fn a_held_value_is_read_from_its_own_register() {
    let mut out = compared(Expr::reg(EXCHANGE, 64), Expr::Const(0));
    let held = out.hold(Expr::reg(7, 64));
    assert_eq!(held, Expr::reg(EXCHANGE, 64));
    // The compare read that register: its operand moved first.
    assert!(
        matches!(out.stmts.as_slice(), [Stmt::Assign { dest: first, .. }, Stmt::Assign { dest, value, .. }]
            if first.index == FLAG_OPERANDS[0] && dest.index == EXCHANGE && *value == Expr::reg(7, 64)),
        "{:?}",
        out.stmts
    );
}

#[test]
fn a_condition_without_pending_flags_is_unknown() {
    let out = Emitter::default();
    assert!(matches!(out.test(Cond::Eq, Isa::X86_64), Test::Unknown));
    let out = compared(Expr::reg(1, 64), Expr::Const(5));
    assert!(!matches!(out.test(Cond::Eq, Isa::X86_64), Test::Unknown));
}

#[test]
fn setting_the_flags_records_that_the_lifter_did() {
    let mut out = Emitter::default();
    assert!(!out.flags.written);
    out.set_flags(None);
    assert!(out.flags.written && out.flags.pending.is_none());
}