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, Attribute};
14 use trans::cabi::{FnType, ArgType};
15 use trans::context::CrateContext;
16 use trans::type_::Type;
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 {
33 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as usize) + 7) / 8
43 let str_tys = ty.field_types();
44 str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
48 let elt = ty.element_type();
51 _ => panic!("ty_size: unhandled type")
55 fn ty_size(ty: Type) -> usize {
59 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as usize) + 7) / 8
67 let str_tys = ty.field_types();
68 str_tys.iter().fold(0, |s, t| s + ty_size(*t))
70 let str_tys = ty.field_types();
71 let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
76 let len = ty.array_length();
77 let elt = ty.element_type();
78 let eltsz = ty_size(elt);
81 _ => panic!("ty_size: unhandled type")
85 fn classify_ret_ty(ccx: &CrateContext, ty: Type) -> ArgType {
87 let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
88 ArgType::direct(ty, None, None, attr)
90 ArgType::indirect(ty, Some(Attribute::StructRet))
94 fn classify_arg_ty(ccx: &CrateContext, ty: Type, offset: &mut usize) -> ArgType {
95 let orig_offset = *offset;
96 let size = ty_size(ty) * 8;
97 let mut align = ty_align(ty);
99 align = cmp::min(cmp::max(align, 4), 8);
100 *offset = align_up_to(*offset, align);
101 *offset += align_up_to(size, align * 8) / 8;
104 let attr = if ty == Type::i1(ccx) { Some(Attribute::ZExt) } else { None };
105 ArgType::direct(ty, None, None, attr)
109 Some(struct_ty(ccx, ty)),
110 padding_ty(ccx, align, orig_offset),
116 fn is_reg_ty(ty: Type) -> bool {
117 return match ty.kind() {
126 fn padding_ty(ccx: &CrateContext, align: usize, offset: usize) -> Option<Type> {
127 if ((align - 1 ) & offset) > 0 {
134 fn coerce_to_int(ccx: &CrateContext, size: usize) -> Vec<Type> {
135 let int_ty = Type::i32(ccx);
136 let mut args = Vec::new();
138 let mut n = size / 32;
147 args.push(Type::from_ref(llvm::LLVMIntTypeInContext(ccx.llcx(), r as c_uint)));
154 fn struct_ty(ccx: &CrateContext, ty: Type) -> Type {
155 let size = ty_size(ty) * 8;
156 Type::struct_(ccx, &coerce_to_int(ccx, size), false)
159 pub fn compute_abi_info(ccx: &CrateContext,
162 ret_def: bool) -> FnType {
163 let ret_ty = if ret_def {
164 classify_ret_ty(ccx, rty)
166 ArgType::direct(Type::void(ccx), None, None, None)
169 let sret = ret_ty.is_indirect();
170 let mut arg_tys = Vec::new();
171 let mut offset = if sret { 4 } else { 0 };
174 let ty = classify_arg_ty(ccx, *aty, &mut offset);