]> git.lizzy.rs Git - rust.git/blob - src/librustc_target/abi/call/powerpc64.rs
Auto merge of #68414 - michaelwoerister:share-drop-glue, r=alexcrichton
[rust.git] / src / librustc_target / abi / call / powerpc64.rs
1 // FIXME:
2 // Alignment of 128 bit types is not currently handled, this will
3 // need to be fixed when PowerPC vector support is added.
4
5 use crate::abi::call::{ArgAbi, FnAbi, Reg, RegKind, Uniform};
6 use crate::abi::{Endian, HasDataLayout, LayoutOf, TyLayout, TyLayoutMethods};
7 use crate::spec::HasTargetSpec;
8
9 #[derive(Debug, Clone, Copy, PartialEq)]
10 enum ABI {
11     ELFv1, // original ABI used for powerpc64 (big-endian)
12     ELFv2, // newer ABI used for powerpc64le and musl (both endians)
13 }
14 use ABI::*;
15
16 fn is_homogeneous_aggregate<'a, Ty, C>(
17     cx: &C,
18     arg: &mut ArgAbi<'a, Ty>,
19     abi: ABI,
20 ) -> Option<Uniform>
21 where
22     Ty: TyLayoutMethods<'a, C> + Copy,
23     C: LayoutOf<Ty = Ty, TyLayout = TyLayout<'a, Ty>> + HasDataLayout,
24 {
25     arg.layout.homogeneous_aggregate(cx).unit().and_then(|unit| {
26         // ELFv1 only passes one-member aggregates transparently.
27         // ELFv2 passes up to eight uniquely addressable members.
28         if (abi == ELFv1 && arg.layout.size > unit.size)
29             || arg.layout.size > unit.size.checked_mul(8, cx).unwrap()
30         {
31             return None;
32         }
33
34         let valid_unit = match unit.kind {
35             RegKind::Integer => false,
36             RegKind::Float => true,
37             RegKind::Vector => arg.layout.size.bits() == 128,
38         };
39
40         valid_unit.then_some(Uniform { unit, total: arg.layout.size })
41     })
42 }
43
44 fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>, abi: ABI)
45 where
46     Ty: TyLayoutMethods<'a, C> + Copy,
47     C: LayoutOf<Ty = Ty, TyLayout = TyLayout<'a, Ty>> + HasDataLayout,
48 {
49     if !ret.layout.is_aggregate() {
50         ret.extend_integer_width_to(64);
51         return;
52     }
53
54     // The ELFv1 ABI doesn't return aggregates in registers
55     if abi == ELFv1 {
56         ret.make_indirect();
57         return;
58     }
59
60     if let Some(uniform) = is_homogeneous_aggregate(cx, ret, abi) {
61         ret.cast_to(uniform);
62         return;
63     }
64
65     let size = ret.layout.size;
66     let bits = size.bits();
67     if bits <= 128 {
68         let unit = if cx.data_layout().endian == Endian::Big {
69             Reg { kind: RegKind::Integer, size }
70         } else if bits <= 8 {
71             Reg::i8()
72         } else if bits <= 16 {
73             Reg::i16()
74         } else if bits <= 32 {
75             Reg::i32()
76         } else {
77             Reg::i64()
78         };
79
80         ret.cast_to(Uniform { unit, total: size });
81         return;
82     }
83
84     ret.make_indirect();
85 }
86
87 fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, abi: ABI)
88 where
89     Ty: TyLayoutMethods<'a, C> + Copy,
90     C: LayoutOf<Ty = Ty, TyLayout = TyLayout<'a, Ty>> + HasDataLayout,
91 {
92     if !arg.layout.is_aggregate() {
93         arg.extend_integer_width_to(64);
94         return;
95     }
96
97     if let Some(uniform) = is_homogeneous_aggregate(cx, arg, abi) {
98         arg.cast_to(uniform);
99         return;
100     }
101
102     let size = arg.layout.size;
103     let (unit, total) = if size.bits() <= 64 {
104         // Aggregates smaller than a doubleword should appear in
105         // the least-significant bits of the parameter doubleword.
106         (Reg { kind: RegKind::Integer, size }, size)
107     } else {
108         // Aggregates larger than a doubleword should be padded
109         // at the tail to fill out a whole number of doublewords.
110         let reg_i64 = Reg::i64();
111         (reg_i64, size.align_to(reg_i64.align(cx)))
112     };
113
114     arg.cast_to(Uniform { unit, total });
115 }
116
117 pub fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
118 where
119     Ty: TyLayoutMethods<'a, C> + Copy,
120     C: LayoutOf<Ty = Ty, TyLayout = TyLayout<'a, Ty>> + HasDataLayout + HasTargetSpec,
121 {
122     let abi = if cx.target_spec().target_env == "musl" {
123         ELFv2
124     } else {
125         match cx.data_layout().endian {
126             Endian::Big => ELFv1,
127             Endian::Little => ELFv2,
128         }
129     };
130
131     if !fn_abi.ret.is_ignore() {
132         classify_ret(cx, &mut fn_abi.ret, abi);
133     }
134
135     for arg in &mut fn_abi.args {
136         if arg.is_ignore() {
137             continue;
138         }
139         classify_arg(cx, arg, abi);
140     }
141 }