diff options
Diffstat (limited to 'src/arm32/flag')
-rw-r--r-- | src/arm32/flag/mod.rs | 144 |
1 files changed, 87 insertions, 57 deletions
diff --git a/src/arm32/flag/mod.rs b/src/arm32/flag/mod.rs index fe564e4..ad9c453 100644 --- a/src/arm32/flag/mod.rs +++ b/src/arm32/flag/mod.rs @@ -26,78 +26,108 @@ use core::fmt::Display; -/// A flag. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -#[repr(u8)] -pub enum Flag<const C: char> { - Off = 0x0, - On = 0x1, -} +macro_rules! define_flag { + { + vis: $vis:vis, + name: $name:ident, + symbol: $symbol:literal, + doc: $doc:literal $(,)? + } => { + #[doc = $doc] + #[derive(Clone, Copy, Debug, Eq, PartialEq)] + #[repr(u8)] + $vis enum $name { + Off = 0x0, + On = 0x1, + } -impl<const C: char> Flag<C> { - /// Checks if the flag is off. - #[inline(always)] - #[must_use] - pub const fn is_off(self) -> bool { self as u8 == Self::Off as u8 } + impl $name { + /// Checks if the flag is on. + #[inline(always)] + #[must_use] + pub const fn is_on(self) -> bool { self as u8 == Self::On as u8 } - /// Checks if the flag is on. - #[inline(always)] - #[must_use] - pub const fn is_on(self) -> bool { self as u8 == Self::On as u8 } -} + /// Checks if the flag is off. + #[inline(always)] + #[must_use] + pub const fn is_off(self) -> bool { self as u8 == Self::Off as u8 } + } + + impl Display for $name { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + const SYMBOL: &str = $symbol; -impl<const C: char> Display for Flag<C> { - fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - if self.is_on() { - write!(f, "{C}")?; + if self.is_on() { write!(f, "{SYMBOL}")? }; + + Ok(()) + } } - Ok(()) - } -} + impl From<bool> for $name { + #[inline(always)] + fn from(value: bool) -> Self { + if value { + Self::On + } else { + Self::Off + } + } + } -impl<const C: char> From<bool> for Flag<C> { - #[inline(always)] - fn from(value: bool) -> Self { - if value { - Self::On - } else { - Self::Off + impl From<$name> for bool { + #[inline(always)] + fn from(value: $name) -> Self { value.is_on() } } - } -} -impl<const C: char> From<Flag<C>> for bool { - #[inline(always)] - fn from(value: Flag<C>) -> Self { value.is_on() } -} + impl From<$name> for u128 { + #[inline(always)] + fn from(value: $name) -> Self { Self::from(value.is_on()) } + } -impl<const C: char> From<Flag<C>> for u128 { - #[inline(always)] - fn from(value: Flag<C>) -> Self { Self::from(value.is_on()) } -} + impl From<$name> for u16 { + #[inline(always)] + fn from(value: $name) -> Self { Self::from(value.is_on()) } + } -impl<const C: char> From<Flag<C>> for u16 { - #[inline(always)] - fn from(value: Flag<C>) -> Self { Self::from(value.is_on()) } -} + impl From<$name> for u32 { + #[inline(always)] + fn from(value: $name) -> Self { Self::from(value.is_on()) } + } -impl<const C: char> From<Flag<C>> for u32 { - #[inline(always)] - fn from(value: Flag<C>) -> Self { Self::from(value.is_on()) } + impl From<$name> for u64 { + #[inline(always)] + fn from(value: $name) -> Self { Self::from(value.is_on()) } + } + + impl From<$name> for u8 { + #[inline(always)] + fn from(value: $name) -> Self { Self::from(value.is_on()) } + } + + impl From<$name> for usize { + #[inline(always)] + fn from(value: $name) -> Self { Self::from(value.is_on()) } + } + }; } -impl<const C: char> From<Flag<C>> for u64 { - #[inline(always)] - fn from(value: Flag<C>) -> Self { Self::from(value.is_on()) } +define_flag! { + vis: pub, + name: Bflag, + symbol: "B", + doc: "A B flag.\n\nThis indicates binary operations on some memory instructions.\n" } -impl<const C: char> From<Flag<C>> for u8 { - #[inline(always)] - fn from(value: Flag<C>) -> Self { Self::from(value.is_on()) } +define_flag! { + vis: pub, + name: Sflag, + symbol: "S", + doc: "An S flag.\n\nThis indicates setting the status register on some arithmetic instructions.\n" } -impl<const C: char> From<Flag<C>> for usize { - #[inline(always)] - fn from(value: Flag<C>) -> Self { Self::from(value.is_on()) } +define_flag! { + vis: pub, + name: Tflag, + symbol: "T", + doc: "An T flag.\n\nThis indicates translation on some memory instructions.\n" } |