2012-05-03 14:07:43 -05:00
|
|
|
// The classification code for the x86_64 ABI is taken from the clay language
|
2012-05-03 11:46:50 -05:00
|
|
|
// https://github.com/jckarter/clay/blob/master/compiler/src/externals.cpp
|
|
|
|
|
2012-10-15 16:56:42 -05:00
|
|
|
use driver::session::arch_x86_64;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::codemap::span;
|
|
|
|
use libc::c_uint;
|
|
|
|
use syntax::{attr, ast_map};
|
|
|
|
use lib::llvm::{ llvm, TypeRef, ValueRef, Integer, Pointer, Float, Double,
|
2012-08-30 14:54:50 -05:00
|
|
|
Struct, Array, ModuleRef, CallConv, Attribute,
|
|
|
|
StructRetAttribute, ByValAttribute,
|
|
|
|
SequentiallyConsistent, Acquire, Release, Xchg };
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::{ast, ast_util};
|
2012-09-18 13:46:39 -05:00
|
|
|
use back::{link, abi};
|
2012-09-04 13:54:36 -05:00
|
|
|
use common::*;
|
|
|
|
use build::*;
|
|
|
|
use base::*;
|
|
|
|
use type_of::*;
|
2012-09-10 17:38:28 -05:00
|
|
|
use std::map::HashMap;
|
2012-09-04 13:54:36 -05:00
|
|
|
use util::ppaux::ty_to_str;
|
2012-08-28 17:54:45 -05:00
|
|
|
use datum::*;
|
|
|
|
use callee::*;
|
|
|
|
use expr::{Dest, Ignore};
|
2012-09-07 09:37:19 -05:00
|
|
|
use ty::{FnTyBase, FnMeta, FnSig};
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2012-07-03 18:11:00 -05:00
|
|
|
export link_name, trans_foreign_mod, register_foreign_fn, trans_foreign_fn,
|
2012-03-23 09:05:16 -05:00
|
|
|
trans_intrinsic;
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2012-03-20 13:44:28 -05:00
|
|
|
enum x86_64_reg_class {
|
|
|
|
no_class,
|
|
|
|
integer_class,
|
|
|
|
sse_fs_class,
|
|
|
|
sse_fv_class,
|
|
|
|
sse_ds_class,
|
|
|
|
sse_dv_class,
|
|
|
|
sse_int_class,
|
|
|
|
sseup_class,
|
|
|
|
x87_class,
|
|
|
|
x87up_class,
|
|
|
|
complex_x87_class,
|
|
|
|
memory_class
|
|
|
|
}
|
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl x86_64_reg_class : cmp::Eq {
|
|
|
|
pure fn eq(other: &x86_64_reg_class) -> bool {
|
|
|
|
(self as uint) == ((*other) as uint)
|
|
|
|
}
|
|
|
|
pure fn ne(other: &x86_64_reg_class) -> bool { !self.eq(other) }
|
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-05-12 21:31:28 -05:00
|
|
|
fn is_sse(++c: x86_64_reg_class) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
return match c {
|
2012-03-20 13:44:28 -05:00
|
|
|
sse_fs_class | sse_fv_class |
|
2012-08-03 21:59:04 -05:00
|
|
|
sse_ds_class | sse_dv_class => true,
|
|
|
|
_ => false
|
2012-03-20 13:44:28 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn is_ymm(cls: ~[x86_64_reg_class]) -> bool {
|
2012-03-20 13:44:28 -05:00
|
|
|
let len = vec::len(cls);
|
2012-08-01 19:30:05 -05:00
|
|
|
return (len > 2u &&
|
2012-03-20 13:44:28 -05:00
|
|
|
is_sse(cls[0]) &&
|
|
|
|
cls[1] == sseup_class &&
|
|
|
|
cls[2] == sseup_class) ||
|
|
|
|
(len > 3u &&
|
|
|
|
is_sse(cls[1]) &&
|
|
|
|
cls[2] == sseup_class &&
|
|
|
|
cls[3] == sseup_class);
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
|
2012-03-20 13:44:28 -05:00
|
|
|
fn align(off: uint, ty: TypeRef) -> uint {
|
|
|
|
let a = ty_align(ty);
|
2012-08-01 19:30:05 -05:00
|
|
|
return (off + a - 1u) / a * a;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn struct_tys(ty: TypeRef) -> ~[TypeRef] {
|
2012-03-20 13:44:28 -05:00
|
|
|
let n = llvm::LLVMCountStructElementTypes(ty);
|
|
|
|
let elts = vec::from_elem(n as uint, ptr::null());
|
2012-09-13 13:46:10 -05:00
|
|
|
do vec::as_imm_buf(elts) |buf, _len| {
|
2012-03-20 13:44:28 -05:00
|
|
|
llvm::LLVMGetStructElementTypes(ty, buf);
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return elts;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ty_align(ty: TypeRef) -> uint {
|
2012-08-30 14:54:50 -05:00
|
|
|
return match llvm::LLVMGetTypeKind(ty) {
|
|
|
|
Integer => {
|
|
|
|
((llvm::LLVMGetIntTypeWidth(ty) as uint) + 7) / 8
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-30 14:54:50 -05:00
|
|
|
Pointer => 8,
|
|
|
|
Float => 4,
|
|
|
|
Double => 8,
|
|
|
|
Struct => {
|
|
|
|
do vec::foldl(0, struct_tys(ty)) |a, t| {
|
2012-09-28 00:20:47 -05:00
|
|
|
uint::max(a, ty_align(*t))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-30 14:54:50 -05:00
|
|
|
Array => {
|
2012-05-02 20:33:57 -05:00
|
|
|
let elt = llvm::LLVMGetElementType(ty);
|
|
|
|
ty_align(elt)
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail ~"ty_size: unhandled type"
|
2012-03-20 13:44:28 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ty_size(ty: TypeRef) -> uint {
|
2012-08-30 14:54:50 -05:00
|
|
|
return match llvm::LLVMGetTypeKind(ty) {
|
|
|
|
Integer => {
|
|
|
|
((llvm::LLVMGetIntTypeWidth(ty) as uint) + 7) / 8
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-30 14:54:50 -05:00
|
|
|
Pointer => 8,
|
|
|
|
Float => 4,
|
|
|
|
Double => 8,
|
|
|
|
Struct => {
|
2012-10-10 15:40:17 -05:00
|
|
|
let size = do vec::foldl(0, struct_tys(ty)) |s, t| {
|
|
|
|
align(s, *t) + ty_size(*t)
|
|
|
|
};
|
|
|
|
align(size, ty)
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-30 14:54:50 -05:00
|
|
|
Array => {
|
2012-05-02 20:33:57 -05:00
|
|
|
let len = llvm::LLVMGetArrayLength(ty) as uint;
|
|
|
|
let elt = llvm::LLVMGetElementType(ty);
|
|
|
|
let eltsz = ty_size(elt);
|
|
|
|
len * eltsz
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail ~"ty_size: unhandled type"
|
2012-03-20 13:44:28 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn all_mem(cls: ~[mut x86_64_reg_class]) {
|
2012-08-30 14:54:50 -05:00
|
|
|
for uint::range(0, cls.len()) |i| {
|
2012-03-20 13:44:28 -05:00
|
|
|
cls[i] = memory_class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn unify(cls: ~[mut x86_64_reg_class],
|
2012-03-14 14:16:46 -05:00
|
|
|
i: uint,
|
|
|
|
newv: x86_64_reg_class) {
|
|
|
|
if cls[i] == newv {
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2012-03-20 13:44:28 -05:00
|
|
|
} else if cls[i] == no_class {
|
2012-03-14 14:16:46 -05:00
|
|
|
cls[i] = newv;
|
|
|
|
} else if newv == no_class {
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2012-03-14 14:16:46 -05:00
|
|
|
} else if cls[i] == memory_class || newv == memory_class {
|
2012-03-20 13:44:28 -05:00
|
|
|
cls[i] = memory_class;
|
2012-03-14 14:16:46 -05:00
|
|
|
} else if cls[i] == integer_class || newv == integer_class {
|
2012-03-20 13:44:28 -05:00
|
|
|
cls[i] = integer_class;
|
|
|
|
} else if cls[i] == x87_class ||
|
|
|
|
cls[i] == x87up_class ||
|
|
|
|
cls[i] == complex_x87_class ||
|
2012-03-14 14:16:46 -05:00
|
|
|
newv == x87_class ||
|
|
|
|
newv == x87up_class ||
|
|
|
|
newv == complex_x87_class {
|
2012-03-20 13:44:28 -05:00
|
|
|
cls[i] = memory_class;
|
|
|
|
} else {
|
2012-03-14 14:16:46 -05:00
|
|
|
cls[i] = newv;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn classify_struct(tys: ~[TypeRef],
|
|
|
|
cls: ~[mut x86_64_reg_class], i: uint,
|
2012-03-20 13:44:28 -05:00
|
|
|
off: uint) {
|
|
|
|
if vec::is_empty(tys) {
|
|
|
|
classify(T_i64(), cls, i, off);
|
|
|
|
} else {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut field_off = off;
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(tys) |ty| {
|
2012-09-18 23:41:37 -05:00
|
|
|
field_off = align(field_off, *ty);
|
|
|
|
classify(*ty, cls, i, field_off);
|
|
|
|
field_off += ty_size(*ty);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn classify(ty: TypeRef,
|
2012-06-29 18:26:56 -05:00
|
|
|
cls: ~[mut x86_64_reg_class], ix: uint,
|
2012-03-20 13:44:28 -05:00
|
|
|
off: uint) {
|
|
|
|
let t_align = ty_align(ty);
|
|
|
|
let t_size = ty_size(ty);
|
|
|
|
|
|
|
|
let misalign = off % t_align;
|
|
|
|
if misalign != 0u {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = off / 8u;
|
2012-03-20 13:44:28 -05:00
|
|
|
let e = (off + t_size + 7u) / 8u;
|
|
|
|
while i < e {
|
2012-05-03 00:59:44 -05:00
|
|
|
unify(cls, ix + i, memory_class);
|
2012-03-20 13:44:28 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match llvm::LLVMGetTypeKind(ty) as int {
|
2012-03-20 13:44:28 -05:00
|
|
|
8 /* integer */ |
|
2012-08-03 21:59:04 -05:00
|
|
|
12 /* pointer */ => {
|
2012-05-03 00:59:44 -05:00
|
|
|
unify(cls, ix + off / 8u, integer_class);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
2 /* float */ => {
|
2012-03-20 13:44:28 -05:00
|
|
|
if off % 8u == 4u {
|
2012-05-03 00:59:44 -05:00
|
|
|
unify(cls, ix + off / 8u, sse_fv_class);
|
2012-03-20 13:44:28 -05:00
|
|
|
} else {
|
2012-05-03 00:59:44 -05:00
|
|
|
unify(cls, ix + off / 8u, sse_fs_class);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
3 /* double */ => {
|
2012-05-03 00:59:44 -05:00
|
|
|
unify(cls, ix + off / 8u, sse_ds_class);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
10 /* struct */ => {
|
2012-05-03 00:59:44 -05:00
|
|
|
classify_struct(struct_tys(ty), cls, ix, off);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
11 /* array */ => {
|
2012-05-03 00:59:44 -05:00
|
|
|
let elt = llvm::LLVMGetElementType(ty);
|
|
|
|
let eltsz = ty_size(elt);
|
|
|
|
let len = llvm::LLVMGetArrayLength(ty) as uint;
|
|
|
|
let mut i = 0u;
|
|
|
|
while i < len {
|
|
|
|
classify(elt, cls, ix, off + i * eltsz);
|
|
|
|
i += 1u;
|
|
|
|
}
|
2012-05-02 20:33:57 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail ~"classify: unhandled type"
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn fixup(ty: TypeRef, cls: ~[mut x86_64_reg_class]) {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
2012-05-03 00:59:44 -05:00
|
|
|
let llty = llvm::LLVMGetTypeKind(ty) as int;
|
2012-03-20 13:44:28 -05:00
|
|
|
let e = vec::len(cls);
|
|
|
|
if vec::len(cls) > 2u &&
|
2012-05-03 00:59:44 -05:00
|
|
|
(llty == 10 /* struct */ ||
|
|
|
|
llty == 11 /* array */) {
|
2012-03-20 13:44:28 -05:00
|
|
|
if is_sse(cls[i]) {
|
|
|
|
i += 1u;
|
|
|
|
while i < e {
|
|
|
|
if cls[i] != sseup_class {
|
|
|
|
all_mem(cls);
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
all_mem(cls);
|
2012-08-01 19:30:05 -05:00
|
|
|
return
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while i < e {
|
|
|
|
if cls[i] == memory_class {
|
|
|
|
all_mem(cls);
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
if cls[i] == x87up_class {
|
|
|
|
// for darwin
|
|
|
|
// cls[i] = sse_ds_class;
|
|
|
|
all_mem(cls);
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
if cls[i] == sseup_class {
|
|
|
|
cls[i] = sse_int_class;
|
|
|
|
} else if is_sse(cls[i]) {
|
2012-09-11 12:26:23 -05:00
|
|
|
i += 1;
|
2012-03-20 13:44:28 -05:00
|
|
|
while cls[i] == sseup_class { i += 1u; }
|
|
|
|
} else if cls[i] == x87_class {
|
2012-09-11 12:26:23 -05:00
|
|
|
i += 1;
|
2012-03-20 13:44:28 -05:00
|
|
|
while cls[i] == x87up_class { i += 1u; }
|
|
|
|
} else {
|
2012-09-11 12:26:23 -05:00
|
|
|
i += 1;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 12:26:23 -05:00
|
|
|
let words = (ty_size(ty) + 7) / 8;
|
2012-03-20 13:44:28 -05:00
|
|
|
let cls = vec::to_mut(vec::from_elem(words, no_class));
|
2012-09-11 12:26:23 -05:00
|
|
|
if words > 4 {
|
2012-03-20 13:44:28 -05:00
|
|
|
all_mem(cls);
|
2012-09-11 12:26:23 -05:00
|
|
|
return vec::from_mut(move cls);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-09-11 12:26:23 -05:00
|
|
|
classify(ty, cls, 0, 0);
|
2012-03-20 13:44:28 -05:00
|
|
|
fixup(ty, cls);
|
2012-09-11 12:26:23 -05:00
|
|
|
return vec::from_mut(move cls);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn llreg_ty(cls: ~[x86_64_reg_class]) -> TypeRef {
|
|
|
|
fn llvec_len(cls: ~[x86_64_reg_class]) -> uint {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut len = 1u;
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(cls) |c| {
|
2012-09-18 23:41:37 -05:00
|
|
|
if *c != sseup_class {
|
2012-03-20 13:44:28 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
len += 1u;
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return len;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut tys = ~[];
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
2012-03-20 13:44:28 -05:00
|
|
|
let e = vec::len(cls);
|
|
|
|
while i < e {
|
2012-08-06 14:34:08 -05:00
|
|
|
match cls[i] {
|
2012-08-03 21:59:04 -05:00
|
|
|
integer_class => {
|
2012-09-26 19:33:34 -05:00
|
|
|
tys.push(T_i64());
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
sse_fv_class => {
|
2012-03-20 13:44:28 -05:00
|
|
|
let vec_len = llvec_len(vec::tailn(cls, i + 1u)) * 2u;
|
|
|
|
let vec_ty = llvm::LLVMVectorType(T_f32(),
|
|
|
|
vec_len as c_uint);
|
2012-09-26 19:33:34 -05:00
|
|
|
tys.push(vec_ty);
|
2012-03-20 13:44:28 -05:00
|
|
|
i += vec_len;
|
2012-09-07 17:32:04 -05:00
|
|
|
loop;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
sse_fs_class => {
|
2012-09-26 19:33:34 -05:00
|
|
|
tys.push(T_f32());
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
sse_ds_class => {
|
2012-09-26 19:33:34 -05:00
|
|
|
tys.push(T_f64());
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => fail ~"llregtype: unhandled class"
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return T_struct(tys);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type x86_64_llty = {
|
|
|
|
cast: bool,
|
|
|
|
ty: TypeRef
|
|
|
|
};
|
|
|
|
|
|
|
|
type x86_64_tys = {
|
2012-06-29 18:26:56 -05:00
|
|
|
arg_tys: ~[x86_64_llty],
|
2012-03-20 13:44:28 -05:00
|
|
|
ret_ty: x86_64_llty,
|
2012-08-20 14:23:37 -05:00
|
|
|
attrs: ~[Option<Attribute>],
|
2012-03-20 13:44:28 -05:00
|
|
|
sret: bool
|
|
|
|
};
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn x86_64_tys(atys: ~[TypeRef],
|
2012-03-20 13:44:28 -05:00
|
|
|
rty: TypeRef,
|
|
|
|
ret_def: bool) -> x86_64_tys {
|
|
|
|
fn is_reg_ty(ty: TypeRef) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
return match llvm::LLVMGetTypeKind(ty) as int {
|
2012-03-20 13:44:28 -05:00
|
|
|
8 /* integer */ |
|
|
|
|
12 /* pointer */ |
|
|
|
|
2 /* float */ |
|
2012-08-03 21:59:04 -05:00
|
|
|
3 /* double */ => true,
|
|
|
|
_ => false
|
2012-03-20 13:44:28 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn is_pass_byval(cls: ~[x86_64_reg_class]) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return cls[0] == memory_class ||
|
2012-03-20 13:44:28 -05:00
|
|
|
cls[0] == x87_class ||
|
|
|
|
cls[0] == complex_x87_class;
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn is_ret_bysret(cls: ~[x86_64_reg_class]) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return cls[0] == memory_class;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn x86_64_ty(ty: TypeRef,
|
2012-06-29 18:26:56 -05:00
|
|
|
is_mem_cls: fn(cls: ~[x86_64_reg_class]) -> bool,
|
2012-08-20 14:23:37 -05:00
|
|
|
attr: Attribute) -> (x86_64_llty, Option<Attribute>) {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut cast = false;
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut ty_attr = option::None;
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut llty = ty;
|
2012-03-20 13:44:28 -05:00
|
|
|
if !is_reg_ty(ty) {
|
|
|
|
let cls = classify_ty(ty);
|
|
|
|
if is_mem_cls(cls) {
|
|
|
|
llty = T_ptr(ty);
|
2012-08-20 14:23:37 -05:00
|
|
|
ty_attr = option::Some(attr);
|
2012-03-20 13:44:28 -05:00
|
|
|
} else {
|
|
|
|
cast = true;
|
|
|
|
llty = llreg_ty(cls);
|
|
|
|
}
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return ({ cast: cast, ty: llty }, ty_attr);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut arg_tys = ~[];
|
|
|
|
let mut attrs = ~[];
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(atys) |t| {
|
2012-09-18 23:41:37 -05:00
|
|
|
let (ty, attr) = x86_64_ty(*t, is_pass_byval, ByValAttribute);
|
2012-09-26 19:33:34 -05:00
|
|
|
arg_tys.push(ty);
|
|
|
|
attrs.push(attr);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut (ret_ty, ret_attr) = x86_64_ty(rty, is_ret_bysret,
|
|
|
|
StructRetAttribute);
|
2012-09-21 21:37:57 -05:00
|
|
|
let sret = ret_attr.is_some();
|
2012-03-20 13:44:28 -05:00
|
|
|
if sret {
|
2012-06-29 18:26:56 -05:00
|
|
|
arg_tys = vec::append(~[ret_ty], arg_tys);
|
2012-03-20 13:44:28 -05:00
|
|
|
ret_ty = { cast: false,
|
|
|
|
ty: T_void()
|
|
|
|
};
|
2012-06-29 18:26:56 -05:00
|
|
|
attrs = vec::append(~[ret_attr], attrs);
|
2012-03-20 13:44:28 -05:00
|
|
|
} else if !ret_def {
|
|
|
|
ret_ty = { cast: false,
|
|
|
|
ty: T_void()
|
|
|
|
};
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return {
|
2012-03-20 13:44:28 -05:00
|
|
|
arg_tys: arg_tys,
|
|
|
|
ret_ty: ret_ty,
|
|
|
|
attrs: attrs,
|
|
|
|
sret: sret
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decl_x86_64_fn(tys: x86_64_tys,
|
|
|
|
decl: fn(fnty: TypeRef) -> ValueRef) -> ValueRef {
|
2012-06-30 18:19:07 -05:00
|
|
|
let atys = vec::map(tys.arg_tys, |t| t.ty);
|
2012-03-20 13:44:28 -05:00
|
|
|
let rty = tys.ret_ty.ty;
|
|
|
|
let fnty = T_fn(atys, rty);
|
|
|
|
let llfn = decl(fnty);
|
|
|
|
|
2012-09-18 23:41:37 -05:00
|
|
|
for vec::eachi(tys.attrs) |i, a| {
|
2012-09-21 20:43:30 -05:00
|
|
|
match *a {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some(attr) => {
|
2012-03-21 09:42:20 -05:00
|
|
|
let llarg = get_param(llfn, i);
|
2012-03-20 13:44:28 -05:00
|
|
|
llvm::LLVMAddAttribute(llarg, attr as c_uint);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return llfn;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
|
2012-07-18 18:18:02 -05:00
|
|
|
fn link_name(ccx: @crate_ctxt, i: @ast::foreign_item) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match attr::first_attr_value_str_by_name(i.attrs, ~"link_name") {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => ccx.sess.str_of(i.ident),
|
|
|
|
option::Some(ln) => ln
|
2012-02-13 16:59:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type c_stack_tys = {
|
2012-06-29 18:26:56 -05:00
|
|
|
arg_tys: ~[TypeRef],
|
2012-02-13 16:59:05 -06:00
|
|
|
ret_ty: TypeRef,
|
|
|
|
ret_def: bool,
|
|
|
|
bundle_ty: TypeRef,
|
2012-03-20 13:44:28 -05:00
|
|
|
shim_fn_ty: TypeRef,
|
2012-08-20 14:23:37 -05:00
|
|
|
x86_64_tys: Option<x86_64_tys>
|
2012-02-13 16:59:05 -06:00
|
|
|
};
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn c_arg_and_ret_lltys(ccx: @crate_ctxt,
|
2012-06-29 18:26:56 -05:00
|
|
|
id: ast::node_id) -> (~[TypeRef], TypeRef, ty::t) {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(ty::node_id_to_type(ccx.tcx, id)).sty {
|
2012-09-07 09:37:19 -05:00
|
|
|
ty::ty_fn(ref fn_ty) => {
|
|
|
|
let llargtys = type_of_explicit_args(ccx, fn_ty.sig.inputs);
|
|
|
|
let llretty = type_of::type_of(ccx, fn_ty.sig.output);
|
|
|
|
(llargtys, llretty, fn_ty.sig.output)
|
|
|
|
}
|
|
|
|
_ => ccx.sess.bug(~"c_arg_and_ret_lltys called on non-function type")
|
2012-02-13 16:59:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn c_stack_tys(ccx: @crate_ctxt,
|
2012-02-13 18:06:56 -06:00
|
|
|
id: ast::node_id) -> @c_stack_tys {
|
|
|
|
let (llargtys, llretty, ret_ty) = c_arg_and_ret_lltys(ccx, id);
|
2012-06-28 17:00:03 -05:00
|
|
|
let bundle_ty = T_struct(vec::append_one(llargtys, T_ptr(llretty)));
|
2012-03-20 13:44:28 -05:00
|
|
|
let ret_def = !ty::type_is_bot(ret_ty) && !ty::type_is_nil(ret_ty);
|
|
|
|
let x86_64 = if ccx.sess.targ_cfg.arch == arch_x86_64 {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some(x86_64_tys(llargtys, llretty, ret_def))
|
2012-03-20 13:44:28 -05:00
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None
|
2012-03-20 13:44:28 -05:00
|
|
|
};
|
2012-08-01 19:30:05 -05:00
|
|
|
return @{
|
2012-02-13 18:06:56 -06:00
|
|
|
arg_tys: llargtys,
|
|
|
|
ret_ty: llretty,
|
2012-03-20 13:44:28 -05:00
|
|
|
ret_def: ret_def,
|
2012-02-13 18:06:56 -06:00
|
|
|
bundle_ty: bundle_ty,
|
2012-06-29 18:26:56 -05:00
|
|
|
shim_fn_ty: T_fn(~[T_ptr(bundle_ty)], T_void()),
|
2012-03-20 13:44:28 -05:00
|
|
|
x86_64_tys: x86_64
|
2012-02-13 18:06:56 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
type shim_arg_builder = fn(bcx: block, tys: @c_stack_tys,
|
2012-06-29 18:26:56 -05:00
|
|
|
llargbundle: ValueRef) -> ~[ValueRef];
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
type shim_ret_builder = fn(bcx: block, tys: @c_stack_tys,
|
2012-02-13 18:06:56 -06:00
|
|
|
llargbundle: ValueRef, llretval: ValueRef);
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn build_shim_fn_(ccx: @crate_ctxt,
|
2012-07-14 00:57:48 -05:00
|
|
|
shim_name: ~str,
|
2012-02-13 18:06:56 -06:00
|
|
|
llbasefn: ValueRef,
|
|
|
|
tys: @c_stack_tys,
|
|
|
|
cc: lib::llvm::CallConv,
|
|
|
|
arg_builder: shim_arg_builder,
|
|
|
|
ret_builder: shim_ret_builder) -> ValueRef {
|
|
|
|
|
|
|
|
let llshimfn = decl_internal_cdecl_fn(
|
|
|
|
ccx.llmod, shim_name, tys.shim_fn_ty);
|
|
|
|
|
|
|
|
// Declare the body of the shim function:
|
2012-08-20 14:23:37 -05:00
|
|
|
let fcx = new_fn_ctxt(ccx, ~[], llshimfn, None);
|
|
|
|
let bcx = top_scope_block(fcx, None);
|
2012-02-13 18:06:56 -06:00
|
|
|
let lltop = bcx.llbb;
|
2012-03-21 09:42:20 -05:00
|
|
|
let llargbundle = get_param(llshimfn, 0u);
|
2012-02-13 18:06:56 -06:00
|
|
|
let llargvals = arg_builder(bcx, tys, llargbundle);
|
|
|
|
|
|
|
|
// Create the call itself and store the return value:
|
|
|
|
let llretval = CallWithConv(bcx, llbasefn,
|
|
|
|
llargvals, cc); // r
|
|
|
|
|
|
|
|
ret_builder(bcx, tys, llargbundle, llretval);
|
|
|
|
|
|
|
|
build_return(bcx);
|
|
|
|
finish_fn(fcx, lltop);
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return llshimfn;
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
type wrap_arg_builder = fn(bcx: block, tys: @c_stack_tys,
|
2012-02-13 18:06:56 -06:00
|
|
|
llwrapfn: ValueRef,
|
|
|
|
llargbundle: ValueRef);
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
type wrap_ret_builder = fn(bcx: block, tys: @c_stack_tys,
|
2012-02-13 18:06:56 -06:00
|
|
|
llargbundle: ValueRef);
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn build_wrap_fn_(ccx: @crate_ctxt,
|
2012-02-13 18:06:56 -06:00
|
|
|
tys: @c_stack_tys,
|
|
|
|
llshimfn: ValueRef,
|
|
|
|
llwrapfn: ValueRef,
|
|
|
|
shim_upcall: ValueRef,
|
|
|
|
arg_builder: wrap_arg_builder,
|
|
|
|
ret_builder: wrap_ret_builder) {
|
|
|
|
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("foreign::build_wrap_fn_");
|
2012-08-20 14:23:37 -05:00
|
|
|
let fcx = new_fn_ctxt(ccx, ~[], llwrapfn, None);
|
|
|
|
let bcx = top_scope_block(fcx, None);
|
2012-02-13 18:06:56 -06:00
|
|
|
let lltop = bcx.llbb;
|
|
|
|
|
|
|
|
// Allocate the struct and write the arguments into it.
|
|
|
|
let llargbundle = alloca(bcx, tys.bundle_ty);
|
|
|
|
arg_builder(bcx, tys, llwrapfn, llargbundle);
|
|
|
|
|
|
|
|
// Create call itself.
|
|
|
|
let llshimfnptr = PointerCast(bcx, llshimfn, T_ptr(T_i8()));
|
|
|
|
let llrawargbundle = PointerCast(bcx, llargbundle, T_ptr(T_i8()));
|
2012-06-29 18:26:56 -05:00
|
|
|
Call(bcx, shim_upcall, ~[llrawargbundle, llshimfnptr]);
|
2012-02-13 18:06:56 -06:00
|
|
|
ret_builder(bcx, tys, llargbundle);
|
|
|
|
|
|
|
|
tie_up_header_blocks(fcx, lltop);
|
|
|
|
|
|
|
|
// Make sure our standard return block (that we didn't use) is terminated
|
2012-07-23 18:00:19 -05:00
|
|
|
let ret_cx = raw_block(fcx, false, fcx.llreturn);
|
2012-02-13 18:06:56 -06:00
|
|
|
Unreachable(ret_cx);
|
|
|
|
}
|
|
|
|
|
2012-07-03 18:11:00 -05:00
|
|
|
// For each foreign function F, we generate a wrapper function W and a shim
|
2012-02-13 16:59:05 -06:00
|
|
|
// function S that all work together. The wrapper function W is the function
|
|
|
|
// that other rust code actually invokes. Its job is to marshall the
|
|
|
|
// arguments into a struct. It then uses a small bit of assembly to switch
|
|
|
|
// over to the C stack and invoke the shim function. The shim function S then
|
|
|
|
// unpacks the arguments from the struct and invokes the actual function F
|
|
|
|
// according to its specified calling convention.
|
|
|
|
//
|
2012-07-03 18:11:00 -05:00
|
|
|
// Example: Given a foreign c-stack function F(x: X, y: Y) -> Z,
|
2012-02-13 16:59:05 -06:00
|
|
|
// we generate a wrapper function W that looks like:
|
|
|
|
//
|
|
|
|
// void W(Z* dest, void *env, X x, Y y) {
|
|
|
|
// struct { X x; Y y; Z *z; } args = { x, y, z };
|
|
|
|
// call_on_c_stack_shim(S, &args);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The shim function S then looks something like:
|
|
|
|
//
|
|
|
|
// void S(struct { X x; Y y; Z *z; } *args) {
|
|
|
|
// *args->z = F(args->x, args->y);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// However, if the return type of F is dynamically sized or of aggregate type,
|
|
|
|
// the shim function looks like:
|
|
|
|
//
|
|
|
|
// void S(struct { X x; Y y; Z *z; } *args) {
|
|
|
|
// F(args->z, args->x, args->y);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Note: on i386, the layout of the args struct is generally the same as the
|
|
|
|
// desired layout of the arguments on the C stack. Therefore, we could use
|
|
|
|
// upcall_alloc_c_stack() to allocate the `args` structure and switch the
|
|
|
|
// stack pointer appropriately to avoid a round of copies. (In fact, the shim
|
|
|
|
// function itself is unnecessary). We used to do this, in fact, and will
|
|
|
|
// perhaps do so in the future.
|
2012-06-26 18:18:37 -05:00
|
|
|
fn trans_foreign_mod(ccx: @crate_ctxt,
|
|
|
|
foreign_mod: ast::foreign_mod, abi: ast::foreign_abi) {
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("foreign::trans_foreign_mod");
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn build_shim_fn(ccx: @crate_ctxt,
|
2012-06-26 18:18:37 -05:00
|
|
|
foreign_item: @ast::foreign_item,
|
2012-02-13 16:59:05 -06:00
|
|
|
tys: @c_stack_tys,
|
|
|
|
cc: lib::llvm::CallConv) -> ValueRef {
|
|
|
|
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("foreign::build_shim_fn");
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn build_args(bcx: block, tys: @c_stack_tys,
|
2012-06-29 18:26:56 -05:00
|
|
|
llargbundle: ValueRef) -> ~[ValueRef] {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("foreign::shim::build_args");
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut llargvals = ~[];
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
2012-02-13 18:06:56 -06:00
|
|
|
let n = vec::len(tys.arg_tys);
|
2012-03-20 13:44:28 -05:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match tys.x86_64_tys {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(x86_64) => {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut atys = x86_64.arg_tys;
|
|
|
|
let mut attrs = x86_64.attrs;
|
2012-03-20 13:44:28 -05:00
|
|
|
if x86_64.sret {
|
2012-08-27 14:16:37 -05:00
|
|
|
let llretptr = GEPi(bcx, llargbundle, [0u, n]);
|
2012-03-20 13:44:28 -05:00
|
|
|
let llretloc = Load(bcx, llretptr);
|
2012-06-29 18:26:56 -05:00
|
|
|
llargvals = ~[llretloc];
|
2012-03-20 13:44:28 -05:00
|
|
|
atys = vec::tail(atys);
|
|
|
|
attrs = vec::tail(attrs);
|
|
|
|
}
|
|
|
|
while i < n {
|
|
|
|
let llargval = if atys[i].cast {
|
2012-08-27 14:16:37 -05:00
|
|
|
let arg_ptr = GEPi(bcx, llargbundle, [0u, i]);
|
2012-03-15 08:47:03 -05:00
|
|
|
let arg_ptr = BitCast(bcx, arg_ptr,
|
|
|
|
T_ptr(atys[i].ty));
|
2012-03-20 13:44:28 -05:00
|
|
|
Load(bcx, arg_ptr)
|
2012-09-21 21:37:57 -05:00
|
|
|
} else if attrs[i].is_some() {
|
2012-08-27 14:16:37 -05:00
|
|
|
GEPi(bcx, llargbundle, [0u, i])
|
2012-03-20 13:44:28 -05:00
|
|
|
} else {
|
2012-08-27 14:16:37 -05:00
|
|
|
load_inbounds(bcx, llargbundle, [0u, i])
|
2012-03-20 13:44:28 -05:00
|
|
|
};
|
2012-09-26 19:33:34 -05:00
|
|
|
llargvals.push(llargval);
|
2012-03-20 13:44:28 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-03-20 13:44:28 -05:00
|
|
|
while i < n {
|
|
|
|
let llargval = load_inbounds(bcx, llargbundle,
|
2012-08-27 14:16:37 -05:00
|
|
|
[0u, i]);
|
2012-09-26 19:33:34 -05:00
|
|
|
llargvals.push(llargval);
|
2012-03-20 13:44:28 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return llargvals;
|
2012-02-13 16:59:05 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn build_ret(bcx: block, tys: @c_stack_tys,
|
2012-02-13 18:06:56 -06:00
|
|
|
llargbundle: ValueRef, llretval: ValueRef) {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("foreign::shim::build_ret");
|
2012-08-06 14:34:08 -05:00
|
|
|
match tys.x86_64_tys {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(x86_64) => {
|
2012-09-18 23:41:37 -05:00
|
|
|
for vec::eachi(x86_64.attrs) |i, a| {
|
2012-09-21 20:43:30 -05:00
|
|
|
match *a {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(attr) => {
|
2012-03-20 13:44:28 -05:00
|
|
|
llvm::LLVMAddInstrAttribute(
|
|
|
|
llretval, (i + 1u) as c_uint,
|
|
|
|
attr as c_uint);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if x86_64.sret || !tys.ret_def {
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
let n = vec::len(tys.arg_tys);
|
2012-08-27 14:16:37 -05:00
|
|
|
let llretptr = GEPi(bcx, llargbundle, [0u, n]);
|
2012-03-20 13:44:28 -05:00
|
|
|
let llretloc = Load(bcx, llretptr);
|
|
|
|
if x86_64.ret_ty.cast {
|
|
|
|
let tmp_ptr = BitCast(bcx, llretloc,
|
|
|
|
T_ptr(x86_64.ret_ty.ty));
|
|
|
|
Store(bcx, llretval, tmp_ptr);
|
|
|
|
} else {
|
|
|
|
Store(bcx, llretval, llretloc);
|
|
|
|
};
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-03-20 13:44:28 -05:00
|
|
|
if tys.ret_def {
|
|
|
|
let n = vec::len(tys.arg_tys);
|
|
|
|
// R** llretptr = &args->r;
|
2012-08-27 14:16:37 -05:00
|
|
|
let llretptr = GEPi(bcx, llargbundle, [0u, n]);
|
2012-03-20 13:44:28 -05:00
|
|
|
// R* llretloc = *llretptr; /* (args->r) */
|
|
|
|
let llretloc = Load(bcx, llretptr);
|
|
|
|
// *args->r = r;
|
|
|
|
Store(bcx, llretval, llretloc);
|
|
|
|
}
|
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
2012-02-13 16:59:05 -06:00
|
|
|
}
|
|
|
|
|
2012-07-18 18:18:02 -05:00
|
|
|
let lname = link_name(ccx, foreign_item);
|
2012-03-22 07:44:16 -05:00
|
|
|
let llbasefn = base_fn(ccx, lname, tys, cc);
|
2012-02-13 18:06:56 -06:00
|
|
|
// Name the shim function
|
2012-07-14 00:57:48 -05:00
|
|
|
let shim_name = lname + ~"__c_stack_shim";
|
2012-08-01 19:30:05 -05:00
|
|
|
return build_shim_fn_(ccx, shim_name, llbasefn, tys, cc,
|
2012-02-13 18:06:56 -06:00
|
|
|
build_args, build_ret);
|
2012-02-13 16:59:05 -06:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn base_fn(ccx: @crate_ctxt, lname: ~str, tys: @c_stack_tys,
|
2012-03-22 07:44:16 -05:00
|
|
|
cc: lib::llvm::CallConv) -> ValueRef {
|
|
|
|
// Declare the "prototype" for the base function F:
|
2012-08-06 14:34:08 -05:00
|
|
|
match tys.x86_64_tys {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(x86_64) => {
|
2012-06-30 18:19:07 -05:00
|
|
|
do decl_x86_64_fn(x86_64) |fnty| {
|
2012-03-22 07:44:16 -05:00
|
|
|
decl_fn(ccx.llmod, lname, cc, fnty)
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-03-22 07:44:16 -05:00
|
|
|
let llbasefnty = T_fn(tys.arg_tys, tys.ret_ty);
|
|
|
|
decl_fn(ccx.llmod, lname, cc, llbasefnty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2535): this is very shaky and probably gets ABIs wrong all
|
|
|
|
// over the place
|
2012-03-22 07:44:16 -05:00
|
|
|
fn build_direct_fn(ccx: @crate_ctxt, decl: ValueRef,
|
2012-06-26 18:18:37 -05:00
|
|
|
item: @ast::foreign_item, tys: @c_stack_tys,
|
2012-03-22 07:44:16 -05:00
|
|
|
cc: lib::llvm::CallConv) {
|
2012-08-20 14:23:37 -05:00
|
|
|
let fcx = new_fn_ctxt(ccx, ~[], decl, None);
|
|
|
|
let bcx = top_scope_block(fcx, None), lltop = bcx.llbb;
|
2012-07-18 18:18:02 -05:00
|
|
|
let llbasefn = base_fn(ccx, link_name(ccx, item), tys, cc);
|
2012-03-22 07:44:16 -05:00
|
|
|
let ty = ty::lookup_item_type(ccx.tcx,
|
|
|
|
ast_util::local_def(item.id)).ty;
|
2012-06-30 18:19:07 -05:00
|
|
|
let args = vec::from_fn(ty::ty_fn_args(ty).len(), |i| {
|
2012-03-22 07:44:16 -05:00
|
|
|
get_param(decl, i + first_real_arg)
|
|
|
|
});
|
|
|
|
let retval = Call(bcx, llbasefn, args);
|
|
|
|
if !ty::type_is_nil(ty::ty_fn_ret(ty)) {
|
|
|
|
Store(bcx, retval, fcx.llretptr);
|
|
|
|
}
|
|
|
|
build_return(bcx);
|
|
|
|
finish_fn(fcx, lltop);
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn build_wrap_fn(ccx: @crate_ctxt,
|
2012-02-13 16:59:05 -06:00
|
|
|
tys: @c_stack_tys,
|
|
|
|
llshimfn: ValueRef,
|
|
|
|
llwrapfn: ValueRef) {
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("foreign::build_wrap_fn");
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn build_args(bcx: block, tys: @c_stack_tys,
|
2012-03-09 03:47:40 -06:00
|
|
|
llwrapfn: ValueRef, llargbundle: ValueRef) {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("foreign::wrap::build_args");
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
|
|
|
let n = vec::len(tys.arg_tys);
|
2012-08-01 19:30:05 -05:00
|
|
|
let implicit_args = first_real_arg; // return + env
|
2012-02-13 18:06:56 -06:00
|
|
|
while i < n {
|
2012-03-21 09:42:20 -05:00
|
|
|
let llargval = get_param(llwrapfn, i + implicit_args);
|
2012-06-29 18:26:56 -05:00
|
|
|
store_inbounds(bcx, llargval, llargbundle, ~[0u, i]);
|
2012-02-13 18:06:56 -06:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-03-21 09:42:20 -05:00
|
|
|
let llretptr = get_param(llwrapfn, 0u);
|
2012-06-29 18:26:56 -05:00
|
|
|
store_inbounds(bcx, llretptr, llargbundle, ~[0u, n]);
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn build_ret(bcx: block, _tys: @c_stack_tys,
|
2012-02-13 18:06:56 -06:00
|
|
|
_llargbundle: ValueRef) {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("foreign::wrap::build_ret");
|
2012-02-13 18:06:56 -06:00
|
|
|
RetVoid(bcx);
|
2012-02-13 16:59:05 -06:00
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
|
|
|
|
build_wrap_fn_(ccx, tys, llshimfn, llwrapfn,
|
|
|
|
ccx.upcalls.call_shim_on_c_stack,
|
2012-03-09 03:47:40 -06:00
|
|
|
build_args, build_ret);
|
2012-02-13 16:59:05 -06:00
|
|
|
}
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
let mut cc = match abi {
|
2012-06-26 18:18:37 -05:00
|
|
|
ast::foreign_abi_rust_intrinsic |
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::foreign_abi_cdecl => lib::llvm::CCallConv,
|
|
|
|
ast::foreign_abi_stdcall => lib::llvm::X86StdcallCallConv
|
2012-03-21 09:42:20 -05:00
|
|
|
};
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(foreign_mod.items) |foreign_item| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match foreign_item.node {
|
2012-09-03 18:44:54 -05:00
|
|
|
ast::foreign_item_fn(*) => {
|
2012-06-26 18:18:37 -05:00
|
|
|
let id = foreign_item.id;
|
|
|
|
if abi != ast::foreign_abi_rust_intrinsic {
|
2012-06-05 20:41:18 -05:00
|
|
|
let llwrapfn = get_item_val(ccx, id);
|
|
|
|
let tys = c_stack_tys(ccx, id);
|
2012-07-14 00:57:48 -05:00
|
|
|
if attr::attrs_contains_name(foreign_item.attrs,
|
|
|
|
~"rust_stack") {
|
2012-09-18 23:41:37 -05:00
|
|
|
build_direct_fn(ccx, llwrapfn, *foreign_item, tys, cc);
|
2012-06-05 20:41:18 -05:00
|
|
|
} else {
|
2012-09-18 23:41:37 -05:00
|
|
|
let llshimfn = build_shim_fn(ccx, *foreign_item, tys, cc);
|
2012-06-05 20:41:18 -05:00
|
|
|
build_wrap_fn(ccx, tys, llshimfn, llwrapfn);
|
|
|
|
}
|
2012-03-22 07:44:16 -05:00
|
|
|
} else {
|
2012-09-03 18:44:54 -05:00
|
|
|
// Intrinsics are emitted by monomorphic fn
|
2012-03-22 07:44:16 -05:00
|
|
|
}
|
2012-02-13 16:59:05 -06:00
|
|
|
}
|
2012-08-30 18:07:55 -05:00
|
|
|
ast::foreign_item_const(*) => {
|
|
|
|
let ident = ccx.sess.parse_sess.interner.get(foreign_item.ident);
|
|
|
|
ccx.item_symbols.insert(foreign_item.id, copy *ident);
|
|
|
|
}
|
2012-02-13 16:59:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-13 17:28:00 -06:00
|
|
|
|
2012-06-26 18:18:37 -05:00
|
|
|
fn trans_intrinsic(ccx: @crate_ctxt, decl: ValueRef, item: @ast::foreign_item,
|
2012-03-23 09:05:16 -05:00
|
|
|
path: ast_map::path, substs: param_substs,
|
2012-08-28 17:54:45 -05:00
|
|
|
ref_id: Option<ast::node_id>)
|
|
|
|
{
|
|
|
|
debug!("trans_intrinsic(item.ident=%s)", ccx.sess.str_of(item.ident));
|
|
|
|
|
2012-10-08 14:39:30 -05:00
|
|
|
let fcx = new_fn_ctxt_w_id(ccx, path, decl, item.id, None,
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(substs), Some(item.span));
|
|
|
|
let mut bcx = top_scope_block(fcx, None), lltop = bcx.llbb;
|
2012-07-18 18:18:02 -05:00
|
|
|
match ccx.sess.str_of(item.ident) {
|
2012-10-21 21:23:50 -05:00
|
|
|
~"atomic_cxchg" => {
|
|
|
|
let old = AtomicCmpXchg(bcx,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
get_param(decl, first_real_arg + 2u),
|
|
|
|
SequentiallyConsistent);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"atomic_cxchg_acq" => {
|
|
|
|
let old = AtomicCmpXchg(bcx,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
get_param(decl, first_real_arg + 2u),
|
|
|
|
Acquire);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"atomic_cxchg_rel" => {
|
|
|
|
let old = AtomicCmpXchg(bcx,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
get_param(decl, first_real_arg + 2u),
|
|
|
|
Release);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
~"atomic_xchg" => {
|
|
|
|
let old = AtomicRMW(bcx, Xchg,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
SequentiallyConsistent);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
2012-03-21 09:42:20 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
~"atomic_xchg_acq" => {
|
|
|
|
let old = AtomicRMW(bcx, Xchg,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
Acquire);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
2012-03-21 09:42:20 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
~"atomic_xchg_rel" => {
|
|
|
|
let old = AtomicRMW(bcx, Xchg,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
Release);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"atomic_xadd" => {
|
|
|
|
let old = AtomicRMW(bcx, lib::llvm::Add,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
SequentiallyConsistent);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"atomic_xadd_acq" => {
|
|
|
|
let old = AtomicRMW(bcx, lib::llvm::Add,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
Acquire);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"atomic_xadd_rel" => {
|
|
|
|
let old = AtomicRMW(bcx, lib::llvm::Add,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
Release);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"atomic_xsub" => {
|
|
|
|
let old = AtomicRMW(bcx, lib::llvm::Sub,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
SequentiallyConsistent);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"atomic_xsub_acq" => {
|
|
|
|
let old = AtomicRMW(bcx, lib::llvm::Sub,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
Acquire);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"atomic_xsub_rel" => {
|
|
|
|
let old = AtomicRMW(bcx, lib::llvm::Sub,
|
|
|
|
get_param(decl, first_real_arg),
|
|
|
|
get_param(decl, first_real_arg + 1u),
|
|
|
|
Release);
|
|
|
|
Store(bcx, old, fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"size_of" => {
|
|
|
|
let tp_ty = substs.tys[0];
|
|
|
|
let lltp_ty = type_of::type_of(ccx, tp_ty);
|
|
|
|
Store(bcx, C_uint(ccx, shape::llsize_of_real(ccx, lltp_ty)),
|
|
|
|
fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"move_val" => {
|
2012-09-20 14:29:15 -05:00
|
|
|
// Create a datum reflecting the value being moved:
|
|
|
|
//
|
|
|
|
// - the datum will be by ref if the value is non-immediate;
|
|
|
|
//
|
|
|
|
// - the datum has a FromRvalue source because, that way,
|
|
|
|
// the `move_to()` method does not feel compelled to
|
|
|
|
// zero out the memory where the datum resides. Zeroing
|
|
|
|
// is not necessary since, for intrinsics, there is no
|
|
|
|
// cleanup to concern ourselves with.
|
2012-08-28 17:54:45 -05:00
|
|
|
let tp_ty = substs.tys[0];
|
2012-09-20 14:29:15 -05:00
|
|
|
let mode = appropriate_mode(tp_ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
let src = Datum {val: get_param(decl, first_real_arg + 1u),
|
2012-09-20 14:29:15 -05:00
|
|
|
ty: tp_ty, mode: mode, source: FromRvalue};
|
2012-08-28 17:54:45 -05:00
|
|
|
bcx = src.move_to(bcx, DROP_EXISTING,
|
|
|
|
get_param(decl, first_real_arg));
|
|
|
|
}
|
|
|
|
~"move_val_init" => {
|
2012-09-20 14:29:15 -05:00
|
|
|
// See comments for `"move_val"`.
|
2012-08-28 17:54:45 -05:00
|
|
|
let tp_ty = substs.tys[0];
|
2012-09-20 14:29:15 -05:00
|
|
|
let mode = appropriate_mode(tp_ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
let src = Datum {val: get_param(decl, first_real_arg + 1u),
|
2012-09-20 14:29:15 -05:00
|
|
|
ty: tp_ty, mode: mode, source: FromRvalue};
|
2012-08-28 17:54:45 -05:00
|
|
|
bcx = src.move_to(bcx, INIT, get_param(decl, first_real_arg));
|
|
|
|
}
|
|
|
|
~"min_align_of" => {
|
|
|
|
let tp_ty = substs.tys[0];
|
|
|
|
let lltp_ty = type_of::type_of(ccx, tp_ty);
|
|
|
|
Store(bcx, C_uint(ccx, shape::llalign_of_min(ccx, lltp_ty)),
|
|
|
|
fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"pref_align_of"=> {
|
|
|
|
let tp_ty = substs.tys[0];
|
|
|
|
let lltp_ty = type_of::type_of(ccx, tp_ty);
|
|
|
|
Store(bcx, C_uint(ccx, shape::llalign_of_pref(ccx, lltp_ty)),
|
|
|
|
fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"get_tydesc" => {
|
|
|
|
let tp_ty = substs.tys[0];
|
|
|
|
let static_ti = get_tydesc(ccx, tp_ty);
|
|
|
|
glue::lazily_emit_all_tydesc_glue(ccx, static_ti);
|
|
|
|
|
2012-10-11 18:42:40 -05:00
|
|
|
// FIXME (#3727): change this to T_ptr(ccx.tydesc_ty) when the
|
2012-08-28 17:54:45 -05:00
|
|
|
// core::sys copy of the get_tydesc interface dies off.
|
|
|
|
let td = PointerCast(bcx, static_ti.tydesc, T_ptr(T_nil()));
|
|
|
|
Store(bcx, td, fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"init" => {
|
|
|
|
let tp_ty = substs.tys[0];
|
|
|
|
let lltp_ty = type_of::type_of(ccx, tp_ty);
|
|
|
|
if !ty::type_is_nil(tp_ty) {
|
|
|
|
Store(bcx, C_null(lltp_ty), fcx.llretptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~"forget" => {}
|
|
|
|
~"reinterpret_cast" => {
|
|
|
|
let tp_ty = substs.tys[0];
|
|
|
|
let lltp_ty = type_of::type_of(ccx, tp_ty);
|
|
|
|
let llout_ty = type_of::type_of(ccx, substs.tys[1]);
|
|
|
|
let tp_sz = shape::llsize_of_real(ccx, lltp_ty),
|
|
|
|
out_sz = shape::llsize_of_real(ccx, llout_ty);
|
|
|
|
if tp_sz != out_sz {
|
2012-09-21 21:37:57 -05:00
|
|
|
let sp = match ccx.tcx.items.get(ref_id.get()) {
|
2012-08-28 17:54:45 -05:00
|
|
|
ast_map::node_expr(e) => e.span,
|
|
|
|
_ => fail ~"reinterpret_cast or forget has non-expr arg"
|
|
|
|
};
|
|
|
|
ccx.sess.span_fatal(
|
|
|
|
sp, fmt!("reinterpret_cast called on types \
|
|
|
|
with different size: %s (%u) to %s (%u)",
|
|
|
|
ty_to_str(ccx.tcx, tp_ty), tp_sz,
|
|
|
|
ty_to_str(ccx.tcx, substs.tys[1]), out_sz));
|
|
|
|
}
|
|
|
|
if !ty::type_is_nil(substs.tys[1]) {
|
|
|
|
// NB: Do not use a Load and Store here. This causes
|
|
|
|
// massive code bloat when reinterpret_cast is used on
|
|
|
|
// large structural types.
|
|
|
|
let llretptr = PointerCast(bcx, fcx.llretptr, T_ptr(T_i8()));
|
|
|
|
let llcast = get_param(decl, first_real_arg);
|
|
|
|
let llcast = PointerCast(bcx, llcast, T_ptr(T_i8()));
|
|
|
|
call_memmove(bcx, llretptr, llcast, llsize_of(ccx, lltp_ty));
|
|
|
|
}
|
2012-03-21 09:42:20 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
~"addr_of" => {
|
|
|
|
Store(bcx, get_param(decl, first_real_arg), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"needs_drop" => {
|
|
|
|
let tp_ty = substs.tys[0];
|
|
|
|
Store(bcx, C_bool(ty::type_needs_drop(ccx.tcx, tp_ty)),
|
|
|
|
fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"visit_tydesc" => {
|
|
|
|
let td = get_param(decl, first_real_arg);
|
|
|
|
let visitor = get_param(decl, first_real_arg + 1u);
|
|
|
|
let td = PointerCast(bcx, td, T_ptr(ccx.tydesc_type));
|
|
|
|
glue::call_tydesc_glue_full(bcx, visitor, td,
|
|
|
|
abi::tydesc_field_visit_glue, None);
|
|
|
|
}
|
|
|
|
~"frame_address" => {
|
|
|
|
let frameaddress = ccx.intrinsics.get(~"llvm.frameaddress");
|
|
|
|
let frameaddress_val = Call(bcx, frameaddress, ~[C_i32(0i32)]);
|
2012-09-07 09:37:19 -05:00
|
|
|
let star_u8 = ty::mk_imm_ptr(
|
|
|
|
bcx.tcx(),
|
|
|
|
ty::mk_mach_uint(bcx.tcx(), ast::ty_u8));
|
|
|
|
let fty = ty::mk_fn(bcx.tcx(), FnTyBase {
|
|
|
|
meta: FnMeta {purity: ast::impure_fn,
|
2012-11-04 22:41:00 -06:00
|
|
|
proto: ast::ProtoBorrowed,
|
2012-11-02 15:33:51 -05:00
|
|
|
onceness: ast::Many,
|
2012-11-04 22:41:00 -06:00
|
|
|
region: ty::re_bound(ty::br_anon(0)),
|
2012-09-07 09:37:19 -05:00
|
|
|
bounds: @~[],
|
|
|
|
ret_style: ast::return_val},
|
|
|
|
sig: FnSig {inputs: ~[{mode: ast::expl(ast::by_val),
|
|
|
|
ty: star_u8}],
|
|
|
|
output: ty::mk_nil(bcx.tcx())}
|
2012-08-28 17:54:45 -05:00
|
|
|
});
|
|
|
|
let datum = Datum {val: get_param(decl, first_real_arg),
|
|
|
|
mode: ByRef, ty: fty, source: FromLvalue};
|
|
|
|
bcx = trans_call_inner(
|
|
|
|
bcx, None, fty, ty::mk_nil(bcx.tcx()),
|
|
|
|
|bcx| Callee {bcx: bcx, data: Closure(datum)},
|
2012-09-19 20:00:26 -05:00
|
|
|
ArgVals(~[frameaddress_val]), Ignore, DontAutorefArg);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
~"morestack_addr" => {
|
|
|
|
// XXX This is a hack to grab the address of this particular
|
|
|
|
// native function. There should be a general in-language
|
|
|
|
// way to do this
|
|
|
|
let llfty = type_of_fn(bcx.ccx(), ~[], ty::mk_nil(bcx.tcx()));
|
|
|
|
let morestack_addr = decl_cdecl_fn(
|
|
|
|
bcx.ccx().llmod, ~"__morestack", llfty);
|
|
|
|
let morestack_addr = PointerCast(bcx, morestack_addr,
|
|
|
|
T_ptr(T_nil()));
|
|
|
|
Store(bcx, morestack_addr, fcx.llretptr);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Could we make this an enum rather than a string? does it get
|
|
|
|
// checked earlier?
|
|
|
|
ccx.sess.span_bug(item.span, ~"unknown intrinsic");
|
|
|
|
}
|
2012-03-21 09:42:20 -05:00
|
|
|
}
|
|
|
|
build_return(bcx);
|
|
|
|
finish_fn(fcx, lltop);
|
|
|
|
}
|
|
|
|
|
2012-07-03 18:11:00 -05:00
|
|
|
fn trans_foreign_fn(ccx: @crate_ctxt, path: ast_map::path, decl: ast::fn_decl,
|
2012-02-13 18:06:56 -06:00
|
|
|
body: ast::blk, llwrapfn: ValueRef, id: ast::node_id) {
|
|
|
|
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("foreign::build_foreign_fn");
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn build_rust_fn(ccx: @crate_ctxt, path: ast_map::path,
|
2012-02-13 18:06:56 -06:00
|
|
|
decl: ast::fn_decl, body: ast::blk,
|
|
|
|
id: ast::node_id) -> ValueRef {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("foreign::foreign::build_rust_fn");
|
2012-02-13 18:06:56 -06:00
|
|
|
let t = ty::node_id_to_type(ccx.tcx, id);
|
2012-09-18 13:46:39 -05:00
|
|
|
let ps = link::mangle_internal_name_by_path(
|
2012-07-18 18:18:02 -05:00
|
|
|
ccx, vec::append_one(path, ast_map::path_name(
|
|
|
|
syntax::parse::token::special_idents::clownshoe_abi
|
|
|
|
)));
|
2012-03-09 03:47:40 -06:00
|
|
|
let llty = type_of_fn_from_ty(ccx, t);
|
2012-02-13 18:06:56 -06:00
|
|
|
let llfndecl = decl_internal_cdecl_fn(ccx.llmod, ps, llty);
|
2012-10-08 14:39:30 -05:00
|
|
|
trans_fn(ccx, path, decl, body, llfndecl, no_self, None, id, None);
|
2012-08-01 19:30:05 -05:00
|
|
|
return llfndecl;
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn build_shim_fn(ccx: @crate_ctxt, path: ast_map::path,
|
2012-02-13 18:06:56 -06:00
|
|
|
llrustfn: ValueRef, tys: @c_stack_tys) -> ValueRef {
|
|
|
|
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("foreign::foreign::build_shim_fn");
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn build_args(bcx: block, tys: @c_stack_tys,
|
2012-06-29 18:26:56 -05:00
|
|
|
llargbundle: ValueRef) -> ~[ValueRef] {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("foreign::extern::shim::build_args");
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut llargvals = ~[];
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
2012-02-13 18:06:56 -06:00
|
|
|
let n = vec::len(tys.arg_tys);
|
2012-06-29 18:26:56 -05:00
|
|
|
let llretptr = load_inbounds(bcx, llargbundle, ~[0u, n]);
|
2012-09-26 19:33:34 -05:00
|
|
|
llargvals.push(llretptr);
|
2012-02-21 07:20:18 -06:00
|
|
|
let llenvptr = C_null(T_opaque_box_ptr(bcx.ccx()));
|
2012-09-26 19:33:34 -05:00
|
|
|
llargvals.push(llenvptr);
|
2012-02-13 18:06:56 -06:00
|
|
|
while i < n {
|
2012-06-29 18:26:56 -05:00
|
|
|
let llargval = load_inbounds(bcx, llargbundle, ~[0u, i]);
|
2012-09-26 19:33:34 -05:00
|
|
|
llargvals.push(llargval);
|
2012-02-13 18:06:56 -06:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return llargvals;
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn build_ret(_bcx: block, _tys: @c_stack_tys,
|
2012-02-13 18:06:56 -06:00
|
|
|
_llargbundle: ValueRef, _llretval: ValueRef) {
|
|
|
|
// Nop. The return pointer in the Rust ABI function
|
|
|
|
// is wired directly into the return slot in the shim struct
|
|
|
|
}
|
|
|
|
|
2012-09-18 13:46:39 -05:00
|
|
|
let shim_name = link::mangle_internal_name_by_path(
|
2012-07-18 18:18:02 -05:00
|
|
|
ccx, vec::append_one(path, ast_map::path_name(
|
|
|
|
syntax::parse::token::special_idents::clownshoe_stack_shim
|
|
|
|
)));
|
2012-08-01 19:30:05 -05:00
|
|
|
return build_shim_fn_(ccx, shim_name, llrustfn, tys,
|
2012-02-13 18:06:56 -06:00
|
|
|
lib::llvm::CCallConv,
|
|
|
|
build_args, build_ret);
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:31:16 -05:00
|
|
|
fn build_wrap_fn(ccx: @crate_ctxt, llshimfn: ValueRef,
|
2012-02-13 18:06:56 -06:00
|
|
|
llwrapfn: ValueRef, tys: @c_stack_tys) {
|
|
|
|
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("foreign::foreign::build_wrap_fn");
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn build_args(bcx: block, tys: @c_stack_tys,
|
2012-02-13 18:06:56 -06:00
|
|
|
llwrapfn: ValueRef, llargbundle: ValueRef) {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("foreign::foreign::wrap::build_args");
|
2012-08-06 14:34:08 -05:00
|
|
|
match tys.x86_64_tys {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some(x86_64) => {
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut atys = x86_64.arg_tys;
|
|
|
|
let mut attrs = x86_64.attrs;
|
|
|
|
let mut j = 0u;
|
2012-03-20 13:44:28 -05:00
|
|
|
let llretptr = if x86_64.sret {
|
|
|
|
atys = vec::tail(atys);
|
|
|
|
attrs = vec::tail(attrs);
|
|
|
|
j = 1u;
|
2012-03-21 09:42:20 -05:00
|
|
|
get_param(llwrapfn, 0u)
|
2012-03-20 13:44:28 -05:00
|
|
|
} else if x86_64.ret_ty.cast {
|
|
|
|
let retptr = alloca(bcx, x86_64.ret_ty.ty);
|
|
|
|
BitCast(bcx, retptr, T_ptr(tys.ret_ty))
|
|
|
|
} else {
|
|
|
|
alloca(bcx, tys.ret_ty)
|
|
|
|
};
|
|
|
|
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = 0u;
|
|
|
|
let n = vec::len(atys);
|
2012-03-20 13:44:28 -05:00
|
|
|
while i < n {
|
2012-03-21 09:42:20 -05:00
|
|
|
let mut argval = get_param(llwrapfn, i + j);
|
2012-09-21 21:37:57 -05:00
|
|
|
if attrs[i].is_some() {
|
2012-03-20 13:44:28 -05:00
|
|
|
argval = Load(bcx, argval);
|
2012-06-25 22:00:46 -05:00
|
|
|
store_inbounds(bcx, argval, llargbundle,
|
2012-08-27 14:16:37 -05:00
|
|
|
[0u, i]);
|
2012-03-20 13:44:28 -05:00
|
|
|
} else if atys[i].cast {
|
2012-08-27 14:16:37 -05:00
|
|
|
let argptr = GEPi(bcx, llargbundle, [0u, i]);
|
2012-03-15 08:47:03 -05:00
|
|
|
let argptr = BitCast(bcx, argptr,
|
|
|
|
T_ptr(atys[i].ty));
|
2012-03-20 13:44:28 -05:00
|
|
|
Store(bcx, argval, argptr);
|
|
|
|
} else {
|
2012-06-25 22:00:46 -05:00
|
|
|
store_inbounds(bcx, argval, llargbundle,
|
2012-08-27 14:16:37 -05:00
|
|
|
[0u, i]);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
2012-08-27 14:16:37 -05:00
|
|
|
store_inbounds(bcx, llretptr, llargbundle, [0u, n]);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-03-20 13:44:28 -05:00
|
|
|
let llretptr = alloca(bcx, tys.ret_ty);
|
2012-03-15 08:47:03 -05:00
|
|
|
let n = vec::len(tys.arg_tys);
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, n) |i| {
|
2012-03-21 09:42:20 -05:00
|
|
|
let llargval = get_param(llwrapfn, i);
|
2012-03-20 13:44:28 -05:00
|
|
|
store_inbounds(bcx, llargval, llargbundle,
|
2012-08-27 14:16:37 -05:00
|
|
|
[0u, i]);
|
2012-05-04 00:31:38 -05:00
|
|
|
};
|
2012-08-27 14:16:37 -05:00
|
|
|
store_inbounds(bcx, llretptr, llargbundle, [0u, n]);
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:17:40 -06:00
|
|
|
fn build_ret(bcx: block, tys: @c_stack_tys,
|
2012-02-13 18:06:56 -06:00
|
|
|
llargbundle: ValueRef) {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = bcx.insn_ctxt("foreign::foreign::wrap::build_ret");
|
2012-08-06 14:34:08 -05:00
|
|
|
match tys.x86_64_tys {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some(x86_64) => {
|
2012-03-20 13:44:28 -05:00
|
|
|
if x86_64.sret || !tys.ret_def {
|
|
|
|
RetVoid(bcx);
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
|
|
|
let n = vec::len(tys.arg_tys);
|
2012-06-29 18:26:56 -05:00
|
|
|
let llretval = load_inbounds(bcx, llargbundle, ~[0u, n]);
|
2012-03-20 13:44:28 -05:00
|
|
|
let llretval = if x86_64.ret_ty.cast {
|
|
|
|
let retptr = BitCast(bcx, llretval,
|
|
|
|
T_ptr(x86_64.ret_ty.ty));
|
|
|
|
Load(bcx, retptr)
|
|
|
|
} else {
|
|
|
|
Load(bcx, llretval)
|
|
|
|
};
|
|
|
|
Ret(bcx, llretval);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-03-20 13:44:28 -05:00
|
|
|
let n = vec::len(tys.arg_tys);
|
2012-06-29 18:26:56 -05:00
|
|
|
let llretval = load_inbounds(bcx, llargbundle, ~[0u, n]);
|
2012-03-20 13:44:28 -05:00
|
|
|
let llretval = Load(bcx, llretval);
|
|
|
|
Ret(bcx, llretval);
|
|
|
|
}
|
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
build_wrap_fn_(ccx, tys, llshimfn, llwrapfn,
|
|
|
|
ccx.upcalls.call_shim_on_rust_stack,
|
|
|
|
build_args, build_ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
let tys = c_stack_tys(ccx, id);
|
|
|
|
// The internal Rust ABI function - runs on the Rust stack
|
|
|
|
let llrustfn = build_rust_fn(ccx, path, decl, body, id);
|
|
|
|
// The internal shim function - runs on the Rust stack
|
|
|
|
let llshimfn = build_shim_fn(ccx, path, llrustfn, tys);
|
2012-07-03 18:11:00 -05:00
|
|
|
// The foreign C function - runs on the C stack
|
2012-02-13 18:06:56 -06:00
|
|
|
build_wrap_fn(ccx, llshimfn, llwrapfn, tys)
|
|
|
|
}
|
|
|
|
|
2012-07-03 18:11:00 -05:00
|
|
|
fn register_foreign_fn(ccx: @crate_ctxt, sp: span,
|
2012-03-06 04:33:25 -06:00
|
|
|
path: ast_map::path, node_id: ast::node_id)
|
|
|
|
-> ValueRef {
|
2012-08-14 18:45:43 -05:00
|
|
|
let _icx = ccx.insn_ctxt("foreign::register_foreign_fn");
|
2012-02-13 18:06:56 -06:00
|
|
|
let t = ty::node_id_to_type(ccx.tcx, node_id);
|
2012-03-20 13:44:28 -05:00
|
|
|
let (llargtys, llretty, ret_ty) = c_arg_and_ret_lltys(ccx, node_id);
|
2012-08-01 19:30:05 -05:00
|
|
|
return if ccx.sess.targ_cfg.arch == arch_x86_64 {
|
2012-03-20 13:44:28 -05:00
|
|
|
let ret_def = !ty::type_is_bot(ret_ty) && !ty::type_is_nil(ret_ty);
|
|
|
|
let x86_64 = x86_64_tys(llargtys, llretty, ret_def);
|
2012-06-30 18:19:07 -05:00
|
|
|
do decl_x86_64_fn(x86_64) |fnty| {
|
2012-03-20 17:15:57 -05:00
|
|
|
register_fn_fuller(ccx, sp, path, node_id,
|
2012-03-20 13:44:28 -05:00
|
|
|
t, lib::llvm::CCallConv, fnty)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let llfty = T_fn(llargtys, llretty);
|
2012-03-20 17:15:57 -05:00
|
|
|
register_fn_fuller(ccx, sp, path, node_id,
|
2012-03-20 13:44:28 -05:00
|
|
|
t, lib::llvm::CCallConv, llfty)
|
|
|
|
}
|
2012-03-07 18:48:57 -06:00
|
|
|
}
|
2012-03-07 05:21:08 -06:00
|
|
|
|
2012-06-26 18:18:37 -05:00
|
|
|
fn abi_of_foreign_fn(ccx: @crate_ctxt, i: @ast::foreign_item)
|
|
|
|
-> ast::foreign_abi {
|
2012-08-06 14:34:08 -05:00
|
|
|
match attr::first_attr_value_str_by_name(i.attrs, ~"abi") {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => match ccx.tcx.items.get(i.id) {
|
2012-08-23 19:39:07 -05:00
|
|
|
ast_map::node_foreign_item(_, abi, _) => abi,
|
|
|
|
// ??
|
|
|
|
_ => fail ~"abi_of_foreign_fn: not foreign"
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(_) => match attr::foreign_abi(i.attrs) {
|
2012-08-14 18:54:13 -05:00
|
|
|
either::Right(abi) => abi,
|
|
|
|
either::Left(msg) => ccx.sess.span_fatal(i.span, msg)
|
2012-03-07 05:21:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|