blob: 8a2a2534cabd2c51a7cbcc2cf4063412ca81d119 [file] [log] [blame]
Akos Kiss6e5fb8b2014-12-12 23:39:271// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
Tim Chevalier41adf9d2013-01-25 22:56:562// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
Steven Fackler3dcd2152014-11-06 08:05:5311pub use self::ArgKind::*;
12
Brian Anderson3096d9b2014-07-08 00:58:0113use llvm::Attribute;
Corey Richardson1662bd32013-06-28 22:32:2614use std::option;
Niko Matsakisdc6e4142014-11-16 01:30:3315use trans::context::CrateContext;
16use trans::cabi_x86;
17use trans::cabi_x86_64;
18use trans::cabi_x86_win64;
19use trans::cabi_arm;
Akos Kiss6e5fb8b2014-12-12 23:39:2720use trans::cabi_aarch64;
Niko Matsakisdc6e4142014-11-16 01:30:3321use trans::cabi_mips;
22use trans::type_::Type;
Tim Chevalier41adf9d2013-01-25 22:56:5623
Jorge Aparicio351409a2015-01-04 03:54:1824#[derive(Clone, Copy, PartialEq)]
Jyun-Yan You95fc31a2013-09-25 10:30:4425pub enum ArgKind {
26 /// Pass the argument directly using the normal converted
27 /// LLVM type or by coercing to another specified type
28 Direct,
29 /// Pass the argument indirectly via a hidden pointer
klutzy2d31bca2014-03-09 06:42:2230 Indirect,
31 /// Ignore the argument (useful for empty struct)
32 Ignore,
Jyun-Yan You95fc31a2013-09-25 10:30:4433}
34
35/// Information about how a specific C type
36/// should be passed to or returned from a function
37///
38/// This is borrowed from clang's ABIInfo.h
Jorge Aparicio351409a2015-01-04 03:54:1839#[derive(Clone, Copy)]
Jyun-Yan You95fc31a2013-09-25 10:30:4440pub struct ArgType {
Alex Crichton89fa1412014-03-28 17:05:2741 pub kind: ArgKind,
Jyun-Yan You95fc31a2013-09-25 10:30:4442 /// Original LLVM type
Alex Crichton89fa1412014-03-28 17:05:2743 pub ty: Type,
Jyun-Yan You95fc31a2013-09-25 10:30:4444 /// Coerced LLVM Type
Alex Crichton89fa1412014-03-28 17:05:2745 pub cast: option::Option<Type>,
Jyun-Yan You95fc31a2013-09-25 10:30:4446 /// Dummy argument, which is emitted before the real argument
Alex Crichton89fa1412014-03-28 17:05:2747 pub pad: option::Option<Type>,
Jyun-Yan You95fc31a2013-09-25 10:30:4448 /// LLVM attribute of argument
Alex Crichton89fa1412014-03-28 17:05:2749 pub attr: option::Option<Attribute>
Jyun-Yan You95fc31a2013-09-25 10:30:4450}
51
52impl ArgType {
53 pub fn direct(ty: Type, cast: option::Option<Type>,
54 pad: option::Option<Type>,
55 attr: option::Option<Attribute>) -> ArgType {
56 ArgType {
57 kind: Direct,
58 ty: ty,
59 cast: cast,
60 pad: pad,
61 attr: attr
62 }
63 }
64
65 pub fn indirect(ty: Type, attr: option::Option<Attribute>) -> ArgType {
66 ArgType {
67 kind: Indirect,
68 ty: ty,
Corey Farwell4ef16742014-11-28 16:57:4169 cast: option::Option::None,
70 pad: option::Option::None,
Jyun-Yan You95fc31a2013-09-25 10:30:4471 attr: attr
72 }
73 }
74
klutzy2d31bca2014-03-09 06:42:2275 pub fn ignore(ty: Type) -> ArgType {
76 ArgType {
77 kind: Ignore,
78 ty: ty,
79 cast: None,
80 pad: None,
81 attr: None,
82 }
83 }
84
Jyun-Yan You95fc31a2013-09-25 10:30:4485 pub fn is_indirect(&self) -> bool {
86 return self.kind == Indirect;
87 }
klutzy2d31bca2014-03-09 06:42:2288
89 pub fn is_ignore(&self) -> bool {
90 return self.kind == Ignore;
91 }
Tim Chevalier41adf9d2013-01-25 22:56:5692}
93
Niko Matsakis303f6502013-05-21 19:25:4494/// Metadata describing how the arguments to a native function
95/// should be passed in order to respect the native ABI.
96///
97/// I will do my best to describe this structure, but these
98/// comments are reverse-engineered and may be inaccurate. -NDM
Patrick Waltonba11e962013-01-30 19:46:1999pub struct FnType {
Jyun-Yan You95fc31a2013-09-25 10:30:44100 /// The LLVM types of each argument.
Alex Crichton89fa1412014-03-28 17:05:27101 pub arg_tys: Vec<ArgType> ,
Niko Matsakis303f6502013-05-21 19:25:44102
103 /// LLVM return type.
Alex Crichton89fa1412014-03-28 17:05:27104 pub ret_ty: ArgType,
Tim Chevalier41adf9d2013-01-25 22:56:56105}
106
Patrick Waltonb9416772013-12-20 00:47:15107pub fn compute_abi_info(ccx: &CrateContext,
Niko Matsakis303f6502013-05-21 19:25:44108 atys: &[Type],
109 rty: Type,
110 ret_def: bool) -> FnType {
Jorge Aparicio517f1cc2015-01-07 16:58:31111 match &ccx.sess().target.target.arch[] {
Corey Richardson6b130e32014-07-23 18:56:36112 "x86" => cabi_x86::compute_abi_info(ccx, atys, rty, ret_def),
113 "x86_64" => if ccx.sess().target.target.options.is_like_windows {
114 cabi_x86_win64::compute_abi_info(ccx, atys, rty, ret_def)
115 } else {
116 cabi_x86_64::compute_abi_info(ccx, atys, rty, ret_def)
117 },
118 "arm" => cabi_arm::compute_abi_info(ccx, atys, rty, ret_def),
Akos Kiss6e5fb8b2014-12-12 23:39:27119 "aarch64" => cabi_aarch64::compute_abi_info(ccx, atys, rty, ret_def),
Corey Richardson6b130e32014-07-23 18:56:36120 "mips" => cabi_mips::compute_abi_info(ccx, atys, rty, ret_def),
Jorge Aparicio517f1cc2015-01-07 16:58:31121 a => ccx.sess().fatal(&format!("unrecognized arch \"{}\" in target specification", a)
122 []),
Tim Chevalier41adf9d2013-01-25 22:56:56123 }
124}