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_uppercase_pattern_statics)]
13 use lib::llvm::{llvm, Integer, Pointer, Float, Double, Struct, Array};
14 use lib::llvm::StructRetAttribute;
15 use middle::trans::cabi::{FnType, ArgType};
16 use middle::trans::context::CrateContext;
17 use middle::trans::type_::Type;
21 fn align_up_to(off: uint, a: uint) -> uint {
22 return (off + a - 1u) / a * a;
25 fn align(off: uint, ty: Type) -> uint {
27 return align_up_to(off, a);
30 fn ty_align(ty: Type) -> uint {
34 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
44 let str_tys = ty.field_types();
45 str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
49 let elt = ty.element_type();
52 _ => fail!("ty_align: unhandled type")
56 fn ty_size(ty: Type) -> uint {
60 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
68 let str_tys = ty.field_types();
69 str_tys.iter().fold(0, |s, t| s + ty_size(*t))
71 let str_tys = ty.field_types();
72 let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
77 let len = ty.array_length();
78 let elt = ty.element_type();
79 let eltsz = ty_size(elt);
82 _ => fail!("ty_size: unhandled type")
86 fn classify_ret_ty(ccx: &CrateContext, ty: Type) -> ArgType {
88 return ArgType::direct(ty, None, None, None);
90 let size = ty_size(ty);
92 let llty = if size <= 1 {
99 return ArgType::direct(ty, Some(llty), None, None);
101 ArgType::indirect(ty, Some(StructRetAttribute))
104 fn classify_arg_ty(ccx: &CrateContext, ty: Type) -> ArgType {
106 return ArgType::direct(ty, None, None, None);
108 let align = ty_align(ty);
109 let size = ty_size(ty);
110 let llty = if align <= 4 {
111 Type::array(&Type::i32(ccx), ((size + 3) / 4) as u64)
113 Type::array(&Type::i64(ccx), ((size + 7) / 8) as u64)
115 ArgType::direct(ty, Some(llty), None, None)
118 fn is_reg_ty(ty: Type) -> bool {
128 pub fn compute_abi_info(ccx: &CrateContext,
131 ret_def: bool) -> FnType {
132 let mut arg_tys = Vec::new();
133 for &aty in atys.iter() {
134 let ty = classify_arg_ty(ccx, aty);
138 let ret_ty = if ret_def {
139 classify_ret_ty(ccx, rty)
141 ArgType::direct(Type::void(ccx), None, None, None)