rustc_target/callconv/
wasm.rs1use rustc_abi::{BackendRepr, Float, HasDataLayout, Integer, Primitive, TyAbiInterface};
2
3use crate::callconv::{ArgAbi, FnAbi};
4
5fn unwrap_trivial_aggregate<'a, Ty, C>(cx: &C, val: &mut ArgAbi<'a, Ty>) -> bool
6where
7 Ty: TyAbiInterface<'a, C> + Copy,
8 C: HasDataLayout,
9{
10 if val.layout.is_aggregate() {
11 if let Some(unit) = val.layout.homogeneous_aggregate(cx).ok().and_then(|ha| ha.unit()) {
12 let size = val.layout.size;
13 if unit.size == size {
17 val.cast_to(unit);
18 return true;
19 }
20 }
21 }
22 false
23}
24
25fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>)
26where
27 Ty: TyAbiInterface<'a, C> + Copy,
28 C: HasDataLayout,
29{
30 ret.extend_integer_width_to(32);
31 if ret.layout.is_aggregate() && !unwrap_trivial_aggregate(cx, ret) {
32 ret.make_indirect();
33 }
34
35 if let BackendRepr::Scalar(scalar) = ret.layout.backend_repr {
37 match scalar.primitive() {
38 Primitive::Int(Integer::I128, _) | Primitive::Float(Float::F128) => {
39 ret.make_indirect();
40 }
41 _ => {}
42 }
43 }
44}
45
46fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
47where
48 Ty: TyAbiInterface<'a, C> + Copy,
49 C: HasDataLayout,
50{
51 if !arg.layout.is_sized() {
52 return;
54 }
55 if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
56 arg.make_indirect();
57 return;
58 }
59 arg.extend_integer_width_to(32);
60 if arg.layout.is_aggregate() && !unwrap_trivial_aggregate(cx, arg) {
61 arg.make_indirect();
62 }
63}
64
65pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
67where
68 Ty: TyAbiInterface<'a, C> + Copy,
69 C: HasDataLayout,
70{
71 if !fn_abi.ret.is_ignore() {
72 classify_ret(cx, &mut fn_abi.ret);
73 }
74
75 for arg in fn_abi.args.iter_mut() {
76 if arg.is_ignore() {
77 continue;
78 }
79 classify_arg(cx, arg);
80 }
81}