Skip to main content

rustc_target/callconv/
s390x.rs

1// Reference: ELF Application Binary Interface s390x Supplement
2// https://github.com/IBM/s390x-abi
3
4use rustc_abi::{
5    BackendRepr, CanonAbi, FieldsShape, HasDataLayout, Primitive, TyAbiInterface, TyAndLayout,
6};
7
8use crate::callconv::{ArgAbi, ComputeCallConv, Reg};
9use crate::spec::{Env, HasTargetSpec, Os};
10
11/// Is this a struct with a single float field?
12fn is_single_fp_element<'a, Ty, C>(mut layout: TyAndLayout<'a, Ty>, cx: &C) -> bool
13where
14    Ty: TyAbiInterface<'a, C> + Copy,
15    C: HasDataLayout,
16{
17    // Contrary to X86, trailing padding is allowed on s390x.
18
19    loop {
20        // We're only looking for scalar types that are non-ZST.
21        layout = layout.peel_transparent_wrappers_from_non_1zst(cx);
22
23        return match layout.backend_repr {
24            BackendRepr::Scalar(scalar) => match scalar.primitive() {
25                Primitive::Float(_) => true,
26                Primitive::Bool | Primitive::Int(_, _) | Primitive::Pointer(_) => false,
27            },
28            BackendRepr::Memory { .. } => {
29                // A single-element array or union does not qualify.
30                if let FieldsShape::Arbitrary { .. } = layout.fields
31                    && layout.fields.count() == 1
32                    && layout.fields.offset(0).bytes() == 0
33                {
34                    layout = layout.field(cx, 0);
35                    continue;
36                } else {
37                    false
38                }
39            }
40            _ => false,
41        };
42    }
43}
44
45fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
46    let size = ret.layout.size;
47    if size.bits() <= 128 && #[allow(non_exhaustive_omitted_patterns)] match ret.layout.backend_repr {
    BackendRepr::SimdVector { .. } => true,
    _ => false,
}matches!(ret.layout.backend_repr, BackendRepr::SimdVector { .. }) {
48        return;
49    }
50    if !ret.layout.is_aggregate() && size.bits() <= 64 {
51        ret.extend_integer_width_to(64);
52    } else {
53        ret.make_indirect();
54    }
55}
56
57fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
58where
59    Ty: TyAbiInterface<'a, C> + Copy,
60    C: HasDataLayout + HasTargetSpec,
61{
62    if !arg.layout.is_sized() {
63        // Not touching this...
64        return;
65    }
66    if arg.is_ignore() {
67        // s390x-unknown-linux-{gnu,musl,uclibc} doesn't ignore ZSTs.
68        if cx.target_spec().os == Os::Linux
69            && #[allow(non_exhaustive_omitted_patterns)] match cx.target_spec().env {
    Env::Gnu | Env::Musl | Env::Uclibc => true,
    _ => false,
}matches!(cx.target_spec().env, Env::Gnu | Env::Musl | Env::Uclibc)
70        {
71            arg.make_indirect_from_ignore();
72        }
73        return;
74    }
75    if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
76        arg.make_indirect();
77        return;
78    }
79
80    if arg.layout.is_complex_number(cx) {
81        arg.make_indirect();
82        return;
83    }
84
85    let size = arg.layout.size;
86    if size.bits() <= 128 {
87        if let BackendRepr::SimdVector { .. } = arg.layout.backend_repr {
88            // pass non-wrapped vector types using `PassMode::Direct`
89            return;
90        }
91
92        if arg.layout.is_single_vector_element(cx, size) {
93            // pass non-transparent wrappers around a vector as `PassMode::Cast`
94            arg.cast_to(Reg::opaque_vector(size));
95            return;
96        }
97    }
98    if !arg.layout.is_aggregate() && size.bits() <= 64 {
99        arg.extend_integer_width_to(64);
100        return;
101    }
102
103    if is_single_fp_element(arg.layout, cx) {
104        // Match GCC and Clang by explicitly passing padding, even though their behavior violates
105        // (our reading of) the specification, which says that:
106        //
107        // > Structures equivalent to a floating point type are passed in floating point registers.
108        // > A structure is equivalent to a floating point type if and only if it has exactly one
109        // > member, which is either of floating point type of itself a structure equivalent to a
110        // > floating point type.
111        //
112        // When the alignment is higher than 8, we pass the argument indirectly, which violates
113        // the specification but is consistent with GCC and Clang.
114        match size.bytes() {
115            2 => arg.cast_to(Reg::f16()),
116            4 => arg.cast_to(Reg::f32()),
117            8 => arg.cast_to(Reg::f64()),
118            _ => arg.make_indirect(),
119        }
120    } else {
121        match size.bytes() {
122            1 => arg.cast_to(Reg::i8()),
123            2 => arg.cast_to(Reg::i16()),
124            4 => arg.cast_to(Reg::i32()),
125            8 => arg.cast_to(Reg::i64()),
126            _ => arg.make_indirect(),
127        }
128    }
129}
130
131pub(crate) struct S390XCallConv;
132
133impl<'a, Ty, C> ComputeCallConv<'a, Ty, C> for S390XCallConv
134where
135    Ty: TyAbiInterface<'a, C> + Copy,
136    C: HasDataLayout + HasTargetSpec,
137{
138    fn new(_cx: &C, _conv: CanonAbi, _c_variadic: bool) -> Self {
139        Self
140    }
141
142    fn passes_zsts(&self) -> bool {
143        true
144    }
145
146    fn compute_ret_abi(&mut self, _cx: &C, ret: &mut ArgAbi<'a, Ty>) {
147        if !ret.is_ignore() {
148            classify_ret(ret);
149        }
150    }
151
152    fn compute_arg_abi(&mut self, cx: &C, arg: &mut ArgAbi<'a, Ty>, _is_vararg: bool) {
153        classify_arg(cx, arg);
154    }
155}