plan9_asm/operand/
mod.rs

1use std::fmt;
2
3use crate::register_with_offset::Register;
4
5use super::register_with_offset::RegisterWithOffset;
6#[derive(Debug, PartialEq)]
7pub enum Operand {
8    Ident(String),
9    Int(i64),
10    RegisterWithOffset(RegisterWithOffset),
11}
12
13impl fmt::Display for Operand {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        let s = match self {
16            Operand::Ident(s) => s.clone(),
17            Operand::Int(n) => format!("${n}"),
18            Operand::RegisterWithOffset(inner) => inner.to_string(),
19        };
20        write!(f, "{s}")
21    }
22}
23
24macro_rules! impl_from_operand {
25    ($from_ty:ty => Ident) => {
26        impl From<$from_ty> for Operand {
27            fn from(v: $from_ty) -> Self {
28                Self::Ident(v.to_string())
29            }
30        }
31    };
32    ($from_ty:ty => RegisterWithOffset) => {
33        impl From<$from_ty> for Operand {
34            fn from(v: $from_ty) -> Self {
35                Self::RegisterWithOffset(crate::register_with_offset::RegisterWithOffset::from(v))
36            }
37        }
38    };
39    ($from_ty:ty => $variant:ident) => {
40        impl From<$from_ty> for Operand {
41            fn from(v: $from_ty) -> Self {
42                Self::$variant(v)
43            }
44        }
45    };
46}
47
48impl_from_operand!(i64 => Int);
49impl_from_operand!(&str => Ident);
50impl_from_operand!(String => Ident);
51impl_from_operand!(RegisterWithOffset => RegisterWithOffset);
52impl_from_operand!(Register => RegisterWithOffset);