]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/cabi_x86.rs
Auto merge of #43651 - petrochenkov:foreign-life, r=eddyb
[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 abi::{ArgAttribute, FnType, LayoutExt, Reg, RegKind};
12 use common::CrateContext;
13
14 #[derive(PartialEq)]
15 pub enum Flavor {
16     General,
17     Fastcall
18 }
19
20 pub fn compute_abi_info<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
21                                   fty: &mut FnType<'tcx>,
22                                   flavor: Flavor) {
23     if !fty.ret.is_ignore() {
24         if fty.ret.layout.is_aggregate() {
25             // Returning a structure. Most often, this will use
26             // a hidden first argument. On some platforms, though,
27             // small structs are returned as integers.
28             //
29             // Some links:
30             // http://www.angelcode.com/dev/callconv/callconv.html
31             // Clang's ABI handling is in lib/CodeGen/TargetInfo.cpp
32             let t = &ccx.sess().target.target;
33             if t.options.is_like_osx || t.options.is_like_windows
34                 || t.options.is_like_openbsd {
35                 let size = fty.ret.layout.size(ccx);
36                 match size.bytes() {
37                     1 => fty.ret.cast_to(ccx, Reg::i8()),
38                     2 => fty.ret.cast_to(ccx, Reg::i16()),
39                     4 => fty.ret.cast_to(ccx, Reg::i32()),
40                     8 => fty.ret.cast_to(ccx, Reg::i64()),
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.layout.is_aggregate() {
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             // At this point we know this must be a primitive of sorts.
77             let unit = arg.layout.homogeneous_aggregate(ccx).unwrap();
78             let size = arg.layout.size(ccx);
79             assert_eq!(unit.size, size);
80             if unit.kind == RegKind::Float {
81                 continue;
82             }
83
84             let size_in_regs = (size.bits() + 31) / 32;
85
86             if size_in_regs == 0 {
87                 continue;
88             }
89
90             if size_in_regs > free_regs {
91                 break;
92             }
93
94             free_regs -= size_in_regs;
95
96             if size.bits() <= 32 && unit.kind == RegKind::Integer {
97                 arg.attrs.set(ArgAttribute::InReg);
98             }
99
100             if free_regs == 0 {
101                 break;
102             }
103         }
104     }
105 }