]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_target/src/abi/call/wasm.rs
c3187dff7af1924238f325d4cd3769e8106009a9
[rust.git] / compiler / rustc_target / src / abi / call / wasm.rs
1 use crate::abi::call::{ArgAbi, FnAbi, Uniform};
2 use crate::abi::{HasDataLayout, LayoutOf, TyAndLayout, TyAndLayoutMethods};
3
4 fn unwrap_trivial_aggregate<'a, Ty, C>(cx: &C, val: &mut ArgAbi<'a, Ty>) -> bool
5 where
6     Ty: TyAndLayoutMethods<'a, C> + Copy,
7     C: LayoutOf<'a, Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
8 {
9     if val.layout.is_aggregate() {
10         if let Some(unit) = val.layout.homogeneous_aggregate(cx).ok().and_then(|ha| ha.unit()) {
11             let size = val.layout.size;
12             if unit.size == size {
13                 val.cast_to(Uniform { unit, total: size });
14                 return true;
15             }
16         }
17     }
18     false
19 }
20
21 fn classify_ret<'a, Ty, C>(cx: &C, ret: &mut ArgAbi<'a, Ty>)
22 where
23     Ty: TyAndLayoutMethods<'a, C> + Copy,
24     C: LayoutOf<'a, Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
25 {
26     ret.extend_integer_width_to(32);
27     if ret.layout.is_aggregate() && !unwrap_trivial_aggregate(cx, ret) {
28         ret.make_indirect();
29     }
30 }
31
32 fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
33 where
34     Ty: TyAndLayoutMethods<'a, C> + Copy,
35     C: LayoutOf<'a, Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
36 {
37     arg.extend_integer_width_to(32);
38     if arg.layout.is_aggregate() && !unwrap_trivial_aggregate(cx, arg) {
39         arg.make_indirect_byval();
40     }
41 }
42
43 /// The purpose of this ABI is to match the C ABI (aka clang) exactly.
44 pub fn compute_c_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
45 where
46     Ty: TyAndLayoutMethods<'a, C> + Copy,
47     C: LayoutOf<'a, Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout,
48 {
49     if !fn_abi.ret.is_ignore() {
50         classify_ret(cx, &mut fn_abi.ret);
51     }
52
53     for arg in &mut fn_abi.args {
54         if arg.is_ignore() {
55             continue;
56         }
57         classify_arg(cx, arg);
58     }
59 }
60
61 /// The purpose of this ABI is for matching the WebAssembly standard. This
62 /// intentionally diverges from the C ABI and is specifically crafted to take
63 /// advantage of LLVM's support of multiple returns in WebAssembly.
64 pub fn compute_wasm_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
65     if !fn_abi.ret.is_ignore() {
66         classify_ret(&mut fn_abi.ret);
67     }
68
69     for arg in &mut fn_abi.args {
70         if arg.is_ignore() {
71             continue;
72         }
73         classify_arg(arg);
74     }
75
76     fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
77         ret.extend_integer_width_to(32);
78     }
79
80     fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
81         arg.extend_integer_width_to(32);
82     }
83 }