use super::{AddrMode, Insn, Mem, Operand, Operands, Reg, Shift, ShiftAmount, ShiftKind, Width};
const HW1_MASK: u16 = 0xFE40;
const HW1_FIXED: u16 = 0xE840;
const OP3_BYTE: u16 = 0b0100;
const OP3_HALF: u16 = 0b0101;
const OP3_DOUBLE: u16 = 0b0111;
fn literal_base(addr: u32) -> u32 {
addr.wrapping_add(4) & !3
}
fn wide(mnemonic: &'static str, addr: u32, operands: Operands) -> Insn {
Insn {
mnemonic,
encoding: "T1",
addr,
width: Width::Wide,
cond: None,
sets_flags: false,
explicit_width: false,
operands,
}
}
fn offset_mem(base: Reg, offset: u32) -> Mem {
Mem {
base,
index: None,
offset,
add: true,
align: 0,
mode: AddrMode::Offset,
}
}
fn store_excl(rd: Reg, rt: Reg, rt2: Option<Reg>, rn: Reg) -> Operands {
let mut operands = Operands::new();
operands.push(Operand::Reg(rd));
operands.push(Operand::Reg(rt));
if let Some(rt2) = rt2 {
operands.push(Operand::Reg(rt2));
}
operands.push(Operand::Mem(offset_mem(rn, 0)));
operands
}
fn load_excl(rt: Reg, rt2: Option<Reg>, rn: Reg) -> Operands {
let mut operands = Operands::new();
operands.push(Operand::Reg(rt));
if let Some(rt2) = rt2 {
operands.push(Operand::Reg(rt2));
}
operands.push(Operand::Mem(offset_mem(rn, 0)));
operands
}
pub(crate) fn decode(hw1: u16, hw2: u16, addr: u32) -> Option<Insn> {
if hw1 & HW1_MASK != HW1_FIXED {
return None;
}
let p = (hw1 >> 8) & 1;
let w = (hw1 >> 5) & 1;
if p == 0 && w == 0 {
decode_sync(hw1, hw2, addr)
} else {
Some(decode_dual(hw1, hw2, addr))
}
}
fn decode_sync(hw1: u16, hw2: u16, addr: u32) -> Option<Insn> {
let rn = Reg((hw1 & 0xF) as u8);
let rt = Reg((hw2 >> 12) as u8);
let mid = (hw2 >> 8) & 0xF;
let op3 = (hw2 >> 4) & 0xF;
let lo = hw2 & 0xF;
let sized = (hw1 >> 7) & 1 == 1;
let load = (hw1 >> 4) & 1 == 1;
match (sized, load) {
(false, false) => {
let imm32 = u32::from(hw2 & 0xFF) * 4;
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(mid as u8)));
operands.push(Operand::Reg(rt));
operands.push(Operand::Mem(offset_mem(rn, imm32)));
Some(wide("strex", addr, operands))
}
(false, true) => {
if mid != 0xF {
return None;
}
let imm32 = u32::from(hw2 & 0xFF) * 4;
let mut operands = Operands::new();
operands.push(Operand::Reg(rt));
operands.push(Operand::Mem(offset_mem(rn, imm32)));
Some(wide("ldrex", addr, operands))
}
(true, false) => match op3 {
OP3_BYTE | OP3_HALF => {
if mid != 0xF {
return None;
}
let mnemonic = if op3 == OP3_BYTE { "strexb" } else { "strexh" };
Some(wide(
mnemonic,
addr,
store_excl(Reg(lo as u8), rt, None, rn),
))
}
OP3_DOUBLE => Some(wide(
"strexd",
addr,
store_excl(Reg(lo as u8), rt, Some(Reg(mid as u8)), rn),
)),
_ => None,
},
(true, true) => match op3 {
0b0000 | 0b0001 => {
if rt.num() != 0xF || mid != 0 {
return None;
}
let shift = if op3 == 1 {
Some(Shift {
kind: ShiftKind::Lsl,
amount: ShiftAmount::Imm(1),
})
} else {
None
};
let mut operands = Operands::new();
operands.push(Operand::Mem(Mem {
base: rn,
index: Some((Reg(lo as u8), shift)),
offset: 0,
add: true,
align: 0,
mode: AddrMode::Offset,
}));
Some(wide(if op3 == 1 { "tbh" } else { "tbb" }, addr, operands))
}
OP3_BYTE | OP3_HALF => {
if mid != 0xF || lo != 0xF {
return None;
}
let mnemonic = if op3 == OP3_BYTE { "ldrexb" } else { "ldrexh" };
Some(wide(mnemonic, addr, load_excl(rt, None, rn)))
}
OP3_DOUBLE => {
if lo != 0xF {
return None;
}
Some(wide(
"ldrexd",
addr,
load_excl(rt, Some(Reg(mid as u8)), rn),
))
}
_ => None,
},
}
}
fn decode_dual(hw1: u16, hw2: u16, addr: u32) -> Insn {
let p = (hw1 >> 8) & 1;
let u = (hw1 >> 7) & 1;
let w = (hw1 >> 5) & 1;
let load = (hw1 >> 4) & 1 == 1;
let rn = Reg((hw1 & 0xF) as u8);
let rt = Reg((hw2 >> 12) as u8);
let rt2 = Reg(((hw2 >> 8) & 0xF) as u8);
let offset = u32::from(hw2 & 0xFF) * 4;
let add = u == 1;
let mode = if p == 0 {
AddrMode::PostIndex
} else if w == 1 {
AddrMode::PreIndex
} else {
AddrMode::Offset
};
let mut operands = Operands::new();
operands.push(Operand::Reg(rt));
operands.push(Operand::Reg(rt2));
operands.push(Operand::Mem(Mem {
base: rn,
index: None,
offset,
add,
align: 0,
mode,
}));
if load && rn.num() == 0xF {
operands.push(Operand::Target(literal_base(addr).wrapping_add(if add {
offset
} else {
offset.wrapping_neg()
})));
}
wide(if load { "ldrd" } else { "strd" }, addr, operands)
}
fn reg_field(op: Option<Operand>) -> Option<u16> {
match op? {
Operand::Reg(r) => Some(r.num() as u16),
_ => None,
}
}
fn mem_field(op: Option<Operand>) -> Option<Mem> {
match op? {
Operand::Mem(m) => Some(m),
_ => None,
}
}
fn bare_base(mem: &Mem) -> Option<u16> {
if mem.index.is_some() || mem.offset != 0 || !mem.add || mem.mode != AddrMode::Offset {
return None;
}
Some(mem.base.num() as u16)
}
fn scaled_base(mem: &Mem) -> Option<(u16, u16)> {
if mem.index.is_some() || mem.mode != AddrMode::Offset || !mem.add {
return None;
}
let bytes = mem.offset;
if bytes % 4 != 0 || bytes / 4 > 0xFF {
return None;
}
Some((mem.base.num() as u16, (bytes / 4) as u16))
}
pub(crate) fn encode(insn: &Insn) -> Option<(u16, u16)> {
if insn.width != Width::Wide || insn.sets_flags || insn.explicit_width || insn.encoding != "T1"
{
return None;
}
match insn.mnemonic {
"strex" => encode_strex(insn),
"ldrex" => encode_ldrex(insn),
"strexb" => encode_store_excl(insn, OP3_BYTE, false),
"strexh" => encode_store_excl(insn, OP3_HALF, false),
"strexd" => encode_store_excl(insn, OP3_DOUBLE, true),
"ldrexb" => encode_load_excl(insn, OP3_BYTE, false),
"ldrexh" => encode_load_excl(insn, OP3_HALF, false),
"ldrexd" => encode_load_excl(insn, OP3_DOUBLE, true),
"tbb" => encode_table_branch(insn, false),
"tbh" => encode_table_branch(insn, true),
"strd" => encode_dual(insn, false),
"ldrd" => encode_dual(insn, true),
_ => None,
}
}
fn encode_strex(insn: &Insn) -> Option<(u16, u16)> {
if insn.operands.len() != 3 {
return None;
}
let rd = reg_field(insn.operands.get(0))?;
let rt = reg_field(insn.operands.get(1))?;
let (rn, imm8) = scaled_base(&mem_field(insn.operands.get(2))?)?;
Some((HW1_FIXED | rn, (rt << 12) | (rd << 8) | imm8))
}
fn encode_ldrex(insn: &Insn) -> Option<(u16, u16)> {
if insn.operands.len() != 2 {
return None;
}
let rt = reg_field(insn.operands.get(0))?;
let (rn, imm8) = scaled_base(&mem_field(insn.operands.get(1))?)?;
Some((HW1_FIXED | 0x0010 | rn, (rt << 12) | 0x0F00 | imm8))
}
fn encode_store_excl(insn: &Insn, op3: u16, dual: bool) -> Option<(u16, u16)> {
let rd = reg_field(insn.operands.get(0))?;
let rt = reg_field(insn.operands.get(1))?;
let (rt2, at) = if dual {
(reg_field(insn.operands.get(2))?, 3)
} else {
(0xF, 2)
};
if insn.operands.len() != at + 1 {
return None;
}
let rn = bare_base(&mem_field(insn.operands.get(at))?)?;
Some((
HW1_FIXED | 0x0080 | rn,
(rt << 12) | (rt2 << 8) | (op3 << 4) | rd,
))
}
fn encode_load_excl(insn: &Insn, op3: u16, dual: bool) -> Option<(u16, u16)> {
let rt = reg_field(insn.operands.get(0))?;
let (rt2, at) = if dual {
(reg_field(insn.operands.get(1))?, 2)
} else {
(0xF, 1)
};
if insn.operands.len() != at + 1 {
return None;
}
let rn = bare_base(&mem_field(insn.operands.get(at))?)?;
Some((
HW1_FIXED | 0x0090 | rn,
(rt << 12) | (rt2 << 8) | (op3 << 4) | 0xF,
))
}
fn encode_table_branch(insn: &Insn, halfword: bool) -> Option<(u16, u16)> {
if insn.operands.len() != 1 {
return None;
}
let mem = mem_field(insn.operands.get(0))?;
if mem.offset != 0 || !mem.add || mem.mode != AddrMode::Offset {
return None;
}
let (rm, shift) = mem.index?;
let want = if halfword {
Some(Shift {
kind: ShiftKind::Lsl,
amount: ShiftAmount::Imm(1),
})
} else {
None
};
if shift != want {
return None;
}
Some((
HW1_FIXED | 0x0090 | mem.base.num() as u16,
0xF000 | (u16::from(halfword) << 4) | rm.num() as u16,
))
}
fn encode_dual(insn: &Insn, load: bool) -> Option<(u16, u16)> {
let rt = reg_field(insn.operands.get(0))?;
let rt2 = reg_field(insn.operands.get(1))?;
let mem = mem_field(insn.operands.get(2))?;
if mem.index.is_some() {
return None;
}
let magnitude = mem.offset;
if magnitude % 4 != 0 || magnitude / 4 > 0xFF {
return None;
}
let imm8 = (magnitude / 4) as u16;
let u = u16::from(mem.add);
let (p, w) = match mem.mode {
AddrMode::Offset => (1, 0),
AddrMode::PreIndex => (1, 1),
AddrMode::PostIndex => (0, 1),
AddrMode::PostIncrement => return None,
};
if load && mem.base == Reg::PC {
if insn.operands.len() != 4 {
return None;
}
let target = match insn.operands.get(3) {
Some(Operand::Target(t)) => t,
_ => return None,
};
if target != literal_base(insn.addr).wrapping_add(mem.displacement() as u32) {
return None;
}
} else if insn.operands.len() != 3 {
return None;
}
let l = u16::from(load);
Some((
HW1_FIXED | (p << 8) | (u << 7) | (w << 5) | (l << 4) | mem.base.num() as u16,
(rt << 12) | (rt2 << 8) | imm8,
))
}
#[cfg(test)]
mod tests {
use super::*;
const ALIGNED: u32 = 0x1000;
const UNALIGNED: u32 = 0x1002;
fn hw1_of(op1: u16, op2: u16, rn: u16) -> u16 {
HW1_FIXED | (op1 << 7) | (op2 << 4) | rn
}
fn ual(hw1: u16, hw2: u16, addr: u32) -> String {
let decoded = decode(hw1, hw2, addr);
assert!(
decoded.is_some(),
"{hw1:#06x} {hw2:#06x} is in Table A5-17 and must decode"
);
decoded.unwrap().to_string()
}
fn bare(rn: u8) -> Operand {
Operand::Mem(Mem {
base: Reg(rn),
index: None,
offset: 0,
add: true,
align: 0,
mode: AddrMode::Offset,
})
}
fn scaled(rn: u8, offset: u32) -> Operand {
Operand::Mem(Mem {
base: Reg(rn),
index: None,
offset,
add: true,
align: 0,
mode: AddrMode::Offset,
})
}
fn table(rn: u8, rm: u8, halfword: bool) -> Operand {
Operand::Mem(Mem {
base: Reg(rn),
index: Some((
Reg(rm),
if halfword {
Some(Shift {
kind: ShiftKind::Lsl,
amount: ShiftAmount::Imm(1),
})
} else {
None
},
)),
offset: 0,
add: true,
align: 0,
mode: AddrMode::Offset,
})
}
fn table_row(op1: u16, op2: u16, op3: u16) -> Option<&'static str> {
if op1 >> 1 == 1 || op2 >> 1 == 1 {
return Some(if op2 & 1 == 1 { "ldrd" } else { "strd" });
}
match (op1, op2, op3) {
(0b00, 0b00, _) => Some("strex"),
(0b00, 0b01, _) => Some("ldrex"),
(0b01, 0b00, 0b0100) => Some("strexb"),
(0b01, 0b00, 0b0101) => Some("strexh"),
(0b01, 0b00, 0b0111) => Some("strexd"), (0b01, 0b01, 0b0000) => Some("tbb"),
(0b01, 0b01, 0b0001) => Some("tbh"),
(0b01, 0b01, 0b0100) => Some("ldrexb"),
(0b01, 0b01, 0b0101) => Some("ldrexh"),
(0b01, 0b01, 0b0111) => Some("ldrexd"), _ => None,
}
}
fn sbo_fields(mnemonic: &str) -> (u16, u16) {
match mnemonic {
"ldrex" | "strexb" | "strexh" => (0x0F00, 0x0F00),
"ldrexb" | "ldrexh" => (0x0F0F, 0x0F0F),
"ldrexd" => (0x000F, 0x000F),
"tbb" | "tbh" => (0xFF00, 0xF000),
_ => (0x0000, 0x0000),
}
}
#[test]
fn sweep_round_trips_and_matches_the_tables() {
let mut decoded = 0usize;
let mut undecoded = 0usize;
let mut minus_zero = 0usize;
for addr in [ALIGNED, UNALIGNED] {
for op1 in 0..4u16 {
for op2 in 0..4u16 {
for rn in 0..16u16 {
let hw1 = hw1_of(op1, op2, rn);
for rt in [0u16, 15] {
for mid in [0u16, 1, 14, 15] {
for op3 in 0..16u16 {
for lo in [0u16, 1, 14, 15] {
let hw2 = (rt << 12) | (mid << 8) | (op3 << 4) | lo;
let want = table_row(op1, op2, op3).filter(|m| {
let (mask, value) = sbo_fields(m);
hw2 & mask == value
});
let got = decode(hw1, hw2, addr);
assert_eq!(
got.map(|i| i.mnemonic),
want,
"{hw1:#06x} {hw2:#06x}"
);
let insn = match got {
None => {
undecoded += 1;
continue;
}
Some(i) => i,
};
decoded += 1;
assert_eq!(insn.width, Width::Wide, "{hw1:#06x}");
assert_eq!(insn.len(), 4, "{hw1:#06x}");
assert_eq!(insn.encoding, "T1", "{hw1:#06x}");
assert_eq!(insn.addr, addr, "{hw1:#06x}");
assert!(insn.cond.is_none(), "{hw1:#06x}");
assert!(!insn.sets_flags, "{hw1:#06x}");
assert!(!insn.explicit_width, "{hw1:#06x}");
let is_dual = matches!(insn.mnemonic, "ldrd" | "strd");
if is_dual && op1 & 1 == 0 && hw2 & 0xFF == 0 {
minus_zero += 1;
assert!(
insn.to_string().contains("#-0"),
"{hw1:#06x} {hw2:#06x} is `#-0` and must print so"
);
}
assert_eq!(
encode(&insn),
Some((hw1, hw2)),
"round-trip of {hw1:#06x} {hw2:#06x} at {addr:#x}"
);
}
}
}
}
}
}
}
}
assert_eq!(decoded + undecoded, 2 * 256 * 512);
let row = |pairs: usize, rt: usize, mid: usize, op3: usize, lo: usize| {
pairs * 16 * rt * mid * op3 * lo
};
let dual = row(12, 2, 4, 16, 4);
let strex = row(1, 2, 4, 16, 4);
let ldrex = row(1, 2, 1, 16, 4);
let sized_stores = 2 * row(1, 2, 1, 1, 4) + row(1, 2, 4, 1, 4);
let sized_loads = 2 * row(1, 1, 1, 1, 4) + 2 * row(1, 2, 1, 1, 1) + row(1, 2, 4, 1, 1);
assert_eq!(
decoded,
2 * (dual + strex + ldrex + sized_stores + sized_loads)
);
assert_eq!(decoded, 2 * 109_632);
assert_eq!(undecoded, 2 * 21_440);
assert_eq!(minus_zero, 2 * 6 * 16 * 8);
}
#[test]
fn outside_the_group_is_not_ours() {
assert!(decode(0xE800, 0, 0).is_none());
assert!(decode(0xE8C0 & !0x40, 0, 0).is_none());
assert!(decode(0xEA40, 0, 0).is_none());
assert!(decode(0x4770, 0, 0).is_none());
assert!(decode(0xF040, 0, 0).is_none());
assert!(decode(0xF840, 0, 0).is_none());
}
#[test]
fn table_a5_17_rows_print_their_ual_syntax() {
assert_eq!(ual(0xE842, 0x1002, 0), "strex r0, r1, [r2, #8]");
assert_eq!(ual(0xE842, 0x1000, 0), "strex r0, r1, [r2]");
assert_eq!(ual(0xE842, 0x10FF, 0), "strex r0, r1, [r2, #1020]");
assert_eq!(ual(0xE851, 0x0F02, 0), "ldrex r0, [r1, #8]");
assert_eq!(ual(0xE851, 0x0F00, 0), "ldrex r0, [r1]");
assert_eq!(ual(0xE9C2, 0x0102, 0), "strd r0, r1, [r2, #8]");
assert_eq!(ual(0xE9E2, 0x0102, 0), "strd r0, r1, [r2, #8]!");
assert_eq!(ual(0xE8E2, 0x0102, 0), "strd r0, r1, [r2], #8");
assert_eq!(ual(0xE942, 0x0102, 0), "strd r0, r1, [r2, #-8]");
assert_eq!(ual(0xE9D2, 0x0102, 0), "ldrd r0, r1, [r2, #8]");
assert_eq!(ual(0xE9F2, 0x0102, 0), "ldrd r0, r1, [r2, #8]!");
assert_eq!(ual(0xE8F2, 0x0102, 0), "ldrd r0, r1, [r2], #8");
assert_eq!(ual(0xE9D2, 0x01FF, 0), "ldrd r0, r1, [r2, #1020]");
assert_eq!(ual(0xE9DF, 0x0102, 0x1002), "ldrd r0, r1, [pc, #8], 0x100c");
assert_eq!(ual(0xE8C2, 0x1F40, 0), "strexb r0, r1, [r2]");
assert_eq!(ual(0xE8C2, 0x1F50, 0), "strexh r0, r1, [r2]");
assert_eq!(ual(0xE8D0, 0xF001, 0), "tbb [r0, r1]");
assert_eq!(ual(0xE8D0, 0xF011, 0), "tbh [r0, r1, lsl #1]");
assert_eq!(ual(0xE8D1, 0x0F4F, 0), "ldrexb r0, [r1]");
assert_eq!(ual(0xE8D1, 0x0F5F, 0), "ldrexh r0, [r1]");
assert_eq!(ual(0xE8C3, 0x1270, 0), "strexd r0, r1, r2, [r3]");
assert_eq!(ual(0xE8D2, 0x017F, 0), "ldrexd r0, r1, [r2]");
assert_eq!(ual(0xE9CD, 0xE102, 0), "strd lr, r1, [sp, #8]");
}
#[test]
fn table_branches_branch_but_have_no_target() {
for (hw2, text) in [
(0xF001u16, "tbb [r0, r1]"),
(0xF011, "tbh [r0, r1, lsl #1]"),
] {
let insn = decode(0xE8D0, hw2, 0x1000).unwrap();
assert_eq!(insn.to_string(), text);
assert!(insn.is_branch(), "{text}");
assert_eq!(insn.branch_target(), None, "{text}");
assert!(!insn.is_call(), "{text}");
assert!(!insn.writes_pc(), "{text}");
assert_eq!(insn.operands.len(), 1);
assert_eq!(insn.operands.get(0), Some(table(0, 1, hw2 & 0x10 != 0)));
assert_eq!(encode(&insn), Some((0xE8D0, hw2)));
}
let tbh = decode(0xE8D0, 0xF011, 0).unwrap();
assert!(tbh.to_string().ends_with("lsl #1]"));
assert_eq!(tbh.operands.get(0), Some(table(0, 1, true)));
assert_eq!(ual(0xE8DF, 0xF002, 0), "tbb [pc, r2]");
}
#[test]
fn exclusive_operand_order_matches_the_manual() {
let strex = decode(0xE842, 0x1002, 0).unwrap();
assert_eq!(strex.operands.len(), 3);
assert_eq!(strex.operands.get(0), Some(Operand::Reg(Reg(0)))); assert_eq!(strex.operands.get(1), Some(Operand::Reg(Reg(1)))); assert_eq!(strex.operands.get(2), Some(scaled(2, 8)));
let ldrex = decode(0xE851, 0x0F02, 0).unwrap();
assert_eq!(ldrex.operands.len(), 2);
assert_eq!(ldrex.operands.get(0), Some(Operand::Reg(Reg(0)))); assert_eq!(ldrex.operands.get(1), Some(scaled(1, 8)));
let strexb = decode(0xE8C2, 0x3F40, 0).unwrap();
assert_eq!(strexb.to_string(), "strexb r0, r3, [r2]");
assert_eq!(strexb.operands.len(), 3);
let ldrexb = decode(0xE8D1, 0x3F4F, 0).unwrap();
assert_eq!(ldrexb.to_string(), "ldrexb r3, [r1]");
assert_eq!(ldrexb.operands.len(), 2);
let strexd = decode(0xE8C3, 0x1270, 0).unwrap();
assert_eq!(strexd.operands.len(), 4);
assert_eq!(strexd.operands.get(0), Some(Operand::Reg(Reg(0)))); assert_eq!(strexd.operands.get(1), Some(Operand::Reg(Reg(1)))); assert_eq!(strexd.operands.get(2), Some(Operand::Reg(Reg(2)))); let ldrexd = decode(0xE8D2, 0x017F, 0).unwrap();
assert_eq!(ldrexd.operands.len(), 3);
assert_eq!(ldrexd.operands.get(0), Some(Operand::Reg(Reg(0)))); assert_eq!(ldrexd.operands.get(1), Some(Operand::Reg(Reg(1))));
for (hw1, hw2) in [
(0xE8C2u16, 0x1F40u16),
(0xE8C2, 0x1F50),
(0xE8D1, 0x0F4F),
(0xE8D1, 0x0F5F),
(0xE8C3, 0x1270),
(0xE8D2, 0x017F),
] {
let insn = decode(hw1, hw2, 0).unwrap();
assert_eq!(
insn.operands.get(insn.operands.len() - 1),
Some(bare((hw1 & 0xF) as u8)),
"{} should end in a bare [<Rn>]",
insn.mnemonic
);
}
}
#[test]
fn ldrd_literal_resolves_against_aligned_pc() {
let add = 0xE9DFu16;
let sub = 0xE95Fu16;
let hw2 = 0x0102u16;
assert_eq!(
decode(add, hw2, 0x1000).unwrap().branch_target(),
Some(0x100C)
);
assert_eq!(
decode(sub, hw2, 0x1000).unwrap().branch_target(),
Some(0x0FFC)
);
assert_eq!(
decode(add, hw2, 0x1002).unwrap().branch_target(),
Some(0x100C)
);
assert_eq!(
decode(sub, hw2, 0x1002).unwrap().branch_target(),
Some(0x0FFC)
);
assert_eq!(
decode(add, hw2, 0x1004).unwrap().branch_target(),
Some(0x1010)
);
assert_eq!(
decode(sub, hw2, 0x1004).unwrap().branch_target(),
Some(0x1000)
);
for addr in [0x1000u32, 0x1002, 0x1004] {
for hw1 in [add, sub] {
let insn = decode(hw1, hw2, addr).unwrap();
assert_eq!(insn.operands.len(), 4);
assert_eq!(
insn.operands.get(2),
Some(Operand::Mem(Mem {
base: Reg::PC,
index: None,
offset: 8,
add: hw1 == add,
align: 0,
mode: AddrMode::Offset,
})),
"{hw1:#06x} at {addr:#x}"
);
let resolved = literal_base(addr).wrapping_add(if hw1 == add {
8
} else {
8u32.wrapping_neg()
});
assert_eq!(
insn.operands.get(3),
Some(Operand::Target(resolved)),
"{hw1:#06x} at {addr:#x}"
);
assert_eq!(encode(&insn), Some((hw1, hw2)), "{hw1:#06x} at {addr:#x}");
}
}
assert_eq!(
decode(sub, hw2, 0x1002).unwrap().to_string(),
"ldrd r0, r1, [pc, #-8], 0xffc"
);
let mut wrong = decode(add, hw2, 0x1002).unwrap();
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(0)));
operands.push(Operand::Reg(Reg(1)));
operands.push(Operand::Mem(offset_mem(Reg::PC, 8)));
operands.push(Operand::Target(0x100E)); wrong.operands = operands;
assert_eq!(encode(&wrong), None);
let strd = decode(0xE9CF, hw2, 0x1002).unwrap();
assert_eq!(strd.to_string(), "strd r0, r1, [pc, #8]");
assert_eq!(strd.branch_target(), None);
assert_eq!(strd.operands.len(), 3);
assert_eq!(encode(&strd), Some((0xE9CF, hw2)));
}
#[test]
fn unpredictable_register_choices_still_decode() {
let same = decode(0xE9D2, 0x0002, 0).unwrap(); assert_eq!(same.to_string(), "ldrd r0, r0, [r2, #8]");
assert_eq!(encode(&same), Some((0xE9D2, 0x0002)));
let wb = decode(0xE9F2, 0x2102, 0).unwrap();
assert_eq!(wb.to_string(), "ldrd r2, r1, [r2, #8]!");
assert_eq!(encode(&wb), Some((0xE9F2, 0x2102)));
let pc = decode(0xE9D2, 0xF102, 0).unwrap();
assert_eq!(pc.to_string(), "ldrd pc, r1, [r2, #8]");
assert!(pc.writes_pc());
assert_eq!(encode(&pc), Some((0xE9D2, 0xF102)));
assert_eq!(ual(0xE8DD, 0xF001, 0), "tbb [sp, r1]");
}
#[test]
fn should_be_one_and_should_be_zero_fields_are_required() {
assert!(decode(0xE851, 0x0F02, 0).is_some()); assert!(decode(0xE851, 0x0E02, 0).is_none());
assert!(decode(0xE8C2, 0x1F40, 0).is_some()); assert!(decode(0xE8C2, 0x1B40, 0).is_none());
assert!(decode(0xE8D1, 0x0F4F, 0).is_some()); assert!(decode(0xE8D1, 0x0F4E, 0).is_none());
assert!(decode(0xE8D1, 0x0D4F, 0).is_none());
assert!(decode(0xE8D2, 0x017F, 0).is_some()); assert!(decode(0xE8D2, 0x0170, 0).is_none());
assert!(decode(0xE8D0, 0xF001, 0).is_some()); assert!(decode(0xE8D0, 0xE001, 0).is_none());
assert!(decode(0xE8D0, 0xF101, 0).is_none());
assert!(decode(0xE842, 0xFFFF, 0).is_some());
assert!(decode(0xE8C3, 0xFF7F, 0).is_some());
for op3 in 0..16u16 {
let sized = 0x0F0Fu16 | (op3 << 4);
let table = 0xF00Fu16 | (op3 << 4);
let named = matches!(op3, OP3_BYTE | OP3_HALF | OP3_DOUBLE);
assert_eq!(
decode(0xE8C0, sized, 0).is_some(),
named,
"store op3 {op3:04b}"
);
assert_eq!(
decode(0xE8D0, sized, 0).is_some(),
named,
"load op3 {op3:04b}"
);
assert_eq!(
decode(0xE8D0, table, 0).is_some(),
matches!(op3, 0b0000 | 0b0001 | OP3_DOUBLE),
"table op3 {op3:04b}"
);
}
}
#[test]
fn minus_zero_is_a_different_instruction_from_plus_zero() {
let plus = decode(0xE9C2, 0x0100, 0).unwrap(); let minus = decode(0xE942, 0x0100, 0).unwrap(); assert_eq!(plus.to_string(), "strd r0, r1, [r2]");
assert_eq!(minus.to_string(), "strd r0, r1, [r2, #-0]");
assert_ne!(plus.operands, minus.operands);
let mem = |i: &Insn| mem_field(i.operands.get(2)).unwrap();
assert!(mem(&plus).add && mem(&plus).offset == 0);
assert!(!mem(&minus).add && mem(&minus).offset == 0);
assert_eq!(mem(&plus).displacement(), mem(&minus).displacement());
assert_eq!(encode(&plus), Some((0xE9C2, 0x0100)));
assert_eq!(encode(&minus), Some((0xE942, 0x0100)));
assert_eq!(
encode(&decode(0xE942, 0x0101, 0).unwrap()),
Some((0xE942, 0x0101))
);
let post = decode(0xE8E2, 0x0100, 0).unwrap(); assert_eq!(post.to_string(), "strd r0, r1, [r2], #0");
assert_eq!(encode(&post), Some((0xE8E2, 0x0100)));
let post_minus = decode(0xE862, 0x0100, 0).unwrap(); assert_eq!(post_minus.to_string(), "strd r0, r1, [r2], #-0");
assert_eq!(encode(&post_minus), Some((0xE862, 0x0100)));
}
#[test]
fn encode_rejects_every_mangled_operand_slot() {
for (hw1, hw2, addr) in [
(0xE842u16, 0x1002u16, 0u32), (0xE851, 0x0F02, 0), (0xE8C2, 0x1F40, 0), (0xE8C2, 0x1F50, 0), (0xE8C3, 0x1270, 0), (0xE8D1, 0x0F4F, 0), (0xE8D1, 0x0F5F, 0), (0xE8D2, 0x017F, 0), (0xE8D0, 0xF001, 0), (0xE8D0, 0xF011, 0), (0xE9C2, 0x0102, 0), (0xE9D2, 0x0102, 0), (0xE9DF, 0x0102, ALIGNED), ] {
let insn = decode(hw1, hw2, addr).unwrap();
assert_eq!(
encode(&insn),
Some((hw1, hw2)),
"`{insn}` should round-trip before anything is mangled"
);
let arity = insn.operands.len();
for slot in 0..arity {
let mut mangled = insn;
mangled.operands = (0..arity)
.map(|i| {
let op = insn.operands.get(i).unwrap();
if i != slot {
op
} else {
match op {
Operand::Reg(_) => Operand::Imm(0),
_ => Operand::Reg(Reg(0)),
}
}
})
.collect();
assert_eq!(encode(&mangled), None, "`{insn}`, operand {slot} mangled");
}
let (fewer, more) = (arity - 1, arity + 1);
let mut short = insn;
short.operands = (0..fewer).map(|i| insn.operands.get(i).unwrap()).collect();
assert_eq!(encode(&short), None, "`{insn}` with {fewer} operands");
let mut long = insn;
long.operands = (0..more)
.map(|i| insn.operands.get(i).unwrap_or(Operand::Imm(0)))
.collect();
assert_eq!(encode(&long), None, "`{insn}` with {more} operands");
let mut empty = insn;
empty.operands = Operands::new();
assert_eq!(encode(&empty), None, "`{insn}` with no operands");
}
let mut ldrex = decode(0xE851, 0x0F02, 0).unwrap();
ldrex.operands = [
Operand::Reg(Reg(0)),
Operand::Mem(offset_mem(Reg(1), 2)),
]
.iter()
.copied()
.collect();
assert_eq!(encode(&ldrex), None, "`ldrex` offsets are word-scaled");
let mut ldrexb = decode(0xE8D1, 0x0F4F, 0).unwrap();
ldrexb.operands = [
Operand::Reg(Reg(0)),
Operand::Mem(offset_mem(Reg(1), 4)),
]
.iter()
.copied()
.collect();
assert_eq!(encode(&ldrexb), None, "`ldrexb` takes a bare `[<Rn>]`");
let tbb = decode(0xE8D0, 0xF001, 0).unwrap();
let indexed = Mem {
base: Reg(0),
index: Some((Reg(1), None)),
offset: 0,
add: true,
align: 0,
mode: AddrMode::Offset,
};
for (mem, why) in [
(offset_mem(Reg(0), 0), "`tbb [r0]` names no table column"),
(
Mem {
offset: 4,
..indexed
},
"there is nowhere to put an immediate beside the index",
),
(
Mem {
add: false,
..indexed
},
"a table index is added; there is no `U` bit",
),
(
Mem {
mode: AddrMode::PreIndex,
..indexed
},
"a table branch does not write its base back",
),
] {
let mut bad = tbb;
bad.operands = [Operand::Mem(mem)].iter().copied().collect();
assert_eq!(encode(&bad), None, "{why}");
}
let mut post_inc = decode(0xE9D2, 0x0102, 0).unwrap();
post_inc.operands = [
Operand::Reg(Reg(0)),
Operand::Reg(Reg(1)),
Operand::Mem(Mem {
mode: AddrMode::PostIncrement,
..offset_mem(Reg(2), 8)
}),
]
.iter()
.copied()
.collect();
assert_eq!(encode(&post_inc), None);
}
#[test]
fn encode_rejects_what_this_group_cannot_hold() {
let base = decode(0xE9D2, 0x0102, 0).unwrap(); assert_eq!(encode(&base), Some((0xE9D2, 0x0102)));
let mut foreign = base;
foreign.mnemonic = "ldm";
assert_eq!(encode(&foreign), None);
for mnemonic in ["ldr", "str", "ldrb", "push", "b", "add"] {
let mut other = base;
other.mnemonic = mnemonic;
assert_eq!(encode(&other), None, "{mnemonic}");
}
let mut narrow = base;
narrow.width = Width::Narrow;
assert_eq!(encode(&narrow), None);
let mut flags = base;
flags.sets_flags = true;
assert_eq!(encode(&flags), None);
let mut widened = base;
widened.explicit_width = true;
assert_eq!(encode(&widened), None);
let mut renamed = base;
renamed.encoding = "T2";
assert_eq!(encode(&renamed), None);
for bad in [1u32, 2, 1024, 6] {
let mut off = base;
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(0)));
operands.push(Operand::Reg(Reg(1)));
operands.push(Operand::Mem(offset_mem(Reg(2), bad)));
off.operands = operands;
assert_eq!(encode(&off), None, "offset {bad}");
}
let mut indexed = base;
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(0)));
operands.push(Operand::Reg(Reg(1)));
operands.push(Operand::Mem(Mem {
base: Reg(2),
index: Some((Reg(3), None)),
offset: 0,
add: true,
align: 0,
mode: AddrMode::Offset,
}));
indexed.operands = operands;
assert_eq!(encode(&indexed), None);
let strex = decode(0xE842, 0x1002, 0).unwrap();
assert_eq!(encode(&strex), Some((0xE842, 0x1002)));
for mem in [
Mem {
add: false,
..offset_mem(Reg(2), 8)
},
offset_mem(Reg(2), 2),
Mem {
base: Reg(2),
index: None,
offset: 8,
add: true,
align: 0,
mode: AddrMode::PreIndex,
},
] {
let mut bad = strex;
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(0)));
operands.push(Operand::Reg(Reg(1)));
operands.push(Operand::Mem(mem));
bad.operands = operands;
assert_eq!(encode(&bad), None, "{mem}");
}
let mut far = strex;
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(0)));
operands.push(Operand::Reg(Reg(1)));
operands.push(Operand::Mem(offset_mem(Reg(2), 1024)));
far.operands = operands;
assert_eq!(encode(&far), None, "imm8:'00' stops at 1020");
let mut top = strex;
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(0)));
operands.push(Operand::Reg(Reg(1)));
operands.push(Operand::Mem(offset_mem(Reg(2), 1020)));
top.operands = operands;
assert_eq!(encode(&top), Some((0xE842, 0x10FF)));
let mut strexb = decode(0xE8C2, 0x1F40, 0).unwrap();
assert_eq!(encode(&strexb), Some((0xE8C2, 0x1F40)));
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(0)));
operands.push(Operand::Reg(Reg(1)));
operands.push(Operand::Mem(offset_mem(Reg(2), 4)));
strexb.operands = operands;
assert_eq!(encode(&strexb), None);
for (mem, why) in [
(
Mem {
add: false,
..offset_mem(Reg(2), 0)
},
"`#-0` is a real and distinct addressing mode",
),
(
Mem {
mode: AddrMode::PreIndex,
..offset_mem(Reg(2), 0)
},
"there is no W bit to carry the writeback",
),
] {
let mut bad = decode(0xE8C2, 0x1F40, 0).unwrap();
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(0)));
operands.push(Operand::Reg(Reg(1)));
operands.push(Operand::Mem(mem));
bad.operands = operands;
assert_eq!(encode(&bad), None, "{why}");
}
let mut tbh = decode(0xE8D0, 0xF011, 0).unwrap();
let mut operands = Operands::new();
operands.push(Operand::Mem(Mem {
base: Reg(0),
index: Some((Reg(1), None)),
offset: 0,
add: true,
align: 0,
mode: AddrMode::Offset,
}));
tbh.operands = operands;
assert_eq!(encode(&tbh), None);
let mut tbb = decode(0xE8D0, 0xF001, 0).unwrap();
let mut operands = Operands::new();
operands.push(Operand::Mem(Mem {
base: Reg(0),
index: Some((
Reg(1),
Some(Shift {
kind: ShiftKind::Lsl,
amount: ShiftAmount::Imm(1),
}),
)),
offset: 0,
add: true,
align: 0,
mode: AddrMode::Offset,
}));
tbb.operands = operands;
assert_eq!(encode(&tbb), None);
let mut short = decode(0xE842, 0x1002, 0).unwrap();
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(1)));
operands.push(Operand::Mem(offset_mem(Reg(2), 8)));
short.operands = operands;
assert_eq!(encode(&short), None);
let mut literal = decode(0xE9DF, 0x0102, 0x1002).unwrap();
let mut operands = Operands::new();
operands.push(Operand::Reg(Reg(0)));
operands.push(Operand::Reg(Reg(1)));
operands.push(Operand::Mem(offset_mem(Reg::PC, 8)));
literal.operands = operands;
assert_eq!(encode(&literal), None);
}
#[test]
fn a_condition_from_an_it_block_does_not_change_the_halfwords() {
let mut insn = decode(0xE9D2, 0x0102, 0).unwrap();
insn.cond = Some(crate::Cond::Eq);
assert_eq!(insn.to_string(), "ldrdeq r0, r1, [r2, #8]");
assert_eq!(encode(&insn), Some((0xE9D2, 0x0102)));
}
}