]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/cabi_powerpc.rs
complete openbsd support for `std::env`
[rust.git] / src / librustc_trans / trans / cabi_powerpc.rs
1 // Copyright 2014-2015 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 use libc::c_uint;
12 use llvm;
13 use llvm::{Integer, Pointer, Float, Double, Struct, Array};
14 use llvm::{StructRetAttribute, ZExtAttribute};
15 use trans::cabi::{FnType, ArgType};
16 use trans::context::CrateContext;
17 use trans::type_::Type;
18
19 use std::cmp;
20
21 fn align_up_to(off: uint, a: uint) -> uint {
22     return (off + a - 1) / a * a;
23 }
24
25 fn align(off: uint, ty: Type) -> uint {
26     let a = ty_align(ty);
27     return align_up_to(off, a);
28 }
29
30 fn ty_align(ty: Type) -> uint {
31     match ty.kind() {
32         Integer => {
33             unsafe {
34                 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
35             }
36         }
37         Pointer => 4,
38         Float => 4,
39         Double => 8,
40         Struct => {
41           if ty.is_packed() {
42             1
43           } else {
44             let str_tys = ty.field_types();
45             str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
46           }
47         }
48         Array => {
49             let elt = ty.element_type();
50             ty_align(elt)
51         }
52         _ => panic!("ty_size: unhandled type")
53     }
54 }
55
56 fn ty_size(ty: Type) -> uint {
57     match ty.kind() {
58         Integer => {
59             unsafe {
60                 ((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
61             }
62         }
63         Pointer => 4,
64         Float => 4,
65         Double => 8,
66         Struct => {
67             if ty.is_packed() {
68                 let str_tys = ty.field_types();
69                 str_tys.iter().fold(0, |s, t| s + ty_size(*t))
70             } else {
71                 let str_tys = ty.field_types();
72                 let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
73                 align(size, ty)
74             }
75         }
76         Array => {
77             let len = ty.array_length();
78             let elt = ty.element_type();
79             let eltsz = ty_size(elt);
80             len * eltsz
81         }
82         _ => panic!("ty_size: unhandled type")
83     }
84 }
85
86 fn classify_ret_ty(ccx: &CrateContext, ty: Type) -> ArgType {
87     if is_reg_ty(ty) {
88         let attr = if ty == Type::i1(ccx) { Some(ZExtAttribute) } else { None };
89         ArgType::direct(ty, None, None, attr)
90     } else {
91         ArgType::indirect(ty, Some(StructRetAttribute))
92     }
93 }
94
95 fn classify_arg_ty(ccx: &CrateContext, ty: Type, offset: &mut uint) -> ArgType {
96     let orig_offset = *offset;
97     let size = ty_size(ty) * 8;
98     let mut align = ty_align(ty);
99
100     align = cmp::min(cmp::max(align, 4), 8);
101     *offset = align_up_to(*offset, align);
102     *offset += align_up_to(size, align * 8) / 8;
103
104     if is_reg_ty(ty) {
105         let attr = if ty == Type::i1(ccx) { Some(ZExtAttribute) } else { None };
106         ArgType::direct(ty, None, None, attr)
107     } else {
108         ArgType::direct(
109             ty,
110             Some(struct_ty(ccx, ty)),
111             padding_ty(ccx, align, orig_offset),
112             None
113         )
114     }
115 }
116
117 fn is_reg_ty(ty: Type) -> bool {
118     return match ty.kind() {
119         Integer
120         | Pointer
121         | Float
122         | Double => true,
123         _ => false
124     };
125 }
126
127 fn padding_ty(ccx: &CrateContext, align: uint, offset: uint) -> Option<Type> {
128     if ((align - 1 ) & offset) > 0 {
129         Some(Type::i32(ccx))
130     } else {
131         None
132     }
133 }
134
135 fn coerce_to_int(ccx: &CrateContext, size: uint) -> Vec<Type> {
136     let int_ty = Type::i32(ccx);
137     let mut args = Vec::new();
138
139     let mut n = size / 32;
140     while n > 0 {
141         args.push(int_ty);
142         n -= 1;
143     }
144
145     let r = size % 32;
146     if r > 0 {
147         unsafe {
148             args.push(Type::from_ref(llvm::LLVMIntTypeInContext(ccx.llcx(), r as c_uint)));
149         }
150     }
151
152     args
153 }
154
155 fn struct_ty(ccx: &CrateContext, ty: Type) -> Type {
156     let size = ty_size(ty) * 8;
157     Type::struct_(ccx, coerce_to_int(ccx, size).as_slice(), false)
158 }
159
160 pub fn compute_abi_info(ccx: &CrateContext,
161                         atys: &[Type],
162                         rty: Type,
163                         ret_def: bool) -> FnType {
164     let ret_ty = if ret_def {
165         classify_ret_ty(ccx, rty)
166     } else {
167         ArgType::direct(Type::void(ccx), None, None, None)
168     };
169
170     let sret = ret_ty.is_indirect();
171     let mut arg_tys = Vec::new();
172     let mut offset = if sret { 4 } else { 0 };
173
174     for aty in atys {
175         let ty = classify_arg_ty(ccx, *aty, &mut offset);
176         arg_tys.push(ty);
177     };
178
179     return FnType {
180         arg_tys: arg_tys,
181         ret_ty: ret_ty,
182     };
183 }