use super::{Insn, Operand, Reg, Width};
pub(crate) fn decode(hw1: u16, _hw2: u16, addr: u32) -> Option<Insn> {
if hw1 >> 10 != 0b010001 {
return None;
}
let op = (hw1 >> 8) & 0b11;
let hi = ((hw1 >> 7) & 1) as u8;
let rm = Reg(((hw1 >> 3) & 0xF) as u8);
let low3 = (hw1 & 0b111) as u8;
let rdn = Reg((hi << 3) | low3);
Some(match op {
0b00 => {
if rm.num() == 13 {
narrow(
"add",
"T1",
addr,
&[Operand::Reg(rdn), Operand::Reg(Reg::SP), Operand::Reg(rdn)],
)
} else {
narrow("add", "T2", addr, &[Operand::Reg(rdn), Operand::Reg(rm)])
}
}
0b01 => {
if hi == 0 && rm.is_low() {
return None;
}
narrow("cmp", "T2", addr, &[Operand::Reg(rdn), Operand::Reg(rm)])
}
0b10 => narrow("mov", "T1", addr, &[Operand::Reg(rdn), Operand::Reg(rm)]),
_ => {
if low3 != 0 {
return None;
}
let mnemonic = if hi == 0 { "bx" } else { "blx" };
narrow(mnemonic, "T1", addr, &[Operand::Reg(rm)])
}
})
}
pub(crate) fn encode(insn: &Insn) -> Option<u16> {
if insn.width != Width::Narrow || insn.sets_flags {
return None;
}
let (a, b, c) = (
insn.operands.get(0),
insn.operands.get(1),
insn.operands.get(2),
);
match (insn.mnemonic, a, b, c) {
("add", Some(Operand::Reg(d)), Some(Operand::Reg(n)), Some(Operand::Reg(m)))
if n.num() == 13 && m.num() == d.num() =>
{
Some(0x4400 | high(d) | (13 << 3) | low(d))
}
("add", Some(Operand::Reg(d)), Some(Operand::Reg(m)), None) if m.num() != 13 => {
Some(0x4400 | high(d) | (u16::from(m.num()) << 3) | low(d))
}
("cmp", Some(Operand::Reg(n)), Some(Operand::Reg(m)), None)
if !n.is_low() || !m.is_low() =>
{
Some(0x4500 | high(n) | (u16::from(m.num()) << 3) | low(n))
}
("mov", Some(Operand::Reg(d)), Some(Operand::Reg(m)), None) => {
Some(0x4600 | high(d) | (u16::from(m.num()) << 3) | low(d))
}
("bx", Some(Operand::Reg(m)), None, None) => Some(0x4700 | (u16::from(m.num()) << 3)),
("blx", Some(Operand::Reg(m)), None, None) => Some(0x4780 | (u16::from(m.num()) << 3)),
_ => None,
}
}
fn narrow(mnemonic: &'static str, encoding: &'static str, addr: u32, ops: &[Operand]) -> Insn {
Insn {
mnemonic,
encoding,
addr,
width: Width::Narrow,
cond: None,
sets_flags: false,
explicit_width: false,
operands: ops.iter().copied().collect(),
}
}
fn high(r: Reg) -> u16 {
u16::from(r.num() & 0b1000) << 4
}
fn low(r: Reg) -> u16 {
u16::from(r.num() & 0b111)
}
#[cfg(test)]
mod tests {
use super::*;
fn dec(hw1: u16) -> Insn {
let decoded = decode(hw1, 0, 0x1000);
assert!(decoded.is_some(), "{hw1:#06x} failed to decode");
decoded.unwrap()
}
#[test]
fn exhaustive_round_trip() {
let mut decoded = 0;
let mut round_tripped = 0;
for hw1 in 0x4400u16..=0x47FF {
if let Some(insn) = decode(hw1, 0, 0x2000) {
decoded += 1;
assert_eq!(
encode(&insn),
Some(hw1),
"{hw1:#06x} re-encoded wrongly as `{insn}`"
);
round_tripped += 1;
}
}
assert_eq!(decoded, 736, "unexpected number of defined encodings");
assert_eq!(round_tripped, decoded);
for hw1 in 0x4500u16..=0x453F {
assert!(decode(hw1, 0, 0).is_none(), "{hw1:#06x} is UNPREDICTABLE");
}
for hw1 in 0x4700u16..=0x47FF {
assert_eq!(
decode(hw1, 0, 0).is_some(),
hw1 & 0b111 == 0,
"{hw1:#06x}: BX/BLX bits [2:0] must be 000"
);
}
}
#[test]
fn bx_lr_is_0x4770() {
let insn = dec(0x4770);
assert_eq!(insn.mnemonic, "bx");
assert_eq!(insn.encoding, "T1");
assert_eq!(insn.operands.get(0), Some(Operand::Reg(Reg::LR)));
assert_eq!(insn.to_string(), "bx lr");
assert_eq!(encode(&insn), Some(0x4770));
}
#[test]
fn high_register_cases() {
let mov = dec(0x4698);
assert_eq!((mov.mnemonic, mov.encoding), ("mov", "T1"));
assert_eq!(mov.to_string(), "mov r8, r3");
assert!(!mov.sets_flags, "MOV (register) T1 preserves the flags");
let add = dec(0x449A);
assert_eq!((add.mnemonic, add.encoding), ("add", "T2"));
assert_eq!(add.to_string(), "add r10, r3");
assert!(!add.sets_flags, "ADD (register) T2 preserves the flags");
let cmp = dec(0x45D1);
assert_eq!((cmp.mnemonic, cmp.encoding), ("cmp", "T2"));
assert_eq!(cmp.to_string(), "cmp r9, r10");
assert!(
!cmp.sets_flags,
"CMP writes the flags but takes no S suffix, and `sets_flags` is the suffix"
);
let blx = dec(0x4798);
assert_eq!((blx.mnemonic, blx.encoding), ("blx", "T1"));
assert_eq!(blx.to_string(), "blx r3");
for n in 0u8..16 {
let hw1 = 0x4600 | (u16::from(n & 8) << 4) | (u16::from(n) << 3) | u16::from(n & 7);
let mov = dec(hw1);
assert_eq!(
mov.operands.get(0),
Some(Operand::Reg(Reg(n))),
"{hw1:#06x}"
);
assert_eq!(
mov.operands.get(1),
Some(Operand::Reg(Reg(n))),
"{hw1:#06x}"
);
assert_eq!(encode(&mov), Some(hw1));
}
}
#[test]
fn sp_forms() {
assert_eq!(dec(0x4468).to_string(), "add r0, sp, r0");
assert_eq!(dec(0x44BD).to_string(), "add sp, r7");
assert_eq!(dec(0x44ED).to_string(), "add sp, sp, sp");
assert_eq!(dec(0x4478).to_string(), "add r0, pc");
}
#[test]
fn pc_writes_are_branches() {
assert!(dec(0x4770).writes_pc(), "bx lr");
assert!(dec(0x46F7).writes_pc(), "mov pc, lr");
assert!(dec(0x44F7).writes_pc(), "add pc, lr");
assert!(!dec(0x4608).writes_pc(), "mov r0, r1");
assert!(!dec(0x4541).writes_pc(), "cmp r1, r8");
assert_eq!(dec(0x4770).branch_target(), None);
}
#[test]
fn classification() {
let bx = dec(0x4770);
assert!(
bx.is_branch() && !bx.is_call(),
"bx is a branch, not a call"
);
let blx = dec(0x4798);
assert!(blx.is_branch() && blx.is_call(), "blx r3 is a call");
assert!(dec(0x46F7).is_branch());
assert!(!dec(0x46F7).is_call());
assert!(!dec(0x4608).is_branch(), "mov r0, r1");
}
#[test]
fn encode_rejects_foreign_forms() {
let mut low_cmp = dec(0x45D1);
low_cmp.operands = [Operand::Reg(Reg(1)), Operand::Reg(Reg(2))]
.iter()
.copied()
.collect();
assert_eq!(encode(&low_cmp), None);
let mut adds = dec(0x449A);
adds.sets_flags = true;
assert_eq!(encode(&adds), None);
let mut add_sp = dec(0x449A);
add_sp.operands = [Operand::Reg(Reg(1)), Operand::Reg(Reg::SP)]
.iter()
.copied()
.collect();
assert_eq!(encode(&add_sp), None);
let mut wide = dec(0x4698);
wide.width = Width::Wide;
assert_eq!(encode(&wide), None);
let sp_add = dec(0x4468);
assert_eq!(sp_add.to_string(), "add r0, sp, r0");
assert_eq!(encode(&sp_add), Some(0x4468));
for (ops, why) in [
([Reg(0), Reg(1), Reg(0)], "Rn is not the SP"),
([Reg(0), Reg::SP, Reg(1)], "Rm is not Rd"),
([Reg(0), Reg(1), Reg(2)], "neither holds"),
] {
let mut mangled = sp_add;
mangled.operands = ops.iter().map(|&r| Operand::Reg(r)).collect();
assert_eq!(encode(&mangled), None, "{why}: `{mangled}`");
}
}
#[test]
fn decode_ignores_other_groups_and_hw2() {
assert!(decode(0x4000, 0, 0).is_none(), "A5.2.2 data processing");
assert!(decode(0x4800, 0, 0).is_none(), "LDR (literal)");
assert_eq!(decode(0x4770, 0xFFFF, 0), decode(0x4770, 0x0000, 0));
assert_eq!(decode(0x4770, 0, 0x8000).unwrap().addr, 0x8000);
}
}