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