Skip to main content

rustc_target/callconv/
hexagon.rs

1use rustc_abi::{HasDataLayout, Size, TyAbiInterface};
2
3use crate::callconv::{ArgAbi, FnAbi, Reg};
4
5fn classify_ret<'a, Ty, C>(_cx: &C, ret: &mut ArgAbi<'a, Ty>)
6where
7    Ty: TyAbiInterface<'a, C> + Copy,
8    C: HasDataLayout,
9{
10    if !ret.layout.is_sized() {
11        return;
12    }
13
14    if !ret.layout.is_aggregate() {
15        ret.extend_integer_width_to(32);
16        return;
17    }
18
19    // Per the Hexagon ABI:
20    // - Aggregates up to 32 bits are returned in R0
21    // - Aggregates 33-64 bits are returned in R1:R0
22    // - Aggregates > 64 bits are returned indirectly via hidden first argument
23    let size = ret.layout.size;
24    if size.bytes() <= 8 {
25        ret.cast_to(smallest_int_reg(size));
26    } else {
27        ret.make_indirect();
28    }
29}
30
31/// The number of 32-bit registers (R0-R5) available for passing arguments.
32const ARG_REGS: u32 = 6;
33
34/// Account for an argument of the given `size` in the remaining argument registers.
35///
36/// Returns `true` if the argument is passed entirely in registers. Returns `false` if it does
37/// not fit in the registers that are left; it is then passed on the stack, and no registers
38/// remain for any subsequent arguments (even ones that would have fit in the skipped registers).
39fn adjust_regs_left(size: Size, regs_left: &mut u32) -> bool {
40    if *regs_left == 0 {
41        return false;
42    }
43    if size.bytes() <= 4 {
44        *regs_left -= 1;
45        return true;
46    }
47    // Arguments larger than 32 bits are passed in an aligned register pair (R1:R0, R3:R2 or
48    // R5:R4), so skip a register if the next free one is odd-numbered.
49    if *regs_left >= 2 && !regs_left.is_multiple_of(2) {
50        *regs_left -= 1;
51    }
52    if *regs_left >= 2 {
53        *regs_left -= 2;
54        true
55    } else {
56        *regs_left = 0;
57        false
58    }
59}
60
61fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, regs_left: &mut u32)
62where
63    Ty: TyAbiInterface<'a, C> + Copy,
64    C: HasDataLayout,
65{
66    if !arg.layout.is_sized() {
67        return;
68    }
69    if arg.layout.pass_indirectly_in_non_rustic_abis(cx) {
70        arg.make_indirect();
71        // The pointer to the indirectly passed value takes up a register.
72        adjust_regs_left(Size::from_bytes(4), regs_left);
73        return;
74    }
75
76    let size = arg.layout.size;
77
78    if !arg.layout.is_aggregate() {
79        if size.bytes() <= 8 {
80            adjust_regs_left(size, regs_left);
81        }
82        arg.extend_integer_width_to(32);
83        return;
84    }
85
86    if size.bytes() > 8 {
87        arg.pass_by_stack_offset(None);
88        return;
89    }
90
91    let in_regs = adjust_regs_left(size, regs_left);
92    // In registers, aggregates have the alignment of the register(s) they are cast to. On the
93    // stack, they just have their natural alignment.
94    let align_bytes =
95        if in_regs { if size.bytes() <= 4 { 4 } else { 8 } } else { arg.layout.align.bytes() };
96
97    if size.bytes() <= align_bytes {
98        arg.cast_to(smallest_int_reg(size));
99    } else {
100        arg.pass_by_stack_offset(None);
101    }
102}
103
104/// The smallest integer register that can hold `size` (which is at most 8 bytes).
105fn smallest_int_reg(size: Size) -> Reg {
106    match size.bytes() {
107        ..=1 => Reg::i8(),
108        ..=2 => Reg::i16(),
109        ..=4 => Reg::i32(),
110        ..=8 => Reg::i64(),
111        _ => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("aggregate of {0} bytes does not fit in a register",
                size.bytes())));
}unreachable!("aggregate of {} bytes does not fit in a register", size.bytes()),
112    }
113}
114
115pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
116where
117    Ty: TyAbiInterface<'a, C> + Copy,
118    C: HasDataLayout,
119{
120    if !fn_abi.ret.is_ignore() {
121        classify_ret(cx, &mut fn_abi.ret);
122    }
123
124    // Note that, matching clang, the hidden pointer for an indirect return value does not use up
125    // any of the argument registers.
126    let mut regs_left = ARG_REGS;
127    for arg in fn_abi.args.iter_mut() {
128        if arg.is_ignore() {
129            continue;
130        }
131        classify_arg(cx, arg, &mut regs_left);
132    }
133}