vlk-analysis 0.3.0

Lifting, SSA and simplification passes of the vlk decompiler
use super::*;
use vlk_ir::ir::{RegRef, VARIABLE_BASE};
use vlk_types::types::CType;

const V1: u16 = VARIABLE_BASE + 1;
const V2: u16 = VARIABLE_BASE + 2;

fn function(stmts: Vec<Stmt>) -> FunctionIr {
    let mut ir = FunctionIr::single_block(vlk_base::target::Isa::X86_64, stmts);
    ir.ret_type = CType::I32;
    ir
}

fn assign(index: u16, value: Expr) -> Stmt {
    Stmt::Assign {
        dest: RegRef { index, width: 64 },
        value,
        at: None,
    }
}

fn narrow(ir: &FunctionIr) -> HashMap<u16, Narrow> {
    let ints = [V1, V2].into_iter().collect();
    narrow_variables(ir, &ints, &HashSet::new(), &HashSet::new())
}

#[test]
fn a_counter_masked_to_32_bits_takes_32() {
    // v1 = (v1 + 1) & 0xffffffff, stored as 32 bits and returned as an int.
    let ir = function(vec![
        assign(V1, Expr::Const(0)),
        assign(
            V1,
            Expr::bin(
                BinOp::And,
                Expr::bin(BinOp::Add, Expr::reg(V1, 64), Expr::Const(1)),
                Expr::Const(0xffff_ffff),
            ),
        ),
        Stmt::Store {
            addr: Expr::reg(7, 64),
            value: Expr::reg(V1, 64),
            width: 4,
            at: None,
        },
        Stmt::Ret {
            value: Expr::reg(V1, 64),
            at: None,
        },
    ]);
    assert_eq!(
        narrow(&ir).get(&V1),
        Some(&Narrow::Read {
            bits: 32,
            extended: false
        })
    );
}

#[test]
fn a_32_bit_value_read_whole_takes_32_bits_without_a_sign() {
    // v1 holds a zero-extended 32-bit load and is compared at 64 bits.
    let ir = function(vec![
        assign(
            V1,
            Expr::Load {
                addr: Box::new(Expr::reg(7, 64)),
                width: 4,
                signed: false,
            },
        ),
        Stmt::Brif {
            cond: vlk_ir::ir::Cond::Eq,
            lhs: Expr::reg(V1, 64),
            rhs: Expr::reg(6, 64),
            target: 0x1000,
            at: None,
        },
    ]);
    assert_eq!(narrow(&ir).get(&V1), Some(&Narrow::Held { bits: 32 }));
}

#[test]
fn a_wide_value_read_whole_stays_wide() {
    let ir = function(vec![
        assign(V1, Expr::reg(6, 64)),
        Stmt::Brif {
            cond: vlk_ir::ir::Cond::Eq,
            lhs: Expr::reg(V1, 64),
            rhs: Expr::Const(0),
            target: 0x1000,
            at: None,
        },
    ]);
    assert_eq!(narrow(&ir).get(&V1), None);
}

#[test]
fn a_copy_read_whole_keeps_its_source_wide() {
    // v2 = v1 is compared whole, so v1, which v2 is assigned from at
    // 64 bits, is read whole as well.
    let ir = function(vec![
        assign(V1, Expr::reg(6, 64)),
        assign(V2, Expr::reg(V1, 64)),
        Stmt::Store {
            addr: Expr::reg(7, 64),
            value: Expr::reg(V1, 64),
            width: 4,
            at: None,
        },
        Stmt::Brif {
            cond: vlk_ir::ir::Cond::Eq,
            lhs: Expr::reg(V2, 64),
            rhs: Expr::Const(0),
            target: 0x1000,
            at: None,
        },
    ]);
    let narrow = narrow(&ir);
    assert_eq!(narrow.get(&V1), None);
    assert_eq!(narrow.get(&V2), None);
}

#[test]
fn an_index_added_to_a_pointer_is_read_whole() {
    let pointers: HashSet<u16> = [7].into_iter().collect();
    let sum = Expr::bin(
        BinOp::And,
        Expr::bin(BinOp::Add, Expr::reg(7, 64), Expr::reg(V1, 64)),
        Expr::Const(0xffff_ffff),
    );
    let ir = function(vec![Stmt::Ret {
        value: sum,
        at: None,
    }]);
    let readers = read_bits(
        &ir,
        &HashMap::new(),
        &HashSet::new(),
        &pointers,
        &HashMap::new(),
    );
    assert_eq!(readers.bits.get(&V1), Some(&64));
}

#[test]
fn a_value_only_stored_as_a_byte_takes_8_bits() {
    let ir = function(vec![
        assign(V1, Expr::reg(6, 64)),
        Stmt::Store {
            addr: Expr::reg(7, 64),
            value: Expr::reg(V1, 64),
            width: 1,
            at: None,
        },
    ]);
    assert_eq!(
        narrow(&ir).get(&V1),
        Some(&Narrow::Read {
            bits: 8,
            extended: false
        })
    );
}

#[test]
fn a_value_extended_with_zeros_by_its_reader_reads_as_unsigned() {
    for mask in [0xffff_ffff, 0x8000_0000] {
        let ir = function(vec![
            assign(V1, Expr::reg(6, 64)),
            Stmt::Store {
                addr: Expr::reg(7, 64),
                value: Expr::bin(
                    BinOp::Or,
                    Expr::bin(BinOp::And, Expr::reg(V1, 64), Expr::Const(mask)),
                    Expr::reg(2, 64),
                ),
                width: 8,
                at: None,
            },
        ]);
        assert_eq!(
            narrow(&ir).get(&V1),
            Some(&Narrow::Read {
                bits: 32,
                extended: true
            })
        );
    }
}

#[test]
fn a_32_bit_value_shifted_into_the_high_half_stays_wide() {
    // v1 = load32; stored as v1 << 32, which C would compute at 32 bits
    // for a 32-bit variable, and saved no cast anywhere.
    let ir = function(vec![
        assign(
            V1,
            Expr::Load {
                addr: Box::new(Expr::reg(7, 64)),
                width: 4,
                signed: false,
            },
        ),
        Stmt::Store {
            addr: Expr::reg(7, 64),
            value: Expr::bin(BinOp::Shl, Expr::reg(V1, 64), Expr::Const(32)),
            width: 8,
            at: None,
        },
    ]);
    assert_eq!(narrow(&ir).get(&V1), None);
}

#[test]
fn a_shift_of_a_value_read_at_32_bits_costs_nothing_more() {
    // v1 << 2 reads v1 at 32 bits, which a 64-bit variable casts too.
    let ir = function(vec![
        assign(
            V1,
            Expr::Load {
                addr: Box::new(Expr::reg(7, 64)),
                width: 4,
                signed: false,
            },
        ),
        Stmt::Store {
            addr: Expr::reg(7, 64),
            value: Expr::bin(BinOp::Shl, Expr::reg(V1, 32), Expr::Const(2)),
            width: 8,
            at: None,
        },
    ]);
    assert_eq!(
        narrow(&ir).get(&V1),
        Some(&Narrow::Read {
            bits: 32,
            extended: false
        })
    );
}