vlk-analysis 0.3.0

Lifting, SSA and simplification passes of the vlk decompiler
use super::{DIRECTION, Lifter, stack_adjust};
use crate::lift::flags::Flags;
use iced_x86::Mnemonic;
use vlk_arch::arch::x86::{RAX, RDX, RSP};
use vlk_ir::ir::{BinOp, Cond, Expr, MachineInstruction, RegRef, Stmt, TEMP_BASE};

const OUTPUT: u16 = TEMP_BASE + 0x50;

pub(super) fn lift(lifter: &mut Lifter<'_>, mnemonic: Mnemonic) {
    let bytes = match mnemonic {
        Mnemonic::Pushf | Mnemonic::Popf => 2,
        Mnemonic::Pushfd | Mnemonic::Popfd => 4,
        _ => 8,
    };
    let (instruction, inputs, count) = match mnemonic {
        Mnemonic::Cpuid => (
            MachineInstruction::Cpuid,
            vec![Lifter::read_reg(RAX, 32), Lifter::read_reg(1, 32)],
            4,
        ),
        Mnemonic::Xgetbv => (MachineInstruction::Xgetbv, vec![Lifter::read_reg(1, 32)], 1),
        Mnemonic::Rdtsc => (MachineInstruction::Rdtsc, vec![], 1),
        Mnemonic::Rdtscp => (MachineInstruction::Rdtscp, vec![], 2),
        Mnemonic::Pushf | Mnemonic::Pushfd | Mnemonic::Pushfq => {
            let mut mask = 1 << 10;
            let mut bits = Expr::bin(BinOp::Shl, Expr::reg(DIRECTION, 64), Expr::constant(10));
            if let Some(flags) = &lifter.out.flags.pending {
                for (test, bit) in [
                    (lifter.test(Cond::Cc), 0),
                    (flags.parity(true), 2),
                    (flags.adjust(), 4),
                    (lifter.test(Cond::Eq), 6),
                    (lifter.test(Cond::Mi), 7),
                    (lifter.test(Cond::Vs), 11),
                ] {
                    if let Some(value) = test.select(Expr::constant(1 << bit), Expr::constant(0)) {
                        mask |= 1 << bit;
                        bits = Expr::bin(BinOp::Or, bits, value);
                    }
                }
            }
            (
                MachineInstruction::ReadFlags,
                vec![Expr::constant(mask), bits],
                1,
            )
        }
        Mnemonic::Popf | Mnemonic::Popfd | Mnemonic::Popfq => (
            MachineInstruction::WriteFlags,
            vec![Expr::Load {
                addr: Box::new(Lifter::read_reg(RSP, 64)),
                width: bytes,
                signed: false,
            }],
            1,
        ),
        _ => return,
    };
    for offset in 0..count {
        lifter
            .out
            .preserve(crate::lift::Write::Register(OUTPUT + offset, None));
    }
    lifter.out.stmts.push(Stmt::Machine {
        at: None,
        instruction,
        inputs,
        outputs: (0..count)
            .map(|offset| RegRef {
                index: OUTPUT + offset,
                width: 64,
            })
            .collect(),
    });
    let output = |offset| Expr::reg(OUTPUT + offset, 64);
    match instruction {
        MachineInstruction::Atomic { .. }
        | MachineInstruction::ArmSystem { .. }
        | MachineInstruction::RiscvCsr { .. }
        | MachineInstruction::MipsFloatControl { .. }
        | MachineInstruction::RiscvRounded { .. }
        | MachineInstruction::ArmBarrier { .. }
        | MachineInstruction::ArmLoadAcquire { .. }
        | MachineInstruction::ArmExclusive { .. }
        | MachineInstruction::ArmCacheZero => unreachable!(),
        MachineInstruction::Cpuid => {
            for (offset, register) in [RAX, 3, 1, RDX].into_iter().enumerate() {
                lifter.assign(register, 32, output(offset as u16));
            }
        }
        MachineInstruction::Xgetbv | MachineInstruction::Rdtsc | MachineInstruction::Rdtscp => {
            lifter.assign(RAX, 32, output(0));
            lifter.assign(
                RDX,
                32,
                Expr::bin(BinOp::Shr, output(0), Expr::constant(32)),
            );
            if instruction == MachineInstruction::Rdtscp {
                lifter.assign(1, 32, output(1));
            }
        }
        MachineInstruction::ReadFlags => {
            stack_adjust(lifter, -i64::from(bytes));
            lifter.out.preserve(crate::lift::Write::Memory);
            lifter.out.stmts.push(Stmt::Store {
                addr: Lifter::read_reg(RSP, 64),
                value: output(0),
                width: bytes,
                at: None,
            });
        }
        MachineInstruction::WriteFlags => {
            stack_adjust(lifter, i64::from(bytes));
            let overflow = Expr::bin(
                BinOp::And,
                Expr::bin(BinOp::Shr, output(0), Expr::constant(11)),
                Expr::constant(1),
            );
            lifter.set_flags(Flags::stored(output(0), Some(overflow)));
            lifter.assign(
                DIRECTION,
                64,
                Expr::bin(
                    BinOp::And,
                    Expr::bin(BinOp::Shr, output(0), Expr::constant(10)),
                    Expr::constant(1),
                ),
            );
        }
    }
}

#[cfg(test)]
mod tests;