1use alloc::alloc::Layout;
2use core::fmt;
3
4use super::Type;
5
6#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct LocalId(u16);
9impl LocalId {
10 #[inline]
12 pub fn from_u16(x: u16) -> Self {
13 debug_assert!(x < u16::MAX, "invalid raw local id");
14 Self(x)
15 }
16
17 #[inline]
19 pub fn as_usize(self) -> usize {
20 self.0 as usize
21 }
22}
23impl cranelift_entity::EntityRef for LocalId {
24 #[inline]
25 fn new(index: usize) -> Self {
26 debug_assert!(index < (u16::MAX as usize));
27 Self(index as u16)
28 }
29
30 #[inline]
31 fn index(self) -> usize {
32 self.0 as usize
33 }
34}
35impl cranelift_entity::packed_option::ReservedValue for LocalId {
36 #[inline]
37 fn reserved_value() -> LocalId {
38 Self(u16::MAX)
39 }
40
41 #[inline]
42 fn is_reserved_value(&self) -> bool {
43 self.0 == u16::MAX
44 }
45}
46impl fmt::Display for LocalId {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 write!(f, "local{}", self.0)
49 }
50}
51impl fmt::Debug for LocalId {
52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53 fmt::Display::fmt(&self.0, f)
54 }
55}
56impl From<LocalId> for u16 {
57 #[inline(always)]
58 fn from(id: LocalId) -> Self {
59 id.0
60 }
61}
62impl From<LocalId> for miden_assembly::ast::Immediate<u16> {
63 #[inline(always)]
64 fn from(id: LocalId) -> Self {
65 miden_assembly::ast::Immediate::Value(miden_assembly::Span::unknown(id.0))
66 }
67}
68
69#[derive(Debug, Clone, PartialEq, Eq)]
71pub struct Local {
72 pub id: LocalId,
81 pub ty: Type,
83}
84impl Local {
85 pub fn layout(&self) -> Layout {
87 self.ty.layout()
88 }
89
90 pub fn size_in_bytes(&self) -> usize {
92 self.ty.size_in_bytes()
93 }
94
95 pub fn size_in_words(&self) -> usize {
97 self.ty.size_in_words()
98 }
99}