use core::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Writeback {
None,
Pre,
Post,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Symbol {
pub bits: u16,
pub name: &'static str,
}
impl Symbol {
pub const fn named(name: &'static str) -> Self {
Symbol { bits: 0, name }
}
}
pub trait IntValue {
fn value(self) -> i64;
}
macro_rules! int_value {
($($t:ty),*) => { $(impl IntValue for $t { #[inline] fn value(self) -> i64 { self as i64 } })* };
}
int_value!(u8, u16, u32, u64, i8, i16, i32, i64, bool);
#[derive(Clone, Copy, PartialEq)]
pub struct Operands<T, const N: usize> {
items: [T; N],
len: u8,
}
impl<T: Copy + Default, const N: usize> Default for Operands<T, N> {
fn default() -> Self {
Operands {
items: [T::default(); N],
len: 0,
}
}
}
impl<T: Copy + Default, const N: usize> Operands<T, N> {
pub fn empty() -> Self {
Self::default()
}
#[inline]
pub fn as_slice(&self) -> &[T] {
&self.items[..self.len as usize]
}
#[inline]
pub fn as_array(&self) -> &[T; N] {
&self.items
}
#[inline]
pub fn len(&self) -> usize {
self.len as usize
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
#[inline]
pub fn of<const M: usize>(operands: [T; M]) -> Self {
const {
assert!(M <= N, "more operands than any instruction has");
}
let mut out = Self::default();
out.items[..M].copy_from_slice(&operands);
out.len = M as u8;
out
}
}
impl<T: Copy + Default, const N: usize> core::ops::Deref for Operands<T, N> {
type Target = [T];
#[inline]
fn deref(&self) -> &[T] {
self.as_slice()
}
}
impl<'a, T: Copy + Default, const N: usize> IntoIterator for &'a Operands<T, N> {
type Item = &'a T;
type IntoIter = core::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.as_slice().iter()
}
}
impl<T: Copy + Default + fmt::Debug, const N: usize> fmt::Debug for Operands<T, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_slice().fmt(f)
}
}
#[cfg(test)]
mod tests {
use super::Operands;
#[test]
fn it_holds_as_many_as_it_was_given() {
let none: Operands<u8, 4> = Operands::empty();
assert!(none.is_empty());
assert_eq!(none.len(), 0);
assert_eq!(none.as_slice(), &[] as &[u8]);
let two: Operands<u8, 4> = Operands::of([7, 9]);
assert_eq!(two.len(), 2);
assert_eq!(two.as_slice(), &[7, 9]);
assert_eq!(two.as_array(), &[7, 9, 0, 0]);
assert_eq!(format!("{two:?}"), "[7, 9]");
}
#[test]
fn it_holds_a_full_array() {
let full: Operands<u8, 3> = Operands::of([1, 2, 3]);
assert_eq!(full.len(), 3);
assert_eq!(full.as_slice(), &[1, 2, 3]);
}
#[test]
fn it_reads_as_a_slice() {
let three: Operands<u8, 5> = Operands::of([4, 5, 6]);
assert_eq!(three.first(), Some(&4));
assert_eq!(three.iter().copied().sum::<u8>(), 15);
assert_eq!((&three).into_iter().count(), 3);
}
}