rustc_target/callconv/
xtensa.rs1use rustc_abi::{BackendRepr, CanonAbi, HasDataLayout, Size, TyAbiInterface};
9
10use crate::callconv::{ArgAbi, ComputeCallConv, IndirectMode, Reg, Uniform};
11use crate::spec::HasTargetSpec;
12
13const NUM_ARG_GPRS: u64 = 6;
14const NUM_RET_GPRS: u64 = 4;
15const MAX_ARG_IN_REGS_SIZE: u64 = NUM_ARG_GPRS * 32;
16const MAX_RET_IN_REGS_SIZE: u64 = NUM_RET_GPRS * 32;
17
18fn classify_ret_ty<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)
19where
20 Ty: TyAbiInterface<'a, C> + Copy,
21{
22 if arg.is_ignore() {
23 return;
24 }
25
26 let mut arg_gprs_left = NUM_RET_GPRS;
29 classify_arg_ty(cx, arg, &mut arg_gprs_left, true);
30 match arg.mode {
32 super::PassMode::Indirect { attrs: _, address_space: _, ref mut mode } => {
33 *mode = IndirectMode::Pointer;
34 }
35 super::PassMode::IndirectUnsized { .. } => {
::core::panicking::panic_fmt(format_args!("unsized returns are not supported"));
}panic!("unsized returns are not supported"),
36 _ => {}
37 }
38}
39
40fn classify_arg_ty<'a, Ty, C>(
41 cx: &C,
42 arg: &mut ArgAbi<'a, Ty>,
43 arg_gprs_left: &mut u64,
44 is_ret: bool,
45) where
46 Ty: TyAbiInterface<'a, C> + Copy,
47{
48 if !(*arg_gprs_left <= NUM_ARG_GPRS) {
{
::core::panicking::panic_fmt(format_args!("Arg GPR tracking underflow"));
}
};assert!(*arg_gprs_left <= NUM_ARG_GPRS, "Arg GPR tracking underflow");
49
50 let max_size = if is_ret { MAX_RET_IN_REGS_SIZE } else { MAX_ARG_IN_REGS_SIZE };
51
52 if !is_ret && arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
53 *arg_gprs_left = arg_gprs_left.saturating_sub(1);
54 arg.make_indirect();
55 return;
56 }
57
58 if arg.layout.is_zst() {
60 return;
61 }
62
63 let size = arg.layout.size.bits();
64 let needed_align = arg.layout.align.bits();
65 let mut must_use_stack = false;
66
67 let mut needed_arg_gprs = size.div_ceil(32);
71 if needed_align == 64 {
72 needed_arg_gprs += *arg_gprs_left % 2;
73 }
74
75 if needed_arg_gprs > *arg_gprs_left
76 || needed_align > 128
77 || (*arg_gprs_left < (max_size / 32) && needed_align == 128)
78 {
79 must_use_stack = true;
80 needed_arg_gprs = *arg_gprs_left;
81 }
82 *arg_gprs_left -= needed_arg_gprs;
83
84 if must_use_stack {
85 arg.pass_by_stack_offset(None);
86 } else if is_xtensa_aggregate(arg) {
87 if size <= 32 {
94 arg.cast_to(Reg::i32());
95 } else {
96 let reg = if needed_align == 2 * 32 { Reg::i64() } else { Reg::i32() };
97 let total = Size::from_bits(((size + 32 - 1) / 32) * 32);
98 arg.cast_to(Uniform::new(reg, total));
99 }
100 } else {
101 if size < 32 {
106 arg.extend_integer_width_to(32);
107 }
108 }
109}
110
111pub(crate) struct XtensaCallConv {
112 arg_gprs_left: u64,
113}
114
115impl<'a, Ty, C> ComputeCallConv<'a, Ty, C> for XtensaCallConv
116where
117 Ty: TyAbiInterface<'a, C> + Copy,
118 C: HasDataLayout + HasTargetSpec,
119{
120 fn new(_cx: &C, _conv: CanonAbi, _c_variadic: bool) -> Self {
121 Self { arg_gprs_left: NUM_ARG_GPRS }
122 }
123
124 fn compute_ret_abi(&mut self, cx: &C, ret: &mut ArgAbi<'a, Ty>) {
125 classify_ret_ty(cx, ret);
126 }
127
128 fn compute_arg_abi(&mut self, cx: &C, arg: &mut ArgAbi<'a, Ty>, _is_vararg: bool) {
129 classify_arg_ty(cx, arg, &mut self.arg_gprs_left, false);
130 }
131}
132
133fn is_xtensa_aggregate<'a, Ty>(arg: &ArgAbi<'a, Ty>) -> bool {
134 match arg.layout.backend_repr {
135 BackendRepr::SimdVector { .. } => true,
136 _ => arg.layout.is_aggregate(),
137 }
138}