Skip to main content

rustc_abi/callconv/
reg.rs

1#[cfg(feature = "nightly")]
2use rustc_macros::HashStable_Generic;
3
4use crate::{Align, HasDataLayout, Integer, Primitive, Size};
5
6#[cfg_attr(feature = "nightly", derive(const _: () =
    {
        impl<__CTX> ::rustc_data_structures::stable_hasher::HashStable<__CTX>
            for RegKind where __CTX: ::rustc_span::HashStableContext {
            #[inline]
            fn hash_stable(&self, __hcx: &mut __CTX,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    RegKind::Integer => {}
                    RegKind::Float => {}
                    RegKind::Vector { hint_vector_elem: ref __binding_0 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable_Generic))]
7#[derive(#[automatically_derived]
impl ::core::marker::Copy for RegKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for RegKind {
    #[inline]
    fn clone(&self) -> RegKind {
        let _: ::core::clone::AssertParamIsClone<Primitive>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for RegKind {
    #[inline]
    fn eq(&self, other: &RegKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (RegKind::Vector { hint_vector_elem: __self_0 },
                    RegKind::Vector { hint_vector_elem: __arg1_0 }) =>
                    __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for RegKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Primitive>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for RegKind {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state);
        match self {
            RegKind::Vector { hint_vector_elem: __self_0 } =>
                ::core::hash::Hash::hash(__self_0, state),
            _ => {}
        }
    }
}Hash, #[automatically_derived]
impl ::core::fmt::Debug for RegKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            RegKind::Integer =>
                ::core::fmt::Formatter::write_str(f, "Integer"),
            RegKind::Float => ::core::fmt::Formatter::write_str(f, "Float"),
            RegKind::Vector { hint_vector_elem: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "Vector", "hint_vector_elem", &__self_0),
        }
    }
}Debug)]
8pub enum RegKind {
9    Integer,
10    Float,
11    Vector {
12        /// The `hint_vector_elem` is strictly for optimization purposes. E.g. it can be used by
13        /// a codegen backend to prevent extra bitcasts that obscure a pattern. Alternatively,
14        /// it can be safely ignored by always picking i8.
15        hint_vector_elem: Primitive,
16    },
17}
18
19#[cfg_attr(feature = "nightly", derive(const _: () =
    {
        impl<__CTX> ::rustc_data_structures::stable_hasher::HashStable<__CTX>
            for Reg where __CTX: ::rustc_span::HashStableContext {
            #[inline]
            fn hash_stable(&self, __hcx: &mut __CTX,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    Reg { kind: ref __binding_0, size: ref __binding_1 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable_Generic))]
20#[derive(#[automatically_derived]
impl ::core::marker::Copy for Reg { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Reg {
    #[inline]
    fn clone(&self) -> Reg {
        let _: ::core::clone::AssertParamIsClone<RegKind>;
        let _: ::core::clone::AssertParamIsClone<Size>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for Reg {
    #[inline]
    fn eq(&self, other: &Reg) -> bool {
        self.kind == other.kind && self.size == other.size
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Reg {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<RegKind>;
        let _: ::core::cmp::AssertParamIsEq<Size>;
    }
}Eq, #[automatically_derived]
impl ::core::hash::Hash for Reg {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.kind, state);
        ::core::hash::Hash::hash(&self.size, state)
    }
}Hash, #[automatically_derived]
impl ::core::fmt::Debug for Reg {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "Reg", "kind",
            &self.kind, "size", &&self.size)
    }
}Debug)]
21pub struct Reg {
22    pub kind: RegKind,
23    pub size: Size,
24}
25
26macro_rules! reg_ctor {
27    ($name:ident, $kind:ident, $bits:expr) => {
28        pub fn $name() -> Reg {
29            Reg { kind: RegKind::$kind, size: Size::from_bits($bits) }
30        }
31    };
32}
33
34impl Reg {
35    Reg
Reg { kind: RegKind::Integer, size: Size::from_bits(8) };reg_ctor!(i8, Integer, 8);
36    Reg
Reg { kind: RegKind::Integer, size: Size::from_bits(16) };reg_ctor!(i16, Integer, 16);
37    Reg
Reg { kind: RegKind::Integer, size: Size::from_bits(32) };reg_ctor!(i32, Integer, 32);
38    Reg
Reg { kind: RegKind::Integer, size: Size::from_bits(64) };reg_ctor!(i64, Integer, 64);
39    Reg
Reg { kind: RegKind::Integer, size: Size::from_bits(128) };reg_ctor!(i128, Integer, 128);
40
41    Reg
Reg { kind: RegKind::Float, size: Size::from_bits(32) };reg_ctor!(f32, Float, 32);
42    Reg
Reg { kind: RegKind::Float, size: Size::from_bits(64) };reg_ctor!(f64, Float, 64);
43    Reg
Reg { kind: RegKind::Float, size: Size::from_bits(128) };reg_ctor!(f128, Float, 128);
44
45    /// A vector of the given size with an unknown (and irrelevant) element type.
46    pub fn opaque_vector(size: Size) -> Reg {
47        // Default to an i8 vector of the given size.
48        Reg { kind: RegKind::Vector { hint_vector_elem: Primitive::Int(Integer::I8, true) }, size }
49    }
50}
51
52impl Reg {
53    pub fn align<C: HasDataLayout>(&self, cx: &C) -> Align {
54        let dl = cx.data_layout();
55        match self.kind {
56            RegKind::Integer => match self.size.bits() {
57                1 => dl.i1_align,
58                2..=8 => dl.i8_align,
59                9..=16 => dl.i16_align,
60                17..=32 => dl.i32_align,
61                33..=64 => dl.i64_align,
62                65..=128 => dl.i128_align,
63                _ => {
    ::core::panicking::panic_fmt(format_args!("unsupported integer: {0:?}",
            self));
}panic!("unsupported integer: {self:?}"),
64            },
65            RegKind::Float => match self.size.bits() {
66                16 => dl.f16_align,
67                32 => dl.f32_align,
68                64 => dl.f64_align,
69                128 => dl.f128_align,
70                _ => {
    ::core::panicking::panic_fmt(format_args!("unsupported float: {0:?}",
            self));
}panic!("unsupported float: {self:?}"),
71            },
72            RegKind::Vector { .. } => dl.llvmlike_vector_align(self.size),
73        }
74    }
75}