|
1 | | -use rustc_abi::TyAbiInterface; |
| 1 | +use rustc_abi::{HasDataLayout, TyAbiInterface}; |
2 | 2 |
|
3 | | -use crate::callconv::{ArgAbi, FnAbi}; |
| 3 | +use crate::callconv::{ArgAbi, FnAbi, Reg, Uniform}; |
4 | 4 |
|
5 | | -fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) { |
6 | | - if ret.layout.is_aggregate() && ret.layout.size.bits() > 64 { |
7 | | - ret.make_indirect(); |
8 | | - } else { |
| 5 | +fn classify_ret<'a, Ty, C>(_cx: &C, ret: &mut ArgAbi<'a, Ty>) |
| 6 | +where |
| 7 | + Ty: TyAbiInterface<'a, C> + Copy, |
| 8 | + C: HasDataLayout, |
| 9 | +{ |
| 10 | + if !ret.layout.is_sized() { |
| 11 | + return; |
| 12 | + } |
| 13 | + if !ret.layout.is_aggregate() { |
9 | 14 | ret.extend_integer_width_to(32); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + let size = ret.layout.size; |
| 19 | + let bits = size.bits(); |
| 20 | + |
| 21 | + // Aggregates larger than 64 bits are returned indirectly |
| 22 | + if bits > 64 { |
| 23 | + ret.make_indirect(); |
| 24 | + return; |
10 | 25 | } |
| 26 | + |
| 27 | + // Small aggregates are returned in registers |
| 28 | + // Cast to appropriate register type to ensure proper ABI |
| 29 | + let align = ret.layout.align.bytes(); |
| 30 | + ret.cast_to(Uniform::consecutive(if align <= 4 { Reg::i32() } else { Reg::i64() }, size)); |
11 | 31 | } |
12 | 32 |
|
13 | 33 | fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>) |
14 | 34 | where |
15 | 35 | Ty: TyAbiInterface<'a, C> + Copy, |
| 36 | + C: HasDataLayout, |
16 | 37 | { |
| 38 | + if !arg.layout.is_sized() { |
| 39 | + return; |
| 40 | + } |
17 | 41 | if arg.layout.pass_indirectly_in_non_rustic_abis(cx) { |
18 | 42 | arg.make_indirect(); |
19 | 43 | return; |
20 | 44 | } |
21 | | - if arg.layout.is_aggregate() && arg.layout.size.bits() > 64 { |
22 | | - arg.make_indirect(); |
23 | | - } else { |
| 45 | + if !arg.layout.is_aggregate() { |
24 | 46 | arg.extend_integer_width_to(32); |
| 47 | + return; |
25 | 48 | } |
| 49 | + |
| 50 | + let size = arg.layout.size; |
| 51 | + let bits = size.bits(); |
| 52 | + |
| 53 | + // Aggregates larger than 64 bits are passed indirectly |
| 54 | + if bits > 64 { |
| 55 | + arg.make_indirect(); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Small aggregates are passed in registers |
| 60 | + // Cast to consecutive register-sized chunks to match the C ABI |
| 61 | + let align = arg.layout.align.bytes(); |
| 62 | + arg.cast_to(Uniform::consecutive(if align <= 4 { Reg::i32() } else { Reg::i64() }, size)); |
26 | 63 | } |
27 | 64 |
|
28 | 65 | pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>) |
29 | 66 | where |
30 | 67 | Ty: TyAbiInterface<'a, C> + Copy, |
| 68 | + C: HasDataLayout, |
31 | 69 | { |
32 | 70 | if !fn_abi.ret.is_ignore() { |
33 | | - classify_ret(&mut fn_abi.ret); |
| 71 | + classify_ret(cx, &mut fn_abi.ret); |
34 | 72 | } |
35 | 73 |
|
36 | 74 | for arg in fn_abi.args.iter_mut() { |
|
0 commit comments