]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/cabi_x86_64.rs
librustc: Automatically change uses of `~[T]` to `Vec<T>` in rustc.
[rust.git] / src / librustc / middle / trans / cabi_x86_64.rs
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.
4 //
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.
10
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
13
14 #[allow(non_uppercase_pattern_statics)];
15
16 use lib::llvm::{llvm, Integer, Pointer, Float, Double};
17 use lib::llvm::{Struct, Array, Attribute};
18 use lib::llvm::{StructRetAttribute, ByValAttribute};
19 use middle::trans::cabi::*;
20 use middle::trans::context::CrateContext;
21
22 use middle::trans::type_::Type;
23
24 use std::cmp;
25 use std::vec;
26
27 #[deriving(Clone, Eq)]
28 enum RegClass {
29     NoClass,
30     Int,
31     SSEFs,
32     SSEFv,
33     SSEDs,
34     SSEDv,
35     SSEInt,
36     SSEUp,
37     X87,
38     X87Up,
39     ComplexX87,
40     Memory
41 }
42
43 trait TypeMethods {
44     fn is_reg_ty(&self) -> bool;
45 }
46
47 impl TypeMethods for Type {
48     fn is_reg_ty(&self) -> bool {
49         match self.kind() {
50             Integer | Pointer | Float | Double => true,
51             _ => false
52         }
53     }
54 }
55
56 impl RegClass {
57     fn is_sse(&self) -> bool {
58         match *self {
59             SSEFs | SSEFv | SSEDs | SSEDv => true,
60             _ => false
61         }
62     }
63 }
64
65 trait ClassList {
66     fn is_pass_byval(&self) -> bool;
67     fn is_ret_bysret(&self) -> bool;
68 }
69
70 impl<'a> ClassList for &'a [RegClass] {
71     fn is_pass_byval(&self) -> bool {
72         if self.len() == 0 { return false; }
73
74         let class = self[0];
75            class == Memory
76         || class == X87
77         || class == ComplexX87
78     }
79
80     fn is_ret_bysret(&self) -> bool {
81         if self.len() == 0 { return false; }
82
83         self[0] == Memory
84     }
85 }
86
87 fn classify_ty(ty: Type) -> Vec<RegClass> {
88     fn align(off: uint, ty: Type) -> uint {
89         let a = ty_align(ty);
90         return (off + a - 1u) / a * a;
91     }
92
93     fn ty_align(ty: Type) -> uint {
94         match ty.kind() {
95             Integer => {
96                 unsafe {
97                     ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
98                 }
99             }
100             Pointer => 8,
101             Float => 4,
102             Double => 8,
103             Struct => {
104               if ty.is_packed() {
105                 1
106               } else {
107                 let str_tys = ty.field_types();
108                 str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
109               }
110             }
111             Array => {
112                 let elt = ty.element_type();
113                 ty_align(elt)
114             }
115             _ => fail!("ty_size: unhandled type")
116         }
117     }
118
119     fn ty_size(ty: Type) -> uint {
120         match ty.kind() {
121             Integer => {
122                 unsafe {
123                     ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
124                 }
125             }
126             Pointer => 8,
127             Float => 4,
128             Double => 8,
129             Struct => {
130                 let str_tys = ty.field_types();
131                 if ty.is_packed() {
132                     str_tys.iter().fold(0, |s, t| s + ty_size(*t))
133                 } else {
134                     let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
135                     align(size, ty)
136                 }
137             }
138             Array => {
139                 let len = ty.array_length();
140                 let elt = ty.element_type();
141                 let eltsz = ty_size(elt);
142                 len * eltsz
143             }
144             _ => fail!("ty_size: unhandled type")
145         }
146     }
147
148     fn all_mem(cls: &mut [RegClass]) {
149         for elt in cls.mut_iter() {
150             *elt = Memory;
151         }
152     }
153
154     fn unify(cls: &mut [RegClass],
155              i: uint,
156              newv: RegClass) {
157         if cls[i] == newv {
158             return;
159         } else if cls[i] == NoClass {
160             cls[i] = newv;
161         } else if newv == NoClass {
162             return;
163         } else if cls[i] == Memory || newv == Memory {
164             cls[i] = Memory;
165         } else if cls[i] == Int || newv == Int {
166             cls[i] = Int;
167         } else if cls[i] == X87 ||
168                   cls[i] == X87Up ||
169                   cls[i] == ComplexX87 ||
170                   newv == X87 ||
171                   newv == X87Up ||
172                   newv == ComplexX87 {
173             cls[i] = Memory;
174         } else {
175             cls[i] = newv;
176         }
177     }
178
179     fn classify_struct(tys: &[Type],
180                        cls: &mut [RegClass], i: uint,
181                        off: uint) {
182         let mut field_off = off;
183         for ty in tys.iter() {
184             field_off = align(field_off, *ty);
185             classify(*ty, cls, i, field_off);
186             field_off += ty_size(*ty);
187         }
188     }
189
190     fn classify(ty: Type,
191                 cls: &mut [RegClass], ix: uint,
192                 off: uint) {
193         let t_align = ty_align(ty);
194         let t_size = ty_size(ty);
195
196         let misalign = off % t_align;
197         if misalign != 0u {
198             let mut i = off / 8u;
199             let e = (off + t_size + 7u) / 8u;
200             while i < e {
201                 unify(cls, ix + i, Memory);
202                 i += 1u;
203             }
204             return;
205         }
206
207         match ty.kind() {
208             Integer |
209             Pointer => {
210                 unify(cls, ix + off / 8u, Int);
211             }
212             Float => {
213                 if off % 8u == 4u {
214                     unify(cls, ix + off / 8u, SSEFv);
215                 } else {
216                     unify(cls, ix + off / 8u, SSEFs);
217                 }
218             }
219             Double => {
220                 unify(cls, ix + off / 8u, SSEDs);
221             }
222             Struct => {
223                 classify_struct(ty.field_types(), cls, ix, off);
224             }
225             Array => {
226                 let len = ty.array_length();
227                 let elt = ty.element_type();
228                 let eltsz = ty_size(elt);
229                 let mut i = 0u;
230                 while i < len {
231                     classify(elt, cls, ix, off + i * eltsz);
232                     i += 1u;
233                 }
234             }
235             _ => fail!("classify: unhandled type")
236         }
237     }
238
239     fn fixup(ty: Type, cls: &mut [RegClass]) {
240         let mut i = 0u;
241         let ty_kind = ty.kind();
242         let e = cls.len();
243         if cls.len() > 2u && (ty_kind == Struct || ty_kind == Array) {
244             if cls[i].is_sse() {
245                 i += 1u;
246                 while i < e {
247                     if cls[i] != SSEUp {
248                         all_mem(cls);
249                         return;
250                     }
251                     i += 1u;
252                 }
253             } else {
254                 all_mem(cls);
255                 return
256             }
257         } else {
258             while i < e {
259                 if cls[i] == Memory {
260                     all_mem(cls);
261                     return;
262                 }
263                 if cls[i] == X87Up {
264                     // for darwin
265                     // cls[i] = SSEDs;
266                     all_mem(cls);
267                     return;
268                 }
269                 if cls[i] == SSEUp {
270                     cls[i] = SSEDv;
271                 } else if cls[i].is_sse() {
272                     i += 1;
273                     while i != e && cls[i] == SSEUp { i += 1u; }
274                 } else if cls[i] == X87 {
275                     i += 1;
276                     while i != e && cls[i] == X87Up { i += 1u; }
277                 } else {
278                     i += 1;
279                 }
280             }
281         }
282     }
283
284     let words = (ty_size(ty) + 7) / 8;
285     let mut cls = vec::from_elem(words, NoClass);
286     if words > 4 {
287         all_mem(cls);
288         return cls;
289     }
290     classify(ty, cls, 0, 0);
291     fixup(ty, cls);
292     return cls;
293 }
294
295 fn llreg_ty(cls: &[RegClass]) -> Type {
296     fn llvec_len(cls: &[RegClass]) -> uint {
297         let mut len = 1u;
298         for c in cls.iter() {
299             if *c != SSEUp {
300                 break;
301             }
302             len += 1u;
303         }
304         return len;
305     }
306
307     let mut tys = Vec::new();
308     let mut i = 0u;
309     let e = cls.len();
310     while i < e {
311         match cls[i] {
312             Int => {
313                 tys.push(Type::i64());
314             }
315             SSEFv => {
316                 let vec_len = llvec_len(cls.tailn(i + 1u));
317                 let vec_ty = Type::vector(&Type::f32(), (vec_len * 2u) as u64);
318                 tys.push(vec_ty);
319                 i += vec_len;
320                 continue;
321             }
322             SSEFs => {
323                 tys.push(Type::f32());
324             }
325             SSEDs => {
326                 tys.push(Type::f64());
327             }
328             _ => fail!("llregtype: unhandled class")
329         }
330         i += 1u;
331     }
332     return Type::struct_(tys, false);
333 }
334
335 pub fn compute_abi_info(_ccx: &CrateContext,
336                         atys: &[Type],
337                         rty: Type,
338                         ret_def: bool) -> FnType {
339     fn x86_64_ty(ty: Type,
340                  is_mem_cls: |cls: &[RegClass]| -> bool,
341                  attr: Attribute)
342                  -> ArgType {
343         if !ty.is_reg_ty() {
344             let cls = classify_ty(ty);
345             if is_mem_cls(cls) {
346                 ArgType::indirect(ty, Some(attr))
347             } else {
348                 ArgType::direct(ty, Some(llreg_ty(cls)), None, None)
349             }
350         } else {
351             ArgType::direct(ty, None, None, None)
352         }
353     }
354
355     let mut arg_tys = Vec::new();
356     for t in atys.iter() {
357         let ty = x86_64_ty(*t, |cls| cls.is_pass_byval(), ByValAttribute);
358         arg_tys.push(ty);
359     }
360
361     let ret_ty = if ret_def {
362         x86_64_ty(rty, |cls| cls.is_ret_bysret(), StructRetAttribute)
363     } else {
364         ArgType::direct(Type::void(), None, None, None)
365     };
366
367     return FnType {
368         arg_tys: arg_tys,
369         ret_ty: ret_ty,
370     };
371 }