use crate::isa::insn::{Insn, Operand, ShiftKind};
use crate::isa::{self, Target};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub struct Flags {
pub n: bool,
pub z: bool,
pub c: bool,
pub v: bool,
}
impl Flags {
pub const NONE: Flags = Flags {
n: false,
z: false,
c: false,
v: false,
};
pub const ALL: Flags = Flags {
n: true,
z: true,
c: true,
v: true,
};
pub const NZC: Flags = Flags {
n: true,
z: true,
c: true,
v: false,
};
pub const NZ: Flags = Flags {
n: true,
z: true,
c: false,
v: false,
};
pub const C: Flags = Flags {
n: false,
z: false,
c: true,
v: false,
};
pub fn is_empty(self) -> bool {
self == Flags::NONE
}
pub fn contains(self, other: Flags) -> bool {
(!other.n || self.n) && (!other.z || self.z) && (!other.c || self.c) && (!other.v || self.v)
}
pub fn union(self, other: Flags) -> Flags {
Flags {
n: self.n || other.n,
z: self.z || other.z,
c: self.c || other.c,
v: self.v || other.v,
}
}
pub fn without(self, other: Flags) -> Flags {
Flags {
n: self.n && !other.n,
z: self.z && !other.z,
c: self.c && !other.c,
v: self.v && !other.v,
}
}
}
pub fn writes(insn: &Insn) -> Flags {
match insn.mnemonic {
"cmp" | "cmn" => Flags::ALL,
"tst" | "teq" => Flags::NZC,
"msr" => match insn.operands.get(0) {
Some(Operand::SpecialReg(name)) => {
let n = name.to_ascii_lowercase();
if n.ends_with("_nzcvq") || n.ends_with("_nzcvqg") {
Flags::ALL
} else {
Flags::NONE
}
}
_ => Flags::NONE,
},
_ if !insn.sets_flags => Flags::NONE,
"and" | "orr" | "eor" | "bic" | "orn" | "mvn" | "lsl" | "lsr" | "asr" | "ror" | "rrx" => {
Flags::NZC
}
"mov" => Flags::NZC,
"mul" | "mla" | "mls" => Flags::NZ,
_ => Flags::ALL,
}
}
pub fn reads(insn: &Insn) -> Flags {
let from_cond = insn.cond.map_or(Flags::NONE, cond_reads);
let from_operation = match insn.mnemonic {
"adc" | "sbc" | "rsc" | "rrx" => Flags::C,
"mrs" => Flags::ALL,
_ => Flags::NONE,
};
let from_shift = insn
.operands
.as_slice()
.find_map(|o| match o {
Operand::RegShifted(_, sh) if sh.kind == ShiftKind::Rrx => Some(Flags::C),
_ => None,
})
.unwrap_or(Flags::NONE);
from_cond.union(from_operation).union(from_shift)
}
fn cond_reads(cond: crate::Cond) -> Flags {
use crate::Cond::*;
const N: Flags = Flags {
n: true,
z: false,
c: false,
v: false,
};
const Z: Flags = Flags {
n: false,
z: true,
c: false,
v: false,
};
const V: Flags = Flags {
n: false,
z: false,
c: false,
v: true,
};
const CZ: Flags = Flags {
n: false,
z: true,
c: true,
v: false,
};
const NV: Flags = Flags {
n: true,
z: false,
c: false,
v: true,
};
const NZV: Flags = Flags {
n: true,
z: true,
c: false,
v: true,
};
match cond {
Eq | Ne => Z,
Hs | Lo => Flags::C,
Mi | Pl => N,
Vs | Vc => V,
Hi | Ls => CZ,
Ge | Lt => NV,
Gt | Le => NZV,
Al => Flags::NONE,
}
}
pub fn live_after(image: &[u8], at: usize, target: Target, limit: usize) -> Flags {
let here = match isa::decode_at_with(image, at, at as u32, target) {
Some(i) => i,
None => return Flags::ALL,
};
let mut live = live_from(image, at + here.len(), target, limit);
if here.is_branch() {
let loads_its_destination = here
.operands
.as_slice()
.any(|o| matches!(o, Operand::Mem(_)));
if loads_its_destination {
return Flags::ALL;
}
match here.branch_target() {
Some(t) if (t as usize) < image.len() => {
live = live.union(live_from(image, t as usize, target, limit));
}
_ => return Flags::ALL,
}
}
live
}
fn live_from(image: &[u8], pos: usize, target: Target, limit: usize) -> Flags {
let mut unresolved = Flags::ALL;
let mut live = Flags::NONE;
let mut decoder = isa::Decoder::at(image, pos, pos as u32).target(target);
let mut seen = 0usize;
while seen < limit {
if unresolved.is_empty() {
break;
}
let insn = match decoder.next() {
Some(i) => i,
None => return live.union(unresolved),
};
seen += 1;
live = live.union(reads(&insn).intersect(unresolved));
unresolved = unresolved.without(writes(&insn));
if insn.is_branch() || insn.writes_pc() {
return live.union(unresolved);
}
}
live.union(unresolved)
}
impl Flags {
pub fn intersect(self, other: Flags) -> Flags {
Flags {
n: self.n && other.n,
z: self.z && other.z,
c: self.c && other.c,
v: self.v && other.v,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::isa::decode_halfwords;
fn at(hw1: u16, hw2: u16) -> Insn {
decode_halfwords(hw1, hw2, 0x1000, Target::Union).expect("decodes")
}
#[test]
fn logical_and_shift_operations_leave_the_overflow_flag_alone() {
for (hw1, hw2, what) in [
(0x4008u16, 0x0000u16, "ands r0, r1"),
(0x4048, 0x0000, "eors r0, r1"),
(0x4308, 0x0000, "orrs r0, r1"),
(0x4388, 0x0000, "bics r0, r1"),
(0x43C8, 0x0000, "mvns r0, r1"),
(0x0088, 0x0000, "lsls r0, r1, #2"),
(0x0888, 0x0000, "lsrs r0, r1, #2"),
(0x1088, 0x0000, "asrs r0, r1, #2"),
] {
let w = writes(&at(hw1, hw2));
assert_eq!(w, Flags::NZC, "{what} should write N, Z, C and not V");
assert!(!w.v, "{what} must not be reported as writing V");
}
}
#[test]
fn arithmetic_and_comparisons_write_every_flag() {
for (hw1, hw2, what) in [
(0x1888u16, 0x0000u16, "adds r0, r1, r2"),
(0x1A88, 0x0000, "subs r0, r1, r2"),
(0x4148, 0x0000, "adcs r0, r1"),
(0x4188, 0x0000, "sbcs r0, r1"),
(0x4288, 0x0000, "cmp r0, r1"),
(0x42C8, 0x0000, "cmn r0, r1"),
] {
assert_eq!(writes(&at(hw1, hw2)), Flags::ALL, "{what}");
}
}
#[test]
fn the_test_instructions_write_flags_without_an_s_suffix() {
let tst = at(0x4208, 0x0000); assert!(!tst.sets_flags, "TST carries no S suffix");
assert_eq!(writes(&tst), Flags::NZC, "TST is an AND: V survives");
}
#[test]
fn moves_and_multiplies_write_less_than_the_arithmetic_operations() {
assert_eq!(writes(&at(0x2001, 0x0000)), Flags::NZC, "movs r0, #1");
assert_eq!(writes(&at(0x4348, 0x0000)), Flags::NZ, "muls r0, r1, r0");
assert_eq!(
writes(&at(0xFB01, 0x3002)),
Flags::NONE,
"mla has no S here"
);
}
#[test]
fn the_status_register_transfers_touch_every_flag() {
let mrs = at(0xF3EF, 0x8000); assert_eq!(mrs.mnemonic, "mrs");
assert_eq!(reads(&mrs), Flags::ALL, "MRS observes the whole APSR");
let msr = at(0xF380, 0x8800); assert_eq!(msr.mnemonic, "msr");
assert_eq!(writes(&msr), Flags::ALL, "MSR replaces the whole APSR");
}
#[test]
fn a_conditional_instruction_inside_an_it_block_still_reads_its_flags() {
let image = [0x00, 0xbf, 0x04, 0xbf, 0x01, 0x31, 0x01, 0x32, 0x70, 0x47];
let live = live_after(&image, 0, Target::Union, 12);
assert!(
live.z,
"the IT block's condition is EQ, which reads Z — a stateless walk \
would miss it and report Z dead"
);
}
#[test]
fn the_ge_only_spelling_of_msr_writes_no_condition_flag() {
let ge = at(0xF380, 0x8400); assert_eq!(ge.mnemonic, "msr");
assert_eq!(
writes(&ge),
Flags::NONE,
"APSR_g writes GE[3:0] and nothing else"
);
let nzcvq = at(0xF380, 0x8800); assert_eq!(writes(&nzcvq), Flags::ALL, "APSR_nzcvq writes all four");
let iapsr = at(0xF380, 0x8801); assert_eq!(iapsr.mnemonic, "msr");
assert_eq!(
writes(&iapsr),
Flags::ALL,
"IAPSR_nzcvq carries the APSR bits too"
);
let primask = at(0xF380, 0x8810); assert_eq!(writes(&primask), Flags::NONE, "PRIMASK is not the APSR");
let mut odd = nzcvq;
odd.operands = [Operand::Reg(crate::isa::insn::Reg(0))]
.iter()
.copied()
.collect();
assert_eq!(writes(&odd), Flags::NONE);
}
#[test]
fn rrx_as_a_shift_operand_reads_carry() {
let shifted = at(0xEA01, 0x0032); assert_eq!(shifted.mnemonic, "and");
assert_eq!(shifted.cond, None, "no condition, so no read from that");
assert!(
reads(&shifted).c,
"the RRX shift consumes carry regardless of the mnemonic"
);
}
#[test]
fn an_instruction_that_loads_its_destination_is_not_followed() {
let image = [0xdf, 0xf8, 0x00, 0xf0, 0xd1, 0x18, 0xd1, 0x18];
let insn = crate::isa::decode_at_with(&image, 0, 0, Target::Union).expect("decodes");
assert!(insn.is_branch(), "writing pc makes this a branch");
assert_eq!(
live_after(&image, 0, Target::Union, 8),
Flags::ALL,
"the destination is loaded from memory, so no path is visible"
);
}
#[test]
fn a_branch_leaving_the_image_resolves_nothing() {
let image = [0xf8, 0xb3, 0x70, 0x47];
assert_eq!(
live_after(&image, 0, Target::Union, 8),
Flags::ALL,
"the taken path leaves the image, so it cannot be cleared"
);
}
#[test]
fn the_carry_consuming_instructions_read_it_unconditionally() {
let adc = at(0x4148, 0x0000); assert_eq!(adc.cond, None, "not in an IT block, so no condition");
assert!(
reads(&adc).c,
"ADC adds the carry bit in; it reads C regardless of any condition"
);
let sbc = at(0x4188, 0x0000);
assert!(reads(&sbc).c, "SBC likewise");
}
#[test]
fn a_condition_reads_only_the_flags_its_test_names() {
use crate::Cond::*;
for (cond, want, why) in [
(
Eq,
Flags {
n: false,
z: true,
c: false,
v: false,
},
"EQ tests Z",
),
(Hs, Flags::C, "HS tests C"),
(
Mi,
Flags {
n: true,
z: false,
c: false,
v: false,
},
"MI tests N",
),
(
Vs,
Flags {
n: false,
z: false,
c: false,
v: true,
},
"VS tests V",
),
(
Hi,
Flags {
n: false,
z: true,
c: true,
v: false,
},
"HI tests C and Z",
),
(
Ge,
Flags {
n: true,
z: false,
c: false,
v: true,
},
"GE tests N == V",
),
(
Gt,
Flags {
n: true,
z: true,
c: false,
v: true,
},
"GT tests Z and N == V",
),
(Al, Flags::NONE, "AL tests nothing"),
] {
assert_eq!(cond_reads(cond), want, "{why}");
}
}
#[test]
fn an_ands_does_not_make_the_overflow_flag_dead() {
let arithmetic = [0x00, 0xbf, 0x88, 0x18, 0x70, 0x47];
assert_eq!(
live_after(&arithmetic, 0, Target::Union, 8),
Flags::NONE,
"ADDS resolves all four, so nothing survives to the return"
);
let logical = [0x00, 0xbf, 0x08, 0x40, 0x70, 0x47];
let live = live_after(&logical, 0, Target::Union, 8);
assert!(live.v, "ANDS does not write V, so V is still live");
assert!(
!live.n && !live.z && !live.c,
"ANDS does write N, Z and C, so those are dead"
);
}
#[test]
fn a_flag_is_dead_once_something_overwrites_it_unread() {
let image = [0x88, 0x18, 0xe3, 0x19, 0x70, 0x47];
assert_eq!(
live_after(&image, 0, Target::Union, 8),
Flags::NONE,
"the second ADDS overwrites all four before anything reads them"
);
let read_first = [0x88, 0x18, 0x00, 0xd0];
let live = live_after(&read_first, 0, Target::Union, 8);
assert!(live.z, "the BEQ reads Z");
}
#[test]
fn what_cannot_be_followed_is_assumed_to_read_everything() {
let branch = [0x88, 0x18, 0x00, 0xe0];
assert_eq!(
live_after(&branch, 0, Target::Union, 8),
Flags::ALL,
"past a branch the flags belong to code this does not follow"
);
let junk = [0x88, 0x18, 0xff, 0xff];
assert_eq!(live_after(&junk, 0, Target::Union, 8), Flags::ALL);
let short = [0x88, 0x18];
assert_eq!(live_after(&short, 0, Target::Union, 8), Flags::ALL);
let long = [0x00, 0xbf, 0x00, 0xbf, 0x00, 0xbf, 0x00, 0xbf];
assert_eq!(live_after(&long, 0, Target::Union, 1), Flags::ALL);
}
#[test]
fn an_undecodable_site_resolves_nothing() {
assert_eq!(live_after(&[0xff, 0xff], 0, Target::Union, 8), Flags::ALL);
}
#[test]
fn the_flag_set_operations_behave() {
assert!(Flags::NONE.is_empty());
assert!(!Flags::C.is_empty());
assert!(Flags::ALL.contains(Flags::NZC));
assert!(!Flags::NZC.contains(Flags::ALL));
assert!(Flags::NZC.contains(Flags::NZ));
let n_only = Flags {
n: true,
z: false,
c: false,
v: false,
};
let z_only = Flags {
n: false,
z: true,
c: false,
v: false,
};
let c_only = Flags {
n: false,
z: false,
c: true,
v: false,
};
assert!(!Flags::NONE.contains(n_only));
assert!(!Flags::NONE.contains(z_only));
assert!(!Flags::NONE.contains(c_only));
assert_eq!(Flags::NZ.union(Flags::C), Flags::NZC);
assert_eq!(
Flags::ALL.without(Flags::NZC),
Flags {
n: false,
z: false,
c: false,
v: true
}
);
assert_eq!(Flags::ALL.intersect(Flags::NZ), Flags::NZ);
assert_eq!(Flags::default(), Flags::NONE);
}
}