2022-11-05 14:36:38 +05:30
|
|
|
use std::str::FromStr;
|
2024-10-16 23:46:39 +02:00
|
|
|
use std::{fmt, iter};
|
2018-04-25 16:45:29 +03:00
|
|
|
|
2024-10-09 12:20:28 -07:00
|
|
|
pub use rustc_abi::{Reg, RegKind};
|
2024-04-29 08:53:45 +10:00
|
|
|
use rustc_macros::HashStable_Generic;
|
2022-02-01 18:44:45 +01:00
|
|
|
use rustc_span::Symbol;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2024-10-16 23:46:39 +02:00
|
|
|
use crate::abi::{
|
|
|
|
self, Abi, AddressSpace, Align, HasDataLayout, Pointer, Size, TyAbiInterface, TyAndLayout,
|
|
|
|
};
|
|
|
|
use crate::spec::abi::Abi as SpecAbi;
|
2024-09-16 22:14:35 +07:00
|
|
|
use crate::spec::{self, HasTargetSpec, HasWasmCAbiOpt, HasX86AbiOpt, WasmCAbi};
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2018-04-25 16:45:29 +03:00
|
|
|
mod aarch64;
|
2018-07-18 22:01:19 -05:00
|
|
|
mod amdgpu;
|
2018-04-25 16:45:29 +03:00
|
|
|
mod arm;
|
2016-05-06 09:32:10 -04:00
|
|
|
mod avr;
|
2020-11-30 19:41:57 +00:00
|
|
|
mod bpf;
|
2023-07-13 22:19:59 +08:00
|
|
|
mod csky;
|
2018-04-25 16:45:29 +03:00
|
|
|
mod hexagon;
|
2022-09-17 18:00:34 +08:00
|
|
|
mod loongarch;
|
2021-08-25 07:45:06 +00:00
|
|
|
mod m68k;
|
2018-04-25 16:45:29 +03:00
|
|
|
mod mips;
|
|
|
|
mod mips64;
|
|
|
|
mod msp430;
|
|
|
|
mod nvptx64;
|
|
|
|
mod powerpc;
|
|
|
|
mod powerpc64;
|
2018-07-24 12:04:17 +02:00
|
|
|
mod riscv;
|
2018-04-25 16:45:29 +03:00
|
|
|
mod s390x;
|
|
|
|
mod sparc;
|
|
|
|
mod sparc64;
|
rustc: Add a new `wasm` ABI
This commit implements the idea of a new ABI for the WebAssembly target,
one called `"wasm"`. This ABI is entirely of my own invention
and has no current precedent, but I think that the addition of this ABI
might help solve a number of issues with the WebAssembly targets.
When `wasm32-unknown-unknown` was first added to Rust I naively
"implemented an abi" for the target. I then went to write `wasm-bindgen`
which accidentally relied on details of this ABI. Turns out the ABI
definition didn't match C, which is causing issues for C/Rust interop.
Currently the compiler has a "wasm32 bindgen compat" ABI which is the
original implementation I added, and it's purely there for, well,
`wasm-bindgen`.
Another issue with the WebAssembly target is that it's not clear to me
when and if the default C ABI will change to account for WebAssembly's
multi-value feature (a feature that allows functions to return multiple
values). Even if this does happen, though, it seems like the C ABI will
be guided based on the performance of WebAssembly code and will likely
not match even what the current wasm-bindgen-compat ABI is today. This
leaves a hole in Rust's expressivity in binding WebAssembly where given
a particular import type, Rust may not be able to import that signature
with an updated C ABI for multi-value.
To fix these issues I had the idea of a new ABI for WebAssembly, one
called `wasm`. The definition of this ABI is "what you write
maps straight to wasm". The goal here is that whatever you write down in
the parameter list or in the return values goes straight into the
function's signature in the WebAssembly file. This special ABI is for
intentionally matching the ABI of an imported function from the
environment or exporting a function with the right signature.
With the addition of a new ABI, this enables rustc to:
* Eventually remove the "wasm-bindgen compat hack". Once this
ABI is stable wasm-bindgen can switch to using it everywhere.
Afterwards the wasm32-unknown-unknown target can have its default ABI
updated to match C.
* Expose the ability to precisely match an ABI signature for a
WebAssembly function, regardless of what the C ABI that clang chooses
turns out to be.
* Continue to evolve the definition of the default C ABI to match what
clang does on all targets, since the purpose of that ABI will be
explicitly matching C rather than generating particular function
imports/exports.
Naturally this is implemented as an unstable feature initially, but it
would be nice for this to get stabilized (if it works) in the near-ish
future to remove the wasm32-unknown-unknown incompatibility with the C
ABI. Doing this, however, requires the feature to be on stable because
wasm-bindgen works with stable Rust.
2021-04-01 16:08:29 -07:00
|
|
|
mod wasm;
|
2018-04-25 16:45:29 +03:00
|
|
|
mod x86;
|
|
|
|
mod x86_64;
|
|
|
|
mod x86_win64;
|
2020-09-12 23:31:14 +01:00
|
|
|
mod xtensa;
|
2018-04-25 16:45:29 +03:00
|
|
|
|
2023-05-03 22:22:24 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
|
2018-04-25 16:45:29 +03:00
|
|
|
pub enum PassMode {
|
2019-08-10 14:38:17 +03:00
|
|
|
/// Ignore the argument.
|
2021-01-23 12:57:35 +01:00
|
|
|
///
|
|
|
|
/// The argument is either uninhabited or a ZST.
|
2019-08-10 14:38:17 +03:00
|
|
|
Ignore,
|
2018-04-25 16:45:29 +03:00
|
|
|
/// Pass the argument directly.
|
2021-01-23 12:57:35 +01:00
|
|
|
///
|
2023-09-07 16:04:22 +02:00
|
|
|
/// The argument has a layout abi of `Scalar` or `Vector`.
|
|
|
|
/// Unfortunately due to past mistakes, in rare cases on wasm, it can also be `Aggregate`.
|
|
|
|
/// This is bad since it leaks LLVM implementation details into the ABI.
|
|
|
|
/// (Also see <https://github.com/rust-lang/rust/issues/115666>.)
|
2018-04-25 16:45:29 +03:00
|
|
|
Direct(ArgAttributes),
|
|
|
|
/// Pass a pair's elements directly in two arguments.
|
2021-01-23 12:57:35 +01:00
|
|
|
///
|
|
|
|
/// The argument has a layout abi of `ScalarPair`.
|
2018-04-25 16:45:29 +03:00
|
|
|
Pair(ArgAttributes, ArgAttributes),
|
2024-03-09 11:40:40 -05:00
|
|
|
/// Pass the argument after casting it. See the `CastTarget` docs for details.
|
|
|
|
///
|
|
|
|
/// `pad_i32` indicates if a `Reg::i32()` dummy argument is emitted before the real argument.
|
2023-09-07 22:06:37 +02:00
|
|
|
Cast { pad_i32: bool, cast: Box<CastTarget> },
|
2018-04-25 16:45:29 +03:00
|
|
|
/// Pass the argument indirectly via a hidden pointer.
|
2024-03-09 11:40:40 -05:00
|
|
|
///
|
2023-09-08 08:48:41 +02:00
|
|
|
/// The `meta_attrs` value, if any, is for the metadata (vtable or length) of an unsized
|
|
|
|
/// argument. (This is the only mode that supports unsized arguments.)
|
2024-03-09 11:40:40 -05:00
|
|
|
///
|
2023-09-08 08:48:41 +02:00
|
|
|
/// `on_stack` defines that the value should be passed at a fixed stack offset in accordance to
|
|
|
|
/// the ABI rather than passed using a pointer. This corresponds to the `byval` LLVM argument
|
2024-03-09 11:40:40 -05:00
|
|
|
/// attribute. The `byval` argument will use a byte array with the same size as the Rust type
|
|
|
|
/// (which ensures that padding is preserved and that we do not rely on LLVM's struct layout),
|
|
|
|
/// and will use the alignment specified in `attrs.pointee_align` (if `Some`) or the type's
|
2024-03-09 12:49:35 -05:00
|
|
|
/// alignment (if `None`). This means that the alignment will not always
|
2024-09-16 13:04:24 -07:00
|
|
|
/// match the Rust type's alignment; see documentation of `pass_by_stack_offset` for more info.
|
2024-03-09 11:40:40 -05:00
|
|
|
///
|
2024-03-07 09:56:27 -05:00
|
|
|
/// `on_stack` cannot be true for unsized arguments, i.e., when `meta_attrs` is `Some`.
|
2023-09-08 08:48:41 +02:00
|
|
|
Indirect { attrs: ArgAttributes, meta_attrs: Option<ArgAttributes>, on_stack: bool },
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2023-09-02 11:59:31 +02:00
|
|
|
impl PassMode {
|
|
|
|
/// Checks if these two `PassMode` are equal enough to be considered "the same for all
|
2023-09-07 16:48:02 +02:00
|
|
|
/// function call ABIs". However, the `Layout` can also impact ABI decisions,
|
|
|
|
/// so that needs to be compared as well!
|
2023-09-02 11:59:31 +02:00
|
|
|
pub fn eq_abi(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
2023-09-07 22:06:37 +02:00
|
|
|
(PassMode::Ignore, PassMode::Ignore) => true,
|
2023-09-02 11:59:31 +02:00
|
|
|
(PassMode::Direct(a1), PassMode::Direct(a2)) => a1.eq_abi(a2),
|
|
|
|
(PassMode::Pair(a1, b1), PassMode::Pair(a2, b2)) => a1.eq_abi(a2) && b1.eq_abi(b2),
|
|
|
|
(
|
2023-09-07 22:06:37 +02:00
|
|
|
PassMode::Cast { cast: c1, pad_i32: pad1 },
|
|
|
|
PassMode::Cast { cast: c2, pad_i32: pad2 },
|
|
|
|
) => c1.eq_abi(c2) && pad1 == pad2,
|
2023-09-02 11:59:31 +02:00
|
|
|
(
|
2023-09-08 08:48:41 +02:00
|
|
|
PassMode::Indirect { attrs: a1, meta_attrs: None, on_stack: s1 },
|
|
|
|
PassMode::Indirect { attrs: a2, meta_attrs: None, on_stack: s2 },
|
2023-09-02 11:59:31 +02:00
|
|
|
) => a1.eq_abi(a2) && s1 == s2,
|
|
|
|
(
|
2023-09-08 08:48:41 +02:00
|
|
|
PassMode::Indirect { attrs: a1, meta_attrs: Some(e1), on_stack: s1 },
|
|
|
|
PassMode::Indirect { attrs: a2, meta_attrs: Some(e2), on_stack: s2 },
|
2023-09-02 11:59:31 +02:00
|
|
|
) => a1.eq_abi(a2) && e1.eq_abi(e2) && s1 == s2,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 16:45:29 +03:00
|
|
|
// Hack to disable non_upper_case_globals only for the bitflags! and not for the rest
|
|
|
|
// of this module
|
2019-02-08 21:00:07 +09:00
|
|
|
pub use attr_impl::ArgAttribute;
|
2018-04-25 16:45:29 +03:00
|
|
|
|
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
#[allow(unused)]
|
|
|
|
mod attr_impl {
|
2024-04-29 08:53:45 +10:00
|
|
|
use rustc_macros::HashStable_Generic;
|
|
|
|
|
2018-04-25 16:45:29 +03:00
|
|
|
// The subset of llvm::Attribute needed for arguments, packed into a bitfield.
|
2023-12-30 17:09:02 +01:00
|
|
|
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq, HashStable_Generic)]
|
|
|
|
pub struct ArgAttribute(u8);
|
2019-02-08 21:00:07 +09:00
|
|
|
bitflags::bitflags! {
|
2023-12-30 17:09:02 +01:00
|
|
|
impl ArgAttribute: u8 {
|
2018-04-25 16:45:29 +03:00
|
|
|
const NoAlias = 1 << 1;
|
|
|
|
const NoCapture = 1 << 2;
|
|
|
|
const NonNull = 1 << 3;
|
|
|
|
const ReadOnly = 1 << 4;
|
2021-03-18 21:50:28 +01:00
|
|
|
const InReg = 1 << 5;
|
2023-01-04 19:24:42 -05:00
|
|
|
const NoUndef = 1 << 6;
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
2023-12-30 17:09:02 +01:00
|
|
|
rustc_data_structures::external_bitflags_debug! { ArgAttribute }
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2020-11-14 12:27:57 +01:00
|
|
|
/// Sometimes an ABI requires small integers to be extended to a full or partial register. This enum
|
2021-04-19 15:57:08 +03:00
|
|
|
/// defines if this extension should be zero-extension or sign-extension when necessary. When it is
|
|
|
|
/// not necessary to extend the argument, this enum is ignored.
|
2021-08-26 21:58:34 +03:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
|
2020-11-14 12:27:57 +01:00
|
|
|
pub enum ArgExtension {
|
|
|
|
None,
|
|
|
|
Zext,
|
|
|
|
Sext,
|
|
|
|
}
|
|
|
|
|
2018-04-25 16:45:29 +03:00
|
|
|
/// A compact representation of LLVM attributes (at least those relevant for this module)
|
|
|
|
/// that can be manipulated without interacting with LLVM's Attribute machinery.
|
2021-08-26 21:58:34 +03:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
|
2018-04-25 16:45:29 +03:00
|
|
|
pub struct ArgAttributes {
|
|
|
|
pub regular: ArgAttribute,
|
2020-11-14 12:27:57 +01:00
|
|
|
pub arg_ext: ArgExtension,
|
2019-11-25 22:45:00 +01:00
|
|
|
/// The minimum size of the pointee, guaranteed to be valid for the duration of the whole call
|
|
|
|
/// (corresponding to LLVM's dereferenceable and dereferenceable_or_null attributes).
|
2018-04-25 16:45:29 +03:00
|
|
|
pub pointee_size: Size,
|
2018-09-09 01:16:45 +03:00
|
|
|
pub pointee_align: Option<Align>,
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ArgAttributes {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
ArgAttributes {
|
|
|
|
regular: ArgAttribute::default(),
|
2020-11-14 12:27:57 +01:00
|
|
|
arg_ext: ArgExtension::None,
|
2018-05-20 14:14:39 +02:00
|
|
|
pointee_size: Size::ZERO,
|
2018-04-25 16:45:29 +03:00
|
|
|
pointee_align: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:06:35 +01:00
|
|
|
pub fn ext(&mut self, ext: ArgExtension) -> &mut Self {
|
2020-12-31 18:02:52 +02:00
|
|
|
assert!(
|
|
|
|
self.arg_ext == ArgExtension::None || self.arg_ext == ext,
|
|
|
|
"cannot set {:?} when {:?} is already set",
|
|
|
|
ext,
|
|
|
|
self.arg_ext
|
|
|
|
);
|
2020-11-21 19:06:35 +01:00
|
|
|
self.arg_ext = ext;
|
2020-11-14 12:27:57 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-25 16:45:29 +03:00
|
|
|
pub fn set(&mut self, attr: ArgAttribute) -> &mut Self {
|
2018-08-09 15:42:43 +02:00
|
|
|
self.regular |= attr;
|
2018-04-25 16:45:29 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains(&self, attr: ArgAttribute) -> bool {
|
|
|
|
self.regular.contains(attr)
|
|
|
|
}
|
2023-09-02 11:59:31 +02:00
|
|
|
|
|
|
|
/// Checks if these two `ArgAttributes` are equal enough to be considered "the same for all
|
|
|
|
/// function call ABIs".
|
|
|
|
pub fn eq_abi(&self, other: &Self) -> bool {
|
|
|
|
// There's only one regular attribute that matters for the call ABI: InReg.
|
|
|
|
// Everything else is things like noalias, dereferenceable, nonnull, ...
|
|
|
|
// (This also applies to pointee_size, pointee_align.)
|
|
|
|
if self.regular.contains(ArgAttribute::InReg) != other.regular.contains(ArgAttribute::InReg)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We also compare the sign extension mode -- this could let the callee make assumptions
|
|
|
|
// about bits that conceptually were not even passed.
|
|
|
|
if self.arg_ext != other.arg_ext {
|
|
|
|
return false;
|
|
|
|
}
|
2024-09-09 12:22:00 +02:00
|
|
|
true
|
2023-09-02 11:59:31 +02:00
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// An argument passed entirely registers with the
|
2018-11-27 02:59:49 +00:00
|
|
|
/// same kind (e.g., HFA / HVA on PPC64 and AArch64).
|
2021-08-26 21:58:34 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
|
2018-04-25 16:45:29 +03:00
|
|
|
pub struct Uniform {
|
|
|
|
pub unit: Reg,
|
|
|
|
|
|
|
|
/// The total size of the argument, which can be:
|
2019-02-08 14:53:55 +01:00
|
|
|
/// * equal to `unit.size` (one scalar/vector),
|
|
|
|
/// * a multiple of `unit.size` (an array of scalar/vectors),
|
make CastTarget::size and CastTarget::llvm_type consistent, remove
special case that's not present in Clang
Making the methods consistent doesn't require much justification. It's
required for us to generate correct code.
The special case was present near the end of `CastTarget::llvm_type`, and
resulted in the final integer component of the ABI type being shrunk to
the smallest integer that fits.
You can see this in action here (https://godbolt.org/z/Pe73cr91d),
where, for a struct with 5 u16 elements, rustc generates
`{ i64, i16 }`, while Clang generates `[2 x i64]`.
This special case was added a long time ago, when the function was
originally written [1]. That commit consolidated logic from many
backends, and in some of the code it deleted, sparc64 [2] and
powerpc64 [3] had similar special cases.
However, looking at Clang today, it doesn't have this special case for
sparc64 (https://godbolt.org/z/YaafvYWdf) or powerpc64
(https://godbolt.org/z/5c3YePTje), so this change just removes it.
[1]: https://github.com/rust-lang/rust/commit/f0636b61c7f84962a609e831760db9d77f4f5e14#diff-183c4dadf10704bd1f521b71f71d89bf755c9603a93f894d66c03bb1effc6021R231
[2]: https://github.com/rust-lang/rust/commit/f0636b61c7f84962a609e831760db9d77f4f5e14#diff-2d8f87ea6db6d7f0a6fbeb1d5549adc07e93331278d951a1e051a40f92914436L163-L166
[3]: https://github.com/rust-lang/rust/commit/f0636b61c7f84962a609e831760db9d77f4f5e14#diff-88af4a9df9ead503a5c7774a0455d270dea3ba60e9b0ec1ce550b4c53d3bce3bL172-L175
2024-03-17 00:14:20 -04:00
|
|
|
/// * if `unit.kind` is `Integer`, the last element can be shorter, i.e., `{ i64, i64, i32 }`
|
|
|
|
/// for 64-bit integers with a total size of 20 bytes. When the argument is actually passed,
|
|
|
|
/// this size will be rounded up to the nearest multiple of `unit.size`.
|
2018-04-25 16:45:29 +03:00
|
|
|
pub total: Size,
|
2024-03-20 14:38:32 +01:00
|
|
|
|
2024-03-21 16:10:23 +01:00
|
|
|
/// Indicate that the argument is consecutive, in the sense that either all values need to be
|
|
|
|
/// passed in register, or all on the stack. If they are passed on the stack, there should be
|
|
|
|
/// no additional padding between elements.
|
|
|
|
pub is_consecutive: bool,
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Reg> for Uniform {
|
|
|
|
fn from(unit: Reg) -> Uniform {
|
2024-03-21 16:10:23 +01:00
|
|
|
Uniform { unit, total: unit.size, is_consecutive: false }
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Uniform {
|
2018-09-09 01:16:45 +03:00
|
|
|
pub fn align<C: HasDataLayout>(&self, cx: &C) -> Align {
|
2018-04-25 16:45:29 +03:00
|
|
|
self.unit.align(cx)
|
|
|
|
}
|
2024-03-21 16:10:23 +01:00
|
|
|
|
|
|
|
/// Pass using one or more values of the given type, without requiring them to be consecutive.
|
|
|
|
/// That is, some values may be passed in register and some on the stack.
|
|
|
|
pub fn new(unit: Reg, total: Size) -> Self {
|
|
|
|
Uniform { unit, total, is_consecutive: false }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pass using one or more consecutive values of the given type. Either all values will be
|
|
|
|
/// passed in registers, or all on the stack.
|
|
|
|
pub fn consecutive(unit: Reg, total: Size) -> Self {
|
|
|
|
Uniform { unit, total, is_consecutive: true }
|
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2023-09-07 22:06:37 +02:00
|
|
|
/// Describes the type used for `PassMode::Cast`.
|
|
|
|
///
|
|
|
|
/// Passing arguments in this mode works as follows: the registers in the `prefix` (the ones that
|
|
|
|
/// are `Some`) get laid out one after the other (using `repr(C)` layout rules). Then the
|
|
|
|
/// `rest.unit` register type gets repeated often enough to cover `rest.size`. This describes the
|
|
|
|
/// actual type used for the call; the Rust type of the argument is then transmuted to this ABI type
|
|
|
|
/// (and all data in the padding between the registers is dropped).
|
2023-05-03 22:22:24 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
|
2018-04-25 16:45:29 +03:00
|
|
|
pub struct CastTarget {
|
2021-12-01 10:03:45 +01:00
|
|
|
pub prefix: [Option<Reg>; 8],
|
2018-04-25 16:45:29 +03:00
|
|
|
pub rest: Uniform,
|
2021-12-01 10:03:45 +01:00
|
|
|
pub attrs: ArgAttributes,
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Reg> for CastTarget {
|
|
|
|
fn from(unit: Reg) -> CastTarget {
|
|
|
|
CastTarget::from(Uniform::from(unit))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Uniform> for CastTarget {
|
|
|
|
fn from(uniform: Uniform) -> CastTarget {
|
2021-12-01 10:03:45 +01:00
|
|
|
CastTarget {
|
|
|
|
prefix: [None; 8],
|
|
|
|
rest: uniform,
|
|
|
|
attrs: ArgAttributes {
|
|
|
|
regular: ArgAttribute::default(),
|
|
|
|
arg_ext: ArgExtension::None,
|
|
|
|
pointee_size: Size::ZERO,
|
|
|
|
pointee_align: None,
|
|
|
|
},
|
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CastTarget {
|
|
|
|
pub fn pair(a: Reg, b: Reg) -> CastTarget {
|
|
|
|
CastTarget {
|
2021-12-01 10:03:45 +01:00
|
|
|
prefix: [Some(a), None, None, None, None, None, None, None],
|
2018-04-25 16:45:29 +03:00
|
|
|
rest: Uniform::from(b),
|
2021-12-01 10:03:45 +01:00
|
|
|
attrs: ArgAttributes {
|
|
|
|
regular: ArgAttribute::default(),
|
|
|
|
arg_ext: ArgExtension::None,
|
|
|
|
pointee_size: Size::ZERO,
|
|
|
|
pointee_align: None,
|
|
|
|
},
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-30 21:33:46 +08:00
|
|
|
/// When you only access the range containing valid data, you can use this unaligned size;
|
|
|
|
/// otherwise, use the safer `size` method.
|
|
|
|
pub fn unaligned_size<C: HasDataLayout>(&self, _cx: &C) -> Size {
|
make CastTarget::size and CastTarget::llvm_type consistent, remove
special case that's not present in Clang
Making the methods consistent doesn't require much justification. It's
required for us to generate correct code.
The special case was present near the end of `CastTarget::llvm_type`, and
resulted in the final integer component of the ABI type being shrunk to
the smallest integer that fits.
You can see this in action here (https://godbolt.org/z/Pe73cr91d),
where, for a struct with 5 u16 elements, rustc generates
`{ i64, i16 }`, while Clang generates `[2 x i64]`.
This special case was added a long time ago, when the function was
originally written [1]. That commit consolidated logic from many
backends, and in some of the code it deleted, sparc64 [2] and
powerpc64 [3] had similar special cases.
However, looking at Clang today, it doesn't have this special case for
sparc64 (https://godbolt.org/z/YaafvYWdf) or powerpc64
(https://godbolt.org/z/5c3YePTje), so this change just removes it.
[1]: https://github.com/rust-lang/rust/commit/f0636b61c7f84962a609e831760db9d77f4f5e14#diff-183c4dadf10704bd1f521b71f71d89bf755c9603a93f894d66c03bb1effc6021R231
[2]: https://github.com/rust-lang/rust/commit/f0636b61c7f84962a609e831760db9d77f4f5e14#diff-2d8f87ea6db6d7f0a6fbeb1d5549adc07e93331278d951a1e051a40f92914436L163-L166
[3]: https://github.com/rust-lang/rust/commit/f0636b61c7f84962a609e831760db9d77f4f5e14#diff-88af4a9df9ead503a5c7774a0455d270dea3ba60e9b0ec1ce550b4c53d3bce3bL172-L175
2024-03-17 00:14:20 -04:00
|
|
|
// Prefix arguments are passed in specific designated registers
|
|
|
|
let prefix_size = self
|
|
|
|
.prefix
|
|
|
|
.iter()
|
|
|
|
.filter_map(|x| x.map(|reg| reg.size))
|
|
|
|
.fold(Size::ZERO, |acc, size| acc + size);
|
|
|
|
// Remaining arguments are passed in chunks of the unit size
|
|
|
|
let rest_size =
|
|
|
|
self.rest.unit.size * self.rest.total.bytes().div_ceil(self.rest.unit.size.bytes());
|
|
|
|
|
|
|
|
prefix_size + rest_size
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2024-06-30 21:33:46 +08:00
|
|
|
pub fn size<C: HasDataLayout>(&self, cx: &C) -> Size {
|
|
|
|
self.unaligned_size(cx).align_to(self.align(cx))
|
|
|
|
}
|
|
|
|
|
2018-09-09 01:16:45 +03:00
|
|
|
pub fn align<C: HasDataLayout>(&self, cx: &C) -> Align {
|
2018-04-25 16:45:29 +03:00
|
|
|
self.prefix
|
|
|
|
.iter()
|
2021-12-01 10:03:45 +01:00
|
|
|
.filter_map(|x| x.map(|reg| reg.align(cx)))
|
2018-09-09 01:16:45 +03:00
|
|
|
.fold(cx.data_layout().aggregate_align.abi.max(self.rest.align(cx)), |acc, align| {
|
2018-04-25 16:45:29 +03:00
|
|
|
acc.max(align)
|
2019-12-22 17:42:04 -05:00
|
|
|
})
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
2023-09-02 11:59:31 +02:00
|
|
|
|
|
|
|
/// Checks if these two `CastTarget` are equal enough to be considered "the same for all
|
|
|
|
/// function call ABIs".
|
|
|
|
pub fn eq_abi(&self, other: &Self) -> bool {
|
|
|
|
let CastTarget { prefix: prefix_l, rest: rest_l, attrs: attrs_l } = self;
|
|
|
|
let CastTarget { prefix: prefix_r, rest: rest_r, attrs: attrs_r } = other;
|
|
|
|
prefix_l == prefix_r && rest_l == rest_r && attrs_l.eq_abi(attrs_r)
|
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Information about how to pass an argument to,
|
|
|
|
/// or return a value from, a function, under some ABI.
|
2023-01-23 16:02:00 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, HashStable_Generic)]
|
2019-10-29 16:35:26 +02:00
|
|
|
pub struct ArgAbi<'a, Ty> {
|
2020-03-04 14:50:21 +00:00
|
|
|
pub layout: TyAndLayout<'a, Ty>,
|
2018-04-25 16:45:29 +03:00
|
|
|
pub mode: PassMode,
|
|
|
|
}
|
|
|
|
|
2023-01-23 16:02:00 +00:00
|
|
|
// Needs to be a custom impl because of the bounds on the `TyAndLayout` debug impl.
|
|
|
|
impl<'a, Ty: fmt::Display> fmt::Debug for ArgAbi<'a, Ty> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let ArgAbi { layout, mode } = self;
|
|
|
|
f.debug_struct("ArgAbi").field("layout", layout).field("mode", mode).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:35:26 +02:00
|
|
|
impl<'a, Ty> ArgAbi<'a, Ty> {
|
2023-09-06 11:12:23 +02:00
|
|
|
/// This defines the "default ABI" for that type, that is then later adjusted in `fn_abi_adjust_for_abi`.
|
2020-12-29 20:00:57 +01:00
|
|
|
pub fn new(
|
|
|
|
cx: &impl HasDataLayout,
|
|
|
|
layout: TyAndLayout<'a, Ty>,
|
2021-08-29 11:06:55 +02:00
|
|
|
scalar_attrs: impl Fn(&TyAndLayout<'a, Ty>, abi::Scalar, Size) -> ArgAttributes,
|
2020-12-29 20:00:57 +01:00
|
|
|
) -> Self {
|
2021-08-29 11:06:55 +02:00
|
|
|
let mode = match layout.abi {
|
2020-12-29 20:00:57 +01:00
|
|
|
Abi::Uninhabited => PassMode::Ignore,
|
|
|
|
Abi::Scalar(scalar) => PassMode::Direct(scalar_attrs(&layout, scalar, Size::ZERO)),
|
|
|
|
Abi::ScalarPair(a, b) => PassMode::Pair(
|
|
|
|
scalar_attrs(&layout, a, Size::ZERO),
|
2022-03-03 12:02:12 +00:00
|
|
|
scalar_attrs(&layout, b, a.size(cx).align_to(b.align(cx).abi)),
|
2020-12-29 20:00:57 +01:00
|
|
|
),
|
|
|
|
Abi::Vector { .. } => PassMode::Direct(ArgAttributes::new()),
|
2023-11-01 23:01:53 +01:00
|
|
|
Abi::Aggregate { .. } => Self::indirect_pass_mode(&layout),
|
2020-12-29 20:00:57 +01:00
|
|
|
};
|
2022-08-25 22:19:38 +10:00
|
|
|
ArgAbi { layout, mode }
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2020-12-29 20:00:57 +01:00
|
|
|
fn indirect_pass_mode(layout: &TyAndLayout<'a, Ty>) -> PassMode {
|
2018-04-25 16:45:29 +03:00
|
|
|
let mut attrs = ArgAttributes::new();
|
|
|
|
|
|
|
|
// For non-immediate arguments the callee gets its own copy of
|
|
|
|
// the value on the stack, so there are no aliases. It's also
|
|
|
|
// program-invisible so can't possibly capture
|
2022-02-05 01:00:37 -05:00
|
|
|
attrs
|
|
|
|
.set(ArgAttribute::NoAlias)
|
|
|
|
.set(ArgAttribute::NoCapture)
|
|
|
|
.set(ArgAttribute::NonNull)
|
|
|
|
.set(ArgAttribute::NoUndef);
|
2020-12-29 20:00:57 +01:00
|
|
|
attrs.pointee_size = layout.size;
|
2022-10-31 20:38:40 -07:00
|
|
|
attrs.pointee_align = Some(layout.align.abi);
|
2018-04-25 16:45:29 +03:00
|
|
|
|
2023-09-08 08:48:41 +02:00
|
|
|
let meta_attrs = layout.is_unsized().then_some(ArgAttributes::new());
|
2018-08-03 23:32:21 +09:00
|
|
|
|
2023-09-08 08:48:41 +02:00
|
|
|
PassMode::Indirect { attrs, meta_attrs, on_stack: false }
|
2020-12-29 20:00:57 +01:00
|
|
|
}
|
|
|
|
|
2023-11-01 23:01:53 +01:00
|
|
|
/// Pass this argument directly instead. Should NOT be used!
|
|
|
|
/// Only exists because of past ABI mistakes that will take time to fix
|
|
|
|
/// (see <https://github.com/rust-lang/rust/issues/115666>).
|
|
|
|
pub fn make_direct_deprecated(&mut self) {
|
2023-11-19 16:03:07 +01:00
|
|
|
match self.mode {
|
|
|
|
PassMode::Indirect { .. } => {
|
|
|
|
self.mode = PassMode::Direct(ArgAttributes::new());
|
|
|
|
}
|
2024-09-09 12:22:00 +02:00
|
|
|
PassMode::Ignore | PassMode::Direct(_) | PassMode::Pair(_, _) => {} // already direct
|
2023-11-19 16:03:07 +01:00
|
|
|
_ => panic!("Tried to make {:?} direct", self.mode),
|
|
|
|
}
|
2023-11-01 23:01:53 +01:00
|
|
|
}
|
|
|
|
|
2024-10-03 15:05:23 +02:00
|
|
|
/// Pass this argument indirectly, by passing a (thin or wide) pointer to the argument instead.
|
2024-03-09 11:40:40 -05:00
|
|
|
/// This is valid for both sized and unsized arguments.
|
2020-12-29 20:00:57 +01:00
|
|
|
pub fn make_indirect(&mut self) {
|
|
|
|
match self.mode {
|
2023-11-19 16:03:07 +01:00
|
|
|
PassMode::Direct(_) | PassMode::Pair(_, _) => {
|
2024-08-21 02:43:12 +01:00
|
|
|
self.mode = Self::indirect_pass_mode(&self.layout);
|
2023-11-19 16:03:07 +01:00
|
|
|
}
|
2023-11-01 23:01:53 +01:00
|
|
|
PassMode::Indirect { attrs: _, meta_attrs: _, on_stack: false } => {
|
|
|
|
// already indirect
|
|
|
|
}
|
2020-12-29 20:00:57 +01:00
|
|
|
_ => panic!("Tried to make {:?} indirect", self.mode),
|
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2024-08-21 02:43:12 +01:00
|
|
|
/// Same as `make_indirect`, but for arguments that are ignored. Only needed for ABIs that pass
|
|
|
|
/// ZSTs indirectly.
|
|
|
|
pub fn make_indirect_from_ignore(&mut self) {
|
|
|
|
match self.mode {
|
|
|
|
PassMode::Ignore => {
|
|
|
|
self.mode = Self::indirect_pass_mode(&self.layout);
|
|
|
|
}
|
|
|
|
PassMode::Indirect { attrs: _, meta_attrs: _, on_stack: false } => {
|
|
|
|
// already indirect
|
|
|
|
}
|
|
|
|
_ => panic!("Tried to make {:?} indirect (expected `PassMode::Ignore`)", self.mode),
|
|
|
|
}
|
2024-06-01 12:25:16 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 11:40:40 -05:00
|
|
|
/// Pass this argument indirectly, by placing it at a fixed stack offset.
|
|
|
|
/// This corresponds to the `byval` LLVM argument attribute.
|
|
|
|
/// This is only valid for sized arguments.
|
|
|
|
///
|
|
|
|
/// `byval_align` specifies the alignment of the `byval` stack slot, which does not need to
|
|
|
|
/// correspond to the type's alignment. This will be `Some` if the target's ABI specifies that
|
|
|
|
/// stack slots used for arguments passed by-value have specific alignment requirements which
|
|
|
|
/// differ from the alignment used in other situations.
|
|
|
|
///
|
|
|
|
/// If `None`, the type's alignment is used.
|
|
|
|
///
|
|
|
|
/// If the resulting alignment differs from the type's alignment,
|
|
|
|
/// the argument will be copied to an alloca with sufficient alignment,
|
|
|
|
/// either in the caller (if the type's alignment is lower than the byval alignment)
|
2024-03-11 09:39:43 -04:00
|
|
|
/// or in the callee (if the type's alignment is higher than the byval alignment),
|
2024-03-09 11:40:40 -05:00
|
|
|
/// to ensure that Rust code never sees an underaligned pointer.
|
2024-09-16 13:04:24 -07:00
|
|
|
pub fn pass_by_stack_offset(&mut self, byval_align: Option<Align>) {
|
2024-03-09 11:40:40 -05:00
|
|
|
assert!(!self.layout.is_unsized(), "used byval ABI for unsized layout");
|
2018-04-25 16:45:29 +03:00
|
|
|
self.make_indirect();
|
|
|
|
match self.mode {
|
2023-09-08 08:48:41 +02:00
|
|
|
PassMode::Indirect { ref mut attrs, meta_attrs: _, ref mut on_stack } => {
|
2020-11-14 14:29:40 +01:00
|
|
|
*on_stack = true;
|
2022-10-31 20:38:40 -07:00
|
|
|
|
|
|
|
// Some platforms, like 32-bit x86, change the alignment of the type when passing
|
|
|
|
// `byval`. Account for that.
|
|
|
|
if let Some(byval_align) = byval_align {
|
|
|
|
// On all targets with byval align this is currently true, so let's assert it.
|
|
|
|
debug_assert!(byval_align >= Align::from_bytes(4).unwrap());
|
|
|
|
attrs.pointee_align = Some(byval_align);
|
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn extend_integer_width_to(&mut self, bits: u64) {
|
|
|
|
// Only integers have signedness
|
2021-08-29 11:06:55 +02:00
|
|
|
if let Abi::Scalar(scalar) = self.layout.abi {
|
2022-03-03 12:02:12 +00:00
|
|
|
if let abi::Int(i, signed) = scalar.primitive() {
|
2018-04-25 16:45:29 +03:00
|
|
|
if i.size().bits() < bits {
|
|
|
|
if let PassMode::Direct(ref mut attrs) = self.mode {
|
2020-11-14 12:27:57 +01:00
|
|
|
if signed {
|
2020-11-21 19:06:35 +01:00
|
|
|
attrs.ext(ArgExtension::Sext)
|
2020-11-14 12:27:57 +01:00
|
|
|
} else {
|
2020-11-21 19:06:35 +01:00
|
|
|
attrs.ext(ArgExtension::Zext)
|
2020-11-14 12:27:57 +01:00
|
|
|
};
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cast_to<T: Into<CastTarget>>(&mut self, target: T) {
|
2023-09-07 22:06:37 +02:00
|
|
|
self.mode = PassMode::Cast { cast: Box::new(target.into()), pad_i32: false };
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2022-08-25 22:19:38 +10:00
|
|
|
pub fn cast_to_and_pad_i32<T: Into<CastTarget>>(&mut self, target: T, pad_i32: bool) {
|
2023-09-07 22:06:37 +02:00
|
|
|
self.mode = PassMode::Cast { cast: Box::new(target.into()), pad_i32 };
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_indirect(&self) -> bool {
|
2021-01-09 12:00:45 -05:00
|
|
|
matches!(self.mode, PassMode::Indirect { .. })
|
2018-08-03 23:32:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_sized_indirect(&self) -> bool {
|
2023-09-08 08:48:41 +02:00
|
|
|
matches!(self.mode, PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ })
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2018-05-29 00:12:55 +09:00
|
|
|
pub fn is_unsized_indirect(&self) -> bool {
|
2023-09-08 08:48:41 +02:00
|
|
|
matches!(self.mode, PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ })
|
2018-05-29 00:12:55 +09:00
|
|
|
}
|
|
|
|
|
2018-04-25 16:45:29 +03:00
|
|
|
pub fn is_ignore(&self) -> bool {
|
2020-10-26 21:02:48 -04:00
|
|
|
matches!(self.mode, PassMode::Ignore)
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
2023-09-07 16:48:02 +02:00
|
|
|
|
|
|
|
/// Checks if these two `ArgAbi` are equal enough to be considered "the same for all
|
|
|
|
/// function call ABIs".
|
2024-08-27 09:04:59 +02:00
|
|
|
pub fn eq_abi(&self, other: &Self) -> bool
|
|
|
|
where
|
|
|
|
Ty: PartialEq,
|
|
|
|
{
|
2023-09-07 16:48:02 +02:00
|
|
|
// Ideally we'd just compare the `mode`, but that is not enough -- for some modes LLVM will look
|
|
|
|
// at the type.
|
2024-08-27 09:04:59 +02:00
|
|
|
self.layout.eq_abi(&other.layout) && self.mode.eq_abi(&other.mode) && {
|
|
|
|
// `fn_arg_sanity_check` accepts `PassMode::Direct` for some aggregates.
|
|
|
|
// That elevates any type difference to an ABI difference since we just use the
|
|
|
|
// full Rust type as the LLVM argument/return type.
|
|
|
|
if matches!(self.mode, PassMode::Direct(..))
|
|
|
|
&& matches!(self.layout.abi, Abi::Aggregate { .. })
|
|
|
|
{
|
|
|
|
// For aggregates in `Direct` mode to be compatible, the types need to be equal.
|
|
|
|
self.layout.ty == other.layout.ty
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2023-09-07 16:48:02 +02:00
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2021-08-26 21:58:34 +03:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
|
2018-04-25 16:45:29 +03:00
|
|
|
pub enum Conv {
|
2019-11-27 13:23:30 +02:00
|
|
|
// General language calling conventions, for which every target
|
|
|
|
// should have its own backend (e.g. LLVM) support.
|
2018-04-25 16:45:29 +03:00
|
|
|
C,
|
2019-11-27 13:23:30 +02:00
|
|
|
Rust,
|
|
|
|
|
2023-08-26 17:42:59 -07:00
|
|
|
Cold,
|
|
|
|
PreserveMost,
|
|
|
|
PreserveAll,
|
2022-05-29 00:25:14 -07:00
|
|
|
|
2019-11-27 13:23:30 +02:00
|
|
|
// Target-specific calling conventions.
|
2018-04-25 16:45:29 +03:00
|
|
|
ArmAapcs,
|
2021-01-24 17:15:05 +00:00
|
|
|
CCmseNonSecureCall,
|
2024-08-15 09:55:56 +02:00
|
|
|
CCmseNonSecureEntry,
|
2018-04-25 16:45:29 +03:00
|
|
|
|
|
|
|
Msp430Intr,
|
|
|
|
|
|
|
|
PtxKernel,
|
|
|
|
|
|
|
|
X86Fastcall,
|
|
|
|
X86Intr,
|
|
|
|
X86Stdcall,
|
|
|
|
X86ThisCall,
|
|
|
|
X86VectorCall,
|
|
|
|
|
|
|
|
X86_64SysV,
|
|
|
|
X86_64Win64,
|
2018-07-01 22:42:00 -05:00
|
|
|
|
2016-05-06 09:32:10 -04:00
|
|
|
AvrInterrupt,
|
|
|
|
AvrNonBlockingInterrupt,
|
feat: `riscv-interrupt-{m,s}` calling conventions
Similar to prior support added for the mips430, avr, and x86 targets
this change implements the rough equivalent of clang's
[`__attribute__((interrupt))`][clang-attr] for riscv targets, enabling
e.g.
```rust
static mut CNT: usize = 0;
pub extern "riscv-interrupt-m" fn isr_m() {
unsafe {
CNT += 1;
}
}
```
to produce highly effective assembly like:
```asm
pub extern "riscv-interrupt-m" fn isr_m() {
420003a0: 1141 addi sp,sp,-16
unsafe {
CNT += 1;
420003a2: c62a sw a0,12(sp)
420003a4: c42e sw a1,8(sp)
420003a6: 3fc80537 lui a0,0x3fc80
420003aa: 63c52583 lw a1,1596(a0) # 3fc8063c <_ZN12esp_riscv_rt3CNT17hcec3e3a214887d53E.0>
420003ae: 0585 addi a1,a1,1
420003b0: 62b52e23 sw a1,1596(a0)
}
}
420003b4: 4532 lw a0,12(sp)
420003b6: 45a2 lw a1,8(sp)
420003b8: 0141 addi sp,sp,16
420003ba: 30200073 mret
```
(disassembly via `riscv64-unknown-elf-objdump -C -S --disassemble ./esp32c3-hal/target/riscv32imc-unknown-none-elf/release/examples/gpio_interrupt`)
This outcome is superior to hand-coded interrupt routines which, lacking
visibility into any non-assembly body of the interrupt handler, have to
be very conservative and save the [entire CPU state to the stack
frame][full-frame-save]. By instead asking LLVM to only save the
registers that it uses, we defer the decision to the tool with the best
context: it can more accurately account for the cost of spills if it
knows that every additional register used is already at the cost of an
implicit spill.
At the LLVM level, this is apparently [implemented by] marking every
register as "[callee-save]," matching the semantics of an interrupt
handler nicely (it has to leave the CPU state just as it found it after
its `{m|s}ret`).
This approach is not suitable for every interrupt handler, as it makes
no attempt to e.g. save the state in a user-accessible stack frame. For
a full discussion of those challenges and tradeoffs, please refer to
[the interrupt calling conventions RFC][rfc].
Inside rustc, this implementation differs from prior art because LLVM
does not expose the "all-saved" function flavor as a calling convention
directly, instead preferring to use an attribute that allows for
differentiating between "machine-mode" and "superivsor-mode" interrupts.
Finally, some effort has been made to guide those who may not yet be
aware of the differences between machine-mode and supervisor-mode
interrupts as to why no `riscv-interrupt` calling convention is exposed
through rustc, and similarly for why `riscv-interrupt-u` makes no
appearance (as it would complicate future LLVM upgrades).
[clang-attr]: https://clang.llvm.org/docs/AttributeReference.html#interrupt-risc-v
[full-frame-save]: https://github.com/esp-rs/esp-riscv-rt/blob/9281af2ecffe13e40992917316f36920c26acaf3/src/lib.rs#L440-L469
[implemented by]: https://github.com/llvm/llvm-project/blob/b7fb2a3fec7c187d58a6d338ab512d9173bca987/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp#L61-L67
[callee-save]: https://github.com/llvm/llvm-project/blob/973f1fe7a8591c7af148e573491ab68cc15b6ecf/llvm/lib/Target/RISCV/RISCVCallingConv.td#L30-L37
[rfc]: https://github.com/rust-lang/rfcs/pull/3246
2023-05-23 15:08:23 -07:00
|
|
|
|
2023-08-26 17:42:59 -07:00
|
|
|
RiscvInterrupt { kind: RiscvInterruptKind },
|
feat: `riscv-interrupt-{m,s}` calling conventions
Similar to prior support added for the mips430, avr, and x86 targets
this change implements the rough equivalent of clang's
[`__attribute__((interrupt))`][clang-attr] for riscv targets, enabling
e.g.
```rust
static mut CNT: usize = 0;
pub extern "riscv-interrupt-m" fn isr_m() {
unsafe {
CNT += 1;
}
}
```
to produce highly effective assembly like:
```asm
pub extern "riscv-interrupt-m" fn isr_m() {
420003a0: 1141 addi sp,sp,-16
unsafe {
CNT += 1;
420003a2: c62a sw a0,12(sp)
420003a4: c42e sw a1,8(sp)
420003a6: 3fc80537 lui a0,0x3fc80
420003aa: 63c52583 lw a1,1596(a0) # 3fc8063c <_ZN12esp_riscv_rt3CNT17hcec3e3a214887d53E.0>
420003ae: 0585 addi a1,a1,1
420003b0: 62b52e23 sw a1,1596(a0)
}
}
420003b4: 4532 lw a0,12(sp)
420003b6: 45a2 lw a1,8(sp)
420003b8: 0141 addi sp,sp,16
420003ba: 30200073 mret
```
(disassembly via `riscv64-unknown-elf-objdump -C -S --disassemble ./esp32c3-hal/target/riscv32imc-unknown-none-elf/release/examples/gpio_interrupt`)
This outcome is superior to hand-coded interrupt routines which, lacking
visibility into any non-assembly body of the interrupt handler, have to
be very conservative and save the [entire CPU state to the stack
frame][full-frame-save]. By instead asking LLVM to only save the
registers that it uses, we defer the decision to the tool with the best
context: it can more accurately account for the cost of spills if it
knows that every additional register used is already at the cost of an
implicit spill.
At the LLVM level, this is apparently [implemented by] marking every
register as "[callee-save]," matching the semantics of an interrupt
handler nicely (it has to leave the CPU state just as it found it after
its `{m|s}ret`).
This approach is not suitable for every interrupt handler, as it makes
no attempt to e.g. save the state in a user-accessible stack frame. For
a full discussion of those challenges and tradeoffs, please refer to
[the interrupt calling conventions RFC][rfc].
Inside rustc, this implementation differs from prior art because LLVM
does not expose the "all-saved" function flavor as a calling convention
directly, instead preferring to use an attribute that allows for
differentiating between "machine-mode" and "superivsor-mode" interrupts.
Finally, some effort has been made to guide those who may not yet be
aware of the differences between machine-mode and supervisor-mode
interrupts as to why no `riscv-interrupt` calling convention is exposed
through rustc, and similarly for why `riscv-interrupt-u` makes no
appearance (as it would complicate future LLVM upgrades).
[clang-attr]: https://clang.llvm.org/docs/AttributeReference.html#interrupt-risc-v
[full-frame-save]: https://github.com/esp-rs/esp-riscv-rt/blob/9281af2ecffe13e40992917316f36920c26acaf3/src/lib.rs#L440-L469
[implemented by]: https://github.com/llvm/llvm-project/blob/b7fb2a3fec7c187d58a6d338ab512d9173bca987/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp#L61-L67
[callee-save]: https://github.com/llvm/llvm-project/blob/973f1fe7a8591c7af148e573491ab68cc15b6ecf/llvm/lib/Target/RISCV/RISCVCallingConv.td#L30-L37
[rfc]: https://github.com/rust-lang/rfcs/pull/3246
2023-05-23 15:08:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
|
|
|
|
pub enum RiscvInterruptKind {
|
|
|
|
Machine,
|
|
|
|
Supervisor,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RiscvInterruptKind {
|
|
|
|
pub fn as_str(&self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Self::Machine => "machine",
|
|
|
|
Self::Supervisor => "supervisor",
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Metadata describing how the arguments to a native function
|
|
|
|
/// should be passed in order to respect the native ABI.
|
|
|
|
///
|
2024-05-14 22:33:19 +02:00
|
|
|
/// The signature represented by this type may not match the MIR function signature.
|
|
|
|
/// Certain attributes, like `#[track_caller]` can introduce additional arguments, which are present in [`FnAbi`], but not in `FnSig`.
|
|
|
|
/// While this difference is rarely relevant, it should still be kept in mind.
|
|
|
|
///
|
2018-04-25 16:45:29 +03:00
|
|
|
/// I will do my best to describe this structure, but these
|
|
|
|
/// comments are reverse-engineered and may be inaccurate. -NDM
|
2023-01-23 16:02:00 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, HashStable_Generic)]
|
2019-10-29 16:35:26 +02:00
|
|
|
pub struct FnAbi<'a, Ty> {
|
2024-05-14 22:33:19 +02:00
|
|
|
/// The type, layout, and information about how each argument is passed.
|
2022-08-25 19:08:04 +10:00
|
|
|
pub args: Box<[ArgAbi<'a, Ty>]>,
|
2018-04-25 16:45:29 +03:00
|
|
|
|
2024-05-14 22:33:19 +02:00
|
|
|
/// The layout, type, and the way a value is returned from this function.
|
2019-10-29 16:35:26 +02:00
|
|
|
pub ret: ArgAbi<'a, Ty>,
|
2018-04-25 16:45:29 +03:00
|
|
|
|
2024-05-14 22:33:19 +02:00
|
|
|
/// Marks this function as variadic (accepting a variable number of arguments).
|
2019-02-08 17:30:42 +00:00
|
|
|
pub c_variadic: bool,
|
2018-04-25 16:45:29 +03:00
|
|
|
|
2020-01-21 21:52:19 +07:00
|
|
|
/// The count of non-variadic arguments.
|
|
|
|
///
|
|
|
|
/// Should only be different from args.len() when c_variadic is true.
|
2020-03-06 12:13:55 +01:00
|
|
|
/// This can be used to know whether an argument is variadic or not.
|
2022-08-25 19:02:22 +10:00
|
|
|
pub fixed_count: u32,
|
2024-05-14 22:33:19 +02:00
|
|
|
/// The calling convention of this function.
|
2018-04-25 16:45:29 +03:00
|
|
|
pub conv: Conv,
|
2024-05-14 22:33:19 +02:00
|
|
|
/// Indicates if an unwind may happen across a call to this function.
|
2020-03-28 21:47:50 -04:00
|
|
|
pub can_unwind: bool,
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2023-01-23 16:02:00 +00:00
|
|
|
// Needs to be a custom impl because of the bounds on the `TyAndLayout` debug impl.
|
|
|
|
impl<'a, Ty: fmt::Display> fmt::Debug for FnAbi<'a, Ty> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let FnAbi { args, ret, c_variadic, fixed_count, conv, can_unwind } = self;
|
|
|
|
f.debug_struct("FnAbi")
|
|
|
|
.field("args", args)
|
|
|
|
.field("ret", ret)
|
|
|
|
.field("c_variadic", c_variadic)
|
|
|
|
.field("fixed_count", fixed_count)
|
|
|
|
.field("conv", conv)
|
|
|
|
.field("can_unwind", can_unwind)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 19:01:55 +03:00
|
|
|
/// Error produced by attempting to adjust a `FnAbi`, for a "foreign" ABI.
|
2022-02-01 18:44:45 +01:00
|
|
|
#[derive(Copy, Clone, Debug, HashStable_Generic)]
|
2021-08-26 19:01:55 +03:00
|
|
|
pub enum AdjustForForeignAbiError {
|
|
|
|
/// Target architecture doesn't support "foreign" (i.e. non-Rust) ABIs.
|
2022-02-01 18:44:45 +01:00
|
|
|
Unsupported { arch: Symbol, abi: spec::abi::Abi },
|
2021-08-26 19:01:55 +03:00
|
|
|
}
|
|
|
|
|
2019-10-29 16:35:26 +02:00
|
|
|
impl<'a, Ty> FnAbi<'a, Ty> {
|
2021-08-26 19:01:55 +03:00
|
|
|
pub fn adjust_for_foreign_abi<C>(
|
|
|
|
&mut self,
|
|
|
|
cx: &C,
|
|
|
|
abi: spec::abi::Abi,
|
|
|
|
) -> Result<(), AdjustForForeignAbiError>
|
2018-04-25 16:45:29 +03:00
|
|
|
where
|
2021-08-25 16:45:24 +03:00
|
|
|
Ty: TyAbiInterface<'a, C> + Copy,
|
2024-09-16 22:14:35 +07:00
|
|
|
C: HasDataLayout + HasTargetSpec + HasWasmCAbiOpt + HasX86AbiOpt,
|
2018-04-25 16:45:29 +03:00
|
|
|
{
|
2021-02-23 21:38:49 +01:00
|
|
|
if abi == spec::abi::Abi::X86Interrupt {
|
|
|
|
if let Some(arg) = self.args.first_mut() {
|
2024-09-16 13:04:24 -07:00
|
|
|
arg.pass_by_stack_offset(None);
|
2021-02-23 21:38:49 +01:00
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2024-07-10 17:20:21 +02:00
|
|
|
let spec = cx.target_spec();
|
|
|
|
match &spec.arch[..] {
|
2018-04-25 16:45:29 +03:00
|
|
|
"x86" => {
|
2024-09-16 22:14:35 +07:00
|
|
|
let (flavor, regparm) = match abi {
|
|
|
|
spec::abi::Abi::Fastcall { .. } | spec::abi::Abi::Vectorcall { .. } => {
|
|
|
|
(x86::Flavor::FastcallOrVectorcall, None)
|
|
|
|
}
|
|
|
|
spec::abi::Abi::C { .. }
|
|
|
|
| spec::abi::Abi::Cdecl { .. }
|
|
|
|
| spec::abi::Abi::Stdcall { .. } => {
|
|
|
|
(x86::Flavor::General, cx.x86_abi_opt().regparm)
|
|
|
|
}
|
|
|
|
_ => (x86::Flavor::General, None),
|
2018-04-25 16:45:29 +03:00
|
|
|
};
|
2024-09-16 22:14:35 +07:00
|
|
|
x86::compute_abi_info(cx, self, x86::X86Options { flavor, regparm });
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
2022-02-01 18:53:45 +01:00
|
|
|
"x86_64" => match abi {
|
|
|
|
spec::abi::Abi::SysV64 { .. } => x86_64::compute_abi_info(cx, self),
|
2024-06-01 12:25:16 +01:00
|
|
|
spec::abi::Abi::Win64 { .. } => x86_win64::compute_abi_info(cx, self),
|
2022-02-01 18:53:45 +01:00
|
|
|
_ => {
|
|
|
|
if cx.target_spec().is_like_windows {
|
2024-06-01 12:25:16 +01:00
|
|
|
x86_win64::compute_abi_info(cx, self)
|
2022-02-01 18:53:45 +01:00
|
|
|
} else {
|
|
|
|
x86_64::compute_abi_info(cx, self)
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2022-02-01 18:53:45 +01:00
|
|
|
},
|
Add arm64ec-pc-windows-msvc target
Introduces the `arm64ec-pc-windows-msvc` target for building Arm64EC ("Emulation Compatible") binaries for Windows.
For more information about Arm64EC see <https://learn.microsoft.com/en-us/windows/arm/arm64ec>.
Tier 3 policy:
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
I will be the maintainer for this target.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
Target uses the `arm64ec` architecture to match LLVM and MSVC, and the `-pc-windows-msvc` suffix to indicate that it targets Windows via the MSVC environment.
> Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
Target name exactly specifies the type of code that will be produced.
> If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.
Done.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
> The target must not introduce license incompatibilities.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
Understood.
> The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
> Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
> "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
> This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
Understood, I am not a member of the Rust team.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
Both `core` and `alloc` are supported.
Support for `std` dependends on making changes to the standard library, `stdarch` and `backtrace` which cannot be done yet as the bootstrapping compiler raises a warning ("unexpected `cfg` condition value") for `target_arch = "arm64ec"`.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation is provided in src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
> Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
> In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.
Understood.
2023-12-15 16:46:34 -08:00
|
|
|
"aarch64" | "arm64ec" => {
|
2023-06-14 22:39:11 -04:00
|
|
|
let kind = if cx.target_spec().is_like_osx {
|
|
|
|
aarch64::AbiKind::DarwinPCS
|
|
|
|
} else if cx.target_spec().is_like_windows {
|
|
|
|
aarch64::AbiKind::Win64
|
2022-06-06 12:40:10 -04:00
|
|
|
} else {
|
2023-06-14 22:39:11 -04:00
|
|
|
aarch64::AbiKind::AAPCS
|
2022-06-06 12:40:10 -04:00
|
|
|
};
|
2023-06-14 22:39:11 -04:00
|
|
|
aarch64::compute_abi_info(cx, self, kind)
|
2022-06-06 12:40:10 -04:00
|
|
|
}
|
2018-07-18 22:01:19 -05:00
|
|
|
"amdgpu" => amdgpu::compute_abi_info(cx, self),
|
2018-04-25 16:45:29 +03:00
|
|
|
"arm" => arm::compute_abi_info(cx, self),
|
2016-05-06 09:32:10 -04:00
|
|
|
"avr" => avr::compute_abi_info(self),
|
2022-09-17 18:00:34 +08:00
|
|
|
"loongarch64" => loongarch::compute_abi_info(cx, self),
|
2021-08-25 07:45:06 +00:00
|
|
|
"m68k" => m68k::compute_abi_info(self),
|
2023-07-13 22:19:59 +08:00
|
|
|
"csky" => csky::compute_abi_info(self),
|
2023-06-28 13:35:39 +08:00
|
|
|
"mips" | "mips32r6" => mips::compute_abi_info(cx, self),
|
2023-06-28 13:24:01 +08:00
|
|
|
"mips64" | "mips64r6" => mips64::compute_abi_info(cx, self),
|
2024-06-01 12:25:16 +01:00
|
|
|
"powerpc" => powerpc::compute_abi_info(cx, self),
|
2018-04-25 16:45:29 +03:00
|
|
|
"powerpc64" => powerpc64::compute_abi_info(cx, self),
|
|
|
|
"s390x" => s390x::compute_abi_info(cx, self),
|
|
|
|
"msp430" => msp430::compute_abi_info(self),
|
|
|
|
"sparc" => sparc::compute_abi_info(cx, self),
|
|
|
|
"sparc64" => sparc64::compute_abi_info(cx, self),
|
2022-03-07 15:09:28 +01:00
|
|
|
"nvptx64" => {
|
2024-07-10 17:20:21 +02:00
|
|
|
if cx.target_spec().adjust_abi(abi, self.c_variadic) == spec::abi::Abi::PtxKernel {
|
2022-03-07 15:09:28 +01:00
|
|
|
nvptx64::compute_ptx_kernel_abi_info(cx, self)
|
|
|
|
} else {
|
|
|
|
nvptx64::compute_abi_info(self)
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
"hexagon" => hexagon::compute_abi_info(self),
|
2020-09-12 23:31:14 +01:00
|
|
|
"xtensa" => xtensa::compute_abi_info(cx, self),
|
2020-01-21 21:52:19 +07:00
|
|
|
"riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
|
2024-07-10 17:20:21 +02:00
|
|
|
"wasm32" => {
|
|
|
|
if spec.os == "unknown" && cx.wasm_c_abi_opt() == WasmCAbi::Legacy {
|
rustc: Add a new `wasm` ABI
This commit implements the idea of a new ABI for the WebAssembly target,
one called `"wasm"`. This ABI is entirely of my own invention
and has no current precedent, but I think that the addition of this ABI
might help solve a number of issues with the WebAssembly targets.
When `wasm32-unknown-unknown` was first added to Rust I naively
"implemented an abi" for the target. I then went to write `wasm-bindgen`
which accidentally relied on details of this ABI. Turns out the ABI
definition didn't match C, which is causing issues for C/Rust interop.
Currently the compiler has a "wasm32 bindgen compat" ABI which is the
original implementation I added, and it's purely there for, well,
`wasm-bindgen`.
Another issue with the WebAssembly target is that it's not clear to me
when and if the default C ABI will change to account for WebAssembly's
multi-value feature (a feature that allows functions to return multiple
values). Even if this does happen, though, it seems like the C ABI will
be guided based on the performance of WebAssembly code and will likely
not match even what the current wasm-bindgen-compat ABI is today. This
leaves a hole in Rust's expressivity in binding WebAssembly where given
a particular import type, Rust may not be able to import that signature
with an updated C ABI for multi-value.
To fix these issues I had the idea of a new ABI for WebAssembly, one
called `wasm`. The definition of this ABI is "what you write
maps straight to wasm". The goal here is that whatever you write down in
the parameter list or in the return values goes straight into the
function's signature in the WebAssembly file. This special ABI is for
intentionally matching the ABI of an imported function from the
environment or exporting a function with the right signature.
With the addition of a new ABI, this enables rustc to:
* Eventually remove the "wasm-bindgen compat hack". Once this
ABI is stable wasm-bindgen can switch to using it everywhere.
Afterwards the wasm32-unknown-unknown target can have its default ABI
updated to match C.
* Expose the ability to precisely match an ABI signature for a
WebAssembly function, regardless of what the C ABI that clang chooses
turns out to be.
* Continue to evolve the definition of the default C ABI to match what
clang does on all targets, since the purpose of that ABI will be
explicitly matching C rather than generating particular function
imports/exports.
Naturally this is implemented as an unstable feature initially, but it
would be nice for this to get stabilized (if it works) in the near-ish
future to remove the wasm32-unknown-unknown incompatibility with the C
ABI. Doing this, however, requires the feature to be on stable because
wasm-bindgen works with stable Rust.
2021-04-01 16:08:29 -07:00
|
|
|
wasm::compute_wasm_abi_info(self)
|
|
|
|
} else {
|
|
|
|
wasm::compute_c_abi_info(cx, self)
|
|
|
|
}
|
|
|
|
}
|
2024-07-10 17:20:21 +02:00
|
|
|
"wasm64" => wasm::compute_c_abi_info(cx, self),
|
2021-04-20 19:03:10 +10:00
|
|
|
"bpf" => bpf::compute_abi_info(self),
|
2021-08-26 19:01:55 +03:00
|
|
|
arch => {
|
2022-02-01 18:44:45 +01:00
|
|
|
return Err(AdjustForForeignAbiError::Unsupported {
|
|
|
|
arch: Symbol::intern(arch),
|
|
|
|
abi,
|
|
|
|
});
|
2021-08-26 19:01:55 +03:00
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-10-16 23:46:39 +02:00
|
|
|
|
|
|
|
pub fn adjust_for_rust_abi<C>(&mut self, cx: &C, abi: SpecAbi)
|
|
|
|
where
|
|
|
|
Ty: TyAbiInterface<'a, C> + Copy,
|
|
|
|
C: HasDataLayout + HasTargetSpec,
|
|
|
|
{
|
|
|
|
let spec = cx.target_spec();
|
|
|
|
match &spec.arch[..] {
|
|
|
|
"x86" => x86::compute_rust_abi_info(cx, self, abi),
|
2024-10-20 02:36:36 +02:00
|
|
|
"riscv32" | "riscv64" => riscv::compute_rust_abi_info(cx, self, abi),
|
2024-10-20 23:17:04 +02:00
|
|
|
"loongarch64" => loongarch::compute_rust_abi_info(cx, self, abi),
|
2024-10-16 23:46:39 +02:00
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (arg_idx, arg) in self
|
|
|
|
.args
|
|
|
|
.iter_mut()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(idx, arg)| (Some(idx), arg))
|
|
|
|
.chain(iter::once((None, &mut self.ret)))
|
|
|
|
{
|
|
|
|
if arg.is_ignore() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if arg_idx.is_none() && arg.layout.size > Pointer(AddressSpace::DATA).size(cx) * 2 {
|
|
|
|
// Return values larger than 2 registers using a return area
|
|
|
|
// pointer. LLVM and Cranelift disagree about how to return
|
|
|
|
// values that don't fit in the registers designated for return
|
|
|
|
// values. LLVM will force the entire return value to be passed
|
|
|
|
// by return area pointer, while Cranelift will look at each IR level
|
|
|
|
// return value independently and decide to pass it in a
|
|
|
|
// register or not, which would result in the return value
|
|
|
|
// being passed partially in registers and partially through a
|
|
|
|
// return area pointer.
|
|
|
|
//
|
|
|
|
// While Cranelift may need to be fixed as the LLVM behavior is
|
|
|
|
// generally more correct with respect to the surface language,
|
|
|
|
// forcing this behavior in rustc itself makes it easier for
|
|
|
|
// other backends to conform to the Rust ABI and for the C ABI
|
|
|
|
// rustc already handles this behavior anyway.
|
|
|
|
//
|
|
|
|
// In addition LLVM's decision to pass the return value in
|
|
|
|
// registers or using a return area pointer depends on how
|
|
|
|
// exactly the return type is lowered to an LLVM IR type. For
|
|
|
|
// example `Option<u128>` can be lowered as `{ i128, i128 }`
|
|
|
|
// in which case the x86_64 backend would use a return area
|
|
|
|
// pointer, or it could be passed as `{ i32, i128 }` in which
|
|
|
|
// case the x86_64 backend would pass it in registers by taking
|
|
|
|
// advantage of an LLVM ABI extension that allows using 3
|
|
|
|
// registers for the x86_64 sysv call conv rather than the
|
|
|
|
// officially specified 2 registers.
|
|
|
|
//
|
|
|
|
// FIXME: Technically we should look at the amount of available
|
|
|
|
// return registers rather than guessing that there are 2
|
|
|
|
// registers for return values. In practice only a couple of
|
|
|
|
// architectures have less than 2 return registers. None of
|
|
|
|
// which supported by Cranelift.
|
|
|
|
//
|
|
|
|
// NOTE: This adjustment is only necessary for the Rust ABI as
|
|
|
|
// for other ABI's the calling convention implementations in
|
|
|
|
// rustc_target already ensure any return value which doesn't
|
|
|
|
// fit in the available amount of return registers is passed in
|
|
|
|
// the right way for the current target.
|
|
|
|
arg.make_indirect();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
match arg.layout.abi {
|
|
|
|
Abi::Aggregate { .. } => {}
|
|
|
|
|
|
|
|
// This is a fun case! The gist of what this is doing is
|
|
|
|
// that we want callers and callees to always agree on the
|
|
|
|
// ABI of how they pass SIMD arguments. If we were to *not*
|
|
|
|
// make these arguments indirect then they'd be immediates
|
|
|
|
// in LLVM, which means that they'd used whatever the
|
|
|
|
// appropriate ABI is for the callee and the caller. That
|
|
|
|
// means, for example, if the caller doesn't have AVX
|
|
|
|
// enabled but the callee does, then passing an AVX argument
|
|
|
|
// across this boundary would cause corrupt data to show up.
|
|
|
|
//
|
|
|
|
// This problem is fixed by unconditionally passing SIMD
|
|
|
|
// arguments through memory between callers and callees
|
|
|
|
// which should get them all to agree on ABI regardless of
|
|
|
|
// target feature sets. Some more information about this
|
|
|
|
// issue can be found in #44367.
|
|
|
|
//
|
|
|
|
// Note that the intrinsic ABI is exempt here as
|
|
|
|
// that's how we connect up to LLVM and it's unstable
|
|
|
|
// anyway, we control all calls to it in libstd.
|
|
|
|
Abi::Vector { .. } if abi != SpecAbi::RustIntrinsic && spec.simd_types_indirect => {
|
|
|
|
arg.make_indirect();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
// Compute `Aggregate` ABI.
|
|
|
|
|
|
|
|
let is_indirect_not_on_stack =
|
|
|
|
matches!(arg.mode, PassMode::Indirect { on_stack: false, .. });
|
|
|
|
assert!(is_indirect_not_on_stack);
|
|
|
|
|
|
|
|
let size = arg.layout.size;
|
|
|
|
if !arg.layout.is_unsized() && size <= Pointer(AddressSpace::DATA).size(cx) {
|
|
|
|
// We want to pass small aggregates as immediates, but using
|
|
|
|
// an LLVM aggregate type for this leads to bad optimizations,
|
|
|
|
// so we pick an appropriately sized integer type instead.
|
|
|
|
arg.cast_to(Reg { kind: RegKind::Integer, size });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
2022-08-25 17:55:21 +10:00
|
|
|
|
2022-11-05 14:36:38 +05:30
|
|
|
impl FromStr for Conv {
|
|
|
|
type Err = String;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"C" => Ok(Conv::C),
|
|
|
|
"Rust" => Ok(Conv::Rust),
|
|
|
|
"RustCold" => Ok(Conv::Rust),
|
|
|
|
"ArmAapcs" => Ok(Conv::ArmAapcs),
|
|
|
|
"CCmseNonSecureCall" => Ok(Conv::CCmseNonSecureCall),
|
2024-08-15 09:55:56 +02:00
|
|
|
"CCmseNonSecureEntry" => Ok(Conv::CCmseNonSecureEntry),
|
2022-11-05 14:36:38 +05:30
|
|
|
"Msp430Intr" => Ok(Conv::Msp430Intr),
|
|
|
|
"PtxKernel" => Ok(Conv::PtxKernel),
|
|
|
|
"X86Fastcall" => Ok(Conv::X86Fastcall),
|
|
|
|
"X86Intr" => Ok(Conv::X86Intr),
|
|
|
|
"X86Stdcall" => Ok(Conv::X86Stdcall),
|
|
|
|
"X86ThisCall" => Ok(Conv::X86ThisCall),
|
|
|
|
"X86VectorCall" => Ok(Conv::X86VectorCall),
|
|
|
|
"X86_64SysV" => Ok(Conv::X86_64SysV),
|
|
|
|
"X86_64Win64" => Ok(Conv::X86_64Win64),
|
|
|
|
"AvrInterrupt" => Ok(Conv::AvrInterrupt),
|
|
|
|
"AvrNonBlockingInterrupt" => Ok(Conv::AvrNonBlockingInterrupt),
|
feat: `riscv-interrupt-{m,s}` calling conventions
Similar to prior support added for the mips430, avr, and x86 targets
this change implements the rough equivalent of clang's
[`__attribute__((interrupt))`][clang-attr] for riscv targets, enabling
e.g.
```rust
static mut CNT: usize = 0;
pub extern "riscv-interrupt-m" fn isr_m() {
unsafe {
CNT += 1;
}
}
```
to produce highly effective assembly like:
```asm
pub extern "riscv-interrupt-m" fn isr_m() {
420003a0: 1141 addi sp,sp,-16
unsafe {
CNT += 1;
420003a2: c62a sw a0,12(sp)
420003a4: c42e sw a1,8(sp)
420003a6: 3fc80537 lui a0,0x3fc80
420003aa: 63c52583 lw a1,1596(a0) # 3fc8063c <_ZN12esp_riscv_rt3CNT17hcec3e3a214887d53E.0>
420003ae: 0585 addi a1,a1,1
420003b0: 62b52e23 sw a1,1596(a0)
}
}
420003b4: 4532 lw a0,12(sp)
420003b6: 45a2 lw a1,8(sp)
420003b8: 0141 addi sp,sp,16
420003ba: 30200073 mret
```
(disassembly via `riscv64-unknown-elf-objdump -C -S --disassemble ./esp32c3-hal/target/riscv32imc-unknown-none-elf/release/examples/gpio_interrupt`)
This outcome is superior to hand-coded interrupt routines which, lacking
visibility into any non-assembly body of the interrupt handler, have to
be very conservative and save the [entire CPU state to the stack
frame][full-frame-save]. By instead asking LLVM to only save the
registers that it uses, we defer the decision to the tool with the best
context: it can more accurately account for the cost of spills if it
knows that every additional register used is already at the cost of an
implicit spill.
At the LLVM level, this is apparently [implemented by] marking every
register as "[callee-save]," matching the semantics of an interrupt
handler nicely (it has to leave the CPU state just as it found it after
its `{m|s}ret`).
This approach is not suitable for every interrupt handler, as it makes
no attempt to e.g. save the state in a user-accessible stack frame. For
a full discussion of those challenges and tradeoffs, please refer to
[the interrupt calling conventions RFC][rfc].
Inside rustc, this implementation differs from prior art because LLVM
does not expose the "all-saved" function flavor as a calling convention
directly, instead preferring to use an attribute that allows for
differentiating between "machine-mode" and "superivsor-mode" interrupts.
Finally, some effort has been made to guide those who may not yet be
aware of the differences between machine-mode and supervisor-mode
interrupts as to why no `riscv-interrupt` calling convention is exposed
through rustc, and similarly for why `riscv-interrupt-u` makes no
appearance (as it would complicate future LLVM upgrades).
[clang-attr]: https://clang.llvm.org/docs/AttributeReference.html#interrupt-risc-v
[full-frame-save]: https://github.com/esp-rs/esp-riscv-rt/blob/9281af2ecffe13e40992917316f36920c26acaf3/src/lib.rs#L440-L469
[implemented by]: https://github.com/llvm/llvm-project/blob/b7fb2a3fec7c187d58a6d338ab512d9173bca987/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp#L61-L67
[callee-save]: https://github.com/llvm/llvm-project/blob/973f1fe7a8591c7af148e573491ab68cc15b6ecf/llvm/lib/Target/RISCV/RISCVCallingConv.td#L30-L37
[rfc]: https://github.com/rust-lang/rfcs/pull/3246
2023-05-23 15:08:23 -07:00
|
|
|
"RiscvInterrupt(machine)" => {
|
|
|
|
Ok(Conv::RiscvInterrupt { kind: RiscvInterruptKind::Machine })
|
|
|
|
}
|
|
|
|
"RiscvInterrupt(supervisor)" => {
|
|
|
|
Ok(Conv::RiscvInterrupt { kind: RiscvInterruptKind::Supervisor })
|
|
|
|
}
|
2023-04-10 22:02:52 +02:00
|
|
|
_ => Err(format!("'{s}' is not a valid value for entry function call convention.")),
|
2022-11-05 14:36:38 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 17:55:21 +10:00
|
|
|
// Some types are used a lot. Make sure they don't unintentionally get bigger.
|
2024-04-16 17:02:20 +10:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2022-08-25 17:55:21 +10:00
|
|
|
mod size_asserts {
|
|
|
|
use rustc_data_structures::static_assert_size;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2022-08-25 17:55:21 +10:00
|
|
|
use super::*;
|
2022-10-05 21:46:21 +02:00
|
|
|
// tidy-alphabetical-start
|
2022-08-25 22:19:38 +10:00
|
|
|
static_assert_size!(ArgAbi<'_, usize>, 56);
|
|
|
|
static_assert_size!(FnAbi<'_, usize>, 80);
|
2022-10-05 21:46:21 +02:00
|
|
|
// tidy-alphabetical-end
|
2022-08-25 17:55:21 +10:00
|
|
|
}
|