1 // Copyright 2014-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.
13 use llvm
::{Integer, Pointer, Float, Double, Struct, Array}
;
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();
47 _
=> bug
!("ty_size: unhandled type")
51 fn ty_size(ty
: Type
) -> usize {
53 Integer
=> ((ty
.int_width() as usize) + 7) / 8,
59 let str_tys
= ty
.field_types();
60 str_tys
.iter().fold(0, |s
, t
| s
+ ty_size(*t
))
62 let str_tys
= ty
.field_types();
63 let size
= str_tys
.iter().fold(0, |s
, t
| align(s
, *t
) + ty_size(*t
));
68 let len
= ty
.array_length();
69 let elt
= ty
.element_type();
70 let eltsz
= ty_size(elt
);
73 _
=> bug
!("ty_size: unhandled type")
77 fn classify_ret_ty(ccx
: &CrateContext
, ret
: &mut ArgType
) {
78 if is_reg_ty(ret
.ty
) {
79 ret
.extend_integer_width_to(32);
81 ret
.make_indirect(ccx
);
85 fn classify_arg_ty(ccx
: &CrateContext
, arg
: &mut ArgType
, offset
: &mut usize) {
86 let orig_offset
= *offset
;
87 let size
= ty_size(arg
.ty
) * 8;
88 let mut align
= ty_align(arg
.ty
);
90 align
= cmp
::min(cmp
::max(align
, 4), 8);
91 *offset
= align_up_to(*offset
, align
);
92 *offset
+= align_up_to(size
, align
* 8) / 8;
94 if !is_reg_ty(arg
.ty
) {
95 arg
.cast
= Some(struct_ty(ccx
, arg
.ty
));
96 arg
.pad
= padding_ty(ccx
, align
, orig_offset
);
98 arg
.extend_integer_width_to(32);
102 fn is_reg_ty(ty
: Type
) -> bool
{
103 return match ty
.kind() {
112 fn padding_ty(ccx
: &CrateContext
, align
: usize, offset
: usize) -> Option
<Type
> {
113 if ((align
- 1 ) & offset
) > 0 {
120 fn coerce_to_int(ccx
: &CrateContext
, size
: usize) -> Vec
<Type
> {
121 let int_ty
= Type
::i32(ccx
);
122 let mut args
= Vec
::new();
124 let mut n
= size
/ 32;
133 args
.push(Type
::from_ref(llvm
::LLVMIntTypeInContext(ccx
.llcx(), r
as c_uint
)));
140 fn struct_ty(ccx
: &CrateContext
, ty
: Type
) -> Type
{
141 let size
= ty_size(ty
) * 8;
142 Type
::struct_(ccx
, &coerce_to_int(ccx
, size
), false)
145 pub fn compute_abi_info(ccx
: &CrateContext
, fty
: &mut FnType
) {
146 if !fty
.ret
.is_ignore() {
147 classify_ret_ty(ccx
, &mut fty
.ret
);
150 let mut offset
= if fty
.ret
.is_indirect() { 4 }
else { 0 }
;
151 for arg
in &mut fty
.args
{
152 if arg
.is_ignore() { continue; }
153 classify_arg_ty(ccx
, arg
, &mut offset
);