rustc_target/callconv/
wasm.rs

1use 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 {
14                val.cast_to(unit);
15                return true;
16            }
17        }
18    }
19    false
20}
21
22fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>)
23where
24    Ty: TyAbiInterface<'a, C> + Copy,
25    C: HasDataLayout,
26{
27    ret.extend_integer_width_to(32);
28    if ret.layout.is_aggregate() && !unwrap_trivial_aggregate(cx, ret) {
29        ret.make_indirect();
30    }
31
32    // `long double`, `__int128_t` and `__uint128_t` use an indirect return
33    if let BackendRepr::Scalar(scalar) = ret.layout.backend_repr {
34        match scalar.primitive() {
35            Primitive::Int(Integer::I128, _) | Primitive::Float(Float::F128) => {
36                ret.make_indirect();
37            }
38            _ => {}
39        }
40    }
41}
42
43fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
44where
45    Ty: TyAbiInterface<'a, C> + Copy,
46    C: HasDataLayout,
47{
48    if !arg.layout.is_sized() {
49        // Not touching this...
50        return;
51    }
52    arg.extend_integer_width_to(32);
53    if arg.layout.is_aggregate() && !unwrap_trivial_aggregate(cx, arg) {
54        arg.make_indirect();
55    }
56}
57
58/// The purpose of this ABI is to match the C ABI (aka clang) exactly.
59pub(crate) fn compute_c_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
60where
61    Ty: TyAbiInterface<'a, C> + Copy,
62    C: HasDataLayout,
63{
64    if !fn_abi.ret.is_ignore() {
65        classify_ret(cx, &mut fn_abi.ret);
66    }
67
68    for arg in fn_abi.args.iter_mut() {
69        if arg.is_ignore() {
70            continue;
71        }
72        classify_arg(cx, arg);
73    }
74}
75
76/// The purpose of this ABI is for matching the WebAssembly standard. This
77/// intentionally diverges from the C ABI and is specifically crafted to take
78/// advantage of LLVM's support of multiple returns in WebAssembly.
79///
80/// This ABI is *bad*! It uses `PassMode::Direct` for `abi::Aggregate` types, which leaks LLVM
81/// implementation details into the ABI. It's just hard to fix because ABIs are hard to change.
82/// Also see <https://github.com/rust-lang/rust/issues/115666>.
83pub(crate) fn compute_wasm_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
84    if !fn_abi.ret.is_ignore() {
85        classify_ret_wasm_abi(&mut fn_abi.ret);
86    }
87
88    for arg in fn_abi.args.iter_mut() {
89        if arg.is_ignore() {
90            continue;
91        }
92        classify_arg_wasm_abi(arg);
93    }
94
95    fn classify_ret_wasm_abi<Ty>(ret: &mut ArgAbi<'_, Ty>) {
96        if !ret.layout.is_sized() {
97            // Not touching this...
98            return;
99        }
100        // FIXME: this is bad! https://github.com/rust-lang/rust/issues/115666
101        ret.make_direct_deprecated();
102        ret.extend_integer_width_to(32);
103    }
104
105    fn classify_arg_wasm_abi<Ty>(arg: &mut ArgAbi<'_, Ty>) {
106        if !arg.layout.is_sized() {
107            // Not touching this...
108            return;
109        }
110        // FIXME: this is bad! https://github.com/rust-lang/rust/issues/115666
111        arg.make_direct_deprecated();
112        arg.extend_integer_width_to(32);
113    }
114}