mod atomic;
mod exclusive;
mod float;
mod integer;
mod lanes;
mod memory;
mod simd;
mod structures;
mod system;
mod vector;
pub(super) use atomic::recover_atomic_loops;
#[cfg(test)]
use float::half_immediate;
use float::{lift_float, lift_vector_float};
use integer::{
destination, extend_shift, immediate, lift_alu, lift_bic, lift_bitfield, lift_bitfield_insert,
lift_conditional_compare, lift_count, lift_cs_family, lift_csel, lift_extract, lift_inverted,
lift_long_multiply, lift_madd, lift_mov, lift_movk, lift_msub, lift_mul_high, lift_reverse,
lift_variable_shift, lift_with_carry, mask_bits, register_width,
};
use lanes::{
absolute_difference, by_halves, complement, doubling_mul_high, halves, immediate_lane, lane,
lane_shift, lane_value, lanewise, narrowed, replicate, replicated, reverse_lanes,
rounding_shift, shift_count, simd, source_halves, unzip, vector, widened, window, write_narrow,
};
use memory::{lift_load, lift_pair, lift_store};
use vector::lift_vector;
use yaxpeax_arch::{Decoder, ReaderBuilder};
use yaxpeax_arm::armv8::a64::{self, Instruction, Operand, SizeCode};
use super::calls::bare;
use super::flags::{Flags, Test};
use super::{Decoded, Machine, Write, flags, reverse_bytes, rotate};
use vlk_arch::arch::aarch64::LR;
use vlk_ir::ir::{BinOp, Cond, Expr, LaneOp, Operation, RegRef, Rounding, Stmt};
use vlk_types::types::{CallKind, Precision};
#[derive(Clone, Copy)]
pub(super) enum LaneParity {
Odd,
Even,
}
#[derive(Clone, Copy)]
pub(super) enum VectorHalf {
Upper,
Lower,
}
#[derive(Clone, Copy)]
pub(super) enum MemoryAccess {
Store,
Load,
}
#[derive(Clone, Copy)]
pub(super) enum FlagUpdate {
Set,
Preserve,
}
#[derive(Clone, Copy)]
pub(super) enum Signedness {
Signed,
Unsigned,
}
pub(super) struct Lifter<'a> {
out: super::Emitter,
image: &'a vlk_loader::loader::Image,
indirect: vlk_types::types::Convention,
}
impl<'a> Lifter<'a> {
pub(super) fn new(image: &'a vlk_loader::loader::Image, addr: u64) -> Self {
let indirect = if crate::lift::convention_of(image, addr).go_assembly() {
vlk_types::types::Convention::Aapcs64
} else {
vlk_types::types::Convention::of(image.isa, image.format, image.go)
};
Self {
out: super::Emitter::default(),
image,
indirect,
}
}
fn target_of(&self, operand: Operand, pc: u64) -> Option<u64> {
if let Some(target) = self.image.relocations.get(&pc) {
return Some(*target);
}
relative_target(operand, pc)
}
fn test(&self, cond: Cond) -> Test {
self.out.test(cond, vlk_base::target::Isa::AArch64)
}
fn hold(&mut self, value: Expr) -> Expr {
self.out.hold(value)
}
fn read_simd(&self, slot: u16, bits: u16) -> Expr {
let register = Expr::reg(slot, 64);
if bits >= 64 {
register
} else {
Expr::bin(BinOp::And, register, Expr::constant((1 << bits) - 1))
}
}
fn write_simd(&mut self, slot: u16, bits: u16, (low, high): (Expr, Expr)) {
let low = if bits < 64 {
Expr::bin(BinOp::And, low, Expr::constant((1 << bits) - 1))
} else {
low
};
self.assign(
RegRef {
index: slot,
width: 64,
},
low,
);
self.assign(
RegRef {
index: slot + 1,
width: 64,
},
high,
);
}
fn write_vector(&mut self, (slot, full, _): (u16, bool, u8), low: Expr, high: Expr) {
let high = if full {
let mut read = std::collections::HashSet::new();
vlk_ir::ir::visit::collect_reads(&high, &mut read);
if read.contains(&slot) {
self.hold(high)
} else {
high
}
} else {
Expr::constant(0)
};
self.write_simd(slot, 128, (low, high));
}
fn simd_store(&mut self, slot: u16, bits: u16, address: &Expr) {
self.out.preserve(Write::Memory);
if bits == 128 {
let high = Expr::bin(BinOp::Add, address.clone(), Expr::constant(8));
self.out.stmts.push(Stmt::Store {
addr: address.clone(),
value: Expr::reg(slot, 64),
width: 8,
at: None,
});
self.out.stmts.push(Stmt::Store {
addr: high,
value: Expr::reg(slot + 1, 64),
width: 8,
at: None,
});
} else {
self.out.stmts.push(Stmt::Store {
addr: address.clone(),
value: self.read_simd(slot, bits),
width: (bits / 8) as u8,
at: None,
});
}
}
fn test_code(&self, code: u8) -> Test {
match Cond::from_code(code) {
Some(cond) => self.test(cond),
None => Test::Always,
}
}
fn select(&mut self, dest: RegRef, test: Test, then: Expr, els: Expr) {
match test.select(then, els) {
Some(value) => self.assign(dest, value),
None => self.out.stmts.push(Stmt::unsupported(
"conditional select on flags not modelled here".to_owned(),
)),
}
}
fn read_reg(index: u16, width: u8) -> Expr {
if index == 31 && width == 32 {
return Expr::constant(0);
}
let slot = Expr::reg(index, 64);
if width == 32 {
Expr::bin(BinOp::And, slot, Expr::constant(0xffff_ffff))
} else {
slot
}
}
fn assign(&mut self, dest: RegRef, value: Expr) {
self.out.preserve(Write::Register(dest.index, Some(&value)));
let value = if dest.width == 32 {
Expr::bin(BinOp::And, value, Expr::constant(0xffff_ffff))
} else {
value
};
self.out.stmts.push(Stmt::Assign {
dest: RegRef {
index: dest.index,
width: 64,
},
value,
at: None,
});
}
fn operand(&self, operand: Operand, width: u8) -> Option<Expr> {
match operand {
Operand::Register(_, 31) => Some(Expr::constant(0)),
Operand::Register(_, index) => Some(Self::read_reg(index, width)),
Operand::RegisterOrSP(_, 31) => Some(Expr::reg(31, 64)),
Operand::RegisterOrSP(size, index) => {
if size == SizeCode::X {
Some(Expr::reg(index, 64))
} else {
Some(Self::read_reg(index, 32))
}
}
Operand::Immediate(value) => Some(Expr::constant(value as u64)),
Operand::Imm16(value) => Some(Expr::constant(value as u64)),
Operand::Imm64(value) => Some(Expr::constant(value)),
Operand::ImmShift(value, shift) => Some(Expr::constant((value as u64) << shift)),
Operand::RegShift(style, amount, size, index) => {
let source = if size == SizeCode::X { 64 } else { 32 };
let value = if index == 31 {
Expr::constant(0)
} else {
Self::read_reg(index, source)
};
let extended = extend_shift(style, amount as u64, value, size)?;
let signed = matches!(
style,
a64::ShiftStyle::SXTB
| a64::ShiftStyle::SXTH
| a64::ShiftStyle::SXTW
| a64::ShiftStyle::SXTX
);
Some(if width == 32 && signed {
Expr::bin(BinOp::And, extended, Expr::constant(0xffff_ffff))
} else {
extended
})
}
Operand::PCOffset(offset) => Some(Expr::constant(offset as u64)),
Operand::Nothing => None,
_ => {
self.out.incomplete.set(true);
None
}
}
}
}
fn sets_flags(opcode: a64::Opcode) -> bool {
use a64::Opcode as O;
matches!(
opcode,
O::ADDS
| O::SUBS
| O::ANDS
| O::BICS
| O::ADCS
| O::SBCS
| O::CCMP
| O::CCMN
| O::FCMP
| O::FCMPE
| O::FCCMP
| O::FCCMPE
| O::MSR
| O::RMIF
| O::SETF8
| O::SETF16
| O::SUBPS
)
}
fn possibly_written_registers(instruction: &Instruction) -> Vec<u16> {
let vector = |number: u16| {
let slot = vlk_arch::arch::aarch64::V0 + 2 * number;
[slot, slot + 1]
};
let mut out = Vec::new();
for operand in instruction.operands {
match operand {
Operand::Register(_, 31) => {}
Operand::Register(_, index) | Operand::RegisterOrSP(_, index) => out.push(index),
Operand::SIMDRegister(_, number)
| Operand::SIMDRegisterElements(_, number, _)
| Operand::SIMDRegisterElementsLane(_, number, ..)
| Operand::SIMDRegisterElementsMultipleLane(_, number, ..) => {
out.extend(vector(number))
}
Operand::SIMDRegisterGroup(_, number, _, count)
| Operand::SIMDRegisterGroupLane(number, _, count, _) => {
for offset in 0..u16::from(count) {
out.extend(vector((number + offset) % 32));
}
}
Operand::RegPreIndex(base, _, true)
| Operand::RegPostIndex(base, _)
| Operand::RegPostIndexReg(base, _) => out.push(base),
_ => {}
}
}
out.sort_unstable();
out.dedup();
out
}
fn link(lifter: &mut Lifter, pc: u64) {
let dest = RegRef {
index: LR,
width: 64,
};
lifter.assign(dest, Expr::constant(pc.wrapping_add(4)));
}
fn relative_target(operand: Operand, pc: u64) -> Option<u64> {
match operand {
Operand::PCOffset(offset) => Some(pc.wrapping_add(offset as u64)),
_ => None,
}
}
fn lift_conditional_branch(lifter: &mut Lifter, operands: [Operand; 4], code: u8, pc: u64) -> bool {
let Some(target) = lifter.target_of(operands[0], pc) else {
lifter
.out
.stmts
.push(Stmt::unsupported("branch target".to_owned()));
return true;
};
let test = lifter.test_code(code);
let cond = Cond::from_code(code).unwrap_or(Cond::Eq);
lifter.out.stmts.extend(test.branch(cond, target));
true
}
fn lift_compare_branch(
lifter: &mut Lifter,
operands: [Operand; 4],
pc: u64,
non_zero: bool,
) -> bool {
let width = destination(operands[0]).map_or(64, |register| register.width);
let Some(value) = lifter.operand(operands[0], width) else {
return true;
};
let Some(target) = relative_target(operands[1], pc) else {
return true;
};
lifter.out.stmts.push(Stmt::Brif {
cond: if non_zero { Cond::Ne } else { Cond::Eq },
lhs: value,
rhs: Expr::constant(0),
target,
at: None,
});
true
}
fn lift_test_branch(lifter: &mut Lifter, operands: [Operand; 4], pc: u64, non_zero: bool) -> bool {
let Some(value) = lifter.operand(operands[0], 64) else {
return true;
};
let Some(bit) = immediate(operands[1]) else {
return true;
};
let Some(target) = relative_target(operands[2], pc) else {
return true;
};
let masked = Expr::bin(BinOp::And, value, Expr::constant(1u64 << (bit & 63)));
lifter.out.stmts.push(Stmt::Brif {
cond: if non_zero { Cond::Ne } else { Cond::Eq },
lhs: masked,
rhs: Expr::constant(0),
target,
at: None,
});
true
}
fn lift_instruction(lifter: &mut Lifter, instruction: &Instruction, pc: u64) -> bool {
if atomic::lift(lifter, instruction)
|| exclusive::lift(lifter, instruction)
|| structures::lift(lifter, instruction)
|| simd::lift(lifter, instruction)
|| system::lift(lifter, instruction)
{
return false;
}
use a64::Opcode as O;
let operands = instruction.operands;
if lift_float(lifter, instruction).is_some()
|| lift_vector(lifter, instruction).is_some()
|| lift_vector_float(lifter, instruction).is_some()
{
return false;
}
let vector_operand = instruction.operands.iter().any(|operand| {
matches!(
operand,
Operand::SIMDRegister(..)
| Operand::SIMDRegisterElements(..)
| Operand::SIMDRegisterElementsLane(..)
| Operand::SIMDRegisterElementsMultipleLane(..)
| Operand::SIMDRegisterGroup(..)
| Operand::SIMDRegisterGroupLane(..)
)
});
if vector_operand
&& !matches!(
instruction.opcode,
O::LDR | O::LDUR | O::STR | O::STUR | O::LDP | O::STP | O::LDNP | O::STNP
)
{
lifter.out.stmts.push(Stmt::unsupported(format!(
"{:?} at 0x{pc:x}",
instruction.opcode
)));
return false;
}
match instruction.opcode {
O::ADD => lift_alu(lifter, operands, BinOp::Add, false),
O::ADDS => lift_alu(lifter, operands, BinOp::Add, true),
O::SUB => lift_alu(lifter, operands, BinOp::Sub, false),
O::SUBS => lift_alu(lifter, operands, BinOp::Sub, true),
O::AND => lift_alu(lifter, operands, BinOp::And, false),
O::ANDS => lift_alu(lifter, operands, BinOp::And, true),
O::ORR => lift_alu(lifter, operands, BinOp::Or, false),
O::EOR => lift_alu(lifter, operands, BinOp::Xor, false),
O::BIC => lift_bic(lifter, operands, false),
O::BICS => lift_bic(lifter, operands, true),
O::MUL => lift_madd(lifter, operands, false),
O::UMULH => lift_mul_high(lifter, operands, false),
O::SMULH => lift_mul_high(lifter, operands, true),
O::MADD => lift_madd(lifter, operands, true),
O::CSEL => lift_csel(lifter, operands),
O::CSINC | O::CSINV | O::CSNEG => lift_cs_family(lifter, operands, instruction.opcode),
O::MSUB => lift_msub(lifter, operands),
O::UMADDL => {
lift_long_multiply(
lifter,
operands,
Signedness::Unsigned,
false,
)
}
O::SMADDL => lift_long_multiply(
lifter,
operands,
Signedness::Signed,
false,
),
O::UMSUBL => {
lift_long_multiply(
lifter,
operands,
Signedness::Unsigned,
true,
)
}
O::SMSUBL => lift_long_multiply(
lifter,
operands,
Signedness::Signed,
true,
),
O::CCMP => lift_conditional_compare(lifter, operands, false),
O::CCMN => lift_conditional_compare(lifter, operands, true),
O::ADC => lift_with_carry(
lifter,
operands,
false,
FlagUpdate::Preserve,
),
O::ADCS => lift_with_carry(lifter, operands, false, FlagUpdate::Set),
O::SBC => lift_with_carry(
lifter,
operands,
true,
FlagUpdate::Preserve,
),
O::SBCS => lift_with_carry(lifter, operands, true, FlagUpdate::Set),
O::BFM => lift_bitfield_insert(lifter, operands),
O::MOVZ => lift_mov(lifter, operands, false),
O::MOVN => lift_mov(lifter, operands, true),
O::MOVK => lift_movk(lifter, operands),
O::UBFM => lift_bitfield(lifter, operands, false),
O::SBFM => lift_bitfield(lifter, operands, true),
O::LSLV => lift_variable_shift(lifter, operands, a64::ShiftStyle::LSL),
O::LSRV => lift_variable_shift(lifter, operands, a64::ShiftStyle::LSR),
O::ASRV => lift_variable_shift(lifter, operands, a64::ShiftStyle::ASR),
O::RORV => lift_variable_shift(lifter, operands, a64::ShiftStyle::ROR),
O::EXTR => lift_extract(lifter, operands),
O::ORN => lift_inverted(lifter, operands, BinOp::Or),
O::EON => lift_inverted(lifter, operands, BinOp::Xor),
O::REV => lift_reverse(lifter, operands, None),
O::CLZ | O::CLS | O::RBIT => lift_count(lifter, operands, instruction.opcode),
O::REV16 => lift_reverse(lifter, operands, Some(2)),
O::REV32 => lift_reverse(lifter, operands, Some(4)),
O::LDR | O::LDRAA | O::LDRAB => lift_load(lifter, operands, O::LDR, pc),
O::LDRB => lift_load(lifter, operands, O::LDRB, pc),
O::LDRH => lift_load(lifter, operands, O::LDRH, pc),
O::LDRSB => lift_load(lifter, operands, O::LDRSB, pc),
O::LDRSH => lift_load(lifter, operands, O::LDRSH, pc),
O::LDRSW => lift_load(lifter, operands, O::LDRSW, pc),
O::STR => lift_store(lifter, operands, O::STR),
O::STRB => lift_store(lifter, operands, O::STRB),
O::STRH => lift_store(lifter, operands, O::STRH),
O::LDUR => lift_load(lifter, operands, O::LDR, pc),
O::LDURB => lift_load(lifter, operands, O::LDRB, pc),
O::LDURH => lift_load(lifter, operands, O::LDRH, pc),
O::LDURSB => lift_load(lifter, operands, O::LDRSB, pc),
O::LDURSH => lift_load(lifter, operands, O::LDRSH, pc),
O::LDURSW => lift_load(lifter, operands, O::LDRSW, pc),
O::STUR => lift_store(lifter, operands, O::STR),
O::STURB => lift_store(lifter, operands, O::STRB),
O::STURH => lift_store(lifter, operands, O::STRH),
O::SDIV => lift_alu(lifter, operands, BinOp::DivS, false),
O::UDIV => lift_alu(lifter, operands, BinOp::DivU, false),
O::LDP | O::LDNP => lift_pair(
lifter,
operands,
MemoryAccess::Load,
false,
),
O::LDPSW => lift_pair(lifter, operands, MemoryAccess::Load, true),
O::STP | O::STNP => lift_pair(
lifter,
operands,
MemoryAccess::Store,
false,
),
O::B => {
let target = lifter.target_of(operands[0], pc);
if target == Some(pc) && lifter.image.relocatable {
lifter.out.stmts.extend([
bare(CallKind::Tail, lifter.image, None),
Stmt::Ret {
value: Expr::reg(vlk_arch::arch::RETURN, 64),
at: None,
},
]);
return true;
}
lifter.out.stmts.push(Stmt::Br { target, at: None });
return true;
}
O::Bcc(code) => return lift_conditional_branch(lifter, operands, code, pc),
O::CBZ => return lift_compare_branch(lifter, operands, pc, false),
O::CBNZ => return lift_compare_branch(lifter, operands, pc, true),
O::TBZ => return lift_test_branch(lifter, operands, pc, false),
O::TBNZ => return lift_test_branch(lifter, operands, pc, true),
O::BR | O::BRAA | O::BRAAZ | O::BRAB | O::BRABZ => {
if let Some(target) = lifter.operand(operands[0], 64) {
lifter.out.stmts.push(Stmt::BrIndirect { target, at: None });
}
return true;
}
O::BLR | O::BLRAA | O::BLRAAZ | O::BLRAB | O::BLRABZ => {
lifter.out.flags.pending = None;
if let Some(through) = lifter.operand(operands[0], 64) {
let through = if matches!(through, Expr::Reg { index: LR, .. }) {
lifter.hold(through)
} else {
through
};
link(lifter, pc);
lifter.out.stmts.push(super::call_through_as(
CallKind::Invoke,
lifter.image,
through,
lifter.indirect,
));
}
}
O::BL => {
let target = lifter.target_of(operands[0], pc);
if let Some(body) = target.and_then(|target| outlined_body(lifter.image, target)) {
link(lifter, pc);
for step in &body {
Machine::step(lifter, &step.instruction, step.pc);
}
return false;
}
lifter.out.flags.pending = None;
link(lifter, pc);
lifter
.out
.stmts
.push(bare(CallKind::Invoke, lifter.image, target));
}
O::BRK | O::UDF => {
lifter.out.stmts.push(Stmt::Trap { at: None });
return true;
}
O::RET | O::RETAA | O::RETAB => {
let value = Lifter::read_reg(0, 64);
lifter.out.stmts.push(Stmt::Ret { value, at: None });
return true;
}
O::ADRP => {
if let (Some(dest), Operand::PCOffset(offset)) = (destination(operands[0]), operands[1])
{
let page = pc & !0xfff;
let value = Expr::constant(page.wrapping_add(offset as u64));
lifter.assign(dest, value);
}
}
O::ADR => {
if let (Some(dest), Operand::PCOffset(offset)) = (destination(operands[0]), operands[1])
{
let value = Expr::constant(pc.wrapping_add(offset as u64));
lifter.assign(dest, value);
}
}
O::PACIA
| O::PACIB
| O::PACDA
| O::PACDB
| O::AUTIA
| O::AUTIB
| O::AUTDA
| O::AUTDB
| O::PACIZA
| O::PACIZB
| O::PACDZA
| O::PACDZB
| O::AUTIZA
| O::AUTIZB
| O::AUTDZA
| O::AUTDZB
| O::PACIASP
| O::PACIAZ
| O::PACIA1716
| O::PACIBSP
| O::PACIBZ
| O::PACIB1716
| O::AUTIASP
| O::AUTIAZ
| O::AUTIA1716
| O::AUTIBSP
| O::AUTIBZ
| O::AUTIB1716
| O::XPACI
| O::XPACD
| O::XPACLRI
| O::HINT => {}
O::CRC32B
| O::CRC32H
| O::CRC32W
| O::CRC32X
| O::CRC32CB
| O::CRC32CH
| O::CRC32CW
| O::CRC32CX => {
let (castagnoli, bits) = match instruction.opcode {
O::CRC32B => (false, 8),
O::CRC32H => (false, 16),
O::CRC32W => (false, 32),
O::CRC32X => (false, 64),
O::CRC32CB => (true, 8),
O::CRC32CH => (true, 16),
O::CRC32CW => (true, 32),
_ => (true, 64),
};
if let (Some(dest), Some(crc), Some(data)) = (
destination(operands[0]),
lifter.operand(operands[1], 32),
lifter.operand(operands[2], bits.max(32)),
) {
let value = Expr::apply(Operation::Crc32 { castagnoli, bits }, vec![crc, data]);
lifter.assign(dest, value);
}
}
O::SVC
if matches!(
operands[0],
Operand::Immediate(0) | Operand::Imm16(0) | Operand::Imm64(0)
) =>
{
lifter.out.stmts.push(Stmt::Call {
kind: CallKind::Syscall,
target: None,
through: None,
convention: vlk_types::types::Convention::SyscallArm,
args: None,
floats_named: true,
abi: None,
clobbers: None,
outputs: vlk_ir::ir::fallback_call_outputs(
vlk_types::types::Convention::SyscallArm,
None,
),
via: None,
at: None,
})
}
other => {
lifter
.out
.stmts
.push(Stmt::unsupported(format!("{other:?} at 0x{pc:x}")));
}
}
false
}
const OUTLINED_LIMIT: usize = 32;
fn outlined_body(
image: &vlk_loader::loader::Image,
address: u64,
) -> Option<Vec<Decoded<Instruction>>> {
use a64::Opcode as O;
let Some(function) = image.functions.get(&address) else {
return None;
};
let named = function.name.iter().chain(&function.aliases).any(|name| {
name.trim_start_matches('_')
.starts_with("OUTLINED_FUNCTION_")
});
let Some(size) = function.size else {
return None;
};
if !named || function.size_estimated || size == 0 || size > (OUTLINED_LIMIT as u64 + 1) * 4 {
return None;
}
let Some(bytes) = image.read_vaddr(address, size) else {
return None;
};
if !bytes.ends_with(&0xd65f_03c0u32.to_le_bytes()) {
return None;
}
let (mut body, stop) = decode(bytes, address);
if stop.is_some() || body.len() as u64 * 4 != size {
return None;
}
body.pop();
let straight = body.iter().all(|step| {
!matches!(
step.instruction.opcode,
O::B | O::Bcc(_)
| O::BL
| O::BLR
| O::BR
| O::CBZ
| O::CBNZ
| O::TBZ
| O::TBNZ
| O::RET
| O::RETAA
| O::RETAB
| O::BRK
| O::UDF
| O::SVC
)
});
straight.then_some(body)
}
fn branch_target(instruction: &Instruction, pc: u64) -> Option<u64> {
use a64::Opcode as O;
match instruction.opcode {
O::B | O::Bcc(_) | O::BL => relative_target(instruction.operands[0], pc),
O::CBZ | O::CBNZ => relative_target(instruction.operands[1], pc),
O::TBZ | O::TBNZ => relative_target(instruction.operands[2], pc),
_ => None,
}
}
pub fn written_register(word: &[u8], instruction: &mut Instruction) {
if instruction.opcode == a64::Opcode::MSR
&& let Operand::Register(_, register) = &mut instruction.operands[1]
&& let Some(first) = word.first()
{
*register = u16::from(first & 31);
}
}
const GO_UNDEFINED: u32 = 0xbea7_1700;
pub(super) fn decode(bytes: &[u8], addr: u64) -> (Vec<Decoded<Instruction>>, Option<String>) {
let mut reader = ReaderBuilder::<u64, u8>::read_at(bytes, 0);
let decoder = a64::InstDecoder::default();
let count = bytes.len() / 4;
let mut decoded = Vec::new();
let mut pc = addr;
for _ in 0..count {
let Some(next) = pc.checked_add(4) else {
return (
decoded,
Some(format!("instruction address overflows at 0x{pc:x}")),
);
};
match decoder.decode(&mut reader) {
Ok(mut instruction) => {
if let Some(word) = bytes.get((pc - addr) as usize..(next - addr) as usize) {
written_register(word, &mut instruction);
}
decoded.push(Decoded {
pc,
next,
instruction,
});
}
Err(_)
if bytes.get((pc - addr) as usize..(next - addr) as usize)
== Some(&GO_UNDEFINED.to_le_bytes()[..])
&& let Ok(instruction) = decoder
.decode(&mut ReaderBuilder::<u64, u8>::read_at(&[0u8; 4][..], 0)) =>
{
decoded.push(Decoded {
pc,
next,
instruction,
});
}
Err(error) => {
return (
decoded,
Some(format!("undecodable instruction at 0x{pc:x}: {error}")),
);
}
}
pc = next;
}
(decoded, None)
}
impl Machine for Lifter<'_> {
type Instruction = Instruction;
fn out(&mut self) -> &mut super::Emitter {
&mut self.out
}
fn branch_target(&self, instruction: &Instruction, pc: u64) -> Option<u64> {
if matches!(instruction.opcode, a64::Opcode::B | a64::Opcode::BL)
&& let Some(target) = self.image.relocations.get(&pc)
{
return Some(*target);
}
branch_target(instruction, pc)
}
fn step(&mut self, instruction: &Instruction, pc: u64) -> bool {
lift_instruction(self, instruction, pc)
}
fn name(&self, instruction: &Instruction) -> String {
format!("{:?}", instruction.opcode)
}
fn written(&mut self, instruction: &Instruction) -> Option<Vec<u16>> {
Some(possibly_written_registers(instruction))
}
fn sets_flags(&self, instruction: &Instruction) -> bool {
sets_flags(instruction.opcode)
}
fn reads_flags(&self, instruction: &Instruction) -> bool {
matches!(
instruction.opcode,
a64::Opcode::CSEL | a64::Opcode::CSINC | a64::Opcode::CSINV | a64::Opcode::CSNEG
)
}
}
#[cfg(test)]
mod tests;