]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/cabi_aarch64.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_trans / cabi_aarch64.rs
1 // Copyright 2015 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 #![allow(non_upper_case_globals)]
12
13 use llvm::{Integer, Pointer, Float, Double, Struct, Array, Vector};
14 use abi::{FnType, ArgType};
15 use context::CrateContext;
16 use type_::Type;
17
18 use std::cmp;
19
20 fn align_up_to(off: usize, a: usize) -> usize {
21 return (off + a - 1) / a * a;
22 }
23
24 fn align(off: usize, ty: Type) -> usize {
25 let a = ty_align(ty);
26 return align_up_to(off, a);
27 }
28
29 fn ty_align(ty: Type) -> usize {
30 match ty.kind() {
31 Integer => ((ty.int_width() as usize) + 7) / 8,
32 Pointer => 8,
33 Float => 4,
34 Double => 8,
35 Struct => {
36 if ty.is_packed() {
37 1
38 } else {
39 let str_tys = ty.field_types();
40 str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
41 }
42 }
43 Array => {
44 let elt = ty.element_type();
45 ty_align(elt)
46 }
47 Vector => {
48 let len = ty.vector_length();
49 let elt = ty.element_type();
50 ty_align(elt) * len
51 }
52 _ => bug!("ty_align: unhandled type")
53 }
54 }
55
56 fn ty_size(ty: Type) -> usize {
57 match ty.kind() {
58 Integer => ((ty.int_width() as usize) + 7) / 8,
59 Pointer => 8,
60 Float => 4,
61 Double => 8,
62 Struct => {
63 if ty.is_packed() {
64 let str_tys = ty.field_types();
65 str_tys.iter().fold(0, |s, t| s + ty_size(*t))
66 } else {
67 let str_tys = ty.field_types();
68 let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
69 align(size, ty)
70 }
71 }
72 Array => {
73 let len = ty.array_length();
74 let elt = ty.element_type();
75 let eltsz = ty_size(elt);
76 len * eltsz
77 }
78 Vector => {
79 let len = ty.vector_length();
80 let elt = ty.element_type();
81 let eltsz = ty_size(elt);
82 len * eltsz
83 }
84 _ => bug!("ty_size: unhandled type")
85 }
86 }
87
88 fn is_homogenous_aggregate_ty(ty: Type) -> Option<(Type, u64)> {
89 fn check_array(ty: Type) -> Option<(Type, u64)> {
90 let len = ty.array_length() as u64;
91 if len == 0 {
92 return None
93 }
94 let elt = ty.element_type();
95
96 // if our element is an HFA/HVA, so are we; multiply members by our len
97 is_homogenous_aggregate_ty(elt).map(|(base_ty, members)| (base_ty, len * members))
98 }
99
100 fn check_struct(ty: Type) -> Option<(Type, u64)> {
101 let str_tys = ty.field_types();
102 if str_tys.len() == 0 {
103 return None
104 }
105
106 let mut prev_base_ty = None;
107 let mut members = 0;
108 for opt_homog_agg in str_tys.iter().map(|t| is_homogenous_aggregate_ty(*t)) {
109 match (prev_base_ty, opt_homog_agg) {
110 // field isn't itself an HFA, so we aren't either
111 (_, None) => return None,
112
113 // first field - store its type and number of members
114 (None, Some((field_ty, field_members))) => {
115 prev_base_ty = Some(field_ty);
116 members = field_members;
117 },
118
119 // 2nd or later field - give up if it's a different type; otherwise incr. members
120 (Some(prev_ty), Some((field_ty, field_members))) => {
121 if prev_ty != field_ty {
122 return None;
123 }
124 members += field_members;
125 }
126 }
127 }
128
129 // Because of previous checks, we know prev_base_ty is Some(...) because
130 // 1. str_tys has at least one element; and
131 // 2. prev_base_ty was filled in (or we would've returned early)
132 let (base_ty, members) = (prev_base_ty.unwrap(), members);
133
134 // Ensure there is no padding.
135 if ty_size(ty) == ty_size(base_ty) * (members as usize) {
136 Some((base_ty, members))
137 } else {
138 None
139 }
140 }
141
142 let homog_agg = match ty.kind() {
143 Float => Some((ty, 1)),
144 Double => Some((ty, 1)),
145 Array => check_array(ty),
146 Struct => check_struct(ty),
147 Vector => match ty_size(ty) {
148 4|8 => Some((ty, 1)),
149 _ => None
150 },
151 _ => None
152 };
153
154 // Ensure we have at most four uniquely addressable members
155 homog_agg.and_then(|(base_ty, members)| {
156 if members > 0 && members <= 4 {
157 Some((base_ty, members))
158 } else {
159 None
160 }
161 })
162 }
163
164 fn classify_ret_ty(ccx: &CrateContext, ret: &mut ArgType) {
165 if is_reg_ty(ret.ty) {
166 ret.extend_integer_width_to(32);
167 return;
168 }
169 if let Some((base_ty, members)) = is_homogenous_aggregate_ty(ret.ty) {
170 ret.cast = Some(Type::array(&base_ty, members));
171 return;
172 }
173 let size = ty_size(ret.ty);
174 if size <= 16 {
175 let llty = if size <= 1 {
176 Type::i8(ccx)
177 } else if size <= 2 {
178 Type::i16(ccx)
179 } else if size <= 4 {
180 Type::i32(ccx)
181 } else if size <= 8 {
182 Type::i64(ccx)
183 } else {
184 Type::array(&Type::i64(ccx), ((size + 7 ) / 8 ) as u64)
185 };
186 ret.cast = Some(llty);
187 return;
188 }
189 ret.make_indirect(ccx);
190 }
191
192 fn classify_arg_ty(ccx: &CrateContext, arg: &mut ArgType) {
193 if is_reg_ty(arg.ty) {
194 arg.extend_integer_width_to(32);
195 return;
196 }
197 if let Some((base_ty, members)) = is_homogenous_aggregate_ty(arg.ty) {
198 arg.cast = Some(Type::array(&base_ty, members));
199 return;
200 }
201 let size = ty_size(arg.ty);
202 if size <= 16 {
203 let llty = if size == 0 {
204 Type::array(&Type::i64(ccx), 0)
205 } else if size == 1 {
206 Type::i8(ccx)
207 } else if size == 2 {
208 Type::i16(ccx)
209 } else if size <= 4 {
210 Type::i32(ccx)
211 } else if size <= 8 {
212 Type::i64(ccx)
213 } else {
214 Type::array(&Type::i64(ccx), ((size + 7 ) / 8 ) as u64)
215 };
216 arg.cast = Some(llty);
217 return;
218 }
219 arg.make_indirect(ccx);
220 }
221
222 fn is_reg_ty(ty: Type) -> bool {
223 match ty.kind() {
224 Integer
225 | Pointer
226 | Float
227 | Double
228 | Vector => true,
229 _ => false
230 }
231 }
232
233 pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType) {
234 if !fty.ret.is_ignore() {
235 classify_ret_ty(ccx, &mut fty.ret);
236 }
237
238 for arg in &mut fty.args {
239 if arg.is_ignore() { continue; }
240 classify_arg_ty(ccx, arg);
241 }
242 }