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.
|
|
|
|
|
2016-11-16 22:36:08 +00:00
|
|
|
use llvm::{self, ValueRef, Integer, Pointer, Float, Double, Struct, Array, Vector, AttributePlace};
|
2016-03-22 17:23:36 +00:00
|
|
|
use base;
|
2016-12-31 23:00:24 +00:00
|
|
|
use builder::Builder;
|
|
|
|
use common::{type_is_fat_ptr, C_uint};
|
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;
|
2016-08-29 00:44:19 +00:00
|
|
|
use machine::{llalign_of_min, llsize_of, llsize_of_alloc};
|
2016-03-22 17:23:36 +00:00
|
|
|
use type_::Type;
|
|
|
|
use type_of;
|
2016-02-23 19:55:19 +00:00
|
|
|
|
2016-03-29 05:50:44 +00:00
|
|
|
use rustc::hir;
|
2016-03-22 15:30:57 +00:00
|
|
|
use rustc::ty::{self, Ty};
|
2016-02-23 19:55:19 +00:00
|
|
|
|
2016-03-06 14:30:21 +00:00
|
|
|
use libc::c_uint;
|
2016-06-07 21:35:01 +00:00
|
|
|
use std::cmp;
|
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};
|
2016-09-07 19:03:21 +00:00
|
|
|
use rustc::ty::layout::Layout;
|
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)]
|
|
|
|
mod attr_impl {
|
|
|
|
// The subset of llvm::Attribute needed for arguments, packed into a bitfield.
|
|
|
|
bitflags! {
|
|
|
|
#[derive(Default, Debug)]
|
2016-12-21 18:42:10 +00:00
|
|
|
flags ArgAttribute : u16 {
|
2016-11-16 22:36:08 +00:00
|
|
|
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,
|
2016-12-21 18:42:10 +00:00
|
|
|
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.
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
pub struct ArgAttributes {
|
|
|
|
regular: ArgAttribute,
|
|
|
|
dereferenceable_bytes: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ArgAttributes {
|
|
|
|
pub fn set(&mut self, attr: ArgAttribute) -> &mut Self {
|
|
|
|
self.regular = self.regular | attr;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_dereferenceable(&mut self, bytes: u64) -> &mut Self {
|
|
|
|
self.dereferenceable_bytes = bytes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn apply_llfn(&self, idx: AttributePlace, llfn: ValueRef) {
|
|
|
|
unsafe {
|
|
|
|
self.regular.for_each_kind(|attr| attr.apply_llfn(idx, llfn));
|
|
|
|
if self.dereferenceable_bytes != 0 {
|
|
|
|
llvm::LLVMRustAddDereferenceableAttr(llfn,
|
|
|
|
idx.as_uint(),
|
|
|
|
self.dereferenceable_bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn apply_callsite(&self, idx: AttributePlace, callsite: ValueRef) {
|
|
|
|
unsafe {
|
|
|
|
self.regular.for_each_kind(|attr| attr.apply_callsite(idx, callsite));
|
|
|
|
if self.dereferenceable_bytes != 0 {
|
|
|
|
llvm::LLVMRustAddDereferenceableCallSiteAttr(callsite,
|
|
|
|
idx.as_uint(),
|
|
|
|
self.dereferenceable_bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2016-02-18 17:49:45 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2013-09-25 10:30:44 +00:00
|
|
|
pub struct ArgType {
|
2016-02-25 17:35:40 +00:00
|
|
|
kind: ArgKind,
|
2013-09-25 10:30:44 +00:00
|
|
|
/// Original LLVM type
|
2016-02-25 13:35:47 +00:00
|
|
|
pub original_ty: Type,
|
|
|
|
/// Sizing LLVM type (pointers are opaque).
|
|
|
|
/// Unlike original_ty, this is guaranteed to be complete.
|
|
|
|
///
|
|
|
|
/// For example, while we're computing the function pointer type in
|
|
|
|
/// `struct Foo(fn(Foo));`, `original_ty` is still LLVM's `%Foo = {}`.
|
|
|
|
/// The field type will likely end up being `void(%Foo)*`, but we cannot
|
|
|
|
/// use `%Foo` to compute properties (e.g. size and alignment) of `Foo`,
|
|
|
|
/// until `%Foo` is completed by having all of its field types inserted,
|
|
|
|
/// so `ty` holds the "sizing type" of `Foo`, which replaces all pointers
|
|
|
|
/// with opaque ones, resulting in `{i8*}` for `Foo`.
|
|
|
|
/// ABI-specific logic can then look at the size, alignment and fields of
|
|
|
|
/// `{i8*}` in order to determine how the argument will be passed.
|
|
|
|
/// Only later will `original_ty` aka `%Foo` be used in the LLVM function
|
|
|
|
/// pointer type, without ever having introspected it.
|
2014-03-28 17:05:27 +00:00
|
|
|
pub ty: Type,
|
2016-02-17 06:33:27 +00:00
|
|
|
/// Signedness for integer types, None for other types
|
|
|
|
pub signedness: Option<bool>,
|
2013-09-25 10:30:44 +00:00
|
|
|
/// Coerced LLVM Type
|
2016-02-23 19:55:19 +00:00
|
|
|
pub cast: Option<Type>,
|
2013-09-25 10:30:44 +00:00
|
|
|
/// Dummy argument, which is emitted before the real argument
|
2016-02-23 19:55:19 +00:00
|
|
|
pub pad: Option<Type>,
|
2016-02-25 17:35:40 +00:00
|
|
|
/// LLVM attributes of argument
|
2016-11-16 22:36:08 +00:00
|
|
|
pub attrs: ArgAttributes
|
2013-09-25 10:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ArgType {
|
2016-02-25 13:35:47 +00:00
|
|
|
fn new(original_ty: Type, ty: Type) -> ArgType {
|
2013-09-25 10:30:44 +00:00
|
|
|
ArgType {
|
2016-02-25 17:35:40 +00:00
|
|
|
kind: ArgKind::Direct,
|
2016-02-25 13:35:47 +00:00
|
|
|
original_ty: original_ty,
|
2013-09-25 10:30:44 +00:00
|
|
|
ty: ty,
|
2016-02-17 06:33:27 +00:00
|
|
|
signedness: None,
|
2014-03-09 06:42:22 +00:00
|
|
|
cast: None,
|
|
|
|
pad: None,
|
2016-11-16 22:36:08 +00:00
|
|
|
attrs: ArgAttributes::default()
|
2014-03-09 06:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 17:35:40 +00:00
|
|
|
pub fn make_indirect(&mut self, ccx: &CrateContext) {
|
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.
|
2016-11-16 22:36:08 +00:00
|
|
|
self.attrs = ArgAttributes::default();
|
2016-02-25 17:35:40 +00:00
|
|
|
|
2016-08-29 00:44:19 +00:00
|
|
|
let llarg_sz = llsize_of_alloc(ccx, self.ty);
|
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)
|
2016-02-25 17:35:40 +00:00
|
|
|
.set_dereferenceable(llarg_sz);
|
|
|
|
|
|
|
|
self.kind = ArgKind::Indirect;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ignore(&mut self) {
|
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
|
|
|
|
if let Some(signed) = self.signedness {
|
|
|
|
if self.ty.int_width() < bits {
|
|
|
|
self.attrs.set(if signed {
|
2016-11-16 22:36:08 +00:00
|
|
|
ArgAttribute::SExt
|
2016-02-17 06:33:27 +00:00
|
|
|
} else {
|
2016-11-16 22:36:08 +00:00
|
|
|
ArgAttribute::ZExt
|
2016-02-17 06:33:27 +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
|
|
|
|
2016-03-06 14:30:21 +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) -> Type {
|
|
|
|
if self.original_ty == Type::i1(ccx) {
|
|
|
|
Type::i8(ccx)
|
|
|
|
} else {
|
|
|
|
self.original_ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2016-12-31 23:00:24 +00:00
|
|
|
pub fn store(&self, bcx: &Builder, mut val: ValueRef, dst: ValueRef) {
|
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() {
|
2016-06-07 21:35:01 +00:00
|
|
|
let llsz = llsize_of(ccx, self.ty);
|
|
|
|
let llalign = llalign_of_min(ccx, self.ty);
|
|
|
|
base::call_memcpy(bcx, dst, val, llsz, llalign as u32);
|
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 {
|
|
|
|
let cast_dst = bcx.pointercast(dst, ty.ptr_to());
|
|
|
|
let llalign = llalign_of_min(ccx, self.ty);
|
2016-12-29 01:20:26 +00:00
|
|
|
bcx.store(val, cast_dst, Some(llalign));
|
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...
|
2016-12-31 23:00:24 +00:00
|
|
|
let llscratch = bcx.alloca(ty, "abi_cast");
|
2016-06-07 21:35:01 +00:00
|
|
|
base::Lifetime::Start.call(bcx, llscratch);
|
|
|
|
|
|
|
|
// ...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,
|
|
|
|
bcx.pointercast(dst, Type::i8p(ccx)),
|
|
|
|
bcx.pointercast(llscratch, Type::i8p(ccx)),
|
2016-08-29 00:44:19 +00:00
|
|
|
C_uint(ccx, llsize_of_alloc(ccx, self.ty)),
|
2016-06-07 21:35:01 +00:00
|
|
|
cmp::min(llalign_of_min(ccx, self.ty),
|
|
|
|
llalign_of_min(ccx, ty)) as u32);
|
|
|
|
|
|
|
|
base::Lifetime::End.call(bcx, llscratch);
|
2016-03-06 11:23:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-06-07 21:35:01 +00:00
|
|
|
if self.original_ty == Type::i1(ccx) {
|
|
|
|
val = bcx.zext(val, Type::i8(ccx));
|
2016-03-06 11:23:20 +00:00
|
|
|
}
|
2016-12-29 01:20:26 +00:00
|
|
|
bcx.store(val, dst, None);
|
2016-03-06 11:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-06 14:30:21 +00:00
|
|
|
|
2017-01-02 20:24:04 +00:00
|
|
|
pub fn store_fn_arg(&self, bcx: &Builder, idx: &mut usize, dst: ValueRef) {
|
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
|
2016-12-31 02:55:29 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2013-01-30 19:46:19 +00:00
|
|
|
pub struct FnType {
|
2013-09-25 10:30:44 +00:00
|
|
|
/// The LLVM types of each argument.
|
2016-02-23 19:55:19 +00:00
|
|
|
pub args: Vec<ArgType>,
|
2013-01-25 22:56:56 +00:00
|
|
|
|
2013-05-21 19:25:44 +00:00
|
|
|
/// LLVM return type.
|
2016-02-23 19:55:19 +00:00
|
|
|
pub ret: ArgType,
|
|
|
|
|
|
|
|
pub variadic: bool,
|
|
|
|
|
|
|
|
pub cconv: llvm::CallConv
|
2013-05-21 19:25:44 +00:00
|
|
|
}
|
2013-04-18 22:53:29 +00:00
|
|
|
|
2016-02-23 19:55:19 +00:00
|
|
|
impl FnType {
|
|
|
|
pub fn new<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
abi: Abi,
|
|
|
|
sig: &ty::FnSig<'tcx>,
|
|
|
|
extra_args: &[Ty<'tcx>]) -> FnType {
|
2016-03-06 10:38:46 +00:00
|
|
|
let mut fn_ty = FnType::unadjusted(ccx, abi, sig, extra_args);
|
|
|
|
fn_ty.adjust_for_abi(ccx, abi, sig);
|
|
|
|
fn_ty
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unadjusted<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
abi: Abi,
|
|
|
|
sig: &ty::FnSig<'tcx>,
|
|
|
|
extra_args: &[Ty<'tcx>]) -> FnType {
|
2016-02-23 20:43:04 +00:00
|
|
|
use self::Abi::*;
|
2016-02-23 19:55:19 +00:00
|
|
|
let cconv = match ccx.sess().target.target.adjust_abi(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,
|
|
|
|
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,
|
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();
|
2016-02-24 17:37:22 +00:00
|
|
|
let extra_args = if 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 {
|
2016-02-24 17:37:22 +00:00
|
|
|
ty::TyTuple(ref tupled_arguments) => {
|
2016-11-29 02:35:38 +00:00
|
|
|
inputs = &sig.inputs()[0..sig.inputs().len() - 1];
|
2016-02-24 17:37:22 +00:00
|
|
|
&tupled_arguments[..]
|
|
|
|
}
|
|
|
|
_ => {
|
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";
|
2016-03-18 12:28:11 +00:00
|
|
|
let rust_abi = match abi {
|
|
|
|
RustIntrinsic | PlatformIntrinsic | Rust | RustCall => true,
|
|
|
|
_ => false
|
|
|
|
};
|
|
|
|
|
|
|
|
let arg_of = |ty: Ty<'tcx>, is_return: bool| {
|
2016-02-25 13:35:47 +00:00
|
|
|
if ty.is_bool() {
|
|
|
|
let llty = Type::i1(ccx);
|
|
|
|
let mut arg = ArgType::new(llty, llty);
|
2016-11-16 22:36:08 +00:00
|
|
|
arg.attrs.set(ArgAttribute::ZExt);
|
2016-02-25 13:35:47 +00:00
|
|
|
arg
|
|
|
|
} else {
|
2016-03-06 10:36:39 +00:00
|
|
|
let mut arg = ArgType::new(type_of::type_of(ccx, ty),
|
|
|
|
type_of::sizing_type_of(ccx, ty));
|
2016-02-17 06:33:27 +00:00
|
|
|
if ty.is_integral() {
|
|
|
|
arg.signedness = Some(ty.is_signed());
|
|
|
|
}
|
2016-09-07 19:03:21 +00:00
|
|
|
// Rust enum types that map onto C enums also need to follow
|
|
|
|
// the target ABI zero-/sign-extension rules.
|
|
|
|
if let Layout::CEnum { signed, .. } = *ccx.layout_of(ty) {
|
|
|
|
arg.signedness = Some(signed);
|
2016-09-07 15:09:24 +00:00
|
|
|
}
|
2016-08-29 00:44:19 +00:00
|
|
|
if llsize_of_alloc(ccx, arg.ty) == 0 {
|
2016-03-18 12:28:11 +00:00
|
|
|
// For some forsaken reason, x86_64-pc-windows-gnu
|
|
|
|
// doesn't ignore zero-sized struct arguments.
|
2016-09-09 21:00:23 +00:00
|
|
|
// The same is true for s390x-unknown-linux-gnu.
|
|
|
|
if is_return || rust_abi ||
|
|
|
|
(!win_x64_gnu && !linux_s390x) {
|
2016-03-18 12:28:11 +00:00
|
|
|
arg.ignore();
|
|
|
|
}
|
2016-03-06 10:36:39 +00:00
|
|
|
}
|
|
|
|
arg
|
2016-02-25 13:35:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-29 02:35:38 +00:00
|
|
|
let ret_ty = sig.output();
|
2016-03-18 12:28:11 +00:00
|
|
|
let mut ret = arg_of(ret_ty, true);
|
2016-03-06 10:36:39 +00:00
|
|
|
|
2016-12-18 18:50:07 +00:00
|
|
|
if !type_is_fat_ptr(ccx, ret_ty) {
|
2016-03-06 10:36:39 +00:00
|
|
|
// The `noalias` attribute on the return value is useful to a
|
|
|
|
// function ptr caller.
|
2017-01-21 14:40:31 +00:00
|
|
|
if ret_ty.is_box() {
|
2016-03-06 10:36:39 +00:00
|
|
|
// `Box` pointer return values never alias because ownership
|
|
|
|
// is transferred
|
2016-11-16 22:36:08 +00:00
|
|
|
ret.attrs.set(ArgAttribute::NoAlias);
|
2016-03-06 10:36:39 +00:00
|
|
|
}
|
2016-02-25 13:35:47 +00:00
|
|
|
|
2016-03-06 10:36:39 +00:00
|
|
|
// We can also mark the return value as `dereferenceable` in certain cases
|
|
|
|
match ret_ty.sty {
|
|
|
|
// These are not really pointers but pairs, (pointer, len)
|
2017-01-21 14:40:31 +00:00
|
|
|
ty::TyRef(_, ty::TypeAndMut { ty, .. }) => {
|
2016-03-06 10:36:39 +00:00
|
|
|
let llty = type_of::sizing_type_of(ccx, ty);
|
2016-08-29 00:44:19 +00:00
|
|
|
let llsz = llsize_of_alloc(ccx, llty);
|
2016-03-06 10:36:39 +00:00
|
|
|
ret.attrs.set_dereferenceable(llsz);
|
2016-02-25 23:10:40 +00:00
|
|
|
}
|
2017-01-21 14:40:31 +00:00
|
|
|
ty::TyAdt(def, _) if def.is_box() => {
|
|
|
|
let llty = type_of::sizing_type_of(ccx, ret_ty.boxed_ty());
|
|
|
|
let llsz = llsize_of_alloc(ccx, llty);
|
|
|
|
ret.attrs.set_dereferenceable(llsz);
|
|
|
|
}
|
2016-03-06 10:36:39 +00:00
|
|
|
_ => {}
|
2016-02-25 23:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 17:37:22 +00:00
|
|
|
let mut args = Vec::with_capacity(inputs.len() + extra_args.len());
|
2016-02-25 23:10:40 +00:00
|
|
|
|
|
|
|
// Handle safe Rust thin and fat pointers.
|
|
|
|
let rust_ptr_attrs = |ty: Ty<'tcx>, arg: &mut ArgType| match ty.sty {
|
|
|
|
// `Box` pointer parameters never alias because ownership is transferred
|
2017-01-21 14:40:31 +00:00
|
|
|
ty::TyAdt(def, _) if def.is_box() => {
|
2016-11-16 22:36:08 +00:00
|
|
|
arg.attrs.set(ArgAttribute::NoAlias);
|
2017-01-21 14:40:31 +00:00
|
|
|
Some(ty.boxed_ty())
|
2016-02-25 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::TyRef(b, mt) => {
|
2016-03-22 15:30:57 +00:00
|
|
|
use rustc::ty::{BrAnon, ReLateBound};
|
2016-02-25 23:10:40 +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
|
|
|
|
let interior_unsafe = mt.ty.type_contents(ccx.tcx()).interior_unsafe();
|
|
|
|
|
|
|
|
if mt.mutbl != hir::MutMutable && !interior_unsafe {
|
2016-11-16 22:36:08 +00:00
|
|
|
arg.attrs.set(ArgAttribute::NoAlias);
|
2016-02-25 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if mt.mutbl == hir::MutImmutable && !interior_unsafe {
|
2016-11-16 22:36:08 +00:00
|
|
|
arg.attrs.set(ArgAttribute::ReadOnly);
|
2016-02-25 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When a reference in an argument has no named lifetime, it's
|
|
|
|
// impossible for that reference to escape this function
|
|
|
|
// (returned or stored beyond the call by a closure).
|
|
|
|
if let ReLateBound(_, BrAnon(_)) = *b {
|
2016-11-16 22:36:08 +00:00
|
|
|
arg.attrs.set(ArgAttribute::NoCapture);
|
2016-02-25 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(mt.ty)
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
|
2016-02-24 17:37:22 +00:00
|
|
|
for ty in inputs.iter().chain(extra_args.iter()) {
|
2016-03-18 12:28:11 +00:00
|
|
|
let mut arg = arg_of(ty, false);
|
2016-02-25 23:10:40 +00:00
|
|
|
|
2016-12-18 18:50:07 +00:00
|
|
|
if type_is_fat_ptr(ccx, ty) {
|
2016-02-25 23:10:40 +00:00
|
|
|
let original_tys = arg.original_ty.field_types();
|
|
|
|
let sizing_tys = arg.ty.field_types();
|
|
|
|
assert_eq!((original_tys.len(), sizing_tys.len()), (2, 2));
|
|
|
|
|
|
|
|
let mut data = ArgType::new(original_tys[0], sizing_tys[0]);
|
|
|
|
let mut info = ArgType::new(original_tys[1], sizing_tys[1]);
|
|
|
|
|
|
|
|
if let Some(inner) = rust_ptr_attrs(ty, &mut data) {
|
2016-11-16 22:36:08 +00:00
|
|
|
data.attrs.set(ArgAttribute::NonNull);
|
2016-02-25 23:10:40 +00:00
|
|
|
if ccx.tcx().struct_tail(inner).is_trait() {
|
2016-11-16 22:36:08 +00:00
|
|
|
info.attrs.set(ArgAttribute::NonNull);
|
2016-02-25 23:10:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
args.push(data);
|
|
|
|
args.push(info);
|
2016-02-24 09:09:25 +00:00
|
|
|
} else {
|
2016-02-25 23:10:40 +00:00
|
|
|
if let Some(inner) = rust_ptr_attrs(ty, &mut arg) {
|
|
|
|
let llty = type_of::sizing_type_of(ccx, inner);
|
2016-08-29 00:44:19 +00:00
|
|
|
let llsz = llsize_of_alloc(ccx, llty);
|
2016-02-25 23:10:40 +00:00
|
|
|
arg.attrs.set_dereferenceable(llsz);
|
|
|
|
}
|
2016-02-25 13:35:47 +00:00
|
|
|
args.push(arg);
|
2016-02-24 09:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 10:38:46 +00:00
|
|
|
FnType {
|
2016-02-24 09:09:25 +00:00
|
|
|
args: args,
|
2016-02-25 13:35:47 +00:00
|
|
|
ret: ret,
|
2016-11-29 03:25:33 +00:00
|
|
|
variadic: sig.variadic,
|
2016-02-23 19:55:19 +00:00
|
|
|
cconv: cconv
|
2016-03-06 10:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-23 19:55:19 +00:00
|
|
|
|
2016-03-06 10:38:46 +00:00
|
|
|
pub fn adjust_for_abi<'a, 'tcx>(&mut self,
|
|
|
|
ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
abi: Abi,
|
|
|
|
sig: &ty::FnSig<'tcx>) {
|
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 {
|
2016-02-24 17:37:22 +00:00
|
|
|
let fixup = |arg: &mut ArgType| {
|
2016-03-17 23:01:47 +00:00
|
|
|
let mut llty = arg.ty;
|
|
|
|
|
|
|
|
// Replace newtypes with their inner-most type.
|
|
|
|
while llty.kind() == llvm::TypeKind::Struct {
|
|
|
|
let inner = llty.field_types();
|
|
|
|
if inner.len() != 1 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
llty = inner[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if !llty.is_aggregate() {
|
2016-02-24 17:37:22 +00:00
|
|
|
// Scalars and vectors, always immediate.
|
2016-03-17 23:01:47 +00:00
|
|
|
if llty != arg.ty {
|
|
|
|
// Needs a cast as we've unpacked a newtype.
|
|
|
|
arg.cast = Some(llty);
|
|
|
|
}
|
2016-02-24 17:37:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-03-17 23:01:47 +00:00
|
|
|
|
2016-08-29 00:44:19 +00:00
|
|
|
let size = llsize_of_alloc(ccx, llty);
|
|
|
|
if size > llsize_of_alloc(ccx, ccx.int_type()) {
|
2016-02-25 17:35:40 +00:00
|
|
|
arg.make_indirect(ccx);
|
2016-02-24 17:37:22 +00:00
|
|
|
} else if size > 0 {
|
|
|
|
// 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.
|
|
|
|
arg.cast = Some(Type::ix(ccx, size * 8));
|
|
|
|
}
|
|
|
|
};
|
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; }
|
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 {
|
2016-03-06 10:38:46 +00:00
|
|
|
cabi_x86_win64::compute_abi_info(ccx, 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),
|
2016-02-23 19:55:19 +00:00
|
|
|
"arm" => {
|
|
|
|
let flavor = if ccx.sess().target.target.target_os == "ios" {
|
|
|
|
cabi_arm::Flavor::Ios
|
|
|
|
} else {
|
|
|
|
cabi_arm::Flavor::General
|
|
|
|
};
|
2016-03-06 10:38:46 +00:00
|
|
|
cabi_arm::compute_abi_info(ccx, self, flavor);
|
2016-02-23 19:55:19 +00:00
|
|
|
},
|
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),
|
2016-11-13 16:03:44 +00:00
|
|
|
"msp430" => cabi_msp430::compute_abi_info(ccx, 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),
|
2016-12-22 21:24:29 +00:00
|
|
|
"nvptx" => cabi_nvptx::compute_abi_info(ccx, self),
|
|
|
|
"nvptx64" => cabi_nvptx64::compute_abi_info(ccx, 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
|
|
|
}
|
|
|
|
|
2016-02-25 10:11:02 +00:00
|
|
|
pub fn llvm_type(&self, ccx: &CrateContext) -> 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() {
|
2016-02-25 13:35:47 +00:00
|
|
|
llargument_tys.push(self.ret.original_ty.ptr_to());
|
2016-02-23 19:55:19 +00:00
|
|
|
Type::void(ccx)
|
2014-07-23 18:56:36 +00:00
|
|
|
} else {
|
2016-02-25 13:35:47 +00:00
|
|
|
self.ret.cast.unwrap_or(self.ret.original_ty)
|
2016-02-23 19:55:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for arg in &self.args {
|
|
|
|
if arg.is_ignore() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// add padding
|
|
|
|
if let Some(ty) = arg.pad {
|
|
|
|
llargument_tys.push(ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
let llarg_ty = if arg.is_indirect() {
|
2016-02-25 13:35:47 +00:00
|
|
|
arg.original_ty.ptr_to()
|
2015-01-09 16:18:23 +00:00
|
|
|
} else {
|
2016-02-25 13:35:47 +00:00
|
|
|
arg.cast.unwrap_or(arg.original_ty)
|
2015-01-09 16:18:23 +00:00
|
|
|
};
|
2016-02-23 19:55:19 +00:00
|
|
|
|
|
|
|
llargument_tys.push(llarg_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
for arg in &self.args {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
for arg in &self.args {
|
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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2016-09-25 00:19:56 +00:00
|
|
|
|
|
|
|
pub fn align_up_to(off: usize, a: usize) -> usize {
|
|
|
|
return (off + a - 1) / a * a;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn align(off: usize, ty: Type, pointer: usize) -> usize {
|
|
|
|
let a = ty_align(ty, pointer);
|
|
|
|
return align_up_to(off, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ty_align(ty: Type, pointer: usize) -> usize {
|
|
|
|
match ty.kind() {
|
|
|
|
Integer => ((ty.int_width() as usize) + 7) / 8,
|
|
|
|
Pointer => pointer,
|
|
|
|
Float => 4,
|
|
|
|
Double => 8,
|
|
|
|
Struct => {
|
|
|
|
if ty.is_packed() {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
let str_tys = ty.field_types();
|
|
|
|
str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t, pointer)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Array => {
|
|
|
|
let elt = ty.element_type();
|
|
|
|
ty_align(elt, pointer)
|
|
|
|
}
|
|
|
|
Vector => {
|
|
|
|
let len = ty.vector_length();
|
|
|
|
let elt = ty.element_type();
|
|
|
|
ty_align(elt, pointer) * len
|
|
|
|
}
|
|
|
|
_ => bug!("ty_align: unhandled type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ty_size(ty: Type, pointer: usize) -> usize {
|
|
|
|
match ty.kind() {
|
|
|
|
Integer => ((ty.int_width() as usize) + 7) / 8,
|
|
|
|
Pointer => pointer,
|
|
|
|
Float => 4,
|
|
|
|
Double => 8,
|
|
|
|
Struct => {
|
|
|
|
if ty.is_packed() {
|
|
|
|
let str_tys = ty.field_types();
|
|
|
|
str_tys.iter().fold(0, |s, t| s + ty_size(*t, pointer))
|
|
|
|
} else {
|
|
|
|
let str_tys = ty.field_types();
|
|
|
|
let size = str_tys.iter().fold(0, |s, t| {
|
|
|
|
align(s, *t, pointer) + ty_size(*t, pointer)
|
|
|
|
});
|
|
|
|
align(size, ty, pointer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Array => {
|
|
|
|
let len = ty.array_length();
|
|
|
|
let elt = ty.element_type();
|
|
|
|
let eltsz = ty_size(elt, pointer);
|
|
|
|
len * eltsz
|
|
|
|
}
|
|
|
|
Vector => {
|
|
|
|
let len = ty.vector_length();
|
|
|
|
let elt = ty.element_type();
|
|
|
|
let eltsz = ty_size(elt, pointer);
|
|
|
|
len * eltsz
|
|
|
|
},
|
|
|
|
_ => bug!("ty_size: unhandled type")
|
|
|
|
}
|
|
|
|
}
|