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