2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
//! Handles translation of callees as well as other call-related
|
|
|
|
//! things. Callees are a superset of normal rust values and sometimes
|
|
|
|
//! have different representations. In particular, top-level fn items
|
|
|
|
//! and methods are represented as just a fn ptr and not a full
|
|
|
|
//! closure.
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::CalleeData::*;
|
|
|
|
pub use self::CallArgs::*;
|
|
|
|
|
2014-06-28 12:55:17 -07:00
|
|
|
use arena::TypedArena;
|
2016-02-14 18:08:08 -05:00
|
|
|
use back::symbol_names;
|
2015-06-18 20:07:36 +02:00
|
|
|
use llvm::{self, ValueRef, get_params};
|
2015-11-25 00:00:26 +02:00
|
|
|
use middle::cstore::LOCAL_CRATE;
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty::subst;
|
|
|
|
use rustc::traits;
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir::map as hir_map;
|
2016-03-22 19:23:36 +02:00
|
|
|
use abi::{Abi, FnType};
|
|
|
|
use adt;
|
|
|
|
use attributes;
|
|
|
|
use base;
|
|
|
|
use base::*;
|
|
|
|
use build::*;
|
|
|
|
use cleanup;
|
|
|
|
use cleanup::CleanupMethods;
|
|
|
|
use closure;
|
|
|
|
use common::{self, Block, Result, CrateContext, FunctionContext};
|
|
|
|
use common::{C_uint, C_undef};
|
|
|
|
use consts;
|
|
|
|
use datum::*;
|
|
|
|
use debuginfo::DebugLoc;
|
|
|
|
use declare;
|
|
|
|
use expr;
|
|
|
|
use glue;
|
|
|
|
use inline;
|
|
|
|
use intrinsic;
|
|
|
|
use machine::{llalign_of_min, llsize_of_store};
|
|
|
|
use meth;
|
|
|
|
use monomorphize::{self, Instance};
|
|
|
|
use type_::Type;
|
|
|
|
use type_of;
|
|
|
|
use value::Value;
|
|
|
|
use Disr;
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir;
|
2012-12-13 13:05:22 -08:00
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
use syntax::codemap::DUMMY_SP;
|
2015-12-14 18:15:39 +13:00
|
|
|
use syntax::errors;
|
2014-09-07 20:09:06 +03:00
|
|
|
use syntax::ptr::P;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
use std::cmp;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2016-02-23 22:00:59 +02:00
|
|
|
pub enum CalleeData {
|
2016-03-06 17:32:47 +02:00
|
|
|
/// Constructor for enum variant/tuple-like-struct.
|
2016-01-16 16:03:09 +01:00
|
|
|
NamedTupleConstructor(Disr),
|
2014-07-09 23:42:08 -07:00
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
/// Function pointer.
|
|
|
|
Fn(ValueRef),
|
2014-01-27 14:18:36 +02:00
|
|
|
|
2016-02-23 22:00:59 +02:00
|
|
|
Intrinsic,
|
2014-07-09 15:31:45 -07:00
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
/// Trait object found in the vtable at that index.
|
|
|
|
Virtual(usize)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
#[derive(Debug)]
|
2016-03-06 17:32:47 +02:00
|
|
|
pub struct Callee<'tcx> {
|
2016-02-23 22:00:59 +02:00
|
|
|
pub data: CalleeData,
|
2015-07-03 05:22:54 +03:00
|
|
|
pub ty: Ty<'tcx>
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
impl<'tcx> Callee<'tcx> {
|
|
|
|
/// Function pointer.
|
|
|
|
pub fn ptr(datum: Datum<'tcx, Rvalue>) -> Callee<'tcx> {
|
|
|
|
Callee {
|
|
|
|
data: Fn(datum.val),
|
|
|
|
ty: datum.ty
|
2015-01-13 06:02:56 +02:00
|
|
|
}
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
/// Trait or impl method call.
|
|
|
|
pub fn method_call<'blk>(bcx: Block<'blk, 'tcx>,
|
|
|
|
method_call: ty::MethodCall)
|
|
|
|
-> Callee<'tcx> {
|
|
|
|
let method = bcx.tcx().tables.borrow().method_map[&method_call];
|
|
|
|
Callee::method(bcx, method)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
/// Trait or impl method.
|
|
|
|
pub fn method<'blk>(bcx: Block<'blk, 'tcx>,
|
|
|
|
method: ty::MethodCallee<'tcx>) -> Callee<'tcx> {
|
2016-04-29 08:30:54 +03:00
|
|
|
let substs = bcx.fcx.monomorphize(&method.substs);
|
2016-02-23 21:21:50 +02:00
|
|
|
Callee::def(bcx.ccx(), method.def_id, substs)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
/// Function or method definition.
|
|
|
|
pub fn def<'a>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
def_id: DefId,
|
2016-02-23 21:21:50 +02:00
|
|
|
substs: &'tcx subst::Substs<'tcx>)
|
2016-03-06 17:32:47 +02:00
|
|
|
-> Callee<'tcx> {
|
|
|
|
let tcx = ccx.tcx();
|
|
|
|
|
|
|
|
if substs.self_ty().is_some() {
|
|
|
|
// Only trait methods can have a Self parameter.
|
2016-02-23 21:21:50 +02:00
|
|
|
return Callee::trait_method(ccx, def_id, substs);
|
2016-03-06 17:32:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let maybe_node_id = inline::get_local_instance(ccx, def_id)
|
|
|
|
.and_then(|def_id| tcx.map.as_local_node_id(def_id));
|
|
|
|
let maybe_ast_node = maybe_node_id.and_then(|node_id| {
|
|
|
|
tcx.map.find(node_id)
|
|
|
|
});
|
2016-02-23 21:21:50 +02:00
|
|
|
|
|
|
|
let data = match maybe_ast_node {
|
2016-03-06 17:32:47 +02:00
|
|
|
Some(hir_map::NodeStructCtor(_)) => {
|
2016-02-23 21:21:50 +02:00
|
|
|
NamedTupleConstructor(Disr(0))
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
2016-03-06 17:32:47 +02:00
|
|
|
Some(hir_map::NodeVariant(_)) => {
|
|
|
|
let vinfo = common::inlined_variant_def(ccx, maybe_node_id.unwrap());
|
2016-02-23 21:21:50 +02:00
|
|
|
NamedTupleConstructor(Disr::from(vinfo.disr_val))
|
|
|
|
}
|
|
|
|
Some(hir_map::NodeForeignItem(fi)) if {
|
|
|
|
let abi = tcx.map.get_foreign_abi(fi.id);
|
|
|
|
abi == Abi::RustIntrinsic || abi == Abi::PlatformIntrinsic
|
|
|
|
} => Intrinsic,
|
|
|
|
|
|
|
|
_ => return Callee::ptr(get_fn(ccx, def_id, substs))
|
|
|
|
};
|
|
|
|
|
|
|
|
Callee {
|
|
|
|
data: data,
|
|
|
|
ty: def_ty(tcx, def_id, substs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait method, which has to be resolved to an impl method.
|
|
|
|
pub fn trait_method<'a>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: &'tcx subst::Substs<'tcx>)
|
|
|
|
-> Callee<'tcx> {
|
|
|
|
let tcx = ccx.tcx();
|
2014-07-09 23:42:08 -07:00
|
|
|
|
2016-02-23 21:21:50 +02:00
|
|
|
let method_item = tcx.impl_or_trait_item(def_id);
|
|
|
|
let trait_id = method_item.container().id();
|
|
|
|
let trait_ref = ty::Binder(substs.to_trait_ref(tcx, trait_id));
|
2016-03-12 03:06:24 +02:00
|
|
|
let trait_ref = tcx.normalize_associated_type(&trait_ref);
|
2016-05-06 00:47:28 -04:00
|
|
|
match common::fulfill_obligation(ccx.shared(), DUMMY_SP, trait_ref) {
|
2016-02-23 21:21:50 +02:00
|
|
|
traits::VtableImpl(vtable_impl) => {
|
|
|
|
let impl_did = vtable_impl.impl_def_id;
|
|
|
|
let mname = tcx.item_name(def_id);
|
|
|
|
// create a concatenated set of substitutions which includes
|
|
|
|
// those from the impl and those from the method:
|
|
|
|
let impl_substs = vtable_impl.substs.with_method_from(&substs);
|
|
|
|
let substs = tcx.mk_substs(impl_substs);
|
2016-02-23 21:32:44 +02:00
|
|
|
let mth = meth::get_impl_method(tcx, impl_did, substs, mname);
|
2016-02-23 21:21:50 +02:00
|
|
|
|
|
|
|
// Translate the function, bypassing Callee::def.
|
|
|
|
// That is because default methods have the same ID as the
|
|
|
|
// trait method used to look up the impl method that ended
|
|
|
|
// up here, so calling Callee::def would infinitely recurse.
|
|
|
|
Callee::ptr(get_fn(ccx, mth.method.def_id, mth.substs))
|
|
|
|
}
|
|
|
|
traits::VtableClosure(vtable_closure) => {
|
|
|
|
// The substitutions should have no type parameters remaining
|
|
|
|
// after passing through fulfill_obligation
|
|
|
|
let trait_closure_kind = tcx.lang_items.fn_trait_kind(trait_id).unwrap();
|
|
|
|
let llfn = closure::trans_closure_method(ccx,
|
|
|
|
vtable_closure.closure_def_id,
|
|
|
|
vtable_closure.substs,
|
|
|
|
trait_closure_kind);
|
|
|
|
|
|
|
|
let method_ty = def_ty(tcx, def_id, substs);
|
|
|
|
let fn_ptr_ty = match method_ty.sty {
|
2016-04-29 08:30:54 +03:00
|
|
|
ty::TyFnDef(_, _, fty) => tcx.mk_fn_ptr(fty),
|
2016-03-29 01:46:02 +02:00
|
|
|
_ => bug!("expected fn item type, found {}",
|
|
|
|
method_ty)
|
2016-03-06 17:32:47 +02:00
|
|
|
};
|
2016-02-23 21:21:50 +02:00
|
|
|
Callee::ptr(immediate_rvalue(llfn, fn_ptr_ty))
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
2016-05-11 14:40:24 -07:00
|
|
|
traits::VtableFnPointer(vtable_fn_pointer) => {
|
2016-02-23 21:21:50 +02:00
|
|
|
let trait_closure_kind = tcx.lang_items.fn_trait_kind(trait_id).unwrap();
|
2016-05-11 14:40:24 -07:00
|
|
|
let llfn = trans_fn_pointer_shim(ccx, trait_closure_kind, vtable_fn_pointer.fn_ty);
|
2016-02-23 21:21:50 +02:00
|
|
|
|
|
|
|
let method_ty = def_ty(tcx, def_id, substs);
|
|
|
|
let fn_ptr_ty = match method_ty.sty {
|
2016-04-29 08:30:54 +03:00
|
|
|
ty::TyFnDef(_, _, fty) => tcx.mk_fn_ptr(fty),
|
2016-03-29 01:46:02 +02:00
|
|
|
_ => bug!("expected fn item type, found {}",
|
|
|
|
method_ty)
|
2016-02-23 21:21:50 +02:00
|
|
|
};
|
|
|
|
Callee::ptr(immediate_rvalue(llfn, fn_ptr_ty))
|
|
|
|
}
|
|
|
|
traits::VtableObject(ref data) => {
|
|
|
|
Callee {
|
2016-03-17 00:15:31 +02:00
|
|
|
data: Virtual(tcx.get_vtable_index_of_object_method(data, def_id)),
|
2016-02-23 21:21:50 +02:00
|
|
|
ty: def_ty(tcx, def_id, substs)
|
2014-07-10 00:20:28 -07:00
|
|
|
}
|
2012-10-24 14:36:00 -07:00
|
|
|
}
|
2016-02-23 21:21:50 +02:00
|
|
|
vtable => {
|
2016-03-29 01:46:02 +02:00
|
|
|
bug!("resolved vtable bad vtable {:?} in trans", vtable);
|
2016-02-23 21:21:50 +02:00
|
|
|
}
|
2016-03-06 17:32:47 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-06 12:38:46 +02:00
|
|
|
|
|
|
|
/// Get the abi::FnType for a direct call. Mainly deals with the fact
|
|
|
|
/// that a Virtual call doesn't take the vtable, like its shim does.
|
|
|
|
/// The extra argument types are for variadic (extern "C") functions.
|
|
|
|
pub fn direct_fn_type<'a>(&self, ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
extra_args: &[Ty<'tcx>]) -> FnType {
|
|
|
|
let abi = self.ty.fn_abi();
|
|
|
|
let sig = ccx.tcx().erase_late_bound_regions(self.ty.fn_sig());
|
2016-03-11 02:33:20 +02:00
|
|
|
let sig = ccx.tcx().normalize_associated_type(&sig);
|
2016-03-06 12:38:46 +02:00
|
|
|
let mut fn_ty = FnType::unadjusted(ccx, abi, &sig, extra_args);
|
|
|
|
if let Virtual(_) = self.data {
|
|
|
|
// Don't pass the vtable, it's not an argument of the virtual fn.
|
|
|
|
fn_ty.args[1].ignore();
|
|
|
|
}
|
|
|
|
fn_ty.adjust_for_abi(ccx, abi, &sig);
|
|
|
|
fn_ty
|
|
|
|
}
|
2016-03-06 17:32:47 +02:00
|
|
|
|
|
|
|
/// This behemoth of a function translates function calls. Unfortunately, in
|
|
|
|
/// order to generate more efficient LLVM output at -O0, it has quite a complex
|
|
|
|
/// signature (refactoring this into two functions seems like a good idea).
|
|
|
|
///
|
|
|
|
/// In particular, for lang items, it is invoked with a dest of None, and in
|
|
|
|
/// that case the return value contains the result of the fn. The lang item must
|
|
|
|
/// not return a structural type or else all heck breaks loose.
|
|
|
|
///
|
|
|
|
/// For non-lang items, `dest` is always Some, and hence the result is written
|
|
|
|
/// into memory somewhere. Nonetheless we return the actual return value of the
|
|
|
|
/// function.
|
|
|
|
pub fn call<'a, 'blk>(self, bcx: Block<'blk, 'tcx>,
|
|
|
|
debug_loc: DebugLoc,
|
|
|
|
args: CallArgs<'a, 'tcx>,
|
|
|
|
dest: Option<expr::Dest>)
|
|
|
|
-> Result<'blk, 'tcx> {
|
|
|
|
trans_call_inner(bcx, debug_loc, self, args, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turn the callee into a function pointer.
|
|
|
|
pub fn reify<'a>(self, ccx: &CrateContext<'a, 'tcx>)
|
|
|
|
-> Datum<'tcx, Rvalue> {
|
2016-03-06 16:30:21 +02:00
|
|
|
let fn_ptr_ty = match self.ty.sty {
|
2016-04-29 08:30:54 +03:00
|
|
|
ty::TyFnDef(_, _, f) => ccx.tcx().mk_fn_ptr(f),
|
2016-03-06 16:30:21 +02:00
|
|
|
_ => self.ty
|
|
|
|
};
|
2016-03-06 17:32:47 +02:00
|
|
|
match self.data {
|
|
|
|
Fn(llfn) => {
|
|
|
|
immediate_rvalue(llfn, fn_ptr_ty)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
2016-03-06 16:30:21 +02:00
|
|
|
Virtual(idx) => {
|
|
|
|
let llfn = meth::trans_object_shim(ccx, self.ty, idx);
|
|
|
|
immediate_rvalue(llfn, fn_ptr_ty)
|
|
|
|
}
|
2016-03-06 17:32:47 +02:00
|
|
|
NamedTupleConstructor(_) => match self.ty.sty {
|
|
|
|
ty::TyFnDef(def_id, substs, _) => {
|
2016-02-23 21:21:50 +02:00
|
|
|
return get_fn(ccx, def_id, substs);
|
2016-03-06 17:32:47 +02:00
|
|
|
}
|
2016-03-29 01:46:02 +02:00
|
|
|
_ => bug!("expected fn item type, found {}", self.ty)
|
2016-03-06 17:32:47 +02:00
|
|
|
},
|
2016-03-29 01:46:02 +02:00
|
|
|
Intrinsic => bug!("intrinsic {} getting reified", self.ty)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 21:21:50 +02:00
|
|
|
/// Given a DefId and some Substs, produces the monomorphic item type.
|
2016-05-03 05:23:22 +03:00
|
|
|
fn def_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
def_id: DefId,
|
|
|
|
substs: &'tcx subst::Substs<'tcx>)
|
|
|
|
-> Ty<'tcx> {
|
2016-02-23 21:21:50 +02:00
|
|
|
let ty = tcx.lookup_item_type(def_id).ty;
|
|
|
|
monomorphize::apply_param_substs(tcx, substs, &ty)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2014-12-01 09:23:40 -05:00
|
|
|
/// Translates an adapter that implements the `Fn` trait for a fn
|
|
|
|
/// pointer. This is basically the equivalent of something like:
|
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2014-12-01 09:23:40 -05:00
|
|
|
/// impl<'a> Fn(&'a int) -> &'a int for fn(&int) -> &int {
|
|
|
|
/// extern "rust-abi" fn call(&self, args: (&'a int,)) -> &'a int {
|
|
|
|
/// (*self)(args.0)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// but for the bare function type given.
|
|
|
|
pub fn trans_fn_pointer_shim<'a, 'tcx>(
|
|
|
|
ccx: &'a CrateContext<'a, 'tcx>,
|
2015-02-15 15:09:26 -05:00
|
|
|
closure_kind: ty::ClosureKind,
|
2014-12-01 09:23:40 -05:00
|
|
|
bare_fn_ty: Ty<'tcx>)
|
|
|
|
-> ValueRef
|
|
|
|
{
|
|
|
|
let _icx = push_ctxt("trans_fn_pointer_shim");
|
|
|
|
let tcx = ccx.tcx();
|
|
|
|
|
2015-02-15 15:09:26 -05:00
|
|
|
// Normalize the type for better caching.
|
2015-09-15 00:47:14 +03:00
|
|
|
let bare_fn_ty = tcx.erase_regions(&bare_fn_ty);
|
2015-02-15 15:09:26 -05:00
|
|
|
|
|
|
|
// If this is an impl of `Fn` or `FnMut` trait, the receiver is `&self`.
|
|
|
|
let is_by_ref = match closure_kind {
|
2016-02-12 16:44:27 +01:00
|
|
|
ty::ClosureKind::Fn | ty::ClosureKind::FnMut => true,
|
|
|
|
ty::ClosureKind::FnOnce => false,
|
2015-02-15 15:09:26 -05:00
|
|
|
};
|
2016-03-06 16:30:21 +02:00
|
|
|
|
|
|
|
let llfnpointer = match bare_fn_ty.sty {
|
|
|
|
ty::TyFnDef(def_id, substs, _) => {
|
|
|
|
// Function definitions have to be turned into a pointer.
|
|
|
|
let llfn = Callee::def(ccx, def_id, substs).reify(ccx).val;
|
|
|
|
if !is_by_ref {
|
|
|
|
// A by-value fn item is ignored, so the shim has
|
|
|
|
// the same signature as the original function.
|
|
|
|
return llfn;
|
|
|
|
}
|
|
|
|
Some(llfn)
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
|
2015-02-15 15:09:26 -05:00
|
|
|
let bare_fn_ty_maybe_ref = if is_by_ref {
|
2015-06-25 04:09:46 +03:00
|
|
|
tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), bare_fn_ty)
|
2015-02-15 15:09:26 -05:00
|
|
|
} else {
|
|
|
|
bare_fn_ty
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if we already trans'd this shim.
|
|
|
|
match ccx.fn_pointer_shims().borrow().get(&bare_fn_ty_maybe_ref) {
|
2014-12-03 20:23:15 -05:00
|
|
|
Some(&llval) => { return llval; }
|
|
|
|
None => { }
|
|
|
|
}
|
|
|
|
|
2015-06-18 20:25:05 +03:00
|
|
|
debug!("trans_fn_pointer_shim(bare_fn_ty={:?})",
|
|
|
|
bare_fn_ty);
|
2014-12-01 09:23:40 -05:00
|
|
|
|
|
|
|
// Construct the "tuply" version of `bare_fn_ty`. It takes two arguments: `self`,
|
|
|
|
// which is the fn pointer, and `args`, which is the arguments tuple.
|
2016-03-06 17:32:47 +02:00
|
|
|
let sig = match bare_fn_ty.sty {
|
|
|
|
ty::TyFnDef(_, _,
|
|
|
|
&ty::BareFnTy { unsafety: hir::Unsafety::Normal,
|
|
|
|
abi: Abi::Rust,
|
|
|
|
ref sig }) |
|
|
|
|
ty::TyFnPtr(&ty::BareFnTy { unsafety: hir::Unsafety::Normal,
|
|
|
|
abi: Abi::Rust,
|
|
|
|
ref sig }) => sig,
|
2014-12-01 09:23:40 -05:00
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
_ => {
|
2016-03-29 01:46:02 +02:00
|
|
|
bug!("trans_fn_pointer_shim invoked on invalid type: {}",
|
|
|
|
bare_fn_ty);
|
2016-03-06 17:32:47 +02:00
|
|
|
}
|
|
|
|
};
|
2015-06-25 23:42:17 +03:00
|
|
|
let sig = tcx.erase_late_bound_regions(sig);
|
2016-03-11 02:33:20 +02:00
|
|
|
let sig = ccx.tcx().normalize_associated_type(&sig);
|
2015-06-25 04:09:46 +03:00
|
|
|
let tuple_input_ty = tcx.mk_tup(sig.inputs.to_vec());
|
2016-03-06 16:30:21 +02:00
|
|
|
let sig = ty::FnSig {
|
|
|
|
inputs: vec![bare_fn_ty_maybe_ref,
|
|
|
|
tuple_input_ty],
|
|
|
|
output: sig.output,
|
|
|
|
variadic: false
|
|
|
|
};
|
|
|
|
let fn_ty = FnType::new(ccx, Abi::RustCall, &sig, &[]);
|
2016-04-29 08:30:54 +03:00
|
|
|
let tuple_fn_ty = tcx.mk_fn_ptr(tcx.mk_bare_fn(ty::BareFnTy {
|
2015-06-13 13:15:03 -07:00
|
|
|
unsafety: hir::Unsafety::Normal,
|
|
|
|
abi: Abi::RustCall,
|
2016-03-06 16:30:21 +02:00
|
|
|
sig: ty::Binder(sig)
|
2016-04-29 08:30:54 +03:00
|
|
|
}));
|
2015-06-18 20:25:05 +03:00
|
|
|
debug!("tuple_fn_ty: {:?}", tuple_fn_ty);
|
2014-12-01 09:23:40 -05:00
|
|
|
|
|
|
|
//
|
2016-02-14 18:08:08 -05:00
|
|
|
let function_name =
|
|
|
|
symbol_names::internal_name_from_type_and_suffix(ccx,
|
|
|
|
bare_fn_ty,
|
|
|
|
"fn_pointer_shim");
|
2016-02-23 21:57:22 +02:00
|
|
|
let llfn = declare::define_internal_fn(ccx, &function_name, tuple_fn_ty);
|
2016-05-27 13:27:56 -04:00
|
|
|
attributes::set_frame_pointer_elimination(ccx, llfn);
|
2014-12-01 09:23:40 -05:00
|
|
|
//
|
2015-01-26 13:38:33 +01:00
|
|
|
let (block_arena, fcx): (TypedArena<_>, FunctionContext);
|
|
|
|
block_arena = TypedArena::new();
|
2016-04-06 08:34:03 +03:00
|
|
|
fcx = FunctionContext::new(ccx, llfn, fn_ty, None, &block_arena);
|
2016-03-08 14:38:13 +02:00
|
|
|
let mut bcx = fcx.init(false, None);
|
2014-12-01 09:23:40 -05:00
|
|
|
|
2015-06-18 20:07:36 +02:00
|
|
|
let llargs = get_params(fcx.llfn);
|
|
|
|
|
2016-03-06 16:30:21 +02:00
|
|
|
let self_idx = fcx.fn_ty.ret.is_indirect() as usize;
|
|
|
|
let llfnpointer = llfnpointer.unwrap_or_else(|| {
|
2016-03-06 17:32:47 +02:00
|
|
|
// the first argument (`self`) will be ptr to the fn pointer
|
2016-03-06 16:30:21 +02:00
|
|
|
if is_by_ref {
|
2016-03-06 17:32:47 +02:00
|
|
|
Load(bcx, llargs[self_idx])
|
|
|
|
} else {
|
|
|
|
llargs[self_idx]
|
|
|
|
}
|
2016-03-06 16:30:21 +02:00
|
|
|
});
|
2014-12-01 09:23:40 -05:00
|
|
|
|
|
|
|
assert!(!fcx.needs_ret_allocas);
|
|
|
|
|
|
|
|
let dest = fcx.llretslotptr.get().map(|_|
|
2016-03-06 16:30:21 +02:00
|
|
|
expr::SaveIn(fcx.get_ret_slot(bcx, "ret_slot"))
|
2014-12-01 09:23:40 -05:00
|
|
|
);
|
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
let callee = Callee {
|
|
|
|
data: Fn(llfnpointer),
|
|
|
|
ty: bare_fn_ty
|
|
|
|
};
|
|
|
|
bcx = callee.call(bcx, DebugLoc::None, ArgVals(&llargs[(self_idx + 1)..]), dest).bcx;
|
2014-12-01 09:23:40 -05:00
|
|
|
|
2016-03-06 16:30:21 +02:00
|
|
|
fcx.finish(bcx, DebugLoc::None);
|
2014-12-01 09:23:40 -05:00
|
|
|
|
2015-02-15 15:09:26 -05:00
|
|
|
ccx.fn_pointer_shims().borrow_mut().insert(bare_fn_ty_maybe_ref, llfn);
|
2014-12-03 20:23:15 -05:00
|
|
|
|
2014-12-01 09:23:40 -05:00
|
|
|
llfn
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
/// Translates a reference to a fn/method item, monomorphizing and
|
|
|
|
/// inlining as it goes.
|
|
|
|
///
|
|
|
|
/// # Parameters
|
|
|
|
///
|
2015-01-04 18:47:58 +02:00
|
|
|
/// - `ccx`: the crate context
|
2014-11-25 21:17:11 -05:00
|
|
|
/// - `def_id`: def id of the fn or method item being referenced
|
|
|
|
/// - `substs`: values for each of the fn/method's parameters
|
2016-02-23 21:21:50 +02:00
|
|
|
fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: &'tcx subst::Substs<'tcx>)
|
|
|
|
-> Datum<'tcx, Rvalue> {
|
2015-01-04 18:47:58 +02:00
|
|
|
let tcx = ccx.tcx();
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-02-23 21:21:50 +02:00
|
|
|
debug!("get_fn(def_id={:?}, substs={:?})", def_id, substs);
|
2012-09-10 12:25:45 -07:00
|
|
|
|
2015-06-24 02:54:32 +03:00
|
|
|
assert!(!substs.types.needs_infer());
|
|
|
|
assert!(!substs.types.has_escaping_regions());
|
2013-03-21 08:34:18 -04:00
|
|
|
|
2012-08-28 15:54:45 -07:00
|
|
|
// Check whether this fn has an inlined copy and, if so, redirect
|
|
|
|
// def_id to the local id of the inlined copy.
|
2014-09-06 01:39:51 +01:00
|
|
|
let def_id = inline::maybe_instantiate_inline(ccx, def_id);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
fn is_named_tuple_constructor(tcx: TyCtxt, def_id: DefId) -> bool {
|
2015-09-04 13:52:28 -04:00
|
|
|
let node_id = match tcx.map.as_local_node_id(def_id) {
|
|
|
|
Some(n) => n,
|
|
|
|
None => { return false; }
|
|
|
|
};
|
2015-12-14 18:15:39 +13:00
|
|
|
let map_node = errors::expect(
|
|
|
|
&tcx.sess.diagnostic(),
|
2015-09-04 13:52:28 -04:00
|
|
|
tcx.map.find(node_id),
|
2014-07-10 10:59:52 -07:00
|
|
|
|| "local item should be in ast map".to_string());
|
|
|
|
|
|
|
|
match map_node {
|
2015-10-02 00:03:22 +03:00
|
|
|
hir_map::NodeVariant(v) => {
|
2015-10-08 23:45:46 +03:00
|
|
|
v.node.data.is_tuple()
|
2015-10-02 00:03:22 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir_map::NodeStructCtor(_) => true,
|
2014-07-10 10:59:52 -07:00
|
|
|
_ => false
|
|
|
|
}
|
2015-09-30 22:28:27 +03:00
|
|
|
}
|
|
|
|
let must_monomorphise =
|
|
|
|
!substs.types.is_empty() || is_named_tuple_constructor(tcx, def_id);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-02-23 21:21:50 +02:00
|
|
|
debug!("get_fn({:?}) must_monomorphise: {}",
|
2015-06-18 20:25:05 +03:00
|
|
|
def_id, must_monomorphise);
|
2015-06-03 11:31:23 +12:00
|
|
|
|
2014-04-21 00:49:39 -04:00
|
|
|
// Create a monomorphic version of generic functions
|
2012-08-28 15:54:45 -07:00
|
|
|
if must_monomorphise {
|
|
|
|
// Should be either intra-crate or inlined.
|
2015-08-16 06:32:28 -04:00
|
|
|
assert_eq!(def_id.krate, LOCAL_CRATE);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
let substs = tcx.mk_substs(substs.clone().erase_regions());
|
2016-02-23 22:04:51 +02:00
|
|
|
let (val, fn_ty) = monomorphize::monomorphic_fn(ccx, def_id, substs);
|
2016-03-06 17:32:47 +02:00
|
|
|
let fn_ptr_ty = match fn_ty.sty {
|
|
|
|
ty::TyFnDef(_, _, fty) => {
|
|
|
|
// Create a fn pointer with the substituted signature.
|
2016-04-29 08:30:54 +03:00
|
|
|
tcx.mk_fn_ptr(fty)
|
2016-03-06 17:32:47 +02:00
|
|
|
}
|
2016-03-29 01:46:02 +02:00
|
|
|
_ => bug!("expected fn item type, found {}", fn_ty)
|
2016-03-06 17:32:47 +02:00
|
|
|
};
|
2016-02-23 22:04:51 +02:00
|
|
|
assert_eq!(type_of::type_of(ccx, fn_ptr_ty), common::val_ty(val));
|
2016-03-06 17:32:47 +02:00
|
|
|
return immediate_rvalue(val, fn_ptr_ty);
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the actual function pointer.
|
2016-02-23 22:04:51 +02:00
|
|
|
let ty = ccx.tcx().lookup_item_type(def_id).ty;
|
|
|
|
let fn_ptr_ty = match ty.sty {
|
2016-04-29 08:30:54 +03:00
|
|
|
ty::TyFnDef(_, _, ref fty) => {
|
2016-02-23 22:04:51 +02:00
|
|
|
// Create a fn pointer with the normalized signature.
|
2016-03-11 02:33:20 +02:00
|
|
|
tcx.mk_fn_ptr(tcx.normalize_associated_type(fty))
|
2016-02-23 22:04:51 +02:00
|
|
|
}
|
2016-03-29 01:46:02 +02:00
|
|
|
_ => bug!("expected fn item type, found {}", ty)
|
2016-02-23 22:04:51 +02:00
|
|
|
};
|
|
|
|
|
2016-05-11 17:11:20 -04:00
|
|
|
let instance = Instance::mono(ccx.shared(), def_id);
|
2016-02-23 22:04:51 +02:00
|
|
|
if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
|
|
|
|
return immediate_rvalue(llfn, fn_ptr_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
let local_id = ccx.tcx().map.as_local_node_id(def_id);
|
2016-05-12 19:52:38 +03:00
|
|
|
let local_item = match local_id.and_then(|id| tcx.map.find(id)) {
|
2016-02-23 22:04:51 +02:00
|
|
|
Some(hir_map::NodeItem(&hir::Item {
|
2016-05-12 19:52:38 +03:00
|
|
|
span, node: hir::ItemFn(..), ..
|
2016-02-23 22:04:51 +02:00
|
|
|
})) |
|
|
|
|
Some(hir_map::NodeTraitItem(&hir::TraitItem {
|
2016-05-12 19:52:38 +03:00
|
|
|
span, node: hir::MethodTraitItem(_, Some(_)), ..
|
2016-02-23 22:04:51 +02:00
|
|
|
})) |
|
|
|
|
Some(hir_map::NodeImplItem(&hir::ImplItem {
|
2016-05-12 19:52:38 +03:00
|
|
|
span, node: hir::ImplItemKind::Method(..), ..
|
2016-02-23 22:04:51 +02:00
|
|
|
})) => {
|
2016-05-12 19:52:38 +03:00
|
|
|
Some(span)
|
2016-02-23 22:04:51 +02:00
|
|
|
}
|
2016-05-12 19:52:38 +03:00
|
|
|
_ => None
|
2012-08-28 15:54:45 -07:00
|
|
|
};
|
|
|
|
|
2013-05-21 15:25:44 -04:00
|
|
|
// This is subtle and surprising, but sometimes we have to bitcast
|
|
|
|
// the resulting fn pointer. The reason has to do with external
|
|
|
|
// functions. If you have two crates that both bind the same C
|
|
|
|
// library, they may not use precisely the same types: for
|
|
|
|
// example, they will probably each declare their own structs,
|
|
|
|
// which are distinct types from LLVM's point of view (nominal
|
|
|
|
// types).
|
|
|
|
//
|
|
|
|
// Now, if those two crates are linked into an application, and
|
|
|
|
// they contain inlined code, you can wind up with a situation
|
|
|
|
// where both of those functions wind up being loaded into this
|
|
|
|
// application simultaneously. In that case, the same function
|
|
|
|
// (from LLVM's point of view) requires two types. But of course
|
|
|
|
// LLVM won't allow one function to have two types.
|
|
|
|
//
|
|
|
|
// What we currently do, therefore, is declare the function with
|
|
|
|
// one of the two types (whichever happens to come first) and then
|
|
|
|
// bitcast as needed when the function is referenced to make sure
|
|
|
|
// it has the type we expect.
|
|
|
|
//
|
|
|
|
// This can occur on either a crate-local or crate-external
|
|
|
|
// reference. It also occurs when testing libcore and in some
|
|
|
|
// other weird situations. Annoying.
|
2016-04-05 13:01:00 +03:00
|
|
|
|
2016-05-12 19:52:38 +03:00
|
|
|
let sym = instance.symbol_name(ccx.shared());
|
2016-02-23 22:04:51 +02:00
|
|
|
let llptrty = type_of::type_of(ccx, fn_ptr_ty);
|
2016-04-05 13:01:00 +03:00
|
|
|
let llfn = if let Some(llfn) = declare::get_declared_value(ccx, &sym) {
|
2016-05-12 19:52:38 +03:00
|
|
|
if let Some(span) = local_item {
|
|
|
|
if declare::get_defined_value(ccx, &sym).is_some() {
|
|
|
|
ccx.sess().span_fatal(span,
|
|
|
|
&format!("symbol `{}` is already defined", sym));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 13:01:00 +03:00
|
|
|
if common::val_ty(llfn) != llptrty {
|
|
|
|
if local_item.is_some() {
|
|
|
|
bug!("symbol `{}` previously declared as {:?}, now wanted as {:?}",
|
|
|
|
sym, Value(llfn), llptrty);
|
|
|
|
}
|
|
|
|
debug!("get_fn: casting {:?} to {:?}", llfn, llptrty);
|
|
|
|
consts::ptrcast(llfn, llptrty)
|
|
|
|
} else {
|
|
|
|
debug!("get_fn: not casting pointer!");
|
|
|
|
llfn
|
|
|
|
}
|
2014-05-28 22:26:56 -07:00
|
|
|
} else {
|
2016-04-05 13:01:00 +03:00
|
|
|
let llfn = declare::declare_fn(ccx, &sym, ty);
|
|
|
|
assert_eq!(common::val_ty(llfn), llptrty);
|
2016-02-23 22:04:51 +02:00
|
|
|
debug!("get_fn: not casting pointer!");
|
2016-04-05 13:01:00 +03:00
|
|
|
|
2016-05-12 19:52:38 +03:00
|
|
|
let attrs = ccx.tcx().get_attrs(def_id);
|
|
|
|
attributes::from_fn_attrs(ccx, &attrs, llfn);
|
2016-04-14 08:39:23 +03:00
|
|
|
if local_item.is_some() {
|
2016-04-05 13:01:00 +03:00
|
|
|
// FIXME(eddyb) Doubt all extern fn should allow unwinding.
|
|
|
|
attributes::unwind(llfn, true);
|
|
|
|
}
|
|
|
|
|
2016-02-23 22:04:51 +02:00
|
|
|
llfn
|
|
|
|
};
|
|
|
|
|
|
|
|
ccx.instances().borrow_mut().insert(instance, llfn);
|
2013-05-21 15:25:44 -04:00
|
|
|
|
2016-02-23 22:04:51 +02:00
|
|
|
immediate_rvalue(llfn, fn_ptr_ty)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ______________________________________________________________________
|
|
|
|
// Translating calls
|
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
fn trans_call_inner<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
|
|
|
|
debug_loc: DebugLoc,
|
|
|
|
callee: Callee<'tcx>,
|
|
|
|
args: CallArgs<'a, 'tcx>,
|
|
|
|
dest: Option<expr::Dest>)
|
|
|
|
-> Result<'blk, 'tcx> {
|
2014-01-15 14:39:08 -05:00
|
|
|
// Introduce a temporary cleanup scope that will contain cleanups
|
|
|
|
// for the arguments while they are being evaluated. The purpose
|
2014-10-09 15:17:22 -04:00
|
|
|
// this cleanup is to ensure that, should a panic occur while
|
2014-01-15 14:39:08 -05:00
|
|
|
// evaluating argument N, the values for arguments 0...N-1 are all
|
2014-10-09 15:17:22 -04:00
|
|
|
// cleaned up. If no panic occurs, the values are handed off to
|
2014-01-15 14:39:08 -05:00
|
|
|
// the callee, and hence none of the cleanups in this temporary
|
|
|
|
// scope will ever execute.
|
|
|
|
let fcx = bcx.fcx;
|
|
|
|
let ccx = fcx.ccx;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
let abi = callee.ty.fn_abi();
|
|
|
|
let sig = callee.ty.fn_sig();
|
|
|
|
let output = bcx.tcx().erase_late_bound_regions(&sig.output());
|
2016-03-11 02:33:20 +02:00
|
|
|
let output = bcx.tcx().normalize_associated_type(&output);
|
2016-03-06 13:23:20 +02:00
|
|
|
|
|
|
|
let extra_args = match args {
|
|
|
|
ArgExprs(args) if abi != Abi::RustCall => {
|
|
|
|
args[sig.0.inputs.len()..].iter().map(|expr| {
|
|
|
|
common::expr_ty_adjusted(bcx, expr)
|
|
|
|
}).collect()
|
2015-01-06 05:03:42 -05:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
_ => vec![]
|
2014-07-09 15:31:45 -07:00
|
|
|
};
|
2016-03-06 13:23:20 +02:00
|
|
|
let fn_ty = callee.direct_fn_type(ccx, &extra_args);
|
2014-07-09 15:31:45 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
let mut callee = match callee.data {
|
2016-02-23 22:00:59 +02:00
|
|
|
Intrinsic => {
|
2016-02-05 13:13:36 +01:00
|
|
|
assert!(abi == Abi::RustIntrinsic || abi == Abi::PlatformIntrinsic);
|
2014-07-09 15:31:45 -07:00
|
|
|
assert!(dest.is_some());
|
2014-01-15 14:39:08 -05:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
return intrinsic::trans_intrinsic_call(bcx, callee.ty, &fn_ty,
|
2016-03-08 14:40:37 +02:00
|
|
|
args, dest.unwrap(),
|
|
|
|
debug_loc);
|
2014-07-09 15:31:45 -07:00
|
|
|
}
|
2015-07-03 05:22:54 +03:00
|
|
|
NamedTupleConstructor(disr) => {
|
2014-07-09 23:42:08 -07:00
|
|
|
assert!(dest.is_some());
|
|
|
|
|
debuginfo: Make sure that all calls to drop glue are associated with debug locations.
This commit makes rustc emit debug locations for all call
and invoke statements in LLVM IR, if they are contained
within a function that debuginfo is enabled for. This is
important because LLVM does not handle the case where a
function body containing debuginfo is inlined into another
function with debuginfo, but the inlined call statement
does not have a debug location. In this case, LLVM will
not know where (in terms of source code coordinates) the
function was inlined to and we end up with some statements
still linked to the source locations in there original,
non-inlined function without any indication that they are
indeed an inline-copy. Later, when generating DWARF from
the IR, LLVM will interpret this as corrupt IR and abort.
Unfortunately, the undesirable case described above can
still occur when using LTO. If there is a crate compiled
without debuginfo calling into a crate compiled with
debuginfo, we again end up with the conditions triggering
the error. This is why some LTO tests still fail with the
dreaded assertion, if the standard library was built with
debuginfo enabled.
That is, `RUSTFLAGS_STAGE2=-g make rustc-stage2` will
succeed but `RUSTFLAGS_STAGE2=-g make check` will still
fail after this commit has been merged. This is a problem
that has to be dealt with separately.
Fixes #17201
Fixes #15816
Fixes #15156
2014-09-24 08:49:38 +02:00
|
|
|
return base::trans_named_tuple_constructor(bcx,
|
2015-07-03 05:22:54 +03:00
|
|
|
callee.ty,
|
debuginfo: Make sure that all calls to drop glue are associated with debug locations.
This commit makes rustc emit debug locations for all call
and invoke statements in LLVM IR, if they are contained
within a function that debuginfo is enabled for. This is
important because LLVM does not handle the case where a
function body containing debuginfo is inlined into another
function with debuginfo, but the inlined call statement
does not have a debug location. In this case, LLVM will
not know where (in terms of source code coordinates) the
function was inlined to and we end up with some statements
still linked to the source locations in there original,
non-inlined function without any indication that they are
indeed an inline-copy. Later, when generating DWARF from
the IR, LLVM will interpret this as corrupt IR and abort.
Unfortunately, the undesirable case described above can
still occur when using LTO. If there is a crate compiled
without debuginfo calling into a crate compiled with
debuginfo, we again end up with the conditions triggering
the error. This is why some LTO tests still fail with the
dreaded assertion, if the standard library was built with
debuginfo enabled.
That is, `RUSTFLAGS_STAGE2=-g make rustc-stage2` will
succeed but `RUSTFLAGS_STAGE2=-g make check` will still
fail after this commit has been merged. This is a problem
that has to be dealt with separately.
Fixes #17201
Fixes #15816
Fixes #15156
2014-09-24 08:49:38 +02:00
|
|
|
disr,
|
|
|
|
args,
|
|
|
|
dest.unwrap(),
|
2015-02-04 17:16:59 +01:00
|
|
|
debug_loc);
|
2014-07-09 23:42:08 -07:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
f => f
|
|
|
|
};
|
2014-07-28 08:23:16 +02:00
|
|
|
|
2014-01-15 14:39:08 -05:00
|
|
|
// Generate a location to store the result. If the user does
|
|
|
|
// not care about the result, just make a stack slot.
|
2014-10-24 21:14:37 +02:00
|
|
|
let opt_llretslot = dest.and_then(|dest| match dest {
|
|
|
|
expr::SaveIn(dst) => Some(dst),
|
|
|
|
expr::Ignore => {
|
2016-03-06 13:23:20 +02:00
|
|
|
let needs_drop = || match output {
|
|
|
|
ty::FnConverging(ret_ty) => bcx.fcx.type_needs_drop(ret_ty),
|
|
|
|
ty::FnDiverging => false
|
2014-10-24 21:14:37 +02:00
|
|
|
};
|
2016-03-06 13:23:20 +02:00
|
|
|
if fn_ty.ret.is_indirect() || fn_ty.ret.cast.is_some() || needs_drop() {
|
2014-10-24 21:14:37 +02:00
|
|
|
// Push the out-pointer if we use an out-pointer for this
|
|
|
|
// return type, otherwise push "undef".
|
2016-03-06 13:23:20 +02:00
|
|
|
if fn_ty.ret.is_ignore() {
|
|
|
|
Some(C_undef(fn_ty.ret.original_ty.ptr_to()))
|
2014-10-24 21:14:37 +02:00
|
|
|
} else {
|
2016-03-06 13:23:20 +02:00
|
|
|
let llresult = alloca(bcx, fn_ty.ret.original_ty, "__llret");
|
2015-08-25 18:24:16 +02:00
|
|
|
call_lifetime_start(bcx, llresult);
|
|
|
|
Some(llresult)
|
2014-10-24 21:14:37 +02:00
|
|
|
}
|
2014-01-15 14:39:08 -05:00
|
|
|
} else {
|
2014-10-24 21:14:37 +02:00
|
|
|
None
|
2013-05-21 15:25:44 -04:00
|
|
|
}
|
2014-01-15 14:39:08 -05:00
|
|
|
}
|
2014-10-24 21:14:37 +02:00
|
|
|
});
|
2013-03-19 14:40:34 -04:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
// If there no destination, return must be direct, with no cast.
|
|
|
|
if opt_llretslot.is_none() {
|
|
|
|
assert!(!fn_ty.ret.is_indirect() && fn_ty.ret.cast.is_none());
|
|
|
|
}
|
2013-04-18 15:53:29 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
let mut llargs = Vec::new();
|
2016-03-06 17:32:47 +02:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
if fn_ty.ret.is_indirect() {
|
|
|
|
let mut llretslot = opt_llretslot.unwrap();
|
|
|
|
if let Some(ty) = fn_ty.ret.cast {
|
|
|
|
llretslot = PointerCast(bcx, llretslot, ty.ptr_to());
|
2014-01-15 14:39:08 -05:00
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
llargs.push(llretslot);
|
|
|
|
}
|
2013-05-21 15:25:44 -04:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
let arg_cleanup_scope = fcx.push_custom_cleanup_scope();
|
|
|
|
bcx = trans_args(bcx, abi, &fn_ty, &mut callee, args, &mut llargs,
|
|
|
|
cleanup::CustomScope(arg_cleanup_scope));
|
|
|
|
fcx.scopes.borrow_mut().last_mut().unwrap().drop_non_lifetime_clean();
|
2016-02-26 01:10:40 +02:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
let llfn = match callee {
|
|
|
|
Fn(f) => f,
|
2016-03-29 01:46:02 +02:00
|
|
|
_ => bug!("expected fn pointer callee, found {:?}", callee)
|
2016-03-06 13:23:20 +02:00
|
|
|
};
|
2016-02-26 01:10:40 +02:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
let (llret, mut bcx) = base::invoke(bcx, llfn, &llargs, debug_loc);
|
|
|
|
if !bcx.unreachable.get() {
|
|
|
|
fn_ty.apply_attrs_callsite(llret);
|
|
|
|
}
|
2014-01-15 14:39:08 -05:00
|
|
|
|
2016-03-06 13:23:20 +02: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.
|
|
|
|
if !fn_ty.ret.is_ignore() && !fn_ty.ret.is_indirect() {
|
|
|
|
if let Some(llforeign_ret_ty) = fn_ty.ret.cast {
|
|
|
|
let llrust_ret_ty = fn_ty.ret.original_ty;
|
|
|
|
let llretslot = opt_llretslot.unwrap();
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
base::call_lifetime_start(bcx, llscratch);
|
|
|
|
Store(bcx, llret, llscratch);
|
|
|
|
let llscratch_i8 = PointerCast(bcx, llscratch, Type::i8(ccx).ptr_to());
|
|
|
|
let llretptr_i8 = PointerCast(bcx, llretslot, Type::i8(ccx).ptr_to());
|
|
|
|
let llrust_size = llsize_of_store(ccx, llrust_ret_ty);
|
|
|
|
let llforeign_align = llalign_of_min(ccx, llforeign_ret_ty);
|
|
|
|
let llrust_align = llalign_of_min(ccx, llrust_ret_ty);
|
|
|
|
let llalign = cmp::min(llforeign_align, llrust_align);
|
|
|
|
debug!("llrust_size={}", llrust_size);
|
2016-03-08 14:29:46 +02:00
|
|
|
|
|
|
|
if !bcx.unreachable.get() {
|
|
|
|
base::call_memcpy(&B(bcx), llretptr_i8, llscratch_i8,
|
|
|
|
C_uint(ccx, llrust_size), llalign as u32);
|
|
|
|
}
|
2016-03-06 13:23:20 +02:00
|
|
|
base::call_lifetime_end(bcx, llscratch);
|
|
|
|
} else if let Some(llretslot) = opt_llretslot {
|
|
|
|
base::store_ty(bcx, llret, llretslot, output.unwrap());
|
2012-10-26 18:23:45 -07:00
|
|
|
}
|
2014-01-15 14:39:08 -05:00
|
|
|
}
|
2013-04-18 15:53:29 -07:00
|
|
|
|
2014-10-13 16:12:38 +02:00
|
|
|
fcx.pop_and_trans_custom_cleanup_scope(bcx, arg_cleanup_scope);
|
|
|
|
|
2014-01-15 14:39:08 -05:00
|
|
|
// If the caller doesn't care about the result of this fn call,
|
|
|
|
// drop the temporary slot we made.
|
2016-03-06 13:23:20 +02:00
|
|
|
match (dest, opt_llretslot, output) {
|
2014-10-24 21:14:37 +02:00
|
|
|
(Some(expr::Ignore), Some(llretslot), ty::FnConverging(ret_ty)) => {
|
2014-01-15 14:39:08 -05:00
|
|
|
// drop the value if it is not being saved.
|
2016-03-06 13:23:20 +02:00
|
|
|
bcx = glue::drop_ty(bcx, llretslot, ret_ty, debug_loc);
|
2014-07-28 08:41:44 +02:00
|
|
|
call_lifetime_end(bcx, llretslot);
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
2014-07-28 08:23:16 +02:00
|
|
|
_ => {}
|
2014-01-15 14:39:08 -05:00
|
|
|
}
|
2013-05-21 15:25:44 -04:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
if output == ty::FnDiverging {
|
2014-10-28 18:32:05 +01:00
|
|
|
Unreachable(bcx);
|
2014-01-15 14:39:08 -05:00
|
|
|
}
|
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
Result::new(bcx, llret)
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
pub enum CallArgs<'a, 'tcx> {
|
2016-03-06 17:32:47 +02:00
|
|
|
/// Supply value of arguments as a list of expressions that must be
|
|
|
|
/// translated. This is used in the common case of `foo(bar, qux)`.
|
2015-07-31 00:04:06 -07:00
|
|
|
ArgExprs(&'a [P<hir::Expr>]),
|
2014-03-08 18:33:39 +02:00
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
/// Supply value of arguments as a list of LLVM value refs; frequently
|
|
|
|
/// used with lang items and so forth, when the argument is an internal
|
|
|
|
/// value.
|
2014-03-08 18:33:39 +02:00
|
|
|
ArgVals(&'a [ValueRef]),
|
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
/// For overloaded operators: `(lhs, Option(rhs))`.
|
|
|
|
/// `lhs` is the left-hand-side and `rhs` is the datum
|
|
|
|
/// of the right-hand-side argument (if any).
|
|
|
|
ArgOverloadedOp(Datum<'tcx, Expr>, Option<Datum<'tcx, Expr>>),
|
2014-05-28 22:26:56 -07:00
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
/// Supply value of arguments as a list of expressions that must be
|
|
|
|
/// translated, for overloaded call operators.
|
2015-07-31 00:04:06 -07:00
|
|
|
ArgOverloadedCall(Vec<&'a hir::Expr>),
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2014-09-06 19:13:04 +03:00
|
|
|
fn trans_args_under_call_abi<'blk, 'tcx>(
|
|
|
|
mut bcx: Block<'blk, 'tcx>,
|
2015-07-31 00:04:06 -07:00
|
|
|
arg_exprs: &[P<hir::Expr>],
|
2016-03-06 13:23:20 +02:00
|
|
|
callee: &mut CalleeData,
|
|
|
|
fn_ty: &FnType,
|
2014-05-28 22:26:56 -07:00
|
|
|
llargs: &mut Vec<ValueRef>,
|
2016-03-06 17:32:47 +02:00
|
|
|
arg_cleanup_scope: cleanup::ScopeId)
|
2015-01-06 05:03:42 -05:00
|
|
|
-> Block<'blk, 'tcx>
|
|
|
|
{
|
2016-03-06 13:23:20 +02:00
|
|
|
let mut arg_idx = 0;
|
2015-01-06 05:03:42 -05:00
|
|
|
|
2014-05-28 22:26:56 -07:00
|
|
|
// Translate the `self` argument first.
|
2016-03-06 17:32:47 +02:00
|
|
|
let arg_datum = unpack_datum!(bcx, expr::trans(bcx, &arg_exprs[0]));
|
|
|
|
bcx = trans_arg_datum(bcx,
|
|
|
|
arg_datum,
|
2016-03-06 13:23:20 +02:00
|
|
|
callee, fn_ty, &mut arg_idx,
|
2016-03-06 17:32:47 +02:00
|
|
|
arg_cleanup_scope,
|
|
|
|
llargs);
|
2014-05-28 22:26:56 -07:00
|
|
|
|
|
|
|
// Now untuple the rest of the arguments.
|
2014-09-07 20:09:06 +03:00
|
|
|
let tuple_expr = &arg_exprs[1];
|
2015-02-04 17:16:59 +01:00
|
|
|
let tuple_type = common::node_id_type(bcx, tuple_expr.id);
|
2014-05-28 22:26:56 -07:00
|
|
|
|
2014-10-31 10:51:16 +02:00
|
|
|
match tuple_type.sty {
|
2015-06-11 16:21:46 -07:00
|
|
|
ty::TyTuple(ref field_types) => {
|
2014-05-28 22:26:56 -07:00
|
|
|
let tuple_datum = unpack_datum!(bcx,
|
2016-02-09 21:24:11 +01:00
|
|
|
expr::trans(bcx, &tuple_expr));
|
2014-05-28 22:26:56 -07:00
|
|
|
let tuple_lvalue_datum =
|
|
|
|
unpack_datum!(bcx,
|
|
|
|
tuple_datum.to_lvalue_datum(bcx,
|
|
|
|
"args",
|
|
|
|
tuple_expr.id));
|
|
|
|
let repr = adt::represent_type(bcx.ccx(), tuple_type);
|
2016-02-09 21:24:11 +01:00
|
|
|
let repr_ptr = &repr;
|
2015-06-18 21:16:47 +02:00
|
|
|
for (i, field_type) in field_types.iter().enumerate() {
|
2014-05-28 22:26:56 -07:00
|
|
|
let arg_datum = tuple_lvalue_datum.get_element(
|
2014-08-06 11:59:40 +02:00
|
|
|
bcx,
|
2015-02-28 21:41:26 +01:00
|
|
|
field_type,
|
2014-05-28 22:26:56 -07:00
|
|
|
|srcval| {
|
2016-01-16 16:03:09 +01:00
|
|
|
adt::trans_field_ptr(bcx, repr_ptr, srcval, Disr(0), i)
|
2015-02-28 21:41:26 +01:00
|
|
|
}).to_expr_datum();
|
2015-06-18 21:16:47 +02:00
|
|
|
bcx = trans_arg_datum(bcx,
|
|
|
|
arg_datum,
|
2016-03-06 13:23:20 +02:00
|
|
|
callee, fn_ty, &mut arg_idx,
|
2015-06-18 21:16:47 +02:00
|
|
|
arg_cleanup_scope,
|
|
|
|
llargs);
|
|
|
|
}
|
2014-05-28 22:26:56 -07:00
|
|
|
}
|
|
|
|
_ => {
|
2016-03-29 01:46:02 +02:00
|
|
|
span_bug!(tuple_expr.span,
|
|
|
|
"argument to `.call()` wasn't a tuple?!")
|
2014-05-28 22:26:56 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bcx
|
|
|
|
}
|
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
pub fn trans_args<'a, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
abi: Abi,
|
|
|
|
fn_ty: &FnType,
|
|
|
|
callee: &mut CalleeData,
|
2014-09-29 22:11:30 +03:00
|
|
|
args: CallArgs<'a, 'tcx>,
|
|
|
|
llargs: &mut Vec<ValueRef>,
|
2016-03-06 13:23:20 +02:00
|
|
|
arg_cleanup_scope: cleanup::ScopeId)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> Block<'blk, 'tcx> {
|
2014-05-28 22:26:56 -07:00
|
|
|
debug!("trans_args(abi={})", abi);
|
|
|
|
|
2013-06-17 16:23:24 +12:00
|
|
|
let _icx = push_ctxt("trans_args");
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
let mut bcx = bcx;
|
|
|
|
let mut arg_idx = 0;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
|
|
|
// First we figure out the caller's view of the types of the arguments.
|
|
|
|
// This will be needed if this is a generic call, because the callee has
|
|
|
|
// to cast her view of the arguments to the caller's view.
|
|
|
|
match args {
|
2014-01-27 14:18:36 +02:00
|
|
|
ArgExprs(arg_exprs) => {
|
2016-02-05 13:13:36 +01:00
|
|
|
if abi == Abi::RustCall {
|
2014-05-28 22:26:56 -07:00
|
|
|
// This is only used for direct calls to the `call`,
|
|
|
|
// `call_mut` or `call_once` functions.
|
2016-03-06 13:23:20 +02:00
|
|
|
return trans_args_under_call_abi(bcx,
|
|
|
|
arg_exprs, callee, fn_ty,
|
2014-05-28 22:26:56 -07:00
|
|
|
llargs,
|
2016-03-06 17:32:47 +02:00
|
|
|
arg_cleanup_scope)
|
2014-05-28 22:26:56 -07:00
|
|
|
}
|
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
for arg_expr in arg_exprs {
|
2016-02-09 21:24:11 +01:00
|
|
|
let arg_datum = unpack_datum!(bcx, expr::trans(bcx, &arg_expr));
|
2016-03-06 13:23:20 +02:00
|
|
|
bcx = trans_arg_datum(bcx,
|
|
|
|
arg_datum,
|
|
|
|
callee, fn_ty, &mut arg_idx,
|
2015-06-18 21:16:47 +02:00
|
|
|
arg_cleanup_scope,
|
|
|
|
llargs);
|
2014-01-27 14:18:36 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-28 22:26:56 -07:00
|
|
|
ArgOverloadedCall(arg_exprs) => {
|
2016-03-06 13:23:20 +02:00
|
|
|
for expr in arg_exprs {
|
|
|
|
let arg_datum =
|
|
|
|
unpack_datum!(bcx, expr::trans(bcx, expr));
|
|
|
|
bcx = trans_arg_datum(bcx,
|
|
|
|
arg_datum,
|
|
|
|
callee, fn_ty, &mut arg_idx,
|
|
|
|
arg_cleanup_scope,
|
|
|
|
llargs);
|
|
|
|
}
|
2014-05-28 22:26:56 -07:00
|
|
|
}
|
2016-03-06 17:32:47 +02:00
|
|
|
ArgOverloadedOp(lhs, rhs) => {
|
2016-03-06 13:23:20 +02:00
|
|
|
bcx = trans_arg_datum(bcx, lhs,
|
|
|
|
callee, fn_ty, &mut arg_idx,
|
2015-06-18 21:16:47 +02:00
|
|
|
arg_cleanup_scope,
|
|
|
|
llargs);
|
2014-01-27 14:18:36 +02:00
|
|
|
|
2016-03-06 17:32:47 +02:00
|
|
|
if let Some(rhs) = rhs {
|
2016-03-06 13:23:20 +02:00
|
|
|
bcx = trans_arg_datum(bcx, rhs,
|
|
|
|
callee, fn_ty, &mut arg_idx,
|
2015-06-18 21:16:47 +02:00
|
|
|
arg_cleanup_scope,
|
|
|
|
llargs);
|
2014-01-27 14:18:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ArgVals(vs) => {
|
2016-03-06 13:23:20 +02:00
|
|
|
match *callee {
|
|
|
|
Virtual(idx) => {
|
|
|
|
llargs.push(vs[0]);
|
|
|
|
|
|
|
|
let fn_ptr = meth::get_virtual_method(bcx, vs[1], idx);
|
|
|
|
let llty = fn_ty.llvm_type(bcx.ccx()).ptr_to();
|
|
|
|
*callee = Fn(PointerCast(bcx, fn_ptr, llty));
|
|
|
|
llargs.extend_from_slice(&vs[2..]);
|
|
|
|
}
|
|
|
|
_ => llargs.extend_from_slice(vs)
|
|
|
|
}
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 18:34:20 -04:00
|
|
|
bcx
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
fn trans_arg_datum<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
arg_datum: Datum<'tcx, Expr>,
|
|
|
|
callee: &mut CalleeData,
|
|
|
|
fn_ty: &FnType,
|
|
|
|
next_idx: &mut usize,
|
|
|
|
arg_cleanup_scope: cleanup::ScopeId,
|
|
|
|
llargs: &mut Vec<ValueRef>)
|
|
|
|
-> Block<'blk, 'tcx> {
|
2014-03-06 19:24:11 +02:00
|
|
|
let _icx = push_ctxt("trans_arg_datum");
|
2014-01-15 14:39:08 -05:00
|
|
|
let mut bcx = bcx;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
debug!("trans_arg_datum({:?})", arg_datum);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
let arg = &fn_ty.args[*next_idx];
|
|
|
|
*next_idx += 1;
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
// Fill padding with undef value, where applicable.
|
|
|
|
if let Some(ty) = arg.pad {
|
|
|
|
llargs.push(C_undef(ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine whether we want a by-ref datum even if not appropriate.
|
|
|
|
let want_by_ref = arg.is_indirect() || arg.cast.is_some();
|
2012-08-28 15:54:45 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
let fat_ptr = common::type_is_fat_ptr(bcx.tcx(), arg_datum.ty);
|
|
|
|
let (by_ref, val) = if fat_ptr && !bcx.fcx.type_needs_drop(arg_datum.ty) {
|
|
|
|
(true, arg_datum.val)
|
2016-03-06 17:32:47 +02:00
|
|
|
} else {
|
|
|
|
// Make this an rvalue, since we are going to be
|
|
|
|
// passing ownership.
|
|
|
|
let arg_datum = unpack_datum!(
|
|
|
|
bcx, arg_datum.to_rvalue_datum(bcx, "arg"));
|
|
|
|
|
|
|
|
// Now that arg_datum is owned, get it into the appropriate
|
|
|
|
// mode (ref vs value).
|
2016-03-06 13:23:20 +02:00
|
|
|
let arg_datum = unpack_datum!(bcx, if want_by_ref {
|
|
|
|
arg_datum.to_ref_datum(bcx)
|
|
|
|
} else {
|
|
|
|
arg_datum.to_appropriate_datum(bcx)
|
|
|
|
});
|
2016-03-06 17:32:47 +02:00
|
|
|
|
|
|
|
// Technically, ownership of val passes to the callee.
|
|
|
|
// However, we must cleanup should we panic before the
|
|
|
|
// callee is actually invoked.
|
2016-03-06 13:23:20 +02:00
|
|
|
(arg_datum.kind.is_by_ref(),
|
|
|
|
arg_datum.add_clean(bcx.fcx, arg_cleanup_scope))
|
2016-03-06 17:32:47 +02:00
|
|
|
};
|
2012-09-18 10:41:05 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
if arg.is_ignore() {
|
|
|
|
return bcx;
|
2012-09-14 12:01:43 -07:00
|
|
|
}
|
2012-09-18 10:41:05 -07:00
|
|
|
|
2016-02-18 19:49:45 +02:00
|
|
|
debug!("--- trans_arg_datum passing {:?}", Value(val));
|
2015-06-18 21:16:47 +02:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
if fat_ptr {
|
|
|
|
// Fat pointers should be passed without any transformations.
|
|
|
|
assert!(!arg.is_indirect() && arg.cast.is_none());
|
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 23:57:40 +02:00
|
|
|
llargs.push(Load(bcx, expr::get_dataptr(bcx, val)));
|
2016-03-06 13:23:20 +02:00
|
|
|
|
|
|
|
let info_arg = &fn_ty.args[*next_idx];
|
|
|
|
*next_idx += 1;
|
|
|
|
assert!(!info_arg.is_indirect() && info_arg.cast.is_none());
|
|
|
|
let info = Load(bcx, expr::get_meta(bcx, val));
|
|
|
|
|
|
|
|
if let Virtual(idx) = *callee {
|
|
|
|
// We have to grab the fn pointer from the vtable when
|
|
|
|
// handling the first argument, ensure that here.
|
|
|
|
assert_eq!(*next_idx, 2);
|
|
|
|
assert!(info_arg.is_ignore());
|
|
|
|
let fn_ptr = meth::get_virtual_method(bcx, info, idx);
|
|
|
|
let llty = fn_ty.llvm_type(bcx.ccx()).ptr_to();
|
|
|
|
*callee = Fn(PointerCast(bcx, fn_ptr, llty));
|
|
|
|
} else {
|
|
|
|
assert!(!info_arg.is_ignore());
|
|
|
|
llargs.push(info);
|
|
|
|
}
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut val = val;
|
|
|
|
if by_ref && !arg.is_indirect() {
|
|
|
|
// Have to load the argument, maybe while casting it.
|
|
|
|
if arg.original_ty == Type::i1(bcx.ccx()) {
|
|
|
|
// We store bools as i8 so we need to truncate to i1.
|
|
|
|
val = LoadRangeAssert(bcx, val, 0, 2, llvm::False);
|
|
|
|
val = Trunc(bcx, val, arg.original_ty);
|
|
|
|
} else if let Some(ty) = arg.cast {
|
|
|
|
val = Load(bcx, PointerCast(bcx, val, ty.ptr_to()));
|
|
|
|
if !bcx.unreachable.get() {
|
|
|
|
let llalign = llalign_of_min(bcx.ccx(), arg.ty);
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMSetAlignment(val, llalign);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val = Load(bcx, val);
|
|
|
|
}
|
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 23:57:40 +02:00
|
|
|
}
|
2015-06-18 21:16:47 +02:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
llargs.push(val);
|
2015-06-18 21:16:47 +02:00
|
|
|
bcx
|
2012-08-28 15:54:45 -07:00
|
|
|
}
|