vlk-analysis 0.3.0

Lifting, SSA and simplification passes of the vlk decompiler
use super::finish_instruction;
use crate::lift::flags::Flags;
use crate::lift::machine::{Emitter, Machine};
use vlk_ir::ir::{Expr, Stmt};

/// A lifter whose every instruction is the same: what it answers of it is
/// set by the test.
struct Fixed {
    out: Emitter,
    sets_flags: bool,
    reads_flags: bool,
    written: Option<Vec<u16>>,
}

impl Fixed {
    fn pending() -> Self {
        Self {
            out: Emitter::pending(Some(Flags::difference(Expr::reg(1, 64), Expr::Const(2)))),
            sets_flags: false,
            reads_flags: false,
            written: None,
        }
    }
}

impl Machine for Fixed {
    type Instruction = ();

    fn out(&mut self) -> &mut Emitter {
        &mut self.out
    }

    fn branch_target(&self, (): &(), _: u64) -> Option<u64> {
        None
    }

    fn step(&mut self, (): &(), _: u64) -> bool {
        false
    }

    fn name(&self, (): &()) -> String {
        "Frob".to_owned()
    }

    fn written(&mut self, (): &()) -> Option<Vec<u16>> {
        self.written.clone()
    }

    fn sets_flags(&self, (): &()) -> bool {
        self.sets_flags
    }

    fn reads_flags(&self, (): &()) -> bool {
        self.reads_flags
    }
}

#[test]
fn an_operand_the_lifter_could_not_express_leaves_a_note() {
    let mut machine = Fixed::pending();
    machine.written = Some(vec![3, 4]);
    machine.out.incomplete.set(true);
    finish_instruction(&mut machine, &(), 0, 0x1234);
    let [Stmt::Unsupported { text, writes, .. }] = machine.out.stmts.as_slice() else {
        panic!("{:?}", machine.out.stmts);
    };
    assert_eq!(text, "Frob operands at 0x1234");
    // The note names the registers the instruction may write.
    assert_eq!(writes.as_deref(), Some([3, 4].as_slice()));
    // An instruction left unsupported may have changed the flags.
    assert!(machine.out.flags.touched);
}

#[test]
fn an_unsupported_statement_keeps_the_registers_it_names() {
    let mut machine = Fixed::pending();
    machine.written = Some(vec![3]);
    machine.out.stmts.push(Stmt::Unsupported {
        text: "frob".to_owned(),
        writes: Some(vec![9]),
        at: None,
    });
    finish_instruction(&mut machine, &(), 0, 0);
    assert!(
        matches!(machine.out.stmts.as_slice(), [Stmt::Unsupported { writes: Some(writes), .. }]
            if writes == &[9]),
        "{:?}",
        machine.out.stmts
    );
    // A lifter that names no registers leaves the statement open.
    let mut machine = Fixed::pending();
    machine.out.stmts.push(Stmt::unsupported("frob"));
    finish_instruction(&mut machine, &(), 0, 0);
    assert!(matches!(
        machine.out.stmts.as_slice(),
        [Stmt::Unsupported { writes: None, .. }]
    ));
}

#[test]
fn flags_set_in_a_way_not_modelled_are_unknown() {
    // The lifter said how the instruction leaves the flags.
    let mut machine = Fixed::pending();
    machine.sets_flags = true;
    machine
        .out
        .set_flags(Some(Flags::difference(Expr::reg(5, 64), Expr::Const(1))));
    finish_instruction(&mut machine, &(), 0, 0);
    assert!(machine.out.flags.pending.is_some());
    assert!(machine.out.flags.touched);
    // It did not: the flags the block had are gone.
    let mut machine = Fixed::pending();
    machine.sets_flags = true;
    finish_instruction(&mut machine, &(), 0, 0);
    assert!(machine.out.flags.pending.is_none());
    assert!(machine.out.flags.touched);
    // An instruction that leaves the flags alone leaves them pending.
    let mut machine = Fixed::pending();
    finish_instruction(&mut machine, &(), 0, 0);
    assert!(machine.out.flags.pending.is_some());
    assert!(!machine.out.flags.touched);
}

#[test]
fn an_unsupported_instruction_that_only_reads_the_flags_leaves_them() {
    for (reads_flags, touched) in [(true, false), (false, true)] {
        let mut machine = Fixed::pending();
        machine.reads_flags = reads_flags;
        machine.out.stmts.push(Stmt::unsupported("frob"));
        finish_instruction(&mut machine, &(), 0, 0);
        assert_eq!(machine.out.flags.touched, touched);
        assert!(machine.out.flags.pending.is_some());
    }
}