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_uppercase_statics)]
17 use llvm::{Integer, Pointer, Float, Double};
18 use llvm::{Struct, Array, Attribute};
19 use llvm::{StructRetAttribute, ByValAttribute, ZExtAttribute};
20 use middle::trans::cabi::{ArgType, FnType};
21 use middle::trans::context::CrateContext;
22 use middle::trans::type_::Type;
26 #[deriving(Clone, PartialEq)]
43 fn is_reg_ty(&self) -> bool;
46 impl TypeMethods for Type {
47 fn is_reg_ty(&self) -> bool {
49 Integer | Pointer | Float | Double => true,
56 fn is_sse(&self) -> bool {
58 SSEFs | SSEFv | SSEDs | SSEDv => true,
65 fn is_pass_byval(&self) -> bool;
66 fn is_ret_bysret(&self) -> bool;
69 impl<'a> ClassList for &'a [RegClass] {
70 fn is_pass_byval(&self) -> bool {
71 if self.len() == 0 { return false; }
76 || class == ComplexX87
79 fn is_ret_bysret(&self) -> bool {
80 if self.len() == 0 { return false; }
86 fn classify_ty(ty: Type) -> Vec<RegClass> {
87 fn align(off: uint, ty: Type) -> uint {
89 return (off + a - 1u) / a * a;
92 fn ty_align(ty: Type) -> uint {
96 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
106 let str_tys = ty.field_types();
107 str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
111 let elt = ty.element_type();
114 _ => fail!("ty_size: unhandled type")
118 fn ty_size(ty: Type) -> uint {
122 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
129 let str_tys = ty.field_types();
131 str_tys.iter().fold(0, |s, t| s + ty_size(*t))
133 let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
138 let len = ty.array_length();
139 let elt = ty.element_type();
140 let eltsz = ty_size(elt);
143 _ => fail!("ty_size: unhandled type")
147 fn all_mem(cls: &mut [RegClass]) {
148 for elt in cls.mut_iter() {
153 fn unify(cls: &mut [RegClass],
158 } else if cls[i] == NoClass {
160 } else if newv == NoClass {
162 } else if cls[i] == Memory || newv == Memory {
164 } else if cls[i] == Int || newv == Int {
166 } else if cls[i] == X87 ||
168 cls[i] == ComplexX87 ||
178 fn classify_struct(tys: &[Type],
179 cls: &mut [RegClass],
183 let mut field_off = off;
184 for ty in tys.iter() {
186 field_off = align(field_off, *ty);
188 classify(*ty, cls, i, field_off);
189 field_off += ty_size(*ty);
193 fn classify(ty: Type,
194 cls: &mut [RegClass], ix: uint,
196 let t_align = ty_align(ty);
197 let t_size = ty_size(ty);
199 let misalign = off % t_align;
201 let mut i = off / 8u;
202 let e = (off + t_size + 7u) / 8u;
204 unify(cls, ix + i, Memory);
213 unify(cls, ix + off / 8u, Int);
217 unify(cls, ix + off / 8u, SSEFv);
219 unify(cls, ix + off / 8u, SSEFs);
223 unify(cls, ix + off / 8u, SSEDs);
226 classify_struct(ty.field_types().as_slice(), cls, ix, off, ty.is_packed());
229 let len = ty.array_length();
230 let elt = ty.element_type();
231 let eltsz = ty_size(elt);
234 classify(elt, cls, ix, off + i * eltsz);
238 _ => fail!("classify: unhandled type")
242 fn fixup(ty: Type, cls: &mut [RegClass]) {
244 let ty_kind = ty.kind();
246 if cls.len() > 2u && (ty_kind == Struct || ty_kind == Array) {
262 if cls[i] == Memory {
274 } else if cls[i].is_sse() {
276 while i != e && cls[i] == SSEUp { i += 1u; }
277 } else if cls[i] == X87 {
279 while i != e && cls[i] == X87Up { i += 1u; }
287 let words = (ty_size(ty) + 7) / 8;
288 let mut cls = Vec::from_elem(words, NoClass);
290 all_mem(cls.as_mut_slice());
293 classify(ty, cls.as_mut_slice(), 0, 0);
294 fixup(ty, cls.as_mut_slice());
298 fn llreg_ty(ccx: &CrateContext, cls: &[RegClass]) -> Type {
299 fn llvec_len(cls: &[RegClass]) -> uint {
301 for c in cls.iter() {
310 let mut tys = Vec::new();
316 tys.push(Type::i64(ccx));
319 let vec_len = llvec_len(cls.tailn(i + 1u));
320 let vec_ty = Type::vector(&Type::f32(ccx), (vec_len * 2u) as u64);
326 tys.push(Type::f32(ccx));
329 tys.push(Type::f64(ccx));
331 _ => fail!("llregtype: unhandled class")
335 return Type::struct_(ccx, tys.as_slice(), false);
338 pub fn compute_abi_info(ccx: &CrateContext,
341 ret_def: bool) -> FnType {
342 fn x86_64_ty(ccx: &CrateContext,
344 is_mem_cls: |cls: &[RegClass]| -> bool,
348 let cls = classify_ty(ty);
349 if is_mem_cls(cls.as_slice()) {
350 ArgType::indirect(ty, Some(ind_attr))
353 Some(llreg_ty(ccx, cls.as_slice())),
358 let attr = if ty == Type::i1(ccx) { Some(ZExtAttribute) } else { None };
359 ArgType::direct(ty, None, None, attr)
363 let mut arg_tys = Vec::new();
364 for t in atys.iter() {
365 let ty = x86_64_ty(ccx, *t, |cls| cls.is_pass_byval(), ByValAttribute);
369 let ret_ty = if ret_def {
370 x86_64_ty(ccx, rty, |cls| cls.is_ret_bysret(), StructRetAttribute)
372 ArgType::direct(Type::void(ccx), None, None, None)