]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/cabi.rs
0ff5264c00f0f8346ebd9d3180affdf7787c9216
[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_powerpc;
22 use trans::cabi_mips;
23 use trans::type_::Type;
24
25 #[derive(Clone, Copy, PartialEq)]
26 pub enum ArgKind {
27     /// Pass the argument directly using the normal converted
28     /// LLVM type or by coercing to another specified type
29     Direct,
30     /// Pass the argument indirectly via a hidden pointer
31     Indirect,
32     /// Ignore the argument (useful for empty struct)
33     Ignore,
34 }
35
36 /// Information about how a specific C type
37 /// should be passed to or returned from a function
38 ///
39 /// This is borrowed from clang's ABIInfo.h
40 #[derive(Clone, Copy)]
41 pub struct ArgType {
42     pub kind: ArgKind,
43     /// Original LLVM type
44     pub ty: Type,
45     /// Coerced LLVM Type
46     pub cast: option::Option<Type>,
47     /// Dummy argument, which is emitted before the real argument
48     pub pad: option::Option<Type>,
49     /// LLVM attribute of argument
50     pub attr: option::Option<Attribute>
51 }
52
53 impl ArgType {
54     pub fn direct(ty: Type, cast: option::Option<Type>,
55                             pad: option::Option<Type>,
56                             attr: option::Option<Attribute>) -> ArgType {
57         ArgType {
58             kind: Direct,
59             ty: ty,
60             cast: cast,
61             pad: pad,
62             attr: attr
63         }
64     }
65
66     pub fn indirect(ty: Type, attr: option::Option<Attribute>) -> ArgType {
67         ArgType {
68             kind: Indirect,
69             ty: ty,
70             cast: option::Option::None,
71             pad: option::Option::None,
72             attr: attr
73         }
74     }
75
76     pub fn ignore(ty: Type) -> ArgType {
77         ArgType {
78             kind: Ignore,
79             ty: ty,
80             cast: None,
81             pad: None,
82             attr: None,
83         }
84     }
85
86     pub fn is_indirect(&self) -> bool {
87         return self.kind == Indirect;
88     }
89
90     pub fn is_ignore(&self) -> bool {
91         return self.kind == Ignore;
92     }
93 }
94
95 /// Metadata describing how the arguments to a native function
96 /// should be passed in order to respect the native ABI.
97 ///
98 /// I will do my best to describe this structure, but these
99 /// comments are reverse-engineered and may be inaccurate. -NDM
100 pub struct FnType {
101     /// The LLVM types of each argument.
102     pub arg_tys: Vec<ArgType> ,
103
104     /// LLVM return type.
105     pub ret_ty: ArgType,
106 }
107
108 pub fn compute_abi_info(ccx: &CrateContext,
109                         atys: &[Type],
110                         rty: Type,
111                         ret_def: bool) -> FnType {
112     match &ccx.sess().target.target.arch[..] {
113         "x86" => cabi_x86::compute_abi_info(ccx, atys, rty, ret_def),
114         "x86_64" => if ccx.sess().target.target.options.is_like_windows {
115             cabi_x86_win64::compute_abi_info(ccx, atys, rty, ret_def)
116         } else {
117             cabi_x86_64::compute_abi_info(ccx, atys, rty, ret_def)
118         },
119         "aarch64" => cabi_aarch64::compute_abi_info(ccx, atys, rty, ret_def),
120         "arm" => {
121             let flavor = if ccx.sess().target.target.target_os == "ios" {
122                 cabi_arm::Flavor::Ios
123             } else {
124                 cabi_arm::Flavor::General
125             };
126             cabi_arm::compute_abi_info(ccx, atys, rty, ret_def, flavor)
127         },
128         "mips" => cabi_mips::compute_abi_info(ccx, atys, rty, ret_def),
129         "powerpc" => cabi_powerpc::compute_abi_info(ccx, atys, rty, ret_def),
130         a => ccx.sess().fatal(&format!("unrecognized arch \"{}\" in target specification", a)
131                               ),
132     }
133 }