Skip to main content

Type

Struct Type 

Source
pub struct Type(/* private fields */);
Expand description

An IR type.

Four bytes, packed, because a type sits on every value in a function and a function has a great many values. The alternative, an enum holding a lane type and a lane count, comes out at twelve bytes for the same information, and the tables this goes in are walked often enough for that to show.

The packing is the low sixteen bits for the width in bits, the next fourteen for the lane count biased by one, and the top two for which of the four kinds it is. That gives a largest integer of Type::MAX_BITS and a widest vector of Type::MAX_LANES, both of which are past anything a target has.

use rucc_ir::{Float, Type};

assert_eq!(Type::int(32).to_string(), "i32");
assert_eq!(Type::float(Float::F64).to_string(), "f64");
assert_eq!(Type::PTR.to_string(), "ptr");
assert_eq!(Type::vector(Type::int(8), 16).to_string(), "i8x16");
assert_eq!(size_of::<Type>(), 4);

Implementations§

Source§

impl Type

Source

pub const MAX_BITS: u32 = BITS_MASK

The widest integer that can be represented, which is what limits _BitInt.

Sixteen bits of width is more than any target’s BITINT_MAXWIDTH and more than any vector register, and it leaves room in the same four bytes for the lane count.

Source

pub const MAX_LANES: u32

The most lanes a vector can have.

Source

pub const VOID: Self

No value.

Source

pub const PTR: Self

An address.

Source

pub const I1: Self

The one-bit integer every comparison produces.

Source

pub const fn int(bits: u32) -> Self

An integer bits wide.

§Panics

Panics if bits is zero or above Type::MAX_BITS. A zero-width integer is not a thing the IR has, and a caller that computed one has a bug that gets much harder to find if it is allowed to travel.

Source

pub const fn float(format: Float) -> Self

A floating point value in the given format.

Source

pub const fn vector(lane: Self, lanes: u32) -> Self

A vector of lanes copies of lane.

§Panics

Panics if lane is not an integer or a floating point type, if it is itself a vector, or if lanes is zero or above Type::MAX_LANES. A vector of pointers is not in the instruction set, so admitting the type would mean admitting a value nothing can be done with.

Source

pub const fn kind(self) -> Kind

Which of the four kinds this is.

Source

pub const fn bits(self) -> u32

The width of one lane in bits, which for a scalar is the width of the type.

Zero for void and for ptr, since the width of an address is a property of the target and not of the type. Ask the target for it.

Source

pub const fn lanes(self) -> u32

How many lanes this has, which is one unless it is a vector.

Source

pub const fn is_scalar(self) -> bool

Whether this has exactly one lane.

Source

pub const fn is_vector(self) -> bool

Whether this has more than one lane.

Source

pub const fn lane(self) -> Self

The type of one lane, which for a scalar is the type itself.

Source

pub const fn with_lane(self, lane: Self) -> Self

The same shape as this, with the lane type replaced.

This is what a comparison does: icmp over i32x4 produces i1x4, and the rule that the lane count is carried across is easier to get right in one place than at every instruction that needs it.

§Panics

Panics under the same conditions as Type::vector.

Source

pub const fn is_int(self) -> bool

Whether this is an integer, of any width, scalar or vector.

Source

pub const fn is_float(self) -> bool

Whether this is a floating point value, scalar or vector.

Source

pub const fn is_ptr(self) -> bool

Whether this is an address. A vector of pointers cannot be built, so this is scalar.

Source

pub const fn is_void(self) -> bool

Whether this is the absence of a value.

Source

pub const fn format(self) -> Option<Float>

The floating point format, if this is one.

Source

pub fn parse(text: &str) -> Option<Self>

Parses the textual form, which is what the printer writes.

use rucc_ir::Type;

assert_eq!(Type::parse("i32"), Some(Type::int(32)));
assert_eq!(Type::parse("f32x4"), Some(Type::vector(Type::float(rucc_ir::Float::F32), 4)));
assert_eq!(Type::parse("i0"), None);
assert_eq!(Type::parse("i32 "), None);

Trait Implementations§

Source§

impl Clone for Type

Source§

fn clone(&self) -> Type

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Type

Source§

impl Debug for Type

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Type

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Type

Source§

impl Hash for Type

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Type

Source§

fn cmp(&self, other: &Type) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Type

Source§

fn eq(&self, other: &Type) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Type

Source§

fn partial_cmp(&self, other: &Type) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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 UnsafeUnpin for Type

§

impl UnwindSafe for Type

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.