use super::{Insn, Operand, Reg, Width};
pub(crate) fn decode(hw1: u16, hw2: u16, addr: u32) -> Option<Insn> {
if hw1 >> 11 != 0b11101 || (hw1 >> 9) & 0b11 != 0 || (hw1 >> 6) & 1 != 0 {
return None;
}
let op = (hw1 >> 7) & 0b11;
let wback = (hw1 >> 5) & 1 == 1;
let load = (hw1 >> 4) & 1 == 1;
let rn = Reg((hw1 & 0xF) as u8);
if op == 0b00 || op == 0b11 {
let increment = op == 0b11;
let encoding = if increment { "T2" } else { "T1" };
if load {
if hw2 != 0xC000 {
return None;
}
let mnemonic = if increment { "rfeia" } else { "rfedb" };
return Some(wide(mnemonic, encoding, addr, false, &[base(rn, wback)]));
}
if rn.num() != 13 || hw2 & 0xFFE0 != 0xC000 {
return None;
}
let mnemonic = if increment { "srsia" } else { "srsdb" };
return Some(wide(
mnemonic,
encoding,
addr,
false,
&[base(Reg::SP, wback), Operand::Imm(i64::from(hw2 & 0x1F))],
));
}
if hw2 & 0x2000 != 0 || (!load && hw2 & 0x8000 != 0) {
return None;
}
if !has_two_registers(hw2) {
return None;
}
let list = Operand::RegList(hw2);
let decrement = op == 0b10;
if wback && rn.num() == 13 {
if load && !decrement {
return Some(wide("pop", "T2", addr, true, &[list]));
}
if !load && decrement {
return Some(wide("push", "T2", addr, true, &[list]));
}
}
let (mnemonic, encoding, explicit_width) = match (decrement, load) {
(false, false) => ("stm", "T2", true),
(false, true) => ("ldm", "T2", true),
(true, false) => ("stmdb", "T1", false),
(true, true) => ("ldmdb", "T1", false),
};
Some(wide(
mnemonic,
encoding,
addr,
explicit_width,
&[base(rn, wback), list],
))
}
pub(crate) fn encode(insn: &Insn) -> Option<(u16, u16)> {
if insn.width != Width::Wide || insn.sets_flags {
return None;
}
match insn.mnemonic {
"push" | "pop" => {
if insn.encoding != "T2" {
return None;
}
let bits = match insn.operands.get(0) {
Some(Operand::RegList(b)) => b,
_ => return None,
};
if insn.operands.len() > 1 {
return None;
}
if bits & 0x2000 != 0 || !has_two_registers(bits) {
return None;
}
if insn.mnemonic == "pop" {
Some((0xE8BD, bits))
} else if bits & 0x8000 != 0 {
None
} else {
Some((0xE92D, bits))
}
}
"stm" | "ldm" | "stmdb" | "ldmdb" => {
let (rn, wback) = base_of(insn.operands.get(0)?)?;
let bits = match insn.operands.get(1) {
Some(Operand::RegList(b)) => b,
_ => return None,
};
if insn.operands.len() > 2 {
return None;
}
let (op, load, encoding) = match insn.mnemonic {
"stm" => (0b01u16, false, "T2"),
"ldm" => (0b01, true, "T2"),
"stmdb" => (0b10, false, "T1"),
_ => (0b10, true, "T1"),
};
if insn.encoding != encoding
|| bits & 0x2000 != 0
|| (!load && bits & 0x8000 != 0)
|| !has_two_registers(bits)
|| (wback && rn.num() == 13 && (load == (op == 0b01)))
{
return None;
}
Some((assemble_hw1(op, wback, load, rn), bits))
}
"srsdb" | "srsia" => {
let increment = insn.mnemonic == "srsia";
if insn.encoding != encoding_name(increment) {
return None;
}
let (rn, wback) = base_of(insn.operands.get(0)?)?;
let mode = match insn.operands.get(1) {
Some(Operand::Imm(m)) if (0..32).contains(&m) => m as u16,
_ => return None,
};
if rn.num() != 13 || insn.operands.len() > 2 {
return None;
}
let op = if increment { 0b11 } else { 0b00 };
Some((assemble_hw1(op, wback, false, rn), 0xC000 | mode))
}
"rfedb" | "rfeia" => {
let increment = insn.mnemonic == "rfeia";
if insn.encoding != encoding_name(increment) {
return None;
}
let (rn, wback) = base_of(insn.operands.get(0)?)?;
if insn.operands.len() > 1 {
return None;
}
let op = if increment { 0b11 } else { 0b00 };
Some((assemble_hw1(op, wback, true, rn), 0xC000))
}
_ => None,
}
}
fn has_two_registers(list: u16) -> bool {
list.count_ones() >= 2
}
const WRITEBACK: [&str; 16] = [
"r0!", "r1!", "r2!", "r3!", "r4!", "r5!", "r6!", "r7!", "r8!", "r9!", "r10!", "r11!", "r12!",
"sp!", "lr!", "pc!",
];
fn base(rn: Reg, wback: bool) -> Operand {
if wback {
Operand::Text(WRITEBACK[rn.num() as usize])
} else {
Operand::Reg(rn)
}
}
fn base_of(op: Operand) -> Option<(Reg, bool)> {
match op {
Operand::Reg(r) => Some((r, false)),
Operand::Text(s) => WRITEBACK
.iter()
.position(|&w| w == s)
.map(|i| (Reg(i as u8), true)),
_ => None,
}
}
fn assemble_hw1(op: u16, wback: bool, load: bool, rn: Reg) -> u16 {
0xE800 | (op << 7) | (u16::from(wback) << 5) | (u16::from(load) << 4) | u16::from(rn.num())
}
fn encoding_name(increment: bool) -> &'static str {
if increment {
"T2"
} else {
"T1"
}
}
fn wide(
mnemonic: &'static str,
encoding: &'static str,
addr: u32,
explicit_width: bool,
ops: &[Operand],
) -> Insn {
Insn {
mnemonic,
encoding,
addr,
width: Width::Wide,
cond: None,
sets_flags: false,
explicit_width,
operands: ops.iter().copied().collect(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::isa::Target;
fn dec(hw1: u16, hw2: u16) -> Insn {
let decoded = decode(hw1, hw2, 0x1000);
assert!(decoded.is_some(), "{hw1:#06x} {hw2:#06x} failed to decode");
decoded.unwrap()
}
fn check(hw1: u16, hw2: u16, text: &str) -> Insn {
let insn = dec(hw1, hw2);
assert_eq!(insn.to_string(), text, "{hw1:#06x} {hw2:#06x}");
assert_eq!(
encode(&insn),
Some((hw1, hw2)),
"`{insn}` did not re-encode to {hw1:#06x} {hw2:#06x}"
);
assert!(!insn.sets_flags, "nothing in this group sets flags");
insn
}
fn is_defined(hw1: u16, hw2: u16) -> bool {
let op = (hw1 >> 7) & 0b11;
let load = (hw1 >> 4) & 1 == 1;
let rn = hw1 & 0xF;
match op {
0b01 | 0b10 => {
hw2 & 0x2000 == 0 && (load || hw2 & 0x8000 == 0) && hw2.count_ones() >= 2
}
_ if load => hw2 == 0xC000,
_ => rn == 13 && hw2 & 0xFFE0 == 0xC000,
}
}
#[test]
fn systematic_round_trip() {
let lists = [
0x0000u16, 0x0001, 0x0003, 0x0007, 0x00FF, 0x0010, 0x1FFF, 0x2000, 0x4000, 0x8000,
0xC000, 0xA000, 0x6000, 0xE000, 0xFFFF, 0x5FFF, 0x9FFF, 0xC010, 0xC01F, 0xC020, 0x400D,
0x000D,
];
let mut decoded = 0;
let mut undefined = 0;
for op in 0u16..4 {
for w in 0u16..2 {
for l in 0u16..2 {
for rn in 0u16..16 {
let hw1 = 0xE800 | (op << 7) | (w << 5) | (l << 4) | rn;
for &hw2 in &lists {
if let Some(insn) = decode(hw1, hw2, 0x2000) {
assert!(is_defined(hw1, hw2), "{hw1:#06x} {hw2:#06x} is UNDEFINED");
decoded += 1;
assert_eq!(
encode(&insn),
Some((hw1, hw2)),
"{hw1:#06x} {hw2:#06x} re-encoded wrongly as `{insn}`"
);
} else {
assert!(!is_defined(hw1, hw2), "{hw1:#06x} {hw2:#06x} is defined");
undefined += 1;
}
}
}
}
}
}
assert_eq!(lists.len(), 22);
assert_eq!(decoded + undefined, 256 * lists.len());
assert_eq!(
decoded,
1216 + 64 + 12,
"unexpected number of defined encodings"
);
assert_eq!(undefined, 5632 - 1292);
}
#[test]
fn table_rows() {
let i = check(0xE8A0, 0x0006, "stm.w r0!, {r1, r2}");
assert_eq!((i.mnemonic, i.encoding), ("stm", "T2"));
check(0xE880, 0x0006, "stm.w r0, {r1, r2}");
let i = check(0xE8B0, 0x0006, "ldm.w r0!, {r1, r2}");
assert_eq!((i.mnemonic, i.encoding), ("ldm", "T2"));
let i = check(0xE8BD, 0x8010, "pop.w {r4, pc}");
assert_eq!((i.mnemonic, i.encoding), ("pop", "T2"));
let i = check(0xE920, 0x0030, "stmdb r0!, {r4, r5}");
assert_eq!((i.mnemonic, i.encoding), ("stmdb", "T1"));
let i = check(0xE92D, 0x4010, "push.w {r4, lr}");
assert_eq!((i.mnemonic, i.encoding), ("push", "T2"));
let i = check(0xE930, 0x0030, "ldmdb r0!, {r4, r5}");
assert_eq!((i.mnemonic, i.encoding), ("ldmdb", "T1"));
let i = check(0xE82D, 0xC011, "srsdb sp!, #0x11");
assert_eq!((i.mnemonic, i.encoding), ("srsdb", "T1"));
check(0xE80D, 0xC011, "srsdb sp, #0x11");
let i = check(0xE830, 0xC000, "rfedb r0!");
assert_eq!((i.mnemonic, i.encoding), ("rfedb", "T1"));
check(0xE810, 0xC000, "rfedb r0");
let i = check(0xE9AD, 0xC011, "srsia sp!, #0x11");
assert_eq!((i.mnemonic, i.encoding), ("srsia", "T2"));
let i = check(0xE9B0, 0xC000, "rfeia r0!");
assert_eq!((i.mnemonic, i.encoding), ("rfeia", "T2"));
}
#[test]
fn writeback_to_sp_is_push_and_pop() {
let pop = dec(0xE8BD, 0x4030);
assert_eq!(pop.mnemonic, "pop");
assert_eq!(pop.to_string(), "pop.w {r4, r5, lr}");
assert_eq!(pop.operands.len(), 1, "a pop has no base operand");
let push = dec(0xE92D, 0x4030);
assert_eq!(push.mnemonic, "push");
assert_eq!(push.to_string(), "push.w {r4, r5, lr}");
assert_eq!(push.operands.len(), 1);
assert_eq!(dec(0xE89D, 0x4030).to_string(), "ldm.w sp, {r4, r5, lr}");
assert_eq!(dec(0xE90D, 0x4030).to_string(), "stmdb sp, {r4, r5, lr}");
assert_eq!(dec(0xE8AD, 0x4030).to_string(), "stm.w sp!, {r4, r5, lr}");
assert_eq!(dec(0xE93D, 0x4030).to_string(), "ldmdb sp!, {r4, r5, lr}");
assert_eq!(dec(0xE8BC, 0x4030).to_string(), "ldm.w r12!, {r4, r5, lr}");
assert_eq!(dec(0xE92C, 0x4030).to_string(), "stmdb r12!, {r4, r5, lr}");
let mut long = dec(0xE8BC, 0x4030);
long.operands = [Operand::Text("sp!"), Operand::RegList(0x4030)]
.iter()
.copied()
.collect();
assert_eq!(encode(&long), None, "`ldm sp!, {{…}}` is `pop`'s encoding");
}
#[test]
fn pop_with_pc_writes_pc() {
let with_pc = dec(0xE8BD, 0x8010);
assert!(with_pc.writes_pc(), "pop.w {{r4, pc}}");
assert!(with_pc.is_branch());
assert!(!with_pc.is_call());
let without = dec(0xE8BD, 0x4010);
assert!(!without.writes_pc(), "pop.w {{r4, lr}}");
assert!(!without.is_branch());
assert!(dec(0xE8B0, 0x8010).writes_pc(), "ldm.w r0!, {{r4, pc}}");
assert!(dec(0xE930, 0x8010).writes_pc(), "ldmdb r0!, {{r4, pc}}");
assert!(!dec(0xE8B0, 0x4010).writes_pc());
assert!(!dec(0xE92D, 0x4010).writes_pc());
assert!(decode(0xE92D, 0x8010, 0).is_none());
}
#[test]
fn unpredictable_rules() {
for hw1 in [0xE880u16, 0xE8B0, 0xE920, 0xE930, 0xE8BD, 0xE92D] {
assert!(
decode(hw1, 0x2010, 0).is_none(),
"{hw1:#06x}: sp in the list"
);
}
assert!(decode(0xE880, 0x8010, 0).is_none(), "stm with pc");
assert!(decode(0xE920, 0x8010, 0).is_none(), "stmdb with pc");
assert!(decode(0xE92D, 0x8010, 0).is_none(), "push with pc");
assert_eq!(dec(0xE890, 0x8010).to_string(), "ldm.w r0, {r4, pc}");
assert_eq!(dec(0xE8BD, 0xC010).to_string(), "pop.w {r4, lr, pc}");
assert_eq!(dec(0xE8B0, 0xC010).to_string(), "ldm.w r0!, {r4, lr, pc}");
assert_eq!(dec(0xE88F, 0x0006).to_string(), "stm.w pc, {r1, r2}");
assert_eq!(dec(0xE83F, 0xC000).to_string(), "rfedb pc!");
assert_eq!(dec(0xE8A0, 0x0007).to_string(), "stm.w r0!, {r0-r2}");
assert_eq!(dec(0xE8B0, 0x0007).to_string(), "ldm.w r0!, {r0-r2}");
}
#[test]
fn a_wide_list_needs_two_registers() {
let forms: [(u16, &str); 6] = [
(0xE880, "stm.w"),
(0xE8B0, "ldm.w"),
(0xE920, "stmdb"),
(0xE930, "ldmdb"),
(0xE8BD, "pop.w"),
(0xE92D, "push.w"),
];
for (hw1, mnemonic) in forms {
assert!(decode(hw1, 0x0000, 0).is_none(), "{mnemonic} {{}}");
for one in [0x0001u16, 0x0010, 0x4000] {
assert!(
decode(hw1, one, 0).is_none(),
"{mnemonic} of one register, list {one:#06x}"
);
}
let two = dec(hw1, 0x0011);
assert!(
two.to_string().starts_with(mnemonic),
"{two} should be a {mnemonic}"
);
assert_eq!(encode(&two), Some((hw1, 0x0011)));
}
assert!(decode(0xE8BD, 0x8000, 0).is_none(), "pop.w {{pc}}");
assert!(decode(0xE8B0, 0x8000, 0).is_none(), "ldm.w r0!, {{pc}}");
let stm = dec(0xE880, 0x0011);
for short in [0x0000u16, 0x0001, 0x4000] {
let mut mangled = stm;
mangled.operands = [Operand::Reg(Reg(0)), Operand::RegList(short)]
.iter()
.copied()
.collect();
assert_eq!(encode(&mangled), None, "stm of list {short:#06x}");
}
let push = dec(0xE92D, 0x0011);
for short in [0x0000u16, 0x0001, 0x4000] {
let mut mangled = push;
mangled.operands = [Operand::RegList(short)].iter().copied().collect();
assert_eq!(encode(&mangled), None, "push of list {short:#06x}");
}
}
#[test]
fn decode_refuses_halfwords_outside_the_group() {
for hw1 in [0x0000u16, 0x4770, 0xE700, 0xEA00, 0xF000] {
assert!(decode(hw1, 0x0011, 0).is_none(), "{hw1:#06x}");
}
assert!(decode(0xE860, 0x0011, 0).is_none());
assert!(decode(0xE8C0, 0x0011, 0).is_none());
assert!(decode(0xE8C0 | 0x0040, 0x0011, 0).is_none());
assert!(decode(0xE8A0 | 0x0040, 0x0011, 0).is_none());
for (hw1, what) in [(0xEA90u16, "eors.w"), (0xF090, "eors")] {
assert!(
decode(hw1, 0x0006, 0).is_none(),
"{hw1:#06x} is `{what}`, not a load/store-multiple"
);
}
assert_eq!(dec(0xE890, 0x0006).to_string(), "ldm.w r0, {r1, r2}");
}
#[test]
fn srs_and_rfe_fixed_fields() {
assert!(
decode(0xE830, 0xC001, 0).is_none(),
"rfe hw2 must be 0xc000"
);
assert!(decode(0xE830, 0x0000, 0).is_none());
assert!(decode(0xE830, 0xE000, 0).is_none());
assert!(decode(0xE82D, 0xC020, 0).is_none(), "srs hw2 bit 5");
assert!(decode(0xE82C, 0xC011, 0).is_none(), "srs base must be sp");
for mode in 0u16..32 {
let i = dec(0xE82D, 0xC000 | mode);
assert_eq!(i.operands.get(1), Some(Operand::Imm(i64::from(mode))));
assert_eq!(encode(&i), Some((0xE82D, 0xC000 | mode)));
}
assert!(dec(0xE830, 0xC000).is_branch());
assert!(dec(0xE830, 0xC000).writes_pc());
assert!(!dec(0xE830, 0xC000).is_call());
assert!(!dec(0xE82D, 0xC011).is_branch());
assert!(!dec(0xE82D, 0xC011).writes_pc());
}
#[test]
fn encode_rejects_foreign_forms() {
let i = dec(0xE8A0, 0x0006);
let mut narrow = i;
narrow.width = Width::Narrow;
assert_eq!(encode(&narrow), None);
let mut flags = i;
flags.sets_flags = true;
assert_eq!(encode(&flags), None);
let mut foreign = i;
foreign.mnemonic = "ldr";
assert_eq!(encode(&foreign), None);
let mut wrong_enc = i;
wrong_enc.encoding = "T1";
assert_eq!(encode(&wrong_enc), None, "`stm` has no T1 in this group");
let mut srs = dec(0xE82D, 0xC011);
srs.encoding = "T2";
assert_eq!(encode(&srs), None, "`srsdb` is T1; T2 is `srsia`");
let mut rfe = dec(0xE830, 0xC000);
rfe.encoding = "T2";
assert_eq!(encode(&rfe), None, "`rfedb` is T1; T2 is `rfeia`");
let mut with_sp = i;
with_sp.operands = [Operand::Text("r0!"), Operand::RegList(0x2006)]
.iter()
.copied()
.collect();
assert_eq!(encode(&with_sp), None);
let mut store_pc = i;
store_pc.operands = [Operand::Text("r0!"), Operand::RegList(0x8006)]
.iter()
.copied()
.collect();
assert_eq!(encode(&store_pc), None);
let mut load_pc = dec(0xE8B0, 0x0006);
load_pc.operands = [Operand::Text("r0!"), Operand::RegList(0x8006)]
.iter()
.copied()
.collect();
assert_eq!(encode(&load_pc), Some((0xE8B0, 0x8006)), "a load may");
let mut odd = i;
odd.operands = [Operand::Imm(0), Operand::RegList(0x0006)]
.iter()
.copied()
.collect();
assert_eq!(encode(&odd), None);
let mut mode = dec(0xE82D, 0xC011);
mode.operands = [Operand::Text("sp!"), Operand::Imm(32)]
.iter()
.copied()
.collect();
assert_eq!(encode(&mode), None);
}
#[test]
fn encode_rejects_wrong_operand_counts_and_shapes() {
fn with(template: Insn, ops: &[Operand]) -> Insn {
let mut insn = template;
insn.operands = ops.iter().copied().collect();
insn
}
let list = Operand::RegList(0x0011);
let push = dec(0xE92D, 0x0011);
let stm = dec(0xE880, 0x0011);
let srs = dec(0xE82D, 0xC011);
let rfe = dec(0xE830, 0xC000);
assert_eq!(encode(&with(push, &[])), None, "push with no operands");
assert_eq!(
encode(&with(push, &[list, Operand::Reg(Reg::SP)])),
None,
"push with a spare operand"
);
assert_eq!(
encode(&with(push, &[Operand::Reg(Reg(0))])),
None,
"push of a register, not a list"
);
assert_eq!(encode(&with(push, &[Operand::RegList(0x2011)])), None);
assert_eq!(encode(&with(push, &[Operand::RegList(0x8011)])), None);
let pop = dec(0xE8BD, 0x0011);
assert_eq!(
encode(&with(pop, &[Operand::RegList(0x8011)])),
Some((0xE8BD, 0x8011))
);
let mut narrow_encoding = push;
narrow_encoding.encoding = "T3";
assert_eq!(
encode(&narrow_encoding),
None,
"`push` T3 is not this group"
);
assert_eq!(encode(&with(stm, &[])), None, "stm with no operands");
assert_eq!(
encode(&with(stm, &[Operand::Reg(Reg(0))])),
None,
"stm with no list"
);
assert_eq!(
encode(&with(stm, &[Operand::Reg(Reg(0)), list, Operand::Imm(0)])),
None,
"stm with a spare operand"
);
assert_eq!(encode(&with(srs, &[])), None, "srs with no operands");
assert_eq!(
encode(&with(srs, &[Operand::Text("sp"), Operand::Imm(0x11)])),
None,
"`\"sp\"` is not a writeback marker, and `Reg` is how a bare base is spelt"
);
assert_eq!(
encode(&with(
srs,
&[Operand::Text("sp!"), Operand::Imm(0x11), Operand::Imm(0)]
)),
None,
"srs with a spare operand"
);
assert_eq!(encode(&with(rfe, &[])), None, "rfe with no operands");
assert_eq!(
encode(&with(rfe, &[Operand::Imm(0)])),
None,
"rfe whose base is not a register"
);
assert_eq!(
encode(&with(rfe, &[Operand::Text("r0!"), Operand::Imm(0)])),
None,
"rfe with a spare operand"
);
}
#[test]
fn reachable_through_the_dispatcher() {
for (hw1, hw2, text) in [
(0xE92Du16, 0x4010u16, "push.w {r4, lr}"),
(0xE8BD, 0x8010, "pop.w {r4, pc}"),
(0xE8A0, 0x0006, "stm.w r0!, {r1, r2}"),
(0xE930, 0x0030, "ldmdb r0!, {r4, r5}"),
(0xE82D, 0xC011, "srsdb sp!, #0x11"),
(0xE9B0, 0xC000, "rfeia r0!"),
] {
let routed = super::super::decode_halfwords(hw1, hw2, 0x1000, Target::Union);
assert!(
routed.is_some(),
"{hw1:#06x} {hw2:#06x} did not reach this module"
);
let insn = routed.unwrap();
assert_eq!(insn.to_string(), text);
assert_eq!(insn.width, Width::Wide);
}
}
#[test]
fn writeback_operand() {
let with = dec(0xE8A0, 0x0006);
assert_eq!(with.operands.get(0), Some(Operand::Text("r0!")));
let without = dec(0xE880, 0x0006);
assert_eq!(without.operands.get(0), Some(Operand::Reg(Reg(0))));
assert_ne!(with, without, "the `!` is not cosmetic");
for n in 0u16..16 {
let hw1 = 0xE880 | (1 << 5) | n;
let insn = dec(hw1, 0x0006);
assert_eq!(
base_of(insn.operands.get(0).unwrap()),
Some((Reg(n as u8), true))
);
assert_eq!(encode(&insn), Some((hw1, 0x0006)));
let insn = dec(0xE880 | n, 0x0006);
assert_eq!(
base_of(insn.operands.get(0).unwrap()),
Some((Reg(n as u8), false))
);
}
}
}