use super::insn::{Insn, Operand, Operands, Reg, Width};
pub(crate) fn owns(hw1: u16, hw2: u16) -> bool {
if hw1 & 0xFF07 == 0x4704 {
return true;
}
if hw1 == 0xE97F && hw2 == 0xE97F {
return true;
}
hw1 & 0xFFF0 == 0xE840 && hw2 & 0xF03F == 0xF000
}
pub(crate) fn decode(hw1: u16, hw2: u16, addr: u32) -> Option<Insn> {
if hw1 & 0xFF07 == 0x4704 {
let rm = Reg(((hw1 >> 3) & 0xF) as u8);
let blx = hw1 & 0x0080 != 0;
if blx && (rm.num() >= 13) {
return None;
}
if !blx && rm.num() == 15 {
return None;
}
return Some(narrow(
if blx { "blxns" } else { "bxns" },
addr,
&[Operand::Reg(rm)],
));
}
if hw1 == 0xE97F && hw2 == 0xE97F {
return Some(wide("sg", addr, &[]));
}
let rn = Reg((hw1 & 0xF) as u8);
let rt = Reg(((hw2 >> 8) & 0xF) as u8);
if rn.num() == 15 || rt.num() == 13 || rt.num() == 15 {
return None;
}
let a = hw2 & 0x0080 != 0;
let z = hw2 & 0x0040 != 0;
let mnemonic = match (a, z) {
(false, false) => "tt",
(false, true) => "ttt",
(true, false) => "tta",
(true, true) => "ttat",
};
Some(wide(mnemonic, addr, &[Operand::Reg(rt), Operand::Reg(rn)]))
}
pub(crate) fn encode(insn: &Insn) -> Option<(u16, u16)> {
if insn.sets_flags {
return None;
}
let (a, b) = (insn.operands.get(0), insn.operands.get(1));
match (insn.mnemonic, a, b) {
("bxns", Some(Operand::Reg(m)), None) | ("blxns", Some(Operand::Reg(m)), None) => {
if insn.width != Width::Narrow {
return None;
}
let blx = insn.mnemonic == "blxns";
if blx && m.num() >= 13 {
return None;
}
if !blx && m.num() == 15 {
return None;
}
let base = if blx { 0x4784 } else { 0x4704 };
Some((base | (u16::from(m.num()) << 3), 0))
}
("sg", None, None) => {
if insn.width != Width::Wide {
return None;
}
Some((0xE97F, 0xE97F))
}
("tt", Some(Operand::Reg(t)), Some(Operand::Reg(n)))
| ("ttt", Some(Operand::Reg(t)), Some(Operand::Reg(n)))
| ("tta", Some(Operand::Reg(t)), Some(Operand::Reg(n)))
| ("ttat", Some(Operand::Reg(t)), Some(Operand::Reg(n))) => {
if insn.width != Width::Wide || insn.operands.get(2).is_some() {
return None;
}
if n.num() == 15 || t.num() == 13 || t.num() == 15 {
return None;
}
let (a, z) = match insn.mnemonic {
"tt" => (0, 0),
"ttt" => (0, 0x0040),
"tta" => (0x0080, 0),
_ => (0x0080, 0x0040),
};
Some((
0xE840 | u16::from(n.num()),
0xF000 | (u16::from(t.num()) << 8) | a | z,
))
}
_ => None,
}
}
fn narrow(mnemonic: &'static str, addr: u32, ops: &[Operand]) -> Insn {
Insn {
mnemonic,
encoding: "T1",
addr,
width: Width::Narrow,
cond: None,
sets_flags: false,
explicit_width: false,
operands: ops.iter().copied().collect(),
}
}
fn wide(mnemonic: &'static str, addr: u32, ops: &[Operand]) -> Insn {
Insn {
mnemonic,
encoding: "T1",
addr,
width: Width::Wide,
cond: None,
sets_flags: false,
explicit_width: false,
operands: ops.iter().copied().collect::<Operands>(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::isa::{decode_halfwords, encode, Target};
const VECTORS: &[(u16, u16, &str)] = &[
(0x4704, 0x0000, "bxns r0"),
(0x473C, 0x0000, "bxns r7"),
(0x4774, 0x0000, "bxns lr"),
(0x4784, 0x0000, "blxns r0"),
(0x47BC, 0x0000, "blxns r7"),
(0xE97F, 0xE97F, "sg"),
(0xE841, 0xF000, "tt r0, r1"),
(0xE849, 0xF340, "ttt r3, r9"),
(0xE841, 0xF080, "tta r0, r1"),
(0xE841, 0xF0C0, "ttat r0, r1"),
];
#[test]
fn every_security_extension_encoding_decodes_and_re_encodes() {
for &(hw1, hw2, text) in VECTORS {
let decoded = decode_halfwords(hw1, hw2, 0x1000, Target::V8M);
assert_eq!(
decoded.map(|i| i.to_string()).as_deref(),
Some(text),
"{hw1:#06x} {hw2:#06x}"
);
let round_tripped = decoded.and_then(|i| {
let wide = i.width == Width::Wide;
encode(&i).map(|got| (got, (hw1, if wide { hw2 } else { 0 })))
});
assert_eq!(
round_tripped.map(|(got, want)| got == want),
Some(true),
"{text} did not re-encode to the bytes it came from"
);
}
}
#[test]
fn the_union_still_reads_these_patterns_the_way_armv7_does() {
for &(hw1, hw2, armv7) in &[
(0xE97F, 0xE97F, Some("ldrd lr, r9, [pc, #-508]!, 0xe08")),
(0xE841, 0xF000, Some("strex r0, pc, [r1]")),
(0x4774, 0x0000, None),
(0x4784, 0x0000, None),
] {
let got = decode_halfwords(hw1, hw2, 0x1000, Target::Union);
assert_eq!(
got.map(|i| i.to_string()).as_deref(),
armv7,
"{hw1:#06x} {hw2:#06x} under Target::Union"
);
}
}
#[test]
fn owns_claims_the_group_and_declines_its_neighbours() {
for &(hw1, hw2, _) in VECTORS {
assert!(owns(hw1, hw2), "{hw1:#06x} {hw2:#06x} not claimed");
}
for &(hw1, hw2) in &[
(0x4700u16, 0x0000u16), (0x4780, 0x0000), (0x4705, 0x0000), (0xE97F, 0xE97E), (0xE97E, 0xE97F),
(0xE841, 0xF001), (0xE850, 0xF000), ] {
assert!(!owns(hw1, hw2), "{hw1:#06x} {hw2:#06x} wrongly claimed");
}
}
#[test]
fn decode_refuses_the_unpredictable_register_choices() {
for &(hw1, hw2, why) in &[
(0x47EC, 0x0000u16, "blxns sp is UNPREDICTABLE"),
(
0x47F4,
0x0000,
"blxns lr would clobber its own return address",
),
(0x47FC, 0x0000, "blxns pc is UNPREDICTABLE"),
(0x477C, 0x0000, "bxns pc names no return address"),
(0xE84F, 0xF000, "tt with Rn = pc"),
(0xE841, 0xFD00, "tt with Rt = sp"),
(0xE841, 0xFF00, "tt with Rt = pc"),
] {
assert!(
owns(hw1, hw2),
"{why}: not claimed, so not this test's case"
);
assert_eq!(
decode_halfwords(hw1, hw2, 0x1000, Target::V8M),
None,
"{why}"
);
}
}
#[test]
fn a_candidate_this_group_builds_can_still_be_rejected_by_faithful() {
let mut tt = decode_halfwords(0xE841, 0xF000, 0x1000, Target::V8M).unwrap();
tt.explicit_width = true;
assert!(super::encode(&tt).is_some(), "the group builds a candidate");
assert_eq!(
crate::isa::encode(&tt),
None,
"an explicit width suffix this group cannot print must be refused"
);
}
#[test]
fn the_default_target_is_the_union() {
assert_eq!(Target::default(), Target::Union);
}
#[test]
fn encode_refuses_what_decode_refuses() {
let tt = decode_halfwords(0xE841, 0xF000, 0x1000, Target::V8M).unwrap();
let bxns = decode_halfwords(0x4704, 0, 0x1000, Target::V8M).unwrap();
let sg = decode_halfwords(0xE97F, 0xE97F, 0x1000, Target::V8M).unwrap();
let mut f = tt;
f.sets_flags = true;
assert_eq!(super::encode(&f), None, "no encoding here sets flags");
let mut w = bxns;
w.width = Width::Wide;
assert_eq!(super::encode(&w), None, "bxns is narrow");
let mut n = sg;
n.width = Width::Narrow;
assert_eq!(super::encode(&n), None, "sg is wide");
let mut tn = tt;
tn.width = Width::Narrow;
assert_eq!(super::encode(&tn), None, "tt is wide");
let mut bad = tt;
bad.operands = [Operand::Reg(Reg(0)), Operand::Reg(Reg(15))]
.iter()
.copied()
.collect();
assert_eq!(super::encode(&bad), None, "tt with Rn = pc");
let mut bad_t = tt;
bad_t.operands = [Operand::Reg(Reg(13)), Operand::Reg(Reg(1))]
.iter()
.copied()
.collect();
assert_eq!(super::encode(&bad_t), None, "tt with Rt = sp");
let mut blxns_sp = bxns;
blxns_sp.mnemonic = "blxns";
blxns_sp.operands = [Operand::Reg(Reg(13))].iter().copied().collect();
assert_eq!(super::encode(&blxns_sp), None, "blxns sp");
let mut bxns_pc = bxns;
bxns_pc.operands = [Operand::Reg(Reg(15))].iter().copied().collect();
assert_eq!(super::encode(&bxns_pc), None, "bxns pc");
let mut three = tt;
three.operands = [
Operand::Reg(Reg(0)),
Operand::Reg(Reg(1)),
Operand::Reg(Reg(2)),
]
.iter()
.copied()
.collect();
assert_eq!(super::encode(&three), None, "tt takes two registers");
let mut other = tt;
other.mnemonic = "strex";
assert_eq!(super::encode(&other), None, "strex is not this group");
let mut sg_ops = sg;
sg_ops.operands = [Operand::Reg(Reg(0))].iter().copied().collect();
assert_eq!(super::encode(&sg_ops), None, "sg takes no operands");
}
}