|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| -#![allow(non_upper_case_globals)] |
12 |
| - |
13 |
| -use llvm::{Integer, Pointer, Float, Double, Struct, Array, Vector}; |
14 |
| -use abi::{self, FnType, ArgType}; |
| 11 | +use abi::{FnType, ArgType, LayoutExt, Reg, RegKind, Uniform}; |
15 | 12 | use context::CrateContext;
|
16 |
| -use type_::Type; |
17 |
| - |
18 |
| -fn ty_size(ty: Type) -> usize { |
19 |
| - abi::ty_size(ty, 8) |
20 |
| -} |
21 |
| - |
22 |
| -fn is_homogenous_aggregate_ty(ty: Type) -> Option<(Type, u64)> { |
23 |
| - fn check_array(ty: Type) -> Option<(Type, u64)> { |
24 |
| - let len = ty.array_length() as u64; |
25 |
| - if len == 0 { |
26 |
| - return None |
27 |
| - } |
28 |
| - let elt = ty.element_type(); |
29 |
| - |
30 |
| - // if our element is an HFA/HVA, so are we; multiply members by our len |
31 |
| - is_homogenous_aggregate_ty(elt).map(|(base_ty, members)| (base_ty, len * members)) |
32 |
| - } |
33 |
| - |
34 |
| - fn check_struct(ty: Type) -> Option<(Type, u64)> { |
35 |
| - let str_tys = ty.field_types(); |
36 |
| - if str_tys.len() == 0 { |
37 |
| - return None |
38 |
| - } |
39 |
| - |
40 |
| - let mut prev_base_ty = None; |
41 |
| - let mut members = 0; |
42 |
| - for opt_homog_agg in str_tys.iter().map(|t| is_homogenous_aggregate_ty(*t)) { |
43 |
| - match (prev_base_ty, opt_homog_agg) { |
44 |
| - // field isn't itself an HFA, so we aren't either |
45 |
| - (_, None) => return None, |
46 |
| - |
47 |
| - // first field - store its type and number of members |
48 |
| - (None, Some((field_ty, field_members))) => { |
49 |
| - prev_base_ty = Some(field_ty); |
50 |
| - members = field_members; |
51 |
| - }, |
52 | 13 |
|
53 |
| - // 2nd or later field - give up if it's a different type; otherwise incr. members |
54 |
| - (Some(prev_ty), Some((field_ty, field_members))) => { |
55 |
| - if prev_ty != field_ty { |
56 |
| - return None; |
57 |
| - } |
58 |
| - members += field_members; |
59 |
| - } |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - // Because of previous checks, we know prev_base_ty is Some(...) because |
64 |
| - // 1. str_tys has at least one element; and |
65 |
| - // 2. prev_base_ty was filled in (or we would've returned early) |
66 |
| - let (base_ty, members) = (prev_base_ty.unwrap(), members); |
| 14 | +fn is_homogenous_aggregate<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, arg: &mut ArgType<'tcx>) |
| 15 | + -> Option<Uniform> { |
| 16 | + arg.layout.homogenous_aggregate(ccx).and_then(|unit| { |
| 17 | + let size = arg.layout.size(ccx); |
67 | 18 |
|
68 |
| - // Ensure there is no padding. |
69 |
| - if ty_size(ty) == ty_size(base_ty) * (members as usize) { |
70 |
| - Some((base_ty, members)) |
71 |
| - } else { |
72 |
| - None |
| 19 | + // Ensure we have at most four uniquely addressable members. |
| 20 | + if size > unit.size.checked_mul(4, ccx).unwrap() { |
| 21 | + return None; |
73 | 22 | }
|
74 |
| - } |
75 | 23 |
|
76 |
| - let homog_agg = match ty.kind() { |
77 |
| - Float => Some((ty, 1)), |
78 |
| - Double => Some((ty, 1)), |
79 |
| - Array => check_array(ty), |
80 |
| - Struct => check_struct(ty), |
81 |
| - Vector => match ty_size(ty) { |
82 |
| - 4|8 => Some((ty, 1)), |
83 |
| - _ => None |
84 |
| - }, |
85 |
| - _ => None |
86 |
| - }; |
| 24 | + let valid_unit = match unit.kind { |
| 25 | + RegKind::Integer => false, |
| 26 | + RegKind::Float => true, |
| 27 | + RegKind::Vector => size.bits() == 64 || size.bits() == 128 |
| 28 | + }; |
87 | 29 |
|
88 |
| - // Ensure we have at most four uniquely addressable members |
89 |
| - homog_agg.and_then(|(base_ty, members)| { |
90 |
| - if members > 0 && members <= 4 { |
91 |
| - Some((base_ty, members)) |
| 30 | + if valid_unit { |
| 31 | + Some(Uniform { |
| 32 | + unit, |
| 33 | + total: size |
| 34 | + }) |
92 | 35 | } else {
|
93 | 36 | None
|
94 | 37 | }
|
95 | 38 | })
|
96 | 39 | }
|
97 | 40 |
|
98 |
| -fn classify_ret_ty(ccx: &CrateContext, ret: &mut ArgType) { |
99 |
| - if is_reg_ty(ret.ty) { |
| 41 | +fn classify_ret_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ret: &mut ArgType<'tcx>) { |
| 42 | + if !ret.layout.is_aggregate() { |
100 | 43 | ret.extend_integer_width_to(32);
|
101 | 44 | return;
|
102 | 45 | }
|
103 |
| - if let Some((base_ty, members)) = is_homogenous_aggregate_ty(ret.ty) { |
104 |
| - ret.cast = Some(Type::array(&base_ty, members)); |
| 46 | + if let Some(uniform) = is_homogenous_aggregate(ccx, ret) { |
| 47 | + ret.cast_to(ccx, uniform); |
105 | 48 | return;
|
106 | 49 | }
|
107 |
| - let size = ty_size(ret.ty); |
108 |
| - if size <= 16 { |
109 |
| - let llty = if size <= 1 { |
110 |
| - Type::i8(ccx) |
111 |
| - } else if size <= 2 { |
112 |
| - Type::i16(ccx) |
113 |
| - } else if size <= 4 { |
114 |
| - Type::i32(ccx) |
115 |
| - } else if size <= 8 { |
116 |
| - Type::i64(ccx) |
| 50 | + let size = ret.layout.size(ccx); |
| 51 | + let bits = size.bits(); |
| 52 | + if bits <= 128 { |
| 53 | + let unit = if bits <= 8 { |
| 54 | + Reg::i8() |
| 55 | + } else if bits <= 16 { |
| 56 | + Reg::i16() |
| 57 | + } else if bits <= 32 { |
| 58 | + Reg::i32() |
117 | 59 | } else {
|
118 |
| - Type::array(&Type::i64(ccx), ((size + 7 ) / 8 ) as u64) |
| 60 | + Reg::i64() |
119 | 61 | };
|
120 |
| - ret.cast = Some(llty); |
| 62 | + |
| 63 | + ret.cast_to(ccx, Uniform { |
| 64 | + unit, |
| 65 | + total: size |
| 66 | + }); |
121 | 67 | return;
|
122 | 68 | }
|
123 | 69 | ret.make_indirect(ccx);
|
124 | 70 | }
|
125 | 71 |
|
126 |
| -fn classify_arg_ty(ccx: &CrateContext, arg: &mut ArgType) { |
127 |
| - if is_reg_ty(arg.ty) { |
| 72 | +fn classify_arg_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, arg: &mut ArgType<'tcx>) { |
| 73 | + if !arg.layout.is_aggregate() { |
128 | 74 | arg.extend_integer_width_to(32);
|
129 | 75 | return;
|
130 | 76 | }
|
131 |
| - if let Some((base_ty, members)) = is_homogenous_aggregate_ty(arg.ty) { |
132 |
| - arg.cast = Some(Type::array(&base_ty, members)); |
| 77 | + if let Some(uniform) = is_homogenous_aggregate(ccx, arg) { |
| 78 | + arg.cast_to(ccx, uniform); |
133 | 79 | return;
|
134 | 80 | }
|
135 |
| - let size = ty_size(arg.ty); |
136 |
| - if size <= 16 { |
137 |
| - let llty = if size == 0 { |
138 |
| - Type::array(&Type::i64(ccx), 0) |
139 |
| - } else if size == 1 { |
140 |
| - Type::i8(ccx) |
141 |
| - } else if size == 2 { |
142 |
| - Type::i16(ccx) |
143 |
| - } else if size <= 4 { |
144 |
| - Type::i32(ccx) |
145 |
| - } else if size <= 8 { |
146 |
| - Type::i64(ccx) |
| 81 | + let size = arg.layout.size(ccx); |
| 82 | + let bits = size.bits(); |
| 83 | + if bits <= 128 { |
| 84 | + let unit = if bits <= 8 { |
| 85 | + Reg::i8() |
| 86 | + } else if bits <= 16 { |
| 87 | + Reg::i16() |
| 88 | + } else if bits <= 32 { |
| 89 | + Reg::i32() |
147 | 90 | } else {
|
148 |
| - Type::array(&Type::i64(ccx), ((size + 7 ) / 8 ) as u64) |
| 91 | + Reg::i64() |
149 | 92 | };
|
150 |
| - arg.cast = Some(llty); |
| 93 | + |
| 94 | + arg.cast_to(ccx, Uniform { |
| 95 | + unit, |
| 96 | + total: size |
| 97 | + }); |
151 | 98 | return;
|
152 | 99 | }
|
153 | 100 | arg.make_indirect(ccx);
|
154 | 101 | }
|
155 | 102 |
|
156 |
| -fn is_reg_ty(ty: Type) -> bool { |
157 |
| - match ty.kind() { |
158 |
| - Integer |
159 |
| - | Pointer |
160 |
| - | Float |
161 |
| - | Double |
162 |
| - | Vector => true, |
163 |
| - _ => false |
164 |
| - } |
165 |
| -} |
166 |
| - |
167 |
| -pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType) { |
| 103 | +pub fn compute_abi_info<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fty: &mut FnType<'tcx>) { |
168 | 104 | if !fty.ret.is_ignore() {
|
169 | 105 | classify_ret_ty(ccx, &mut fty.ret);
|
170 | 106 | }
|
|
0 commit comments