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.
11 use abi::{ArgAttribute, FnType, LayoutExt, Reg, RegKind};
12 use common::CrateContext;
20 pub fn compute_abi_info<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
21 fty: &mut FnType<'tcx>,
23 if !fty.ret.is_ignore() {
24 if fty.ret.layout.is_aggregate() {
25 // Returning a structure. Most often, this will use
26 // a hidden first argument. On some platforms, though,
27 // small structs are returned as integers.
30 // http://www.angelcode.com/dev/callconv/callconv.html
31 // Clang's ABI handling is in lib/CodeGen/TargetInfo.cpp
32 let t = &ccx.sess().target.target;
33 if t.options.is_like_osx || t.options.is_like_windows
34 || t.options.is_like_openbsd {
35 let size = fty.ret.layout.size(ccx);
37 1 => fty.ret.cast_to(ccx, Reg::i8()),
38 2 => fty.ret.cast_to(ccx, Reg::i16()),
39 4 => fty.ret.cast_to(ccx, Reg::i32()),
40 8 => fty.ret.cast_to(ccx, Reg::i64()),
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.layout.is_aggregate() {
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 // At this point we know this must be a primitive of sorts.
77 let unit = arg.layout.homogeneous_aggregate(ccx).unwrap();
78 let size = arg.layout.size(ccx);
79 assert_eq!(unit.size, size);
80 if unit.kind == RegKind::Float {
84 let size_in_regs = (size.bits() + 31) / 32;
86 if size_in_regs == 0 {
90 if size_in_regs > free_regs {
94 free_regs -= size_in_regs;
96 if size.bits() <= 32 && unit.kind == RegKind::Integer {
97 arg.attrs.set(ArgAttribute::InReg);