use super::{AddrMode, Insn, Mem, Operand, Operands, Reg, Shift, ShiftAmount, ShiftKind, Width};
const BYTE: u16 = 0b001;
const HALF: u16 = 0b011;
const WORD: u16 = 0b101;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Form {
Literal,
Imm12,
Imm8,
Register,
Unpriv,
}
fn row(
size: u16,
signed: bool,
hint: bool,
form: Form,
) -> Option<(&'static str, &'static str, bool)> {
Some(match (size, signed, hint) {
(WORD, false, false) => match form {
Form::Literal => ("ldr", "T2", true), Form::Imm12 => ("ldr", "T3", true), Form::Imm8 => ("ldr", "T4", false), Form::Register => ("ldr", "T2", true), Form::Unpriv => ("ldrt", "T1", false), },
(BYTE, false, false) => match form {
Form::Literal => ("ldrb", "T1", false), Form::Imm12 => ("ldrb", "T2", true), Form::Imm8 => ("ldrb", "T3", false), Form::Register => ("ldrb", "T2", true), Form::Unpriv => ("ldrbt", "T1", false), },
(BYTE, true, false) => match form {
Form::Literal => ("ldrsb", "T1", false), Form::Imm12 => ("ldrsb", "T1", false), Form::Imm8 => ("ldrsb", "T2", false), Form::Register => ("ldrsb", "T2", true), Form::Unpriv => ("ldrsbt", "T1", false), },
(HALF, false, false) => match form {
Form::Literal => ("ldrh", "T1", false), Form::Imm12 => ("ldrh", "T2", true), Form::Imm8 => ("ldrh", "T3", false), Form::Register => ("ldrh", "T2", true), Form::Unpriv => ("ldrht", "T1", false), },
(HALF, true, false) => match form {
Form::Literal => ("ldrsh", "T1", false), Form::Imm12 => ("ldrsh", "T1", false), Form::Imm8 => ("ldrsh", "T2", false), Form::Register => ("ldrsh", "T2", true), Form::Unpriv => ("ldrsht", "T1", false), },
(BYTE, false, true) => match form {
Form::Literal => ("pld", "T1", false), Form::Imm12 => ("pld", "T1", false), Form::Imm8 => ("pld", "T2", false), Form::Register => ("pld", "T1", false), Form::Unpriv => return None, },
(BYTE, true, true) => match form {
Form::Literal => ("pli", "T3", false), Form::Imm12 => ("pli", "T1", false), Form::Imm8 => ("pli", "T2", false), Form::Register => ("pli", "T1", false), Form::Unpriv => return None, },
(HALF, false, true) => match form {
Form::Literal => return None, Form::Imm12 => ("pldw", "T1", false), Form::Imm8 => ("pldw", "T2", false), Form::Register => ("pldw", "T1", false), Form::Unpriv => return None, },
_ => return None,
})
}
fn classify(mnemonic: &str) -> Option<(u16, bool, bool, bool)> {
Some(match mnemonic {
"ldr" => (WORD, false, false, false),
"ldrt" => (WORD, false, false, true),
"ldrb" => (BYTE, false, false, false),
"ldrbt" => (BYTE, false, false, true),
"ldrsb" => (BYTE, true, false, false),
"ldrsbt" => (BYTE, true, false, true),
"ldrh" => (HALF, false, false, false),
"ldrht" => (HALF, false, false, true),
"ldrsh" => (HALF, true, false, false),
"ldrsht" => (HALF, true, false, true),
"pld" => (BYTE, false, true, false),
"pli" => (BYTE, true, true, false),
"pldw" => (HALF, false, true, false),
_ => return None,
})
}
fn literal_base(addr: u32) -> u32 {
addr.wrapping_add(4) & !3
}
fn hw1_of(size: u16, signed: bool, op1_low: bool, rn: Reg) -> u16 {
0xF800
| (u16::from(signed) << 8)
| (u16::from(op1_low) << 7)
| (size << 4)
| u16::from(rn.num())
}
fn imm8_hw2(rt: u16, p: bool, u: bool, w: bool, imm8: u16) -> u16 {
rt | 0x0800 | (u16::from(p) << 10) | (u16::from(u) << 9) | (u16::from(w) << 8) | imm8
}
struct Fields {
hw1: u16,
hw2: u16,
addr: u32,
size: u16,
signed: bool,
rn: Reg,
rt: Reg,
hint: bool,
}
impl Fields {
fn build(&self, form: Form, mem: Mem, target: Option<u32>) -> Option<Insn> {
let (mnemonic, encoding, explicit_width) = row(self.size, self.signed, self.hint, form)?;
let mut operands = Operands::new();
if !self.hint {
operands.push(Operand::Reg(self.rt));
}
operands.push(Operand::Mem(mem));
if let Some(t) = target {
operands.push(Operand::Target(t));
}
Some(Insn {
mnemonic,
encoding,
addr: self.addr,
width: Width::Wide,
cond: None,
sets_flags: false,
explicit_width,
operands,
})
}
fn literal(&self) -> Option<Insn> {
let mem = Mem {
base: Reg::PC,
index: None,
offset: u32::from(self.hw2 & 0x0FFF),
add: self.hw1 & 0x0080 != 0,
align: 0,
mode: AddrMode::Offset,
};
let target = literal_base(self.addr).wrapping_add(mem.displacement() as u32);
self.build(Form::Literal, mem, Some(target))
}
fn imm12(&self) -> Option<Insn> {
let mem = Mem {
base: self.rn,
index: None,
offset: u32::from(self.hw2 & 0x0FFF),
add: true,
align: 0,
mode: AddrMode::Offset,
};
self.build(Form::Imm12, mem, None)
}
fn register(&self) -> Option<Insn> {
let imm2 = ((self.hw2 >> 4) & 0b11) as u8;
let shift = if imm2 == 0 {
None
} else {
Some(Shift {
kind: ShiftKind::Lsl,
amount: ShiftAmount::Imm(imm2),
})
};
let mem = Mem {
base: self.rn,
index: Some((Reg((self.hw2 & 0xF) as u8), shift)),
offset: 0,
add: true,
align: 0,
mode: AddrMode::Offset,
};
self.build(Form::Register, mem, None)
}
fn imm8(&self) -> Option<Insn> {
let p = self.hw2 & 0x0400 != 0;
let u = self.hw2 & 0x0200 != 0;
let w = self.hw2 & 0x0100 != 0;
let imm = u32::from(self.hw2 & 0x00FF);
if !w {
if !p {
return None; }
let mem = Mem {
base: self.rn,
index: None,
offset: imm,
add: u,
align: 0,
mode: AddrMode::Offset,
};
return self.build(if u { Form::Unpriv } else { Form::Imm8 }, mem, None);
}
if self.hint {
return None;
}
let mem = Mem {
base: self.rn,
index: None,
offset: imm,
add: u,
align: 0,
mode: if p {
AddrMode::PreIndex
} else {
AddrMode::PostIndex
},
};
self.build(Form::Imm8, mem, None)
}
}
pub(crate) fn decode(hw1: u16, hw2: u16, addr: u32) -> Option<Insn> {
let size = (hw1 >> 4) & 0b111;
if hw1 >> 11 != 0b11111 || hw1 & 0x0600 != 0 || !matches!(size, BYTE | HALF | WORD) {
return None;
}
let signed = hw1 & 0x0100 != 0;
if size == WORD && signed {
return None;
}
let rt = Reg(((hw2 >> 12) & 0xF) as u8);
let f = Fields {
hw1,
hw2,
addr,
size,
signed,
rn: Reg((hw1 & 0xF) as u8),
rt,
hint: size != WORD && rt.num() == 15,
};
if f.rn.num() == 15 {
return f.literal();
}
if hw1 & 0x0080 != 0 {
return f.imm12();
}
let op2 = (hw2 >> 6) & 0b11_1111;
if op2 == 0 {
return f.register();
}
if op2 & 0b10_0000 == 0 {
return None;
}
f.imm8()
}
pub(crate) fn encode(insn: &Insn) -> Option<(u16, u16)> {
if insn.width != Width::Wide || insn.sets_flags {
return None;
}
let (size, signed, hint, unpriv) = classify(insn.mnemonic)?;
let (rt, mem, has_target) = operands_of(insn, hint)?;
let literal = mem.base.num() == 15 && mem.index.is_none();
if literal != has_target {
return None;
}
if let Some((rm, shift)) = mem.index {
if unpriv || mem.mode != AddrMode::Offset || mem.offset != 0 || !mem.add {
return None;
}
let imm2 = match shift {
None => 0,
Some(Shift {
kind: ShiftKind::Lsl,
amount: ShiftAmount::Imm(n),
}) if (1..=3).contains(&n) => u16::from(n),
_ => return None,
};
let hw2 = rt | (imm2 << 4) | u16::from(rm.num());
return verify(insn, hw1_of(size, signed, false, mem.base), hw2);
}
if literal {
if unpriv || mem.mode != AddrMode::Offset {
return None;
}
if mem.offset > 0x0FFF {
return None;
}
let hw1 = hw1_of(size, signed, mem.add, Reg::PC);
return verify(insn, hw1, rt | mem.offset as u16);
}
if unpriv {
if mem.mode != AddrMode::Offset || !mem.add || mem.offset > 255 {
return None;
}
let hw1 = hw1_of(size, signed, false, mem.base);
return verify(
insn,
hw1,
imm8_hw2(rt, true, true, false, mem.offset as u16),
);
}
let indexed = hw1_of(size, signed, false, mem.base);
let imm = mem.offset;
match mem.mode {
AddrMode::Offset if mem.add => {
if imm > 0x0FFF {
return None;
}
verify(insn, hw1_of(size, signed, true, mem.base), rt | imm as u16)
}
AddrMode::Offset => {
if imm > 0xFF {
return None;
}
verify(insn, indexed, imm8_hw2(rt, true, false, false, imm as u16))
}
AddrMode::PreIndex | AddrMode::PostIndex => {
if imm > 0xFF {
return None;
}
let p = mem.mode == AddrMode::PreIndex;
let hw2 = imm8_hw2(rt, p, mem.add, true, imm as u16);
verify(insn, indexed, hw2)
}
AddrMode::PostIncrement => None,
}
}
fn operands_of(insn: &Insn, hint: bool) -> Option<(u16, Mem, bool)> {
let (rt, at) = if hint {
(0xF000, 0)
} else {
match insn.operands.get(0)? {
Operand::Reg(r) => (u16::from(r.num()) << 12, 1),
_ => return None,
}
};
let mem = match insn.operands.get(at)? {
Operand::Mem(m) => m,
_ => return None,
};
let target = match insn.operands.get(at + 1) {
None => false,
Some(Operand::Target(_)) => true,
Some(_) => return None,
};
if insn.operands.len() != at + 1 + usize::from(target) {
return None;
}
Some((rt, mem, target))
}
fn verify(insn: &Insn, hw1: u16, hw2: u16) -> Option<(u16, u16)> {
let mut back = decode(hw1, hw2, insn.addr)?;
back.cond = insn.cond;
if back == *insn {
Some((hw1, hw2))
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::isa::Target;
const ALIGNED: u32 = 0x1000;
const UNALIGNED: u32 = 0x1002;
fn ual(hw1: u16, hw2: u16, addr: u32) -> String {
let decoded = decode(hw1, hw2, addr);
assert!(decoded.is_some(), "{hw1:#06x} {hw2:#06x} did not decode");
decoded.unwrap().to_string()
}
fn none(hw1: u16, hw2: u16) {
assert!(
decode(hw1, hw2, ALIGNED).is_none(),
"{hw1:#06x} {hw2:#06x} should not decode"
);
}
fn hw1(size: u16, signed: bool, low: bool, rn: u16) -> u16 {
0xF800 | (u16::from(signed) << 8) | (u16::from(low) << 7) | (size << 4) | rn
}
fn puw(rt: u16, bits: u16, imm8: u16) -> u16 {
(rt << 12) | 0x0800 | (bits << 8) | imm8
}
fn hw1_sweep() -> Vec<u16> {
let mut out = Vec::new();
for size in [BYTE, HALF, WORD] {
for signed in [false, true] {
for low in [false, true] {
for rn in [0, 1, 7, 13, 15] {
out.push(hw1(size, signed, low, rn));
}
}
}
}
out
}
fn hw2_sweep() -> Vec<u16> {
let mut out = Vec::new();
for rt in [0u16, 3, 13, 15] {
let rt_bits = rt << 12;
for imm12 in [0u16, 1, 4094, 4095] {
out.push(rt_bits | imm12);
}
for bits in 0..8u16 {
for imm8 in [0u16, 1, 254, 255] {
out.push(puw(rt, bits, imm8));
}
}
for imm2 in 0..4u16 {
for rm in [0u16, 2, 13, 15] {
out.push(rt_bits | (imm2 << 4) | rm);
}
}
out.push(rt_bits | 0x0400);
}
out
}
fn spells_negative_zero(hw1: u16, hw2: u16) -> bool {
let literal = hw1 & 0xF == 0xF;
if literal {
return hw1 & 0x0080 == 0 && hw2 & 0x0FFF == 0;
}
hw1 & 0x0080 == 0
&& hw2 & 0x0800 != 0
&& hw2 & 0x0100 != 0
&& hw2 & 0x0200 == 0
&& hw2 & 0x00FF == 0
}
#[test]
fn round_trips_at_both_alignments() {
let hw1s = hw1_sweep();
let hw2s = hw2_sweep();
let (mut decoded, mut rejected, mut twins) = (0u32, 0u32, 0u32);
for &addr in &[ALIGNED, UNALIGNED] {
for &hw1 in &hw1s {
for &hw2 in &hw2s {
let insn = match decode(hw1, hw2, addr) {
Some(i) => i,
None => {
rejected += 1;
continue;
}
};
decoded += 1;
assert_eq!(insn.width, Width::Wide, "{hw1:#06x} {hw2:#06x}");
assert_eq!(insn.len(), 4, "{hw1:#06x} {hw2:#06x}");
assert_eq!(insn.addr, addr, "{hw1:#06x} {hw2:#06x}");
assert!(insn.cond.is_none(), "{hw1:#06x} {hw2:#06x}");
assert!(!insn.sets_flags, "{hw1:#06x} {hw2:#06x} printed an S");
assert_eq!(
encode(&insn),
Some((hw1, hw2)),
"{hw1:#06x} {hw2:#06x} decoded to `{insn}` and did not round-trip"
);
if spells_negative_zero(hw1, hw2) {
twins += 1;
assert!(
insn.to_string().contains("#-0"),
"{hw1:#06x} {hw2:#06x} is `#-0` and must print so"
);
}
}
}
}
assert_eq!(decoded + rejected, 2 * 60 * 212);
assert_eq!((decoded, rejected), (18_032, 7_408));
assert_eq!(twins, 328);
}
#[test]
fn not_this_group() {
assert!(decode(0xF841, 0x0004, 0).is_none());
assert!(decode(0xF871, 0x0004, 0).is_none());
assert!(decode(0xFA51, 0x0004, 0).is_none());
assert!(decode(0x6800, 0, 0).is_none());
}
#[test]
fn table_a5_18_load_word() {
let imm12 = hw1(WORD, false, true, 1); let other = hw1(WORD, false, false, 1);
assert_eq!(ual(imm12, 0x0004, ALIGNED), "ldr.w r0, [r1, #4]");
assert_eq!(ual(other, puw(0, 0b111, 4), ALIGNED), "ldr r0, [r1, #4]!");
assert_eq!(ual(other, puw(0, 0b011, 4), ALIGNED), "ldr r0, [r1], #4");
assert_eq!(ual(other, puw(0, 0b100, 4), ALIGNED), "ldr r0, [r1, #-4]");
assert_eq!(ual(other, puw(0, 0b110, 4), ALIGNED), "ldrt r0, [r1, #4]");
assert_eq!(ual(other, 0x0002, ALIGNED), "ldr.w r0, [r1, r2]");
assert_eq!(ual(other, 0x0012, ALIGNED), "ldr.w r0, [r1, r2, lsl #1]");
assert_eq!(
ual(hw1(WORD, false, true, 15), 0x0004, ALIGNED),
"ldr.w r0, [pc, #4], 0x1008"
);
assert_eq!(
ual(hw1(WORD, false, false, 15), 0x0004, ALIGNED),
"ldr.w r0, [pc, #-4], 0x1000"
);
none(hw1(WORD, true, false, 1), 0x0004);
none(hw1(WORD, true, true, 1), 0x0004);
none(hw1(WORD, true, false, 15), 0x0004);
none(other, puw(0, 0b010, 4));
none(other, puw(0, 0b000, 4));
none(other, 0x0400);
assert_eq!(ual(imm12, 0xF004, ALIGNED), "ldr.w pc, [r1, #4]");
assert!(decode(imm12, 0xF004, ALIGNED).unwrap().writes_pc());
assert_eq!(ual(other, 0xF002, ALIGNED), "ldr.w pc, [r1, r2]");
assert_eq!(ual(other, puw(15, 0b110, 4), ALIGNED), "ldrt pc, [r1, #4]");
}
#[test]
fn table_a5_19_load_halfword() {
let imm12 = hw1(HALF, false, true, 1); let other = hw1(HALF, false, false, 1); let simm12 = hw1(HALF, true, true, 1); let sother = hw1(HALF, true, false, 1);
assert_eq!(
ual(hw1(HALF, false, true, 15), 0x0004, ALIGNED),
"ldrh r0, [pc, #4], 0x1008"
);
assert_eq!(
ual(hw1(HALF, false, false, 15), 0x0004, ALIGNED),
"ldrh r0, [pc, #-4], 0x1000"
);
assert_eq!(ual(other, puw(0, 0b111, 4), ALIGNED), "ldrh r0, [r1, #4]!");
assert_eq!(ual(other, puw(0, 0b011, 4), ALIGNED), "ldrh r0, [r1], #4");
assert_eq!(ual(other, puw(0, 0b100, 4), ALIGNED), "ldrh r0, [r1, #-4]");
assert_eq!(ual(imm12, 0x0004, ALIGNED), "ldrh.w r0, [r1, #4]");
assert_eq!(ual(other, 0x0002, ALIGNED), "ldrh.w r0, [r1, r2]");
assert_eq!(ual(other, puw(0, 0b110, 4), ALIGNED), "ldrht r0, [r1, #4]");
assert_eq!(ual(other, 0xF002, ALIGNED), "pldw [r1, r2]");
assert_eq!(ual(other, puw(15, 0b100, 4), ALIGNED), "pldw [r1, #-4]");
assert_eq!(ual(imm12, 0xF004, ALIGNED), "pldw [r1, #4]");
none(other, puw(15, 0b111, 4));
none(other, puw(15, 0b011, 4));
none(other, puw(15, 0b110, 4));
none(hw1(HALF, false, true, 15), 0xF004);
none(hw1(HALF, false, false, 15), 0xF004);
assert_eq!(
ual(sother, puw(0, 0b111, 4), ALIGNED),
"ldrsh r0, [r1, #4]!"
);
assert_eq!(ual(sother, puw(0, 0b011, 4), ALIGNED), "ldrsh r0, [r1], #4");
assert_eq!(
ual(sother, puw(0, 0b100, 4), ALIGNED),
"ldrsh r0, [r1, #-4]"
);
assert_eq!(ual(simm12, 0x0004, ALIGNED), "ldrsh r0, [r1, #4]");
assert_eq!(
ual(hw1(HALF, true, true, 15), 0x0004, ALIGNED),
"ldrsh r0, [pc, #4], 0x1008"
);
assert_eq!(
ual(hw1(HALF, true, false, 15), 0x0004, ALIGNED),
"ldrsh r0, [pc, #-4], 0x1000"
);
assert_eq!(ual(sother, 0x0002, ALIGNED), "ldrsh.w r0, [r1, r2]");
assert_eq!(
ual(sother, puw(0, 0b110, 4), ALIGNED),
"ldrsht r0, [r1, #4]"
);
none(sother, 0xF002);
none(sother, puw(15, 0b100, 4));
none(hw1(HALF, true, true, 15), 0xF004);
none(hw1(HALF, true, false, 15), 0xF004);
none(simm12, 0xF004);
none(sother, puw(15, 0b111, 4));
none(sother, puw(15, 0b011, 4));
none(sother, puw(15, 0b110, 4));
none(other, puw(0, 0b010, 4));
none(sother, puw(0, 0b000, 4));
none(other, 0x0400);
}
#[test]
fn table_a5_20_load_byte() {
let imm12 = hw1(BYTE, false, true, 1); let other = hw1(BYTE, false, false, 1); let simm12 = hw1(BYTE, true, true, 1); let sother = hw1(BYTE, true, false, 1);
assert_eq!(
ual(hw1(BYTE, false, true, 15), 0x0004, ALIGNED),
"ldrb r0, [pc, #4], 0x1008"
);
assert_eq!(
ual(hw1(BYTE, false, false, 15), 0x0004, ALIGNED),
"ldrb r0, [pc, #-4], 0x1000"
);
assert_eq!(ual(imm12, 0x0004, ALIGNED), "ldrb.w r0, [r1, #4]");
assert_eq!(ual(other, puw(0, 0b111, 4), ALIGNED), "ldrb r0, [r1, #4]!");
assert_eq!(ual(other, puw(0, 0b011, 4), ALIGNED), "ldrb r0, [r1], #4");
assert_eq!(ual(other, puw(0, 0b100, 4), ALIGNED), "ldrb r0, [r1, #-4]");
assert_eq!(ual(other, puw(0, 0b110, 4), ALIGNED), "ldrbt r0, [r1, #4]");
assert_eq!(ual(other, 0x0002, ALIGNED), "ldrb.w r0, [r1, r2]");
assert_eq!(
ual(hw1(BYTE, true, true, 15), 0x0004, ALIGNED),
"ldrsb r0, [pc, #4], 0x1008"
);
assert_eq!(
ual(hw1(BYTE, true, false, 15), 0x0004, ALIGNED),
"ldrsb r0, [pc, #-4], 0x1000"
);
assert_eq!(ual(simm12, 0x0004, ALIGNED), "ldrsb r0, [r1, #4]");
assert_eq!(
ual(sother, puw(0, 0b111, 4), ALIGNED),
"ldrsb r0, [r1, #4]!"
);
assert_eq!(ual(sother, puw(0, 0b011, 4), ALIGNED), "ldrsb r0, [r1], #4");
assert_eq!(
ual(sother, puw(0, 0b100, 4), ALIGNED),
"ldrsb r0, [r1, #-4]"
);
assert_eq!(
ual(sother, puw(0, 0b110, 4), ALIGNED),
"ldrsbt r0, [r1, #4]"
);
assert_eq!(ual(sother, 0x0002, ALIGNED), "ldrsb.w r0, [r1, r2]");
assert_eq!(
ual(hw1(BYTE, false, true, 15), 0xF004, ALIGNED),
"pld [pc, #4], 0x1008"
);
assert_eq!(
ual(hw1(BYTE, false, false, 15), 0xF004, ALIGNED),
"pld [pc, #-4], 0x1000"
);
assert_eq!(ual(imm12, 0xF004, ALIGNED), "pld [r1, #4]");
assert_eq!(ual(other, puw(15, 0b100, 4), ALIGNED), "pld [r1, #-4]");
assert_eq!(ual(other, 0xF002, ALIGNED), "pld [r1, r2]");
assert_eq!(ual(other, 0xF032, ALIGNED), "pld [r1, r2, lsl #3]");
none(other, puw(15, 0b111, 4));
none(other, puw(15, 0b011, 4));
none(other, puw(15, 0b110, 4));
assert_eq!(
ual(hw1(BYTE, true, true, 15), 0xF004, ALIGNED),
"pli [pc, #4], 0x1008"
);
assert_eq!(
ual(hw1(BYTE, true, false, 15), 0xF004, ALIGNED),
"pli [pc, #-4], 0x1000"
);
assert_eq!(ual(simm12, 0xF004, ALIGNED), "pli [r1, #4]");
assert_eq!(ual(sother, puw(15, 0b100, 4), ALIGNED), "pli [r1, #-4]");
assert_eq!(ual(sother, 0xF002, ALIGNED), "pli [r1, r2]");
none(sother, puw(15, 0b111, 4));
none(sother, puw(15, 0b011, 4));
none(sother, puw(15, 0b110, 4));
none(other, puw(0, 0b010, 4));
none(sother, puw(15, 0b000, 4));
none(other, 0x0400);
}
#[test]
fn signed_and_unsigned_differ_by_one_bit() {
for (size, unsigned, signed) in [(BYTE, "ldrb", "ldrsb"), (HALF, "ldrh", "ldrsh")] {
let u = hw1(size, false, true, 1);
let s = hw1(size, true, true, 1);
assert_eq!(s, u | 0x0100);
assert_eq!(decode(u, 0x0004, 0).unwrap().mnemonic, unsigned);
assert_eq!(decode(s, 0x0004, 0).unwrap().mnemonic, signed);
let u = hw1(size, false, false, 1);
let s = hw1(size, true, false, 1);
assert_eq!(decode(u, 0x0002, 0).unwrap().mnemonic, unsigned);
assert_eq!(decode(s, 0x0002, 0).unwrap().mnemonic, signed);
assert_eq!(
decode(u, puw(0, 0b111, 4), 0).unwrap().mnemonic,
unsigned,
"{unsigned} pre-indexed"
);
assert_eq!(decode(s, puw(0, 0b111, 4), 0).unwrap().mnemonic, signed);
assert_eq!(
decode(u, puw(0, 0b110, 4), 0).unwrap().mnemonic,
format!("{unsigned}t")
);
assert_eq!(
decode(s, puw(0, 0b110, 4), 0).unwrap().mnemonic,
format!("{signed}t")
);
let u = hw1(size, false, true, 15);
let s = hw1(size, true, true, 15);
assert_eq!(decode(u, 0x0004, 0).unwrap().mnemonic, unsigned);
assert_eq!(decode(s, 0x0004, 0).unwrap().mnemonic, signed);
}
let n = hw1(BYTE, false, true, 1);
assert_eq!(decode(n, 0xF004, 0).unwrap().mnemonic, "pld");
assert_eq!(decode(n | 0x0100, 0xF004, 0).unwrap().mnemonic, "pli");
}
#[test]
fn addressing_modes_print_as_ual_writes_them() {
let base = hw1(WORD, false, false, 0); assert_eq!(ual(base, puw(0, 0b100, 4), 0), "ldr r0, [r0, #-4]");
assert_eq!(
ual(hw1(WORD, false, true, 0), 0x0004, 0),
"ldr.w r0, [r0, #4]"
);
assert_eq!(ual(base, puw(0, 0b111, 4), 0), "ldr r0, [r0, #4]!");
assert_eq!(ual(base, puw(0, 0b101, 4), 0), "ldr r0, [r0, #-4]!");
assert_eq!(ual(base, puw(0, 0b011, 4), 0), "ldr r0, [r0], #4");
assert_eq!(ual(base, puw(0, 0b001, 4), 0), "ldr r0, [r0], #-4");
assert_eq!(ual(base, puw(0, 0b111, 0), 0), "ldr r0, [r0, #0]!");
assert_eq!(ual(base, puw(0, 0b101, 0), 0), "ldr r0, [r0, #-0]!");
assert_eq!(ual(base, puw(0, 0b011, 0), 0), "ldr r0, [r0], #0");
assert_eq!(ual(base, puw(0, 0b001, 0), 0), "ldr r0, [r0], #-0");
assert_eq!(ual(base, puw(0, 0b100, 0), 0), "ldr r0, [r0, #-0]");
assert_eq!(ual(hw1(WORD, false, true, 0), 0x0000, 0), "ldr.w r0, [r0]");
let expect = |hw2, add, mode| {
let m = Mem {
base: Reg(0),
index: None,
offset: 4,
add,
align: 0,
mode,
};
assert_eq!(
decode(base, hw2, 0).unwrap().operands.get(1),
Some(Operand::Mem(m)),
"{hw2:#06x}"
);
m
};
expect(puw(0, 0b100, 4), false, AddrMode::Offset);
expect(puw(0, 0b111, 4), true, AddrMode::PreIndex);
expect(puw(0, 0b101, 4), false, AddrMode::PreIndex);
expect(puw(0, 0b011, 4), true, AddrMode::PostIndex);
expect(puw(0, 0b001, 4), false, AddrMode::PostIndex);
assert_eq!(
expect(puw(0, 0b101, 4), false, AddrMode::PreIndex).displacement(),
-4
);
}
#[test]
fn offsets_are_byte_offsets_not_scaled() {
assert_eq!(
ual(hw1(WORD, false, true, 1), 0x0004, 0),
"ldr.w r0, [r1, #4]"
);
assert_eq!(
ual(hw1(HALF, false, true, 1), 0x0004, 0),
"ldrh.w r0, [r1, #4]"
);
assert_eq!(
ual(hw1(BYTE, false, true, 1), 0x0004, 0),
"ldrb.w r0, [r1, #4]"
);
assert_eq!(
ual(hw1(WORD, false, true, 1), 0x0FFF, 0),
"ldr.w r0, [r1, #4095]"
);
assert_eq!(
ual(hw1(WORD, false, false, 1), puw(0, 0b111, 255), 0),
"ldr r0, [r1, #255]!"
);
assert_eq!(
ual(hw1(WORD, false, false, 1), puw(0, 0b100, 255), 0),
"ldr r0, [r1, #-255]"
);
}
#[test]
fn register_index_shifts_only_by_lsl() {
let base = hw1(WORD, false, false, 1);
assert_eq!(ual(base, 0x0002, 0), "ldr.w r0, [r1, r2]");
for n in 1..=3u16 {
assert_eq!(
ual(base, (n << 4) | 2, 0),
format!("ldr.w r0, [r1, r2, lsl #{n}]")
);
}
assert_eq!(
decode(base, 0x0032, 0).unwrap().operands.get(1),
Some(Operand::Mem(Mem {
base: Reg(1),
index: Some((
Reg(2),
Some(Shift {
kind: ShiftKind::Lsl,
amount: ShiftAmount::Imm(3),
})
)),
offset: 0,
add: true,
align: 0,
mode: AddrMode::Offset,
}))
);
}
#[test]
fn literals_resolve_against_aligned_pc() {
let add = hw1(WORD, false, true, 15); let sub = hw1(WORD, false, false, 15); assert_eq!(
ual(add, 0x0010, ALIGNED),
"ldr.w r0, [pc, #16], 0x1014" );
assert_eq!(ual(add, 0x0010, UNALIGNED), "ldr.w r0, [pc, #16], 0x1014");
assert_eq!(
ual(sub, 0x0010, ALIGNED),
"ldr.w r0, [pc, #-16], 0xff4" );
assert_eq!(ual(sub, 0x0010, UNALIGNED), "ldr.w r0, [pc, #-16], 0xff4");
assert_eq!(ual(add, 0x0010, 0x1004), "ldr.w r0, [pc, #16], 0x1018");
assert_eq!(ual(add, 0x0010, 0x1006), "ldr.w r0, [pc, #16], 0x1018");
assert_eq!(
ual(hw1(BYTE, false, true, 15), 0x0FFF, ALIGNED),
"ldrb r0, [pc, #4095], 0x2003"
);
assert_eq!(
ual(hw1(HALF, true, false, 15), 0x0FFF, ALIGNED),
"ldrsh r0, [pc, #-4095], 0x5"
);
assert_eq!(
ual(hw1(BYTE, false, false, 15), 0xF001, UNALIGNED),
"pld [pc, #-1], 0x1003"
);
assert_eq!(
ual(hw1(BYTE, true, true, 15), 0xF001, UNALIGNED),
"pli [pc, #1], 0x1005"
);
for hw2 in [0x0000, 0x0001, 0x0FFF] {
assert_eq!(
decode(add, hw2, ALIGNED).unwrap().branch_target(),
decode(add, hw2, UNALIGNED).unwrap().branch_target()
);
}
}
#[test]
fn literal_encode_checks_the_resolved_target() {
let insn = decode(hw1(WORD, false, true, 15), 0x0010, ALIGNED).unwrap();
assert_eq!(encode(&insn), Some((hw1(WORD, false, true, 15), 0x0010)));
let mut moved = insn;
moved.addr = ALIGNED + 8;
assert_eq!(encode(&moved), None);
let mut bent = insn;
bent.operands = insn
.operands
.as_slice()
.map(|o| match o {
Operand::Target(t) => Operand::Target(t + 4),
other => other,
})
.collect();
assert_eq!(encode(&bent), None);
}
#[test]
fn encode_refuses_what_this_group_cannot_say() {
let insn = decode(hw1(WORD, false, true, 1), 0x0004, ALIGNED).unwrap();
let mut narrow = insn;
narrow.width = Width::Narrow;
assert_eq!(encode(&narrow), None);
let mut flags = insn;
flags.sets_flags = true;
assert_eq!(encode(&flags), None);
let mut store = insn;
store.mnemonic = "str";
assert_eq!(encode(&store), None);
let mut far = insn;
far.operands = [
Operand::Reg(Reg(0)),
Operand::Mem(Mem {
base: Reg(1),
index: None,
offset: 4096,
add: true,
align: 0,
mode: AddrMode::Offset,
}),
]
.into_iter()
.collect();
assert_eq!(encode(&far), None);
let mut wide_imm8 = insn;
wide_imm8.encoding = "T4";
wide_imm8.explicit_width = false;
wide_imm8.operands = [
Operand::Reg(Reg(0)),
Operand::Mem(Mem {
base: Reg(1),
index: None,
offset: 256,
add: true,
align: 0,
mode: AddrMode::PreIndex,
}),
]
.into_iter()
.collect();
assert_eq!(encode(&wide_imm8), None);
for shift in [
Shift {
kind: ShiftKind::Lsr,
amount: ShiftAmount::Imm(1),
},
Shift {
kind: ShiftKind::Lsl,
amount: ShiftAmount::Imm(4),
},
Shift {
kind: ShiftKind::Lsl,
amount: ShiftAmount::Imm(0),
},
] {
let mut bad = insn;
bad.encoding = "T2";
bad.operands = [
Operand::Reg(Reg(0)),
Operand::Mem(Mem {
base: Reg(1),
index: Some((Reg(2), Some(shift))),
offset: 0,
add: true,
align: 0,
mode: AddrMode::Offset,
}),
]
.into_iter()
.collect();
assert_eq!(encode(&bad), None);
}
let mut hinted = insn;
hinted.mnemonic = "pld";
assert_eq!(encode(&hinted), None);
let pld = decode(hw1(BYTE, false, true, 1), 0xF004, ALIGNED).unwrap();
let mut loaded = pld;
loaded.mnemonic = "ldrb";
assert_eq!(encode(&loaded), None);
}
#[test]
fn encode_refuses_operand_shapes_the_decoder_never_emits() {
fn with(template: Insn, ops: &[Operand]) -> Insn {
let mut insn = template;
insn.operands = ops.iter().copied().collect();
insn
}
fn at(base: u8, offset: u32, add: bool, mode: AddrMode) -> Operand {
Operand::Mem(Mem {
base: Reg(base),
index: None,
offset,
add,
align: 0,
mode,
})
}
fn indexed(base: u8, offset: u32, add: bool, mode: AddrMode) -> Operand {
Operand::Mem(Mem {
base: Reg(base),
index: Some((Reg(2), None)),
offset,
add,
align: 0,
mode,
})
}
let rt = Operand::Reg(Reg(0));
let imm12 = decode(hw1(WORD, false, true, 1), 0x0004, ALIGNED).unwrap();
let imm8 = decode(hw1(WORD, false, false, 1), puw(0, 0b111, 4), ALIGNED).unwrap();
let reg = decode(hw1(WORD, false, false, 1), 0x0002, ALIGNED).unwrap();
let unpriv = decode(hw1(WORD, false, false, 1), puw(0, 0b110, 4), ALIGNED).unwrap();
let literal = decode(hw1(WORD, false, true, 15), 0x0010, ALIGNED).unwrap();
let target = Operand::Target(0x1014);
assert_eq!(encode(&with(imm12, &[])), None, "no operands at all");
assert_eq!(
encode(&with(imm12, &[rt])),
None,
"a load with a destination and no address"
);
assert_eq!(
encode(&with(imm12, &[at(1, 4, true, AddrMode::Offset), rt])),
None,
"the destination comes first, then the address"
);
assert_eq!(
encode(&with(
imm12,
&[rt, at(1, 4, true, AddrMode::Offset), Operand::Imm(0)]
)),
None,
"the only operand that may follow an address is a resolved target"
);
assert_eq!(
encode(&with(
literal,
&[rt, at(15, 16, true, AddrMode::Offset), target, target]
)),
None,
"one target, not two"
);
assert_eq!(
encode(&with(
imm12,
&[rt, at(1, 4, true, AddrMode::Offset), target]
)),
None,
"only a pc base with no index resolves a target"
);
assert_eq!(
encode(&with(literal, &[rt, at(15, 16, true, AddrMode::Offset)])),
None,
"a literal load without its resolved target is not one this module built"
);
assert_eq!(
encode(&with(
literal,
&[rt, at(15, 16, true, AddrMode::PreIndex), target]
)),
None,
"there is no writeback literal: `Rn == 1111` leaves no `P`/`W`"
);
let mut literal_t: Insn = with(literal, &[rt, at(15, 16, true, AddrMode::Offset), target]);
literal_t.mnemonic = "ldrt";
assert_eq!(
encode(&literal_t),
None,
"the unprivileged row has no `Rn == 1111` encoding"
);
for (mem, why) in [
(
indexed(1, 0, true, AddrMode::PreIndex),
"a register index has no writeback encoding here",
),
(
indexed(1, 4, true, AddrMode::Offset),
"there is nowhere to put an immediate beside the index",
),
(
indexed(1, 0, false, AddrMode::Offset),
"every register index is added; there is no `U` bit",
),
] {
assert_eq!(encode(&with(reg, &[rt, mem])), None, "{why}");
}
let mut unpriv_indexed = with(reg, &[rt, indexed(1, 0, true, AddrMode::Offset)]);
unpriv_indexed.mnemonic = "ldrt";
assert_eq!(
encode(&unpriv_indexed),
None,
"the unprivileged row has no register-index encoding"
);
for (mem, why) in [
(
at(1, 4, true, AddrMode::PreIndex),
"`P`, `U` and `W` are all fixed, so there is no writeback form",
),
(
at(1, 4, false, AddrMode::Offset),
"`U == 1` is part of the row; `#-4` and `#-0` are not spellings of it",
),
(
at(1, 256, true, AddrMode::Offset),
"the field is eight bits",
),
] {
assert_eq!(encode(&with(unpriv, &[rt, mem])), None, "{why}");
}
assert_eq!(
encode(&with(imm8, &[rt, at(1, 256, false, AddrMode::Offset)])),
None,
"a subtracting plain offset is the 8-bit row, and its field is eight bits"
);
assert_eq!(
encode(&with(imm8, &[rt, at(1, 4, true, AddrMode::PostIncrement)])),
None,
"`[rn]!` with an implicit increment is Advanced SIMD's mode, not a load's"
);
let pldw = decode(hw1(HALF, false, true, 1), 0xF004, ALIGNED).unwrap();
assert_eq!(pldw.mnemonic, "pldw");
assert_eq!(
encode(&with(
pldw,
&[at(15, 16, true, AddrMode::Offset), Operand::Target(0x1014)]
)),
None,
"there is no `pldw` literal encoding to emit"
);
}
#[test]
fn negative_zero_is_a_different_instruction() {
let sub = hw1(WORD, false, false, 15);
let add = hw1(WORD, false, true, 15);
let minus = decode(sub, 0x0000, ALIGNED).unwrap();
let plus = decode(add, 0x0000, ALIGNED).unwrap();
assert_ne!(minus, plus);
assert!(minus.to_string().starts_with("ldr.w r0, [pc, #-0]"));
assert!(plus.to_string().starts_with("ldr.w r0, [pc]"));
assert_eq!(encode(&minus), Some((sub, 0x0000)));
assert_eq!(encode(&plus), Some((add, 0x0000)));
assert_eq!(minus.branch_target(), plus.branch_target());
let base = hw1(WORD, false, false, 1);
for u_bits in [0b111u16, 0b011] {
let plus = decode(base, puw(0, u_bits, 0), ALIGNED).unwrap();
let minus = decode(base, puw(0, u_bits & !0b010, 0), ALIGNED).unwrap();
assert_ne!(plus, minus, "{u_bits:03b}");
assert_eq!(encode(&plus), Some((base, puw(0, u_bits, 0))));
assert_eq!(
encode(&minus),
Some((base, puw(0, u_bits & !0b010, 0))),
"{u_bits:03b}"
);
}
let zero = decode(base, puw(0, 0b100, 0), ALIGNED).unwrap();
assert_eq!(zero.to_string(), "ldr r0, [r1, #-0]");
assert_eq!(zero.encoding, "T4");
assert_eq!(encode(&zero), Some((base, puw(0, 0b100, 0))));
let t3 = decode(hw1(WORD, false, true, 1), 0x0000, ALIGNED).unwrap();
assert_eq!(t3.to_string(), "ldr.w r0, [r1]");
assert_eq!(t3.encoding, "T3");
assert_eq!(encode(&t3), Some((hw1(WORD, false, true, 1), 0x0000)));
}
#[test]
fn round_trips_through_the_group_dispatcher() {
let cases = [
(hw1(WORD, false, true, 1), 0x0004), (hw1(WORD, false, false, 1), puw(0, 0b111, 4)), (hw1(WORD, false, false, 1), puw(0, 0b011, 4)), (hw1(WORD, false, false, 1), puw(0, 0b100, 4)), (hw1(WORD, false, false, 1), puw(0, 0b110, 4)), (hw1(WORD, false, false, 1), 0x0012), (hw1(WORD, false, true, 15), 0x0010), (hw1(BYTE, false, true, 1), 0x0004), (hw1(BYTE, true, true, 1), 0x0004), (hw1(BYTE, false, false, 1), puw(0, 0b110, 4)), (hw1(BYTE, true, false, 1), puw(0, 0b110, 4)), (hw1(HALF, false, true, 1), 0x0004), (hw1(HALF, true, true, 1), 0x0004), (hw1(HALF, false, false, 1), puw(0, 0b110, 4)), (hw1(HALF, true, false, 1), puw(0, 0b110, 4)), (hw1(HALF, true, false, 15), 0x0010), (hw1(BYTE, false, true, 1), 0xF004), (hw1(BYTE, false, false, 1), 0xF002), (hw1(BYTE, false, true, 15), 0xF010), (hw1(BYTE, true, true, 1), 0xF004), (hw1(BYTE, true, false, 1), 0xF002), (hw1(BYTE, true, false, 15), 0xF010), (hw1(HALF, false, true, 1), 0xF004), (hw1(HALF, false, false, 1), 0xF002), ];
for &addr in &[ALIGNED, UNALIGNED] {
for (hw1, hw2) in cases {
let mine = decode(hw1, hw2, addr).expect("this module decodes it");
let theirs = crate::isa::decode_halfwords(hw1, hw2, addr, Target::Union);
assert_eq!(theirs, Some(mine), "dispatch of {hw1:#06x} {hw2:#06x}");
assert_eq!(
crate::isa::encode(&mine),
Some((hw1, hw2)),
"crate-level re-encode of {hw1:#06x} {hw2:#06x}"
);
}
}
}
#[test]
fn pop_alias_is_decoded_as_the_load_it_encodes() {
let insn = decode(hw1(WORD, false, false, 13), puw(3, 0b011, 4), 0).unwrap();
assert_eq!(insn.to_string(), "ldr r3, [sp], #4");
assert_eq!(insn.mnemonic, "ldr");
assert_eq!(encode(&insn), Some((hw1(WORD, false, false, 13), 0x3B04)));
}
}