]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/cabi.rs
bb05221ae10190880eb1add628ad1438fe90c3a4
[rust.git] / src / librustc / middle / trans / cabi.rs
1 // Copyright 2012 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 use lib::llvm::Attribute;
12 use std::option;
13 use middle::trans::context::CrateContext;
14 use middle::trans::cabi_x86;
15 use middle::trans::cabi_x86_64;
16 use middle::trans::cabi_arm;
17 use middle::trans::cabi_mips;
18 use middle::trans::type_::Type;
19 use syntax::abi::{X86, X86_64, Arm, Mips};
20
21 #[deriving(Clone, Eq)]
22 pub enum ArgKind {
23     /// Pass the argument directly using the normal converted
24     /// LLVM type or by coercing to another specified type
25     Direct,
26     /// Pass the argument indirectly via a hidden pointer
27     Indirect
28 }
29
30 /// Information about how a specific C type
31 /// should be passed to or returned from a function
32 ///
33 /// This is borrowed from clang's ABIInfo.h
34 #[deriving(Clone)]
35 pub struct ArgType {
36     kind: ArgKind,
37     /// Original LLVM type
38     ty: Type,
39     /// Coerced LLVM Type
40     cast: option::Option<Type>,
41     /// Dummy argument, which is emitted before the real argument
42     pad: option::Option<Type>,
43     /// LLVM attribute of argument
44     attr: option::Option<Attribute>
45 }
46
47 impl ArgType {
48     pub fn direct(ty: Type, cast: option::Option<Type>,
49                             pad: option::Option<Type>,
50                             attr: option::Option<Attribute>) -> ArgType {
51         ArgType {
52             kind: Direct,
53             ty: ty,
54             cast: cast,
55             pad: pad,
56             attr: attr
57         }
58     }
59
60     pub fn indirect(ty: Type, attr: option::Option<Attribute>) -> ArgType {
61         ArgType {
62             kind: Indirect,
63             ty: ty,
64             cast: option::None,
65             pad: option::None,
66             attr: attr
67         }
68     }
69
70     pub fn is_direct(&self) -> bool {
71         return self.kind == Direct;
72     }
73
74     pub fn is_indirect(&self) -> bool {
75         return self.kind == Indirect;
76     }
77 }
78
79 /// Metadata describing how the arguments to a native function
80 /// should be passed in order to respect the native ABI.
81 ///
82 /// I will do my best to describe this structure, but these
83 /// comments are reverse-engineered and may be inaccurate. -NDM
84 pub struct FnType {
85     /// The LLVM types of each argument.
86     arg_tys: ~[ArgType],
87
88     /// LLVM return type.
89     ret_ty: ArgType,
90 }
91
92 pub fn compute_abi_info(ccx: &CrateContext,
93                         atys: &[Type],
94                         rty: Type,
95                         ret_def: bool) -> FnType {
96     match ccx.sess.targ_cfg.arch {
97         X86 => cabi_x86::compute_abi_info(ccx, atys, rty, ret_def),
98         X86_64 => cabi_x86_64::compute_abi_info(ccx, atys, rty, ret_def),
99         Arm => cabi_arm::compute_abi_info(ccx, atys, rty, ret_def),
100         Mips => cabi_mips::compute_abi_info(ccx, atys, rty, ret_def),
101     }
102 }