2016-02-23 20:43:04 +00:00
|
|
|
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
|
2013-01-25 22:56:56 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-03-10 04:25:57 +00:00
|
|
|
use llvm::{self, ValueRef, AttributePlace};
|
2016-03-22 17:23:36 +00:00
|
|
|
use base;
|
2016-12-31 23:00:24 +00:00
|
|
|
use builder::Builder;
|
2017-08-05 09:27:28 +00:00
|
|
|
use common::{instance_ty, ty_fn_sig, type_is_fat_ptr, C_usize};
|
2016-03-22 17:23:36 +00:00
|
|
|
use context::CrateContext;
|
|
|
|
use cabi_x86;
|
|
|
|
use cabi_x86_64;
|
|
|
|
use cabi_x86_win64;
|
|
|
|
use cabi_arm;
|
|
|
|
use cabi_aarch64;
|
|
|
|
use cabi_powerpc;
|
|
|
|
use cabi_powerpc64;
|
2016-09-09 21:00:23 +00:00
|
|
|
use cabi_s390x;
|
2016-03-22 17:23:36 +00:00
|
|
|
use cabi_mips;
|
2016-08-26 22:06:13 +00:00
|
|
|
use cabi_mips64;
|
2016-03-22 17:23:36 +00:00
|
|
|
use cabi_asmjs;
|
2016-11-13 16:03:44 +00:00
|
|
|
use cabi_msp430;
|
2016-12-12 17:41:45 +00:00
|
|
|
use cabi_sparc;
|
2016-12-03 16:55:50 +00:00
|
|
|
use cabi_sparc64;
|
2016-12-22 21:24:29 +00:00
|
|
|
use cabi_nvptx;
|
|
|
|
use cabi_nvptx64;
|
2017-04-09 06:03:31 +00:00
|
|
|
use cabi_hexagon;
|
2017-06-25 09:41:24 +00:00
|
|
|
use mir::lvalue::LvalueRef;
|
2016-03-22 17:23:36 +00:00
|
|
|
use type_::Type;
|
2017-10-04 23:21:10 +00:00
|
|
|
use type_of::{LayoutLlvmExt, PointerKind};
|
2016-02-23 19:55:19 +00:00
|
|
|
|
2016-03-22 15:30:57 +00:00
|
|
|
use rustc::ty::{self, Ty};
|
2017-09-21 17:40:50 +00:00
|
|
|
use rustc::ty::layout::{self, Align, Size, TyLayout};
|
2017-09-12 21:33:56 +00:00
|
|
|
use rustc::ty::layout::{HasDataLayout, LayoutOf};
|
2016-02-23 19:55:19 +00:00
|
|
|
|
2016-03-06 14:30:21 +00:00
|
|
|
use libc::c_uint;
|
2017-09-14 19:50:18 +00:00
|
|
|
use std::{cmp, iter};
|
2016-03-06 14:30:21 +00:00
|
|
|
|
2016-02-23 20:43:04 +00:00
|
|
|
pub use syntax::abi::Abi;
|
2016-04-19 06:11:46 +00:00
|
|
|
pub use rustc::ty::layout::{FAT_PTR_ADDR, FAT_PTR_EXTRA};
|
2013-01-25 22:56:56 +00:00
|
|
|
|
2016-02-18 17:49:45 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
2016-02-25 17:35:40 +00:00
|
|
|
enum ArgKind {
|
2013-09-25 10:30:44 +00:00
|
|
|
/// Pass the argument directly using the normal converted
|
|
|
|
/// LLVM type or by coercing to another specified type
|
|
|
|
Direct,
|
|
|
|
/// Pass the argument indirectly via a hidden pointer
|
2014-03-09 06:42:22 +00:00
|
|
|
Indirect,
|
|
|
|
/// Ignore the argument (useful for empty struct)
|
|
|
|
Ignore,
|
2013-09-25 10:30:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 22:36:08 +00:00
|
|
|
// Hack to disable non_upper_case_globals only for the bitflags! and not for the rest
|
|
|
|
// of this module
|
|
|
|
pub use self::attr_impl::ArgAttribute;
|
|
|
|
|
|
|
|
#[allow(non_upper_case_globals)]
|
2017-03-13 23:08:21 +00:00
|
|
|
#[allow(unused)]
|
2016-11-16 22:36:08 +00:00
|
|
|
mod attr_impl {
|
|
|
|
// The subset of llvm::Attribute needed for arguments, packed into a bitfield.
|
|
|
|
bitflags! {
|
2017-09-08 19:08:01 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ArgAttribute: u16 {
|
|
|
|
const ByVal = 1 << 0;
|
|
|
|
const NoAlias = 1 << 1;
|
|
|
|
const NoCapture = 1 << 2;
|
|
|
|
const NonNull = 1 << 3;
|
|
|
|
const ReadOnly = 1 << 4;
|
|
|
|
const SExt = 1 << 5;
|
|
|
|
const StructRet = 1 << 6;
|
|
|
|
const ZExt = 1 << 7;
|
|
|
|
const InReg = 1 << 8;
|
2016-11-16 22:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! for_each_kind {
|
|
|
|
($flags: ident, $f: ident, $($kind: ident),+) => ({
|
|
|
|
$(if $flags.contains(ArgAttribute::$kind) { $f(llvm::Attribute::$kind) })+
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ArgAttribute {
|
|
|
|
fn for_each_kind<F>(&self, mut f: F) where F: FnMut(llvm::Attribute) {
|
|
|
|
for_each_kind!(self, f,
|
2016-12-21 18:42:10 +00:00
|
|
|
ByVal, NoAlias, NoCapture, NonNull, ReadOnly, SExt, StructRet, ZExt, InReg)
|
2016-11-16 22:36:08 +00: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.
|
2017-10-03 07:45:07 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2016-11-16 22:36:08 +00:00
|
|
|
pub struct ArgAttributes {
|
|
|
|
regular: ArgAttribute,
|
2017-10-03 07:45:07 +00:00
|
|
|
pointee_size: Size,
|
|
|
|
pointee_align: Option<Align>
|
2016-11-16 22:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ArgAttributes {
|
2017-10-03 07:45:07 +00:00
|
|
|
fn new() -> Self {
|
|
|
|
ArgAttributes {
|
|
|
|
regular: ArgAttribute::default(),
|
|
|
|
pointee_size: Size::from_bytes(0),
|
|
|
|
pointee_align: None,
|
|
|
|
}
|
2016-11-16 22:36:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 07:45:07 +00:00
|
|
|
pub fn set(&mut self, attr: ArgAttribute) -> &mut Self {
|
|
|
|
self.regular = self.regular | attr;
|
2016-11-16 22:36:08 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:09:09 +00:00
|
|
|
pub fn contains(&self, attr: ArgAttribute) -> bool {
|
|
|
|
self.regular.contains(attr)
|
|
|
|
}
|
|
|
|
|
2016-11-16 22:36:08 +00:00
|
|
|
pub fn apply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
|
2017-10-03 07:45:07 +00:00
|
|
|
let mut regular = self.regular;
|
2016-11-16 22:36:08 +00:00
|
|
|
unsafe {
|
2017-10-03 07:45:07 +00:00
|
|
|
let deref = self.pointee_size.bytes();
|
|
|
|
if deref != 0 {
|
|
|
|
if regular.contains(ArgAttribute::NonNull) {
|
|
|
|
llvm::LLVMRustAddDereferenceableAttr(llfn,
|
|
|
|
idx.as_uint(),
|
|
|
|
deref);
|
|
|
|
} else {
|
|
|
|
llvm::LLVMRustAddDereferenceableOrNullAttr(llfn,
|
|
|
|
idx.as_uint(),
|
|
|
|
deref);
|
|
|
|
}
|
|
|
|
regular -= ArgAttribute::NonNull;
|
2016-11-16 22:36:08 +00:00
|
|
|
}
|
2017-10-03 07:45:07 +00:00
|
|
|
if let Some(align) = self.pointee_align {
|
|
|
|
llvm::LLVMRustAddAlignmentAttr(llfn,
|
|
|
|
idx.as_uint(),
|
|
|
|
align.abi() as u32);
|
|
|
|
}
|
|
|
|
regular.for_each_kind(|attr| attr.apply_llfn(idx, llfn));
|
2016-11-16 22:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn apply_callsite(&self, idx: AttributePlace, callsite: ValueRef) {
|
2017-10-03 07:45:07 +00:00
|
|
|
let mut regular = self.regular;
|
2016-11-16 22:36:08 +00:00
|
|
|
unsafe {
|
2017-10-03 07:45:07 +00:00
|
|
|
let deref = self.pointee_size.bytes();
|
|
|
|
if deref != 0 {
|
|
|
|
if regular.contains(ArgAttribute::NonNull) {
|
|
|
|
llvm::LLVMRustAddDereferenceableCallSiteAttr(callsite,
|
|
|
|
idx.as_uint(),
|
|
|
|
deref);
|
|
|
|
} else {
|
|
|
|
llvm::LLVMRustAddDereferenceableOrNullCallSiteAttr(callsite,
|
|
|
|
idx.as_uint(),
|
|
|
|
deref);
|
|
|
|
}
|
|
|
|
regular -= ArgAttribute::NonNull;
|
|
|
|
}
|
|
|
|
if let Some(align) = self.pointee_align {
|
|
|
|
llvm::LLVMRustAddAlignmentCallSiteAttr(callsite,
|
|
|
|
idx.as_uint(),
|
|
|
|
align.abi() as u32);
|
2016-11-16 22:36:08 +00:00
|
|
|
}
|
2017-10-03 07:45:07 +00:00
|
|
|
regular.for_each_kind(|attr| attr.apply_callsite(idx, callsite));
|
2016-11-16 22:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 04:25:57 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum RegKind {
|
|
|
|
Integer,
|
|
|
|
Float,
|
|
|
|
Vector
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
|
|
|
Reg {
|
|
|
|
kind: RegKind::$kind,
|
|
|
|
size: Size::from_bits($bits)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reg {
|
|
|
|
reg_ctor!(i8, Integer, 8);
|
|
|
|
reg_ctor!(i16, Integer, 16);
|
|
|
|
reg_ctor!(i32, Integer, 32);
|
|
|
|
reg_ctor!(i64, Integer, 64);
|
|
|
|
|
|
|
|
reg_ctor!(f32, Float, 32);
|
|
|
|
reg_ctor!(f64, Float, 64);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reg {
|
2017-06-01 18:50:53 +00:00
|
|
|
pub fn align(&self, ccx: &CrateContext) -> Align {
|
|
|
|
let dl = ccx.data_layout();
|
|
|
|
match self.kind {
|
|
|
|
RegKind::Integer => {
|
|
|
|
match self.size.bits() {
|
|
|
|
1 => dl.i1_align,
|
|
|
|
2...8 => dl.i8_align,
|
|
|
|
9...16 => dl.i16_align,
|
|
|
|
17...32 => dl.i32_align,
|
|
|
|
33...64 => dl.i64_align,
|
|
|
|
65...128 => dl.i128_align,
|
|
|
|
_ => bug!("unsupported integer: {:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RegKind::Float => {
|
|
|
|
match self.size.bits() {
|
|
|
|
32 => dl.f32_align,
|
|
|
|
64 => dl.f64_align,
|
|
|
|
_ => bug!("unsupported float: {:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RegKind::Vector => dl.vector_align(self.size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn llvm_type(&self, ccx: &CrateContext) -> Type {
|
2017-03-10 04:25:57 +00:00
|
|
|
match self.kind {
|
|
|
|
RegKind::Integer => Type::ix(ccx, self.size.bits()),
|
|
|
|
RegKind::Float => {
|
|
|
|
match self.size.bits() {
|
|
|
|
32 => Type::f32(ccx),
|
|
|
|
64 => Type::f64(ccx),
|
|
|
|
_ => bug!("unsupported float: {:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RegKind::Vector => {
|
|
|
|
Type::vector(&Type::i8(ccx), self.size.bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An argument passed entirely registers with the
|
|
|
|
/// same kind (e.g. HFA / HVA on PPC64 and AArch64).
|
2017-06-01 18:50:53 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2017-03-10 04:25:57 +00:00
|
|
|
pub struct Uniform {
|
|
|
|
pub unit: Reg,
|
|
|
|
|
|
|
|
/// The total size of the argument, which can be:
|
|
|
|
/// * equal to `unit.size` (one scalar/vector)
|
|
|
|
/// * a multiple of `unit.size` (an array of scalar/vectors)
|
|
|
|
/// * 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
|
|
|
|
pub total: Size,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Reg> for Uniform {
|
|
|
|
fn from(unit: Reg) -> Uniform {
|
|
|
|
Uniform {
|
|
|
|
unit,
|
|
|
|
total: unit.size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Uniform {
|
2017-06-01 18:50:53 +00:00
|
|
|
pub fn align(&self, ccx: &CrateContext) -> Align {
|
|
|
|
self.unit.align(ccx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn llvm_type(&self, ccx: &CrateContext) -> Type {
|
2017-03-10 04:25:57 +00:00
|
|
|
let llunit = self.unit.llvm_type(ccx);
|
|
|
|
|
|
|
|
if self.total <= self.unit.size {
|
|
|
|
return llunit;
|
|
|
|
}
|
|
|
|
|
|
|
|
let count = self.total.bytes() / self.unit.size.bytes();
|
|
|
|
let rem_bytes = self.total.bytes() % self.unit.size.bytes();
|
|
|
|
|
|
|
|
if rem_bytes == 0 {
|
|
|
|
return Type::array(&llunit, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only integers can be really split further.
|
|
|
|
assert_eq!(self.unit.kind, RegKind::Integer);
|
|
|
|
|
|
|
|
let args: Vec<_> = (0..count).map(|_| llunit)
|
|
|
|
.chain(iter::once(Type::ix(ccx, rem_bytes * 8)))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Type::struct_(ccx, &args, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait LayoutExt<'tcx> {
|
|
|
|
fn is_aggregate(&self) -> bool;
|
2017-07-22 01:08:40 +00:00
|
|
|
fn homogeneous_aggregate<'a>(&self, ccx: &CrateContext<'a, 'tcx>) -> Option<Reg>;
|
2017-03-10 04:25:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 17:40:50 +00:00
|
|
|
impl<'tcx> LayoutExt<'tcx> for TyLayout<'tcx> {
|
2017-03-10 04:25:57 +00:00
|
|
|
fn is_aggregate(&self) -> bool {
|
2017-09-14 19:50:18 +00:00
|
|
|
match self.abi {
|
2017-09-26 18:34:10 +00:00
|
|
|
layout::Abi::Uninhabited |
|
2017-09-14 19:50:18 +00:00
|
|
|
layout::Abi::Scalar(_) |
|
2017-09-26 04:27:48 +00:00
|
|
|
layout::Abi::Vector => false,
|
2017-10-06 07:25:35 +00:00
|
|
|
layout::Abi::ScalarPair(..) |
|
2017-09-17 01:42:22 +00:00
|
|
|
layout::Abi::Aggregate { .. } => true
|
2017-03-10 04:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 01:08:40 +00:00
|
|
|
fn homogeneous_aggregate<'a>(&self, ccx: &CrateContext<'a, 'tcx>) -> Option<Reg> {
|
2017-09-14 19:50:18 +00:00
|
|
|
match self.abi {
|
2017-09-26 18:34:10 +00:00
|
|
|
layout::Abi::Uninhabited => None,
|
|
|
|
|
2017-09-14 19:50:18 +00:00
|
|
|
// The primitive for this algorithm.
|
2017-09-26 11:41:06 +00:00
|
|
|
layout::Abi::Scalar(ref scalar) => {
|
|
|
|
let kind = match scalar.value {
|
2017-09-16 13:39:53 +00:00
|
|
|
layout::Int(..) |
|
2017-03-10 04:25:57 +00:00
|
|
|
layout::Pointer => RegKind::Integer,
|
|
|
|
layout::F32 |
|
|
|
|
layout::F64 => RegKind::Float
|
|
|
|
};
|
|
|
|
Some(Reg {
|
|
|
|
kind,
|
2017-09-22 19:44:40 +00:00
|
|
|
size: self.size
|
2017-03-10 04:25:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-26 04:27:48 +00:00
|
|
|
layout::Abi::Vector => {
|
2017-03-10 04:25:57 +00:00
|
|
|
Some(Reg {
|
2017-04-10 22:04:33 +00:00
|
|
|
kind: RegKind::Vector,
|
2017-09-22 19:44:40 +00:00
|
|
|
size: self.size
|
2017-03-10 04:25:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-06 07:25:35 +00:00
|
|
|
layout::Abi::ScalarPair(..) |
|
2017-09-17 01:42:22 +00:00
|
|
|
layout::Abi::Aggregate { .. } => {
|
2017-09-14 19:50:18 +00:00
|
|
|
let mut total = Size::from_bytes(0);
|
2017-03-10 04:25:57 +00:00
|
|
|
let mut result = None;
|
|
|
|
|
2017-09-21 17:40:50 +00:00
|
|
|
let is_union = match self.fields {
|
2017-09-19 09:52:30 +00:00
|
|
|
layout::FieldPlacement::Array { count, .. } => {
|
|
|
|
if count > 0 {
|
|
|
|
return self.field(ccx, 0).homogeneous_aggregate(ccx);
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
2017-03-10 04:25:57 +00:00
|
|
|
}
|
2017-09-19 09:52:30 +00:00
|
|
|
layout::FieldPlacement::Union(_) => true,
|
2017-09-14 19:50:18 +00:00
|
|
|
layout::FieldPlacement::Arbitrary { .. } => false
|
|
|
|
};
|
2017-03-10 04:25:57 +00:00
|
|
|
|
2017-09-14 19:50:18 +00:00
|
|
|
for i in 0..self.fields.count() {
|
|
|
|
if !is_union && total != self.fields.offset(i) {
|
|
|
|
return None;
|
2017-03-10 04:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let field = self.field(ccx, i);
|
2017-07-22 01:08:40 +00:00
|
|
|
match (result, field.homogeneous_aggregate(ccx)) {
|
|
|
|
// The field itself must be a homogeneous aggregate.
|
2017-03-10 04:25:57 +00:00
|
|
|
(_, None) => return None,
|
|
|
|
// If this is the first field, record the unit.
|
|
|
|
(None, Some(unit)) => {
|
|
|
|
result = Some(unit);
|
|
|
|
}
|
|
|
|
// For all following fields, the unit must be the same.
|
|
|
|
(Some(prev_unit), Some(unit)) => {
|
|
|
|
if prev_unit != unit {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of the offset (without padding).
|
2017-09-22 19:44:40 +00:00
|
|
|
let size = field.size;
|
2017-09-14 19:50:18 +00:00
|
|
|
if is_union {
|
|
|
|
total = cmp::max(total, size);
|
|
|
|
} else {
|
|
|
|
total += size;
|
2017-03-10 04:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There needs to be no padding.
|
2017-09-22 19:44:40 +00:00
|
|
|
if total != self.size {
|
2017-03-10 04:25:57 +00:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 18:50:53 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2017-03-10 04:25:57 +00:00
|
|
|
pub enum CastTarget {
|
|
|
|
Uniform(Uniform),
|
|
|
|
Pair(Reg, Reg)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Reg> for CastTarget {
|
|
|
|
fn from(unit: Reg) -> CastTarget {
|
|
|
|
CastTarget::Uniform(Uniform::from(unit))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Uniform> for CastTarget {
|
|
|
|
fn from(uniform: Uniform) -> CastTarget {
|
|
|
|
CastTarget::Uniform(uniform)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CastTarget {
|
2017-06-01 18:50:53 +00:00
|
|
|
pub fn size(&self, ccx: &CrateContext) -> Size {
|
|
|
|
match *self {
|
|
|
|
CastTarget::Uniform(u) => u.total,
|
|
|
|
CastTarget::Pair(a, b) => {
|
|
|
|
(a.size.abi_align(a.align(ccx)) + b.size)
|
|
|
|
.abi_align(self.align(ccx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn align(&self, ccx: &CrateContext) -> Align {
|
|
|
|
match *self {
|
|
|
|
CastTarget::Uniform(u) => u.align(ccx),
|
|
|
|
CastTarget::Pair(a, b) => {
|
|
|
|
ccx.data_layout().aggregate_align
|
|
|
|
.max(a.align(ccx))
|
|
|
|
.max(b.align(ccx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn llvm_type(&self, ccx: &CrateContext) -> Type {
|
2017-03-10 04:25:57 +00:00
|
|
|
match *self {
|
|
|
|
CastTarget::Uniform(u) => u.llvm_type(ccx),
|
|
|
|
CastTarget::Pair(a, b) => {
|
|
|
|
Type::struct_(ccx, &[
|
|
|
|
a.llvm_type(ccx),
|
|
|
|
b.llvm_type(ccx)
|
|
|
|
], false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 22:36:08 +00:00
|
|
|
|
2013-09-25 10:30:44 +00:00
|
|
|
/// Information about how a specific C type
|
|
|
|
/// should be passed to or returned from a function
|
|
|
|
///
|
|
|
|
/// This is borrowed from clang's ABIInfo.h
|
2017-09-20 02:16:06 +00:00
|
|
|
#[derive(Debug)]
|
2017-03-10 04:25:57 +00:00
|
|
|
pub struct ArgType<'tcx> {
|
2016-02-25 17:35:40 +00:00
|
|
|
kind: ArgKind,
|
2017-09-21 17:40:50 +00:00
|
|
|
pub layout: TyLayout<'tcx>,
|
2017-06-01 18:50:53 +00:00
|
|
|
/// Cast target, either a single uniform or a pair of registers.
|
|
|
|
pub cast: Option<CastTarget>,
|
|
|
|
/// Dummy argument, which is emitted before the real argument.
|
|
|
|
pub pad: Option<Reg>,
|
|
|
|
/// Attributes of argument.
|
2017-09-20 02:16:06 +00:00
|
|
|
pub attrs: ArgAttributes,
|
|
|
|
pub nested: Vec<ArgType<'tcx>>
|
2013-09-25 10:30:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 04:25:57 +00:00
|
|
|
impl<'a, 'tcx> ArgType<'tcx> {
|
2017-09-21 17:40:50 +00:00
|
|
|
fn new(layout: TyLayout<'tcx>) -> ArgType<'tcx> {
|
2017-10-03 07:45:07 +00:00
|
|
|
let mut attrs = ArgAttributes::new();
|
|
|
|
|
|
|
|
if let layout::Abi::Scalar(ref scalar) = layout.abi {
|
|
|
|
if scalar.is_bool() {
|
|
|
|
attrs.set(ArgAttribute::ZExt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 10:30:44 +00:00
|
|
|
ArgType {
|
2016-02-25 17:35:40 +00:00
|
|
|
kind: ArgKind::Direct,
|
2017-08-07 05:54:09 +00:00
|
|
|
layout,
|
2014-03-09 06:42:22 +00:00
|
|
|
cast: None,
|
|
|
|
pad: None,
|
2017-10-03 07:45:07 +00:00
|
|
|
attrs,
|
2017-09-20 02:16:06 +00:00
|
|
|
nested: vec![]
|
2014-03-09 06:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:44:40 +00:00
|
|
|
pub fn make_indirect(&mut self) {
|
2017-09-20 02:16:06 +00:00
|
|
|
assert!(self.nested.is_empty());
|
2016-03-06 10:36:39 +00:00
|
|
|
assert_eq!(self.kind, ArgKind::Direct);
|
|
|
|
|
2016-02-25 17:35:40 +00:00
|
|
|
// Wipe old attributes, likely not valid through indirection.
|
2017-10-03 07:45:07 +00:00
|
|
|
self.attrs = ArgAttributes::new();
|
2016-02-25 17:35:40 +00:00
|
|
|
|
|
|
|
// 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
|
2016-11-16 22:36:08 +00:00
|
|
|
self.attrs.set(ArgAttribute::NoAlias)
|
|
|
|
.set(ArgAttribute::NoCapture)
|
2017-10-03 07:45:07 +00:00
|
|
|
.set(ArgAttribute::NonNull);
|
|
|
|
self.attrs.pointee_size = self.layout.size;
|
|
|
|
self.attrs.pointee_align = Some(self.layout.align);
|
2016-02-25 17:35:40 +00:00
|
|
|
|
|
|
|
self.kind = ArgKind::Indirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ignore(&mut self) {
|
2017-09-20 02:16:06 +00:00
|
|
|
assert!(self.nested.is_empty());
|
2016-03-06 10:36:39 +00:00
|
|
|
assert_eq!(self.kind, ArgKind::Direct);
|
2016-02-25 17:35:40 +00:00
|
|
|
self.kind = ArgKind::Ignore;
|
|
|
|
}
|
|
|
|
|
2016-02-17 06:33:27 +00:00
|
|
|
pub fn extend_integer_width_to(&mut self, bits: u64) {
|
|
|
|
// Only integers have signedness
|
2017-09-26 11:41:06 +00:00
|
|
|
if let layout::Abi::Scalar(ref scalar) = self.layout.abi {
|
|
|
|
if let layout::Int(i, signed) = scalar.value {
|
2017-09-16 13:39:53 +00:00
|
|
|
if i.size().bits() < bits {
|
|
|
|
self.attrs.set(if signed {
|
|
|
|
ArgAttribute::SExt
|
|
|
|
} else {
|
|
|
|
ArgAttribute::ZExt
|
|
|
|
});
|
|
|
|
}
|
2016-02-17 06:33:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 18:50:53 +00:00
|
|
|
pub fn cast_to<T: Into<CastTarget>>(&mut self, target: T) {
|
2017-09-20 02:16:06 +00:00
|
|
|
assert!(self.nested.is_empty());
|
2017-06-01 18:50:53 +00:00
|
|
|
self.cast = Some(target.into());
|
2017-03-10 04:25:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-01 18:50:53 +00:00
|
|
|
pub fn pad_with(&mut self, reg: Reg) {
|
2017-09-20 02:16:06 +00:00
|
|
|
assert!(self.nested.is_empty());
|
2017-06-01 18:50:53 +00:00
|
|
|
self.pad = Some(reg);
|
2017-03-10 04:25:57 +00:00
|
|
|
}
|
|
|
|
|
2013-09-25 10:30:44 +00:00
|
|
|
pub fn is_indirect(&self) -> bool {
|
2016-02-25 17:35:40 +00:00
|
|
|
self.kind == ArgKind::Indirect
|
2013-09-25 10:30:44 +00:00
|
|
|
}
|
2014-03-09 06:42:22 +00:00
|
|
|
|
|
|
|
pub fn is_ignore(&self) -> bool {
|
2016-02-25 17:35:40 +00:00
|
|
|
self.kind == ArgKind::Ignore
|
2014-03-09 06:42:22 +00:00
|
|
|
}
|
2016-03-06 11:23:20 +00:00
|
|
|
|
2017-03-10 04:25:57 +00:00
|
|
|
/// Get the LLVM type for an lvalue of the original Rust type of
|
|
|
|
/// this argument/return, i.e. the result of `type_of::type_of`.
|
|
|
|
pub fn memory_ty(&self, ccx: &CrateContext<'a, 'tcx>) -> Type {
|
2017-09-20 20:07:47 +00:00
|
|
|
self.layout.llvm_type(ccx)
|
2017-03-10 04:25:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 11:23:20 +00:00
|
|
|
/// Store a direct/indirect value described by this ArgType into a
|
|
|
|
/// lvalue for the original Rust type of this argument/return.
|
|
|
|
/// Can be used for both storing formal arguments into Rust variables
|
|
|
|
/// or results of call/invoke instructions into their destinations.
|
2017-06-25 09:41:24 +00:00
|
|
|
pub fn store(&self, bcx: &Builder<'a, 'tcx>, mut val: ValueRef, dst: LvalueRef<'tcx>) {
|
2016-03-06 11:23:20 +00:00
|
|
|
if self.is_ignore() {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-19 23:25:00 +00:00
|
|
|
let ccx = bcx.ccx;
|
2016-03-06 11:23:20 +00:00
|
|
|
if self.is_indirect() {
|
2017-09-22 19:44:40 +00:00
|
|
|
let llsz = C_usize(ccx, self.layout.size.bytes());
|
|
|
|
base::call_memcpy(bcx, dst.llval, val, llsz, self.layout.align);
|
2016-03-06 11:23:20 +00:00
|
|
|
} else if let Some(ty) = self.cast {
|
2016-06-07 21:35:01 +00:00
|
|
|
// FIXME(eddyb): Figure out when the simpler Store is safe, clang
|
|
|
|
// uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
|
|
|
|
let can_store_through_cast_ptr = false;
|
|
|
|
if can_store_through_cast_ptr {
|
2017-06-25 09:41:24 +00:00
|
|
|
let cast_dst = bcx.pointercast(dst.llval, ty.llvm_type(ccx).ptr_to());
|
2017-09-22 19:44:40 +00:00
|
|
|
bcx.store(val, cast_dst, Some(self.layout.align));
|
2016-06-07 21:35:01 +00:00
|
|
|
} else {
|
|
|
|
// The actual return type is a struct, but the ABI
|
|
|
|
// adaptation code has cast it into some scalar type. The
|
|
|
|
// code that follows is the only reliable way I have
|
|
|
|
// found to do a transform like i64 -> {i32,i32}.
|
|
|
|
// Basically we dump the data onto the stack then memcpy it.
|
|
|
|
//
|
|
|
|
// Other approaches I tried:
|
|
|
|
// - Casting rust ret pointer to the foreign type and using Store
|
|
|
|
// is (a) unsafe if size of foreign type > size of rust type and
|
|
|
|
// (b) runs afoul of strict aliasing rules, yielding invalid
|
|
|
|
// assembly under -O (specifically, the store gets removed).
|
|
|
|
// - Truncating foreign type to correct integral type and then
|
|
|
|
// bitcasting to the struct type yields invalid cast errors.
|
|
|
|
|
|
|
|
// We instead thus allocate some scratch space...
|
2017-06-01 18:50:53 +00:00
|
|
|
let llscratch = bcx.alloca(ty.llvm_type(ccx), "abi_cast", None);
|
|
|
|
let scratch_size = ty.size(ccx);
|
|
|
|
bcx.lifetime_start(llscratch, scratch_size);
|
2016-06-07 21:35:01 +00:00
|
|
|
|
|
|
|
// ...where we first store the value...
|
2016-12-29 01:20:26 +00:00
|
|
|
bcx.store(val, llscratch, None);
|
2016-06-07 21:35:01 +00:00
|
|
|
|
|
|
|
// ...and then memcpy it to the intended destination.
|
|
|
|
base::call_memcpy(bcx,
|
2017-06-25 09:41:24 +00:00
|
|
|
bcx.pointercast(dst.llval, Type::i8p(ccx)),
|
2016-06-07 21:35:01 +00:00
|
|
|
bcx.pointercast(llscratch, Type::i8p(ccx)),
|
2017-09-22 19:44:40 +00:00
|
|
|
C_usize(ccx, self.layout.size.bytes()),
|
|
|
|
self.layout.align.min(ty.align(ccx)));
|
2016-06-07 21:35:01 +00:00
|
|
|
|
2017-06-01 18:50:53 +00:00
|
|
|
bcx.lifetime_end(llscratch, scratch_size);
|
2016-03-06 11:23:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-25 09:41:24 +00:00
|
|
|
val = base::from_immediate(bcx, val);
|
|
|
|
bcx.store(val, dst.llval, None);
|
2016-03-06 11:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-06 14:30:21 +00:00
|
|
|
|
2017-06-25 09:41:24 +00:00
|
|
|
pub fn store_fn_arg(&self, bcx: &Builder<'a, 'tcx>, idx: &mut usize, dst: LvalueRef<'tcx>) {
|
2017-09-20 02:16:06 +00:00
|
|
|
if !self.nested.is_empty() {
|
|
|
|
for (i, arg) in self.nested.iter().enumerate() {
|
|
|
|
arg.store_fn_arg(bcx, idx, dst.project_field(bcx, i));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2016-03-06 14:30:21 +00:00
|
|
|
if self.pad.is_some() {
|
|
|
|
*idx += 1;
|
|
|
|
}
|
|
|
|
if self.is_ignore() {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-31 23:00:24 +00:00
|
|
|
let val = llvm::get_param(bcx.llfn(), *idx as c_uint);
|
2016-03-06 14:30:21 +00:00
|
|
|
*idx += 1;
|
2016-03-09 12:20:22 +00:00
|
|
|
self.store(bcx, val, dst);
|
2016-03-06 14:30:21 +00:00
|
|
|
}
|
2013-01-25 22:56:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 19:25:44 +00: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
|
2017-09-20 02:16:06 +00:00
|
|
|
#[derive(Debug)]
|
2017-03-10 04:25:57 +00:00
|
|
|
pub struct FnType<'tcx> {
|
2013-09-25 10:30:44 +00:00
|
|
|
/// The LLVM types of each argument.
|
2017-03-10 04:25:57 +00:00
|
|
|
pub args: Vec<ArgType<'tcx>>,
|
2013-01-25 22:56:56 +00:00
|
|
|
|
2013-05-21 19:25:44 +00:00
|
|
|
/// LLVM return type.
|
2017-03-10 04:25:57 +00:00
|
|
|
pub ret: ArgType<'tcx>,
|
2016-02-23 19:55:19 +00:00
|
|
|
|
|
|
|
pub variadic: bool,
|
|
|
|
|
|
|
|
pub cconv: llvm::CallConv
|
2013-05-21 19:25:44 +00:00
|
|
|
}
|
2013-04-18 22:53:29 +00:00
|
|
|
|
2017-03-10 04:25:57 +00:00
|
|
|
impl<'a, 'tcx> FnType<'tcx> {
|
2017-05-10 23:01:25 +00:00
|
|
|
pub fn of_instance(ccx: &CrateContext<'a, 'tcx>, instance: &ty::Instance<'tcx>)
|
|
|
|
-> Self {
|
2017-09-12 15:28:17 +00:00
|
|
|
let fn_ty = instance_ty(ccx.tcx(), &instance);
|
2017-05-10 23:01:25 +00:00
|
|
|
let sig = ty_fn_sig(ccx, fn_ty);
|
|
|
|
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&sig);
|
2017-09-21 17:40:50 +00:00
|
|
|
FnType::new(ccx, sig, &[])
|
2017-05-10 23:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 04:25:57 +00:00
|
|
|
pub fn new(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
sig: ty::FnSig<'tcx>,
|
|
|
|
extra_args: &[Ty<'tcx>]) -> FnType<'tcx> {
|
2017-02-13 08:51:06 +00:00
|
|
|
let mut fn_ty = FnType::unadjusted(ccx, sig, extra_args);
|
|
|
|
fn_ty.adjust_for_abi(ccx, sig);
|
2016-03-06 10:38:46 +00:00
|
|
|
fn_ty
|
|
|
|
}
|
|
|
|
|
2017-03-10 04:25:57 +00:00
|
|
|
pub fn new_vtable(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
sig: ty::FnSig<'tcx>,
|
|
|
|
extra_args: &[Ty<'tcx>]) -> FnType<'tcx> {
|
2017-03-08 16:33:21 +00:00
|
|
|
let mut fn_ty = FnType::unadjusted(ccx, sig, extra_args);
|
|
|
|
// Don't pass the vtable, it's not an argument of the virtual fn.
|
2017-09-20 02:16:06 +00:00
|
|
|
assert_eq!(fn_ty.args[0].nested.len(), 2);
|
|
|
|
fn_ty.args[0].nested[1].ignore();
|
2017-03-08 16:33:21 +00:00
|
|
|
fn_ty.adjust_for_abi(ccx, sig);
|
|
|
|
fn_ty
|
|
|
|
}
|
|
|
|
|
2017-03-10 04:25:57 +00:00
|
|
|
pub fn unadjusted(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
sig: ty::FnSig<'tcx>,
|
|
|
|
extra_args: &[Ty<'tcx>]) -> FnType<'tcx> {
|
2017-05-10 23:01:25 +00:00
|
|
|
debug!("FnType::unadjusted({:?}, {:?})", sig, extra_args);
|
|
|
|
|
2016-02-23 20:43:04 +00:00
|
|
|
use self::Abi::*;
|
2017-02-13 08:51:06 +00:00
|
|
|
let cconv = match ccx.sess().target.target.adjust_abi(sig.abi) {
|
2016-03-06 10:38:46 +00:00
|
|
|
RustIntrinsic | PlatformIntrinsic |
|
2016-02-24 17:37:22 +00:00
|
|
|
Rust | RustCall => llvm::CCallConv,
|
2016-02-23 19:55:19 +00:00
|
|
|
|
|
|
|
// It's the ABI's job to select this, not us.
|
2016-03-28 23:46:02 +00:00
|
|
|
System => bug!("system abi should be selected elsewhere"),
|
2016-02-23 19:55:19 +00:00
|
|
|
|
|
|
|
Stdcall => llvm::X86StdcallCallConv,
|
|
|
|
Fastcall => llvm::X86FastcallCallConv,
|
|
|
|
Vectorcall => llvm::X86_VectorCall,
|
2017-05-17 13:40:46 +00:00
|
|
|
Thiscall => llvm::X86_ThisCall,
|
2016-02-23 19:55:19 +00:00
|
|
|
C => llvm::CCallConv,
|
2016-12-23 08:05:41 +00:00
|
|
|
Unadjusted => llvm::CCallConv,
|
2016-02-23 19:55:19 +00:00
|
|
|
Win64 => llvm::X86_64_Win64,
|
2016-06-27 00:34:02 +00:00
|
|
|
SysV64 => llvm::X86_64_SysV,
|
2016-11-16 22:33:23 +00:00
|
|
|
Aapcs => llvm::ArmAapcsCallConv,
|
2016-12-22 21:24:29 +00:00
|
|
|
PtxKernel => llvm::PtxKernel,
|
2016-12-19 04:45:20 +00:00
|
|
|
Msp430Interrupt => llvm::Msp430Intr,
|
2017-02-14 20:39:42 +00:00
|
|
|
X86Interrupt => llvm::X86_Intr,
|
2016-02-23 19:55:19 +00:00
|
|
|
|
|
|
|
// These API constants ought to be more specific...
|
|
|
|
Cdecl => llvm::CCallConv,
|
|
|
|
};
|
|
|
|
|
2016-11-29 02:35:38 +00:00
|
|
|
let mut inputs = sig.inputs();
|
2017-02-13 08:51:06 +00:00
|
|
|
let extra_args = if sig.abi == RustCall {
|
2016-11-29 03:25:33 +00:00
|
|
|
assert!(!sig.variadic && extra_args.is_empty());
|
2016-02-24 17:37:22 +00:00
|
|
|
|
2016-11-29 02:35:38 +00:00
|
|
|
match sig.inputs().last().unwrap().sty {
|
2017-01-11 07:58:37 +00:00
|
|
|
ty::TyTuple(ref tupled_arguments, _) => {
|
2016-11-29 02:35:38 +00:00
|
|
|
inputs = &sig.inputs()[0..sig.inputs().len() - 1];
|
2017-03-30 03:52:45 +00:00
|
|
|
tupled_arguments
|
2016-02-24 17:37:22 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2016-03-28 23:46:02 +00:00
|
|
|
bug!("argument to function with \"rust-call\" ABI \
|
|
|
|
is not a tuple");
|
2016-02-24 17:37:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-29 03:25:33 +00:00
|
|
|
assert!(sig.variadic || extra_args.is_empty());
|
2016-02-24 17:37:22 +00:00
|
|
|
extra_args
|
|
|
|
};
|
|
|
|
|
2016-03-18 12:28:11 +00:00
|
|
|
let target = &ccx.sess().target.target;
|
|
|
|
let win_x64_gnu = target.target_os == "windows"
|
|
|
|
&& target.arch == "x86_64"
|
|
|
|
&& target.target_env == "gnu";
|
2016-09-09 21:00:23 +00:00
|
|
|
let linux_s390x = target.target_os == "linux"
|
|
|
|
&& target.arch == "s390x"
|
|
|
|
&& target.target_env == "gnu";
|
2017-02-13 08:51:06 +00:00
|
|
|
let rust_abi = match sig.abi {
|
2016-03-18 12:28:11 +00:00
|
|
|
RustIntrinsic | PlatformIntrinsic | Rust | RustCall => true,
|
|
|
|
_ => false
|
|
|
|
};
|
|
|
|
|
2017-10-03 07:45:07 +00:00
|
|
|
// Handle safe Rust thin and fat pointers.
|
|
|
|
let adjust_for_rust_type = |arg: &mut ArgType<'tcx>, is_return: bool| {
|
|
|
|
match arg.layout.abi {
|
2017-10-04 23:21:10 +00:00
|
|
|
layout::Abi::Scalar(layout::Scalar {
|
|
|
|
value: layout::Pointer,
|
|
|
|
ref valid_range
|
|
|
|
}) => {
|
|
|
|
if valid_range.start > 0 && valid_range.start < valid_range.end {
|
|
|
|
arg.attrs.set(ArgAttribute::NonNull);
|
2017-10-03 07:45:07 +00:00
|
|
|
}
|
2017-01-21 14:40:31 +00:00
|
|
|
}
|
2017-10-06 07:25:35 +00:00
|
|
|
_ => {
|
|
|
|
// Nothing to do for non-pointer types.
|
|
|
|
return;
|
|
|
|
}
|
2016-02-25 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 07:25:35 +00:00
|
|
|
if let Some(pointee) = arg.layout.pointee_info_at(ccx, Size::from_bytes(0)) {
|
2017-10-04 23:21:10 +00:00
|
|
|
if let Some(kind) = pointee.safe {
|
|
|
|
arg.attrs.pointee_size = pointee.size;
|
|
|
|
arg.attrs.pointee_align = Some(pointee.align);
|
2016-02-25 23:10:40 +00:00
|
|
|
|
2017-10-04 23:21:10 +00:00
|
|
|
// HACK(eddyb) LLVM inserts `llvm.assume` calls when inlining functions
|
|
|
|
// with align attributes, and those calls later block optimizations.
|
|
|
|
if !is_return {
|
|
|
|
arg.attrs.pointee_align = None;
|
|
|
|
}
|
2016-02-25 23:10:40 +00:00
|
|
|
|
2017-10-04 23:21:10 +00:00
|
|
|
// `Box` pointer parameters never alias because ownership is transferred
|
2017-10-03 07:45:07 +00:00
|
|
|
// `&mut` pointer parameters never alias other parameters,
|
|
|
|
// or mutable global data
|
|
|
|
//
|
|
|
|
// `&T` where `T` contains no `UnsafeCell<U>` is immutable,
|
|
|
|
// and can be marked as both `readonly` and `noalias`, as
|
|
|
|
// LLVM's definition of `noalias` is based solely on memory
|
|
|
|
// dependencies rather than pointer equality
|
2017-10-04 23:21:10 +00:00
|
|
|
let no_alias = match kind {
|
|
|
|
PointerKind::Shared => false,
|
|
|
|
PointerKind::Frozen | PointerKind::UniqueOwned => true,
|
|
|
|
PointerKind::UniqueBorrowed => !is_return
|
|
|
|
};
|
|
|
|
if no_alias {
|
2017-10-03 07:45:07 +00:00
|
|
|
arg.attrs.set(ArgAttribute::NoAlias);
|
|
|
|
}
|
2016-02-25 23:10:40 +00:00
|
|
|
|
2017-10-04 23:21:10 +00:00
|
|
|
if kind == PointerKind::Frozen && !is_return {
|
2017-10-03 07:45:07 +00:00
|
|
|
arg.attrs.set(ArgAttribute::ReadOnly);
|
|
|
|
}
|
2016-02-25 23:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-03 07:45:07 +00:00
|
|
|
let arg_of = |ty: Ty<'tcx>, is_return: bool| {
|
|
|
|
let mut arg = ArgType::new(ccx.layout_of(ty));
|
|
|
|
if arg.layout.is_zst() {
|
|
|
|
// For some forsaken reason, x86_64-pc-windows-gnu
|
|
|
|
// doesn't ignore zero-sized struct arguments.
|
|
|
|
// The same is true for s390x-unknown-linux-gnu.
|
|
|
|
if is_return || rust_abi ||
|
|
|
|
(!win_x64_gnu && !linux_s390x) {
|
|
|
|
arg.ignore();
|
2017-09-20 02:16:06 +00:00
|
|
|
}
|
2017-10-03 07:45:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(eddyb) other ABIs don't have logic for nested.
|
|
|
|
if !is_return && type_is_fat_ptr(ccx, arg.layout.ty) && rust_abi {
|
|
|
|
arg.nested = vec![
|
|
|
|
ArgType::new(arg.layout.field(ccx, 0)),
|
|
|
|
ArgType::new(arg.layout.field(ccx, 1))
|
|
|
|
];
|
|
|
|
adjust_for_rust_type(&mut arg.nested[0], false);
|
|
|
|
adjust_for_rust_type(&mut arg.nested[1], false);
|
2016-02-24 09:09:25 +00:00
|
|
|
} else {
|
2017-10-03 07:45:07 +00:00
|
|
|
adjust_for_rust_type(&mut arg, is_return);
|
2016-02-24 09:09:25 +00:00
|
|
|
}
|
2017-10-03 07:45:07 +00:00
|
|
|
|
|
|
|
arg
|
|
|
|
};
|
2016-02-24 09:09:25 +00:00
|
|
|
|
2016-03-06 10:38:46 +00:00
|
|
|
FnType {
|
2017-10-03 07:45:07 +00:00
|
|
|
ret: arg_of(sig.output(), true),
|
|
|
|
args: inputs.iter().chain(extra_args.iter()).map(|ty| {
|
|
|
|
arg_of(ty, false)
|
|
|
|
}).collect(),
|
2016-11-29 03:25:33 +00:00
|
|
|
variadic: sig.variadic,
|
2017-08-07 05:54:09 +00:00
|
|
|
cconv,
|
2016-03-06 10:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-23 19:55:19 +00:00
|
|
|
|
2017-03-10 04:25:57 +00:00
|
|
|
fn adjust_for_abi(&mut self,
|
|
|
|
ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
sig: ty::FnSig<'tcx>) {
|
2017-02-13 08:51:06 +00:00
|
|
|
let abi = sig.abi;
|
2016-12-23 08:05:41 +00:00
|
|
|
if abi == Abi::Unadjusted { return }
|
|
|
|
|
2016-03-06 10:38:46 +00:00
|
|
|
if abi == Abi::Rust || abi == Abi::RustCall ||
|
|
|
|
abi == Abi::RustIntrinsic || abi == Abi::PlatformIntrinsic {
|
2017-03-10 04:25:57 +00:00
|
|
|
let fixup = |arg: &mut ArgType<'tcx>| {
|
2017-09-14 19:50:18 +00:00
|
|
|
match arg.layout.abi {
|
2017-09-17 01:42:22 +00:00
|
|
|
layout::Abi::Aggregate { .. } => {}
|
2017-09-14 19:50:18 +00:00
|
|
|
_ => return
|
2016-03-17 23:01:47 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 19:44:40 +00:00
|
|
|
let size = arg.layout.size;
|
2017-03-10 04:25:57 +00:00
|
|
|
|
2017-07-22 01:08:40 +00:00
|
|
|
if let Some(unit) = arg.layout.homogeneous_aggregate(ccx) {
|
2017-03-10 04:25:57 +00:00
|
|
|
// Replace newtypes with their inner-most type.
|
|
|
|
if unit.size == size {
|
2016-03-17 23:01:47 +00:00
|
|
|
// Needs a cast as we've unpacked a newtype.
|
2017-06-01 18:50:53 +00:00
|
|
|
arg.cast_to(unit);
|
2017-03-10 04:25:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pairs of floats.
|
|
|
|
if unit.kind == RegKind::Float {
|
|
|
|
if unit.size.checked_mul(2, ccx) == Some(size) {
|
|
|
|
// FIXME(eddyb) This should be using Uniform instead of a pair,
|
|
|
|
// but the resulting [2 x float/double] breaks emscripten.
|
|
|
|
// See https://github.com/kripken/emscripten-fastcomp/issues/178.
|
2017-06-01 18:50:53 +00:00
|
|
|
arg.cast_to(CastTarget::Pair(unit, unit));
|
2017-03-10 04:25:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-03-17 23:01:47 +00:00
|
|
|
}
|
2016-02-24 17:37:22 +00:00
|
|
|
}
|
2016-03-17 23:01:47 +00:00
|
|
|
|
2017-03-10 04:25:57 +00:00
|
|
|
if size > layout::Pointer.size(ccx) {
|
2017-09-22 19:44:40 +00:00
|
|
|
arg.make_indirect();
|
2017-03-10 04:25:57 +00:00
|
|
|
} else {
|
2016-02-24 17:37:22 +00:00
|
|
|
// We want to pass small aggregates as immediates, but using
|
|
|
|
// a LLVM aggregate type for this leads to bad optimizations,
|
|
|
|
// so we pick an appropriately sized integer type instead.
|
2017-06-01 18:50:53 +00:00
|
|
|
arg.cast_to(Reg {
|
2017-03-10 04:25:57 +00:00
|
|
|
kind: RegKind::Integer,
|
|
|
|
size
|
|
|
|
});
|
2016-02-24 17:37:22 +00:00
|
|
|
}
|
|
|
|
};
|
2016-03-06 10:38:46 +00:00
|
|
|
// Fat pointers are returned by-value.
|
|
|
|
if !self.ret.is_ignore() {
|
2016-12-18 18:50:07 +00:00
|
|
|
if !type_is_fat_ptr(ccx, sig.output()) {
|
2016-03-06 10:38:46 +00:00
|
|
|
fixup(&mut self.ret);
|
2016-02-24 17:37:22 +00:00
|
|
|
}
|
2016-02-25 13:35:47 +00:00
|
|
|
}
|
2016-03-06 10:38:46 +00:00
|
|
|
for arg in &mut self.args {
|
|
|
|
if arg.is_ignore() { continue; }
|
2017-09-20 02:16:06 +00:00
|
|
|
if !arg.nested.is_empty() {
|
|
|
|
for arg in &mut arg.nested {
|
|
|
|
assert!(arg.nested.is_empty());
|
|
|
|
fixup(arg);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-24 17:37:22 +00:00
|
|
|
fixup(arg);
|
|
|
|
}
|
2016-03-06 10:38:46 +00:00
|
|
|
if self.ret.is_indirect() {
|
2016-11-16 22:36:08 +00:00
|
|
|
self.ret.attrs.set(ArgAttribute::StructRet);
|
2016-02-25 17:35:40 +00:00
|
|
|
}
|
2016-03-06 10:38:46 +00:00
|
|
|
return;
|
2016-02-24 17:37:22 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 19:55:19 +00:00
|
|
|
match &ccx.sess().target.target.arch[..] {
|
2016-12-21 18:42:10 +00:00
|
|
|
"x86" => {
|
|
|
|
let flavor = if abi == Abi::Fastcall {
|
|
|
|
cabi_x86::Flavor::Fastcall
|
|
|
|
} else {
|
|
|
|
cabi_x86::Flavor::General
|
|
|
|
};
|
|
|
|
cabi_x86::compute_abi_info(ccx, self, flavor);
|
|
|
|
},
|
2016-08-30 01:54:29 +00:00
|
|
|
"x86_64" => if abi == Abi::SysV64 {
|
|
|
|
cabi_x86_64::compute_abi_info(ccx, self);
|
|
|
|
} else if abi == Abi::Win64 || ccx.sess().target.target.options.is_like_windows {
|
2017-09-22 19:44:40 +00:00
|
|
|
cabi_x86_win64::compute_abi_info(self);
|
2016-02-23 19:55:19 +00:00
|
|
|
} else {
|
2016-03-06 10:38:46 +00:00
|
|
|
cabi_x86_64::compute_abi_info(ccx, self);
|
2016-02-23 19:55:19 +00:00
|
|
|
},
|
2016-03-06 10:38:46 +00:00
|
|
|
"aarch64" => cabi_aarch64::compute_abi_info(ccx, self),
|
2017-03-10 04:25:57 +00:00
|
|
|
"arm" => cabi_arm::compute_abi_info(ccx, self),
|
2016-03-06 10:38:46 +00:00
|
|
|
"mips" => cabi_mips::compute_abi_info(ccx, self),
|
2016-08-26 22:06:13 +00:00
|
|
|
"mips64" => cabi_mips64::compute_abi_info(ccx, self),
|
2016-03-06 10:38:46 +00:00
|
|
|
"powerpc" => cabi_powerpc::compute_abi_info(ccx, self),
|
|
|
|
"powerpc64" => cabi_powerpc64::compute_abi_info(ccx, self),
|
2016-09-09 21:00:23 +00:00
|
|
|
"s390x" => cabi_s390x::compute_abi_info(ccx, self),
|
2016-03-06 10:38:46 +00:00
|
|
|
"asmjs" => cabi_asmjs::compute_abi_info(ccx, self),
|
2016-09-06 00:41:50 +00:00
|
|
|
"wasm32" => cabi_asmjs::compute_abi_info(ccx, self),
|
2017-09-22 19:44:40 +00:00
|
|
|
"msp430" => cabi_msp430::compute_abi_info(self),
|
2016-12-12 17:41:45 +00:00
|
|
|
"sparc" => cabi_sparc::compute_abi_info(ccx, self),
|
2016-12-03 16:55:50 +00:00
|
|
|
"sparc64" => cabi_sparc64::compute_abi_info(ccx, self),
|
2017-09-22 19:44:40 +00:00
|
|
|
"nvptx" => cabi_nvptx::compute_abi_info(self),
|
|
|
|
"nvptx64" => cabi_nvptx64::compute_abi_info(self),
|
|
|
|
"hexagon" => cabi_hexagon::compute_abi_info(self),
|
2016-02-23 19:55:19 +00:00
|
|
|
a => ccx.sess().fatal(&format!("unrecognized arch \"{}\" in target specification", a))
|
|
|
|
}
|
|
|
|
|
2016-03-06 10:38:46 +00:00
|
|
|
if self.ret.is_indirect() {
|
2016-11-16 22:36:08 +00:00
|
|
|
self.ret.attrs.set(ArgAttribute::StructRet);
|
2016-02-25 17:35:40 +00:00
|
|
|
}
|
2016-02-23 19:55:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 04:25:57 +00:00
|
|
|
pub fn llvm_type(&self, ccx: &CrateContext<'a, 'tcx>) -> Type {
|
2016-02-23 19:55:19 +00:00
|
|
|
let mut llargument_tys = Vec::new();
|
|
|
|
|
2016-03-06 10:36:39 +00:00
|
|
|
let llreturn_ty = if self.ret.is_ignore() {
|
|
|
|
Type::void(ccx)
|
|
|
|
} else if self.ret.is_indirect() {
|
2017-03-10 04:25:57 +00:00
|
|
|
llargument_tys.push(self.ret.memory_ty(ccx).ptr_to());
|
2016-02-23 19:55:19 +00:00
|
|
|
Type::void(ccx)
|
2017-06-01 18:50:53 +00:00
|
|
|
} else if let Some(cast) = self.ret.cast {
|
|
|
|
cast.llvm_type(ccx)
|
2014-07-23 18:56:36 +00:00
|
|
|
} else {
|
2017-09-20 20:07:47 +00:00
|
|
|
self.ret.layout.immediate_llvm_type(ccx)
|
2016-02-23 19:55:19 +00:00
|
|
|
};
|
|
|
|
|
2017-09-20 02:16:06 +00:00
|
|
|
{
|
|
|
|
let mut push = |arg: &ArgType<'tcx>| {
|
|
|
|
if arg.is_ignore() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// add padding
|
|
|
|
if let Some(ty) = arg.pad {
|
|
|
|
llargument_tys.push(ty.llvm_type(ccx));
|
|
|
|
}
|
2016-02-23 19:55:19 +00:00
|
|
|
|
2017-09-20 02:16:06 +00:00
|
|
|
let llarg_ty = if arg.is_indirect() {
|
|
|
|
arg.memory_ty(ccx).ptr_to()
|
|
|
|
} else if let Some(cast) = arg.cast {
|
|
|
|
cast.llvm_type(ccx)
|
|
|
|
} else {
|
2017-09-20 20:07:47 +00:00
|
|
|
arg.layout.immediate_llvm_type(ccx)
|
2017-09-20 02:16:06 +00:00
|
|
|
};
|
2016-02-23 19:55:19 +00:00
|
|
|
|
2017-09-20 02:16:06 +00:00
|
|
|
llargument_tys.push(llarg_ty);
|
|
|
|
};
|
|
|
|
for arg in &self.args {
|
|
|
|
if !arg.nested.is_empty() {
|
|
|
|
for arg in &arg.nested {
|
|
|
|
assert!(arg.nested.is_empty());
|
|
|
|
push(arg);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
push(arg);
|
|
|
|
}
|
2016-02-23 19:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.variadic {
|
|
|
|
Type::variadic_func(&llargument_tys, &llreturn_ty)
|
|
|
|
} else {
|
|
|
|
Type::func(&llargument_tys, &llreturn_ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 23:10:40 +00:00
|
|
|
pub fn apply_attrs_llfn(&self, llfn: ValueRef) {
|
|
|
|
let mut i = if self.ret.is_indirect() { 1 } else { 0 };
|
2016-03-06 10:36:39 +00:00
|
|
|
if !self.ret.is_ignore() {
|
2016-08-02 21:25:19 +00:00
|
|
|
self.ret.attrs.apply_llfn(llvm::AttributePlace::Argument(i), llfn);
|
2016-03-06 10:36:39 +00:00
|
|
|
}
|
2016-02-25 23:10:40 +00:00
|
|
|
i += 1;
|
2017-09-20 02:16:06 +00:00
|
|
|
let mut apply = |arg: &ArgType| {
|
2016-02-25 23:10:40 +00:00
|
|
|
if !arg.is_ignore() {
|
|
|
|
if arg.pad.is_some() { i += 1; }
|
2016-08-02 21:25:19 +00:00
|
|
|
arg.attrs.apply_llfn(llvm::AttributePlace::Argument(i), llfn);
|
2016-02-25 23:10:40 +00:00
|
|
|
i += 1;
|
|
|
|
}
|
2017-09-20 02:16:06 +00:00
|
|
|
};
|
|
|
|
for arg in &self.args {
|
|
|
|
if !arg.nested.is_empty() {
|
|
|
|
for arg in &arg.nested {
|
|
|
|
assert!(arg.nested.is_empty());
|
|
|
|
apply(arg);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
apply(arg);
|
2016-02-25 23:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn apply_attrs_callsite(&self, callsite: ValueRef) {
|
2016-02-25 10:11:02 +00:00
|
|
|
let mut i = if self.ret.is_indirect() { 1 } else { 0 };
|
2016-03-06 10:36:39 +00:00
|
|
|
if !self.ret.is_ignore() {
|
2016-08-02 21:25:19 +00:00
|
|
|
self.ret.attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite);
|
2016-03-06 10:36:39 +00:00
|
|
|
}
|
2016-02-23 19:55:19 +00:00
|
|
|
i += 1;
|
2017-09-20 02:16:06 +00:00
|
|
|
let mut apply = |arg: &ArgType| {
|
2016-02-25 17:35:40 +00:00
|
|
|
if !arg.is_ignore() {
|
|
|
|
if arg.pad.is_some() { i += 1; }
|
2016-08-02 21:25:19 +00:00
|
|
|
arg.attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite);
|
2016-02-25 17:35:40 +00:00
|
|
|
i += 1;
|
2016-02-23 19:55:19 +00:00
|
|
|
}
|
2017-09-20 02:16:06 +00:00
|
|
|
};
|
|
|
|
for arg in &self.args {
|
|
|
|
if !arg.nested.is_empty() {
|
|
|
|
for arg in &arg.nested {
|
|
|
|
assert!(arg.nested.is_empty());
|
|
|
|
apply(arg);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
apply(arg);
|
2016-02-23 19:55:19 +00:00
|
|
|
}
|
2016-03-06 10:34:31 +00:00
|
|
|
|
|
|
|
if self.cconv != llvm::CCallConv {
|
|
|
|
llvm::SetInstructionCallConv(callsite, self.cconv);
|
|
|
|
}
|
2013-01-25 22:56:56 +00:00
|
|
|
}
|
|
|
|
}
|