use alloc::alloc::Layout;
use core::fmt;
use super::Type;
#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LocalId(u16);
impl LocalId {
#[inline]
pub fn from_u16(x: u16) -> Self {
debug_assert!(x < u16::MAX, "invalid raw local id");
Self(x)
}
#[inline]
pub fn as_usize(self) -> usize {
self.0 as usize
}
}
impl cranelift_entity::EntityRef for LocalId {
#[inline]
fn new(index: usize) -> Self {
debug_assert!(index < (u16::MAX as usize));
Self(index as u16)
}
#[inline]
fn index(self) -> usize {
self.0 as usize
}
}
impl cranelift_entity::packed_option::ReservedValue for LocalId {
#[inline]
fn reserved_value() -> LocalId {
Self(u16::MAX)
}
#[inline]
fn is_reserved_value(&self) -> bool {
self.0 == u16::MAX
}
}
impl fmt::Display for LocalId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "local{}", self.0)
}
}
impl fmt::Debug for LocalId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl From<LocalId> for u16 {
#[inline(always)]
fn from(id: LocalId) -> Self {
id.0
}
}
impl From<LocalId> for miden_assembly::ast::Immediate<u16> {
#[inline(always)]
fn from(id: LocalId) -> Self {
miden_assembly::ast::Immediate::Value(miden_assembly::Span::unknown(id.0))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Local {
pub id: LocalId,
pub ty: Type,
}
impl Local {
pub fn layout(&self) -> Layout {
self.ty.layout()
}
pub fn size_in_bytes(&self) -> usize {
self.ty.size_in_bytes()
}
pub fn size_in_words(&self) -> usize {
self.ty.size_in_words()
}
}