Skip to main content

rustc_target/callconv/
arm.rs

1use rustc_abi::{ArmCall, CanonAbi, HasDataLayout, TyAbiInterface};
2
3use crate::callconv::{ArgAbi, ComputeCallConv, Reg, RegKind, Uniform};
4use crate::spec::{CfgAbi, HasTargetSpec, Os};
5
6#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for ArmAbiKind {
}
#[automatically_derived]impl ::core::clone::Clone for ArmAbiKind {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::Copy for ArmAbiKind { }Copy)]
7enum ArmAbiKind {
8    Aapcs,
9    AapcsVfp,
10    Aapcs16Vfp,
11}
12
13fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>) -> Option<Uniform>
14where
15    Ty: TyAbiInterface<'a, C> + Copy,
16    C: HasDataLayout,
17{
18    arg.layout.homogeneous_aggregate(cx).ok().and_then(|ha| ha.unit()).and_then(|unit| {
19        let size = arg.layout.size;
20
21        // Ensure we have at most four uniquely addressable members.
22        if size > unit.size.checked_mul(4, cx).unwrap() {
23            return None;
24        }
25
26        let valid_unit = match unit.kind {
27            RegKind::Integer => false,
28            RegKind::Float => unit.size.bits() == 32 || unit.size.bits() == 64,
29            RegKind::Vector { .. } => unit.size.bits() == 64 || unit.size.bits() == 128,
30            RegKind::PpcF128 => ::core::panicking::unreachable_default_msg()unreachable!(),
31        };
32
33        valid_unit.then_some(Uniform::consecutive(unit, size))
34    })
35}
36
37fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>, abi_kind: ArmAbiKind, vfp: bool)
38where
39    Ty: TyAbiInterface<'a, C> + Copy,
40    C: HasDataLayout,
41{
42    if !ret.layout.is_sized() {
43        // Not touching this...
44        return;
45    }
46    if !ret.layout.is_aggregate() {
47        ret.extend_integer_width_to(32);
48        return;
49    }
50
51    if vfp {
52        if let Some(uniform) = is_homogeneous_aggregate(cx, ret) {
53            ret.cast_to(uniform);
54            return;
55        }
56    }
57
58    let size = ret.layout.size;
59    let bits = size.bits();
60
61    if bits <= 32 {
62        // Aggregates <= 4 bytes are returned in r0; other aggregates are returned indirectly.
63        ret.cast_to(Uniform::new(Reg::i32(), size));
64        return;
65    } else if #[allow(non_exhaustive_omitted_patterns)] match abi_kind {
    ArmAbiKind::Aapcs16Vfp => true,
    _ => false,
}matches!(abi_kind, ArmAbiKind::Aapcs16Vfp) && bits <= 128 {
66        // watchOS returns the remaining aggregates of up to 128 bits in GPRs.
67        ret.cast_to(Uniform::consecutive(Reg::i32(), size));
68        return;
69    }
70
71    ret.make_indirect();
72}
73
74fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, abi_kind: ArmAbiKind, vfp: bool)
75where
76    Ty: TyAbiInterface<'a, C> + Copy,
77    C: HasDataLayout,
78{
79    if !arg.layout.is_sized() {
80        // Not touching this...
81        return;
82    }
83    if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
84        arg.make_indirect();
85        return;
86    }
87    if !arg.layout.is_aggregate() {
88        arg.extend_integer_width_to(32);
89        return;
90    }
91
92    // watchOS also passes homogeneous aggregates in VFP registers, and unlike `AapcsVfp` it does
93    // so even for variadics and for `extern "aapcs"`: the backend will use GPRs if needed.
94    if vfp || #[allow(non_exhaustive_omitted_patterns)] match abi_kind {
    ArmAbiKind::Aapcs16Vfp => true,
    _ => false,
}matches!(abi_kind, ArmAbiKind::Aapcs16Vfp) {
95        if let Some(uniform) = is_homogeneous_aggregate(cx, arg) {
96            arg.cast_to(uniform);
97            return;
98        }
99    }
100
101    // For the composites that are left, watchOS adopts the 64-bit AAPCS rule: those larger than
102    // 128 bits are placed in space allocated by the caller, and a pointer is passed.
103    if #[allow(non_exhaustive_omitted_patterns)] match abi_kind {
    ArmAbiKind::Aapcs16Vfp => true,
    _ => false,
}matches!(abi_kind, ArmAbiKind::Aapcs16Vfp) && arg.layout.size.bits() > 128 {
104        arg.make_indirect();
105        return;
106    }
107
108    let align = match abi_kind {
109        ArmAbiKind::Aapcs | ArmAbiKind::AapcsVfp => arg.layout.unadjusted_abi_align.bytes(),
110        ArmAbiKind::Aapcs16Vfp => arg.layout.align.bytes(),
111    };
112
113    let total = arg.layout.size;
114    arg.cast_to(Uniform::consecutive(if align <= 4 { Reg::i32() } else { Reg::i64() }, total));
115}
116
117pub(crate) struct ArmCallConv {
118    abi_kind: ArmAbiKind,
119    conv: CanonAbi,
120    c_variadic: bool,
121}
122
123impl ArmCallConv {
124    /// Whether we must use the VFP registers for homogeneous aggregates.
125    fn is_effectively_vfp(&self, conv: CanonAbi, accept_aapcs16: bool) -> bool {
126        // When the user requested aapcs explicitly, honor that.
127        if #[allow(non_exhaustive_omitted_patterns)] match conv {
    CanonAbi::Arm(ArmCall::Aapcs) => true,
    _ => false,
}matches!(conv, CanonAbi::Arm(ArmCall::Aapcs)) {
128            return false;
129        }
130
131        match self.abi_kind {
132            ArmAbiKind::AapcsVfp => true,
133            ArmAbiKind::Aapcs16Vfp => accept_aapcs16,
134            ArmAbiKind::Aapcs => false,
135        }
136    }
137}
138
139impl<'a, Ty, C> ComputeCallConv<'a, Ty, C> for ArmCallConv
140where
141    Ty: TyAbiInterface<'a, C> + Copy,
142    C: HasDataLayout + HasTargetSpec,
143{
144    fn new(cx: &C, conv: CanonAbi, c_variadic: bool) -> Self {
145        let target = cx.target_spec();
146        let abi_kind = if target.os == Os::WatchOs {
147            ArmAbiKind::Aapcs16Vfp
148        } else if target.cfg_abi == CfgAbi::EabiHf || target.os == Os::Windows {
149            ArmAbiKind::AapcsVfp
150        } else {
151            ArmAbiKind::Aapcs
152        };
153        ArmCallConv { abi_kind, conv, c_variadic }
154    }
155
156    fn compute_ret_abi(&mut self, cx: &C, ret: &mut ArgAbi<'a, Ty>) {
157        classify_ret(
158            cx,
159            ret,
160            self.abi_kind,
161            !self.c_variadic && self.is_effectively_vfp(self.conv, true),
162        );
163    }
164
165    fn compute_arg_abi(&mut self, cx: &C, arg: &mut ArgAbi<'a, Ty>, _is_vararg: bool) {
166        let is_arg_vfp = !self.c_variadic && self.is_effectively_vfp(self.conv, false);
167        classify_arg(cx, arg, self.abi_kind, is_arg_vfp);
168    }
169}