1 use crate::abi::call::{
2 ArgAbi, ArgAttribute, ArgAttributes, ArgExtension, CastTarget, FnAbi, PassMode, Reg, Uniform,
4 use crate::abi::{self, HasDataLayout, Size, TyAbiInterface};
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);
19 arg.extend_integer_width_to(bits);
22 fn float_reg<'a, Ty, C>(cx: &C, ret: &ArgAbi<'a, Ty>, i: usize) -> Option<Reg>
24 Ty: TyAbiInterface<'a, C> + Copy,
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()),
37 fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>)
39 Ty: TyAbiInterface<'a, C> + Copy,
42 if !ret.layout.is_aggregate() {
43 extend_integer_width_mips(ret, 64);
47 let size = ret.layout.size;
48 let bits = size.bits();
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
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) {
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));
70 // Cast to a uniform int structure
71 ret.cast_to(Uniform { unit: Reg::i64(), total: size });
77 fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
79 Ty: TyAbiInterface<'a, C> + Copy,
82 if !arg.layout.is_aggregate() {
83 extend_integer_width_mips(arg, 64);
87 let dl = cx.data_layout();
88 let size = arg.layout.size;
89 let mut prefix = [None; 8];
90 let mut prefix_index = 0;
92 match arg.layout.fields {
93 abi::FieldsShape::Primitive => unreachable!(),
94 abi::FieldsShape::Array { .. } => {
95 // Arrays are passed indirectly
99 abi::FieldsShape::Union(_) => {
100 // Unions and are always treated as a series of 64-bit integer chunks
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;
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);
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)
120 prefix[prefix_index] = Some(Reg::i64());
124 if prefix_index == prefix.len() {
128 prefix[prefix_index] = Some(Reg::f64());
130 last_offset = offset + Reg::f64().size;
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 {
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,
152 pub fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
154 Ty: TyAbiInterface<'a, C> + Copy,
157 if !fn_abi.ret.is_ignore() {
158 classify_ret(cx, &mut fn_abi.ret);
161 for arg in fn_abi.args.iter_mut() {
165 classify_arg(cx, arg);