2014-08-01 23:25:41 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
use back::{link};
|
2014-11-11 19:22:41 -06:00
|
|
|
use llvm::{ValueRef, CallConv, get_param};
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm;
|
2014-05-19 11:30:09 -05:00
|
|
|
use middle::weak_lang_items;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::base::{llvm_linkage_by_name, push_ctxt};
|
|
|
|
use trans::base;
|
|
|
|
use trans::build::*;
|
|
|
|
use trans::cabi;
|
|
|
|
use trans::common::*;
|
|
|
|
use trans::machine;
|
|
|
|
use trans::type_::Type;
|
|
|
|
use trans::type_of::*;
|
|
|
|
use trans::type_of;
|
2013-04-26 21:13:38 -05:00
|
|
|
use middle::ty::FnSig;
|
2014-09-13 13:09:25 -05:00
|
|
|
use middle::ty::{mod, Ty};
|
2014-11-06 01:24:44 -06:00
|
|
|
use middle::subst::{Subst, Substs};
|
2013-11-29 13:19:22 -06:00
|
|
|
use std::cmp;
|
2014-02-26 11:58:41 -06:00
|
|
|
use libc::c_uint;
|
2014-04-02 03:19:41 -05:00
|
|
|
use syntax::abi::{Cdecl, Aapcs, C, Win64, Abi};
|
2014-05-29 00:26:56 -05:00
|
|
|
use syntax::abi::{RustIntrinsic, Rust, RustCall, Stdcall, Fastcall, System};
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2014-01-10 16:02:36 -06:00
|
|
|
use syntax::parse::token::{InternedString, special_idents};
|
|
|
|
use syntax::parse::token;
|
2013-05-21 14:25:44 -05:00
|
|
|
use syntax::{ast};
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::{attr, ast_map};
|
2014-07-23 13:56:36 -05:00
|
|
|
use util::ppaux::Repr;
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Type definitions
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
struct ForeignTypes<'tcx> {
|
2013-05-21 14:25:44 -05:00
|
|
|
/// Rust signature of the function
|
2014-09-29 14:11:30 -05:00
|
|
|
fn_sig: ty::FnSig<'tcx>,
|
2013-03-08 19:44:37 -06:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
/// Adapter object for handling native ABI rules (trust me, you
|
|
|
|
/// don't want to know)
|
|
|
|
fn_ty: cabi::FnType,
|
|
|
|
|
2013-03-08 19:44:37 -06:00
|
|
|
/// LLVM types that will appear on the foreign function
|
|
|
|
llsig: LlvmSignature,
|
2013-02-19 01:40:42 -06:00
|
|
|
}
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2013-03-08 19:44:37 -06:00
|
|
|
struct LlvmSignature {
|
2013-05-21 14:25:44 -05:00
|
|
|
// LLVM versions of the types of this function's arguments.
|
2014-03-04 12:02:49 -06:00
|
|
|
llarg_tys: Vec<Type> ,
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// LLVM version of the type that this function returns. Note that
|
|
|
|
// this *may not be* the declared return type of the foreign
|
|
|
|
// function, because the foreign function may opt to return via an
|
|
|
|
// out pointer.
|
|
|
|
llret_ty: Type,
|
2014-10-24 14:14:37 -05:00
|
|
|
|
|
|
|
/// True if there is a return value (not bottom, not unit)
|
|
|
|
ret_def: bool,
|
2013-03-08 19:44:37 -06:00
|
|
|
}
|
|
|
|
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Calls to external functions
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
pub fn llvm_calling_convention(ccx: &CrateContext,
|
2014-07-23 13:56:36 -05:00
|
|
|
abi: Abi) -> CallConv {
|
|
|
|
match ccx.sess().target.target.adjust_abi(abi) {
|
|
|
|
RustIntrinsic => {
|
|
|
|
// Intrinsics are emitted at the call site
|
|
|
|
ccx.sess().bug("asked to register intrinsic fn");
|
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2014-07-23 13:56:36 -05:00
|
|
|
Rust => {
|
|
|
|
// FIXME(#3678) Implement linking to foreign fns with Rust ABI
|
|
|
|
ccx.sess().unimpl("foreign functions with Rust ABI");
|
|
|
|
}
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2014-07-23 13:56:36 -05:00
|
|
|
RustCall => {
|
|
|
|
// FIXME(#3678) Implement linking to foreign fns with Rust ABI
|
|
|
|
ccx.sess().unimpl("foreign functions with RustCall ABI");
|
|
|
|
}
|
2014-05-29 00:26:56 -05:00
|
|
|
|
2014-07-23 13:56:36 -05:00
|
|
|
// It's the ABI's job to select this, not us.
|
|
|
|
System => ccx.sess().bug("system abi should be selected elsewhere"),
|
2013-11-08 13:06:57 -06:00
|
|
|
|
2014-07-23 13:56:36 -05:00
|
|
|
Stdcall => llvm::X86StdcallCallConv,
|
|
|
|
Fastcall => llvm::X86FastcallCallConv,
|
|
|
|
C => llvm::CCallConv,
|
|
|
|
Win64 => llvm::X86_64_Win64,
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2014-07-23 13:56:36 -05:00
|
|
|
// These API constants ought to be more specific...
|
|
|
|
Cdecl => llvm::CCallConv,
|
|
|
|
Aapcs => llvm::CCallConv,
|
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2014-03-06 10:47:24 -06:00
|
|
|
pub fn register_static(ccx: &CrateContext,
|
|
|
|
foreign_item: &ast::ForeignItem) -> ValueRef {
|
2014-03-15 15:29:34 -05:00
|
|
|
let ty = ty::node_id_to_type(ccx.tcx(), foreign_item.id);
|
2014-02-25 18:15:10 -06:00
|
|
|
let llty = type_of::type_of(ccx, ty);
|
|
|
|
|
|
|
|
let ident = link_name(foreign_item);
|
|
|
|
match attr::first_attr_value_str_by_name(foreign_item.attrs.as_slice(),
|
|
|
|
"linkage") {
|
|
|
|
// If this is a static with a linkage specified, then we need to handle
|
|
|
|
// it a little specially. The typesystem prevents things like &T and
|
|
|
|
// extern "C" fn() from being non-null, so we can't just declare a
|
|
|
|
// static and call it a day. Some linkages (like weak) will make it such
|
|
|
|
// that the static actually has a null value.
|
|
|
|
Some(name) => {
|
|
|
|
let linkage = match llvm_linkage_by_name(name.get()) {
|
|
|
|
Some(linkage) => linkage,
|
|
|
|
None => {
|
2014-03-06 10:47:24 -06:00
|
|
|
ccx.sess().span_fatal(foreign_item.span,
|
|
|
|
"invalid linkage specified");
|
2014-02-25 18:15:10 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let llty2 = match ty::get(ty).sty {
|
|
|
|
ty::ty_ptr(ref mt) => type_of::type_of(ccx, mt.ty),
|
|
|
|
_ => {
|
2014-03-06 10:47:24 -06:00
|
|
|
ccx.sess().span_fatal(foreign_item.span,
|
|
|
|
"must have type `*T` or `*mut T`");
|
2014-02-25 18:15:10 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
unsafe {
|
2014-07-21 18:42:34 -05:00
|
|
|
// Declare a symbol `foo` with the desired linkage.
|
2014-02-25 18:15:10 -06:00
|
|
|
let g1 = ident.get().with_c_str(|buf| {
|
2014-09-05 11:18:53 -05:00
|
|
|
llvm::LLVMAddGlobal(ccx.llmod(), llty2.to_ref(), buf)
|
2014-02-25 18:15:10 -06:00
|
|
|
});
|
2014-07-07 19:58:01 -05:00
|
|
|
llvm::SetLinkage(g1, linkage);
|
2014-02-25 18:15:10 -06:00
|
|
|
|
2014-07-21 18:42:34 -05:00
|
|
|
// Declare an internal global `extern_with_linkage_foo` which
|
|
|
|
// is initialized with the address of `foo`. If `foo` is
|
|
|
|
// discarded during linking (for example, if `foo` has weak
|
|
|
|
// linkage and there are no definitions), then
|
|
|
|
// `extern_with_linkage_foo` will instead be initialized to
|
|
|
|
// zero.
|
2014-05-25 05:17:19 -05:00
|
|
|
let mut real_name = "_rust_extern_with_linkage_".to_string();
|
2014-05-20 01:19:56 -05:00
|
|
|
real_name.push_str(ident.get());
|
2014-02-25 18:15:10 -06:00
|
|
|
let g2 = real_name.with_c_str(|buf| {
|
2014-09-05 11:18:53 -05:00
|
|
|
llvm::LLVMAddGlobal(ccx.llmod(), llty.to_ref(), buf)
|
2014-02-25 18:15:10 -06:00
|
|
|
});
|
2014-07-07 19:58:01 -05:00
|
|
|
llvm::SetLinkage(g2, llvm::InternalLinkage);
|
2014-02-25 18:15:10 -06:00
|
|
|
llvm::LLVMSetInitializer(g2, g1);
|
|
|
|
g2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => unsafe {
|
2014-07-21 18:42:34 -05:00
|
|
|
// Generate an external declaration.
|
2014-02-25 18:15:10 -06:00
|
|
|
ident.get().with_c_str(|buf| {
|
2014-09-05 11:18:53 -05:00
|
|
|
llvm::LLVMAddGlobal(ccx.llmod(), llty.to_ref(), buf)
|
2014-02-25 18:15:10 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-16 06:11:17 -05:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn register_foreign_item_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
abi: Abi, fty: Ty<'tcx>,
|
|
|
|
name: &str) -> ValueRef {
|
2013-05-21 14:25:44 -05:00
|
|
|
/*!
|
|
|
|
* Registers a foreign function found in a library.
|
|
|
|
* Just adds a LLVM global.
|
|
|
|
*/
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2014-04-02 03:19:41 -05:00
|
|
|
debug!("register_foreign_item_fn(abi={}, \
|
2014-05-14 01:07:33 -05:00
|
|
|
ty={}, \
|
|
|
|
name={})",
|
2014-04-02 03:19:41 -05:00
|
|
|
abi.repr(ccx.tcx()),
|
2014-05-14 01:07:33 -05:00
|
|
|
fty.repr(ccx.tcx()),
|
|
|
|
name);
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2014-07-23 13:56:36 -05:00
|
|
|
let cc = llvm_calling_convention(ccx, abi);
|
2013-03-08 19:44:37 -06:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// Register the function as a C extern fn
|
2014-05-14 01:07:33 -05:00
|
|
|
let tys = foreign_types_for_fn_ty(ccx, fty);
|
2013-03-08 19:44:37 -06:00
|
|
|
|
2013-10-25 00:56:34 -05:00
|
|
|
// Make sure the calling convention is right for variadic functions
|
|
|
|
// (should've been caught if not in typeck)
|
|
|
|
if tys.fn_sig.variadic {
|
2014-07-07 19:58:01 -05:00
|
|
|
assert!(cc == llvm::CCallConv);
|
2013-10-25 00:56:34 -05:00
|
|
|
}
|
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// Create the LLVM value for the C extern fn
|
2014-03-15 15:29:34 -05:00
|
|
|
let llfn_ty = lltype_for_fn_from_foreign_types(ccx, &tys);
|
2013-12-18 20:24:34 -06:00
|
|
|
|
2014-05-21 14:07:48 -05:00
|
|
|
let llfn = base::get_extern_fn(ccx,
|
2014-09-05 11:18:53 -05:00
|
|
|
&mut *ccx.externs().borrow_mut(),
|
2014-05-14 01:07:33 -05:00
|
|
|
name,
|
2014-01-10 16:02:36 -06:00
|
|
|
cc,
|
|
|
|
llfn_ty,
|
2014-05-21 14:07:48 -05:00
|
|
|
fty);
|
2013-05-21 14:25:44 -05:00
|
|
|
add_argument_attributes(&tys, llfn);
|
2013-03-13 21:25:28 -05:00
|
|
|
|
2014-02-13 23:07:09 -06:00
|
|
|
llfn
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn trans_native_call<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
2014-09-29 14:11:30 -05:00
|
|
|
callee_ty: Ty<'tcx>,
|
2014-09-06 11:13:04 -05:00
|
|
|
llfn: ValueRef,
|
|
|
|
llretptr: ValueRef,
|
|
|
|
llargs_rust: &[ValueRef],
|
2014-09-29 14:11:30 -05:00
|
|
|
passed_arg_tys: Vec<Ty<'tcx>>)
|
2014-09-06 11:13:04 -05:00
|
|
|
-> Block<'blk, 'tcx> {
|
2013-05-21 14:25:44 -05:00
|
|
|
/*!
|
|
|
|
* Prepares a call to a native function. This requires adapting
|
|
|
|
* from the Rust argument passing rules to the native rules.
|
|
|
|
*
|
|
|
|
* # Parameters
|
|
|
|
*
|
|
|
|
* - `callee_ty`: Rust type for the function we are calling
|
|
|
|
* - `llfn`: the function pointer we are calling
|
|
|
|
* - `llretptr`: where to store the return value of the function
|
|
|
|
* - `llargs_rust`: a list of the argument values, prepared
|
|
|
|
* as they would be if calling a Rust function
|
2013-10-25 00:56:34 -05:00
|
|
|
* - `passed_arg_tys`: Rust type for the arguments. Normally we
|
|
|
|
* can derive these from callee_ty but in the case of variadic
|
|
|
|
* functions passed_arg_tys will include the Rust type of all
|
|
|
|
* the arguments including the ones not specified in the fn's signature.
|
2013-05-21 14:25:44 -05:00
|
|
|
*/
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let tcx = bcx.tcx();
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("trans_native_call(callee_ty={}, \
|
2013-09-28 00:38:08 -05:00
|
|
|
llfn={}, \
|
|
|
|
llretptr={})",
|
2013-05-21 14:25:44 -05:00
|
|
|
callee_ty.repr(tcx),
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().val_to_string(llfn),
|
|
|
|
ccx.tn().val_to_string(llretptr));
|
2012-03-22 07:44:16 -05:00
|
|
|
|
2014-04-02 03:19:41 -05:00
|
|
|
let (fn_abi, fn_sig) = match ty::get(callee_ty).sty {
|
|
|
|
ty::ty_bare_fn(ref fn_ty) => (fn_ty.abi, fn_ty.sig.clone()),
|
2014-03-05 08:36:01 -06:00
|
|
|
_ => ccx.sess().bug("trans_native_call called on non-function type")
|
2013-05-21 14:25:44 -05:00
|
|
|
};
|
2014-03-08 14:36:22 -06:00
|
|
|
let llsig = foreign_signature(ccx, &fn_sig, passed_arg_tys.as_slice());
|
2013-05-21 14:25:44 -05:00
|
|
|
let fn_type = cabi::compute_abi_info(ccx,
|
2014-03-08 14:36:22 -06:00
|
|
|
llsig.llarg_tys.as_slice(),
|
2013-05-21 14:25:44 -05:00
|
|
|
llsig.llret_ty,
|
2014-10-24 14:14:37 -05:00
|
|
|
llsig.ret_def);
|
2013-05-21 14:25:44 -05:00
|
|
|
|
2014-03-08 14:36:22 -06:00
|
|
|
let arg_tys: &[cabi::ArgType] = fn_type.arg_tys.as_slice();
|
2013-05-21 14:25:44 -05:00
|
|
|
|
2014-03-04 12:02:49 -06:00
|
|
|
let mut llargs_foreign = Vec::new();
|
2013-05-21 14:25:44 -05:00
|
|
|
|
|
|
|
// If the foreign ABI expects return value by pointer, supply the
|
|
|
|
// pointer that Rust gave us. Sometimes we have to bitcast
|
|
|
|
// because foreign fns return slightly different (but equivalent)
|
|
|
|
// views on the same type (e.g., i64 in place of {i32,i32}).
|
2013-09-25 05:30:44 -05:00
|
|
|
if fn_type.ret_ty.is_indirect() {
|
|
|
|
match fn_type.ret_ty.cast {
|
|
|
|
Some(ty) => {
|
2013-05-21 14:25:44 -05:00
|
|
|
let llcastedretptr =
|
2013-09-25 05:30:44 -05:00
|
|
|
BitCast(bcx, llretptr, ty.ptr_to());
|
2013-05-21 14:25:44 -05:00
|
|
|
llargs_foreign.push(llcastedretptr);
|
2013-09-25 05:30:44 -05:00
|
|
|
}
|
|
|
|
None => {
|
2013-05-21 14:25:44 -05:00
|
|
|
llargs_foreign.push(llretptr);
|
|
|
|
}
|
2013-03-29 18:55:04 -05:00
|
|
|
}
|
2013-09-25 05:30:44 -05:00
|
|
|
}
|
2013-03-29 18:55:04 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
for (i, &llarg_rust) in llargs_rust.iter().enumerate() {
|
|
|
|
let mut llarg_rust = llarg_rust;
|
2012-03-22 07:44:16 -05:00
|
|
|
|
2014-03-09 00:42:22 -06:00
|
|
|
if arg_tys[i].is_ignore() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// Does Rust pass this argument by pointer?
|
2014-10-15 01:05:01 -05:00
|
|
|
let rust_indirect = type_of::arg_is_indirect(ccx, passed_arg_tys[i]);
|
2013-03-08 19:44:37 -06:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("argument {}, llarg_rust={}, rust_indirect={}, arg_ty={}",
|
2013-05-21 14:25:44 -05:00
|
|
|
i,
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().val_to_string(llarg_rust),
|
2013-05-21 14:25:44 -05:00
|
|
|
rust_indirect,
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().type_to_string(arg_tys[i].ty));
|
2013-06-20 09:42:44 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// Ensure that we always have the Rust value indirectly,
|
|
|
|
// because it makes bitcasting easier.
|
|
|
|
if !rust_indirect {
|
2014-03-08 14:36:22 -06:00
|
|
|
let scratch =
|
|
|
|
base::alloca(bcx,
|
2014-10-15 01:05:01 -05:00
|
|
|
type_of::type_of(ccx, passed_arg_tys[i]),
|
2014-03-08 14:36:22 -06:00
|
|
|
"__arg");
|
2014-10-15 01:05:01 -05:00
|
|
|
base::store_ty(bcx, llarg_rust, scratch, passed_arg_tys[i]);
|
2013-05-21 14:25:44 -05:00
|
|
|
llarg_rust = scratch;
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("llarg_rust={} (after indirection)",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().val_to_string(llarg_rust));
|
2012-02-13 17:28:00 -06:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// Check whether we need to do any casting
|
2013-09-25 05:30:44 -05:00
|
|
|
match arg_tys[i].cast {
|
|
|
|
Some(ty) => llarg_rust = BitCast(bcx, llarg_rust, ty.ptr_to()),
|
|
|
|
None => ()
|
2013-07-11 12:31:38 -05:00
|
|
|
}
|
2013-08-07 14:40:09 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("llarg_rust={} (after casting)",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().val_to_string(llarg_rust));
|
2013-07-11 12:31:38 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// Finally, load the value if needed for the foreign ABI
|
2013-09-25 05:30:44 -05:00
|
|
|
let foreign_indirect = arg_tys[i].is_indirect();
|
2013-05-21 14:25:44 -05:00
|
|
|
let llarg_foreign = if foreign_indirect {
|
|
|
|
llarg_rust
|
|
|
|
} else {
|
2014-10-15 01:05:01 -05:00
|
|
|
if ty::type_is_bool(passed_arg_tys[i]) {
|
2014-07-07 19:58:01 -05:00
|
|
|
let val = LoadRangeAssert(bcx, llarg_rust, 0, 2, llvm::False);
|
2014-07-05 14:47:14 -05:00
|
|
|
Trunc(bcx, val, Type::i1(bcx.ccx()))
|
|
|
|
} else {
|
|
|
|
Load(bcx, llarg_rust)
|
|
|
|
}
|
2013-07-11 12:31:38 -05:00
|
|
|
};
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("argument {}, llarg_foreign={}",
|
2014-09-05 11:18:53 -05:00
|
|
|
i, ccx.tn().val_to_string(llarg_foreign));
|
2013-07-11 12:31:38 -05:00
|
|
|
|
2013-09-25 05:30:44 -05:00
|
|
|
// fill padding with undef value
|
|
|
|
match arg_tys[i].pad {
|
|
|
|
Some(ty) => llargs_foreign.push(C_undef(ty)),
|
|
|
|
None => ()
|
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
llargs_foreign.push(llarg_foreign);
|
2013-07-11 12:31:38 -05:00
|
|
|
}
|
|
|
|
|
2014-07-23 13:56:36 -05:00
|
|
|
let cc = llvm_calling_convention(ccx, fn_abi);
|
Generate better code for intrinsics
Currently, our intrinsics are generated as functions that have the
usual setup, which means an alloca, and therefore also a jump, for
those intrinsics that return an immediate value. This is especially bad
for unoptimized builds because it means that an intrinsic like
"contains_managed" that should be just "ret 0" or "ret 1" actually ends
up allocating stack space, doing a jump and a store/load sequence
before it finally returns the value.
To fix that, we need a way to stop the generic function declaration
mechanism from allocating stack space for the return value. This
implicitly also kills the jump, because the block for static allocas
isn't required anymore.
Additionally, trans_intrinsic needs to build the return itself instead
of calling finish_fn, because the latter relies on the availability of
the return value pointer.
With these changes, we get the bare minimum code required for our
intrinsics, which makes them small enough that inlining them makes the
resulting code smaller, so we can mark them as "always inline" to get
better performing unoptimized builds.
Optimized builds also benefit slightly from this change as there's less
code for LLVM to translate and the smaller intrinsics help it to make
better inlining decisions for a few code paths.
Building stage2 librustc gets ~1% faster for the optimized version and 5% for
the unoptimized version.
2013-07-16 12:25:06 -05:00
|
|
|
|
2013-09-12 23:48:49 -05:00
|
|
|
// A function pointer is called without the declaration available, so we have to apply
|
2014-06-17 14:51:24 -05:00
|
|
|
// any attributes with ABI implications directly to the call instruction.
|
2014-07-25 18:06:44 -05:00
|
|
|
let mut attrs = llvm::AttrBuilder::new();
|
2014-05-21 14:07:48 -05:00
|
|
|
|
2014-06-17 14:51:24 -05:00
|
|
|
// Add attributes that are always applicable, independent of the concrete foreign ABI
|
|
|
|
if fn_type.ret_ty.is_indirect() {
|
2014-07-25 20:33:10 -05:00
|
|
|
let llret_sz = machine::llsize_of_real(ccx, fn_type.ret_ty.ty);
|
|
|
|
|
2014-05-21 14:07:48 -05:00
|
|
|
// The outptr can be noalias and nocapture because it's entirely
|
2014-07-25 20:33:10 -05:00
|
|
|
// invisible to the program. We also know it's nonnull as well
|
|
|
|
// as how many bytes we can dereference
|
2014-07-25 18:06:44 -05:00
|
|
|
attrs.arg(1, llvm::NoAliasAttribute)
|
|
|
|
.arg(1, llvm::NoCaptureAttribute)
|
2014-07-25 20:33:10 -05:00
|
|
|
.arg(1, llvm::DereferenceableAttribute(llret_sz));
|
2014-01-15 13:39:08 -06:00
|
|
|
};
|
2014-06-17 14:51:24 -05:00
|
|
|
|
|
|
|
// Add attributes that depend on the concrete foreign ABI
|
|
|
|
let mut arg_idx = if fn_type.ret_ty.is_indirect() { 1 } else { 0 };
|
|
|
|
match fn_type.ret_ty.attr {
|
2014-07-25 18:06:44 -05:00
|
|
|
Some(attr) => { attrs.arg(arg_idx, attr); },
|
2014-06-17 14:51:24 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
|
|
|
arg_idx += 1;
|
|
|
|
for arg_ty in fn_type.arg_tys.iter() {
|
|
|
|
if arg_ty.is_ignore() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// skip padding
|
|
|
|
if arg_ty.pad.is_some() { arg_idx += 1; }
|
|
|
|
|
|
|
|
match arg_ty.attr {
|
2014-07-25 18:06:44 -05:00
|
|
|
Some(attr) => { attrs.arg(arg_idx, attr); },
|
2014-06-17 14:51:24 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
arg_idx += 1;
|
|
|
|
}
|
|
|
|
|
2014-03-08 14:36:22 -06:00
|
|
|
let llforeign_retval = CallWithConv(bcx,
|
|
|
|
llfn,
|
|
|
|
llargs_foreign.as_slice(),
|
|
|
|
cc,
|
2014-07-25 18:06:44 -05:00
|
|
|
Some(attrs));
|
2013-04-20 06:58:56 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// If the function we just called does not use an outpointer,
|
|
|
|
// store the result into the rust outpointer. Cast the outpointer
|
|
|
|
// type to match because some ABIs will use a different type than
|
|
|
|
// the Rust type. e.g., a {u32,u32} struct could be returned as
|
|
|
|
// u64.
|
2014-10-24 14:14:37 -05:00
|
|
|
if llsig.ret_def && !fn_type.ret_ty.is_indirect() {
|
2013-05-21 14:25:44 -05:00
|
|
|
let llrust_ret_ty = llsig.llret_ty;
|
2013-09-25 05:30:44 -05:00
|
|
|
let llforeign_ret_ty = match fn_type.ret_ty.cast {
|
|
|
|
Some(ty) => ty,
|
|
|
|
None => fn_type.ret_ty.ty
|
|
|
|
};
|
2013-06-15 18:58:51 -05:00
|
|
|
|
2014-09-05 11:18:53 -05:00
|
|
|
debug!("llretptr={}", ccx.tn().val_to_string(llretptr));
|
|
|
|
debug!("llforeign_retval={}", ccx.tn().val_to_string(llforeign_retval));
|
|
|
|
debug!("llrust_ret_ty={}", ccx.tn().type_to_string(llrust_ret_ty));
|
|
|
|
debug!("llforeign_ret_ty={}", ccx.tn().type_to_string(llforeign_ret_ty));
|
2013-06-15 18:58:51 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
if llrust_ret_ty == llforeign_ret_ty {
|
2014-10-24 14:14:37 -05:00
|
|
|
match fn_sig.output {
|
|
|
|
ty::FnConverging(result_ty) => {
|
|
|
|
base::store_ty(bcx, llforeign_retval, llretptr, result_ty)
|
|
|
|
}
|
|
|
|
ty::FnDiverging => {}
|
|
|
|
}
|
2013-06-15 18:58:51 -05:00
|
|
|
} else {
|
2013-05-21 14:25:44 -05:00
|
|
|
// The actual return type is a struct, but the ABI
|
|
|
|
// adaptation code has cast it into some scalar type. The
|
|
|
|
// code that follows is the only reliable way I have
|
|
|
|
// found to do a transform like i64 -> {i32,i32}.
|
|
|
|
// Basically we dump the data onto the stack then memcpy it.
|
|
|
|
//
|
|
|
|
// Other approaches I tried:
|
|
|
|
// - Casting rust ret pointer to the foreign type and using Store
|
|
|
|
// is (a) unsafe if size of foreign type > size of rust type and
|
|
|
|
// (b) runs afoul of strict aliasing rules, yielding invalid
|
|
|
|
// assembly under -O (specifically, the store gets removed).
|
|
|
|
// - Truncating foreign type to correct integral type and then
|
|
|
|
// bitcasting to the struct type yields invalid cast errors.
|
|
|
|
let llscratch = base::alloca(bcx, llforeign_ret_ty, "__cast");
|
|
|
|
Store(bcx, llforeign_retval, llscratch);
|
2014-03-15 15:29:34 -05:00
|
|
|
let llscratch_i8 = BitCast(bcx, llscratch, Type::i8(ccx).ptr_to());
|
|
|
|
let llretptr_i8 = BitCast(bcx, llretptr, Type::i8(ccx).ptr_to());
|
2013-05-21 14:25:44 -05:00
|
|
|
let llrust_size = machine::llsize_of_store(ccx, llrust_ret_ty);
|
|
|
|
let llforeign_align = machine::llalign_of_min(ccx, llforeign_ret_ty);
|
|
|
|
let llrust_align = machine::llalign_of_min(ccx, llrust_ret_ty);
|
2013-11-29 13:19:22 -06:00
|
|
|
let llalign = cmp::min(llforeign_align, llrust_align);
|
2014-10-15 01:25:34 -05:00
|
|
|
debug!("llrust_size={}", llrust_size);
|
2013-05-21 14:25:44 -05:00
|
|
|
base::call_memcpy(bcx, llretptr_i8, llscratch_i8,
|
2014-10-14 15:36:11 -05:00
|
|
|
C_uint(ccx, llrust_size), llalign as u32);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-06-15 18:58:51 -05:00
|
|
|
}
|
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
2013-04-22 18:22:36 -05:00
|
|
|
|
2014-03-06 10:47:24 -06:00
|
|
|
pub fn trans_foreign_mod(ccx: &CrateContext, foreign_mod: &ast::ForeignMod) {
|
2013-05-21 14:25:44 -05:00
|
|
|
let _icx = push_ctxt("foreign::trans_foreign_mod");
|
2014-05-16 12:15:33 -05:00
|
|
|
for foreign_item in foreign_mod.items.iter() {
|
|
|
|
let lname = link_name(&**foreign_item);
|
2014-05-14 01:07:33 -05:00
|
|
|
|
2013-10-25 00:56:34 -05:00
|
|
|
match foreign_item.node {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ForeignItemFn(..) => {
|
2014-04-02 03:19:41 -05:00
|
|
|
match foreign_mod.abi {
|
|
|
|
Rust | RustIntrinsic => {}
|
2014-05-14 01:07:33 -05:00
|
|
|
abi => {
|
|
|
|
let ty = ty::node_id_to_type(ccx.tcx(), foreign_item.id);
|
|
|
|
register_foreign_item_fn(ccx, abi, ty,
|
2014-07-23 13:56:36 -05:00
|
|
|
lname.get().as_slice());
|
2014-07-21 18:42:34 -05:00
|
|
|
// Unlike for other items, we shouldn't call
|
|
|
|
// `base::update_linkage` here. Foreign items have
|
|
|
|
// special linkage requirements, which are handled
|
|
|
|
// inside `foreign::register_*`.
|
2014-05-14 01:07:33 -05:00
|
|
|
}
|
2013-10-25 00:56:34 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-13 23:07:09 -06:00
|
|
|
_ => {}
|
2013-10-25 00:56:34 -05:00
|
|
|
}
|
|
|
|
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.item_symbols().borrow_mut().insert(foreign_item.id,
|
2014-05-25 05:17:19 -05:00
|
|
|
lname.get().to_string());
|
2012-03-21 09:42:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Rust functions with foreign ABIs
|
|
|
|
//
|
|
|
|
// These are normal Rust functions defined with foreign ABIs. For
|
|
|
|
// now, and perhaps forever, we translate these using a "layer of
|
|
|
|
// indirection". That is, given a Rust declaration like:
|
|
|
|
//
|
|
|
|
// extern "C" fn foo(i: u32) -> u32 { ... }
|
|
|
|
//
|
|
|
|
// we will generate a function like:
|
|
|
|
//
|
|
|
|
// S foo(T i) {
|
|
|
|
// S r;
|
|
|
|
// foo0(&r, NULL, i);
|
|
|
|
// return r;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[inline_always]
|
|
|
|
// void foo0(uint32_t *r, void *env, uint32_t i) { ... }
|
|
|
|
//
|
|
|
|
// Here the (internal) `foo0` function follows the Rust ABI as normal,
|
|
|
|
// where the `foo` function follows the C ABI. We rely on LLVM to
|
|
|
|
// inline the one into the other. Of course we could just generate the
|
|
|
|
// correct code in the first place, but this is much simpler.
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn decl_rust_fn_with_foreign_abi<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
t: Ty<'tcx>,
|
|
|
|
name: &str)
|
|
|
|
-> ValueRef {
|
2014-08-01 23:25:41 -05:00
|
|
|
let tys = foreign_types_for_fn_ty(ccx, t);
|
|
|
|
let llfn_ty = lltype_for_fn_from_foreign_types(ccx, &tys);
|
|
|
|
let cconv = match ty::get(t).sty {
|
|
|
|
ty::ty_bare_fn(ref fn_ty) => {
|
2014-07-23 13:56:36 -05:00
|
|
|
llvm_calling_convention(ccx, fn_ty.abi)
|
2014-08-01 23:25:41 -05:00
|
|
|
}
|
2014-10-09 14:17:22 -05:00
|
|
|
_ => panic!("expected bare fn in decl_rust_fn_with_foreign_abi")
|
2014-08-01 23:25:41 -05:00
|
|
|
};
|
2014-11-04 06:57:21 -06:00
|
|
|
let llfn = base::decl_fn(ccx, name, cconv, llfn_ty, ty::FnConverging(ty::mk_nil(ccx.tcx())));
|
2014-08-01 23:25:41 -05:00
|
|
|
add_argument_attributes(&tys, llfn);
|
|
|
|
debug!("decl_rust_fn_with_foreign_abi(llfn_ty={}, llfn={})",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().type_to_string(llfn_ty), ccx.tn().val_to_string(llfn));
|
2014-08-01 23:25:41 -05:00
|
|
|
llfn
|
|
|
|
}
|
|
|
|
|
2014-03-06 10:47:24 -06:00
|
|
|
pub fn register_rust_fn_with_foreign_abi(ccx: &CrateContext,
|
2013-08-31 11:13:04 -05:00
|
|
|
sp: Span,
|
2014-05-22 18:57:53 -05:00
|
|
|
sym: String,
|
2013-05-21 14:25:44 -05:00
|
|
|
node_id: ast::NodeId)
|
|
|
|
-> ValueRef {
|
|
|
|
let _icx = push_ctxt("foreign::register_foreign_fn");
|
|
|
|
|
|
|
|
let tys = foreign_types_for_id(ccx, node_id);
|
2014-03-15 15:29:34 -05:00
|
|
|
let llfn_ty = lltype_for_fn_from_foreign_types(ccx, &tys);
|
|
|
|
let t = ty::node_id_to_type(ccx.tcx(), node_id);
|
2014-05-21 14:07:48 -05:00
|
|
|
let cconv = match ty::get(t).sty {
|
2013-11-16 18:59:08 -06:00
|
|
|
ty::ty_bare_fn(ref fn_ty) => {
|
2014-07-23 13:56:36 -05:00
|
|
|
llvm_calling_convention(ccx, fn_ty.abi)
|
2013-11-16 18:59:08 -06:00
|
|
|
}
|
2014-10-09 14:17:22 -05:00
|
|
|
_ => panic!("expected bare fn in register_rust_fn_with_foreign_abi")
|
2013-11-16 18:59:08 -06:00
|
|
|
};
|
2014-05-21 14:07:48 -05:00
|
|
|
let llfn = base::register_fn_llvmty(ccx, sp, sym, node_id, cconv, llfn_ty);
|
2013-05-21 14:25:44 -05:00
|
|
|
add_argument_attributes(&tys, llfn);
|
2014-10-15 01:25:34 -05:00
|
|
|
debug!("register_rust_fn_with_foreign_abi(node_id={}, llfn_ty={}, llfn={})",
|
2014-09-05 11:18:53 -05:00
|
|
|
node_id, ccx.tn().type_to_string(llfn_ty), ccx.tn().val_to_string(llfn));
|
2013-05-21 14:25:44 -05:00
|
|
|
llfn
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn trans_rust_fn_with_foreign_abi<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
decl: &ast::FnDecl,
|
|
|
|
body: &ast::Block,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
llwrapfn: ValueRef,
|
2014-11-06 01:24:44 -06:00
|
|
|
param_substs: &Substs<'tcx>,
|
2014-09-29 14:11:30 -05:00
|
|
|
id: ast::NodeId,
|
|
|
|
hash: Option<&str>) {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("foreign::build_foreign_fn");
|
2014-08-01 23:25:41 -05:00
|
|
|
|
|
|
|
let fnty = ty::node_id_to_type(ccx.tcx(), id);
|
2014-11-06 01:24:44 -06:00
|
|
|
let mty = fnty.subst(ccx.tcx(), param_substs);
|
2014-08-01 23:25:41 -05:00
|
|
|
let tys = foreign_types_for_fn_ty(ccx, mty);
|
2013-05-21 14:25:44 -05:00
|
|
|
|
|
|
|
unsafe { // unsafe because we call LLVM operations
|
|
|
|
// Build up the Rust function (`foo0` above).
|
2014-08-11 18:54:16 -05:00
|
|
|
let llrustfn = build_rust_fn(ccx, decl, body, param_substs, attrs, id, hash);
|
2013-05-21 14:25:44 -05:00
|
|
|
|
|
|
|
// Build up the foreign wrapper (`foo` above).
|
2014-08-01 23:25:41 -05:00
|
|
|
return build_wrap_fn(ccx, llrustfn, llwrapfn, &tys, mty);
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn build_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
decl: &ast::FnDecl,
|
|
|
|
body: &ast::Block,
|
2014-11-06 01:24:44 -06:00
|
|
|
param_substs: &Substs<'tcx>,
|
2014-09-29 14:11:30 -05:00
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
id: ast::NodeId,
|
|
|
|
hash: Option<&str>)
|
|
|
|
-> ValueRef {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("foreign::foreign::build_rust_fn");
|
2014-03-15 15:29:34 -05:00
|
|
|
let tcx = ccx.tcx();
|
2014-11-06 01:24:44 -06:00
|
|
|
let t = ty::node_id_to_type(tcx, id).subst(ccx.tcx(), param_substs);
|
2014-02-13 23:07:09 -06:00
|
|
|
|
2014-09-05 11:18:53 -05:00
|
|
|
let ps = ccx.tcx().map.with_path(id, |path| {
|
2014-02-13 23:07:09 -06:00
|
|
|
let abi = Some(ast_map::PathName(special_idents::clownshoe_abi.name));
|
2014-09-14 22:27:36 -05:00
|
|
|
link::mangle(path.chain(abi.into_iter()), hash)
|
2014-02-13 23:07:09 -06:00
|
|
|
});
|
2013-08-19 20:28:31 -05:00
|
|
|
|
2013-09-10 17:42:01 -05:00
|
|
|
// Compute the type that the function would have if it were just a
|
|
|
|
// normal Rust function. This will be the type of the wrappee fn.
|
2014-05-21 14:07:48 -05:00
|
|
|
match ty::get(t).sty {
|
2013-08-19 20:28:31 -05:00
|
|
|
ty::ty_bare_fn(ref f) => {
|
2014-04-02 03:19:41 -05:00
|
|
|
assert!(f.abi != Rust && f.abi != RustIntrinsic);
|
2013-08-19 20:28:31 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2014-03-05 08:36:01 -06:00
|
|
|
ccx.sess().bug(format!("build_rust_fn: extern fn {} has ty {}, \
|
2014-05-16 12:45:16 -05:00
|
|
|
expected a bare fn ty",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tcx().map.path_to_string(id),
|
2014-05-16 12:45:16 -05:00
|
|
|
t.repr(tcx)).as_slice());
|
2013-08-19 20:28:31 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-13 23:07:09 -06:00
|
|
|
debug!("build_rust_fn: path={} id={} t={}",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tcx().map.path_to_string(id),
|
2014-02-13 23:07:09 -06:00
|
|
|
id, t.repr(tcx));
|
2013-08-19 20:28:31 -05:00
|
|
|
|
2014-05-21 14:07:48 -05:00
|
|
|
let llfn = base::decl_internal_rust_fn(ccx, t, ps.as_slice());
|
2014-09-05 19:28:24 -05:00
|
|
|
base::set_llvm_fn_attrs(ccx, attrs, llfn);
|
2014-11-17 02:39:01 -06:00
|
|
|
base::trans_fn(ccx, decl, body, llfn, param_substs, id, &[]);
|
2014-01-27 06:18:36 -06:00
|
|
|
llfn
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
unsafe fn build_wrap_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
llrustfn: ValueRef,
|
|
|
|
llwrapfn: ValueRef,
|
|
|
|
tys: &ForeignTypes<'tcx>,
|
|
|
|
t: Ty<'tcx>) {
|
2013-05-21 14:25:44 -05:00
|
|
|
let _icx = push_ctxt(
|
|
|
|
"foreign::trans_rust_fn_with_foreign_abi::build_wrap_fn");
|
2014-03-15 15:29:34 -05:00
|
|
|
let tcx = ccx.tcx();
|
2013-05-21 14:25:44 -05:00
|
|
|
|
2014-05-21 14:07:48 -05:00
|
|
|
debug!("build_wrap_fn(llrustfn={}, llwrapfn={}, t={})",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().val_to_string(llrustfn),
|
|
|
|
ccx.tn().val_to_string(llwrapfn),
|
2014-05-21 14:07:48 -05:00
|
|
|
t.repr(ccx.tcx()));
|
2013-05-21 14:25:44 -05:00
|
|
|
|
|
|
|
// Avoid all the Rust generation stuff and just generate raw
|
|
|
|
// LLVM here.
|
|
|
|
//
|
|
|
|
// We want to generate code like this:
|
|
|
|
//
|
|
|
|
// S foo(T i) {
|
|
|
|
// S r;
|
|
|
|
// foo0(&r, NULL, i);
|
|
|
|
// return r;
|
|
|
|
// }
|
|
|
|
|
|
|
|
let the_block =
|
2013-09-18 12:54:43 -05:00
|
|
|
"the block".with_c_str(
|
2014-09-05 11:18:53 -05:00
|
|
|
|s| llvm::LLVMAppendBasicBlockInContext(ccx.llcx(), llwrapfn, s));
|
2013-05-21 14:25:44 -05:00
|
|
|
|
2014-07-04 07:50:36 -05:00
|
|
|
let builder = ccx.builder();
|
|
|
|
builder.position_at_end(the_block);
|
2013-05-21 14:25:44 -05:00
|
|
|
|
|
|
|
// Array for the arguments we will pass to the rust function.
|
2014-03-04 12:02:49 -06:00
|
|
|
let mut llrust_args = Vec::new();
|
2013-05-21 14:25:44 -05:00
|
|
|
let mut next_foreign_arg_counter: c_uint = 0;
|
2013-11-19 15:22:03 -06:00
|
|
|
let next_foreign_arg: |pad: bool| -> c_uint = |pad: bool| {
|
|
|
|
next_foreign_arg_counter += if pad {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
|
|
|
next_foreign_arg_counter - 1
|
2013-05-21 14:25:44 -05:00
|
|
|
};
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// If there is an out pointer on the foreign function
|
|
|
|
let foreign_outptr = {
|
2013-09-25 05:30:44 -05:00
|
|
|
if tys.fn_ty.ret_ty.is_indirect() {
|
2014-05-29 00:26:56 -05:00
|
|
|
Some(get_param(llwrapfn, next_foreign_arg(false)))
|
2013-05-21 14:25:44 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
2013-03-08 19:44:37 -06:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// Push Rust return pointer, using null if it will be unused.
|
2014-10-24 14:14:37 -05:00
|
|
|
let rust_uses_outptr = match tys.fn_sig.output {
|
|
|
|
ty::FnConverging(ret_ty) => type_of::return_uses_outptr(ccx, ret_ty),
|
|
|
|
ty::FnDiverging => false
|
|
|
|
};
|
2013-05-21 14:25:44 -05:00
|
|
|
let return_alloca: Option<ValueRef>;
|
|
|
|
let llrust_ret_ty = tys.llsig.llret_ty;
|
|
|
|
let llrust_retptr_ty = llrust_ret_ty.ptr_to();
|
|
|
|
if rust_uses_outptr {
|
|
|
|
// Rust expects to use an outpointer. If the foreign fn
|
|
|
|
// also uses an outpointer, we can reuse it, but the types
|
|
|
|
// may vary, so cast first to the Rust type. If the
|
add sret + noalias to the out pointer parameter
This brings Rust in line with how `clang` handles return pointers.
Example:
pub fn bar() -> [uint, .. 8] {
let a = [0, .. 8];
a
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* nocapture, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
"function top level":
%a = alloca [8 x i64], align 8
%2 = bitcast [8 x i64]* %a to i8*
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
%3 = bitcast [8 x i64]* %0 to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 64, i32 8, i1 false)
ret void
}
After:
; Function Attrs: nounwind uwtable
define void @_ZN3bar17ha4635c6f704bfa334v0.0E([8 x i64]* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #1 {
"function top level":
%2 = bitcast [8 x i64]* %0 to i8*
call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 64, i32 8, i1 false)
ret void
}
Closes #9072
Closes #7298
Closes #9154
2013-09-10 13:28:59 -05:00
|
|
|
// foreign fn does NOT use an outpointer, we will have to
|
2013-05-21 14:25:44 -05:00
|
|
|
// alloca some scratch space on the stack.
|
|
|
|
match foreign_outptr {
|
|
|
|
Some(llforeign_outptr) => {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("out pointer, foreign={}",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().val_to_string(llforeign_outptr));
|
2013-05-21 14:25:44 -05:00
|
|
|
let llrust_retptr =
|
2014-10-24 14:14:37 -05:00
|
|
|
builder.bitcast(llforeign_outptr, llrust_retptr_ty);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("out pointer, foreign={} (casted)",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().val_to_string(llrust_retptr));
|
2013-05-21 14:25:44 -05:00
|
|
|
llrust_args.push(llrust_retptr);
|
|
|
|
return_alloca = None;
|
2013-03-08 19:44:37 -06:00
|
|
|
}
|
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
None => {
|
2014-07-04 07:50:36 -05:00
|
|
|
let slot = builder.alloca(llrust_ret_ty, "return_alloca");
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("out pointer, \
|
2013-09-28 00:38:08 -05:00
|
|
|
allocad={}, \
|
|
|
|
llrust_ret_ty={}, \
|
|
|
|
return_ty={}",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().val_to_string(slot),
|
|
|
|
ccx.tn().type_to_string(llrust_ret_ty),
|
2013-05-21 14:25:44 -05:00
|
|
|
tys.fn_sig.output.repr(tcx));
|
|
|
|
llrust_args.push(slot);
|
|
|
|
return_alloca = Some(slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Rust does not expect an outpointer. If the foreign fn
|
|
|
|
// does use an outpointer, then we will do a store of the
|
|
|
|
// value that the Rust fn returns.
|
|
|
|
return_alloca = None;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Build up the arguments to the call to the rust function.
|
|
|
|
// Careful to adapt for cases where the native convention uses
|
|
|
|
// a pointer and Rust does not or vice versa.
|
|
|
|
for i in range(0, tys.fn_sig.inputs.len()) {
|
2014-10-15 01:05:01 -05:00
|
|
|
let rust_ty = tys.fn_sig.inputs[i];
|
|
|
|
let llrust_ty = tys.llsig.llarg_tys[i];
|
2013-05-21 14:25:44 -05:00
|
|
|
let rust_indirect = type_of::arg_is_indirect(ccx, rust_ty);
|
2014-10-15 01:05:01 -05:00
|
|
|
let llforeign_arg_ty = tys.fn_ty.arg_tys[i];
|
2013-09-25 05:30:44 -05:00
|
|
|
let foreign_indirect = llforeign_arg_ty.is_indirect();
|
|
|
|
|
2014-08-13 17:08:44 -05:00
|
|
|
if llforeign_arg_ty.is_ignore() {
|
|
|
|
debug!("skipping ignored arg #{}", i);
|
|
|
|
llrust_args.push(C_undef(llrust_ty));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-09-25 05:30:44 -05:00
|
|
|
// skip padding
|
|
|
|
let foreign_index = next_foreign_arg(llforeign_arg_ty.pad.is_some());
|
2014-05-29 00:26:56 -05:00
|
|
|
let mut llforeign_arg = get_param(llwrapfn, foreign_index);
|
2013-05-21 14:25:44 -05:00
|
|
|
|
2014-05-28 11:24:28 -05:00
|
|
|
debug!("llforeign_arg {}{}: {}", "#",
|
2014-09-05 11:18:53 -05:00
|
|
|
i, ccx.tn().val_to_string(llforeign_arg));
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("rust_indirect = {}, foreign_indirect = {}",
|
2013-05-21 14:25:44 -05:00
|
|
|
rust_indirect, foreign_indirect);
|
|
|
|
|
|
|
|
// Ensure that the foreign argument is indirect (by
|
|
|
|
// pointer). It makes adapting types easier, since we can
|
|
|
|
// always just bitcast pointers.
|
|
|
|
if !foreign_indirect {
|
2014-07-05 14:47:14 -05:00
|
|
|
llforeign_arg = if ty::type_is_bool(rust_ty) {
|
|
|
|
let lltemp = builder.alloca(Type::bool(ccx), "");
|
|
|
|
builder.store(builder.zext(llforeign_arg, Type::bool(ccx)), lltemp);
|
|
|
|
lltemp
|
|
|
|
} else {
|
|
|
|
let lltemp = builder.alloca(val_ty(llforeign_arg), "");
|
|
|
|
builder.store(llforeign_arg, lltemp);
|
|
|
|
lltemp
|
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the types in the ABI and the Rust types don't match,
|
|
|
|
// bitcast the llforeign_arg pointer so it matches the types
|
|
|
|
// Rust expects.
|
2013-09-25 05:30:44 -05:00
|
|
|
if llforeign_arg_ty.cast.is_some() {
|
2013-05-21 14:25:44 -05:00
|
|
|
assert!(!foreign_indirect);
|
2014-07-04 07:50:36 -05:00
|
|
|
llforeign_arg = builder.bitcast(llforeign_arg, llrust_ty.ptr_to());
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
let llrust_arg = if rust_indirect {
|
|
|
|
llforeign_arg
|
2013-04-18 17:53:29 -05:00
|
|
|
} else {
|
2014-07-05 14:47:14 -05:00
|
|
|
if ty::type_is_bool(rust_ty) {
|
2014-07-07 19:58:01 -05:00
|
|
|
let tmp = builder.load_range_assert(llforeign_arg, 0, 2, llvm::False);
|
2014-07-05 14:47:14 -05:00
|
|
|
builder.trunc(tmp, Type::i1(ccx))
|
|
|
|
} else {
|
|
|
|
builder.load(llforeign_arg)
|
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
};
|
|
|
|
|
2014-05-28 11:24:28 -05:00
|
|
|
debug!("llrust_arg {}{}: {}", "#",
|
2014-09-05 11:18:53 -05:00
|
|
|
i, ccx.tn().val_to_string(llrust_arg));
|
2013-05-21 14:25:44 -05:00
|
|
|
llrust_args.push(llrust_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the call itself
|
2014-09-05 11:18:53 -05:00
|
|
|
debug!("calling llrustfn = {}, t = {}",
|
|
|
|
ccx.tn().val_to_string(llrustfn), t.repr(ccx.tcx()));
|
2014-05-21 14:07:48 -05:00
|
|
|
let attributes = base::get_fn_llvm_attributes(ccx, t);
|
2014-07-25 18:06:44 -05:00
|
|
|
let llrust_ret_val = builder.call(llrustfn, llrust_args.as_slice(), Some(attributes));
|
2014-05-21 14:07:48 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
// Get the return value where the foreign fn expects it.
|
2013-09-25 05:30:44 -05:00
|
|
|
let llforeign_ret_ty = match tys.fn_ty.ret_ty.cast {
|
|
|
|
Some(ty) => ty,
|
|
|
|
None => tys.fn_ty.ret_ty.ty
|
|
|
|
};
|
2013-05-21 14:25:44 -05:00
|
|
|
match foreign_outptr {
|
2014-10-24 14:14:37 -05:00
|
|
|
None if !tys.llsig.ret_def => {
|
2013-05-21 14:25:44 -05:00
|
|
|
// Function returns `()` or `bot`, which in Rust is the LLVM
|
|
|
|
// type "{}" but in foreign ABIs is "Void".
|
2014-07-04 07:50:36 -05:00
|
|
|
builder.ret_void();
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
None if rust_uses_outptr => {
|
|
|
|
// Rust uses an outpointer, but the foreign ABI does not. Load.
|
|
|
|
let llrust_outptr = return_alloca.unwrap();
|
|
|
|
let llforeign_outptr_casted =
|
2014-07-04 07:50:36 -05:00
|
|
|
builder.bitcast(llrust_outptr, llforeign_ret_ty.ptr_to());
|
|
|
|
let llforeign_retval = builder.load(llforeign_outptr_casted);
|
|
|
|
builder.ret(llforeign_retval);
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
None if llforeign_ret_ty != llrust_ret_ty => {
|
|
|
|
// Neither ABI uses an outpointer, but the types don't
|
|
|
|
// quite match. Must cast. Probably we should try and
|
|
|
|
// examine the types and use a concrete llvm cast, but
|
|
|
|
// right now we just use a temp memory location and
|
|
|
|
// bitcast the pointer, which is the same thing the
|
|
|
|
// old wrappers used to do.
|
2014-07-04 07:50:36 -05:00
|
|
|
let lltemp = builder.alloca(llforeign_ret_ty, "");
|
|
|
|
let lltemp_casted = builder.bitcast(lltemp, llrust_ret_ty.ptr_to());
|
|
|
|
builder.store(llrust_ret_val, lltemp_casted);
|
|
|
|
let llforeign_retval = builder.load(lltemp);
|
|
|
|
builder.ret(llforeign_retval);
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
None => {
|
|
|
|
// Neither ABI uses an outpointer, and the types
|
|
|
|
// match. Easy peasy.
|
2014-07-04 07:50:36 -05:00
|
|
|
builder.ret(llrust_ret_val);
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(llforeign_outptr) if !rust_uses_outptr => {
|
|
|
|
// Foreign ABI requires an out pointer, but Rust doesn't.
|
|
|
|
// Store Rust return value.
|
|
|
|
let llforeign_outptr_casted =
|
2014-07-04 07:50:36 -05:00
|
|
|
builder.bitcast(llforeign_outptr, llrust_retptr_ty);
|
|
|
|
builder.store(llrust_ret_val, llforeign_outptr_casted);
|
|
|
|
builder.ret_void();
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(_) => {
|
|
|
|
// Both ABIs use outpointers. Easy peasy.
|
2014-07-04 07:50:36 -05:00
|
|
|
builder.ret_void();
|
2013-04-18 17:53:29 -05:00
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// General ABI Support
|
|
|
|
//
|
|
|
|
// This code is kind of a confused mess and needs to be reworked given
|
|
|
|
// the massive simplifications that have occurred.
|
|
|
|
|
2014-03-06 10:47:24 -06:00
|
|
|
pub fn link_name(i: &ast::ForeignItem) -> InternedString {
|
2014-05-19 11:30:09 -05:00
|
|
|
match attr::first_attr_value_str_by_name(i.attrs.as_slice(), "link_name") {
|
2014-01-10 16:02:36 -06:00
|
|
|
Some(ln) => ln.clone(),
|
2014-05-19 11:30:09 -05:00
|
|
|
None => match weak_lang_items::link_name(i.attrs.as_slice()) {
|
|
|
|
Some(name) => name,
|
|
|
|
None => token::get_ident(i.ident),
|
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn foreign_signature<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
fn_sig: &ty::FnSig<'tcx>, arg_tys: &[Ty<'tcx>])
|
|
|
|
-> LlvmSignature {
|
2013-05-21 14:25:44 -05:00
|
|
|
/*!
|
|
|
|
* The ForeignSignature is the LLVM types of the arguments/return type
|
|
|
|
* of a function. Note that these LLVM types are not quite the same
|
|
|
|
* as the LLVM types would be for a native Rust function because foreign
|
|
|
|
* functions just plain ignore modes. They also don't pass aggregate
|
|
|
|
* values by pointer like we do.
|
|
|
|
*/
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2014-07-05 14:47:14 -05:00
|
|
|
let llarg_tys = arg_tys.iter().map(|&arg| arg_type_of(ccx, arg)).collect();
|
2014-10-24 14:14:37 -05:00
|
|
|
let (llret_ty, ret_def) = match fn_sig.output {
|
|
|
|
ty::FnConverging(ret_ty) =>
|
|
|
|
(type_of::arg_type_of(ccx, ret_ty), !return_type_is_void(ccx, ret_ty)),
|
|
|
|
ty::FnDiverging =>
|
|
|
|
(Type::nil(ccx), false)
|
|
|
|
};
|
2013-05-21 14:25:44 -05:00
|
|
|
LlvmSignature {
|
|
|
|
llarg_tys: llarg_tys,
|
2014-10-24 14:14:37 -05:00
|
|
|
llret_ty: llret_ty,
|
|
|
|
ret_def: ret_def
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
2012-02-13 18:06:56 -06:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn foreign_types_for_id<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
id: ast::NodeId) -> ForeignTypes<'tcx> {
|
2014-03-15 15:29:34 -05:00
|
|
|
foreign_types_for_fn_ty(ccx, ty::node_id_to_type(ccx.tcx(), id))
|
2012-02-13 18:06:56 -06:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn foreign_types_for_fn_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
ty: Ty<'tcx>) -> ForeignTypes<'tcx> {
|
2013-05-21 14:25:44 -05:00
|
|
|
let fn_sig = match ty::get(ty).sty {
|
|
|
|
ty::ty_bare_fn(ref fn_ty) => fn_ty.sig.clone(),
|
2014-03-05 08:36:01 -06:00
|
|
|
_ => ccx.sess().bug("foreign_types_for_fn_ty called on non-function type")
|
2013-05-21 14:25:44 -05:00
|
|
|
};
|
2014-03-08 14:36:22 -06:00
|
|
|
let llsig = foreign_signature(ccx, &fn_sig, fn_sig.inputs.as_slice());
|
2013-05-21 14:25:44 -05:00
|
|
|
let fn_ty = cabi::compute_abi_info(ccx,
|
2014-03-08 14:36:22 -06:00
|
|
|
llsig.llarg_tys.as_slice(),
|
2013-05-21 14:25:44 -05:00
|
|
|
llsig.llret_ty,
|
2014-10-24 14:14:37 -05:00
|
|
|
llsig.ret_def);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("foreign_types_for_fn_ty(\
|
2013-09-28 00:38:08 -05:00
|
|
|
ty={}, \
|
|
|
|
llsig={} -> {}, \
|
|
|
|
fn_ty={} -> {}, \
|
|
|
|
ret_def={}",
|
2014-03-15 15:29:34 -05:00
|
|
|
ty.repr(ccx.tcx()),
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().types_to_str(llsig.llarg_tys.as_slice()),
|
|
|
|
ccx.tn().type_to_string(llsig.llret_ty),
|
|
|
|
ccx.tn().types_to_str(fn_ty.arg_tys.iter().map(|t| t.ty).collect::<Vec<_>>().as_slice()),
|
|
|
|
ccx.tn().type_to_string(fn_ty.ret_ty.ty),
|
2014-10-24 14:14:37 -05:00
|
|
|
llsig.ret_def);
|
2013-05-21 14:25:44 -05:00
|
|
|
|
|
|
|
ForeignTypes {
|
|
|
|
fn_sig: fn_sig,
|
|
|
|
llsig: llsig,
|
|
|
|
fn_ty: fn_ty
|
|
|
|
}
|
|
|
|
}
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2014-03-15 15:29:34 -05:00
|
|
|
fn lltype_for_fn_from_foreign_types(ccx: &CrateContext, tys: &ForeignTypes) -> Type {
|
2014-03-04 12:02:49 -06:00
|
|
|
let mut llargument_tys = Vec::new();
|
2013-09-25 05:30:44 -05:00
|
|
|
|
|
|
|
let ret_ty = tys.fn_ty.ret_ty;
|
|
|
|
let llreturn_ty = if ret_ty.is_indirect() {
|
|
|
|
llargument_tys.push(ret_ty.ty.ptr_to());
|
2014-03-15 15:29:34 -05:00
|
|
|
Type::void(ccx)
|
2013-09-25 05:30:44 -05:00
|
|
|
} else {
|
|
|
|
match ret_ty.cast {
|
|
|
|
Some(ty) => ty,
|
|
|
|
None => ret_ty.ty
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for &arg_ty in tys.fn_ty.arg_tys.iter() {
|
2014-03-09 00:42:22 -06:00
|
|
|
if arg_ty.is_ignore() {
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-25 05:30:44 -05:00
|
|
|
// add padding
|
|
|
|
match arg_ty.pad {
|
|
|
|
Some(ty) => llargument_tys.push(ty),
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
|
|
|
|
let llarg_ty = if arg_ty.is_indirect() {
|
|
|
|
arg_ty.ty.ptr_to()
|
|
|
|
} else {
|
|
|
|
match arg_ty.cast {
|
|
|
|
Some(ty) => ty,
|
|
|
|
None => arg_ty.ty
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
llargument_tys.push(llarg_ty);
|
|
|
|
}
|
|
|
|
|
2013-10-25 00:56:34 -05:00
|
|
|
if tys.fn_sig.variadic {
|
2014-03-08 14:36:22 -06:00
|
|
|
Type::variadic_func(llargument_tys.as_slice(), &llreturn_ty)
|
2013-10-25 00:56:34 -05:00
|
|
|
} else {
|
2014-03-08 14:36:22 -06:00
|
|
|
Type::func(llargument_tys.as_slice(), &llreturn_ty)
|
2013-10-25 00:56:34 -05:00
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn lltype_for_foreign_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
ty: Ty<'tcx>) -> Type {
|
2014-03-15 15:29:34 -05:00
|
|
|
lltype_for_fn_from_foreign_types(ccx, &foreign_types_for_fn_ty(ccx, ty))
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
2013-04-18 17:53:29 -05:00
|
|
|
|
2013-05-21 14:25:44 -05:00
|
|
|
fn add_argument_attributes(tys: &ForeignTypes,
|
|
|
|
llfn: ValueRef) {
|
2014-06-27 14:30:25 -05:00
|
|
|
let mut i = if tys.fn_ty.ret_ty.is_indirect() {
|
|
|
|
1i
|
|
|
|
} else {
|
|
|
|
0i
|
|
|
|
};
|
2013-09-25 05:30:44 -05:00
|
|
|
|
2014-06-18 06:01:23 -05:00
|
|
|
match tys.fn_ty.ret_ty.attr {
|
|
|
|
Some(attr) => unsafe {
|
2014-09-12 01:00:07 -05:00
|
|
|
llvm::LLVMAddFunctionAttribute(llfn, i as c_uint, attr.bits() as u64);
|
2014-06-18 06:01:23 -05:00
|
|
|
},
|
|
|
|
None => {}
|
2013-09-25 05:30:44 -05:00
|
|
|
}
|
|
|
|
|
2014-06-18 06:01:23 -05:00
|
|
|
i += 1;
|
|
|
|
|
2013-09-25 05:30:44 -05:00
|
|
|
for &arg_ty in tys.fn_ty.arg_tys.iter() {
|
2014-03-09 00:42:22 -06:00
|
|
|
if arg_ty.is_ignore() {
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-25 05:30:44 -05:00
|
|
|
// skip padding
|
|
|
|
if arg_ty.pad.is_some() { i += 1; }
|
|
|
|
|
|
|
|
match arg_ty.attr {
|
2014-06-18 06:01:23 -05:00
|
|
|
Some(attr) => unsafe {
|
2014-09-12 01:00:07 -05:00
|
|
|
llvm::LLVMAddFunctionAttribute(llfn, i as c_uint, attr.bits() as u64);
|
2014-06-18 06:01:23 -05:00
|
|
|
},
|
2013-05-21 14:25:44 -05:00
|
|
|
None => ()
|
|
|
|
}
|
2013-09-25 05:30:44 -05:00
|
|
|
|
|
|
|
i += 1;
|
2012-03-20 13:44:28 -05:00
|
|
|
}
|
2012-03-07 18:48:57 -06:00
|
|
|
}
|