miden_assembly/ast/
immediate.rs

1use core::fmt;
2
3use crate::{Felt, SourceSpan, Span, Spanned, ast::Ident};
4
5/// An 8-bit unsigned immediate
6pub type ImmU8 = Immediate<u8>;
7
8/// A 16-bit unsigned immediate
9pub type ImmU16 = Immediate<u16>;
10
11/// A 32-bit unsigned immediate
12pub type ImmU32 = Immediate<u32>;
13
14/// A field element immediate
15pub type ImmFelt = Immediate<Felt>;
16
17/// Represents the 32-bit immediate used for assertion error codes
18pub type ErrorCode = Immediate<u32>;
19
20/// Represents an instruction immediate, e.g. `add.1` or `add.CONST`
21pub enum Immediate<T> {
22    /// A literal integer value, either decimal or hex-encoded
23    Value(Span<T>),
24    /// A constant identifier
25    ///
26    /// This must refer to a constant definition in the current module.
27    ///
28    /// All immediates of this type are folded to `Value` during
29    /// semantic analysis, once all constant definitions are evaluated.
30    Constant(Ident),
31}
32
33/// All immediates
34impl<T> Immediate<T> {
35    pub fn is_literal(&self) -> bool {
36        matches!(self, Self::Value(_))
37    }
38
39    /// Transform the type of this immediate from T to U, using `map`
40    pub fn map<U, F>(self, map: F) -> Immediate<U>
41    where
42        F: FnMut(T) -> U,
43    {
44        match self {
45            Self::Constant(id) => Immediate::Constant(id),
46            Self::Value(value) => Immediate::Value(value.map(map)),
47        }
48    }
49}
50
51/// Copy-able immediates (in practice, all of them)
52impl<T: Copy> Immediate<T> {
53    pub fn expect_value(&self) -> T {
54        match self {
55            Self::Value(value) => value.into_inner(),
56            Self::Constant(name) => panic!("tried to unwrap unresolved constant: '{name}'"),
57        }
58    }
59
60    pub fn expect_spanned_value(&self) -> Span<T> {
61        match self {
62            Self::Value(value) => *value,
63            Self::Constant(name) => panic!("tried to unwrap unresolved constant: '{name}'"),
64        }
65    }
66}
67
68impl<T> Spanned for Immediate<T> {
69    fn span(&self) -> SourceSpan {
70        match self {
71            Self::Value(spanned) => spanned.span(),
72            Self::Constant(spanned) => spanned.span(),
73        }
74    }
75}
76
77impl<T> From<T> for Immediate<T> {
78    fn from(value: T) -> Self {
79        Self::Value(Span::unknown(value))
80    }
81}
82
83impl<T> From<Span<T>> for Immediate<T> {
84    fn from(value: Span<T>) -> Self {
85        Self::Value(value)
86    }
87}
88
89impl<T: Clone> Clone for Immediate<T> {
90    fn clone(&self) -> Self {
91        match self {
92            Self::Value(value) => Self::Value(value.clone()),
93            Self::Constant(name) => Self::Constant(name.clone()),
94        }
95    }
96}
97
98impl<T: Eq> Eq for Immediate<T> {}
99
100impl<T: PartialEq> PartialEq for Immediate<T> {
101    fn eq(&self, other: &Self) -> bool {
102        match (self, other) {
103            (Self::Value(l), Self::Value(r)) => l == r,
104            (Self::Constant(l), Self::Constant(r)) => l == r,
105            _ => false,
106        }
107    }
108}
109
110impl<T: PartialEq> PartialEq<T> for Immediate<T> {
111    fn eq(&self, other: &T) -> bool {
112        match self {
113            Self::Value(l) => l == other,
114            _ => false,
115        }
116    }
117}
118
119impl<T: fmt::Debug> fmt::Debug for Immediate<T> {
120    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121        match self {
122            Self::Value(value) if f.alternate() => write!(f, "Value({value:#?})"),
123            Self::Value(value) => write!(f, "Value({value:?})"),
124            Self::Constant(name) => write!(f, "Constant({name})"),
125        }
126    }
127}
128
129impl<T: fmt::Display> fmt::Display for Immediate<T> {
130    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131        match self {
132            Self::Value(value) => write!(f, "{value}"),
133            Self::Constant(name) => write!(f, "{name}"),
134        }
135    }
136}
137
138impl<T: crate::prettier::PrettyPrint> crate::prettier::PrettyPrint for Immediate<T> {
139    fn render(&self) -> crate::prettier::Document {
140        use crate::prettier::*;
141
142        match self {
143            Self::Value(value) => value.render(),
144            Self::Constant(name) => text(name),
145        }
146    }
147}