2015-12-28 21:09:06 +00:00
|
|
|
// Copyright 2014-2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-03-10 06:25:57 +02:00
|
|
|
// FIXME:
|
2015-12-28 21:09:06 +00:00
|
|
|
// Alignment of 128 bit types is not currently handled, this will
|
|
|
|
// need to be fixed when PowerPC vector support is added.
|
|
|
|
|
2017-03-10 06:25:57 +02:00
|
|
|
use abi::{FnType, ArgType, LayoutExt, Reg, RegKind, Uniform};
|
2016-03-22 19:23:36 +02:00
|
|
|
use context::CrateContext;
|
2017-08-23 17:38:45 -04:00
|
|
|
use rustc::ty::layout;
|
2015-12-28 21:09:06 +00:00
|
|
|
|
2017-08-23 17:38:45 -04:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
enum ABI {
|
|
|
|
ELFv1, // original ABI used for powerpc64 (big-endian)
|
|
|
|
ELFv2, // newer ABI used for powerpc64le
|
|
|
|
}
|
|
|
|
use self::ABI::*;
|
|
|
|
|
|
|
|
fn is_homogeneous_aggregate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
arg: &mut ArgType<'tcx>,
|
|
|
|
abi: ABI)
|
2017-03-10 06:25:57 +02:00
|
|
|
-> Option<Uniform> {
|
2017-07-21 18:08:40 -07:00
|
|
|
arg.layout.homogeneous_aggregate(ccx).and_then(|unit| {
|
2017-03-10 06:25:57 +02:00
|
|
|
let size = arg.layout.size(ccx);
|
2015-12-28 21:09:06 +00:00
|
|
|
|
2017-08-23 17:38:45 -04:00
|
|
|
// ELFv1 only passes one-member aggregates transparently.
|
|
|
|
// ELFv2 passes up to eight uniquely addressable members.
|
|
|
|
if (abi == ELFv1 && size > unit.size)
|
|
|
|
|| size > unit.size.checked_mul(8, ccx).unwrap() {
|
2017-03-10 06:25:57 +02:00
|
|
|
return None;
|
2015-12-28 21:09:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 06:25:57 +02:00
|
|
|
let valid_unit = match unit.kind {
|
|
|
|
RegKind::Integer => false,
|
|
|
|
RegKind::Float => true,
|
|
|
|
RegKind::Vector => size.bits() == 128
|
|
|
|
};
|
2015-12-28 21:09:06 +00:00
|
|
|
|
2017-03-10 06:25:57 +02:00
|
|
|
if valid_unit {
|
|
|
|
Some(Uniform {
|
|
|
|
unit,
|
|
|
|
total: size
|
|
|
|
})
|
2015-12-28 21:09:06 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-23 17:38:45 -04:00
|
|
|
fn classify_ret_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ret: &mut ArgType<'tcx>, abi: ABI) {
|
2017-03-10 06:25:57 +02:00
|
|
|
if !ret.layout.is_aggregate() {
|
2016-02-17 19:33:27 +13:00
|
|
|
ret.extend_integer_width_to(64);
|
2016-02-25 10:49:58 +02:00
|
|
|
return;
|
2015-12-28 21:09:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 17:38:45 -04:00
|
|
|
// The ELFv1 ABI doesn't return aggregates in registers
|
|
|
|
if abi == ELFv1 {
|
2016-02-25 19:35:40 +02:00
|
|
|
ret.make_indirect(ccx);
|
2017-08-23 17:38:45 -04:00
|
|
|
return;
|
2015-12-28 21:09:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 17:38:45 -04:00
|
|
|
if let Some(uniform) = is_homogeneous_aggregate(ccx, ret, abi) {
|
2017-03-10 06:25:57 +02:00
|
|
|
ret.cast_to(ccx, uniform);
|
2016-02-25 10:49:58 +02:00
|
|
|
return;
|
2015-12-28 21:09:06 +00:00
|
|
|
}
|
2017-08-23 17:38:45 -04:00
|
|
|
|
2017-03-10 06:25:57 +02:00
|
|
|
let size = ret.layout.size(ccx);
|
|
|
|
let bits = size.bits();
|
|
|
|
if bits <= 128 {
|
|
|
|
let unit = if bits <= 8 {
|
|
|
|
Reg::i8()
|
|
|
|
} else if bits <= 16 {
|
|
|
|
Reg::i16()
|
|
|
|
} else if bits <= 32 {
|
|
|
|
Reg::i32()
|
2015-12-28 21:09:06 +00:00
|
|
|
} else {
|
2017-03-10 06:25:57 +02:00
|
|
|
Reg::i64()
|
2015-12-28 21:09:06 +00:00
|
|
|
};
|
2017-03-10 06:25:57 +02:00
|
|
|
|
|
|
|
ret.cast_to(ccx, Uniform {
|
|
|
|
unit,
|
|
|
|
total: size
|
|
|
|
});
|
2016-02-25 10:49:58 +02:00
|
|
|
return;
|
2015-12-28 21:09:06 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 19:35:40 +02:00
|
|
|
ret.make_indirect(ccx);
|
2015-12-28 21:09:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 17:38:45 -04:00
|
|
|
fn classify_arg_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, arg: &mut ArgType<'tcx>, abi: ABI) {
|
2017-03-10 06:25:57 +02:00
|
|
|
if !arg.layout.is_aggregate() {
|
2016-02-17 19:33:27 +13:00
|
|
|
arg.extend_integer_width_to(64);
|
2016-02-25 10:49:58 +02:00
|
|
|
return;
|
2015-12-28 21:09:06 +00:00
|
|
|
}
|
2016-02-25 10:49:58 +02:00
|
|
|
|
2017-08-23 17:38:45 -04:00
|
|
|
if let Some(uniform) = is_homogeneous_aggregate(ccx, arg, abi) {
|
2017-03-10 06:25:57 +02:00
|
|
|
arg.cast_to(ccx, uniform);
|
2016-02-25 10:49:58 +02:00
|
|
|
return;
|
2015-12-28 21:09:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 17:38:45 -04:00
|
|
|
let size = arg.layout.size(ccx);
|
|
|
|
let (unit, total) = match abi {
|
|
|
|
ELFv1 => {
|
|
|
|
// In ELFv1, aggregates smaller than a doubleword should appear in
|
|
|
|
// the least-significant bits of the parameter doubleword. The rest
|
|
|
|
// should be padded at their tail to fill out multiple doublewords.
|
|
|
|
if size.bits() <= 64 {
|
|
|
|
(Reg { kind: RegKind::Integer, size }, size)
|
|
|
|
} else {
|
|
|
|
let align = layout::Align::from_bits(64, 64).unwrap();
|
|
|
|
(Reg::i64(), size.abi_align(align))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ELFv2 => {
|
|
|
|
// In ELFv2, we can just cast directly.
|
|
|
|
(Reg::i64(), size)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-03-10 06:25:57 +02:00
|
|
|
arg.cast_to(ccx, Uniform {
|
2017-08-23 17:38:45 -04:00
|
|
|
unit,
|
2017-03-10 06:25:57 +02:00
|
|
|
total
|
|
|
|
});
|
2015-12-28 21:09:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 06:25:57 +02:00
|
|
|
pub fn compute_abi_info<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fty: &mut FnType<'tcx>) {
|
2017-08-23 17:38:45 -04:00
|
|
|
let abi = match ccx.sess().target.target.target_endian.as_str() {
|
|
|
|
"big" => ELFv1,
|
|
|
|
"little" => ELFv2,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
|
|
|
|
2016-03-06 12:36:39 +02:00
|
|
|
if !fty.ret.is_ignore() {
|
2017-08-23 17:38:45 -04:00
|
|
|
classify_ret_ty(ccx, &mut fty.ret, abi);
|
2016-02-23 21:55:19 +02:00
|
|
|
}
|
2015-12-28 21:09:06 +00:00
|
|
|
|
2016-02-23 21:55:19 +02:00
|
|
|
for arg in &mut fty.args {
|
2016-03-06 12:36:39 +02:00
|
|
|
if arg.is_ignore() { continue; }
|
2017-08-23 17:38:45 -04:00
|
|
|
classify_arg_ty(ccx, arg, abi);
|
2016-02-23 21:55:19 +02:00
|
|
|
}
|
2015-12-28 21:09:06 +00:00
|
|
|
}
|