]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/cabi_x86.rs
fea005f3d77da388194cdaf7a9f4d1734b544dd0
[rust.git] / src / librustc_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 use llvm::*;
12 use abi::{ArgAttribute, FnType};
13 use type_::Type;
14 use super::common::*;
15 use super::machine::*;
16
17 #[derive(PartialEq)]
18 pub enum Flavor {
19     General,
20     Fastcall
21 }
22
23 pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType, flavor: Flavor) {
24     if !fty.ret.is_ignore() {
25         if fty.ret.ty.kind() == Struct {
26             // Returning a structure. Most often, this will use
27             // a hidden first argument. On some platforms, though,
28             // small structs are returned as integers.
29             //
30             // Some links:
31             // http://www.angelcode.com/dev/callconv/callconv.html
32             // Clang's ABI handling is in lib/CodeGen/TargetInfo.cpp
33             let t = &ccx.sess().target.target;
34             if t.options.is_like_osx || t.options.is_like_windows
35                 || t.options.is_like_openbsd {
36                 match llsize_of_alloc(ccx, fty.ret.ty) {
37                     1 => fty.ret.cast = Some(Type::i8(ccx)),
38                     2 => fty.ret.cast = Some(Type::i16(ccx)),
39                     4 => fty.ret.cast = Some(Type::i32(ccx)),
40                     8 => fty.ret.cast = Some(Type::i64(ccx)),
41                     _ => fty.ret.make_indirect(ccx)
42                 }
43             } else {
44                 fty.ret.make_indirect(ccx);
45             }
46         } else {
47             fty.ret.extend_integer_width_to(32);
48         }
49     }
50
51     for arg in &mut fty.args {
52         if arg.is_ignore() { continue; }
53         if arg.ty.kind() == Struct {
54             arg.make_indirect(ccx);
55             arg.attrs.set(ArgAttribute::ByVal);
56         } else {
57             arg.extend_integer_width_to(32);
58         }
59     }
60
61     if flavor == Flavor::Fastcall {
62         // Mark arguments as InReg like clang does it,
63         // so our fastcall is compatible with C/C++ fastcall.
64
65         // Clang reference: lib/CodeGen/TargetInfo.cpp
66         // See X86_32ABIInfo::shouldPrimitiveUseInReg(), X86_32ABIInfo::updateFreeRegs()
67
68         // IsSoftFloatABI is only set to true on ARM platforms,
69         // which in turn can't be x86?
70
71         let mut free_regs = 2;
72
73         for arg in &mut fty.args {
74             if arg.is_ignore() || arg.is_indirect() { continue; }
75
76             if arg.ty.kind() == Float {
77                 continue;
78             }
79
80             let size = llbitsize_of_real(ccx, arg.ty);
81             let size_in_regs = (size + 31) / 32;
82
83             if size_in_regs == 0 {
84                 continue;
85             }
86
87             if size_in_regs > free_regs {
88                 break;
89             }
90
91             free_regs -= size_in_regs;
92
93             if size <= 32 && (arg.ty.kind() == Pointer || arg.ty.kind() == Integer) {
94                 arg.attrs.set(ArgAttribute::InReg);
95             }
96
97             if free_regs == 0 {
98                 break;
99             }
100         }
101     }
102 }