]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/cabi_powerpc64.rs
Auto merge of #43651 - petrochenkov:foreign-life, r=eddyb
[rust.git] / src / librustc_trans / cabi_powerpc64.rs
1 // Copyright 2014-2016 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 // FIXME:
12 // Alignment of 128 bit types is not currently handled, this will
13 // need to be fixed when PowerPC vector support is added.
14
15 use abi::{FnType, ArgType, LayoutExt, Reg, RegKind, Uniform};
16 use context::CrateContext;
17
18 fn is_homogeneous_aggregate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, arg: &mut ArgType<'tcx>)
19                                      -> Option<Uniform> {
20     arg.layout.homogeneous_aggregate(ccx).and_then(|unit| {
21         let size = arg.layout.size(ccx);
22
23         // Ensure we have at most eight uniquely addressable members.
24         if size > unit.size.checked_mul(8, ccx).unwrap() {
25             return None;
26         }
27
28         let valid_unit = match unit.kind {
29             RegKind::Integer => false,
30             RegKind::Float => true,
31             RegKind::Vector => size.bits() == 128
32         };
33
34         if valid_unit {
35             Some(Uniform {
36                 unit,
37                 total: size
38             })
39         } else {
40             None
41         }
42     })
43 }
44
45 fn classify_ret_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ret: &mut ArgType<'tcx>) {
46     if !ret.layout.is_aggregate() {
47         ret.extend_integer_width_to(64);
48         return;
49     }
50
51     // The PowerPC64 big endian ABI doesn't return aggregates in registers
52     if ccx.sess().target.target.target_endian == "big" {
53         ret.make_indirect(ccx);
54     }
55
56     if let Some(uniform) = is_homogeneous_aggregate(ccx, ret) {
57         ret.cast_to(ccx, uniform);
58         return;
59     }
60     let size = ret.layout.size(ccx);
61     let bits = size.bits();
62     if bits <= 128 {
63         let unit = if bits <= 8 {
64             Reg::i8()
65         } else if bits <= 16 {
66             Reg::i16()
67         } else if bits <= 32 {
68             Reg::i32()
69         } else {
70             Reg::i64()
71         };
72
73         ret.cast_to(ccx, Uniform {
74             unit,
75             total: size
76         });
77         return;
78     }
79
80     ret.make_indirect(ccx);
81 }
82
83 fn classify_arg_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, arg: &mut ArgType<'tcx>) {
84     if !arg.layout.is_aggregate() {
85         arg.extend_integer_width_to(64);
86         return;
87     }
88
89     if let Some(uniform) = is_homogeneous_aggregate(ccx, arg) {
90         arg.cast_to(ccx, uniform);
91         return;
92     }
93
94     let total = arg.layout.size(ccx);
95     arg.cast_to(ccx, Uniform {
96         unit: Reg::i64(),
97         total
98     });
99 }
100
101 pub fn compute_abi_info<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fty: &mut FnType<'tcx>) {
102     if !fty.ret.is_ignore() {
103         classify_ret_ty(ccx, &mut fty.ret);
104     }
105
106     for arg in &mut fty.args {
107         if arg.is_ignore() { continue; }
108         classify_arg_ty(ccx, arg);
109     }
110 }