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 #![allow(non_upper_case_globals)]
15 use self::RegClass::*;
18 use llvm::{Integer, Pointer, Float, Double};
19 use llvm::{Struct, Array, Attribute};
20 use llvm::{StructRetAttribute, ByValAttribute, ZExtAttribute};
21 use trans::cabi::{ArgType, FnType};
22 use trans::context::CrateContext;
23 use trans::type_::Type;
27 #[deriving(Clone, PartialEq)]
44 fn is_reg_ty(&self) -> bool;
47 impl TypeMethods for Type {
48 fn is_reg_ty(&self) -> bool {
50 Integer | Pointer | Float | Double => true,
57 fn is_sse(&self) -> bool {
59 SSEFs | SSEFv | SSEDs | SSEDv => true,
65 trait ClassList for Sized? {
66 fn is_pass_byval(&self) -> bool;
67 fn is_ret_bysret(&self) -> bool;
70 impl ClassList for [RegClass] {
71 fn is_pass_byval(&self) -> bool {
72 if self.len() == 0 { return false; }
77 || class == ComplexX87
80 fn is_ret_bysret(&self) -> bool {
81 if self.len() == 0 { return false; }
87 fn classify_ty(ty: Type) -> Vec<RegClass> {
88 fn align(off: uint, ty: Type) -> uint {
90 return (off + a - 1u) / a * a;
93 fn ty_align(ty: Type) -> uint {
97 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
107 let str_tys = ty.field_types();
108 str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
112 let elt = ty.element_type();
115 _ => panic!("ty_size: unhandled type")
119 fn ty_size(ty: Type) -> uint {
123 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
130 let str_tys = ty.field_types();
132 str_tys.iter().fold(0, |s, t| s + ty_size(*t))
134 let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
139 let len = ty.array_length();
140 let elt = ty.element_type();
141 let eltsz = ty_size(elt);
144 _ => panic!("ty_size: unhandled type")
148 fn all_mem(cls: &mut [RegClass]) {
149 for elt in cls.iter_mut() {
154 fn unify(cls: &mut [RegClass],
159 } else if cls[i] == NoClass {
161 } else if newv == NoClass {
163 } else if cls[i] == Memory || newv == Memory {
165 } else if cls[i] == Int || newv == Int {
167 } else if cls[i] == X87 ||
169 cls[i] == ComplexX87 ||
179 fn classify_struct(tys: &[Type],
180 cls: &mut [RegClass],
184 let mut field_off = off;
185 for ty in tys.iter() {
187 field_off = align(field_off, *ty);
189 classify(*ty, cls, i, field_off);
190 field_off += ty_size(*ty);
194 fn classify(ty: Type,
195 cls: &mut [RegClass], ix: uint,
197 let t_align = ty_align(ty);
198 let t_size = ty_size(ty);
200 let misalign = off % t_align;
202 let mut i = off / 8u;
203 let e = (off + t_size + 7u) / 8u;
205 unify(cls, ix + i, Memory);
214 unify(cls, ix + off / 8u, Int);
218 unify(cls, ix + off / 8u, SSEFv);
220 unify(cls, ix + off / 8u, SSEFs);
224 unify(cls, ix + off / 8u, SSEDs);
227 classify_struct(ty.field_types().as_slice(), cls, ix, off, ty.is_packed());
230 let len = ty.array_length();
231 let elt = ty.element_type();
232 let eltsz = ty_size(elt);
235 classify(elt, cls, ix, off + i * eltsz);
239 _ => panic!("classify: unhandled type")
243 fn fixup(ty: Type, cls: &mut [RegClass]) {
245 let ty_kind = ty.kind();
247 if cls.len() > 2u && (ty_kind == Struct || ty_kind == Array) {
263 if cls[i] == Memory {
275 } else if cls[i].is_sse() {
277 while i != e && cls[i] == SSEUp { i += 1u; }
278 } else if cls[i] == X87 {
280 while i != e && cls[i] == X87Up { i += 1u; }
288 let words = (ty_size(ty) + 7) / 8;
289 let mut cls = Vec::from_elem(words, NoClass);
291 all_mem(cls.as_mut_slice());
294 classify(ty, cls.as_mut_slice(), 0, 0);
295 fixup(ty, cls.as_mut_slice());
299 fn llreg_ty(ccx: &CrateContext, cls: &[RegClass]) -> Type {
300 fn llvec_len(cls: &[RegClass]) -> uint {
302 for c in cls.iter() {
311 let mut tys = Vec::new();
317 tys.push(Type::i64(ccx));
320 let vec_len = llvec_len(cls[i + 1u..]);
321 let vec_ty = Type::vector(&Type::f32(ccx), (vec_len * 2u) as u64);
327 tys.push(Type::f32(ccx));
330 tys.push(Type::f64(ccx));
332 _ => panic!("llregtype: unhandled class")
336 return Type::struct_(ccx, tys.as_slice(), false);
339 pub fn compute_abi_info(ccx: &CrateContext,
342 ret_def: bool) -> FnType {
343 fn x86_64_ty(ccx: &CrateContext,
345 is_mem_cls: |cls: &[RegClass]| -> bool,
349 let cls = classify_ty(ty);
350 if is_mem_cls(cls.as_slice()) {
351 ArgType::indirect(ty, Some(ind_attr))
354 Some(llreg_ty(ccx, cls.as_slice())),
359 let attr = if ty == Type::i1(ccx) { Some(ZExtAttribute) } else { None };
360 ArgType::direct(ty, None, None, attr)
364 let mut arg_tys = Vec::new();
365 for t in atys.iter() {
366 let ty = x86_64_ty(ccx, *t, |cls| cls.is_pass_byval(), ByValAttribute);
370 let ret_ty = if ret_def {
371 x86_64_ty(ccx, rty, |cls| cls.is_ret_bysret(), StructRetAttribute)
373 ArgType::direct(Type::void(ccx), None, None, None)