2020-03-31 13:44:52 +00:00
|
|
|
use crate::abi::{self, Abi, Align, FieldsShape, Size};
|
2020-03-04 14:50:21 +00:00
|
|
|
use crate::abi::{HasDataLayout, LayoutOf, TyAndLayout, TyAndLayoutMethods};
|
2021-01-26 11:09:06 +01:00
|
|
|
use crate::spec::{self, HasTargetSpec};
|
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;
|
2018-04-25 16:45:29 +03:00
|
|
|
mod hexagon;
|
|
|
|
mod mips;
|
|
|
|
mod mips64;
|
|
|
|
mod msp430;
|
|
|
|
mod nvptx;
|
|
|
|
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;
|
2019-12-22 17:42:04 -05:00
|
|
|
mod wasm32;
|
|
|
|
mod wasm32_bindgen_compat;
|
2018-04-25 16:45:29 +03:00
|
|
|
mod x86;
|
|
|
|
mod x86_64;
|
|
|
|
mod x86_win64;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
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
|
|
|
///
|
2021-01-26 13:42:40 +01:00
|
|
|
/// The argument has a layout abi of `Scalar`, `Vector` or in rare cases `Aggregate`.
|
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),
|
|
|
|
/// Pass the argument after casting it, to either
|
|
|
|
/// a single uniform or a pair of registers.
|
|
|
|
Cast(CastTarget),
|
|
|
|
/// Pass the argument indirectly via a hidden pointer.
|
2020-11-14 14:29:40 +01:00
|
|
|
/// The `extra_attrs` value, if any, is for the extra data (vtable or length)
|
2018-08-03 23:32:21 +09:00
|
|
|
/// which indicates that it refers to an unsized rvalue.
|
2020-11-14 14:29:40 +01:00
|
|
|
/// `on_stack` defines that the 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 attribute.
|
|
|
|
Indirect { attrs: ArgAttributes, extra_attrs: Option<ArgAttributes>, on_stack: bool },
|
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 {
|
|
|
|
// The subset of llvm::Attribute needed for arguments, packed into a bitfield.
|
2019-02-08 21:00:07 +09:00
|
|
|
bitflags::bitflags! {
|
2018-04-25 16:45:29 +03:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ArgAttribute: u16 {
|
|
|
|
const NoAlias = 1 << 1;
|
|
|
|
const NoCapture = 1 << 2;
|
|
|
|
const NonNull = 1 << 3;
|
|
|
|
const ReadOnly = 1 << 4;
|
|
|
|
const InReg = 1 << 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
/// defines if this extension should be zero-extension or sign-extension when necssary. When it is
|
|
|
|
/// not necesary to extend the argument, this enum is ignored.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
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.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
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,
|
2019-12-22 17:42:04 -05: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum RegKind {
|
|
|
|
Integer,
|
|
|
|
Float,
|
2019-12-22 17:42:04 -05:00
|
|
|
Vector,
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub struct Reg {
|
|
|
|
pub kind: RegKind,
|
|
|
|
pub size: Size,
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! reg_ctor {
|
|
|
|
($name:ident, $kind:ident, $bits:expr) => {
|
|
|
|
pub fn $name() -> Reg {
|
2019-12-22 17:42:04 -05:00
|
|
|
Reg { kind: RegKind::$kind, size: Size::from_bits($bits) }
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
};
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Reg {
|
|
|
|
reg_ctor!(i8, Integer, 8);
|
|
|
|
reg_ctor!(i16, Integer, 16);
|
|
|
|
reg_ctor!(i32, Integer, 32);
|
|
|
|
reg_ctor!(i64, Integer, 64);
|
2020-01-21 21:52:19 +07:00
|
|
|
reg_ctor!(i128, Integer, 128);
|
2018-04-25 16:45:29 +03:00
|
|
|
|
|
|
|
reg_ctor!(f32, Float, 32);
|
|
|
|
reg_ctor!(f64, Float, 64);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reg {
|
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
|
|
|
let dl = cx.data_layout();
|
|
|
|
match self.kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
RegKind::Integer => match self.size.bits() {
|
|
|
|
1 => dl.i1_align.abi,
|
|
|
|
2..=8 => dl.i8_align.abi,
|
|
|
|
9..=16 => dl.i16_align.abi,
|
|
|
|
17..=32 => dl.i32_align.abi,
|
|
|
|
33..=64 => dl.i64_align.abi,
|
|
|
|
65..=128 => dl.i128_align.abi,
|
|
|
|
_ => panic!("unsupported integer: {:?}", self),
|
|
|
|
},
|
|
|
|
RegKind::Float => match self.size.bits() {
|
|
|
|
32 => dl.f32_align.abi,
|
|
|
|
64 => dl.f64_align.abi,
|
|
|
|
_ => panic!("unsupported float: {:?}", self),
|
|
|
|
},
|
2018-09-09 01:16:45 +03:00
|
|
|
RegKind::Vector => dl.vector_align(self.size).abi,
|
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).
|
2018-04-25 16:45:29 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
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),
|
2018-04-25 16:45:29 +03:00
|
|
|
/// * if `unit.kind` is `Integer`, the last element
|
2018-11-27 02:59:49 +00:00
|
|
|
/// can be shorter, i.e., `{ i64, i64, i32 }` for
|
2019-02-08 14:53:55 +01:00
|
|
|
/// 64-bit integers with a total size of 20 bytes.
|
2018-04-25 16:45:29 +03:00
|
|
|
pub total: Size,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Reg> for Uniform {
|
|
|
|
fn from(unit: Reg) -> Uniform {
|
2019-12-22 17:42:04 -05:00
|
|
|
Uniform { unit, total: unit.size }
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
pub struct CastTarget {
|
|
|
|
pub prefix: [Option<RegKind>; 8],
|
2020-11-15 10:12:10 +01:00
|
|
|
pub prefix_chunk_size: Size,
|
2018-04-25 16:45:29 +03:00
|
|
|
pub rest: Uniform,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-11-15 10:12:10 +01:00
|
|
|
CastTarget { prefix: [None; 8], prefix_chunk_size: Size::ZERO, rest: uniform }
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CastTarget {
|
|
|
|
pub fn pair(a: Reg, b: Reg) -> CastTarget {
|
|
|
|
CastTarget {
|
|
|
|
prefix: [Some(a.kind), None, None, None, None, None, None, None],
|
2020-11-15 10:12:10 +01:00
|
|
|
prefix_chunk_size: a.size,
|
2019-12-22 17:42:04 -05:00
|
|
|
rest: Uniform::from(b),
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 22:57:53 +02:00
|
|
|
pub fn size<C: HasDataLayout>(&self, cx: &C) -> Size {
|
2020-11-15 10:12:10 +01:00
|
|
|
(self.prefix_chunk_size * self.prefix.iter().filter(|x| x.is_some()).count() as u64)
|
2019-12-22 17:42:04 -05:00
|
|
|
.align_to(self.rest.align(cx))
|
|
|
|
+ self.rest.total
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2018-09-09 01:16:45 +03:00
|
|
|
pub fn align<C: HasDataLayout>(&self, cx: &C) -> Align {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.prefix
|
|
|
|
.iter()
|
2020-11-15 10:12:10 +01:00
|
|
|
.filter_map(|x| x.map(|kind| Reg { kind, size: self.prefix_chunk_size }.align(cx)))
|
2019-12-22 17:42:04 -05:00
|
|
|
.fold(cx.data_layout().aggregate_align.abi.max(self.rest.align(cx)), |acc, align| {
|
|
|
|
acc.max(align)
|
|
|
|
})
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 02:52:14 +02:00
|
|
|
/// Return value from the `homogeneous_aggregate` test function.
|
2019-01-09 15:16:32 -05:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum HomogeneousAggregate {
|
|
|
|
/// Yes, all the "leaf fields" of this struct are passed in the
|
|
|
|
/// same way (specified in the `Reg` value).
|
|
|
|
Homogeneous(Reg),
|
|
|
|
|
|
|
|
/// There are no leaf fields at all.
|
|
|
|
NoData,
|
|
|
|
}
|
|
|
|
|
2020-01-22 02:52:14 +02:00
|
|
|
/// Error from the `homogeneous_aggregate` test function, indicating
|
|
|
|
/// there are distinct leaf fields passed in different ways,
|
|
|
|
/// or this is uninhabited.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct Heterogeneous;
|
|
|
|
|
2019-01-09 15:16:32 -05:00
|
|
|
impl HomogeneousAggregate {
|
|
|
|
/// If this is a homogeneous aggregate, returns the homogeneous
|
|
|
|
/// unit, else `None`.
|
|
|
|
pub fn unit(self) -> Option<Reg> {
|
2020-01-22 02:52:14 +02:00
|
|
|
match self {
|
|
|
|
HomogeneousAggregate::Homogeneous(reg) => Some(reg),
|
|
|
|
HomogeneousAggregate::NoData => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to combine two `HomogeneousAggregate`s, e.g. from two fields in
|
|
|
|
/// the same `struct`. Only succeeds if only one of them has any data,
|
|
|
|
/// or both units are identical.
|
|
|
|
fn merge(self, other: HomogeneousAggregate) -> Result<HomogeneousAggregate, Heterogeneous> {
|
|
|
|
match (self, other) {
|
|
|
|
(x, HomogeneousAggregate::NoData) | (HomogeneousAggregate::NoData, x) => Ok(x),
|
|
|
|
|
|
|
|
(HomogeneousAggregate::Homogeneous(a), HomogeneousAggregate::Homogeneous(b)) => {
|
|
|
|
if a != b {
|
|
|
|
return Err(Heterogeneous);
|
|
|
|
}
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
2019-01-09 15:16:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:50:21 +00:00
|
|
|
impl<'a, Ty> TyAndLayout<'a, Ty> {
|
2018-04-25 16:45:29 +03:00
|
|
|
fn is_aggregate(&self) -> bool {
|
|
|
|
match self.abi {
|
2019-12-22 17:42:04 -05:00
|
|
|
Abi::Uninhabited | Abi::Scalar(_) | Abi::Vector { .. } => false,
|
|
|
|
Abi::ScalarPair(..) | Abi::Aggregate { .. } => true,
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 02:52:14 +02:00
|
|
|
/// Returns `Homogeneous` if this layout is an aggregate containing fields of
|
|
|
|
/// only a single type (e.g., `(u32, u32)`). Such aggregates are often
|
2019-01-09 15:16:32 -05:00
|
|
|
/// special-cased in ABIs.
|
|
|
|
///
|
|
|
|
/// Note: We generally ignore fields of zero-sized type when computing
|
2019-02-08 14:53:55 +01:00
|
|
|
/// this value (see #56877).
|
2019-01-09 15:16:32 -05:00
|
|
|
///
|
|
|
|
/// This is public so that it can be used in unit tests, but
|
|
|
|
/// should generally only be relevant to the ABI details of
|
|
|
|
/// specific targets.
|
2020-01-22 02:52:14 +02:00
|
|
|
pub fn homogeneous_aggregate<C>(&self, cx: &C) -> Result<HomogeneousAggregate, Heterogeneous>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
2020-03-04 14:50:21 +00:00
|
|
|
Ty: TyAndLayoutMethods<'a, C> + Copy,
|
|
|
|
C: LayoutOf<Ty = Ty, TyAndLayout = Self>,
|
2018-04-25 16:45:29 +03:00
|
|
|
{
|
|
|
|
match self.abi {
|
2020-01-22 02:52:14 +02:00
|
|
|
Abi::Uninhabited => Err(Heterogeneous),
|
2018-04-25 16:45:29 +03:00
|
|
|
|
|
|
|
// The primitive for this algorithm.
|
|
|
|
Abi::Scalar(ref scalar) => {
|
|
|
|
let kind = match scalar.value {
|
2019-12-22 17:42:04 -05:00
|
|
|
abi::Int(..) | abi::Pointer => RegKind::Integer,
|
2019-11-07 16:54:25 +02:00
|
|
|
abi::F32 | abi::F64 => RegKind::Float,
|
2018-04-25 16:45:29 +03:00
|
|
|
};
|
2020-01-22 02:52:14 +02:00
|
|
|
Ok(HomogeneousAggregate::Homogeneous(Reg { kind, size: self.size }))
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Abi::Vector { .. } => {
|
2019-01-09 15:16:32 -05:00
|
|
|
assert!(!self.is_zst());
|
2020-01-22 02:52:14 +02:00
|
|
|
Ok(HomogeneousAggregate::Homogeneous(Reg {
|
|
|
|
kind: RegKind::Vector,
|
|
|
|
size: self.size,
|
|
|
|
}))
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
Abi::ScalarPair(..) | Abi::Aggregate { .. } => {
|
2020-04-16 15:15:46 +00:00
|
|
|
// Helper for computing `homogeneous_aggregate`, allowing a custom
|
2020-01-22 02:52:54 +02:00
|
|
|
// starting offset (used below for handling variants).
|
2020-01-22 02:52:14 +02:00
|
|
|
let from_fields_at =
|
|
|
|
|layout: Self,
|
|
|
|
start: Size|
|
|
|
|
-> Result<(HomogeneousAggregate, Size), Heterogeneous> {
|
|
|
|
let is_union = match layout.fields {
|
2020-04-16 15:15:46 +00:00
|
|
|
FieldsShape::Primitive => {
|
|
|
|
unreachable!("aggregates can't have `FieldsShape::Primitive`")
|
|
|
|
}
|
2020-03-31 13:44:52 +00:00
|
|
|
FieldsShape::Array { count, .. } => {
|
2020-01-22 02:52:14 +02:00
|
|
|
assert_eq!(start, Size::ZERO);
|
|
|
|
|
|
|
|
let result = if count > 0 {
|
|
|
|
layout.field(cx, 0).homogeneous_aggregate(cx)?
|
|
|
|
} else {
|
|
|
|
HomogeneousAggregate::NoData
|
|
|
|
};
|
|
|
|
return Ok((result, layout.size));
|
|
|
|
}
|
2020-03-31 13:44:52 +00:00
|
|
|
FieldsShape::Union(_) => true,
|
|
|
|
FieldsShape::Arbitrary { .. } => false,
|
2020-01-22 02:52:14 +02:00
|
|
|
};
|
2018-04-25 16:45:29 +03:00
|
|
|
|
2020-01-22 02:52:14 +02:00
|
|
|
let mut result = HomogeneousAggregate::NoData;
|
|
|
|
let mut total = start;
|
2018-04-25 16:45:29 +03:00
|
|
|
|
2020-01-22 02:52:14 +02:00
|
|
|
for i in 0..layout.fields.count() {
|
|
|
|
if !is_union && total != layout.fields.offset(i) {
|
|
|
|
return Err(Heterogeneous);
|
|
|
|
}
|
2019-01-09 15:16:32 -05:00
|
|
|
|
2020-01-22 02:52:14 +02:00
|
|
|
let field = layout.field(cx, i);
|
|
|
|
|
|
|
|
result = result.merge(field.homogeneous_aggregate(cx)?)?;
|
|
|
|
|
|
|
|
// Keep track of the offset (without padding).
|
|
|
|
let size = field.size;
|
|
|
|
if is_union {
|
|
|
|
total = total.max(size);
|
|
|
|
} else {
|
|
|
|
total += size;
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 02:52:14 +02:00
|
|
|
Ok((result, total))
|
|
|
|
};
|
|
|
|
|
|
|
|
let (mut result, mut total) = from_fields_at(*self, Size::ZERO)?;
|
2018-04-25 16:45:29 +03:00
|
|
|
|
2020-01-22 02:52:54 +02:00
|
|
|
match &self.variants {
|
|
|
|
abi::Variants::Single { .. } => {}
|
|
|
|
abi::Variants::Multiple { variants, .. } => {
|
|
|
|
// Treat enum variants like union members.
|
|
|
|
// HACK(eddyb) pretend the `enum` field (discriminant)
|
|
|
|
// is at the start of every variant (otherwise the gap
|
|
|
|
// at the start of all variants would disqualify them).
|
|
|
|
//
|
|
|
|
// NB: for all tagged `enum`s (which include all non-C-like
|
|
|
|
// `enum`s with defined FFI representation), this will
|
2020-03-06 12:13:55 +01:00
|
|
|
// match the homogeneous computation on the equivalent
|
2020-01-22 02:52:54 +02:00
|
|
|
// `struct { tag; union { variant1; ... } }` and/or
|
|
|
|
// `union { struct { tag; variant1; } ... }`
|
|
|
|
// (the offsets of variant fields should be identical
|
2020-03-06 12:13:55 +01:00
|
|
|
// between the two for either to be a homogeneous aggregate).
|
2020-01-22 02:52:54 +02:00
|
|
|
let variant_start = total;
|
|
|
|
for variant_idx in variants.indices() {
|
|
|
|
let (variant_result, variant_total) =
|
|
|
|
from_fields_at(self.for_variant(cx, variant_idx), variant_start)?;
|
|
|
|
|
|
|
|
result = result.merge(variant_result)?;
|
|
|
|
total = total.max(variant_total);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 16:45:29 +03:00
|
|
|
// There needs to be no padding.
|
|
|
|
if total != self.size {
|
2020-01-22 02:52:14 +02:00
|
|
|
Err(Heterogeneous)
|
2018-04-25 16:45:29 +03:00
|
|
|
} else {
|
2019-01-09 15:16:32 -05:00
|
|
|
match result {
|
2020-01-22 02:52:14 +02:00
|
|
|
HomogeneousAggregate::Homogeneous(_) => {
|
2019-01-09 15:16:32 -05:00
|
|
|
assert_ne!(total, Size::ZERO);
|
|
|
|
}
|
2020-01-22 02:52:14 +02:00
|
|
|
HomogeneousAggregate::NoData => {
|
2019-01-09 15:16:32 -05:00
|
|
|
assert_eq!(total, Size::ZERO);
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 02:52:14 +02:00
|
|
|
Ok(result)
|
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.
|
|
|
|
#[derive(Debug)]
|
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
|
|
|
|
|
|
|
/// Dummy argument, which is emitted before the real argument.
|
|
|
|
pub pad: Option<Reg>,
|
|
|
|
|
|
|
|
pub mode: PassMode,
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:35:26 +02:00
|
|
|
impl<'a, Ty> ArgAbi<'a, Ty> {
|
2020-12-29 20:00:57 +01:00
|
|
|
pub fn new(
|
|
|
|
cx: &impl HasDataLayout,
|
|
|
|
layout: TyAndLayout<'a, Ty>,
|
|
|
|
scalar_attrs: impl Fn(&TyAndLayout<'a, Ty>, &abi::Scalar, Size) -> ArgAttributes,
|
|
|
|
) -> Self {
|
|
|
|
let mode = match &layout.abi {
|
|
|
|
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),
|
|
|
|
scalar_attrs(&layout, b, a.value.size(cx).align_to(b.value.align(cx).abi)),
|
|
|
|
),
|
|
|
|
Abi::Vector { .. } => PassMode::Direct(ArgAttributes::new()),
|
2021-01-26 13:42:40 +01:00
|
|
|
Abi::Aggregate { .. } => PassMode::Direct(ArgAttributes::new()),
|
2020-12-29 20:00:57 +01:00
|
|
|
};
|
|
|
|
ArgAbi { layout, pad: None, 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
|
2019-12-22 17:42:04 -05:00
|
|
|
attrs.set(ArgAttribute::NoAlias).set(ArgAttribute::NoCapture).set(ArgAttribute::NonNull);
|
2020-12-29 20:00:57 +01:00
|
|
|
attrs.pointee_size = layout.size;
|
2018-04-25 16:45:29 +03:00
|
|
|
// FIXME(eddyb) We should be doing this, but at least on
|
|
|
|
// i686-pc-windows-msvc, it results in wrong stack offsets.
|
2020-12-29 20:00:57 +01:00
|
|
|
// attrs.pointee_align = Some(layout.align.abi);
|
2018-04-25 16:45:29 +03:00
|
|
|
|
2020-12-29 20:00:57 +01:00
|
|
|
let extra_attrs = layout.is_unsized().then_some(ArgAttributes::new());
|
2018-08-03 23:32:21 +09:00
|
|
|
|
2020-12-29 20:00:57 +01:00
|
|
|
PassMode::Indirect { attrs, extra_attrs, on_stack: false }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_indirect(&mut self) {
|
|
|
|
match self.mode {
|
|
|
|
PassMode::Direct(_) | PassMode::Pair(_, _) => {}
|
|
|
|
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: false } => return,
|
|
|
|
_ => panic!("Tried to make {:?} indirect", self.mode),
|
|
|
|
}
|
|
|
|
|
|
|
|
self.mode = Self::indirect_pass_mode(&self.layout);
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_indirect_byval(&mut self) {
|
|
|
|
self.make_indirect();
|
|
|
|
match self.mode {
|
2020-11-14 14:29:40 +01:00
|
|
|
PassMode::Indirect { attrs: _, extra_attrs: _, ref mut on_stack } => {
|
|
|
|
*on_stack = true;
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => unreachable!(),
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn extend_integer_width_to(&mut self, bits: u64) {
|
|
|
|
// Only integers have signedness
|
|
|
|
if let Abi::Scalar(ref scalar) = self.layout.abi {
|
|
|
|
if let abi::Int(i, signed) = scalar.value {
|
|
|
|
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) {
|
|
|
|
self.mode = PassMode::Cast(target.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pad_with(&mut self, reg: Reg) {
|
|
|
|
self.pad = Some(reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-11-14 14:29:40 +01:00
|
|
|
matches!(self.mode, PassMode::Indirect { attrs: _, extra_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 {
|
2020-11-14 14:29:40 +01:00
|
|
|
matches!(self.mode, PassMode::Indirect { attrs: _, extra_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
|
|
|
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,
|
|
|
|
|
|
|
|
// Target-specific calling conventions.
|
2018-04-25 16:45:29 +03:00
|
|
|
ArmAapcs,
|
2021-01-24 17:15:05 +00:00
|
|
|
CCmseNonSecureCall,
|
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
|
|
|
|
|
|
|
AmdGpuKernel,
|
2016-05-06 09:32:10 -04:00
|
|
|
AvrInterrupt,
|
|
|
|
AvrNonBlockingInterrupt,
|
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.
|
|
|
|
///
|
|
|
|
/// I will do my best to describe this structure, but these
|
|
|
|
/// comments are reverse-engineered and may be inaccurate. -NDM
|
|
|
|
#[derive(Debug)]
|
2019-10-29 16:35:26 +02:00
|
|
|
pub struct FnAbi<'a, Ty> {
|
2018-04-25 16:45:29 +03:00
|
|
|
/// The LLVM types of each argument.
|
2019-10-29 16:35:26 +02:00
|
|
|
pub args: Vec<ArgAbi<'a, Ty>>,
|
2018-04-25 16:45:29 +03:00
|
|
|
|
|
|
|
/// LLVM return type.
|
2019-10-29 16:35:26 +02:00
|
|
|
pub ret: ArgAbi<'a, Ty>,
|
2018-04-25 16:45:29 +03:00
|
|
|
|
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.
|
2020-01-21 21:52:19 +07:00
|
|
|
pub fixed_count: usize,
|
|
|
|
|
2018-04-25 16:45:29 +03:00
|
|
|
pub conv: Conv,
|
2020-03-28 21:47:50 -04:00
|
|
|
|
|
|
|
pub can_unwind: bool,
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
2019-10-29 16:35:26 +02:00
|
|
|
impl<'a, Ty> FnAbi<'a, Ty> {
|
2021-01-26 13:38:59 +01:00
|
|
|
pub fn adjust_for_cabi<C>(&mut self, cx: &C, abi: spec::abi::Abi) -> Result<(), String>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
2020-03-04 14:50:21 +00:00
|
|
|
Ty: TyAndLayoutMethods<'a, C> + Copy,
|
|
|
|
C: LayoutOf<Ty = Ty, TyAndLayout = TyAndLayout<'a, Ty>> + HasDataLayout + HasTargetSpec,
|
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() {
|
|
|
|
arg.make_indirect_byval();
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2018-04-25 16:45:29 +03:00
|
|
|
match &cx.target_spec().arch[..] {
|
|
|
|
"x86" => {
|
2019-02-08 21:00:07 +09:00
|
|
|
let flavor = if abi == spec::abi::Abi::Fastcall {
|
2018-04-25 16:45:29 +03:00
|
|
|
x86::Flavor::Fastcall
|
|
|
|
} else {
|
|
|
|
x86::Flavor::General
|
|
|
|
};
|
|
|
|
x86::compute_abi_info(cx, self, flavor);
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
|
|
|
"x86_64" => {
|
|
|
|
if abi == spec::abi::Abi::SysV64 {
|
|
|
|
x86_64::compute_abi_info(cx, self);
|
2020-11-08 14:27:51 +03:00
|
|
|
} else if abi == spec::abi::Abi::Win64 || cx.target_spec().is_like_windows {
|
2019-12-22 17:42:04 -05:00
|
|
|
x86_win64::compute_abi_info(self);
|
|
|
|
} else {
|
|
|
|
x86_64::compute_abi_info(cx, self);
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 16:45:29 +03:00
|
|
|
"aarch64" => aarch64::compute_abi_info(cx, self),
|
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),
|
2018-04-25 16:45:29 +03:00
|
|
|
"mips" => mips::compute_abi_info(cx, self),
|
|
|
|
"mips64" => mips64::compute_abi_info(cx, self),
|
2019-09-04 20:40:18 -05:00
|
|
|
"powerpc" => powerpc::compute_abi_info(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),
|
|
|
|
"nvptx" => nvptx::compute_abi_info(self),
|
|
|
|
"nvptx64" => nvptx64::compute_abi_info(self),
|
|
|
|
"hexagon" => hexagon::compute_abi_info(self),
|
2020-01-21 21:52:19 +07:00
|
|
|
"riscv32" | "riscv64" => riscv::compute_abi_info(cx, self),
|
2021-01-26 11:09:06 +01:00
|
|
|
"wasm32" => match cx.target_spec().os.as_str() {
|
|
|
|
"emscripten" | "wasi" => wasm32::compute_abi_info(cx, self),
|
2021-01-26 13:38:59 +01:00
|
|
|
_ => wasm32_bindgen_compat::compute_abi_info(self),
|
2021-01-11 15:31:52 -06:00
|
|
|
},
|
|
|
|
"asmjs" => wasm32::compute_abi_info(cx, self),
|
2019-12-22 17:42:04 -05:00
|
|
|
a => return Err(format!("unrecognized arch \"{}\" in target specification", a)),
|
2018-04-25 16:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|