]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/cabi_powerpc.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / librustc_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 abi::{FnType, ArgType};
15 use context::CrateContext;
16 use type_::Type;
17
18 use std::cmp;
19
20 fn align_up_to(off: usize, a: usize) -> usize {
21 return (off + a - 1) / a * a;
22 }
23
24 fn align(off: usize, ty: Type) -> usize {
25 let a = ty_align(ty);
26 return align_up_to(off, a);
27 }
28
29 fn ty_align(ty: Type) -> usize {
30 match ty.kind() {
31 Integer => ((ty.int_width() as usize) + 7) / 8,
32 Pointer => 4,
33 Float => 4,
34 Double => 8,
35 Struct => {
36 if ty.is_packed() {
37 1
38 } else {
39 let str_tys = ty.field_types();
40 str_tys.iter().fold(1, |a, t| cmp::max(a, ty_align(*t)))
41 }
42 }
43 Array => {
44 let elt = ty.element_type();
45 ty_align(elt)
46 }
47 _ => bug!("ty_size: unhandled type")
48 }
49 }
50
51 fn ty_size(ty: Type) -> usize {
52 match ty.kind() {
53 Integer => ((ty.int_width() as usize) + 7) / 8,
54 Pointer => 4,
55 Float => 4,
56 Double => 8,
57 Struct => {
58 if ty.is_packed() {
59 let str_tys = ty.field_types();
60 str_tys.iter().fold(0, |s, t| s + ty_size(*t))
61 } else {
62 let str_tys = ty.field_types();
63 let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
64 align(size, ty)
65 }
66 }
67 Array => {
68 let len = ty.array_length();
69 let elt = ty.element_type();
70 let eltsz = ty_size(elt);
71 len * eltsz
72 }
73 _ => bug!("ty_size: unhandled type")
74 }
75 }
76
77 fn classify_ret_ty(ccx: &CrateContext, ret: &mut ArgType) {
78 if is_reg_ty(ret.ty) {
79 ret.extend_integer_width_to(32);
80 } else {
81 ret.make_indirect(ccx);
82 }
83 }
84
85 fn classify_arg_ty(ccx: &CrateContext, arg: &mut ArgType, offset: &mut usize) {
86 let orig_offset = *offset;
87 let size = ty_size(arg.ty) * 8;
88 let mut align = ty_align(arg.ty);
89
90 align = cmp::min(cmp::max(align, 4), 8);
91 *offset = align_up_to(*offset, align);
92 *offset += align_up_to(size, align * 8) / 8;
93
94 if !is_reg_ty(arg.ty) {
95 arg.cast = Some(struct_ty(ccx, arg.ty));
96 arg.pad = padding_ty(ccx, align, orig_offset);
97 } else {
98 arg.extend_integer_width_to(32);
99 }
100 }
101
102 fn is_reg_ty(ty: Type) -> bool {
103 return match ty.kind() {
104 Integer
105 | Pointer
106 | Float
107 | Double => true,
108 _ => false
109 };
110 }
111
112 fn padding_ty(ccx: &CrateContext, align: usize, offset: usize) -> Option<Type> {
113 if ((align - 1 ) & offset) > 0 {
114 Some(Type::i32(ccx))
115 } else {
116 None
117 }
118 }
119
120 fn coerce_to_int(ccx: &CrateContext, size: usize) -> Vec<Type> {
121 let int_ty = Type::i32(ccx);
122 let mut args = Vec::new();
123
124 let mut n = size / 32;
125 while n > 0 {
126 args.push(int_ty);
127 n -= 1;
128 }
129
130 let r = size % 32;
131 if r > 0 {
132 unsafe {
133 args.push(Type::from_ref(llvm::LLVMIntTypeInContext(ccx.llcx(), r as c_uint)));
134 }
135 }
136
137 args
138 }
139
140 fn struct_ty(ccx: &CrateContext, ty: Type) -> Type {
141 let size = ty_size(ty) * 8;
142 Type::struct_(ccx, &coerce_to_int(ccx, size), false)
143 }
144
145 pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType) {
146 if !fty.ret.is_ignore() {
147 classify_ret_ty(ccx, &mut fty.ret);
148 }
149
150 let mut offset = if fty.ret.is_indirect() { 4 } else { 0 };
151 for arg in &mut fty.args {
152 if arg.is_ignore() { continue; }
153 classify_arg_ty(ccx, arg, &mut offset);
154 }
155 }