1 // Copyright 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.
12 use abi::{ArgAttribute, FnType};
15 use super::machine::*;
23 pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType, flavor: Flavor) {
24 if !fty.ret.is_ignore() {
25 if fty.ret.ty.kind() == Struct {
26 // Returning a structure. Most often, this will use
27 // a hidden first argument. On some platforms, though,
28 // small structs are returned as integers.
31 // http://www.angelcode.com/dev/callconv/callconv.html
32 // Clang's ABI handling is in lib/CodeGen/TargetInfo.cpp
33 let t = &ccx.sess().target.target;
34 if t.options.is_like_osx || t.options.is_like_windows
35 || t.options.is_like_openbsd {
36 match llsize_of_alloc(ccx, fty.ret.ty) {
37 1 => fty.ret.cast = Some(Type::i8(ccx)),
38 2 => fty.ret.cast = Some(Type::i16(ccx)),
39 4 => fty.ret.cast = Some(Type::i32(ccx)),
40 8 => fty.ret.cast = Some(Type::i64(ccx)),
41 _ => fty.ret.make_indirect(ccx)
44 fty.ret.make_indirect(ccx);
47 fty.ret.extend_integer_width_to(32);
51 for arg in &mut fty.args {
52 if arg.is_ignore() { continue; }
53 if arg.ty.kind() == Struct {
54 arg.make_indirect(ccx);
55 arg.attrs.set(ArgAttribute::ByVal);
57 arg.extend_integer_width_to(32);
61 if flavor == Flavor::Fastcall {
62 // Mark arguments as InReg like clang does it,
63 // so our fastcall is compatible with C/C++ fastcall.
65 // Clang reference: lib/CodeGen/TargetInfo.cpp
66 // See X86_32ABIInfo::shouldPrimitiveUseInReg(), X86_32ABIInfo::updateFreeRegs()
68 // IsSoftFloatABI is only set to true on ARM platforms,
69 // which in turn can't be x86?
71 let mut free_regs = 2;
73 for arg in &mut fty.args {
74 if arg.is_ignore() || arg.is_indirect() { continue; }
76 if arg.ty.kind() == Float {
80 let size = llbitsize_of_real(ccx, arg.ty);
81 let size_in_regs = (size + 31) / 32;
83 if size_in_regs == 0 {
87 if size_in_regs > free_regs {
91 free_regs -= size_in_regs;
93 if size <= 32 && (arg.ty.kind() == Pointer || arg.ty.kind() == Integer) {
94 arg.attrs.set(ArgAttribute::InReg);