use super::{Insn, Operand, Operands, Reg, Width};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Form {
Binary,
Test,
Move,
}
impl Form {
fn arity(self) -> usize {
match self {
Form::Binary => 3,
Form::Test | Form::Move => 2,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Row {
mnemonic: &'static str,
encoding: &'static str,
form: Form,
sets_flags: bool,
explicit_width: bool,
}
fn resolve(op: u16, s: bool, rn: u16, rd: u16) -> Option<Row> {
fn plain(mnemonic: &'static str, s: bool) -> Row {
Row {
mnemonic,
encoding: "T1",
form: Form::Binary,
sets_flags: s,
explicit_width: false,
}
}
fn test(mnemonic: &'static str, encoding: &'static str, explicit_width: bool) -> Row {
Row {
mnemonic,
encoding,
form: Form::Test,
sets_flags: false,
explicit_width,
}
}
let alias_rd = rd == 0b1111 && s;
Some(match op {
0b0000 if alias_rd => test("tst", "T1", false),
0b0000 => plain("and", s),
0b0001 => plain("bic", s),
0b0010 if rn == 0b1111 => Row {
mnemonic: "mov",
encoding: "T2",
form: Form::Move,
sets_flags: s,
explicit_width: true,
},
0b0010 => plain("orr", s),
0b0011 if rn == 0b1111 => Row {
mnemonic: "mvn",
encoding: "T1",
form: Form::Move,
sets_flags: s,
explicit_width: false,
},
0b0011 => plain("orn", s),
0b0100 if alias_rd => test("teq", "T1", false),
0b0100 => plain("eor", s),
0b1000 if alias_rd => test("cmn", "T1", false),
0b1000 => Row {
mnemonic: "add",
encoding: "T3",
form: Form::Binary,
sets_flags: s,
explicit_width: true,
},
0b1010 => plain("adc", s),
0b1011 => plain("sbc", s),
0b1101 if alias_rd => test("cmp", "T2", true),
0b1101 => Row {
mnemonic: "sub",
encoding: if rn == 0b1101 { "T2" } else { "T3" },
form: Form::Binary,
sets_flags: s,
explicit_width: true,
},
0b1110 => Row {
mnemonic: "rsb",
encoding: "T2",
form: Form::Binary,
sets_flags: s,
explicit_width: true,
},
_ => return None,
})
}
fn thumb_expand_imm(imm12: u16) -> Option<u32> {
let imm12 = imm12 & 0x0FFF;
let imm8 = u32::from(imm12 & 0x00FF);
if (imm12 & 0b1100_0000_0000) != 0 {
let unrotated = 0x80 | u32::from(imm12 & 0x7F);
return Some(unrotated.rotate_right(u32::from(imm12 >> 7)));
}
match (imm12 >> 8) & 0b11 {
0b00 => Some(imm8),
0b01 => {
if imm8 == 0 {
None
} else {
Some((imm8 << 16) | imm8)
}
}
0b10 => {
if imm8 == 0 {
None
} else {
Some((imm8 << 24) | (imm8 << 8))
}
}
_ => {
if imm8 == 0 {
None
} else {
Some((imm8 << 24) | (imm8 << 16) | (imm8 << 8) | imm8)
}
}
}
}
fn encode_modified_imm(value: u32) -> Option<u16> {
if value <= 0xFF {
return Some(value as u16);
}
let byte0 = value & 0xFF;
let byte1 = (value >> 8) & 0xFF;
if byte0 != 0 && value == (byte0 << 16) | byte0 {
return Some(0x100 | byte0 as u16);
}
if byte1 != 0 && value == (byte1 << 24) | (byte1 << 8) {
return Some(0x200 | byte1 as u16);
}
if byte0 != 0 && value == (byte0 << 24) | (byte0 << 16) | (byte0 << 8) | byte0 {
return Some(0x300 | byte0 as u16);
}
let s = 31 - value.leading_zeros() - 7;
if value & ((1 << s) - 1) != 0 {
return None;
}
let unrotated = value >> s;
Some((((32 - s) as u16) << 7) | ((unrotated & 0x7F) as u16))
}
pub(crate) fn decode(hw1: u16, hw2: u16, addr: u32) -> Option<Insn> {
if hw1 >> 11 != 0b11110 || (hw1 & (1 << 9)) != 0 || (hw2 & (1 << 15)) != 0 {
return None;
}
let op = (hw1 >> 5) & 0xF;
let s = (hw1 & (1 << 4)) != 0;
let rn = hw1 & 0xF;
let rd = (hw2 >> 8) & 0xF;
let imm12 = ((hw1 >> 10) & 1) << 11 | ((hw2 >> 12) & 0b111) << 8 | (hw2 & 0xFF);
let row = resolve(op, s, rn, rd)?;
let imm32 = thumb_expand_imm(imm12)?;
let mut operands = Operands::new();
match row.form {
Form::Binary => {
operands.push(Operand::Reg(Reg(rd as u8)));
operands.push(Operand::Reg(Reg(rn as u8)));
}
Form::Test => operands.push(Operand::Reg(Reg(rn as u8))),
Form::Move => operands.push(Operand::Reg(Reg(rd as u8))),
}
operands.push(Operand::Imm(i64::from(imm32)));
Some(Insn {
mnemonic: row.mnemonic,
encoding: row.encoding,
addr,
width: Width::Wide,
cond: None,
sets_flags: row.sets_flags,
explicit_width: row.explicit_width,
operands,
})
}
pub(crate) fn encode(insn: &Insn) -> Option<(u16, u16)> {
if insn.width != Width::Wide {
return None;
}
let (op, form) = match insn.mnemonic {
"and" => (0b0000, Form::Binary),
"tst" => (0b0000, Form::Test),
"bic" => (0b0001, Form::Binary),
"orr" => (0b0010, Form::Binary),
"mov" => (0b0010, Form::Move),
"orn" => (0b0011, Form::Binary),
"mvn" => (0b0011, Form::Move),
"eor" => (0b0100, Form::Binary),
"teq" => (0b0100, Form::Test),
"add" => (0b1000, Form::Binary),
"cmn" => (0b1000, Form::Test),
"adc" => (0b1010, Form::Binary),
"sbc" => (0b1011, Form::Binary),
"sub" => (0b1101, Form::Binary),
"cmp" => (0b1101, Form::Test),
"rsb" => (0b1110, Form::Binary),
_ => return None,
};
if insn.operands.len() != form.arity() {
return None;
}
let (rd, rn, imm) = match (form, insn.operands.get(0), insn.operands.get(1)) {
(Form::Binary, Some(Operand::Reg(d)), Some(Operand::Reg(n))) => {
match insn.operands.get(2) {
Some(Operand::Imm(v)) => (u16::from(d.num()), u16::from(n.num()), v),
_ => return None,
}
}
(Form::Test, Some(Operand::Reg(n)), Some(Operand::Imm(v))) => {
(0b1111, u16::from(n.num()), v)
}
(Form::Move, Some(Operand::Reg(d)), Some(Operand::Imm(v))) => {
(u16::from(d.num()), 0b1111, v)
}
_ => return None,
};
let s = form == Form::Test || insn.sets_flags;
let bits = if (0..=0xFFFF_FFFF).contains(&imm) {
imm as u32
} else {
return None;
};
let imm12 = encode_modified_imm(bits)?;
let hw1 = (0b11110 << 11) | ((imm12 >> 11) << 10) | (op << 5) | (u16::from(s) << 4) | rn;
let hw2 = ((imm12 >> 8) & 0b111) << 12 | (rd << 8) | (imm12 & 0xFF);
let faithful = decode(hw1, hw2, insn.addr).map_or(false, |back| {
back.mnemonic == insn.mnemonic
&& back.encoding == insn.encoding
&& back.width == insn.width
&& back.sets_flags == insn.sets_flags
&& back.explicit_width == insn.explicit_width
&& back.operands == insn.operands
});
if faithful {
Some((hw1, hw2))
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
fn bits(op: u16, s: bool, rn: u16, rd: u16, imm12: u16) -> (u16, u16) {
let hw1 = (0b11110 << 11) | ((imm12 >> 11) << 10) | (op << 5) | (u16::from(s) << 4) | rn;
let hw2 = ((imm12 >> 8) & 0b111) << 12 | (rd << 8) | (imm12 & 0xFF);
(hw1, hw2)
}
fn dec(op: u16, s: bool, rn: u16, rd: u16, imm12: u16) -> Insn {
let (hw1, hw2) = bits(op, s, rn, rd, imm12);
decode(hw1, hw2, 0x1000).expect("should decode")
}
#[test]
fn thumb_expand_imm_table_a5_11() {
assert_eq!(thumb_expand_imm(0x000), Some(0x0000_0000));
assert_eq!(thumb_expand_imm(0x05A), Some(0x0000_005A));
assert_eq!(thumb_expand_imm(0x0FF), Some(0x0000_00FF));
assert_eq!(thumb_expand_imm(0x1AB), Some(0x00AB_00AB));
assert_eq!(thumb_expand_imm(0x101), Some(0x0001_0001));
assert_eq!(thumb_expand_imm(0x2AB), Some(0xAB00_AB00));
assert_eq!(thumb_expand_imm(0x280), Some(0x8000_8000));
assert_eq!(thumb_expand_imm(0x3AB), Some(0xABAB_ABAB));
assert_eq!(thumb_expand_imm(0x3FF), Some(0xFFFF_FFFF));
assert_eq!(thumb_expand_imm(0x100), None);
assert_eq!(thumb_expand_imm(0x200), None);
assert_eq!(thumb_expand_imm(0x300), None);
assert_eq!(thumb_expand_imm(8 << 7), Some(0x8000_0000));
assert_eq!(thumb_expand_imm((8 << 7) | 0x7F), Some(0xFF00_0000));
assert_eq!(thumb_expand_imm(9 << 7), Some(0x4000_0000));
assert_eq!(thumb_expand_imm((9 << 7) | 0x01), Some(0x4080_0000));
assert_eq!(thumb_expand_imm(10 << 7), Some(0x2000_0000));
assert_eq!(thumb_expand_imm(11 << 7), Some(0x1000_0000));
assert_eq!(thumb_expand_imm(29 << 7), Some(0x0000_0400));
assert_eq!(thumb_expand_imm(30 << 7), Some(0x0000_0200));
assert_eq!(thumb_expand_imm(31 << 7), Some(0x0000_0100));
assert_eq!(thumb_expand_imm((31 << 7) | 0x7F), Some(0x0000_01FE));
}
#[test]
fn thumb_expand_imm_rotate_is_a_left_shift() {
for r in 8u16..=31 {
for v in [0u16, 1, 0x3F, 0x7F] {
let expected = (0x80 | u32::from(v)) << (32 - u32::from(r));
assert_eq!(
thumb_expand_imm((r << 7) | v),
Some(expected),
"r={r} v={v}"
);
}
}
}
#[test]
fn expansion_is_a_bijection_on_the_decodable_space() {
let mut seen: Vec<(u32, u16)> = Vec::with_capacity(4096);
for imm12 in 0u16..4096 {
match thumb_expand_imm(imm12) {
Some(v) => {
assert_eq!(
encode_modified_imm(v),
Some(imm12),
"inverse disagrees for imm12={imm12:#05x} -> {v:#010x}"
);
seen.push((v, imm12));
}
None => assert!(
[0x100, 0x200, 0x300].contains(&imm12),
"unexpected UNPREDICTABLE immediate {imm12:#05x}"
),
}
}
assert_eq!(seen.len(), 4093);
seen.sort_unstable();
let mut deduped = seen.clone();
deduped.dedup_by_key(|(v, _)| *v);
assert_eq!(deduped.len(), seen.len(), "two encodings share a constant");
}
#[test]
fn unrepresentable_constants_are_rejected() {
for v in [
0x1234_5678, 0x0000_0100 + 1,
0x0001_0002, 0x00AB_00AC,
0xAB00_AB01,
0x0000_01FF, 0x8000_0001,
0xFFFF_FFFE,
] {
assert_eq!(encode_modified_imm(v), None, "{v:#010x} is not encodable");
}
assert_eq!(encode_modified_imm(0x0000_00FF), Some(0x0FF));
assert_eq!(encode_modified_imm(0x00AB_00AB), Some(0x1AB));
assert_eq!(encode_modified_imm(0xAB00_AB00), Some(0x2AB));
assert_eq!(encode_modified_imm(0xABAB_ABAB), Some(0x3AB));
assert_eq!(encode_modified_imm(0x8000_0000), Some(8 << 7));
assert_eq!(encode_modified_imm(0x0000_01FE), Some((31 << 7) | 0x7F));
}
#[test]
fn printed_ual_for_every_row_of_table_a5_10() {
let one = 0x001;
let cases: [(u16, bool, u16, u16, &str); 22] = [
(0b0000, false, 1, 0, "and r0, r1, #1"),
(0b0000, true, 1, 0, "ands r0, r1, #1"),
(0b0000, true, 1, 0b1111, "tst r1, #1"),
(0b0001, false, 1, 0, "bic r0, r1, #1"),
(0b0001, true, 1, 0, "bics r0, r1, #1"),
(0b0010, false, 1, 0, "orr r0, r1, #1"),
(0b0010, true, 1, 0, "orrs r0, r1, #1"),
(0b0010, false, 0b1111, 0, "mov.w r0, #1"),
(0b0010, true, 0b1111, 0, "movs.w r0, #1"),
(0b0011, false, 1, 0, "orn r0, r1, #1"),
(0b0011, false, 0b1111, 0, "mvn r0, #1"),
(0b0011, true, 0b1111, 0, "mvns r0, #1"),
(0b0100, false, 1, 0, "eor r0, r1, #1"),
(0b0100, true, 1, 0b1111, "teq r1, #1"),
(0b1000, false, 1, 0, "add.w r0, r1, #1"),
(0b1000, true, 1, 0, "adds.w r0, r1, #1"),
(0b1000, true, 1, 0b1111, "cmn r1, #1"),
(0b1010, false, 1, 0, "adc r0, r1, #1"),
(0b1011, false, 1, 0, "sbc r0, r1, #1"),
(0b1101, false, 1, 0, "sub.w r0, r1, #1"),
(0b1101, true, 1, 0b1111, "cmp.w r1, #1"),
(0b1110, false, 1, 0, "rsb.w r0, r1, #1"),
];
for (op, s, rn, rd, expected) in cases {
assert_eq!(dec(op, s, rn, rd, one).to_string(), expected);
}
}
#[test]
fn test_forms_print_no_s_suffix() {
for (op, mnemonic) in [
(0b0000, "tst"),
(0b0100, "teq"),
(0b1000, "cmn"),
(0b1101, "cmp"),
] {
let insn = dec(op, true, 2, 0b1111, 0x0FF);
assert_eq!(insn.mnemonic, mnemonic);
assert!(!insn.sets_flags, "{mnemonic} would print as {mnemonic}s");
assert_eq!(insn.operands.len(), 2);
assert_eq!(encode(&insn), Some(bits(op, true, 2, 0b1111, 0x0FF)));
}
}
#[test]
fn explicit_width_matches_the_manuals_syntax_lines() {
let wide = [
(0b0010, false, 0b1111, 0, "mov"),
(0b1000, false, 1, 0, "add"),
(0b1101, false, 1, 0, "sub"),
(0b1101, true, 1, 0b1111, "cmp"),
(0b1110, false, 1, 0, "rsb"),
];
for (op, s, rn, rd, mnemonic) in wide {
let insn = dec(op, s, rn, rd, 0x001);
assert_eq!(insn.mnemonic, mnemonic);
assert!(insn.explicit_width, "{mnemonic} needs .w to reassemble");
}
for (op, s, rn, rd) in [
(0b0000, false, 1, 0),
(0b0000, true, 1, 0b1111),
(0b0001, false, 1, 0),
(0b0010, false, 1, 0),
(0b0011, false, 1, 0),
(0b0011, false, 0b1111, 0),
(0b0100, false, 1, 0),
(0b0100, true, 1, 0b1111),
(0b1000, true, 1, 0b1111),
(0b1010, false, 1, 0),
(0b1011, false, 1, 0),
] {
let insn = dec(op, s, rn, rd, 0x001);
assert!(
!insn.explicit_width,
"{} has no narrow immediate form, so no .w",
insn.mnemonic
);
}
}
#[test]
fn rn_1111_aliases_orr_to_mov_and_orn_to_mvn() {
let mov = dec(0b0010, true, 0b1111, 3, 0x0FF);
assert_eq!(mov.mnemonic, "mov");
assert_eq!(mov.encoding, "T2");
assert_eq!(mov.operands.len(), 2);
assert_eq!(mov.to_string(), "movs.w r3, #0xff");
let orr = dec(0b0010, true, 0b1110, 3, 0x0FF);
assert_eq!(orr.mnemonic, "orr");
assert_eq!(orr.to_string(), "orrs r3, lr, #0xff");
let mvn = dec(0b0011, false, 0b1111, 3, 0x0FF);
assert_eq!(mvn.mnemonic, "mvn");
assert_eq!(mvn.operands.len(), 2);
let orn = dec(0b0011, false, 0b1110, 3, 0x0FF);
assert_eq!(orn.mnemonic, "orn");
}
#[test]
fn rd_1111_with_s_aliases_to_the_test_forms() {
assert_eq!(dec(0b0000, true, 1, 0b1111, 0x001).mnemonic, "tst");
assert_eq!(dec(0b0100, true, 1, 0b1111, 0x001).mnemonic, "teq");
assert_eq!(dec(0b1000, true, 1, 0b1111, 0x001).mnemonic, "cmn");
assert_eq!(dec(0b1101, true, 1, 0b1111, 0x001).mnemonic, "cmp");
for op in [0b0001, 0b0010, 0b0011, 0b1010, 0b1011, 0b1110] {
let insn = dec(op, true, 1, 0b1111, 0x001);
assert_eq!(insn.operands.get(0), Some(Operand::Reg(Reg::PC)));
}
}
#[test]
fn rd_1111_without_s_is_unpredictable_but_still_decoded() {
let and = dec(0b0000, false, 1, 0b1111, 0x001);
assert_eq!(and.mnemonic, "and");
assert!(!and.sets_flags);
assert_eq!(and.to_string(), "and pc, r1, #1");
assert_eq!(encode(&and), Some(bits(0b0000, false, 1, 0b1111, 0x001)));
for (op, mnemonic) in [(0b0100, "eor"), (0b1000, "add"), (0b1101, "sub")] {
let insn = dec(op, false, 1, 0b1111, 0x001);
assert_eq!(insn.mnemonic, mnemonic);
assert_eq!(insn.operands.get(0), Some(Operand::Reg(Reg::PC)));
}
let mut ands = and;
ands.sets_flags = true;
assert_eq!(encode(&ands), None);
}
#[test]
fn rn_1101_is_the_sp_form() {
let add = dec(0b1000, false, 0b1101, 0, 0x008);
assert_eq!((add.mnemonic, add.encoding), ("add", "T3"));
assert_eq!(add.to_string(), "add.w r0, sp, #8");
assert_eq!(encode(&add), Some(bits(0b1000, false, 0b1101, 0, 0x008)));
let sub = dec(0b1101, false, 0b1101, 0, 0x008);
assert_eq!((sub.mnemonic, sub.encoding), ("sub", "T2"));
assert_eq!(sub.to_string(), "sub.w r0, sp, #8");
assert_eq!(encode(&sub), Some(bits(0b1101, false, 0b1101, 0, 0x008)));
assert_eq!(dec(0b1000, true, 0b1101, 0b1111, 0x008).mnemonic, "cmn");
assert_eq!(dec(0b1101, true, 0b1101, 0b1111, 0x008).mnemonic, "cmp");
}
#[test]
fn unallocated_ops_are_undefined() {
for op in [0b0101, 0b0110, 0b0111, 0b1001, 0b1100, 0b1111] {
for s in [false, true] {
for rn in [0u16, 1, 0b1101, 0b1111] {
for rd in [0u16, 1, 0b1101, 0b1111] {
let (hw1, hw2) = bits(op, s, rn, rd, 0x001);
assert_eq!(decode(hw1, hw2, 0), None, "op={op:04b}");
}
}
}
}
}
#[test]
fn group_guard_rejects_neighbours() {
let (hw1, hw2) = bits(0b1000, false, 1, 0, 0x001);
assert!(decode(hw1, hw2, 0).is_some());
assert_eq!(decode(hw1 | 1 << 9, hw2, 0), None, "that is A5.3.3");
assert_eq!(decode(hw1, hw2 | 1 << 15, 0), None, "that is A5.3.4");
assert_eq!(decode(hw1 & !(0b11 << 11), hw2, 0), None, "16-bit space");
assert_eq!(decode(0xF20F, 0x0000, 0), None);
}
#[test]
fn round_trip_sweep() {
let mut decoded = 0usize;
for op in 0u16..16 {
for s in [false, true] {
for rn in [0u16, 1, 0b1101, 0b1111] {
for rd in [0u16, 3, 0b1101, 0b1111] {
for imm12 in 0u16..4096 {
let (hw1, hw2) = bits(op, s, rn, rd, imm12);
let insn = match decode(hw1, hw2, 0x2000) {
Some(i) => i,
None => continue,
};
decoded += 1;
assert_eq!(
encode(&insn),
Some((hw1, hw2)),
"{hw1:#06x} {hw2:#06x} decoded as `{insn}`"
);
}
}
}
}
}
assert_eq!(decoded, 10 * 2 * 16 * 4093);
}
#[test]
fn encode_ignores_an_it_block_condition() {
let mut insn = dec(0b0000, true, 1, 0, 0x0FF);
insn.cond = Some(crate::Cond::Ne);
assert_eq!(insn.to_string(), "andsne r0, r1, #0xff");
assert_eq!(encode(&insn), Some(bits(0b0000, true, 1, 0, 0x0FF)));
}
#[test]
fn encode_declines_what_is_not_ours() {
let insn = dec(0b1000, false, 1, 0, 0x001);
let mut narrow = insn;
narrow.width = Width::Narrow;
assert_eq!(encode(&narrow), None);
let mut reg = insn;
reg.operands = Operands::new();
reg.operands.push(Operand::Reg(Reg(0)));
reg.operands.push(Operand::Reg(Reg(1)));
reg.operands.push(Operand::Reg(Reg(2)));
assert_eq!(encode(®), None);
let mut addw = insn;
addw.encoding = "T4";
assert_eq!(encode(&addw), None);
let mut unrepresentable = insn;
unrepresentable.operands = Operands::new();
unrepresentable.operands.push(Operand::Reg(Reg(0)));
unrepresentable.operands.push(Operand::Reg(Reg(1)));
unrepresentable.operands.push(Operand::Imm(0x123));
assert_eq!(encode(&unrepresentable), None);
let mut alien = insn;
alien.mnemonic = "ldr";
assert_eq!(encode(&alien), None);
let mut short = insn;
short.operands = Operands::new();
short.operands.push(Operand::Reg(Reg(0)));
short.operands.push(Operand::Imm(1));
assert_eq!(encode(&short), None);
for v in [-1i64, 1 << 32, i64::MIN] {
let mut bad = insn;
bad.operands = Operands::new();
bad.operands.push(Operand::Reg(Reg(0)));
bad.operands.push(Operand::Reg(Reg(1)));
bad.operands.push(Operand::Imm(v));
assert_eq!(encode(&bad), None, "{v}");
}
}
#[test]
fn known_halfwords() {
let mov = decode(0xF04F, 0x00FF, 0).expect("mov.w");
assert_eq!(mov.to_string(), "mov.w r0, #0xff");
let mov_all = decode(0xF04F, 0x30FF, 0).expect("mov.w");
assert_eq!(mov_all.to_string(), "mov.w r0, #0xffffffff");
let sub = decode(0xF1A2, 0x0201, 0).expect("sub.w");
assert_eq!(sub.to_string(), "sub.w r2, r2, #1");
let cmp = decode(0xF5B0, 0x5F00, 0).expect("cmp.w");
assert_eq!(cmp.to_string(), "cmp.w r0, #0x2000");
assert_eq!(encode(&cmp), Some((0xF5B0, 0x5F00)));
}
}