2014-12-12 23:39:27 +00:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2013-01-25 14:56:56 -08: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 00:05:53 -08:00
|
|
|
pub use self::ArgKind::*;
|
|
|
|
|
2014-07-07 17:58:01 -07:00
|
|
|
use llvm::Attribute;
|
2013-06-28 18:32:26 -04:00
|
|
|
use std::option;
|
2014-11-15 20:30:33 -05: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-09 20:13:23 -08:00
|
|
|
use trans::cabi_powerpc;
|
2015-12-28 21:09:06 +00:00
|
|
|
use trans::cabi_powerpc64;
|
2014-11-15 20:30:33 -05:00
|
|
|
use trans::cabi_mips;
|
|
|
|
use trans::type_::Type;
|
2013-01-25 14:56:56 -08:00
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
2013-09-25 18:30:44 +08: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 15:42:22 +09:00
|
|
|
Indirect,
|
|
|
|
/// Ignore the argument (useful for empty struct)
|
|
|
|
Ignore,
|
2013-09-25 18:30:44 +08: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
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, Copy)]
|
2013-09-25 18:30:44 +08:00
|
|
|
pub struct ArgType {
|
2014-03-28 10:05:27 -07:00
|
|
|
pub kind: ArgKind,
|
2013-09-25 18:30:44 +08:00
|
|
|
/// Original LLVM type
|
2014-03-28 10:05:27 -07:00
|
|
|
pub ty: Type,
|
2013-09-25 18:30:44 +08:00
|
|
|
/// Coerced LLVM Type
|
2014-03-28 10:05:27 -07:00
|
|
|
pub cast: option::Option<Type>,
|
2013-09-25 18:30:44 +08:00
|
|
|
/// Dummy argument, which is emitted before the real argument
|
2014-03-28 10:05:27 -07:00
|
|
|
pub pad: option::Option<Type>,
|
2013-09-25 18:30:44 +08:00
|
|
|
/// LLVM attribute of argument
|
2014-03-28 10:05:27 -07:00
|
|
|
pub attr: option::Option<Attribute>
|
2013-09-25 18:30:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ArgType {
|
|
|
|
pub fn direct(ty: Type, cast: option::Option<Type>,
|
|
|
|
pad: option::Option<Type>,
|
|
|
|
attr: option::Option<Attribute>) -> ArgType {
|
|
|
|
ArgType {
|
|
|
|
kind: Direct,
|
|
|
|
ty: ty,
|
|
|
|
cast: cast,
|
|
|
|
pad: pad,
|
|
|
|
attr: attr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn indirect(ty: Type, attr: option::Option<Attribute>) -> ArgType {
|
|
|
|
ArgType {
|
|
|
|
kind: Indirect,
|
|
|
|
ty: ty,
|
2014-11-28 11:57:41 -05:00
|
|
|
cast: option::Option::None,
|
|
|
|
pad: option::Option::None,
|
2013-09-25 18:30:44 +08:00
|
|
|
attr: attr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-09 15:42:22 +09:00
|
|
|
pub fn ignore(ty: Type) -> ArgType {
|
|
|
|
ArgType {
|
|
|
|
kind: Ignore,
|
|
|
|
ty: ty,
|
|
|
|
cast: None,
|
|
|
|
pad: None,
|
|
|
|
attr: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 18:30:44 +08:00
|
|
|
pub fn is_indirect(&self) -> bool {
|
|
|
|
return self.kind == Indirect;
|
|
|
|
}
|
2014-03-09 15:42:22 +09:00
|
|
|
|
|
|
|
pub fn is_ignore(&self) -> bool {
|
|
|
|
return self.kind == Ignore;
|
|
|
|
}
|
2013-01-25 14:56:56 -08:00
|
|
|
}
|
|
|
|
|
2013-05-21 15:25:44 -04: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 11:46:19 -08:00
|
|
|
pub struct FnType {
|
2013-09-25 18:30:44 +08:00
|
|
|
/// The LLVM types of each argument.
|
2014-03-28 10:05:27 -07:00
|
|
|
pub arg_tys: Vec<ArgType> ,
|
2013-01-25 14:56:56 -08:00
|
|
|
|
2013-05-21 15:25:44 -04:00
|
|
|
/// LLVM return type.
|
2014-03-28 10:05:27 -07:00
|
|
|
pub ret_ty: ArgType,
|
2013-05-21 15:25:44 -04:00
|
|
|
}
|
2013-04-18 15:53:29 -07:00
|
|
|
|
2013-12-19 16:47:15 -08:00
|
|
|
pub fn compute_abi_info(ccx: &CrateContext,
|
2013-05-21 15:25:44 -04:00
|
|
|
atys: &[Type],
|
|
|
|
rty: Type,
|
|
|
|
ret_def: bool) -> FnType {
|
2015-02-20 14:08:14 -05:00
|
|
|
match &ccx.sess().target.target.arch[..] {
|
2014-07-23 11:56:36 -07:00
|
|
|
"x86" => cabi_x86::compute_abi_info(ccx, atys, rty, ret_def),
|
|
|
|
"x86_64" => if ccx.sess().target.target.options.is_like_windows {
|
|
|
|
cabi_x86_win64::compute_abi_info(ccx, atys, rty, ret_def)
|
|
|
|
} else {
|
|
|
|
cabi_x86_64::compute_abi_info(ccx, atys, rty, ret_def)
|
|
|
|
},
|
2014-12-12 23:39:27 +00:00
|
|
|
"aarch64" => cabi_aarch64::compute_abi_info(ccx, atys, rty, ret_def),
|
2015-01-09 18:18:23 +02:00
|
|
|
"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, atys, rty, ret_def, flavor)
|
|
|
|
},
|
2014-07-23 11:56:36 -07:00
|
|
|
"mips" => cabi_mips::compute_abi_info(ccx, atys, rty, ret_def),
|
2015-01-09 20:13:23 -08:00
|
|
|
"powerpc" => cabi_powerpc::compute_abi_info(ccx, atys, rty, ret_def),
|
2016-01-30 13:27:00 -08:00
|
|
|
"powerpc64" => cabi_powerpc64::compute_abi_info(ccx, atys, rty, ret_def),
|
2015-01-07 11:58:31 -05:00
|
|
|
a => ccx.sess().fatal(&format!("unrecognized arch \"{}\" in target specification", a)
|
2015-02-20 14:08:14 -05:00
|
|
|
),
|
2013-01-25 14:56:56 -08:00
|
|
|
}
|
|
|
|
}
|