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.
|
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
pub use self::ArgKind::*;
|
|
|
|
|
2016-02-25 10:11:02 +00:00
|
|
|
use llvm;
|
2016-02-24 09:09:25 +00:00
|
|
|
use trans::common::{return_type_is_void, type_is_fat_ptr};
|
2014-11-16 01:30:33 +00:00
|
|
|
use trans::context::CrateContext;
|
|
|
|
use trans::cabi_x86;
|
|
|
|
use trans::cabi_x86_64;
|
|
|
|
use trans::cabi_x86_win64;
|
|
|
|
use trans::cabi_arm;
|
2014-12-12 23:39:27 +00:00
|
|
|
use trans::cabi_aarch64;
|
2015-01-10 04:13:23 +00:00
|
|
|
use trans::cabi_powerpc;
|
2015-12-28 21:09:06 +00:00
|
|
|
use trans::cabi_powerpc64;
|
2014-11-16 01:30:33 +00:00
|
|
|
use trans::cabi_mips;
|
2015-11-26 19:05:10 +00:00
|
|
|
use trans::cabi_asmjs;
|
2016-02-25 10:11:02 +00:00
|
|
|
use trans::machine::{llsize_of_alloc, llsize_of_real};
|
2014-11-16 01:30:33 +00:00
|
|
|
use trans::type_::Type;
|
2016-02-23 19:55:19 +00:00
|
|
|
use trans::type_of;
|
|
|
|
|
|
|
|
use middle::ty::{self, Ty};
|
|
|
|
|
2016-02-23 20:43:04 +00:00
|
|
|
pub use syntax::abi::Abi;
|
|
|
|
|
|
|
|
/// The first half of a fat pointer.
|
|
|
|
/// - For a closure, this is the code address.
|
|
|
|
/// - For an object or trait instance, this is the address of the box.
|
|
|
|
/// - For a slice, this is the base address.
|
|
|
|
pub const FAT_PTR_ADDR: usize = 0;
|
|
|
|
|
|
|
|
/// The second half of a fat pointer.
|
|
|
|
/// - For a closure, this is the address of the environment.
|
|
|
|
/// - For an object or trait instance, this is the address of the vtable.
|
|
|
|
/// - For a slice, this is the length.
|
|
|
|
pub const FAT_PTR_EXTRA: usize = 1;
|
2013-01-25 22:56:56 +00:00
|
|
|
|
2016-02-18 17:49:45 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
2013-09-25 10:30:44 +00:00
|
|
|
pub enum ArgKind {
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
2014-03-28 17:05:27 +00:00
|
|
|
pub kind: ArgKind,
|
2013-09-25 10:30:44 +00:00
|
|
|
/// Original LLVM type
|
2014-03-28 17:05:27 +00:00
|
|
|
pub ty: Type,
|
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>,
|
2013-09-25 10:30:44 +00:00
|
|
|
/// LLVM attribute of argument
|
2016-02-23 19:55:19 +00:00
|
|
|
pub attr: Option<llvm::Attribute>
|
2013-09-25 10:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ArgType {
|
2016-02-25 08:49:58 +00:00
|
|
|
fn new(ty: Type) -> ArgType {
|
2013-09-25 10:30:44 +00:00
|
|
|
ArgType {
|
|
|
|
kind: Direct,
|
|
|
|
ty: ty,
|
2014-03-09 06:42:22 +00:00
|
|
|
cast: None,
|
|
|
|
pad: None,
|
2016-02-25 08:49:58 +00:00
|
|
|
attr: None
|
2014-03-09 06:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 10:30:44 +00:00
|
|
|
pub fn is_indirect(&self) -> bool {
|
2016-02-25 08:49:58 +00:00
|
|
|
self.kind == 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 08:49:58 +00:00
|
|
|
self.kind == Ignore
|
2014-03-09 06:42:22 +00:00
|
|
|
}
|
2013-01-25 22:56:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 19:55:19 +00:00
|
|
|
fn c_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> Type {
|
|
|
|
if ty.is_bool() {
|
|
|
|
Type::i1(cx)
|
|
|
|
} else {
|
|
|
|
type_of::type_of(cx, ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
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-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) {
|
|
|
|
RustIntrinsic => {
|
|
|
|
// Intrinsics are emitted at the call site
|
2016-02-24 17:37:22 +00:00
|
|
|
ccx.sess().bug("asked to compute FnType of intrinsic");
|
2016-02-23 19:55:19 +00:00
|
|
|
}
|
|
|
|
PlatformIntrinsic => {
|
|
|
|
// Intrinsics are emitted at the call site
|
2016-02-24 17:37:22 +00:00
|
|
|
ccx.sess().bug("asked to compute FnType of platform intrinsic");
|
2016-02-23 19:55:19 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
System => ccx.sess().bug("system abi should be selected elsewhere"),
|
|
|
|
|
|
|
|
Stdcall => llvm::X86StdcallCallConv,
|
|
|
|
Fastcall => llvm::X86FastcallCallConv,
|
|
|
|
Vectorcall => llvm::X86_VectorCall,
|
|
|
|
C => llvm::CCallConv,
|
|
|
|
Win64 => llvm::X86_64_Win64,
|
|
|
|
|
|
|
|
// These API constants ought to be more specific...
|
|
|
|
Cdecl => llvm::CCallConv,
|
|
|
|
Aapcs => llvm::CCallConv,
|
|
|
|
};
|
|
|
|
|
|
|
|
let rty = match sig.output {
|
|
|
|
ty::FnConverging(ret_ty) if !return_type_is_void(ccx, ret_ty) => {
|
|
|
|
c_type_of(ccx, ret_ty)
|
|
|
|
}
|
|
|
|
_ => Type::void(ccx)
|
|
|
|
};
|
|
|
|
|
2016-02-24 17:37:22 +00:00
|
|
|
let mut inputs = &sig.inputs[..];
|
|
|
|
let extra_args = if abi == RustCall {
|
|
|
|
assert!(!sig.variadic && extra_args.is_empty());
|
|
|
|
|
|
|
|
match inputs[inputs.len() - 1].sty {
|
|
|
|
ty::TyTuple(ref tupled_arguments) => {
|
|
|
|
inputs = &inputs[..inputs.len() - 1];
|
|
|
|
&tupled_arguments[..]
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
unreachable!("argument to function with \"rust-call\" ABI \
|
|
|
|
is not a tuple");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert!(sig.variadic || extra_args.is_empty());
|
|
|
|
extra_args
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut args = Vec::with_capacity(inputs.len() + extra_args.len());
|
|
|
|
for ty in inputs.iter().chain(extra_args.iter()) {
|
2016-02-24 09:09:25 +00:00
|
|
|
let llty = c_type_of(ccx, ty);
|
|
|
|
if type_is_fat_ptr(ccx.tcx(), ty) {
|
2016-02-25 08:49:58 +00:00
|
|
|
args.extend(llty.field_types().into_iter().map(ArgType::new));
|
2016-02-24 09:09:25 +00:00
|
|
|
} else {
|
2016-02-25 08:49:58 +00:00
|
|
|
args.push(ArgType::new(llty));
|
2016-02-24 09:09:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 19:55:19 +00:00
|
|
|
let mut fty = FnType {
|
2016-02-24 09:09:25 +00:00
|
|
|
args: args,
|
2016-02-25 08:49:58 +00:00
|
|
|
ret: ArgType::new(rty),
|
2016-02-23 19:55:19 +00:00
|
|
|
variadic: sig.variadic,
|
|
|
|
cconv: cconv
|
|
|
|
};
|
|
|
|
|
2016-02-25 10:11:02 +00:00
|
|
|
// Add ZExt attributes to i1 arguments and returns.
|
|
|
|
for arg in Some(&mut fty.ret).into_iter().chain(&mut fty.args) {
|
|
|
|
if arg.ty == Type::i1(ccx) {
|
|
|
|
arg.attr = Some(llvm::Attribute::ZExt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 17:37:22 +00:00
|
|
|
if abi == Rust || abi == RustCall {
|
|
|
|
let fixup = |arg: &mut ArgType| {
|
|
|
|
if !arg.ty.is_aggregate() {
|
|
|
|
// Scalars and vectors, always immediate.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let size = llsize_of_alloc(ccx, arg.ty);
|
|
|
|
if size > llsize_of_alloc(ccx, ccx.int_type()) {
|
|
|
|
arg.kind = Indirect;
|
|
|
|
} 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));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let ty::FnConverging(ret_ty) = sig.output {
|
|
|
|
// Fat pointers are returned by-value.
|
|
|
|
if !type_is_fat_ptr(ccx.tcx(), ret_ty) &&
|
|
|
|
fty.ret.ty != Type::void(ccx) {
|
|
|
|
fixup(&mut fty.ret);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for arg in &mut fty.args {
|
|
|
|
fixup(arg);
|
|
|
|
}
|
|
|
|
return fty;
|
|
|
|
}
|
|
|
|
|
2016-02-23 19:55:19 +00:00
|
|
|
match &ccx.sess().target.target.arch[..] {
|
|
|
|
"x86" => cabi_x86::compute_abi_info(ccx, &mut fty),
|
|
|
|
"x86_64" => if ccx.sess().target.target.options.is_like_windows {
|
|
|
|
cabi_x86_win64::compute_abi_info(ccx, &mut fty);
|
|
|
|
} else {
|
|
|
|
cabi_x86_64::compute_abi_info(ccx, &mut fty);
|
|
|
|
},
|
|
|
|
"aarch64" => cabi_aarch64::compute_abi_info(ccx, &mut fty),
|
|
|
|
"arm" => {
|
|
|
|
let flavor = if ccx.sess().target.target.target_os == "ios" {
|
|
|
|
cabi_arm::Flavor::Ios
|
|
|
|
} else {
|
|
|
|
cabi_arm::Flavor::General
|
|
|
|
};
|
|
|
|
cabi_arm::compute_abi_info(ccx, &mut fty, flavor);
|
|
|
|
},
|
|
|
|
"mips" => cabi_mips::compute_abi_info(ccx, &mut fty),
|
|
|
|
"powerpc" => cabi_powerpc::compute_abi_info(ccx, &mut fty),
|
|
|
|
"powerpc64" => cabi_powerpc64::compute_abi_info(ccx, &mut fty),
|
|
|
|
"asmjs" => cabi_asmjs::compute_abi_info(ccx, &mut fty),
|
|
|
|
a => ccx.sess().fatal(&format!("unrecognized arch \"{}\" in target specification", a))
|
|
|
|
}
|
|
|
|
|
|
|
|
fty
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
let llreturn_ty = if self.ret.is_indirect() {
|
|
|
|
llargument_tys.push(self.ret.ty.ptr_to());
|
|
|
|
Type::void(ccx)
|
2014-07-23 18:56:36 +00:00
|
|
|
} else {
|
2016-02-23 19:55:19 +00:00
|
|
|
self.ret.cast.unwrap_or(self.ret.ty)
|
|
|
|
};
|
|
|
|
|
|
|
|
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() {
|
|
|
|
arg.ty.ptr_to()
|
2015-01-09 16:18:23 +00:00
|
|
|
} else {
|
2016-02-23 19:55:19 +00:00
|
|
|
arg.cast.unwrap_or(arg.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 10:11:02 +00:00
|
|
|
pub fn llvm_attrs(&self, ccx: &CrateContext) -> llvm::AttrBuilder {
|
|
|
|
let mut attrs = llvm::AttrBuilder::new();
|
|
|
|
let mut i = if self.ret.is_indirect() { 1 } else { 0 };
|
|
|
|
|
|
|
|
// Add attributes that are always applicable, independent of the concrete foreign ABI
|
|
|
|
if self.ret.is_indirect() {
|
|
|
|
let llret_sz = llsize_of_real(ccx, self.ret.ty);
|
|
|
|
|
|
|
|
// The outptr can be noalias and nocapture because it's entirely
|
|
|
|
// invisible to the program. We also know it's nonnull as well
|
|
|
|
// as how many bytes we can dereference
|
|
|
|
attrs.arg(i, llvm::Attribute::StructRet)
|
|
|
|
.arg(i, llvm::Attribute::NoAlias)
|
|
|
|
.arg(i, llvm::Attribute::NoCapture)
|
|
|
|
.arg(i, llvm::DereferenceableAttribute(llret_sz));
|
2016-02-23 19:55:19 +00:00
|
|
|
};
|
|
|
|
|
2016-02-25 10:11:02 +00:00
|
|
|
// Add attributes that depend on the concrete foreign ABI
|
2016-02-23 19:55:19 +00:00
|
|
|
if let Some(attr) = self.ret.attr {
|
2016-02-25 10:11:02 +00:00
|
|
|
attrs.arg(i, attr);
|
2016-02-23 19:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
for arg in &self.args {
|
|
|
|
if arg.is_ignore() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// skip padding
|
|
|
|
if arg.pad.is_some() { i += 1; }
|
|
|
|
|
|
|
|
if let Some(attr) = arg.attr {
|
2016-02-25 10:11:02 +00:00
|
|
|
attrs.arg(i, attr);
|
2016-02-23 19:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
2016-02-25 10:11:02 +00:00
|
|
|
attrs
|
2013-01-25 22:56:56 +00:00
|
|
|
}
|
|
|
|
}
|