2013-01-25 16:56:56 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
use lib::llvm::Attribute;
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::option;
|
2013-05-21 14:25:44 -05:00
|
|
|
use middle::trans::context::CrateContext;
|
|
|
|
use middle::trans::cabi_x86;
|
|
|
|
use middle::trans::cabi_x86_64;
|
|
|
|
use middle::trans::cabi_arm;
|
|
|
|
use middle::trans::cabi_mips;
|
|
|
|
use middle::trans::type_::Type;
|
2014-03-08 14:36:22 -06:00
|
|
|
use std::vec_ng::Vec;
|
2013-05-21 14:25:44 -05:00
|
|
|
use syntax::abi::{X86, X86_64, Arm, Mips};
|
2013-01-25 16:56:56 -06:00
|
|
|
|
2013-09-25 05:30:44 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
|
|
|
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
|
|
|
|
Indirect
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Information about how a specific C type
|
|
|
|
/// should be passed to or returned from a function
|
|
|
|
///
|
|
|
|
/// This is borrowed from clang's ABIInfo.h
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone)]
|
2013-09-25 05:30:44 -05:00
|
|
|
pub struct ArgType {
|
|
|
|
kind: ArgKind,
|
|
|
|
/// Original LLVM type
|
|
|
|
ty: Type,
|
|
|
|
/// Coerced LLVM Type
|
|
|
|
cast: option::Option<Type>,
|
|
|
|
/// Dummy argument, which is emitted before the real argument
|
|
|
|
pad: option::Option<Type>,
|
|
|
|
/// LLVM attribute of argument
|
|
|
|
attr: option::Option<Attribute>
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
cast: option::None,
|
|
|
|
pad: option::None,
|
|
|
|
attr: attr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_direct(&self) -> bool {
|
|
|
|
return self.kind == Direct;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_indirect(&self) -> bool {
|
|
|
|
return self.kind == Indirect;
|
|
|
|
}
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
|
2013-05-21 14:25:44 -05: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 13:46:19 -06:00
|
|
|
pub struct FnType {
|
2013-09-25 05:30:44 -05:00
|
|
|
/// The LLVM types of each argument.
|
2014-03-04 12:02:49 -06:00
|
|
|
arg_tys: Vec<ArgType> ,
|
2013-01-25 16:56:56 -06:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
/// LLVM return type.
|
2013-09-25 05:30:44 -05:00
|
|
|
ret_ty: ArgType,
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn compute_abi_info(ccx: &CrateContext,
|
2013-05-21 14:25:44 -05:00
|
|
|
atys: &[Type],
|
|
|
|
rty: Type,
|
|
|
|
ret_def: bool) -> FnType {
|
|
|
|
match ccx.sess.targ_cfg.arch {
|
|
|
|
X86 => cabi_x86::compute_abi_info(ccx, atys, rty, ret_def),
|
|
|
|
X86_64 => cabi_x86_64::compute_abi_info(ccx, atys, rty, ret_def),
|
|
|
|
Arm => cabi_arm::compute_abi_info(ccx, atys, rty, ret_def),
|
|
|
|
Mips => cabi_mips::compute_abi_info(ccx, atys, rty, ret_def),
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
}
|