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.
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.
11 #![allow(non_upper_case_globals)]
13 use llvm
::{Integer, Pointer, Float, Double, Struct, Array, Vector}
;
14 use abi
::{FnType, ArgType}
;
15 use context
::CrateContext
;
20 fn align_up_to(off
: usize, a
: usize) -> usize {
21 return (off
+ a
- 1) / a
* a
;
24 fn align(off
: usize, ty
: Type
) -> usize {
26 return align_up_to(off
, a
);
29 fn ty_align(ty
: Type
) -> usize {
31 Integer
=> ((ty
.int_width() as usize) + 7) / 8,
39 let str_tys
= ty
.field_types();
40 str_tys
.iter().fold(1, |a
, t
| cmp
::max(a
, ty_align(*t
)))
44 let elt
= ty
.element_type();
48 let len
= ty
.vector_length();
49 let elt
= ty
.element_type();
52 _
=> bug
!("ty_align: unhandled type")
56 fn ty_size(ty
: Type
) -> usize {
58 Integer
=> ((ty
.int_width() as usize) + 7) / 8,
64 let str_tys
= ty
.field_types();
65 str_tys
.iter().fold(0, |s
, t
| s
+ ty_size(*t
))
67 let str_tys
= ty
.field_types();
68 let size
= str_tys
.iter().fold(0, |s
, t
| align(s
, *t
) + ty_size(*t
));
73 let len
= ty
.array_length();
74 let elt
= ty
.element_type();
75 let eltsz
= ty_size(elt
);
79 let len
= ty
.vector_length();
80 let elt
= ty
.element_type();
81 let eltsz
= ty_size(elt
);
84 _
=> bug
!("ty_size: unhandled type")
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;
94 let elt
= ty
.element_type();
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
))
100 fn check_struct(ty
: Type
) -> Option
<(Type
, u64)> {
101 let str_tys
= ty
.field_types();
102 if str_tys
.len() == 0 {
106 let mut prev_base_ty
= None
;
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
,
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
;
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
{
124 members
+= field_members
;
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
);
134 // Ensure there is no padding.
135 if ty_size(ty
) == ty_size(base_ty
) * (members
as usize) {
136 Some((base_ty
, members
))
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)),
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
))
164 fn classify_ret_ty(ccx
: &CrateContext
, ret
: &mut ArgType
) {
165 if is_reg_ty(ret
.ty
) {
166 ret
.extend_integer_width_to(32);
169 if let Some((base_ty
, members
)) = is_homogenous_aggregate_ty(ret
.ty
) {
170 ret
.cast
= Some(Type
::array(&base_ty
, members
));
173 let size
= ty_size(ret
.ty
);
175 let llty
= if size
<= 1 {
177 } else if size
<= 2 {
179 } else if size
<= 4 {
181 } else if size
<= 8 {
184 Type
::array(&Type
::i64(ccx
), ((size
+ 7 ) / 8 ) as u64)
186 ret
.cast
= Some(llty
);
189 ret
.make_indirect(ccx
);
192 fn classify_arg_ty(ccx
: &CrateContext
, arg
: &mut ArgType
) {
193 if is_reg_ty(arg
.ty
) {
194 arg
.extend_integer_width_to(32);
197 if let Some((base_ty
, members
)) = is_homogenous_aggregate_ty(arg
.ty
) {
198 arg
.cast
= Some(Type
::array(&base_ty
, members
));
201 let size
= ty_size(arg
.ty
);
203 let llty
= if size
== 0 {
204 Type
::array(&Type
::i64(ccx
), 0)
205 } else if size
== 1 {
207 } else if size
== 2 {
209 } else if size
<= 4 {
211 } else if size
<= 8 {
214 Type
::array(&Type
::i64(ccx
), ((size
+ 7 ) / 8 ) as u64)
216 arg
.cast
= Some(llty
);
219 arg
.make_indirect(ccx
);
222 fn is_reg_ty(ty
: Type
) -> bool
{
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
);
238 for arg
in &mut fty
.args
{
239 if arg
.is_ignore() { continue; }
240 classify_arg_ty(ccx
, arg
);