Enum midenc_hir::Type
source · pub enum Type {
}Expand description
Represents the type of a value
Variants§
Unknown
This indicates a failure to type a value, or a value which is untypable
Unit
This type is used to indicate the absence of a value, such as a function which returns nothing
Never
This type is the bottom type, and represents divergence, akin to Rust’s Never/! type
I1
I8
U8
I16
U16
I32
U32
I64
U64
I128
U128
U256
F64
Felt
Field element
Ptr(Box<Type>)
A pointer to a value in the default byte-addressable address space used by the IR.
Pointers of this type will be translated to an appropriate address space during code generation.
NativePtr(Box<Type>, AddressSpace)
A pointer to a valude in Miden’s native word-addressable address space.
In the type system, we represent the type of the pointee, as well as an address space identifier.
This pointer type is represented on Miden’s operand stack as a u64 value, consisting of two 32-bit elements, the most-significant bits being on top of the stack:
- Metadata for the pointer (in the upper 32-bit limb):
- The least-significant 2 bits represent a zero-based element index (range is 0-3)
- The next most significant 4 bits represent a zero-based byte index (range is 0-31)
- The remaining 26 bits represent an address space identifier
- The lower 32-bit limb contains the word-aligned address, which forms the base address of the pointer.
Dereferencing a pointer of this type involves popping the pointer metadata, and determining what type of load to issue based on the size of the value being loaded, and where the start of the data is according to the metadata. Then the word-aligned address is popped and the value is loaded.
If the load is naturally aligned, i.e. the element index and byte offset are zero, and the size is exactly one element or word; then a mem_load or mem_loadw are issued and no further action is required. If the load is not naturally aligned, then either one or two words will be loaded, depending on the type being loaded, unused elements will be dropped, and if the byte offset is non-zero, the data will be shifted bitwise into alignment on an element boundary.
Struct(StructType)
A compound type of fixed shape and size
Array(Box<Type>, usize)
A vector of fixed size
List(Box<Type>)
A dynamically sized list of values of the given type
NOTE: Currently this only exists to support the Wasm Canonical ABI, but it has no defined represenation yet, so in practice cannot be used in most places except during initial translation in the Wasm frontend.
Implementations§
source§impl Type
impl Type
sourcepub fn to_raw_parts(self) -> Option<SmallVec<[Type; 4]>>
pub fn to_raw_parts(self) -> Option<SmallVec<[Type; 4]>>
Convert this type into a vector of types corresponding to how this type will be represented in memory.
The largest “part” size is 32 bits, so types that fit in 32 bits remain unchanged. For types larger than 32 bits, they will be broken up into parts that do fit in 32 bits, preserving accurate types to the extent possible. For types smaller than 32 bits, they will be merged into packed structs no larger than 32 bits, to preserve the type information, and make it possible to reason about how to extract parts of the original type.
For an example, a struct of type { *ptr, u8, u8 } will be encoded on the
operand stack as [*ptr, {u8, u8}], where the first value is the 32-bit pointer
field, and the remaining fields are encoded as a 16-bit struct in the second value.
sourcepub fn split(self, n: usize) -> (Type, Option<Type>)
pub fn split(self, n: usize) -> (Type, Option<Type>)
Split this type into two parts:
- The first part is no more than
nbytes in size, and may contain the type itself if it fits - The second part is None if the first part is smaller than or equal in size to the requested split size
- The second part is Some if there is data left in the original type after the split. This part will be a type that attempts to preserve, to the extent possible, the original type structure, but will fall back to an array of bytes if a larger type must be split down the middle somewhere.
sourcepub fn min_alignment(&self) -> usize
pub fn min_alignment(&self) -> usize
Returns the minimum alignment, in bytes, of this type
sourcepub fn size_in_bits(&self) -> usize
pub fn size_in_bits(&self) -> usize
Returns the size in bits of this type, without alignment padding.
sourcepub fn size_in_bytes(&self) -> usize
pub fn size_in_bytes(&self) -> usize
Returns the minimum number of bytes required to store a value of this type
sourcepub fn aligned_size_in_bytes(&self) -> usize
pub fn aligned_size_in_bytes(&self) -> usize
Same as size_in_bytes, but with sufficient padding to guarantee alignment of the value.
sourcepub fn size_in_felts(&self) -> usize
pub fn size_in_felts(&self) -> usize
Returns the size in field elements of this type
sourcepub fn size_in_words(&self) -> usize
pub fn size_in_words(&self) -> usize
Returns the size in words of this type
sourcepub fn is_loadable(&self) -> bool
pub fn is_loadable(&self) -> bool
Returns true if this type can be loaded on to the operand stack
The rule for “loadability” is a bit arbitrary, but the purpose is to force users of the IR to either pass large values by reference, or calculate the addresses of the individual fields needed from a large structure or array, and issue loads/stores against those instead.
In effect, we reject loads of values that are larger than a single word, as that is the largest value which can be worked with on the operand stack of the Miden VM.
source§impl Type
impl Type
sourcepub fn is_zst(&self) -> bool
pub fn is_zst(&self) -> bool
Returns true if this type is a zero-sized type, which includes:
- Types with no size, e.g.
Type::Unit - Zero-sized arrays
- Arrays with a zero-sized element type
- Structs composed of nothing but zero-sized fields
pub fn is_numeric(&self) -> bool
pub fn is_integer(&self) -> bool
pub fn is_signed_integer(&self) -> bool
pub fn is_unsigned_integer(&self) -> bool
sourcepub fn as_unsigned(&self) -> Type
pub fn as_unsigned(&self) -> Type
Get this type as its unsigned integral twin, e.g. i32 becomes u32.
This function will panic if the type is not an integer type, or has no unsigned representation
sourcepub fn as_signed(&self) -> Type
pub fn as_signed(&self) -> Type
Get this type as its signed integral twin, e.g. u32 becomes i32.
This function will panic if the type is not an integer type, or has no signed representation
pub fn is_float(&self) -> bool
pub fn is_felt(&self) -> bool
pub fn is_pointer(&self) -> bool
pub fn is_struct(&self) -> bool
pub fn is_array(&self) -> bool
sourcepub fn is_compatible_operand(&self, other: &Type) -> bool
pub fn is_compatible_operand(&self, other: &Type) -> bool
Returns true if self and other are compatible operand types for a binary operator, e.g.
add
In short, the rules are as follows:
- The operand order is assumed to be
self <op> other, i.e.opis being applied toselfusingother. The left-hand operand is used as the “controlling” type for the operator, i.e. it determines what instruction will be used to perform the operation. - The operand types must be numeric, or support being manipulated numerically
- If the controlling type is unsigned, it is never compatible with signed types, because Miden instructions for unsigned types use a simple unsigned binary encoding, thus they will not handle signed operands using two’s complement correctly.
- If the controlling type is signed, it is compatible with both signed and unsigned types,
as long as the values fit in the range of the controlling type, e.g. adding a
u16to ani32is fine, but adding au32to ani32is not. - Pointer types are permitted to be the controlling type, and since they are represented using u32, they have the same compatibility set as u32 does. In all other cases, pointer types are treated the same as any other non-numeric type.
- Non-numeric types are always incompatible, since no operators support these types
pub fn pointee(&self) -> Option<&Type>
Trait Implementations§
source§impl From<StructType> for Type
impl From<StructType> for Type
source§fn from(ty: StructType) -> Type
fn from(ty: StructType) -> Type
source§impl StackElement for Type
impl StackElement for Type
source§impl TryFrom<Type> for StructType
impl TryFrom<Type> for StructType
impl Eq for Type
impl StructuralPartialEq for Type
Auto Trait Implementations§
impl Freeze for Type
impl RefUnwindSafe for Type
impl Send for Type
impl Sync for Type
impl Unpin for Type
impl UnwindSafe for Type
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CallHasher for T
impl<T> CallHasher for T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moresource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moresource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more