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;
18 use middle::trans::type_::Type;
21 use std::option::{None, Some};
23 fn align_up_to(off: uint, a: uint) -> uint {
24 return (off + a - 1u) / a * a;
27 fn align(off: uint, ty: Type) -> uint {
29 return align_up_to(off, a);
32 fn ty_align(ty: Type) -> uint {
36 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
46 let str_tys = ty.field_types();
47 str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
51 let elt = ty.element_type();
54 _ => fail!("ty_align: unhandled type")
58 fn ty_size(ty: Type) -> uint {
62 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
70 let str_tys = ty.field_types();
71 str_tys.iter().fold(0, |s, t| s + ty_size(*t))
73 let str_tys = ty.field_types();
74 let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
79 let len = ty.array_length();
80 let elt = ty.element_type();
81 let eltsz = ty_size(elt);
84 _ => fail!("ty_size: unhandled type")
88 fn classify_ret_ty(ty: Type) -> ArgType {
90 return ArgType::direct(ty, None, None, None);
92 let size = ty_size(ty);
94 let llty = if size <= 1 {
101 return ArgType::direct(ty, Some(llty), None, None);
103 ArgType::indirect(ty, Some(StructRetAttribute))
106 fn classify_arg_ty(ty: Type) -> ArgType {
108 return ArgType::direct(ty, None, None, None);
110 let align = ty_align(ty);
111 let size = ty_size(ty);
112 let llty = if align <= 4 {
113 Type::array(&Type::i32(), ((size + 3) / 4) as u64)
115 Type::array(&Type::i64(), ((size + 7) / 8) as u64)
117 ArgType::direct(ty, Some(llty), None, None)
120 fn is_reg_ty(ty: Type) -> bool {
130 pub fn compute_abi_info(_ccx: &CrateContext,
133 ret_def: bool) -> FnType {
134 let mut arg_tys = ~[];
135 for &aty in atys.iter() {
136 let ty = classify_arg_ty(aty);
140 let ret_ty = if ret_def {
143 ArgType::direct(Type::void(), None, None, None)