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