|
| 1 | +// Copyright 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: The assumes we're using the non-vector ABI, i.e. compiling |
| 12 | +// for a pre-z13 machine or using -mno-vx. |
| 13 | + |
| 14 | +use llvm::{Integer, Pointer, Float, Double, Struct, Array, Vector}; |
| 15 | +use abi::{FnType, ArgType}; |
| 16 | +use context::CrateContext; |
| 17 | +use type_::Type; |
| 18 | + |
| 19 | +use std::cmp; |
| 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 => 8, |
| 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 => ty_size(ty), |
| 49 | + _ => bug!("ty_align: unhandled type") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn ty_size(ty: Type) -> usize { |
| 54 | + match ty.kind() { |
| 55 | + Integer => ((ty.int_width() as usize) + 7) / 8, |
| 56 | + Pointer => 8, |
| 57 | + Float => 4, |
| 58 | + Double => 8, |
| 59 | + Struct => { |
| 60 | + if ty.is_packed() { |
| 61 | + let str_tys = ty.field_types(); |
| 62 | + str_tys.iter().fold(0, |s, t| s + ty_size(*t)) |
| 63 | + } else { |
| 64 | + let str_tys = ty.field_types(); |
| 65 | + let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t)); |
| 66 | + align(size, ty) |
| 67 | + } |
| 68 | + } |
| 69 | + Array => { |
| 70 | + let len = ty.array_length(); |
| 71 | + let elt = ty.element_type(); |
| 72 | + let eltsz = ty_size(elt); |
| 73 | + len * eltsz |
| 74 | + } |
| 75 | + Vector => { |
| 76 | + let len = ty.vector_length(); |
| 77 | + let elt = ty.element_type(); |
| 78 | + let eltsz = ty_size(elt); |
| 79 | + len * eltsz |
| 80 | + } |
| 81 | + _ => bug!("ty_size: unhandled type") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fn classify_ret_ty(ccx: &CrateContext, ret: &mut ArgType) { |
| 86 | + if is_reg_ty(ret.ty) { |
| 87 | + ret.extend_integer_width_to(64); |
| 88 | + } else { |
| 89 | + ret.make_indirect(ccx); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +fn classify_arg_ty(ccx: &CrateContext, arg: &mut ArgType) { |
| 94 | + if arg.ty.kind() == Struct { |
| 95 | + fn is_single_fp_element(tys: &[Type]) -> bool { |
| 96 | + if tys.len() != 1 { |
| 97 | + return false; |
| 98 | + } |
| 99 | + match tys[0].kind() { |
| 100 | + Float | Double => true, |
| 101 | + Struct => is_single_fp_element(&tys[0].field_types()), |
| 102 | + _ => false |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if is_single_fp_element(&arg.ty.field_types()) { |
| 107 | + match ty_size(arg.ty) { |
| 108 | + 4 => arg.cast = Some(Type::f32(ccx)), |
| 109 | + 8 => arg.cast = Some(Type::f64(ccx)), |
| 110 | + _ => arg.make_indirect(ccx) |
| 111 | + } |
| 112 | + } else { |
| 113 | + match ty_size(arg.ty) { |
| 114 | + 1 => arg.cast = Some(Type::i8(ccx)), |
| 115 | + 2 => arg.cast = Some(Type::i16(ccx)), |
| 116 | + 4 => arg.cast = Some(Type::i32(ccx)), |
| 117 | + 8 => arg.cast = Some(Type::i64(ccx)), |
| 118 | + _ => arg.make_indirect(ccx) |
| 119 | + } |
| 120 | + } |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if is_reg_ty(arg.ty) { |
| 125 | + arg.extend_integer_width_to(64); |
| 126 | + } else { |
| 127 | + arg.make_indirect(ccx); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +fn is_reg_ty(ty: Type) -> bool { |
| 132 | + match ty.kind() { |
| 133 | + Integer |
| 134 | + | Pointer |
| 135 | + | Float |
| 136 | + | Double => ty_size(ty) <= 8, |
| 137 | + _ => false |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +pub fn compute_abi_info(ccx: &CrateContext, fty: &mut FnType) { |
| 142 | + if !fty.ret.is_ignore() { |
| 143 | + classify_ret_ty(ccx, &mut fty.ret); |
| 144 | + } |
| 145 | + |
| 146 | + for arg in &mut fty.args { |
| 147 | + if arg.is_ignore() { continue; } |
| 148 | + classify_arg_ty(ccx, arg); |
| 149 | + } |
| 150 | +} |
0 commit comments