]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/cabi_sparc64.rs
rustc_trans: use ty::layout for ABI computation instead of LLVM types.
[rust.git] / src / librustc_trans / cabi_sparc64.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: This needs an audit for correctness and completeness.
12
13 use abi::{FnType, ArgType, LayoutExt, Reg, RegKind, Uniform};
14 use context::CrateContext;
15
16 fn is_homogenous_aggregate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, arg: &mut ArgType<'tcx>)
17                                      -> Option<Uniform> {
18     arg.layout.homogenous_aggregate(ccx).and_then(|unit| {
19         let size = arg.layout.size(ccx);
20
21         // Ensure we have at most eight uniquely addressable members.
22         if size > unit.size.checked_mul(8, ccx).unwrap() {
23             return None;
24         }
25
26         let valid_unit = match unit.kind {
27             RegKind::Integer => false,
28             RegKind::Float => true,
29             RegKind::Vector => size.bits() == 128
30         };
31
32         if valid_unit {
33             Some(Uniform {
34                 unit,
35                 total: size
36             })
37         } else {
38             None
39         }
40     })
41 }
42
43 fn classify_ret_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ret: &mut ArgType<'tcx>) {
44     if !ret.layout.is_aggregate() {
45         ret.extend_integer_width_to(64);
46         return;
47     }
48
49     if let Some(uniform) = is_homogenous_aggregate(ccx, ret) {
50         ret.cast_to(ccx, uniform);
51         return;
52     }
53     let size = ret.layout.size(ccx);
54     let bits = size.bits();
55     if bits <= 128 {
56         let unit = if bits <= 8 {
57             Reg::i8()
58         } else if bits <= 16 {
59             Reg::i16()
60         } else if bits <= 32 {
61             Reg::i32()
62         } else {
63             Reg::i64()
64         };
65
66         ret.cast_to(ccx, Uniform {
67             unit,
68             total: size
69         });
70         return;
71     }
72
73     // don't return aggregates in registers
74     ret.make_indirect(ccx);
75 }
76
77 fn classify_arg_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, arg: &mut ArgType<'tcx>) {
78     if !arg.layout.is_aggregate() {
79         arg.extend_integer_width_to(64);
80         return;
81     }
82
83     if let Some(uniform) = is_homogenous_aggregate(ccx, arg) {
84         arg.cast_to(ccx, uniform);
85         return;
86     }
87
88     let total = arg.layout.size(ccx);
89     arg.cast_to(ccx, Uniform {
90         unit: Reg::i64(),
91         total
92     });
93 }
94
95 pub fn compute_abi_info<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fty: &mut FnType<'tcx>) {
96     if !fty.ret.is_ignore() {
97         classify_ret_ty(ccx, &mut fty.ret);
98     }
99
100     for arg in &mut fty.args {
101         if arg.is_ignore() { continue; }
102         classify_arg_ty(ccx, arg);
103     }
104 }