|
| 1 | +// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// Reference: RISC-V ELF psABI specification |
| 12 | +// https://github.com/riscv/riscv-elf-psabi-doc |
| 13 | + |
| 14 | +use abi::call::{ArgType, FnType}; |
| 15 | + |
| 16 | +fn classify_ret_ty<Ty>(arg: &mut ArgType<Ty>, xlen: u64) { |
| 17 | + // "Scalars wider than 2✕XLEN are passed by reference and are replaced in |
| 18 | + // the argument list with the address." |
| 19 | + // "Aggregates larger than 2✕XLEN bits are passed by reference and are |
| 20 | + // replaced in the argument list with the address, as are C++ aggregates |
| 21 | + // with nontrivial copy constructors, destructors, or vtables." |
| 22 | + if arg.layout.size.bits() > 2 * xlen { |
| 23 | + arg.make_indirect(); |
| 24 | + } |
| 25 | + |
| 26 | + // "When passed in registers, scalars narrower than XLEN bits are widened |
| 27 | + // according to the sign of their type up to 32 bits, then sign-extended to |
| 28 | + // XLEN bits." |
| 29 | + arg.extend_integer_width_to(xlen); // this method only affects integer scalars |
| 30 | +} |
| 31 | + |
| 32 | +fn classify_arg_ty<Ty>(arg: &mut ArgType<Ty>, xlen: u64) { |
| 33 | + // "Scalars wider than 2✕XLEN are passed by reference and are replaced in |
| 34 | + // the argument list with the address." |
| 35 | + // "Aggregates larger than 2✕XLEN bits are passed by reference and are |
| 36 | + // replaced in the argument list with the address, as are C++ aggregates |
| 37 | + // with nontrivial copy constructors, destructors, or vtables." |
| 38 | + if arg.layout.size.bits() > 2 * xlen { |
| 39 | + arg.make_indirect(); |
| 40 | + } |
| 41 | + |
| 42 | + // "When passed in registers, scalars narrower than XLEN bits are widened |
| 43 | + // according to the sign of their type up to 32 bits, then sign-extended to |
| 44 | + // XLEN bits." |
| 45 | + arg.extend_integer_width_to(xlen); // this method only affects integer scalars |
| 46 | +} |
| 47 | + |
| 48 | +pub fn compute_abi_info<Ty>(fty: &mut FnType<Ty>, xlen: u64) { |
| 49 | + if !fty.ret.is_ignore() { |
| 50 | + classify_ret_ty(&mut fty.ret, xlen); |
| 51 | + } |
| 52 | + |
| 53 | + for arg in &mut fty.args { |
| 54 | + if arg.is_ignore() { |
| 55 | + continue; |
| 56 | + } |
| 57 | + classify_arg_ty(arg, xlen); |
| 58 | + } |
| 59 | +} |
0 commit comments