]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/cabi.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / librustc_trans / trans / cabi.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // 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
11 pub use self::ArgKind::*;
12
13 use llvm::Attribute;
14 use std::option;
15 use trans::context::CrateContext;
16 use trans::cabi_x86;
17 use trans::cabi_x86_64;
18 use trans::cabi_x86_win64;
19 use trans::cabi_arm;
20 use trans::cabi_aarch64;
21 use trans::cabi_mips;
22 use trans::type_::Type;
23
24 #[derive(Clone, Copy, PartialEq)]
25 pub 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
30     Indirect,
31     /// Ignore the argument (useful for empty struct)
32     Ignore,
33 }
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
39 #[derive(Clone, Copy)]
40 pub struct ArgType {
41     pub kind: ArgKind,
42     /// Original LLVM type
43     pub ty: Type,
44     /// Coerced LLVM Type
45     pub cast: option::Option<Type>,
46     /// Dummy argument, which is emitted before the real argument
47     pub pad: option::Option<Type>,
48     /// LLVM attribute of argument
49     pub attr: option::Option<Attribute>
50 }
51
52 impl 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,
69             cast: option::Option::None,
70             pad: option::Option::None,
71             attr: attr
72         }
73     }
74
75     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
85     pub fn is_indirect(&self) -> bool {
86         return self.kind == Indirect;
87     }
88
89     pub fn is_ignore(&self) -> bool {
90         return self.kind == Ignore;
91     }
92 }
93
94 /// 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
99 pub struct FnType {
100     /// The LLVM types of each argument.
101     pub arg_tys: Vec<ArgType> ,
102
103     /// LLVM return type.
104     pub ret_ty: ArgType,
105 }
106
107 pub fn compute_abi_info(ccx: &CrateContext,
108                         atys: &[Type],
109                         rty: Type,
110                         ret_def: bool) -> FnType {
111     match ccx.sess().target.target.arch[] {
112         "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),
119         "aarch64" => cabi_aarch64::compute_abi_info(ccx, atys, rty, ret_def),
120         "mips" => cabi_mips::compute_abi_info(ccx, atys, rty, ret_def),
121         a => ccx.sess().fatal((format!("unrecognized arch \"{}\" in target specification", a))
122                               []),
123     }
124 }