2023-06-24 17:18:31 -05:00
|
|
|
//! HoleyBytes register value definition
|
|
|
|
|
2023-10-18 05:14:24 -05:00
|
|
|
use crate::utils::static_assert;
|
|
|
|
|
|
|
|
/// Define [`Value`] »union« (it's fake)
|
2023-06-24 17:16:14 -05:00
|
|
|
///
|
|
|
|
/// # Safety
|
2023-10-18 05:14:24 -05:00
|
|
|
/// Its variants have to be sound to byte-reinterpretate
|
2023-06-24 17:16:14 -05:00
|
|
|
/// between each other. Otherwise the behaviour is undefined.
|
2023-06-20 19:07:48 -05:00
|
|
|
macro_rules! value_def {
|
|
|
|
($($ty:ident),* $(,)?) => {
|
2023-06-24 17:16:14 -05:00
|
|
|
/// HBVM register value
|
2023-06-20 19:07:48 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2023-10-18 05:14:24 -05:00
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct Value(pub u64);
|
2023-06-20 19:07:48 -05:00
|
|
|
|
|
|
|
$(
|
|
|
|
impl From<$ty> for Value {
|
|
|
|
#[inline]
|
|
|
|
fn from(value: $ty) -> Self {
|
2023-10-18 05:14:24 -05:00
|
|
|
let mut new = core::mem::MaybeUninit::<u64>::zeroed();
|
|
|
|
unsafe {
|
|
|
|
new.as_mut_ptr().cast::<$ty>().write(value);
|
|
|
|
Self(new.assume_init())
|
|
|
|
}
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
}
|
2023-07-24 11:48:42 -05:00
|
|
|
|
2023-10-18 05:14:24 -05:00
|
|
|
static_assert!(core::mem::size_of::<$ty>() <= core::mem::size_of::<Value>());
|
2023-07-24 11:48:42 -05:00
|
|
|
|
2023-08-19 16:24:31 -05:00
|
|
|
impl private::Sealed for $ty {}
|
2023-07-24 11:48:42 -05:00
|
|
|
unsafe impl ValueVariant for $ty {}
|
2023-06-20 19:07:48 -05:00
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-24 11:48:42 -05:00
|
|
|
impl Value {
|
2023-08-07 20:03:15 -05:00
|
|
|
/// Byte reinterpret value to target variant
|
2023-07-24 11:48:42 -05:00
|
|
|
#[inline]
|
2023-08-07 20:03:15 -05:00
|
|
|
pub fn cast<V: ValueVariant>(self) -> V {
|
2023-10-18 05:14:24 -05:00
|
|
|
unsafe { core::mem::transmute_copy(&self.0) }
|
2023-07-24 11:48:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # Safety
|
|
|
|
/// - N/A, not to be implemented manually
|
2023-08-19 16:24:31 -05:00
|
|
|
pub unsafe trait ValueVariant: private::Sealed + Copy + Into<Value> {}
|
|
|
|
|
|
|
|
mod private {
|
|
|
|
pub trait Sealed {}
|
|
|
|
}
|
2023-07-24 11:48:42 -05:00
|
|
|
|
2023-10-18 05:14:24 -05:00
|
|
|
value_def!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
|
|
|
|
static_assert!(core::mem::size_of::<Value>() == 8);
|
2023-06-20 19:07:48 -05:00
|
|
|
|
2023-07-24 11:48:42 -05:00
|
|
|
impl core::fmt::Debug for Value {
|
2023-06-20 19:07:48 -05:00
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
2023-06-24 17:16:14 -05:00
|
|
|
// Print formatted as hexadecimal, unsigned integer
|
2023-07-24 11:48:42 -05:00
|
|
|
write!(f, "{:x}", self.cast::<u64>())
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
}
|
2023-10-18 05:14:24 -05:00
|
|
|
|
|
|
|
pub(crate) trait CheckedDivRem {
|
|
|
|
fn checked_div(self, other: Self) -> Option<Self>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
fn checked_rem(self, other: Self) -> Option<Self>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_checked_div_rem {
|
|
|
|
($($ty:ty),* $(,)?) => {
|
|
|
|
$(impl CheckedDivRem for $ty {
|
|
|
|
#[inline(always)]
|
|
|
|
fn checked_div(self, another: Self) -> Option<Self>
|
|
|
|
{ self.checked_div(another) }
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn checked_rem(self, another: Self) -> Option<Self>
|
|
|
|
{ self.checked_rem(another) }
|
|
|
|
})*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_checked_div_rem!(u8, u16, u32, u64, i8, i16, i32, i64);
|