]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_target/src/abi/call/mips64.rs
Auto merge of #106959 - tmiasko:opt-funclets, r=davidtwco
[rust.git] / compiler / rustc_target / src / abi / call / mips64.rs
1 use crate::abi::call::{
2     ArgAbi, ArgAttribute, ArgAttributes, ArgExtension, CastTarget, FnAbi, PassMode, Reg, Uniform,
3 };
4 use crate::abi::{self, HasDataLayout, Size, TyAbiInterface};
5
6 fn extend_integer_width_mips<Ty>(arg: &mut ArgAbi<'_, Ty>, bits: u64) {
7     // Always sign extend u32 values on 64-bit mips
8     if let abi::Abi::Scalar(scalar) = arg.layout.abi {
9         if let abi::Int(i, signed) = scalar.primitive() {
10             if !signed && i.size().bits() == 32 {
11                 if let PassMode::Direct(ref mut attrs) = arg.mode {
12                     attrs.ext(ArgExtension::Sext);
13                     return;
14                 }
15             }
16         }
17     }
18
19     arg.extend_integer_width_to(bits);
20 }
21
22 fn float_reg<'a, Ty, C>(cx: &C, ret: &ArgAbi<'a, Ty>, i: usize) -> Option<Reg>
23 where
24     Ty: TyAbiInterface<'a, C> + Copy,
25     C: HasDataLayout,
26 {
27     match ret.layout.field(cx, i).abi {
28         abi::Abi::Scalar(scalar) => match scalar.primitive() {
29             abi::F32 => Some(Reg::f32()),
30             abi::F64 => Some(Reg::f64()),
31             _ => None,
32         },
33         _ => None,
34     }
35 }
36
37 fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>)
38 where
39     Ty: TyAbiInterface<'a, C> + Copy,
40     C: HasDataLayout,
41 {
42     if !ret.layout.is_aggregate() {
43         extend_integer_width_mips(ret, 64);
44         return;
45     }
46
47     let size = ret.layout.size;
48     let bits = size.bits();
49     if bits <= 128 {
50         // Unlike other architectures which return aggregates in registers, MIPS n64 limits the
51         // use of float registers to structures (not unions) containing exactly one or two
52         // float fields.
53
54         if let abi::FieldsShape::Arbitrary { .. } = ret.layout.fields {
55             if ret.layout.fields.count() == 1 {
56                 if let Some(reg) = float_reg(cx, ret, 0) {
57                     ret.cast_to(reg);
58                     return;
59                 }
60             } else if ret.layout.fields.count() == 2 {
61                 if let Some(reg0) = float_reg(cx, ret, 0) {
62                     if let Some(reg1) = float_reg(cx, ret, 1) {
63                         ret.cast_to(CastTarget::pair(reg0, reg1));
64                         return;
65                     }
66                 }
67             }
68         }
69
70         // Cast to a uniform int structure
71         ret.cast_to(Uniform { unit: Reg::i64(), total: size });
72     } else {
73         ret.make_indirect();
74     }
75 }
76
77 fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
78 where
79     Ty: TyAbiInterface<'a, C> + Copy,
80     C: HasDataLayout,
81 {
82     if !arg.layout.is_aggregate() {
83         extend_integer_width_mips(arg, 64);
84         return;
85     }
86
87     let dl = cx.data_layout();
88     let size = arg.layout.size;
89     let mut prefix = [None; 8];
90     let mut prefix_index = 0;
91
92     match arg.layout.fields {
93         abi::FieldsShape::Primitive => unreachable!(),
94         abi::FieldsShape::Array { .. } => {
95             // Arrays are passed indirectly
96             arg.make_indirect();
97             return;
98         }
99         abi::FieldsShape::Union(_) => {
100             // Unions and are always treated as a series of 64-bit integer chunks
101         }
102         abi::FieldsShape::Arbitrary { .. } => {
103             // Structures are split up into a series of 64-bit integer chunks, but any aligned
104             // doubles not part of another aggregate are passed as floats.
105             let mut last_offset = Size::ZERO;
106
107             for i in 0..arg.layout.fields.count() {
108                 let field = arg.layout.field(cx, i);
109                 let offset = arg.layout.fields.offset(i);
110
111                 // We only care about aligned doubles
112                 if let abi::Abi::Scalar(scalar) = field.abi {
113                     if let abi::F64 = scalar.primitive() {
114                         if offset.is_aligned(dl.f64_align.abi) {
115                             // Insert enough integers to cover [last_offset, offset)
116                             assert!(last_offset.is_aligned(dl.f64_align.abi));
117                             for _ in 0..((offset - last_offset).bits() / 64)
118                                 .min((prefix.len() - prefix_index) as u64)
119                             {
120                                 prefix[prefix_index] = Some(Reg::i64());
121                                 prefix_index += 1;
122                             }
123
124                             if prefix_index == prefix.len() {
125                                 break;
126                             }
127
128                             prefix[prefix_index] = Some(Reg::f64());
129                             prefix_index += 1;
130                             last_offset = offset + Reg::f64().size;
131                         }
132                     }
133                 }
134             }
135         }
136     };
137
138     // Extract first 8 chunks as the prefix
139     let rest_size = size - Size::from_bytes(8) * prefix_index as u64;
140     arg.cast_to(CastTarget {
141         prefix,
142         rest: Uniform { unit: Reg::i64(), total: rest_size },
143         attrs: ArgAttributes {
144             regular: ArgAttribute::default(),
145             arg_ext: ArgExtension::None,
146             pointee_size: Size::ZERO,
147             pointee_align: None,
148         },
149     });
150 }
151
152 pub fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
153 where
154     Ty: TyAbiInterface<'a, C> + Copy,
155     C: HasDataLayout,
156 {
157     if !fn_abi.ret.is_ignore() {
158         classify_ret(cx, &mut fn_abi.ret);
159     }
160
161     for arg in fn_abi.args.iter_mut() {
162         if arg.is_ignore() {
163             continue;
164         }
165         classify_arg(cx, arg);
166     }
167 }