]> git.lizzy.rs Git - rust.git/blob - src/librustc_target/abi/call/arm.rs
Auto merge of #68414 - michaelwoerister:share-drop-glue, r=alexcrichton
[rust.git] / src / librustc_target / abi / call / arm.rs
1 use crate::abi::call::{ArgAbi, Conv, FnAbi, Reg, RegKind, Uniform};
2 use crate::abi::{HasDataLayout, LayoutOf, TyLayout, TyLayoutMethods};
3 use crate::spec::HasTargetSpec;
4
5 fn is_homogeneous_aggregate<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>) -> Option<Uniform>
6 where
7     Ty: TyLayoutMethods<'a, C> + Copy,
8     C: LayoutOf<Ty = Ty, TyLayout = TyLayout<'a, Ty>> + HasDataLayout,
9 {
10     arg.layout.homogeneous_aggregate(cx).unit().and_then(|unit| {
11         let size = arg.layout.size;
12
13         // Ensure we have at most four uniquely addressable members.
14         if size > unit.size.checked_mul(4, cx).unwrap() {
15             return None;
16         }
17
18         let valid_unit = match unit.kind {
19             RegKind::Integer => false,
20             RegKind::Float => true,
21             RegKind::Vector => size.bits() == 64 || size.bits() == 128,
22         };
23
24         valid_unit.then_some(Uniform { unit, total: size })
25     })
26 }
27
28 fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>, vfp: bool)
29 where
30     Ty: TyLayoutMethods<'a, C> + Copy,
31     C: LayoutOf<Ty = Ty, TyLayout = TyLayout<'a, Ty>> + HasDataLayout,
32 {
33     if !ret.layout.is_aggregate() {
34         ret.extend_integer_width_to(32);
35         return;
36     }
37
38     if vfp {
39         if let Some(uniform) = is_homogeneous_aggregate(cx, ret) {
40             ret.cast_to(uniform);
41             return;
42         }
43     }
44
45     let size = ret.layout.size;
46     let bits = size.bits();
47     if bits <= 32 {
48         let unit = if bits <= 8 {
49             Reg::i8()
50         } else if bits <= 16 {
51             Reg::i16()
52         } else {
53             Reg::i32()
54         };
55         ret.cast_to(Uniform { unit, total: size });
56         return;
57     }
58     ret.make_indirect();
59 }
60
61 fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, vfp: bool)
62 where
63     Ty: TyLayoutMethods<'a, C> + Copy,
64     C: LayoutOf<Ty = Ty, TyLayout = TyLayout<'a, Ty>> + HasDataLayout,
65 {
66     if !arg.layout.is_aggregate() {
67         arg.extend_integer_width_to(32);
68         return;
69     }
70
71     if vfp {
72         if let Some(uniform) = is_homogeneous_aggregate(cx, arg) {
73             arg.cast_to(uniform);
74             return;
75         }
76     }
77
78     let align = arg.layout.align.abi.bytes();
79     let total = arg.layout.size;
80     arg.cast_to(Uniform { unit: if align <= 4 { Reg::i32() } else { Reg::i64() }, total });
81 }
82
83 pub fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
84 where
85     Ty: TyLayoutMethods<'a, C> + Copy,
86     C: LayoutOf<Ty = Ty, TyLayout = TyLayout<'a, Ty>> + HasDataLayout + HasTargetSpec,
87 {
88     // If this is a target with a hard-float ABI, and the function is not explicitly
89     // `extern "aapcs"`, then we must use the VFP registers for homogeneous aggregates.
90     let vfp = cx.target_spec().llvm_target.ends_with("hf")
91         && fn_abi.conv != Conv::ArmAapcs
92         && !fn_abi.c_variadic;
93
94     if !fn_abi.ret.is_ignore() {
95         classify_ret(cx, &mut fn_abi.ret, vfp);
96     }
97
98     for arg in &mut fn_abi.args {
99         if arg.is_ignore() {
100             continue;
101         }
102         classify_arg(cx, arg, vfp);
103     }
104 }