rustc_target/callconv/
s390x.rs1use 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
11fn 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 loop {
20 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 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 return;
65 }
66 if arg.is_ignore() {
67 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 return;
90 }
91
92 if arg.layout.is_single_vector_element(cx, size) {
93 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 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}