]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/cabi_x86.rs
616dc0703a73e7616c4e8369c9fab3a4ba6c1c47
[rust.git] / src / librustc / middle / trans / cabi_x86.rs
1 // Copyright 2013 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
12 use syntax::abi::{OsWin32, OsMacos};
13 use lib::llvm::*;
14 use super::cabi::*;
15 use super::common::*;
16 use super::machine::*;
17 use middle::trans::type_::Type;
18
19 pub fn compute_abi_info(ccx: &CrateContext,
20                         atys: &[Type],
21                         rty: Type,
22                         ret_def: bool) -> FnType {
23     let mut arg_tys = ~[];
24
25     let ret_ty;
26     if !ret_def {
27         ret_ty = ArgType::direct(Type::void(), None, None, None);
28     } else if rty.kind() == Struct {
29         // Returning a structure. Most often, this will use
30         // a hidden first argument. On some platforms, though,
31         // small structs are returned as integers.
32         //
33         // Some links:
34         // http://www.angelcode.com/dev/callconv/callconv.html
35         // Clang's ABI handling is in lib/CodeGen/TargetInfo.cpp
36
37         enum Strategy { RetValue(Type), RetPointer }
38         let strategy = match ccx.sess.targ_cfg.os {
39             OsWin32 | OsMacos => {
40                 match llsize_of_alloc(ccx, rty) {
41                     1 => RetValue(Type::i8()),
42                     2 => RetValue(Type::i16()),
43                     4 => RetValue(Type::i32()),
44                     8 => RetValue(Type::i64()),
45                     _ => RetPointer
46                 }
47             }
48             _ => {
49                 RetPointer
50             }
51         };
52
53         match strategy {
54             RetValue(t) => {
55                 ret_ty = ArgType::direct(rty, Some(t), None, None);
56             }
57             RetPointer => {
58                 ret_ty = ArgType::indirect(rty, Some(StructRetAttribute));
59             }
60         }
61     } else {
62         ret_ty = ArgType::direct(rty, None, None, None);
63     }
64
65     for &a in atys.iter() {
66         arg_tys.push(ArgType::direct(a, None, None, None));
67     }
68
69     return FnType {
70         arg_tys: arg_tys,
71         ret_ty: ret_ty,
72     };
73 }