rustc_target/callconv/
sparc64.rs

1// FIXME: This needs an audit for correctness and completeness.
2
3use rustc_abi::{
4    BackendRepr, FieldsShape, Float, HasDataLayout, Primitive, Reg, Scalar, Size, TyAbiInterface,
5    TyAndLayout,
6};
7
8use crate::callconv::{ArgAbi, ArgAttribute, CastTarget, FnAbi, Uniform};
9use crate::spec::{Env, HasTargetSpec, Os};
10
11#[derive(Clone, Debug)]
12struct Sdata {
13    pub prefix: [Option<Reg>; 8],
14    pub prefix_index: usize,
15    pub last_offset: Size,
16    pub has_float: bool,
17    pub arg_attribute: ArgAttribute,
18}
19
20fn arg_scalar<C>(cx: &C, scalar: &Scalar, offset: Size, mut data: Sdata) -> Sdata
21where
22    C: HasDataLayout,
23{
24    let dl = cx.data_layout();
25
26    if !matches!(scalar.primitive(), Primitive::Float(Float::F32 | Float::F64)) {
27        return data;
28    }
29
30    data.has_float = true;
31
32    if !data.last_offset.is_aligned(dl.f64_align) && data.last_offset < offset {
33        if data.prefix_index == data.prefix.len() {
34            return data;
35        }
36        data.prefix[data.prefix_index] = Some(Reg::i32());
37        data.prefix_index += 1;
38        data.last_offset = data.last_offset + Reg::i32().size;
39    }
40
41    for _ in 0..((offset - data.last_offset).bits() / 64)
42        .min((data.prefix.len() - data.prefix_index) as u64)
43    {
44        data.prefix[data.prefix_index] = Some(Reg::i64());
45        data.prefix_index += 1;
46        data.last_offset = data.last_offset + Reg::i64().size;
47    }
48
49    if data.last_offset < offset {
50        if data.prefix_index == data.prefix.len() {
51            return data;
52        }
53        data.prefix[data.prefix_index] = Some(Reg::i32());
54        data.prefix_index += 1;
55        data.last_offset = data.last_offset + Reg::i32().size;
56    }
57
58    if data.prefix_index == data.prefix.len() {
59        return data;
60    }
61
62    if scalar.primitive() == Primitive::Float(Float::F32) {
63        data.arg_attribute = ArgAttribute::InReg;
64        data.prefix[data.prefix_index] = Some(Reg::f32());
65        data.last_offset = offset + Reg::f32().size;
66    } else {
67        data.prefix[data.prefix_index] = Some(Reg::f64());
68        data.last_offset = offset + Reg::f64().size;
69    }
70    data.prefix_index += 1;
71    data
72}
73
74fn arg_scalar_pair<C>(
75    cx: &C,
76    scalar1: &Scalar,
77    scalar2: &Scalar,
78    mut offset: Size,
79    mut data: Sdata,
80) -> Sdata
81where
82    C: HasDataLayout,
83{
84    data = arg_scalar(cx, scalar1, offset, data);
85    match (scalar1.primitive(), scalar2.primitive()) {
86        (Primitive::Float(Float::F32), _) => offset += Reg::f32().size,
87        (_, Primitive::Float(Float::F64)) => offset += Reg::f64().size,
88        (Primitive::Int(i, _signed), _) => offset += i.size(),
89        (Primitive::Pointer(_), _) => offset += Reg::i64().size,
90        _ => {}
91    }
92
93    if !offset.bytes().is_multiple_of(4)
94        && matches!(scalar2.primitive(), Primitive::Float(Float::F32 | Float::F64))
95    {
96        offset += Size::from_bytes(4 - (offset.bytes() % 4));
97    }
98    data = arg_scalar(cx, scalar2, offset, data);
99    data
100}
101
102fn parse_structure<'a, Ty, C>(
103    cx: &C,
104    layout: TyAndLayout<'a, Ty>,
105    mut data: Sdata,
106    mut offset: Size,
107) -> Sdata
108where
109    Ty: TyAbiInterface<'a, C> + Copy,
110    C: HasDataLayout,
111{
112    if let FieldsShape::Union(_) = layout.fields {
113        return data;
114    }
115
116    match layout.backend_repr {
117        BackendRepr::Scalar(scalar) => {
118            data = arg_scalar(cx, &scalar, offset, data);
119        }
120        BackendRepr::Memory { .. } => {
121            for i in 0..layout.fields.count() {
122                if offset < layout.fields.offset(i) {
123                    offset = layout.fields.offset(i);
124                }
125                data = parse_structure(cx, layout.field(cx, i), data.clone(), offset);
126            }
127        }
128        _ => {
129            if let BackendRepr::ScalarPair(scalar1, scalar2) = &layout.backend_repr {
130                data = arg_scalar_pair(cx, scalar1, scalar2, offset, data);
131            }
132        }
133    }
134
135    data
136}
137
138fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, in_registers_max: Size)
139where
140    Ty: TyAbiInterface<'a, C> + Copy,
141    C: HasDataLayout,
142{
143    if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
144        arg.make_indirect();
145        return;
146    }
147    if !arg.layout.is_aggregate() {
148        arg.extend_integer_width_to(64);
149        return;
150    }
151
152    let total = arg.layout.size;
153    if total > in_registers_max {
154        arg.make_indirect();
155        return;
156    }
157
158    match arg.layout.fields {
159        FieldsShape::Primitive => unreachable!(),
160        FieldsShape::Array { .. } => {
161            // Arrays are passed indirectly
162            arg.make_indirect();
163            return;
164        }
165        FieldsShape::Union(_) => {
166            // Unions and are always treated as a series of 64-bit integer chunks
167        }
168        FieldsShape::Arbitrary { .. } => {
169            // Structures with floating point numbers need special care.
170
171            let mut data = parse_structure(
172                cx,
173                arg.layout,
174                Sdata {
175                    prefix: [None; 8],
176                    prefix_index: 0,
177                    last_offset: Size::ZERO,
178                    has_float: false,
179                    arg_attribute: ArgAttribute::default(),
180                },
181                Size::ZERO,
182            );
183
184            if data.has_float {
185                // Structure { float, int, int } doesn't like to be handled like
186                // { float, long int }. Other way around it doesn't mind.
187                if data.last_offset < arg.layout.size
188                    && !data.last_offset.bytes().is_multiple_of(8)
189                    && data.prefix_index < data.prefix.len()
190                {
191                    data.prefix[data.prefix_index] = Some(Reg::i32());
192                    data.prefix_index += 1;
193                    data.last_offset += Reg::i32().size;
194                }
195
196                let mut rest_size = arg.layout.size - data.last_offset;
197                if !rest_size.bytes().is_multiple_of(8) && data.prefix_index < data.prefix.len() {
198                    data.prefix[data.prefix_index] = Some(Reg::i32());
199                    rest_size = rest_size - Reg::i32().size;
200                }
201
202                arg.cast_to(
203                    CastTarget::prefixed(data.prefix, Uniform::new(Reg::i64(), rest_size))
204                        .with_attrs(data.arg_attribute.into()),
205                );
206                return;
207            }
208        }
209    }
210
211    arg.cast_to(Uniform::new(Reg::i64(), total));
212}
213
214pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
215where
216    Ty: TyAbiInterface<'a, C> + Copy,
217    C: HasDataLayout + HasTargetSpec,
218{
219    if !fn_abi.ret.is_ignore() {
220        classify_arg(cx, &mut fn_abi.ret, Size::from_bytes(32));
221    }
222
223    for arg in fn_abi.args.iter_mut() {
224        if arg.is_ignore() {
225            // sparc64-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs.
226            if cx.target_spec().os == Os::Linux
227                && matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc)
228                && arg.layout.is_zst()
229            {
230                arg.make_indirect_from_ignore();
231            }
232            return;
233        }
234        classify_arg(cx, arg, Size::from_bytes(16));
235    }
236}