|
| 1 | +// Copyright 2014-2016 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 | +// FIXME: This needs an audit for correctness and completeness. |
| 12 | + |
| 13 | +use llvm::{Integer, Pointer, Float, Double, Struct, Vector, Array}; |
| 14 | +use abi::{self, FnType, ArgType}; |
| 15 | +use context::CrateContext; |
| 16 | +use type_::Type; |
| 17 | + |
| 18 | +fn ty_size(ty: Type) -> usize { |
| 19 | + if ty.kind() == Vector { |
| 20 | + bug!("ty_size: unhandled type") |
| 21 | + } else { |
| 22 | + abi::ty_size(ty, 8) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +fn is_homogenous_aggregate_ty(ty: Type) -> Option<(Type, u64)> { |
| 27 | + fn check_array(ty: Type) -> Option<(Type, u64)> { |
| 28 | + let len = ty.array_length() as u64; |
| 29 | + if len == 0 { |
| 30 | + return None |
| 31 | + } |
| 32 | + let elt = ty.element_type(); |
| 33 | + |
| 34 | + // if our element is an HFA/HVA, so are we; multiply members by our len |
| 35 | + is_homogenous_aggregate_ty(elt).map(|(base_ty, members)| (base_ty, len * members)) |
| 36 | + } |
| 37 | + |
| 38 | + fn check_struct(ty: Type) -> Option<(Type, u64)> { |
| 39 | + let str_tys = ty.field_types(); |
| 40 | + if str_tys.len() == 0 { |
| 41 | + return None |
| 42 | + } |
| 43 | + |
| 44 | + let mut prev_base_ty = None; |
| 45 | + let mut members = 0; |
| 46 | + for opt_homog_agg in str_tys.iter().map(|t| is_homogenous_aggregate_ty(*t)) { |
| 47 | + match (prev_base_ty, opt_homog_agg) { |
| 48 | + // field isn't itself an HFA, so we aren't either |
| 49 | + (_, None) => return None, |
| 50 | + |
| 51 | + // first field - store its type and number of members |
| 52 | + (None, Some((field_ty, field_members))) => { |
| 53 | + prev_base_ty = Some(field_ty); |
| 54 | + members = field_members; |
| 55 | + }, |
| 56 | + |
| 57 | + // 2nd or later field - give up if it's a different type; otherwise incr. members |
| 58 | + (Some(prev_ty), Some((field_ty, field_members))) => { |
| 59 | + if prev_ty != field_ty { |
| 60 | + return None; |
| 61 | + } |
| 62 | + members += field_members; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Because of previous checks, we know prev_base_ty is Some(...) because |
| 68 | + // 1. str_tys has at least one element; and |
| 69 | + // 2. prev_base_ty was filled in (or we would've returned early) |
| 70 | + let (base_ty, members) = (prev_base_ty.unwrap(), members); |
| 71 | + |
| 72 | + // Ensure there is no padding. |
| 73 | + if ty_size(ty) == ty_size(base_ty) * (members as usize) { |
| 74 | + Some((base_ty, members)) |
| 75 | + } else { |
| 76 | + None |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + let homog_agg = match ty.kind() { |
| 81 | + Float => Some((ty, 1)), |
| 82 | + Double => Some((ty, 1)), |
| 83 | + Array => check_array(ty), |
| 84 | + Struct => check_struct(ty), |
| 85 | + _ => None |
| 86 | + }; |
| 87 | + |
| 88 | + // Ensure we have at most eight uniquely addressable members |
| 89 | + homog_agg.and_then(|(base_ty, members)| { |
| 90 | + if members > 0 && members <= 8 { |
| 91 | + Some((base_ty, members)) |
| 92 | + } else { |
| 93 | + None |
| 94 | + } |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +fn classify_ret_ty(ccx: &CrateContext, ret: &mut ArgType) { |
| 99 | + if is_reg_ty(ret.ty) { |
| 100 | + ret.extend_integer_width_to(64); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // don't return aggregates in registers |
| 105 | + ret.make_indirect(ccx); |
| 106 | + |
| 107 | + if let Some((base_ty, members)) = is_homogenous_aggregate_ty(ret.ty) { |
| 108 | + ret.cast = Some(Type::array(&base_ty, members)); |
| 109 | + return; |
| 110 | + } |
| 111 | + let size = ty_size(ret.ty); |
| 112 | + if size <= 16 { |
| 113 | + let llty = if size <= 1 { |
| 114 | + Type::i8(ccx) |
| 115 | + } else if size <= 2 { |
| 116 | + Type::i16(ccx) |
| 117 | + } else if size <= 4 { |
| 118 | + Type::i32(ccx) |
| 119 | + } else if size <= 8 { |
| 120 | + Type::i64(ccx) |
| 121 | + } else { |
| 122 | + Type::array(&Type::i64(ccx), ((size + 7 ) / 8 ) as u64) |
| 123 | + }; |
| 124 | + ret.cast = Some(llty); |
| 125 | + return; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +fn classify_arg_ty(ccx: &CrateContext, arg: &mut ArgType) { |
| 130 | + if is_reg_ty(arg.ty) { |
| 131 | + arg.extend_integer_width_to(64); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + if let Some((base_ty, members)) = is_homogenous_aggregate_ty(arg.ty) { |
| 136 | + arg.cast = Some(Type::array(&base_ty, members)); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + arg.cast = Some(struct_ty(ccx, arg.ty)); |
| 141 | +} |
| 142 | + |
| 143 | +fn is_reg_ty(ty: Type) -> bool { |
| 144 | + match ty.kind() { |
| 145 | + Integer |
| 146 | + | Pointer |
| 147 | + | Float |
| 148 | + | Double => true, |
| 149 | + _ => false |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +fn coerce_to_long(ccx: &CrateContext, size: usize) -> Vec<Type> { |
| 154 | + let long_ty = Type::i64(ccx); |
| 155 | + let mut args = Vec::new(); |
| 156 | + |
| 157 | + let mut n = size / 64; |
| 158 | + while n > 0 { |
| 159 | + args.push(long_ty); |
| 160 | + n -= 1; |
| 161 | + } |
| 162 | + |
| 163 | + let r = size % 64; |
| 164 | + if r > 0 { |
| 165 | + args.push(Type::ix(ccx, r as u64)); |
| 166 | + } |
| 167 | + |
| 168 | + args |
| 169 | +} |
| 170 | + |
| 171 | +fn struct_ty(ccx: &CrateContext, ty: Type) -> Type { |
| 172 | + let size = ty_size(ty) * 8; |
| 173 | + Type::struct_(ccx, &coerce_to_long(ccx, size), false) |
| 174 | +} |
| 175 | + |
| 176 | +pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType) { |
| 177 | + if !fty.ret.is_ignore() { |
| 178 | + classify_ret_ty(ccx, &mut fty.ret); |
| 179 | + } |
| 180 | + |
| 181 | + for arg in &mut fty.args { |
| 182 | + if arg.is_ignore() { continue; } |
| 183 | + classify_arg_ty(ccx, arg); |
| 184 | + } |
| 185 | +} |
0 commit comments