thumb_asm/cond.rs
1//! Condition codes, shared by every conditional form in the instruction set.
2//!
3//! One type, used by the 16-bit `B<cond>` (T1), the 32-bit `B<cond>.W` (T3),
4//! and `IT`. The bit values are the architectural ones (ARM DDI 0403E.e
5//! Table A7-1), so [`Cond::bits`] and [`Cond::from_bits`] are the encoding.
6
7/// An ARM condition code.
8///
9/// `0b1110` (`AL`, always) is [`Cond::Al`]. `0b1111` is **not** a condition:
10/// in the 16-bit branch space it is `SVC`, and in the 32-bit space it is
11/// reserved — so [`Cond::from_bits`] rejects it rather than inventing a
12/// fifteenth condition.
13///
14/// ```
15/// use thumb_asm::Cond;
16///
17/// assert_eq!(Cond::from_bits(0b0001), Some(Cond::Ne));
18/// assert_eq!(Cond::Ne.bits(), 0b0001);
19/// assert_eq!(Cond::Ne.invert(), Cond::Eq);
20/// assert_eq!(Cond::Hs.to_string(), "hs");
21/// assert_eq!(Cond::from_bits(0b1111), None); // not a condition
22/// ```
23#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
24pub enum Cond {
25 /// Equal — `Z == 1`.
26 Eq,
27 /// Not equal — `Z == 0`.
28 Ne,
29 /// Unsigned higher or same, also spelled `CS` — `C == 1`.
30 Hs,
31 /// Unsigned lower, also spelled `CC` — `C == 0`.
32 Lo,
33 /// Negative — `N == 1`.
34 Mi,
35 /// Positive or zero — `N == 0`.
36 Pl,
37 /// Overflow — `V == 1`.
38 Vs,
39 /// No overflow — `V == 0`.
40 Vc,
41 /// Unsigned higher — `C == 1 && Z == 0`.
42 Hi,
43 /// Unsigned lower or same — `C == 0 || Z == 1`.
44 Ls,
45 /// Signed greater than or equal — `N == V`.
46 Ge,
47 /// Signed less than — `N != V`.
48 Lt,
49 /// Signed greater than — `Z == 0 && N == V`.
50 Gt,
51 /// Signed less than or equal — `Z == 1 || N != V`.
52 Le,
53 /// Always. Encodable in `IT` and in `B<cond>.W`, but never in the 16-bit
54 /// `B<cond>` (there, `0b1110` is the permanently-undefined `UDF` space).
55 Al,
56}
57
58impl Cond {
59 /// The condition with this 4-bit encoding, or `None` for `0b1111`, which
60 /// the architecture does not define as a condition.
61 pub fn from_bits(bits: u8) -> Option<Self> {
62 Some(match bits & 0xF {
63 0b0000 => Cond::Eq,
64 0b0001 => Cond::Ne,
65 0b0010 => Cond::Hs,
66 0b0011 => Cond::Lo,
67 0b0100 => Cond::Mi,
68 0b0101 => Cond::Pl,
69 0b0110 => Cond::Vs,
70 0b0111 => Cond::Vc,
71 0b1000 => Cond::Hi,
72 0b1001 => Cond::Ls,
73 0b1010 => Cond::Ge,
74 0b1011 => Cond::Lt,
75 0b1100 => Cond::Gt,
76 0b1101 => Cond::Le,
77 0b1110 => Cond::Al,
78 _ => return None,
79 })
80 }
81
82 /// This condition's 4-bit encoding.
83 pub fn bits(self) -> u8 {
84 match self {
85 Cond::Eq => 0b0000,
86 Cond::Ne => 0b0001,
87 Cond::Hs => 0b0010,
88 Cond::Lo => 0b0011,
89 Cond::Mi => 0b0100,
90 Cond::Pl => 0b0101,
91 Cond::Vs => 0b0110,
92 Cond::Vc => 0b0111,
93 Cond::Hi => 0b1000,
94 Cond::Ls => 0b1001,
95 Cond::Ge => 0b1010,
96 Cond::Lt => 0b1011,
97 Cond::Gt => 0b1100,
98 Cond::Le => 0b1101,
99 Cond::Al => 0b1110,
100 }
101 }
102
103 /// The condition that is true exactly when this one is false — i.e. this
104 /// condition's encoding with bit 0 flipped. `AL` inverts to itself, since
105 /// `0b1111` is not a condition; callers branching on an inverted `AL`
106 /// should emit nothing at all rather than an always-false branch.
107 pub fn invert(self) -> Self {
108 match self {
109 Cond::Al => Cond::Al,
110 other => Cond::from_bits(other.bits() ^ 1).unwrap_or(Cond::Al),
111 }
112 }
113
114 /// The UAL mnemonic suffix (`"eq"`, `"ne"`, …). `AL` renders as the empty
115 /// string, which is how it is written in assembly.
116 pub fn suffix(self) -> &'static str {
117 match self {
118 Cond::Eq => "eq",
119 Cond::Ne => "ne",
120 Cond::Hs => "hs",
121 Cond::Lo => "lo",
122 Cond::Mi => "mi",
123 Cond::Pl => "pl",
124 Cond::Vs => "vs",
125 Cond::Vc => "vc",
126 Cond::Hi => "hi",
127 Cond::Ls => "ls",
128 Cond::Ge => "ge",
129 Cond::Lt => "lt",
130 Cond::Gt => "gt",
131 Cond::Le => "le",
132 Cond::Al => "",
133 }
134 }
135}
136
137impl std::fmt::Display for Cond {
138 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
139 f.write_str(self.suffix())
140 }
141}