|
| 1 | +// reference: https://github.com/espressif/clang-xtensa/commit/6fb488d2553f06029e6611cf81c6efbd45b56e47#diff-aa74ae1e1ab6b7149789237edb78e688R8450 |
| 2 | + |
| 3 | +use crate::abi::call::{ArgAbi, FnAbi, Reg, Uniform}; |
| 4 | + |
| 5 | +const NUM_ARG_GPR: u64 = 6; |
| 6 | +const MAX_ARG_IN_REGS_SIZE: u64 = 4 * 32; |
| 7 | +// const MAX_ARG_DIRECT_SIZE: u64 = MAX_ARG_IN_REGS_SIZE; |
| 8 | +const MAX_RET_IN_REGS_SIZE: u64 = 2 * 32; |
| 9 | + |
| 10 | +fn classify_ret_ty<Ty>(arg: &mut ArgAbi<'_, Ty>, xlen: u64) { |
| 11 | + // The rules for return and argument types are the same, so defer to |
| 12 | + // classifyArgumentType. |
| 13 | + classify_arg_ty(arg, xlen, &mut 2); // two as max return size |
| 14 | +} |
| 15 | + |
| 16 | +fn classify_arg_ty<Ty>(arg: &mut ArgAbi<'_, Ty>, xlen: u64, remaining_gpr: &mut u64) { |
| 17 | + // Determine the number of GPRs needed to pass the current argument |
| 18 | + // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" |
| 19 | + // register pairs, so may consume 3 registers. |
| 20 | + |
| 21 | + let arg_size = arg.layout.size; |
| 22 | + if arg_size.bits() > MAX_ARG_IN_REGS_SIZE { |
| 23 | + arg.make_indirect(); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + let alignment = arg.layout.align.abi; |
| 28 | + let mut required_gpr = 1u64; // at least one per arg |
| 29 | + |
| 30 | + if alignment.bits() == 2 * xlen { |
| 31 | + required_gpr = 2 + (*remaining_gpr % 2); |
| 32 | + } else if arg_size.bits() > xlen && arg_size.bits() <= MAX_ARG_IN_REGS_SIZE { |
| 33 | + required_gpr = (arg_size.bits() + (xlen - 1)) / xlen; |
| 34 | + } |
| 35 | + |
| 36 | + let mut stack_required = false; |
| 37 | + if required_gpr > *remaining_gpr { |
| 38 | + stack_required = true; |
| 39 | + required_gpr = *remaining_gpr; |
| 40 | + } |
| 41 | + *remaining_gpr -= required_gpr; |
| 42 | + |
| 43 | + // if a value can fit in a reg and the |
| 44 | + // stack is not required, extend |
| 45 | + if !arg.layout.is_aggregate() { |
| 46 | + // non-aggregate types |
| 47 | + if arg_size.bits() < xlen && !stack_required { |
| 48 | + arg.extend_integer_width_to(xlen); |
| 49 | + } |
| 50 | + } else if arg_size.bits() as u64 <= MAX_ARG_IN_REGS_SIZE { |
| 51 | + // aggregate types |
| 52 | + // Aggregates which are <= 4*32 will be passed in registers if possible, |
| 53 | + // so coerce to integers. |
| 54 | + |
| 55 | + // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is |
| 56 | + // required, and a 2-element XLen array if only XLen alignment is |
| 57 | + // required. |
| 58 | + // if alignment == 2 * xlen { |
| 59 | + // arg.extend_integer_width_to(xlen * 2); |
| 60 | + // } else { |
| 61 | + // arg.extend_integer_width_to(arg_size + (xlen - 1) / xlen); |
| 62 | + // } |
| 63 | + if alignment.bits() == 2 * xlen { |
| 64 | + arg.cast_to(Uniform { unit: Reg::i64(), total: arg_size }); |
| 65 | + } else { |
| 66 | + //FIXME array type - this should be a homogenous array type |
| 67 | + // arg.extend_integer_width_to(arg_size + (xlen - 1) / xlen); |
| 68 | + } |
| 69 | + } else { |
| 70 | + // if we get here the stack is required |
| 71 | + assert!(stack_required); |
| 72 | + arg.make_indirect(); |
| 73 | + } |
| 74 | + |
| 75 | + // if arg_size as u64 <= MAX_ARG_IN_REGS_SIZE { |
| 76 | + // let align = arg.layout.align.abi.bytes(); |
| 77 | + // let total = arg.layout.size; |
| 78 | + // arg.cast_to(Uniform { |
| 79 | + // unit: if align <= 4 { Reg::i32() } else { Reg::i64() }, |
| 80 | + // total |
| 81 | + // }); |
| 82 | + // return; |
| 83 | + // } |
| 84 | +} |
| 85 | + |
| 86 | +pub fn compute_abi_info<Ty>(fabi: &mut FnAbi<'_, Ty>, xlen: u64) { |
| 87 | + if !fabi.ret.is_ignore() { |
| 88 | + classify_ret_ty(&mut fabi.ret, xlen); |
| 89 | + } |
| 90 | + |
| 91 | + let return_indirect = |
| 92 | + fabi.ret.layout.size.bits() > MAX_RET_IN_REGS_SIZE || fabi.ret.is_indirect(); |
| 93 | + |
| 94 | + let mut remaining_gpr = if return_indirect { NUM_ARG_GPR - 1 } else { NUM_ARG_GPR }; |
| 95 | + |
| 96 | + for arg in &mut fabi.args { |
| 97 | + if arg.is_ignore() { |
| 98 | + continue; |
| 99 | + } |
| 100 | + classify_arg_ty(arg, xlen, &mut remaining_gpr); |
| 101 | + } |
| 102 | +} |
0 commit comments