Skip to content

Commit a6bd5ae

Browse files
committed
[RISCV] Implement call abi.
1 parent 2d5f62f commit a6bd5ae

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/librustc_target/abi/call/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod nvptx;
2323
mod nvptx64;
2424
mod powerpc;
2525
mod powerpc64;
26+
mod riscv;
2627
mod s390x;
2728
mod sparc;
2829
mod sparc64;
@@ -500,6 +501,8 @@ impl<'a, Ty> FnType<'a, Ty> {
500501
"nvptx" => nvptx::compute_abi_info(self),
501502
"nvptx64" => nvptx64::compute_abi_info(self),
502503
"hexagon" => hexagon::compute_abi_info(self),
504+
"riscv32" => riscv::compute_abi_info(self, 32),
505+
"riscv64" => riscv::compute_abi_info(self, 64),
503506
a => return Err(format!("unrecognized arch \"{}\" in target specification", a))
504507
}
505508

src/librustc_target/abi/call/riscv.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)