2013-04-10 07:47:22 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2013-01-25 16:56:56 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// The classification code for the x86_64 ABI is taken from the clay language
|
|
|
|
// https://github.com/jckarter/clay/blob/master/compiler/src/externals.cpp
|
|
|
|
|
2013-09-30 10:44:58 -05:00
|
|
|
#[allow(non_uppercase_pattern_statics)];
|
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
use lib::llvm::{llvm, Integer, Pointer, Float, Double};
|
2013-01-25 16:56:56 -06:00
|
|
|
use lib::llvm::{Struct, Array, Attribute};
|
|
|
|
use lib::llvm::{StructRetAttribute, ByValAttribute};
|
|
|
|
use middle::trans::cabi::*;
|
2013-05-21 14:25:44 -05:00
|
|
|
use middle::trans::context::CrateContext;
|
2013-01-25 16:56:56 -06:00
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
use middle::trans::type_::Type;
|
|
|
|
|
Replaces the free-standing functions in f32, &c.
The free-standing functions in f32, f64, i8, i16, i32, i64, u8, u16,
u32, u64, float, int, and uint are replaced with generic functions in
num instead.
If you were previously using any of those functions, just replace them
with the corresponding function with the same name in num.
Note: If you were using a function that corresponds to an operator, use
the operator instead.
2013-07-08 11:05:17 -05:00
|
|
|
use std::num;
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::option;
|
|
|
|
use std::option::Option;
|
|
|
|
use std::vec;
|
2013-02-25 13:11:21 -06:00
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2013-06-15 22:45:48 -05:00
|
|
|
enum RegClass {
|
|
|
|
NoClass,
|
2013-06-16 05:52:44 -05:00
|
|
|
Int,
|
2013-06-15 22:45:48 -05:00
|
|
|
SSEFs,
|
|
|
|
SSEFv,
|
|
|
|
SSEDs,
|
|
|
|
SSEDv,
|
|
|
|
SSEInt,
|
|
|
|
SSEUp,
|
|
|
|
X87,
|
|
|
|
X87Up,
|
|
|
|
ComplexX87,
|
|
|
|
Memory
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
|
2013-06-25 20:25:27 -05:00
|
|
|
trait TypeMethods {
|
|
|
|
fn is_reg_ty(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeMethods for Type {
|
2013-06-15 22:45:48 -05:00
|
|
|
fn is_reg_ty(&self) -> bool {
|
2013-06-16 05:52:44 -05:00
|
|
|
match self.kind() {
|
2013-06-15 22:45:48 -05:00
|
|
|
Integer | Pointer | Float | Double => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RegClass {
|
|
|
|
fn is_sse(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
SSEFs | SSEFv | SSEDs | SSEDv => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
trait ClassList {
|
|
|
|
fn is_pass_byval(&self) -> bool;
|
|
|
|
fn is_ret_bysret(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
impl<'self> ClassList for &'self [RegClass] {
|
|
|
|
fn is_pass_byval(&self) -> bool {
|
|
|
|
if self.len() == 0 { return false; }
|
|
|
|
|
|
|
|
let class = self[0];
|
|
|
|
class == Memory
|
|
|
|
|| class == X87
|
|
|
|
|| class == ComplexX87
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_ret_bysret(&self) -> bool {
|
|
|
|
if self.len() == 0 { return false; }
|
|
|
|
|
|
|
|
self[0] == Memory
|
|
|
|
}
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
fn classify_ty(ty: Type) -> ~[RegClass] {
|
|
|
|
fn align(off: uint, ty: Type) -> uint {
|
2013-01-25 16:56:56 -06:00
|
|
|
let a = ty_align(ty);
|
|
|
|
return (off + a - 1u) / a * a;
|
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
fn ty_align(ty: Type) -> uint {
|
2013-06-16 05:52:44 -05:00
|
|
|
match ty.kind() {
|
|
|
|
Integer => {
|
|
|
|
unsafe {
|
2013-06-15 22:45:48 -05:00
|
|
|
((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
2013-06-16 05:52:44 -05:00
|
|
|
}
|
|
|
|
Pointer => 8,
|
|
|
|
Float => 4,
|
|
|
|
Double => 8,
|
|
|
|
Struct => {
|
|
|
|
if ty.is_packed() {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
let str_tys = ty.field_types();
|
Replaces the free-standing functions in f32, &c.
The free-standing functions in f32, f64, i8, i16, i32, i64, u8, u16,
u32, u64, float, int, and uint are replaced with generic functions in
num instead.
If you were previously using any of those functions, just replace them
with the corresponding function with the same name in num.
Note: If you were using a function that corresponds to an operator, use
the operator instead.
2013-07-08 11:05:17 -05:00
|
|
|
str_tys.iter().fold(1, |a, t| num::max(a, ty_align(*t)))
|
2013-06-16 05:52:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Array => {
|
|
|
|
let elt = ty.element_type();
|
|
|
|
ty_align(elt)
|
|
|
|
}
|
2013-09-28 00:38:08 -05:00
|
|
|
_ => fail2!("ty_size: unhandled type")
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
fn ty_size(ty: Type) -> uint {
|
|
|
|
match ty.kind() {
|
|
|
|
Integer => {
|
|
|
|
unsafe {
|
|
|
|
((llvm::LLVMGetIntTypeWidth(ty.to_ref()) as uint) + 7) / 8
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
2013-06-16 05:52:44 -05:00
|
|
|
}
|
|
|
|
Pointer => 8,
|
|
|
|
Float => 4,
|
|
|
|
Double => 8,
|
|
|
|
Struct => {
|
2013-06-28 14:28:58 -05:00
|
|
|
let str_tys = ty.field_types();
|
2013-06-16 05:52:44 -05:00
|
|
|
if ty.is_packed() {
|
|
|
|
str_tys.iter().fold(0, |s, t| s + ty_size(*t))
|
|
|
|
} else {
|
|
|
|
let size = str_tys.iter().fold(0, |s, t| align(s, *t) + ty_size(*t));
|
|
|
|
align(size, ty)
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
2013-06-15 22:45:48 -05:00
|
|
|
}
|
2013-06-16 05:52:44 -05:00
|
|
|
Array => {
|
|
|
|
let len = ty.array_length();
|
|
|
|
let elt = ty.element_type();
|
|
|
|
let eltsz = ty_size(elt);
|
|
|
|
len * eltsz
|
|
|
|
}
|
2013-09-28 00:38:08 -05:00
|
|
|
_ => fail2!("ty_size: unhandled type")
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
fn all_mem(cls: &mut [RegClass]) {
|
2013-08-07 13:19:15 -05:00
|
|
|
for elt in cls.mut_iter() {
|
|
|
|
*elt = Memory;
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
fn unify(cls: &mut [RegClass],
|
2013-01-25 16:56:56 -06:00
|
|
|
i: uint,
|
2013-06-15 22:45:48 -05:00
|
|
|
newv: RegClass) {
|
2013-01-25 16:56:56 -06:00
|
|
|
if cls[i] == newv {
|
|
|
|
return;
|
2013-06-16 05:52:44 -05:00
|
|
|
} else if cls[i] == NoClass {
|
2013-01-25 16:56:56 -06:00
|
|
|
cls[i] = newv;
|
2013-06-16 05:52:44 -05:00
|
|
|
} else if newv == NoClass {
|
2013-01-25 16:56:56 -06:00
|
|
|
return;
|
2013-06-16 05:52:44 -05:00
|
|
|
} else if cls[i] == Memory || newv == Memory {
|
|
|
|
cls[i] = Memory;
|
|
|
|
} else if cls[i] == Int || newv == Int {
|
|
|
|
cls[i] = Int;
|
|
|
|
} else if cls[i] == X87 ||
|
|
|
|
cls[i] == X87Up ||
|
|
|
|
cls[i] == ComplexX87 ||
|
|
|
|
newv == X87 ||
|
|
|
|
newv == X87Up ||
|
|
|
|
newv == ComplexX87 {
|
|
|
|
cls[i] = Memory;
|
2013-01-25 16:56:56 -06:00
|
|
|
} else {
|
|
|
|
cls[i] = newv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
fn classify_struct(tys: &[Type],
|
|
|
|
cls: &mut [RegClass], i: uint,
|
2013-01-25 16:56:56 -06:00
|
|
|
off: uint) {
|
|
|
|
let mut field_off = off;
|
2013-08-03 11:45:23 -05:00
|
|
|
for ty in tys.iter() {
|
2013-01-25 16:56:56 -06:00
|
|
|
field_off = align(field_off, *ty);
|
|
|
|
classify(*ty, cls, i, field_off);
|
|
|
|
field_off += ty_size(*ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
fn classify(ty: Type,
|
|
|
|
cls: &mut [RegClass], ix: uint,
|
2013-01-25 16:56:56 -06:00
|
|
|
off: uint) {
|
2013-06-16 06:11:17 -05:00
|
|
|
let t_align = ty_align(ty);
|
|
|
|
let t_size = ty_size(ty);
|
2013-01-25 16:56:56 -06:00
|
|
|
|
2013-06-16 06:11:17 -05:00
|
|
|
let misalign = off % t_align;
|
|
|
|
if misalign != 0u {
|
|
|
|
let mut i = off / 8u;
|
|
|
|
let e = (off + t_size + 7u) / 8u;
|
|
|
|
while i < e {
|
|
|
|
unify(cls, ix + i, Memory);
|
|
|
|
i += 1u;
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
2013-06-16 06:11:17 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-01-25 16:56:56 -06:00
|
|
|
|
2013-06-16 06:11:17 -05:00
|
|
|
match ty.kind() {
|
|
|
|
Integer |
|
|
|
|
Pointer => {
|
|
|
|
unify(cls, ix + off / 8u, Int);
|
|
|
|
}
|
|
|
|
Float => {
|
|
|
|
if off % 8u == 4u {
|
|
|
|
unify(cls, ix + off / 8u, SSEFv);
|
|
|
|
} else {
|
|
|
|
unify(cls, ix + off / 8u, SSEFs);
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
2013-06-16 06:11:17 -05:00
|
|
|
}
|
|
|
|
Double => {
|
|
|
|
unify(cls, ix + off / 8u, SSEDs);
|
|
|
|
}
|
|
|
|
Struct => {
|
|
|
|
classify_struct(ty.field_types(), cls, ix, off);
|
|
|
|
}
|
|
|
|
Array => {
|
|
|
|
let len = ty.array_length();
|
|
|
|
let elt = ty.element_type();
|
|
|
|
let eltsz = ty_size(elt);
|
|
|
|
let mut i = 0u;
|
|
|
|
while i < len {
|
|
|
|
classify(elt, cls, ix, off + i * eltsz);
|
|
|
|
i += 1u;
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
}
|
2013-09-28 00:38:08 -05:00
|
|
|
_ => fail2!("classify: unhandled type")
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
fn fixup(ty: Type, cls: &mut [RegClass]) {
|
2013-06-16 06:11:17 -05:00
|
|
|
let mut i = 0u;
|
|
|
|
let ty_kind = ty.kind();
|
|
|
|
let e = cls.len();
|
2013-06-28 14:28:58 -05:00
|
|
|
if cls.len() > 2u && (ty_kind == Struct || ty_kind == Array) {
|
2013-06-16 06:11:17 -05:00
|
|
|
if cls[i].is_sse() {
|
|
|
|
i += 1u;
|
2013-01-25 16:56:56 -06:00
|
|
|
while i < e {
|
2013-06-16 06:11:17 -05:00
|
|
|
if cls[i] != SSEUp {
|
2013-01-25 16:56:56 -06:00
|
|
|
all_mem(cls);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-16 06:11:17 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
all_mem(cls);
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while i < e {
|
|
|
|
if cls[i] == Memory {
|
|
|
|
all_mem(cls);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if cls[i] == X87Up {
|
|
|
|
// for darwin
|
|
|
|
// cls[i] = SSEDs;
|
|
|
|
all_mem(cls);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if cls[i] == SSEUp {
|
2013-06-28 14:28:58 -05:00
|
|
|
cls[i] = SSEDv;
|
2013-06-16 06:11:17 -05:00
|
|
|
} else if cls[i].is_sse() {
|
|
|
|
i += 1;
|
|
|
|
while i != e && cls[i] == SSEUp { i += 1u; }
|
|
|
|
} else if cls[i] == X87 {
|
|
|
|
i += 1;
|
|
|
|
while i != e && cls[i] == X87Up { i += 1u; }
|
|
|
|
} else {
|
|
|
|
i += 1;
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let words = (ty_size(ty) + 7) / 8;
|
2013-06-16 05:52:44 -05:00
|
|
|
let mut cls = vec::from_elem(words, NoClass);
|
2013-01-25 16:56:56 -06:00
|
|
|
if words > 4 {
|
|
|
|
all_mem(cls);
|
2013-02-15 03:14:34 -06:00
|
|
|
return cls;
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
classify(ty, cls, 0, 0);
|
|
|
|
fixup(ty, cls);
|
2013-02-15 03:14:34 -06:00
|
|
|
return cls;
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
fn llreg_ty(cls: &[RegClass]) -> Type {
|
|
|
|
fn llvec_len(cls: &[RegClass]) -> uint {
|
2013-01-25 16:56:56 -06:00
|
|
|
let mut len = 1u;
|
2013-08-03 11:45:23 -05:00
|
|
|
for c in cls.iter() {
|
2013-06-16 05:52:44 -05:00
|
|
|
if *c != SSEUp {
|
2013-01-25 16:56:56 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
len += 1u;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2013-06-16 06:11:17 -05:00
|
|
|
let mut tys = ~[];
|
|
|
|
let mut i = 0u;
|
|
|
|
let e = cls.len();
|
|
|
|
while i < e {
|
|
|
|
match cls[i] {
|
|
|
|
Int => {
|
|
|
|
tys.push(Type::i64());
|
|
|
|
}
|
|
|
|
SSEFv => {
|
2013-06-28 14:28:58 -05:00
|
|
|
let vec_len = llvec_len(cls.tailn(i + 1u));
|
|
|
|
let vec_ty = Type::vector(&Type::f32(), (vec_len * 2u) as u64);
|
2013-06-16 06:11:17 -05:00
|
|
|
tys.push(vec_ty);
|
|
|
|
i += vec_len;
|
2013-10-01 16:31:03 -05:00
|
|
|
continue;
|
2013-06-16 06:11:17 -05:00
|
|
|
}
|
|
|
|
SSEFs => {
|
|
|
|
tys.push(Type::f32());
|
|
|
|
}
|
|
|
|
SSEDs => {
|
|
|
|
tys.push(Type::f64());
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
2013-09-28 00:38:08 -05:00
|
|
|
_ => fail2!("llregtype: unhandled class")
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
2013-06-16 06:11:17 -05:00
|
|
|
i += 1u;
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
2013-06-16 06:11:17 -05:00
|
|
|
return Type::struct_(tys, false);
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
pub fn compute_abi_info(_ccx: &mut CrateContext,
|
|
|
|
atys: &[Type],
|
|
|
|
rty: Type,
|
|
|
|
ret_def: bool) -> FnType {
|
2013-06-15 22:45:48 -05:00
|
|
|
fn x86_64_ty(ty: Type,
|
|
|
|
is_mem_cls: &fn(cls: &[RegClass]) -> bool,
|
2013-01-25 16:56:56 -06:00
|
|
|
attr: Attribute) -> (LLVMType, Option<Attribute>) {
|
2013-06-16 05:52:44 -05:00
|
|
|
|
2013-06-15 22:45:48 -05:00
|
|
|
let (cast, attr, ty) = if !ty.is_reg_ty() {
|
2013-01-25 16:56:56 -06:00
|
|
|
let cls = classify_ty(ty);
|
|
|
|
if is_mem_cls(cls) {
|
2013-06-15 22:45:48 -05:00
|
|
|
(false, option::Some(attr), ty.ptr_to())
|
2013-01-25 16:56:56 -06:00
|
|
|
} else {
|
2013-06-15 22:45:48 -05:00
|
|
|
(true, option::None, llreg_ty(cls))
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
2013-06-16 05:52:44 -05:00
|
|
|
} else {
|
|
|
|
(false, option::None, ty)
|
2013-06-15 22:45:48 -05:00
|
|
|
};
|
2013-06-16 05:52:44 -05:00
|
|
|
|
|
|
|
(LLVMType { cast: cast, ty: ty }, attr)
|
2013-01-25 16:56:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut arg_tys = ~[];
|
|
|
|
let mut attrs = ~[];
|
2013-08-03 11:45:23 -05:00
|
|
|
for t in atys.iter() {
|
2013-06-15 22:45:48 -05:00
|
|
|
let (ty, attr) = x86_64_ty(*t, |cls| cls.is_pass_byval(), ByValAttribute);
|
2013-01-25 16:56:56 -06:00
|
|
|
arg_tys.push(ty);
|
|
|
|
attrs.push(attr);
|
|
|
|
}
|
2013-06-06 20:54:14 -05:00
|
|
|
let (ret_ty, ret_attr) = x86_64_ty(rty, |cls| cls.is_ret_bysret(),
|
2013-01-25 16:56:56 -06:00
|
|
|
StructRetAttribute);
|
2013-06-06 20:54:14 -05:00
|
|
|
let mut ret_ty = ret_ty;
|
2013-01-25 16:56:56 -06:00
|
|
|
let sret = ret_attr.is_some();
|
|
|
|
if sret {
|
|
|
|
arg_tys = vec::append(~[ret_ty], arg_tys);
|
|
|
|
ret_ty = LLVMType {
|
|
|
|
cast: false,
|
2013-06-15 22:45:48 -05:00
|
|
|
ty: Type::void()
|
2013-01-25 16:56:56 -06:00
|
|
|
};
|
|
|
|
attrs = vec::append(~[ret_attr], attrs);
|
|
|
|
} else if !ret_def {
|
|
|
|
ret_ty = LLVMType {
|
|
|
|
cast: false,
|
2013-06-15 22:45:48 -05:00
|
|
|
ty: Type::void()
|
2013-01-25 16:56:56 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return FnType {
|
|
|
|
arg_tys: arg_tys,
|
|
|
|
ret_ty: ret_ty,
|
|
|
|
attrs: attrs,
|
|
|
|
sret: sret
|
|
|
|
};
|
|
|
|
}
|