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
|
|
|
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
use back::{abi, 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;
|
2015-02-28 15:53:12 -06:00
|
|
|
use trans::attributes;
|
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::*;
|
2015-02-04 10:42:32 -06:00
|
|
|
use trans::debuginfo::DebugLoc;
|
2015-03-03 17:08:06 -06:00
|
|
|
use trans::declare;
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
use trans::expr;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::machine;
|
2014-12-17 13:16:28 -06:00
|
|
|
use trans::monomorphize;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::type_::Type;
|
|
|
|
use trans::type_of::*;
|
|
|
|
use trans::type_of;
|
2015-10-12 13:50:57 -05:00
|
|
|
use middle::infer;
|
2016-02-29 17:36:51 -06:00
|
|
|
use middle::ty::{self, Ty, TyCtxt};
|
2014-11-25 15:28:35 -06:00
|
|
|
use middle::subst::Substs;
|
|
|
|
|
2013-11-29 13:19:22 -06:00
|
|
|
use std::cmp;
|
2015-09-17 13:29:59 -05:00
|
|
|
use std::iter::once;
|
2014-02-26 11:58:41 -06:00
|
|
|
use libc::c_uint;
|
2016-02-05 06:13:36 -06:00
|
|
|
use syntax::abi::Abi;
|
2015-09-14 04:58:20 -05:00
|
|
|
use syntax::attr;
|
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};
|
2015-03-28 04:23:20 -05:00
|
|
|
use syntax::ast;
|
2016-01-12 13:39:22 -06:00
|
|
|
use syntax::attr::AttrMetaMethods;
|
2015-07-31 02:04:06 -05:00
|
|
|
|
|
|
|
use rustc_front::print::pprust;
|
|
|
|
use rustc_front::hir;
|
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
|
2015-01-06 04:03:42 -06: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 {
|
2016-02-05 06:13:36 -06:00
|
|
|
use syntax::abi::Abi::*;
|
2014-07-23 13:56:36 -05:00
|
|
|
match ccx.sess().target.target.adjust_abi(abi) {
|
|
|
|
RustIntrinsic => {
|
|
|
|
// Intrinsics are emitted at the call site
|
|
|
|
ccx.sess().bug("asked to register intrinsic fn");
|
|
|
|
}
|
2015-08-06 13:11:26 -05:00
|
|
|
PlatformIntrinsic => {
|
|
|
|
// Intrinsics are emitted at the call site
|
|
|
|
ccx.sess().bug("asked to register platform 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,
|
2015-12-26 14:29:28 -06:00
|
|
|
Vectorcall => llvm::X86_VectorCall,
|
2014-07-23 13:56:36 -05:00
|
|
|
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,
|
2015-07-31 02:04:06 -05:00
|
|
|
foreign_item: &hir::ForeignItem) -> ValueRef {
|
2015-06-25 15:42:17 -05:00
|
|
|
let ty = ccx.tcx().node_id_to_type(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);
|
2016-01-12 13:39:22 -06:00
|
|
|
let c = match attr::first_attr_value_str_by_name(&foreign_item.attrs,
|
|
|
|
"linkage") {
|
2014-02-25 18:15:10 -06:00
|
|
|
// 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) => {
|
2015-02-04 14:48:12 -06:00
|
|
|
let linkage = match llvm_linkage_by_name(&name) {
|
2014-02-25 18:15:10 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
2014-10-31 03:51:16 -05:00
|
|
|
let llty2 = match ty.sty {
|
2015-06-11 18:21:46 -05:00
|
|
|
ty::TyRawPtr(ref mt) => type_of::type_of(ccx, mt.ty),
|
2014-02-25 18:15:10 -06:00
|
|
|
_ => {
|
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.
|
2015-03-03 17:08:06 -06:00
|
|
|
let g1 = declare::declare_global(ccx, &ident[..], llty2);
|
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();
|
2015-02-04 14:48:12 -06:00
|
|
|
real_name.push_str(&ident);
|
2015-03-03 17:08:06 -06:00
|
|
|
let g2 = declare::define_global(ccx, &real_name[..], llty).unwrap_or_else(||{
|
|
|
|
ccx.sess().span_fatal(foreign_item.span,
|
|
|
|
&format!("symbol `{}` is already defined", ident))
|
|
|
|
});
|
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
|
|
|
|
}
|
|
|
|
}
|
2015-03-03 17:08:06 -06:00
|
|
|
None => // Generate an external declaration.
|
|
|
|
declare::declare_global(ccx, &ident[..], llty),
|
2016-01-12 13:39:22 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Handle thread-local external statics.
|
|
|
|
for attr in foreign_item.attrs.iter() {
|
|
|
|
if attr.check_name("thread_local") {
|
|
|
|
llvm::set_thread_local(c, true);
|
|
|
|
}
|
2014-02-25 18:15:10 -06:00
|
|
|
}
|
2016-01-12 13:39:22 -06:00
|
|
|
|
|
|
|
return c;
|
2014-02-25 18:15:10 -06:00
|
|
|
}
|
2013-06-16 06:11:17 -05:00
|
|
|
|
2015-02-28 15:55:50 -06:00
|
|
|
// only use this for foreign function ABIs and glue, use `get_extern_rust_fn` for Rust functions
|
|
|
|
pub fn get_extern_fn(ccx: &CrateContext,
|
|
|
|
externs: &mut ExternMap,
|
|
|
|
name: &str,
|
|
|
|
cc: llvm::CallConv,
|
|
|
|
ty: Type,
|
|
|
|
output: Ty)
|
|
|
|
-> ValueRef {
|
|
|
|
match externs.get(name) {
|
|
|
|
Some(n) => return *n,
|
|
|
|
None => {}
|
|
|
|
}
|
2015-03-03 17:08:06 -06:00
|
|
|
let f = declare::declare_fn(ccx, name, cc, ty, ty::FnConverging(output));
|
2015-02-28 15:55:50 -06:00
|
|
|
externs.insert(name.to_string(), f);
|
|
|
|
f
|
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Registers a foreign function found in a library. Just adds a LLVM global.
|
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>,
|
2015-09-11 13:07:12 -05:00
|
|
|
name: &str,
|
2015-09-14 04:58:20 -05:00
|
|
|
attrs: &[ast::Attribute])-> ValueRef {
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("register_foreign_item_fn(abi={:?}, \
|
|
|
|
ty={:?}, \
|
2014-05-14 01:07:33 -05:00
|
|
|
name={})",
|
2015-06-18 12:25:05 -05:00
|
|
|
abi,
|
|
|
|
fty,
|
2014-05-14 01:07:33 -05:00
|
|
|
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)
|
2015-01-06 04:03:42 -06:00
|
|
|
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
|
|
|
|
2015-02-28 15:55:50 -06:00
|
|
|
let llfn = get_extern_fn(ccx, &mut *ccx.externs().borrow_mut(), name, cc, llfn_ty, fty);
|
2015-09-11 13:10:43 -05:00
|
|
|
attributes::unwind(llfn, false);
|
2013-05-21 14:25:44 -05:00
|
|
|
add_argument_attributes(&tys, llfn);
|
2015-09-11 13:07:12 -05:00
|
|
|
attributes::from_fn_attrs(ccx, attrs, llfn);
|
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-11-25 20:17:11 -06: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
|
|
|
|
/// - `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.
|
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],
|
2015-02-04 10:42:32 -06:00
|
|
|
passed_arg_tys: Vec<Ty<'tcx>>,
|
|
|
|
call_debug_loc: DebugLoc)
|
2015-01-06 04:03:42 -06:00
|
|
|
-> Block<'blk, 'tcx>
|
|
|
|
{
|
2013-05-21 14:25:44 -05:00
|
|
|
let ccx = bcx.ccx();
|
2012-02-13 16:59:05 -06:00
|
|
|
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("trans_native_call(callee_ty={:?}, \
|
2013-09-28 00:38:08 -05:00
|
|
|
llfn={}, \
|
|
|
|
llretptr={})",
|
2015-06-18 12:25:05 -05:00
|
|
|
callee_ty,
|
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-10-31 03:51:16 -05:00
|
|
|
let (fn_abi, fn_sig) = match callee_ty.sty {
|
2016-02-16 10:36:41 -06:00
|
|
|
ty::TyFnDef(_, _, ref fn_ty) |
|
2015-06-13 15:15:03 -05:00
|
|
|
ty::TyFnPtr(ref fn_ty) => (fn_ty.abi, &fn_ty.sig),
|
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
|
|
|
};
|
2015-06-25 15:42:17 -05:00
|
|
|
let fn_sig = ccx.tcx().erase_late_bound_regions(fn_sig);
|
2015-10-12 13:50:57 -05:00
|
|
|
let fn_sig = infer::normalize_associated_type(ccx.tcx(), &fn_sig);
|
2015-02-18 13:48:57 -06:00
|
|
|
let llsig = foreign_signature(ccx, &fn_sig, &passed_arg_tys[..]);
|
2013-05-21 14:25:44 -05:00
|
|
|
let fn_type = cabi::compute_abi_info(ccx,
|
2015-02-20 13:08:14 -06:00
|
|
|
&llsig.llarg_tys,
|
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
|
|
|
|
2015-02-20 13:08:14 -06:00
|
|
|
let arg_tys: &[cabi::ArgType] = &fn_type.arg_tys;
|
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
|
|
|
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
let mut offset = 0;
|
|
|
|
for (i, arg_ty) in arg_tys.iter().enumerate() {
|
|
|
|
let mut llarg_rust = llargs_rust[i + offset];
|
2012-03-22 07:44:16 -05:00
|
|
|
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
if arg_ty.is_ignore() {
|
2014-03-09 00:42:22 -06:00
|
|
|
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,
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
ccx.tn().type_to_string(arg_ty.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 {
|
2015-08-25 11:29:24 -05:00
|
|
|
let scratch = base::alloc_ty(bcx, passed_arg_tys[i], "__arg");
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
if type_is_fat_ptr(ccx.tcx(), passed_arg_tys[i]) {
|
|
|
|
Store(bcx, llargs_rust[i + offset], expr::get_dataptr(bcx, scratch));
|
2015-08-24 14:50:50 -05:00
|
|
|
Store(bcx, llargs_rust[i + offset + 1], expr::get_meta(bcx, scratch));
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
offset += 1;
|
|
|
|
} else {
|
|
|
|
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
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
match arg_ty.cast {
|
2013-09-25 05:30:44 -05:00
|
|
|
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
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
let foreign_indirect = arg_ty.is_indirect();
|
2013-05-21 14:25:44 -05:00
|
|
|
let llarg_foreign = if foreign_indirect {
|
|
|
|
llarg_rust
|
|
|
|
} else {
|
2015-06-24 00:24:13 -05:00
|
|
|
if passed_arg_tys[i].is_bool() {
|
2016-01-18 04:30:52 -06: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
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
match arg_ty.pad {
|
2013-09-25 05:30:44 -05:00
|
|
|
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
|
2015-04-29 17:00:20 -05:00
|
|
|
attrs.arg(1, llvm::Attribute::NoAlias)
|
|
|
|
.arg(1, llvm::Attribute::NoCapture)
|
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;
|
2015-01-31 11:20:46 -06:00
|
|
|
for arg_ty in &fn_type.arg_tys {
|
2014-06-17 14:51:24 -05:00
|
|
|
if arg_ty.is_ignore() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// skip padding
|
|
|
|
if arg_ty.pad.is_some() { arg_idx += 1; }
|
|
|
|
|
2014-11-29 15:41:21 -06:00
|
|
|
if let Some(attr) = arg_ty.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,
|
2015-02-18 13:48:57 -06:00
|
|
|
&llargs_foreign[..],
|
2014-03-08 14:36:22 -06:00
|
|
|
cc,
|
2015-02-04 10:42:32 -06:00
|
|
|
Some(attrs),
|
|
|
|
call_debug_loc);
|
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 {
|
2015-01-06 04:03:42 -06:00
|
|
|
match fn_sig.output {
|
2014-10-24 14:14:37 -05:00
|
|
|
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");
|
2015-08-25 11:27:44 -05:00
|
|
|
base::call_lifetime_start(bcx, llscratch);
|
2013-05-21 14:25:44 -05:00
|
|
|
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);
|
2015-08-25 11:27:44 -05:00
|
|
|
base::call_lifetime_end(bcx, llscratch);
|
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
|
|
|
|
2015-01-15 17:16:20 -06:00
|
|
|
// feature gate SIMD types in FFI, since I (huonw) am not sure the
|
|
|
|
// ABIs are handled at all correctly.
|
2016-02-29 17:36:51 -06:00
|
|
|
fn gate_simd_ffi(tcx: &TyCtxt, decl: &hir::FnDecl, ty: &ty::BareFnTy) {
|
2015-01-15 17:16:20 -06:00
|
|
|
if !tcx.sess.features.borrow().simd_ffi {
|
2015-07-31 02:04:06 -05:00
|
|
|
let check = |ast_ty: &hir::Ty, ty: ty::Ty| {
|
2015-08-06 10:25:15 -05:00
|
|
|
if ty.is_simd() {
|
2015-12-20 15:00:43 -06:00
|
|
|
tcx.sess.struct_span_err(ast_ty.span,
|
2015-01-15 17:16:20 -06:00
|
|
|
&format!("use of SIMD type `{}` in FFI is highly experimental and \
|
|
|
|
may result in invalid code",
|
2015-12-20 15:00:43 -06:00
|
|
|
pprust::ty_to_string(ast_ty)))
|
|
|
|
.fileline_help(ast_ty.span,
|
|
|
|
"add #![feature(simd_ffi)] to the crate attributes to enable")
|
|
|
|
.emit();
|
2015-01-15 17:16:20 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let sig = &ty.sig.0;
|
2015-06-10 11:22:20 -05:00
|
|
|
for (input, ty) in decl.inputs.iter().zip(&sig.inputs) {
|
2016-02-09 14:24:11 -06:00
|
|
|
check(&input.ty, *ty)
|
2015-01-15 17:16:20 -06:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
if let hir::Return(ref ty) = decl.output {
|
2016-02-09 14:24:11 -06:00
|
|
|
check(&ty, sig.output.unwrap())
|
2015-01-15 17:16:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
pub fn trans_foreign_mod(ccx: &CrateContext, foreign_mod: &hir::ForeignMod) {
|
2013-05-21 14:25:44 -05:00
|
|
|
let _icx = push_ctxt("foreign::trans_foreign_mod");
|
2015-01-31 11:20:46 -06:00
|
|
|
for foreign_item in &foreign_mod.items {
|
2015-12-07 08:17:41 -06:00
|
|
|
let lname = link_name(foreign_item);
|
2014-05-14 01:07:33 -05:00
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
if let hir::ForeignItemFn(ref decl, _) = foreign_item.node {
|
2014-11-29 15:41:21 -06:00
|
|
|
match foreign_mod.abi {
|
2016-02-05 06:13:36 -06:00
|
|
|
Abi::Rust | Abi::RustIntrinsic | Abi::PlatformIntrinsic => {}
|
2014-11-29 15:41:21 -06:00
|
|
|
abi => {
|
2015-06-25 15:42:17 -05:00
|
|
|
let ty = ccx.tcx().node_id_to_type(foreign_item.id);
|
2015-01-15 17:16:20 -06:00
|
|
|
match ty.sty {
|
2016-02-16 10:36:41 -06:00
|
|
|
ty::TyFnDef(_, _, bft) |
|
2015-06-13 15:15:03 -05:00
|
|
|
ty::TyFnPtr(bft) => gate_simd_ffi(ccx.tcx(), &decl, bft),
|
2015-01-15 17:16:20 -06:00
|
|
|
_ => ccx.tcx().sess.span_bug(foreign_item.span,
|
|
|
|
"foreign fn's sty isn't a bare_fn_ty?")
|
|
|
|
}
|
|
|
|
|
2015-09-11 13:07:12 -05:00
|
|
|
register_foreign_item_fn(ccx, abi, ty, &lname, &foreign_item.attrs);
|
2014-11-29 15:41:21 -06: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_*`.
|
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,
|
2015-02-03 18:04:50 -06:00
|
|
|
lname.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);
|
2014-10-31 03:51:16 -05:00
|
|
|
let cconv = match t.sty {
|
2016-02-16 10:36:41 -06:00
|
|
|
ty::TyFnDef(_, _, ref fn_ty) | ty::TyFnPtr(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
|
|
|
};
|
2015-03-03 17:08:06 -06:00
|
|
|
let llfn = declare::declare_fn(ccx, name, cconv, llfn_ty,
|
2015-06-24 20:09:46 -05:00
|
|
|
ty::FnConverging(ccx.tcx().mk_nil()));
|
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");
|
|
|
|
|
2015-06-25 15:42:17 -05:00
|
|
|
let t = ccx.tcx().node_id_to_type(node_id);
|
2014-10-31 03:51:16 -05:00
|
|
|
let cconv = match t.sty {
|
2016-02-16 10:36:41 -06:00
|
|
|
ty::TyFnDef(_, _, ref fn_ty) | ty::TyFnPtr(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
|
|
|
};
|
2015-10-12 13:50:57 -05:00
|
|
|
let tys = foreign_types_for_fn_ty(ccx, t);
|
|
|
|
let llfn_ty = lltype_for_fn_from_foreign_types(ccx, &tys);
|
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>,
|
2015-07-31 02:04:06 -05:00
|
|
|
decl: &hir::FnDecl,
|
|
|
|
body: &hir::Block,
|
2015-09-14 04:58:20 -05:00
|
|
|
attrs: &[ast::Attribute],
|
2014-09-29 14:11:30 -05:00
|
|
|
llwrapfn: ValueRef,
|
2015-01-29 06:03:34 -06:00
|
|
|
param_substs: &'tcx 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
|
|
|
|
2015-06-25 15:42:17 -05:00
|
|
|
let fnty = ccx.tcx().node_id_to_type(id);
|
2014-12-17 13:16:28 -06:00
|
|
|
let mty = monomorphize::apply_param_substs(ccx.tcx(), param_substs, &fnty);
|
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>,
|
2015-07-31 02:04:06 -05:00
|
|
|
decl: &hir::FnDecl,
|
|
|
|
body: &hir::Block,
|
2015-01-29 06:03:34 -06:00
|
|
|
param_substs: &'tcx Substs<'tcx>,
|
2015-09-14 04:58:20 -05:00
|
|
|
attrs: &[ast::Attribute],
|
2014-09-29 14:11:30 -05:00
|
|
|
id: ast::NodeId,
|
|
|
|
hash: Option<&str>)
|
2014-12-17 13:16:28 -06:00
|
|
|
-> 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();
|
2015-06-25 15:42:17 -05:00
|
|
|
let t = tcx.node_id_to_type(id);
|
2014-12-17 13:16:28 -06:00
|
|
|
let t = monomorphize::apply_param_substs(tcx, param_substs, &t);
|
2014-02-13 23:07:09 -06:00
|
|
|
|
2015-09-17 13:29:59 -05:00
|
|
|
let path =
|
|
|
|
tcx.map.def_path_from_id(id)
|
|
|
|
.into_iter()
|
|
|
|
.map(|e| e.data.as_interned_str())
|
|
|
|
.chain(once(special_idents::clownshoe_abi.name.as_str()));
|
|
|
|
let ps = link::mangle(path, hash);
|
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-10-31 03:51:16 -05:00
|
|
|
match t.sty {
|
2016-02-16 10:36:41 -06:00
|
|
|
ty::TyFnDef(_, _, ref f) | ty::TyFnPtr(ref f)=> {
|
2016-02-05 06:13:36 -06:00
|
|
|
assert!(f.abi != Abi::Rust);
|
|
|
|
assert!(f.abi != Abi::RustIntrinsic);
|
|
|
|
assert!(f.abi != Abi::PlatformIntrinsic);
|
2013-08-19 20:28:31 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2015-06-18 12:25:05 -05: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),
|
2015-06-18 12:25:05 -05:00
|
|
|
t));
|
2013-08-19 20:28:31 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("build_rust_fn: path={} id={} t={:?}",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tcx().map.path_to_string(id),
|
2015-06-18 12:25:05 -05:00
|
|
|
id, t);
|
2013-08-19 20:28:31 -05:00
|
|
|
|
2015-07-20 15:27:38 -05:00
|
|
|
let llfn = declare::define_internal_rust_fn(ccx, &ps, t);
|
2015-03-03 17:03:25 -06:00
|
|
|
attributes::from_fn_attrs(ccx, attrs, llfn);
|
2015-12-31 07:06:23 -06:00
|
|
|
base::trans_fn(ccx, decl, body, llfn, param_substs, id, attrs);
|
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");
|
|
|
|
|
2015-06-18 12:25:05 -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),
|
2015-06-18 12:25:05 -05:00
|
|
|
t);
|
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;
|
|
|
|
// }
|
|
|
|
|
2015-02-27 06:18:39 -06:00
|
|
|
if llvm::LLVMCountBasicBlocks(llwrapfn) != 0 {
|
|
|
|
ccx.sess().bug("wrapping a function inside non-empty wrapper, most likely cause is \
|
|
|
|
multiple functions being wrapped");
|
|
|
|
}
|
|
|
|
|
2014-11-25 15:28:35 -06:00
|
|
|
let ptr = "the block\0".as_ptr();
|
|
|
|
let the_block = llvm::LLVMAppendBasicBlockInContext(ccx.llcx(), llwrapfn,
|
|
|
|
ptr as *const _);
|
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;
|
2015-02-01 11:44:15 -06:00
|
|
|
let mut next_foreign_arg = |pad: bool| -> c_uint {
|
2013-11-19 15:22:03 -06:00
|
|
|
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
|
|
|
|
2015-02-10 16:43:38 -06:00
|
|
|
let rustfn_ty = Type::from_ref(llvm::LLVMTypeOf(llrustfn)).element_type();
|
|
|
|
let mut rust_param_tys = rustfn_ty.func_params().into_iter();
|
2013-05-21 14:25:44 -05:00
|
|
|
// Push Rust return pointer, using null if it will be unused.
|
2015-01-06 04:03:42 -06:00
|
|
|
let rust_uses_outptr = match tys.fn_sig.output {
|
2014-10-24 14:14:37 -05:00
|
|
|
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>;
|
2015-02-10 16:43:38 -06:00
|
|
|
let llrust_ret_ty = if rust_uses_outptr {
|
|
|
|
rust_param_tys.next().expect("Missing return type!").element_type()
|
|
|
|
} else {
|
|
|
|
rustfn_ty.return_type()
|
|
|
|
};
|
2013-05-21 14:25:44 -05:00
|
|
|
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 =
|
2015-02-10 16:43:38 -06:00
|
|
|
builder.bitcast(llforeign_outptr, llrust_ret_ty.ptr_to());
|
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={}, \
|
2015-06-18 12:25:05 -05:00
|
|
|
return_ty={:?}",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().val_to_string(slot),
|
|
|
|
ccx.tn().type_to_string(llrust_ret_ty),
|
2015-06-18 12:25:05 -05:00
|
|
|
tys.fn_sig.output);
|
2013-05-21 14:25:44 -05:00
|
|
|
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.
|
2015-01-26 15:05:07 -06:00
|
|
|
for i in 0..tys.fn_sig.inputs.len() {
|
2015-01-06 04:03:42 -06:00
|
|
|
let rust_ty = tys.fn_sig.inputs[i];
|
2013-05-21 14:25:44 -05:00
|
|
|
let rust_indirect = type_of::arg_is_indirect(ccx, rust_ty);
|
2015-02-10 16:43:38 -06:00
|
|
|
let llty = rust_param_tys.next().expect("Not enough parameter types!");
|
|
|
|
let llrust_ty = if rust_indirect {
|
|
|
|
llty.element_type()
|
|
|
|
} else {
|
|
|
|
llty
|
|
|
|
};
|
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 {
|
2015-06-24 00:24:13 -05:00
|
|
|
llforeign_arg = if rust_ty.is_bool() {
|
2014-07-05 14:47:14 -05:00
|
|
|
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.
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
if llforeign_arg_ty.cast.is_some() && !type_is_fat_ptr(ccx.tcx(), rust_ty){
|
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
|
|
|
}
|
|
|
|
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
let llrust_arg = if rust_indirect || type_is_fat_ptr(ccx.tcx(), rust_ty) {
|
2013-05-21 14:25:44 -05:00
|
|
|
llforeign_arg
|
2013-04-18 17:53:29 -05:00
|
|
|
} else {
|
2015-06-24 00:24:13 -05:00
|
|
|
if rust_ty.is_bool() {
|
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))
|
2015-01-08 09:28:07 -06:00
|
|
|
} else if type_of::type_of(ccx, rust_ty).is_aggregate() {
|
|
|
|
// We want to pass small aggregates as immediate values, but using an aggregate
|
|
|
|
// LLVM type for this leads to bad optimizations, so its arg type is an
|
|
|
|
// appropriately sized integer and we have to convert it
|
|
|
|
let tmp = builder.bitcast(llforeign_arg,
|
|
|
|
type_of::arg_type_of(ccx, rust_ty).ptr_to());
|
2015-04-15 13:14:54 -05:00
|
|
|
let load = builder.load(tmp);
|
|
|
|
llvm::LLVMSetAlignment(load, type_of::align_of(ccx, rust_ty));
|
|
|
|
load
|
2014-07-05 14:47:14 -05:00
|
|
|
} 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));
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
if type_is_fat_ptr(ccx.tcx(), rust_ty) {
|
|
|
|
let next_llrust_ty = rust_param_tys.next().expect("Not enough parameter types!");
|
2015-08-24 15:51:57 -05:00
|
|
|
llrust_args.push(builder.load(builder.bitcast(builder.struct_gep(
|
|
|
|
llrust_arg, abi::FAT_PTR_ADDR), llrust_ty.ptr_to())));
|
|
|
|
llrust_args.push(builder.load(builder.bitcast(builder.struct_gep(
|
|
|
|
llrust_arg, abi::FAT_PTR_EXTRA), next_llrust_ty.ptr_to())));
|
Pass fat pointers in two immediate arguments
This has a number of advantages compared to creating a copy in memory
and passing a pointer. The obvious one is that we don't have to put the
data into memory but can keep it in registers. Since we're currently
passing a pointer anyway (instead of using e.g. a known offset on the
stack, which is what the `byval` attribute would achieve), we only use a
single additional register for each fat pointer, but save at least two
pointers worth of stack in exchange (sometimes more because more than
one copy gets eliminated). On archs that pass arguments on the stack, we
save a pointer worth of stack even without considering the omitted
copies.
Additionally, LLVM can optimize the code a lot better, to a large degree
due to the fact that lots of copies are gone or can be optimized away.
Additionally, we can now emit attributes like nonnull on the data and/or
vtable pointers contained in the fat pointer, potentially allowing for
even more optimizations.
This results in LLVM passes being about 3-7% faster (depending on the
crate), and the resulting code is also a few percent smaller, for
example:
text data filename
5671479 3941461 before/librustc-d8ace771.so
5447663 3905745 after/librustc-d8ace771.so
1944425 2394024 before/libstd-d8ace771.so
1896769 2387610 after/libstd-d8ace771.so
I had to remove a call in the backtrace-debuginfo test, because LLVM can
now merge the tails of some blocks when optimizations are turned on,
which can't correctly preserve line info.
Fixes #22924
Cc #22891 (at least for fat pointers the code is good now)
2015-06-18 16:57:40 -05:00
|
|
|
} else {
|
|
|
|
llrust_args.push(llrust_arg);
|
|
|
|
}
|
2013-05-21 14:25:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the call itself
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("calling llrustfn = {}, t = {:?}",
|
|
|
|
ccx.tn().val_to_string(llrustfn), t);
|
2015-03-03 17:03:25 -06:00
|
|
|
let attributes = attributes::from_fn_type(ccx, t);
|
2015-10-23 20:18:44 -05:00
|
|
|
let llrust_ret_val = builder.call(llrustfn, &llrust_args,
|
|
|
|
None, 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 =
|
2015-02-10 16:43:38 -06:00
|
|
|
builder.bitcast(llforeign_outptr, llrust_ret_ty.ptr_to());
|
2014-07-04 07:50:36 -05:00
|
|
|
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.
|
|
|
|
|
2015-07-31 02:04:06 -05:00
|
|
|
pub fn link_name(i: &hir::ForeignItem) -> InternedString {
|
2015-02-20 13:08:14 -06:00
|
|
|
match attr::first_attr_value_str_by_name(&i.attrs, "link_name") {
|
2014-01-10 16:02:36 -06:00
|
|
|
Some(ln) => ln.clone(),
|
2015-02-20 13:08:14 -06:00
|
|
|
None => match weak_lang_items::link_name(&i.attrs) {
|
2014-05-19 11:30:09 -05:00
|
|
|
Some(name) => name,
|
2015-09-19 20:50:30 -05:00
|
|
|
None => i.name.as_str(),
|
2014-05-19 11:30:09 -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
|
|
|
|
2014-11-25 20:17:11 -06: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.
|
2014-09-29 14:11:30 -05:00
|
|
|
fn foreign_signature<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
2015-01-06 04:03:42 -06:00
|
|
|
fn_sig: &ty::FnSig<'tcx>,
|
|
|
|
arg_tys: &[Ty<'tcx>])
|
2014-09-29 14:11:30 -05:00
|
|
|
-> LlvmSignature {
|
2015-01-08 09:28:07 -06:00
|
|
|
let llarg_tys = arg_tys.iter().map(|&arg| foreign_arg_type_of(ccx, arg)).collect();
|
2015-01-06 04:03:42 -06:00
|
|
|
let (llret_ty, ret_def) = match fn_sig.output {
|
2014-10-24 14:14:37 -05:00
|
|
|
ty::FnConverging(ret_ty) =>
|
2015-01-08 09:28:07 -06:00
|
|
|
(type_of::foreign_arg_type_of(ccx, ret_ty), !return_type_is_void(ccx, ret_ty)),
|
2014-10-24 14:14:37 -05:00
|
|
|
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_fn_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
ty: Ty<'tcx>) -> ForeignTypes<'tcx> {
|
2014-10-31 03:51:16 -05:00
|
|
|
let fn_sig = match ty.sty {
|
2016-02-16 10:36:41 -06:00
|
|
|
ty::TyFnDef(_, _, ref fn_ty) | ty::TyFnPtr(ref fn_ty) => &fn_ty.sig,
|
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
|
|
|
};
|
2015-06-25 15:42:17 -05:00
|
|
|
let fn_sig = ccx.tcx().erase_late_bound_regions(fn_sig);
|
2015-10-12 13:50:57 -05:00
|
|
|
let fn_sig = infer::normalize_associated_type(ccx.tcx(), &fn_sig);
|
2015-02-01 20:53:25 -06:00
|
|
|
let llsig = foreign_signature(ccx, &fn_sig, &fn_sig.inputs);
|
2013-05-21 14:25:44 -05:00
|
|
|
let fn_ty = cabi::compute_abi_info(ccx,
|
2015-02-20 13:08:14 -06:00
|
|
|
&llsig.llarg_tys,
|
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(\
|
2015-06-18 12:25:05 -05:00
|
|
|
ty={:?}, \
|
2013-09-28 00:38:08 -05:00
|
|
|
llsig={} -> {}, \
|
|
|
|
fn_ty={} -> {}, \
|
|
|
|
ret_def={}",
|
2015-06-18 12:25:05 -05:00
|
|
|
ty,
|
2015-02-20 13:08:14 -06:00
|
|
|
ccx.tn().types_to_str(&llsig.llarg_tys),
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().type_to_string(llsig.llret_ty),
|
2015-02-01 20:53:25 -06:00
|
|
|
ccx.tn().types_to_str(&fn_ty.arg_tys.iter().map(|t| t.ty).collect::<Vec<_>>()),
|
2014-09-05 11:18:53 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-31 11:20:46 -06:00
|
|
|
for &arg_ty in &tys.fn_ty.arg_tys {
|
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);
|
|
|
|
}
|
|
|
|
|
2015-01-06 04:03:42 -06:00
|
|
|
if tys.fn_sig.variadic {
|
2015-02-01 20:53:25 -06:00
|
|
|
Type::variadic_func(&llargument_tys, &llreturn_ty)
|
2013-10-25 00:56:34 -05:00
|
|
|
} else {
|
2015-02-18 13:48:57 -06:00
|
|
|
Type::func(&llargument_tys[..], &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() {
|
2015-01-25 15:05:03 -06:00
|
|
|
1
|
2014-06-27 14:30:25 -05:00
|
|
|
} else {
|
2015-01-25 15:05:03 -06:00
|
|
|
0
|
2014-06-27 14:30:25 -05:00
|
|
|
};
|
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;
|
|
|
|
|
2015-01-31 11:20:46 -06:00
|
|
|
for &arg_ty in &tys.fn_ty.arg_tys {
|
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
|
|
|
}
|