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 llvm::{StructRetAttribute, ZExtAttribute};
15 use trans::cabi::{FnType, ArgType};
16 use trans::context::CrateContext;
17 use trans::type_::Type;
26 type TyAlignFn = fn(ty: Type) -> usize;
28 fn align_up_to(off: usize, a: usize) -> usize {
29 return (off + a - 1) / a * a;
32 fn align(off: usize, ty: Type, align_fn: TyAlignFn) -> usize {
34 return align_up_to(off, a);
37 fn general_ty_align(ty: Type) -> usize {
39 Integer => ((ty.int_width() as usize) + 7) / 8,
47 let str_tys = ty.field_types();
48 str_tys.iter().fold(1, |a, t| cmp::max(a, general_ty_align(*t)))
52 let elt = ty.element_type();
56 let len = ty.vector_length();
57 let elt = ty.element_type();
58 general_ty_align(elt) * len
60 _ => panic!("ty_align: unhandled type")
64 // For more information see:
66 // https://developer.apple.com/library/ios/documentation/Xcode/Conceptual
67 // /iPhoneOSABIReference/Articles/ARMv7FunctionCallingConventions.html
69 // https://developer.apple.com/library/ios/documentation/Xcode/Conceptual
70 // /iPhoneOSABIReference/Articles/ARMv6FunctionCallingConventions.html
71 fn ios_ty_align(ty: Type) -> usize {
73 Integer => cmp::min(4, ((ty.int_width() as usize) + 7) / 8),
81 let str_tys = ty.field_types();
82 str_tys.iter().fold(1, |a, t| cmp::max(a, ios_ty_align(*t)))
86 let elt = ty.element_type();
90 let len = ty.vector_length();
91 let elt = ty.element_type();
92 ios_ty_align(elt) * len
94 _ => panic!("ty_align: unhandled type")
98 fn ty_size(ty: Type, align_fn: TyAlignFn) -> usize {
100 Integer => ((ty.int_width() as usize) + 7) / 8,
106 let str_tys = ty.field_types();
107 str_tys.iter().fold(0, |s, t| s + ty_size(*t, align_fn))
109 let str_tys = ty.field_types();
110 let size = str_tys.iter()
112 align(s, *t, align_fn) + ty_size(*t, align_fn)
114 align(size, ty, align_fn)
118 let len = ty.array_length();
119 let elt = ty.element_type();
120 let eltsz = ty_size(elt, align_fn);
124 let len = ty.vector_length();
125 let elt = ty.element_type();
126 let eltsz = ty_size(elt, align_fn);
129 _ => panic!("ty_size: unhandled type")
133 fn classify_ret_ty(ccx: &CrateContext, ty: Type, align_fn: TyAlignFn) -> ArgType {
135 let attr = if ty == Type::i1(ccx) { Some(ZExtAttribute) } else { None };
136 return ArgType::direct(ty, None, None, attr);
138 let size = ty_size(ty, align_fn);
140 let llty = if size <= 1 {
142 } else if size <= 2 {
147 return ArgType::direct(ty, Some(llty), None, None);
149 ArgType::indirect(ty, Some(StructRetAttribute))
152 fn classify_arg_ty(ccx: &CrateContext, ty: Type, align_fn: TyAlignFn) -> ArgType {
154 let attr = if ty == Type::i1(ccx) { Some(ZExtAttribute) } else { None };
155 return ArgType::direct(ty, None, None, attr);
157 let align = align_fn(ty);
158 let size = ty_size(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 ArgType::direct(ty, Some(llty), None, None)
167 fn is_reg_ty(ty: Type) -> bool {
178 pub fn compute_abi_info(ccx: &CrateContext,
182 flavor: Flavor) -> FnType {
183 let align_fn = match flavor {
184 Flavor::General => general_ty_align as TyAlignFn,
185 Flavor::Ios => ios_ty_align as TyAlignFn,
188 let mut arg_tys = Vec::new();
190 let ty = classify_arg_ty(ccx, aty, align_fn);
194 let ret_ty = if ret_def {
195 classify_ret_ty(ccx, rty, align_fn)
197 ArgType::direct(Type::void(ccx), None, None, None)