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, Attribute}
;
17 use trans
::cabi
::{ArgType, FnType}
;
18 use trans
::context
::CrateContext
;
19 use trans
::type_
::Type
;
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 _
=> panic
!("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 _
=> panic
!("ty_size: unhandled type")
89 fn classify_ret_ty(ccx
: &CrateContext
, ty
: Type
) -> ArgType
{
91 let attr
= if ty
== Type
::i1(ccx
) { Some(Attribute::ZExt) }
else { None }
;
92 ArgType
::direct(ty
, None
, None
, attr
)
94 ArgType
::indirect(ty
, Some(Attribute
::StructRet
))
98 fn classify_arg_ty(ccx
: &CrateContext
, ty
: Type
, offset
: &mut usize) -> ArgType
{
99 let orig_offset
= *offset
;
100 let size
= ty_size(ty
) * 8;
101 let mut align
= ty_align(ty
);
103 align
= cmp
::min(cmp
::max(align
, 4), 8);
104 *offset
= align_up_to(*offset
, align
);
105 *offset
+= align_up_to(size
, align
* 8) / 8;
108 let attr
= if ty
== Type
::i1(ccx
) { Some(Attribute::ZExt) }
else { None }
;
109 ArgType
::direct(ty
, None
, None
, attr
)
113 Some(struct_ty(ccx
, ty
)),
114 padding_ty(ccx
, align
, orig_offset
),
120 fn is_reg_ty(ty
: Type
) -> bool
{
121 return match ty
.kind() {
131 fn padding_ty(ccx
: &CrateContext
, align
: usize, offset
: usize) -> Option
<Type
> {
132 if ((align
- 1 ) & offset
) > 0 {
139 fn coerce_to_int(ccx
: &CrateContext
, size
: usize) -> Vec
<Type
> {
140 let int_ty
= Type
::i32(ccx
);
141 let mut args
= Vec
::new();
143 let mut n
= size
/ 32;
152 args
.push(Type
::from_ref(llvm
::LLVMIntTypeInContext(ccx
.llcx(), r
as c_uint
)));
159 fn struct_ty(ccx
: &CrateContext
, ty
: Type
) -> Type
{
160 let size
= ty_size(ty
) * 8;
161 Type
::struct_(ccx
, &coerce_to_int(ccx
, size
), false)
164 pub fn compute_abi_info(ccx
: &CrateContext
,
167 ret_def
: bool
) -> FnType
{
168 let ret_ty
= if ret_def
{
169 classify_ret_ty(ccx
, rty
)
171 ArgType
::direct(Type
::void(ccx
), None
, None
, None
)
174 let sret
= ret_ty
.is_indirect();
175 let mut arg_tys
= Vec
::new();
176 let mut offset
= if sret { 4 }
else { 0 }
;
179 let ty
= classify_arg_ty(ccx
, *aty
, &mut offset
);