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 // The classification code for the x86_64 ABI is taken from the clay language
12 // https://github.com/jckarter/clay/blob/master/compiler/src/externals.cpp
14 use abi::{ArgType, CastTarget, FnType, LayoutExt, Reg, RegKind};
15 use context::CodegenCx;
17 use rustc::ty::layout::{self, TyLayout, Size};
19 /// Classification of "eightbyte" components.
20 // NB: the order of the variants is from general to specific,
21 // such that `unify(a, b)` is the "smaller" of `a` and `b`.
22 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
29 #[derive(Clone, Copy, Debug)]
32 // Currently supported vector size (AVX-512).
33 const LARGEST_VECTOR_SIZE: usize = 512;
34 const MAX_EIGHTBYTES: usize = LARGEST_VECTOR_SIZE / 64;
36 fn classify_arg<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, arg: &ArgType<'tcx>)
37 -> Result<[Option<Class>; MAX_EIGHTBYTES], Memory> {
38 fn classify<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
39 layout: TyLayout<'tcx>,
40 cls: &mut [Option<Class>],
42 -> Result<(), Memory> {
43 if !off.is_abi_aligned(layout.align) {
50 let mut c = match layout.abi {
51 layout::Abi::Uninhabited => return Ok(()),
53 layout::Abi::Scalar(ref scalar) => {
56 layout::Pointer => Class::Int,
58 layout::F64 => Class::Sse
62 layout::Abi::Vector { .. } => Class::Sse,
64 layout::Abi::ScalarPair(..) |
65 layout::Abi::Aggregate { .. } => {
66 match layout.variants {
67 layout::Variants::Single { .. } => {
68 for i in 0..layout.fields.count() {
69 let field_off = off + layout.fields.offset(i);
70 classify(cx, layout.field(cx, i), cls, field_off)?;
74 layout::Variants::Tagged { .. } |
75 layout::Variants::NicheFilling { .. } => return Err(Memory),
81 // Fill in `cls` for scalars (Int/Sse) and vectors (Sse).
82 let first = (off.bytes() / 8) as usize;
83 let last = ((off.bytes() + layout.size.bytes() - 1) / 8) as usize;
84 for cls in &mut cls[first..=last] {
85 *cls = Some(cls.map_or(c, |old| old.min(c)));
87 // Everything after the first Sse "eightbyte"
88 // component is the upper half of a register.
97 let n = ((arg.layout.size.bytes() + 7) / 8) as usize;
98 if n > MAX_EIGHTBYTES {
102 let mut cls = [None; MAX_EIGHTBYTES];
103 classify(cx, arg.layout, &mut cls, Size::from_bytes(0))?;
105 if cls[0] != Some(Class::Sse) {
108 if cls[1..n].iter().any(|&c| c != Some(Class::SseUp)) {
114 if cls[i] == Some(Class::SseUp) {
115 cls[i] = Some(Class::Sse);
116 } else if cls[i] == Some(Class::Sse) {
118 while i != n && cls[i] == Some(Class::SseUp) { i += 1; }
128 fn reg_component(cls: &[Option<Class>], i: &mut usize, size: Size) -> Option<Reg> {
135 Some(Class::Int) => {
137 Some(if size.bytes() < 8 {
139 kind: RegKind::Integer,
146 Some(Class::Sse) => {
147 let vec_len = 1 + cls[*i+1..].iter()
148 .take_while(|&&c| c == Some(Class::SseUp))
151 Some(if vec_len == 1 {
158 kind: RegKind::Vector,
159 size: Size::from_bytes(8) * (vec_len as u64)
163 Some(c) => bug!("reg_component: unhandled class {:?}", c)
167 fn cast_target(cls: &[Option<Class>], size: Size) -> CastTarget {
169 let lo = reg_component(cls, &mut i, size).unwrap();
170 let offset = Size::from_bytes(8) * (i as u64);
171 let mut target = CastTarget::from(lo);
173 if let Some(hi) = reg_component(cls, &mut i, size - offset) {
174 target = CastTarget::Pair(lo, hi);
177 assert_eq!(reg_component(cls, &mut i, Size::from_bytes(0)), None);
181 pub fn compute_abi_info<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, fty: &mut FnType<'tcx>) {
182 let mut int_regs = 6; // RDI, RSI, RDX, RCX, R8, R9
183 let mut sse_regs = 8; // XMM0-7
185 let mut x86_64_ty = |arg: &mut ArgType<'tcx>, is_arg: bool| {
186 let mut cls_or_mem = classify_arg(cx, arg);
188 let mut needed_int = 0;
189 let mut needed_sse = 0;
191 if let Ok(cls) = cls_or_mem {
194 Some(Class::Int) => needed_int += 1,
195 Some(Class::Sse) => needed_sse += 1,
199 if arg.layout.is_aggregate() {
200 if int_regs < needed_int || sse_regs < needed_sse {
201 cls_or_mem = Err(Memory);
210 arg.make_indirect_byval();
212 // `sret` parameter thus one less integer register available
218 // split into sized chunks passed individually
219 int_regs -= needed_int;
220 sse_regs -= needed_sse;
222 if arg.layout.is_aggregate() {
223 let size = arg.layout.size;
224 arg.cast_to(cast_target(cls, size))
226 arg.extend_integer_width_to(32);
232 if !fty.ret.is_ignore() {
233 x86_64_ty(&mut fty.ret, false);
236 for arg in &mut fty.args {
237 if arg.is_ignore() { continue; }
238 x86_64_ty(arg, true);