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)]
13 use llvm::{Integer, Pointer, Float, Double, Struct, Array, Vector};
14 use abi::{FnType, ArgType};
15 use context::CrateContext;
25 type TyAlignFn = fn(ty: Type) -> usize;
27 fn align_up_to(off: usize, a: usize) -> usize {
28 return (off + a - 1) / a * a;
31 fn align(off: usize, ty: Type, align_fn: TyAlignFn) -> usize {
33 return align_up_to(off, a);
36 fn general_ty_align(ty: Type) -> usize {
38 Integer => ((ty.int_width() as usize) + 7) / 8,
46 let str_tys = ty.field_types();
47 str_tys.iter().fold(1, |a, t| cmp::max(a, general_ty_align(*t)))
51 let elt = ty.element_type();
55 let len = ty.vector_length();
56 let elt = ty.element_type();
57 general_ty_align(elt) * len
59 _ => bug!("ty_align: unhandled type")
63 // For more information see:
65 // https://developer.apple.com/library/ios/documentation/Xcode/Conceptual
66 // /iPhoneOSABIReference/Articles/ARMv7FunctionCallingConventions.html
68 // https://developer.apple.com/library/ios/documentation/Xcode/Conceptual
69 // /iPhoneOSABIReference/Articles/ARMv6FunctionCallingConventions.html
70 fn ios_ty_align(ty: Type) -> usize {
72 Integer => cmp::min(4, ((ty.int_width() as usize) + 7) / 8),
80 let str_tys = ty.field_types();
81 str_tys.iter().fold(1, |a, t| cmp::max(a, ios_ty_align(*t)))
85 let elt = ty.element_type();
89 let len = ty.vector_length();
90 let elt = ty.element_type();
91 ios_ty_align(elt) * len
93 _ => bug!("ty_align: unhandled type")
97 fn ty_size(ty: Type, align_fn: TyAlignFn) -> usize {
99 Integer => ((ty.int_width() as usize) + 7) / 8,
105 let str_tys = ty.field_types();
106 str_tys.iter().fold(0, |s, t| s + ty_size(*t, align_fn))
108 let str_tys = ty.field_types();
109 let size = str_tys.iter()
111 align(s, *t, align_fn) + ty_size(*t, align_fn)
113 align(size, ty, align_fn)
117 let len = ty.array_length();
118 let elt = ty.element_type();
119 let eltsz = ty_size(elt, align_fn);
123 let len = ty.vector_length();
124 let elt = ty.element_type();
125 let eltsz = ty_size(elt, align_fn);
128 _ => bug!("ty_size: unhandled type")
132 fn classify_ret_ty(ccx: &CrateContext, ret: &mut ArgType, align_fn: TyAlignFn) {
133 if is_reg_ty(ret.ty) {
134 ret.extend_integer_width_to(32);
137 let size = ty_size(ret.ty, align_fn);
139 let llty = if size <= 1 {
141 } else if size <= 2 {
146 ret.cast = Some(llty);
149 ret.make_indirect(ccx);
152 fn classify_arg_ty(ccx: &CrateContext, arg: &mut ArgType, align_fn: TyAlignFn) {
153 if is_reg_ty(arg.ty) {
154 arg.extend_integer_width_to(32);
157 let align = align_fn(arg.ty);
158 let size = ty_size(arg.ty, align_fn);
159 let llty = if align <= 4 {
160 Type::array(&Type::i32(ccx), ((size + 3) / 4) as u64)
162 Type::array(&Type::i64(ccx), ((size + 7) / 8) as u64)
164 arg.cast = Some(llty);
167 fn is_reg_ty(ty: Type) -> bool {
178 pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType, flavor: Flavor) {
179 let align_fn = match flavor {
180 Flavor::General => general_ty_align as TyAlignFn,
181 Flavor::Ios => ios_ty_align as TyAlignFn,
184 if !fty.ret.is_ignore() {
185 classify_ret_ty(ccx, &mut fty.ret, align_fn);
188 for arg in &mut fty.args {
189 if arg.is_ignore() { continue; }
190 classify_arg_ty(ccx, arg, align_fn);