1 use crate::abi::call::{ArgAbi, FnAbi, Reg, RegKind, Uniform};
2 use crate::abi::{HasDataLayout, TyAbiInterface};
4 /// Given integer-types M and register width N (e.g. M=u16 and N=32 bits), the
5 /// `ParamExtension` policy specifies how a uM value should be treated when
6 /// passed via register or stack-slot of width N. See also rust-lang/rust#97463.
7 #[derive(Copy, Clone, PartialEq)]
8 pub enum ParamExtension {
9 /// Indicates that when passing an i8/i16, either as a function argument or
10 /// as a return value, it must be sign-extended to 32 bits, and likewise a
11 /// u8/u16 must be zero-extended to 32-bits. (This variant is here to
12 /// accommodate Apple's deviation from the usual AArch64 ABI as defined by
15 /// See also: <https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Pass-Arguments-to-Functions-Correctly>
18 /// Indicates that no sign- nor zero-extension is performed: if a value of
19 /// type with bitwidth M is passed as function argument or return value,
20 /// then M bits are copied into the least significant M bits, and the
21 /// remaining bits of the register (or word of memory) are untouched.
25 fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>) -> Option<Uniform>
27 Ty: TyAbiInterface<'a, C> + Copy,
30 arg.layout.homogeneous_aggregate(cx).ok().and_then(|ha| ha.unit()).and_then(|unit| {
31 let size = arg.layout.size;
33 // Ensure we have at most four uniquely addressable members.
34 if size > unit.size.checked_mul(4, cx).unwrap() {
38 let valid_unit = match unit.kind {
39 RegKind::Integer => false,
40 RegKind::Float => true,
41 RegKind::Vector => size.bits() == 64 || size.bits() == 128,
44 valid_unit.then_some(Uniform { unit, total: size })
48 fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>, param_policy: ParamExtension)
50 Ty: TyAbiInterface<'a, C> + Copy,
53 if !ret.layout.is_aggregate() {
55 ParamExtension::ExtendTo32Bits => ret.extend_integer_width_to(32),
56 ParamExtension::NoExtension => {}
60 if let Some(uniform) = is_homogeneous_aggregate(cx, ret) {
64 let size = ret.layout.size;
65 let bits = size.bits();
67 ret.cast_to(Uniform { unit: Reg::i64(), total: size });
73 fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, param_policy: ParamExtension)
75 Ty: TyAbiInterface<'a, C> + Copy,
78 if !arg.layout.is_aggregate() {
80 ParamExtension::ExtendTo32Bits => arg.extend_integer_width_to(32),
81 ParamExtension::NoExtension => {}
85 if let Some(uniform) = is_homogeneous_aggregate(cx, arg) {
89 let size = arg.layout.size;
90 let bits = size.bits();
92 arg.cast_to(Uniform { unit: Reg::i64(), total: size });
98 pub fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>, param_policy: ParamExtension)
100 Ty: TyAbiInterface<'a, C> + Copy,
103 if !fn_abi.ret.is_ignore() {
104 classify_ret(cx, &mut fn_abi.ret, param_policy);
107 for arg in fn_abi.args.iter_mut() {
111 classify_arg(cx, arg, param_policy);