1 // Copyright 2012-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.
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)]
16 use llvm
::{Integer, Pointer, Float, Double, Struct, Array, Vector}
;
17 use abi
::{ArgType, FnType}
;
18 use context
::CrateContext
;
21 fn align_up_to(off
: usize, a
: usize) -> usize {
22 return (off
+ a
- 1) / a
* a
;
25 fn align(off
: usize, ty
: Type
) -> usize {
27 return align_up_to(off
, a
);
30 fn ty_align(ty
: Type
) -> usize {
32 Integer
=> ((ty
.int_width() as usize) + 7) / 8,
40 let str_tys
= ty
.field_types();
41 str_tys
.iter().fold(1, |a
, t
| cmp
::max(a
, ty_align(*t
)))
45 let elt
= ty
.element_type();
49 let len
= ty
.vector_length();
50 let elt
= ty
.element_type();
53 _
=> bug
!("ty_align: unhandled type")
57 fn ty_size(ty
: Type
) -> usize {
59 Integer
=> ((ty
.int_width() as usize) + 7) / 8,
65 let str_tys
= ty
.field_types();
66 str_tys
.iter().fold(0, |s
, t
| s
+ ty_size(*t
))
68 let str_tys
= ty
.field_types();
69 let size
= str_tys
.iter().fold(0, |s
, t
| align(s
, *t
) + ty_size(*t
));
74 let len
= ty
.array_length();
75 let elt
= ty
.element_type();
76 let eltsz
= ty_size(elt
);
80 let len
= ty
.vector_length();
81 let elt
= ty
.element_type();
82 let eltsz
= ty_size(elt
);
85 _
=> bug
!("ty_size: unhandled type")
89 fn classify_ret_ty(ccx
: &CrateContext
, ret
: &mut ArgType
) {
90 if is_reg_ty(ret
.ty
) {
91 ret
.extend_integer_width_to(32);
93 ret
.make_indirect(ccx
);
97 fn classify_arg_ty(ccx
: &CrateContext
, arg
: &mut ArgType
, offset
: &mut usize) {
98 let orig_offset
= *offset
;
99 let size
= ty_size(arg
.ty
) * 8;
100 let mut align
= ty_align(arg
.ty
);
102 align
= cmp
::min(cmp
::max(align
, 4), 8);
103 *offset
= align_up_to(*offset
, align
);
104 *offset
+= align_up_to(size
, align
* 8) / 8;
106 if !is_reg_ty(arg
.ty
) {
107 arg
.cast
= Some(struct_ty(ccx
, arg
.ty
));
108 arg
.pad
= padding_ty(ccx
, align
, orig_offset
);
110 arg
.extend_integer_width_to(32);
114 fn is_reg_ty(ty
: Type
) -> bool
{
115 return match ty
.kind() {
125 fn padding_ty(ccx
: &CrateContext
, align
: usize, offset
: usize) -> Option
<Type
> {
126 if ((align
- 1 ) & offset
) > 0 {
133 fn coerce_to_int(ccx
: &CrateContext
, size
: usize) -> Vec
<Type
> {
134 let int_ty
= Type
::i32(ccx
);
135 let mut args
= Vec
::new();
137 let mut n
= size
/ 32;
146 args
.push(Type
::from_ref(llvm
::LLVMIntTypeInContext(ccx
.llcx(), r
as c_uint
)));
153 fn struct_ty(ccx
: &CrateContext
, ty
: Type
) -> Type
{
154 let size
= ty_size(ty
) * 8;
155 Type
::struct_(ccx
, &coerce_to_int(ccx
, size
), false)
158 pub fn compute_abi_info(ccx
: &CrateContext
, fty
: &mut FnType
) {
159 if !fty
.ret
.is_ignore() {
160 classify_ret_ty(ccx
, &mut fty
.ret
);
163 let mut offset
= if fty
.ret
.is_indirect() { 4 }
else { 0 }
;
164 for arg
in &mut fty
.args
{
165 if arg
.is_ignore() { continue; }
166 classify_arg_ty(ccx
, arg
, &mut offset
);