2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2012-12-13 15:05:22 -06:00
|
|
|
use back::{link, abi};
|
2012-10-15 16:56:42 -05:00
|
|
|
use driver::session::arch_x86_64;
|
2012-11-29 18:21:49 -06:00
|
|
|
use driver::session::arch_arm;
|
2012-12-13 15:05:22 -06:00
|
|
|
use lib::llvm::{SequentiallyConsistent, Acquire, Release, Xchg};
|
|
|
|
use lib::llvm::{Struct, Array, ModuleRef, CallConv, Attribute};
|
|
|
|
use lib::llvm::{StructRetAttribute, ByValAttribute};
|
|
|
|
use lib::llvm::{llvm, TypeRef, ValueRef, Integer, Pointer, Float, Double};
|
2012-12-23 16:41:37 -06:00
|
|
|
use lib;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::base::*;
|
2013-01-25 16:56:56 -06:00
|
|
|
use middle::trans::cabi;
|
|
|
|
use middle::trans::cabi_x86_64::*;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::build::*;
|
|
|
|
use middle::trans::callee::*;
|
|
|
|
use middle::trans::common::*;
|
|
|
|
use middle::trans::datum::*;
|
|
|
|
use middle::trans::expr::{Dest, Ignore};
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::glue;
|
|
|
|
use middle::trans::machine;
|
|
|
|
use middle::trans::shape;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::type_of::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::type_of;
|
2013-01-25 18:57:39 -06:00
|
|
|
use middle::ty::{FnTyBase, FnMeta, FnSig, arg};
|
2012-12-13 15:05:22 -06:00
|
|
|
use util::ppaux::ty_to_str;
|
|
|
|
|
|
|
|
use core::libc::c_uint;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::codemap::span;
|
|
|
|
use syntax::{ast, ast_util};
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::{attr, ast_map};
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::parse::token::special_idents;
|
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
|
|
|
|
2013-01-25 16:56:56 -06:00
|
|
|
fn abi_info(arch: session::arch) -> cabi::ABIInfo {
|
|
|
|
return match arch {
|
|
|
|
arch_x86_64 => x86_64_abi_info(),
|
|
|
|
_ => cabi::llvm_abi_info()
|
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),
|
2013-01-07 16:16:52 -06:00
|
|
|
option::Some(ref ln) => (/*bad*/copy *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,
|
2013-01-25 16:56:56 -06:00
|
|
|
fn_ty: cabi::FnType
|
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) => {
|
2013-01-07 16:16:52 -06:00
|
|
|
let llargtys = type_of_explicit_args(
|
|
|
|
ccx,
|
|
|
|
/*bad*/copy fn_ty.sig.inputs);
|
2012-09-07 09:37:19 -05:00
|
|
|
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);
|
2013-01-07 16:16:52 -06:00
|
|
|
// XXX: Bad copy.
|
|
|
|
let bundle_ty = T_struct(vec::append_one(copy 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);
|
2013-01-25 16:56:56 -06:00
|
|
|
let fn_ty = abi_info(ccx.sess.targ_cfg.arch).
|
|
|
|
compute_info(llargtys, llretty, ret_def);
|
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()),
|
2013-01-25 16:56:56 -06:00
|
|
|
fn_ty: fn_ty
|
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,
|
2013-01-07 16:16:52 -06: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");
|
2013-01-25 16:56:56 -06:00
|
|
|
return tys.fn_ty.build_shim_args(bcx, tys.arg_tys, llargbundle);
|
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");
|
2013-01-25 16:56:56 -06:00
|
|
|
tys.fn_ty.build_shim_ret(bcx, tys.arg_tys, tys.ret_def,
|
|
|
|
llargbundle, llretval);
|
2012-02-13 16:59:05 -06:00
|
|
|
}
|
|
|
|
|
2012-07-18 18:18:02 -05:00
|
|
|
let lname = link_name(ccx, foreign_item);
|
2013-01-07 16:16:52 -06:00
|
|
|
let llbasefn = base_fn(ccx, copy 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
|
|
|
}
|
|
|
|
|
2013-01-07 16:16:52 -06: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:
|
2013-01-25 16:56:56 -06:00
|
|
|
do tys.fn_ty.decl_fn |fnty| {
|
|
|
|
decl_fn(ccx.llmod, /*bad*/copy lname, cc, fnty)
|
2012-03-22 07:44:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2013-01-10 01:17:57 -06: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,
|
2013-01-07 16:16:52 -06: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));
|
|
|
|
|
2013-01-07 16:16:52 -06:00
|
|
|
// XXX: Bad copy.
|
2012-10-08 14:39:30 -05:00
|
|
|
let fcx = new_fn_ctxt_w_id(ccx, path, decl, item.id, None,
|
2013-01-07 16:16:52 -06:00
|
|
|
Some(copy substs), Some(item.span));
|
2012-08-20 14:23:37 -05:00
|
|
|
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]);
|
2012-12-05 22:21:29 -06:00
|
|
|
let tp_sz = machine::llbitsize_of_real(ccx, lltp_ty),
|
|
|
|
out_sz = machine::llbitsize_of_real(ccx, llout_ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
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 \
|
2012-12-05 22:21:29 -06:00
|
|
|
with different size: %s (%u bit(s)) to %s \
|
|
|
|
(%u bit(s))",
|
2012-08-28 17:54:45 -05:00
|
|
|
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()));
|
2012-11-16 13:26:26 -06:00
|
|
|
call_memcpy(bcx, llretptr, llcast, llsize_of(ccx, lltp_ty));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2012-11-29 05:48:07 -06: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)),
|
2013-01-08 08:21:19 -06:00
|
|
|
bounds: @~[]},
|
2013-01-25 18:57:39 -06:00
|
|
|
sig: FnSig {inputs: ~[arg {mode: ast::expl(ast::by_val),
|
|
|
|
ty: star_u8}],
|
2012-09-07 09:37:19 -05:00
|
|
|
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);
|
|
|
|
}
|
2013-01-29 00:44:59 -06:00
|
|
|
~"memmove32" => {
|
|
|
|
let dst_ptr = get_param(decl, first_real_arg);
|
|
|
|
let src_ptr = get_param(decl, first_real_arg + 1);
|
|
|
|
let size = get_param(decl, first_real_arg + 2);
|
|
|
|
let align = C_i32(1);
|
|
|
|
let volatile = C_bool(false);
|
2013-01-30 00:40:13 -06:00
|
|
|
let llfn = bcx.ccx().intrinsics.get(
|
|
|
|
~"llvm.memmove.p0i8.p0i8.i32");
|
2013-01-29 00:44:59 -06:00
|
|
|
Call(bcx, llfn, ~[dst_ptr, src_ptr, size, align, volatile]);
|
|
|
|
}
|
|
|
|
~"memmove64" => {
|
|
|
|
let dst_ptr = get_param(decl, first_real_arg);
|
|
|
|
let src_ptr = get_param(decl, first_real_arg + 1);
|
|
|
|
let size = get_param(decl, first_real_arg + 2);
|
|
|
|
let align = C_i32(1);
|
|
|
|
let volatile = C_bool(false);
|
2013-01-30 00:40:13 -06:00
|
|
|
let llfn = bcx.ccx().intrinsics.get(
|
|
|
|
~"llvm.memmove.p0i8.p0i8.i64");
|
2013-01-29 00:44:59 -06:00
|
|
|
Call(bcx, llfn, ~[dst_ptr, src_ptr, size, align, volatile]);
|
|
|
|
}
|
2012-11-29 05:48:07 -06:00
|
|
|
~"sqrtf32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let sqrtf = ccx.intrinsics.get(~"llvm.sqrt.f32");
|
|
|
|
Store(bcx, Call(bcx, sqrtf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"sqrtf64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let sqrtf = ccx.intrinsics.get(~"llvm.sqrt.f64");
|
|
|
|
Store(bcx, Call(bcx, sqrtf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"powif32" => {
|
|
|
|
let a = get_param(decl, first_real_arg);
|
|
|
|
let x = get_param(decl, first_real_arg + 1u);
|
|
|
|
let powif = ccx.intrinsics.get(~"llvm.powi.f32");
|
|
|
|
Store(bcx, Call(bcx, powif, ~[a, x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"powif64" => {
|
|
|
|
let a = get_param(decl, first_real_arg);
|
|
|
|
let x = get_param(decl, first_real_arg + 1u);
|
|
|
|
let powif = ccx.intrinsics.get(~"llvm.powi.f64");
|
|
|
|
Store(bcx, Call(bcx, powif, ~[a, x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"sinf32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let sinf = ccx.intrinsics.get(~"llvm.sin.f32");
|
|
|
|
Store(bcx, Call(bcx, sinf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"sinf64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let sinf = ccx.intrinsics.get(~"llvm.sin.f64");
|
|
|
|
Store(bcx, Call(bcx, sinf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"cosf32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let cosf = ccx.intrinsics.get(~"llvm.cos.f32");
|
|
|
|
Store(bcx, Call(bcx, cosf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"cosf64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let cosf = ccx.intrinsics.get(~"llvm.cos.f64");
|
|
|
|
Store(bcx, Call(bcx, cosf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"powf32" => {
|
|
|
|
let a = get_param(decl, first_real_arg);
|
|
|
|
let x = get_param(decl, first_real_arg + 1u);
|
|
|
|
let powf = ccx.intrinsics.get(~"llvm.pow.f32");
|
|
|
|
Store(bcx, Call(bcx, powf, ~[a, x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"powf64" => {
|
|
|
|
let a = get_param(decl, first_real_arg);
|
|
|
|
let x = get_param(decl, first_real_arg + 1u);
|
|
|
|
let powf = ccx.intrinsics.get(~"llvm.pow.f64");
|
|
|
|
Store(bcx, Call(bcx, powf, ~[a, x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"expf32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let expf = ccx.intrinsics.get(~"llvm.exp.f32");
|
|
|
|
Store(bcx, Call(bcx, expf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"expf64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let expf = ccx.intrinsics.get(~"llvm.exp.f64");
|
|
|
|
Store(bcx, Call(bcx, expf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"exp2f32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let exp2f = ccx.intrinsics.get(~"llvm.exp2.f32");
|
|
|
|
Store(bcx, Call(bcx, exp2f, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"exp2f64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let exp2f = ccx.intrinsics.get(~"llvm.exp2.f64");
|
|
|
|
Store(bcx, Call(bcx, exp2f, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"logf32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let logf = ccx.intrinsics.get(~"llvm.log.f32");
|
|
|
|
Store(bcx, Call(bcx, logf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"logf64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let logf = ccx.intrinsics.get(~"llvm.log.f64");
|
|
|
|
Store(bcx, Call(bcx, logf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"log10f32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let log10f = ccx.intrinsics.get(~"llvm.log10.f32");
|
|
|
|
Store(bcx, Call(bcx, log10f, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"log10f64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let log10f = ccx.intrinsics.get(~"llvm.log10.f64");
|
|
|
|
Store(bcx, Call(bcx, log10f, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"log2f32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let log2f = ccx.intrinsics.get(~"llvm.log2.f32");
|
|
|
|
Store(bcx, Call(bcx, log2f, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"log2f64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let log2f = ccx.intrinsics.get(~"llvm.log2.f64");
|
|
|
|
Store(bcx, Call(bcx, log2f, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"fmaf32" => {
|
|
|
|
let a = get_param(decl, first_real_arg);
|
|
|
|
let b = get_param(decl, first_real_arg + 1u);
|
|
|
|
let c = get_param(decl, first_real_arg + 2u);
|
|
|
|
let fmaf = ccx.intrinsics.get(~"llvm.fma.f32");
|
|
|
|
Store(bcx, Call(bcx, fmaf, ~[a, b, c]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"fmaf64" => {
|
|
|
|
let a = get_param(decl, first_real_arg);
|
|
|
|
let b = get_param(decl, first_real_arg + 1u);
|
|
|
|
let c = get_param(decl, first_real_arg + 2u);
|
|
|
|
let fmaf = ccx.intrinsics.get(~"llvm.fma.f64");
|
|
|
|
Store(bcx, Call(bcx, fmaf, ~[a, b, c]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"fabsf32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let fabsf = ccx.intrinsics.get(~"llvm.fabs.f32");
|
|
|
|
Store(bcx, Call(bcx, fabsf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"fabsf64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let fabsf = ccx.intrinsics.get(~"llvm.fabs.f64");
|
|
|
|
Store(bcx, Call(bcx, fabsf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"floorf32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let floorf = ccx.intrinsics.get(~"llvm.floor.f32");
|
|
|
|
Store(bcx, Call(bcx, floorf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"floorf64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let floorf = ccx.intrinsics.get(~"llvm.floor.f64");
|
|
|
|
Store(bcx, Call(bcx, floorf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"ceilf32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let ceilf = ccx.intrinsics.get(~"llvm.ceil.f32");
|
|
|
|
Store(bcx, Call(bcx, ceilf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"ceilf64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let ceilf = ccx.intrinsics.get(~"llvm.ceil.f64");
|
|
|
|
Store(bcx, Call(bcx, ceilf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"truncf32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let truncf = ccx.intrinsics.get(~"llvm.trunc.f32");
|
|
|
|
Store(bcx, Call(bcx, truncf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
|
|
|
~"truncf64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let truncf = ccx.intrinsics.get(~"llvm.trunc.f64");
|
|
|
|
Store(bcx, Call(bcx, truncf, ~[x]), fcx.llretptr);
|
|
|
|
}
|
2012-12-21 12:30:33 -06:00
|
|
|
~"ctpop8" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let ctpop = ccx.intrinsics.get(~"llvm.ctpop.i8");
|
|
|
|
Store(bcx, Call(bcx, ctpop, ~[x]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"ctpop16" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let ctpop = ccx.intrinsics.get(~"llvm.ctpop.i16");
|
|
|
|
Store(bcx, Call(bcx, ctpop, ~[x]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"ctpop32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let ctpop = ccx.intrinsics.get(~"llvm.ctpop.i32");
|
|
|
|
Store(bcx, Call(bcx, ctpop, ~[x]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"ctpop64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let ctpop = ccx.intrinsics.get(~"llvm.ctpop.i64");
|
|
|
|
Store(bcx, Call(bcx, ctpop, ~[x]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"ctlz8" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let y = C_bool(false);
|
|
|
|
let ctlz = ccx.intrinsics.get(~"llvm.ctlz.i8");
|
|
|
|
Store(bcx, Call(bcx, ctlz, ~[x, y]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"ctlz16" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let y = C_bool(false);
|
|
|
|
let ctlz = ccx.intrinsics.get(~"llvm.ctlz.i16");
|
|
|
|
Store(bcx, Call(bcx, ctlz, ~[x, y]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"ctlz32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let y = C_bool(false);
|
|
|
|
let ctlz = ccx.intrinsics.get(~"llvm.ctlz.i32");
|
|
|
|
Store(bcx, Call(bcx, ctlz, ~[x, y]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"ctlz64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let y = C_bool(false);
|
|
|
|
let ctlz = ccx.intrinsics.get(~"llvm.ctlz.i64");
|
|
|
|
Store(bcx, Call(bcx, ctlz, ~[x, y]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"cttz8" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let y = C_bool(false);
|
|
|
|
let cttz = ccx.intrinsics.get(~"llvm.cttz.i8");
|
|
|
|
Store(bcx, Call(bcx, cttz, ~[x, y]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"cttz16" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let y = C_bool(false);
|
|
|
|
let cttz = ccx.intrinsics.get(~"llvm.cttz.i16");
|
|
|
|
Store(bcx, Call(bcx, cttz, ~[x, y]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"cttz32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let y = C_bool(false);
|
|
|
|
let cttz = ccx.intrinsics.get(~"llvm.cttz.i32");
|
|
|
|
Store(bcx, Call(bcx, cttz, ~[x, y]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"cttz64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let y = C_bool(false);
|
|
|
|
let cttz = ccx.intrinsics.get(~"llvm.cttz.i64");
|
|
|
|
Store(bcx, Call(bcx, cttz, ~[x, y]), fcx.llretptr)
|
|
|
|
}
|
2012-12-27 12:07:05 -06:00
|
|
|
~"bswap16" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let cttz = ccx.intrinsics.get(~"llvm.bswap.i16");
|
|
|
|
Store(bcx, Call(bcx, cttz, ~[x]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"bswap32" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let cttz = ccx.intrinsics.get(~"llvm.bswap.i32");
|
|
|
|
Store(bcx, Call(bcx, cttz, ~[x]), fcx.llretptr)
|
|
|
|
}
|
|
|
|
~"bswap64" => {
|
|
|
|
let x = get_param(decl, first_real_arg);
|
|
|
|
let cttz = ccx.intrinsics.get(~"llvm.bswap.i64");
|
|
|
|
Store(bcx, Call(bcx, cttz, ~[x]), fcx.llretptr)
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2013-01-07 16:16:52 -06:00
|
|
|
fn trans_foreign_fn(ccx: @crate_ctxt, +path: ast_map::path,
|
|
|
|
decl: ast::fn_decl, 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
|
|
|
|
2013-01-07 16:16:52 -06: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);
|
2013-01-07 16:16:52 -06:00
|
|
|
// XXX: Bad copy.
|
2012-09-18 13:46:39 -05:00
|
|
|
let ps = link::mangle_internal_name_by_path(
|
2013-01-07 16:16:52 -06:00
|
|
|
ccx, vec::append_one(copy path, ast_map::path_name(
|
2012-12-23 16:41:37 -06:00
|
|
|
special_idents::clownshoe_abi
|
2012-07-18 18:18:02 -05:00
|
|
|
)));
|
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
|
|
|
}
|
|
|
|
|
2013-01-07 16:16:52 -06: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(
|
2012-12-23 16:41:37 -06:00
|
|
|
special_idents::clownshoe_stack_shim
|
2012-07-18 18:18:02 -05:00
|
|
|
)));
|
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");
|
2013-01-25 16:56:56 -06:00
|
|
|
tys.fn_ty.build_wrap_args(bcx, tys.ret_ty,
|
|
|
|
llwrapfn, llargbundle);
|
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");
|
2013-01-25 16:56:56 -06:00
|
|
|
tys.fn_ty.build_wrap_ret(bcx, tys.arg_tys, llargbundle);
|
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
|
2013-01-07 16:16:52 -06:00
|
|
|
// XXX: Bad copy.
|
|
|
|
let llrustfn = build_rust_fn(ccx, copy path, decl, body, id);
|
2012-02-13 18:06:56 -06:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2013-01-07 16:16:52 -06:00
|
|
|
fn register_foreign_fn(ccx: @crate_ctxt,
|
|
|
|
sp: span,
|
|
|
|
+path: ast_map::path,
|
2013-01-10 01:17:57 -06:00
|
|
|
node_id: ast::node_id,
|
|
|
|
attrs: &[ast::attribute])
|
2013-01-07 16:16:52 -06:00
|
|
|
-> 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);
|
2013-01-25 16:56:56 -06:00
|
|
|
let ret_def = !ty::type_is_bot(ret_ty) && !ty::type_is_nil(ret_ty);
|
|
|
|
let fn_ty = abi_info(ccx.sess.targ_cfg.arch).
|
|
|
|
compute_info(llargtys, llretty, ret_def);
|
|
|
|
do fn_ty.decl_fn |fnty| {
|
|
|
|
register_fn_fuller(ccx, sp, /*bad*/copy path, node_id, attrs,
|
|
|
|
t, lib::llvm::CCallConv, fnty)
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
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,
|
2013-01-07 16:16:52 -06:00
|
|
|
either::Left(ref msg) => {
|
|
|
|
ccx.sess.span_fatal(i.span, (/*bad*/copy *msg))
|
|
|
|
}
|
2012-03-07 05:21:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|