2020-09-23 13:13:49 +00:00
|
|
|
//! Handling of everything related to the calling convention. Also fills `fx.local_map`.
|
|
|
|
|
2019-08-30 12:21:24 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
mod comments;
|
2019-08-30 09:58:52 +00:00
|
|
|
mod pass_mode;
|
2019-08-31 17:28:09 +00:00
|
|
|
mod returning;
|
2019-08-30 09:58:52 +00:00
|
|
|
|
2020-05-29 09:06:29 +00:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
2020-08-28 10:10:48 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2018-07-19 17:33:42 +00:00
|
|
|
|
2020-04-20 15:13:43 +00:00
|
|
|
use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};
|
2019-12-24 11:40:18 +00:00
|
|
|
|
2019-08-30 09:58:52 +00:00
|
|
|
use self::pass_mode::*;
|
2019-08-31 17:28:09 +00:00
|
|
|
use crate::prelude::*;
|
2018-09-08 16:00:06 +00:00
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) use self::returning::{can_return_to_ssa_var, codegen_return};
|
2019-08-30 10:30:57 +00:00
|
|
|
|
2020-07-23 16:07:38 +00:00
|
|
|
// Copied from https://github.com/rust-lang/rust/blob/f52c72948aa1dd718cc1f168d21c91c584c0a662/src/librustc_middle/ty/layout.rs#L2301
|
2020-08-28 10:20:24 +00:00
|
|
|
#[rustfmt::skip]
|
|
|
|
pub(crate) fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> ty::PolyFnSig<'tcx> {
|
2020-07-23 16:07:38 +00:00
|
|
|
use rustc_middle::ty::subst::Subst;
|
|
|
|
|
|
|
|
// FIXME(davidtwco,eddyb): A `ParamEnv` should be passed through to this function.
|
|
|
|
let ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
|
2020-09-05 08:38:49 +00:00
|
|
|
match *ty.kind() {
|
2020-07-23 16:07:38 +00:00
|
|
|
ty::FnDef(..) => {
|
|
|
|
// HACK(davidtwco,eddyb): This is a workaround for polymorphization considering
|
|
|
|
// parameters unused if they show up in the signature, but not in the `mir::Body`
|
|
|
|
// (i.e. due to being inside a projection that got normalized, see
|
|
|
|
// `src/test/ui/polymorphization/normalized_sig_types.rs`), and codegen not keeping
|
|
|
|
// track of a polymorphization `ParamEnv` to allow normalizing later.
|
2020-09-05 08:38:49 +00:00
|
|
|
let mut sig = match *ty.kind() {
|
2020-07-23 16:07:38 +00:00
|
|
|
ty::FnDef(def_id, substs) => tcx
|
|
|
|
.normalize_erasing_regions(tcx.param_env(def_id), tcx.fn_sig(def_id))
|
|
|
|
.subst(tcx, substs),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2019-12-05 20:00:57 +00:00
|
|
|
if let ty::InstanceDef::VtableShim(..) = instance.def {
|
|
|
|
// Modify `fn(self, ...)` to `fn(self: *mut Self, ...)`.
|
|
|
|
sig = sig.map_bound(|mut sig| {
|
|
|
|
let mut inputs_and_output = sig.inputs_and_output.to_vec();
|
|
|
|
inputs_and_output[0] = tcx.mk_mut_ptr(inputs_and_output[0]);
|
|
|
|
sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
|
|
|
|
sig
|
|
|
|
});
|
|
|
|
}
|
|
|
|
sig
|
|
|
|
}
|
|
|
|
ty::Closure(def_id, substs) => {
|
2020-03-24 12:09:44 +00:00
|
|
|
let sig = substs.as_closure().sig();
|
2019-12-05 20:00:57 +00:00
|
|
|
|
|
|
|
let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
|
2020-07-23 16:07:38 +00:00
|
|
|
sig.map_bound(|sig| {
|
|
|
|
tcx.mk_fn_sig(
|
|
|
|
std::iter::once(env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
|
|
|
|
sig.output(),
|
|
|
|
sig.c_variadic,
|
|
|
|
sig.unsafety,
|
|
|
|
sig.abi,
|
|
|
|
)
|
|
|
|
})
|
2019-12-05 20:00:57 +00:00
|
|
|
}
|
2020-04-18 13:42:49 +00:00
|
|
|
ty::Generator(_, substs, _) => {
|
2020-03-24 12:09:44 +00:00
|
|
|
let sig = substs.as_generator().poly_sig();
|
2019-12-05 20:00:57 +00:00
|
|
|
|
|
|
|
let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
|
|
|
|
let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
|
|
|
|
|
2020-09-05 08:38:49 +00:00
|
|
|
let pin_did = tcx.require_lang_item(rustc_hir::LangItem::Pin, None);
|
2019-12-05 20:00:57 +00:00
|
|
|
let pin_adt_ref = tcx.adt_def(pin_did);
|
|
|
|
let pin_substs = tcx.intern_substs(&[env_ty.into()]);
|
|
|
|
let env_ty = tcx.mk_adt(pin_adt_ref, pin_substs);
|
|
|
|
|
|
|
|
sig.map_bound(|sig| {
|
2020-09-05 08:38:49 +00:00
|
|
|
let state_did = tcx.require_lang_item(rustc_hir::LangItem::GeneratorState, None);
|
2019-12-05 20:00:57 +00:00
|
|
|
let state_adt_ref = tcx.adt_def(state_did);
|
2020-08-28 10:20:24 +00:00
|
|
|
let state_substs =
|
|
|
|
tcx.intern_substs(&[sig.yield_ty.into(), sig.return_ty.into()]);
|
2019-12-05 20:00:57 +00:00
|
|
|
let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
|
|
|
|
|
2020-04-18 13:42:49 +00:00
|
|
|
tcx.mk_fn_sig(
|
|
|
|
[env_ty, sig.resume_ty].iter(),
|
|
|
|
&ret_ty,
|
2019-12-05 20:00:57 +00:00
|
|
|
false,
|
2020-01-09 16:43:10 +00:00
|
|
|
rustc_hir::Unsafety::Normal,
|
2020-07-23 16:07:38 +00:00
|
|
|
rustc_target::spec::abi::Abi::Rust,
|
2019-12-05 20:00:57 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2020-07-23 16:07:38 +00:00
|
|
|
_ => bug!("unexpected type {:?} in Instance::fn_sig", ty),
|
2019-12-05 20:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 17:28:09 +00:00
|
|
|
fn clif_sig_from_fn_sig<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-12-17 14:03:32 +00:00
|
|
|
triple: &target_lexicon::Triple,
|
2019-08-31 17:28:09 +00:00
|
|
|
sig: FnSig<'tcx>,
|
2020-06-20 17:03:54 +00:00
|
|
|
span: Span,
|
2019-08-31 17:28:09 +00:00
|
|
|
is_vtable_fn: bool,
|
2020-01-11 15:49:42 +00:00
|
|
|
requires_caller_location: bool,
|
2019-08-31 17:28:09 +00:00
|
|
|
) -> Signature {
|
2019-08-16 16:16:24 +00:00
|
|
|
let abi = match sig.abi {
|
2020-06-16 09:36:39 +00:00
|
|
|
Abi::System => Abi::C,
|
2019-08-16 16:16:24 +00:00
|
|
|
abi => abi,
|
|
|
|
};
|
2020-04-05 11:48:26 +00:00
|
|
|
let (call_conv, inputs, output): (CallConv, Vec<Ty<'tcx>>, Ty<'tcx>) = match abi {
|
2020-08-28 10:10:48 +00:00
|
|
|
Abi::Rust => (
|
|
|
|
CallConv::triple_default(triple),
|
|
|
|
sig.inputs().to_vec(),
|
|
|
|
sig.output(),
|
|
|
|
),
|
|
|
|
Abi::C | Abi::Unadjusted => (
|
|
|
|
CallConv::triple_default(triple),
|
|
|
|
sig.inputs().to_vec(),
|
|
|
|
sig.output(),
|
|
|
|
),
|
2020-07-19 12:45:58 +00:00
|
|
|
Abi::SysV64 => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
|
2018-07-20 11:36:53 +00:00
|
|
|
Abi::RustCall => {
|
2018-07-27 17:01:38 +00:00
|
|
|
assert_eq!(sig.inputs().len(), 2);
|
2020-09-05 08:38:49 +00:00
|
|
|
let extra_args = match sig.inputs().last().unwrap().kind() {
|
2018-08-24 12:51:02 +00:00
|
|
|
ty::Tuple(ref tupled_arguments) => tupled_arguments,
|
2018-07-20 11:38:49 +00:00
|
|
|
_ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
|
|
|
|
};
|
2020-04-05 11:48:26 +00:00
|
|
|
let mut inputs: Vec<Ty<'tcx>> = vec![sig.inputs()[0]];
|
2019-04-28 18:37:44 +00:00
|
|
|
inputs.extend(extra_args.types());
|
2019-12-17 14:03:32 +00:00
|
|
|
(CallConv::triple_default(triple), inputs, sig.output())
|
2018-07-20 11:36:53 +00:00
|
|
|
}
|
2019-08-16 16:16:24 +00:00
|
|
|
Abi::System => unreachable!(),
|
2020-08-28 10:10:48 +00:00
|
|
|
Abi::RustIntrinsic => (
|
|
|
|
CallConv::triple_default(triple),
|
|
|
|
sig.inputs().to_vec(),
|
|
|
|
sig.output(),
|
|
|
|
),
|
2018-07-20 11:36:53 +00:00
|
|
|
_ => unimplemented!("unsupported abi {:?}", sig.abi),
|
2018-07-19 17:33:42 +00:00
|
|
|
};
|
2018-08-11 10:34:46 +00:00
|
|
|
|
|
|
|
let inputs = inputs
|
|
|
|
.into_iter()
|
2019-06-16 10:54:37 +00:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, ty)| {
|
|
|
|
let mut layout = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap();
|
|
|
|
if i == 0 && is_vtable_fn {
|
|
|
|
// Virtual calls turn their self param into a thin pointer.
|
2019-06-16 15:27:51 +00:00
|
|
|
// See https://github.com/rust-lang/rust/blob/37b6a5e5e82497caf5353d9d856e4eb5d14cbe06/src/librustc/ty/layout.rs#L2519-L2572 for more info
|
2019-08-31 17:28:09 +00:00
|
|
|
layout = tcx
|
|
|
|
.layout_of(ParamEnv::reveal_all().and(tcx.mk_mut_ptr(tcx.mk_unit())))
|
|
|
|
.unwrap();
|
2019-06-16 10:54:37 +00:00
|
|
|
}
|
2020-06-20 17:03:54 +00:00
|
|
|
let pass_mode = get_pass_mode(tcx, layout);
|
|
|
|
if abi != Abi::Rust && abi != Abi::RustCall && abi != Abi::RustIntrinsic {
|
|
|
|
match pass_mode {
|
|
|
|
PassMode::NoPass | PassMode::ByVal(_) => {}
|
2020-04-20 15:13:43 +00:00
|
|
|
PassMode::ByRef { size: Some(size) } => {
|
|
|
|
let purpose = ArgumentPurpose::StructArgument(u32::try_from(size.bytes()).expect("struct too big to pass on stack"));
|
|
|
|
return EmptySinglePair::Single(AbiParam::special(pointer_ty(tcx), purpose)).into_iter();
|
|
|
|
}
|
|
|
|
PassMode::ByValPair(_, _) | PassMode::ByRef { size: None } => {
|
2020-06-20 17:03:54 +00:00
|
|
|
tcx.sess.span_warn(
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"Argument of type `{:?}` with pass mode `{:?}` is not yet supported \
|
|
|
|
for non-rust abi `{}`. Calling this function may result in a crash.",
|
|
|
|
layout.ty,
|
|
|
|
pass_mode,
|
|
|
|
abi,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-20 15:13:43 +00:00
|
|
|
pass_mode.get_param_ty(tcx).map(AbiParam::new).into_iter()
|
2019-08-31 17:28:09 +00:00
|
|
|
})
|
|
|
|
.flatten();
|
2019-06-16 10:54:37 +00:00
|
|
|
|
2020-01-11 15:49:42 +00:00
|
|
|
let (mut params, returns): (Vec<_>, Vec<_>) = match get_pass_mode(
|
2019-08-31 17:28:09 +00:00
|
|
|
tcx,
|
|
|
|
tcx.layout_of(ParamEnv::reveal_all().and(output)).unwrap(),
|
|
|
|
) {
|
2020-04-20 15:13:43 +00:00
|
|
|
PassMode::NoPass => (inputs.collect(), vec![]),
|
2020-08-28 10:10:48 +00:00
|
|
|
PassMode::ByVal(ret_ty) => (inputs.collect(), vec![AbiParam::new(ret_ty)]),
|
2019-06-16 10:54:37 +00:00
|
|
|
PassMode::ByValPair(ret_ty_a, ret_ty_b) => (
|
2020-04-20 15:13:43 +00:00
|
|
|
inputs.collect(),
|
2019-06-16 10:54:37 +00:00
|
|
|
vec![AbiParam::new(ret_ty_a), AbiParam::new(ret_ty_b)],
|
|
|
|
),
|
2020-04-20 15:13:43 +00:00
|
|
|
PassMode::ByRef { size: Some(_) } => {
|
2018-08-11 10:34:46 +00:00
|
|
|
(
|
2018-09-08 15:24:52 +00:00
|
|
|
Some(pointer_ty(tcx)) // First param is place to put return val
|
|
|
|
.into_iter()
|
2020-04-20 15:13:43 +00:00
|
|
|
.map(|ty| AbiParam::special(ty, ArgumentPurpose::StructReturn))
|
2018-08-11 10:34:46 +00:00
|
|
|
.chain(inputs)
|
|
|
|
.collect(),
|
|
|
|
vec![],
|
|
|
|
)
|
|
|
|
}
|
2020-04-20 15:13:43 +00:00
|
|
|
PassMode::ByRef { size: None } => todo!(),
|
2018-08-11 10:34:46 +00:00
|
|
|
};
|
|
|
|
|
2020-01-11 15:49:42 +00:00
|
|
|
if requires_caller_location {
|
|
|
|
params.push(AbiParam::new(pointer_ty(tcx)));
|
|
|
|
}
|
|
|
|
|
2018-07-19 17:33:42 +00:00
|
|
|
Signature {
|
2018-08-11 10:34:46 +00:00
|
|
|
params,
|
|
|
|
returns,
|
2018-07-19 17:33:42 +00:00
|
|
|
call_conv,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn get_function_name_and_sig<'tcx>(
|
2019-06-16 09:13:49 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-12-17 14:03:32 +00:00
|
|
|
triple: &target_lexicon::Triple,
|
2018-08-11 11:59:08 +00:00
|
|
|
inst: Instance<'tcx>,
|
2019-02-21 14:06:09 +00:00
|
|
|
support_vararg: bool,
|
2018-08-11 11:59:08 +00:00
|
|
|
) -> (String, Signature) {
|
2020-04-13 11:30:39 +00:00
|
|
|
assert!(!inst.substs.needs_infer());
|
2020-08-28 10:10:48 +00:00
|
|
|
let fn_sig = tcx.normalize_erasing_late_bound_regions(
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
&fn_sig_for_fn_abi(tcx, inst),
|
|
|
|
);
|
2019-03-01 17:55:20 +00:00
|
|
|
if fn_sig.c_variadic && !support_vararg {
|
2020-08-28 10:10:48 +00:00
|
|
|
tcx.sess.span_fatal(
|
|
|
|
tcx.def_span(inst.def_id()),
|
|
|
|
"Variadic function definitions are not yet supported",
|
|
|
|
);
|
2019-01-02 11:20:32 +00:00
|
|
|
}
|
2020-08-28 10:10:48 +00:00
|
|
|
let sig = clif_sig_from_fn_sig(
|
|
|
|
tcx,
|
|
|
|
triple,
|
|
|
|
fn_sig,
|
|
|
|
tcx.def_span(inst.def_id()),
|
|
|
|
false,
|
|
|
|
inst.def.requires_caller_location(tcx),
|
|
|
|
);
|
2020-07-17 17:15:33 +00:00
|
|
|
(tcx.symbol_name(inst).name.to_string(), sig)
|
2018-08-11 11:59:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 11:20:32 +00:00
|
|
|
/// Instance must be monomorphized
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn import_function<'tcx>(
|
2019-06-16 09:13:49 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-10-01 08:38:23 +00:00
|
|
|
module: &mut impl Module,
|
2019-01-02 11:20:32 +00:00
|
|
|
inst: Instance<'tcx>,
|
|
|
|
) -> FuncId {
|
2019-12-17 14:03:32 +00:00
|
|
|
let (name, sig) = get_function_name_and_sig(tcx, module.isa().triple(), inst, true);
|
2019-01-02 11:20:32 +00:00
|
|
|
module
|
|
|
|
.declare_function(&name, Linkage::Import, &sig)
|
|
|
|
.unwrap()
|
|
|
|
}
|
2018-09-08 16:00:06 +00:00
|
|
|
|
2020-10-01 08:38:23 +00:00
|
|
|
impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> {
|
2018-09-08 16:00:06 +00:00
|
|
|
/// Instance must be monomorphized
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
|
2020-08-22 14:47:31 +00:00
|
|
|
let func_id = import_function(self.tcx, &mut self.cx.module, inst);
|
2019-02-21 14:06:09 +00:00
|
|
|
let func_ref = self
|
2020-08-28 10:10:48 +00:00
|
|
|
.cx
|
|
|
|
.module
|
2018-12-27 09:59:01 +00:00
|
|
|
.declare_func_in_func(func_id, &mut self.bcx.func);
|
2018-12-28 16:07:40 +00:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 11:18:40 +00:00
|
|
|
self.add_comment(func_ref, format!("{:?}", inst));
|
2018-12-28 16:07:40 +00:00
|
|
|
|
2018-12-27 09:59:01 +00:00
|
|
|
func_ref
|
2018-07-19 17:33:42 +00:00
|
|
|
}
|
2018-07-20 11:36:53 +00:00
|
|
|
|
2020-04-25 17:07:25 +00:00
|
|
|
pub(crate) fn lib_call(
|
2018-07-30 13:34:34 +00:00
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
input_tys: Vec<types::Type>,
|
2019-07-20 15:57:00 +00:00
|
|
|
output_tys: Vec<types::Type>,
|
2018-07-30 13:34:34 +00:00
|
|
|
args: &[Value],
|
2019-07-20 15:57:00 +00:00
|
|
|
) -> &[Value] {
|
2018-07-30 13:34:34 +00:00
|
|
|
let sig = Signature {
|
|
|
|
params: input_tys.iter().cloned().map(AbiParam::new).collect(),
|
2019-07-20 15:57:00 +00:00
|
|
|
returns: output_tys.iter().cloned().map(AbiParam::new).collect(),
|
2019-12-17 14:03:32 +00:00
|
|
|
call_conv: CallConv::triple_default(self.triple()),
|
2018-07-30 13:34:34 +00:00
|
|
|
};
|
2018-07-31 10:25:16 +00:00
|
|
|
let func_id = self
|
2020-08-28 10:10:48 +00:00
|
|
|
.cx
|
|
|
|
.module
|
2018-07-31 10:25:16 +00:00
|
|
|
.declare_function(&name, Linkage::Import, &sig)
|
|
|
|
.unwrap();
|
|
|
|
let func_ref = self
|
2020-08-28 10:10:48 +00:00
|
|
|
.cx
|
|
|
|
.module
|
2018-07-31 10:25:16 +00:00
|
|
|
.declare_func_in_func(func_id, &mut self.bcx.func);
|
2018-07-30 13:34:34 +00:00
|
|
|
let call_inst = self.bcx.ins().call(func_ref, args);
|
2019-08-31 17:28:09 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
2019-07-30 13:00:15 +00:00
|
|
|
self.add_comment(call_inst, format!("easy_call {}", name));
|
|
|
|
}
|
2018-07-30 13:34:34 +00:00
|
|
|
let results = self.bcx.inst_results(call_inst);
|
2019-07-20 15:57:00 +00:00
|
|
|
assert!(results.len() <= 2, "{}", results.len());
|
|
|
|
results
|
2018-07-30 13:34:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn easy_call(
|
2018-07-31 10:25:16 +00:00
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
args: &[CValue<'tcx>],
|
|
|
|
return_ty: Ty<'tcx>,
|
|
|
|
) -> CValue<'tcx> {
|
|
|
|
let (input_tys, args): (Vec<_>, Vec<_>) = args
|
2020-11-03 10:00:04 +00:00
|
|
|
.iter()
|
2018-07-31 10:25:16 +00:00
|
|
|
.map(|arg| {
|
|
|
|
(
|
2018-11-12 15:23:39 +00:00
|
|
|
self.clif_type(arg.layout().ty).unwrap(),
|
2019-01-02 12:37:56 +00:00
|
|
|
arg.load_scalar(self),
|
2018-07-31 10:25:16 +00:00
|
|
|
)
|
2018-10-10 17:07:13 +00:00
|
|
|
})
|
|
|
|
.unzip();
|
2018-07-30 13:34:34 +00:00
|
|
|
let return_layout = self.layout_of(return_ty);
|
2020-09-05 08:38:49 +00:00
|
|
|
let return_tys = if let ty::Tuple(tup) = return_ty.kind() {
|
2019-07-20 15:57:00 +00:00
|
|
|
tup.types().map(|ty| self.clif_type(ty).unwrap()).collect()
|
2018-07-30 13:58:46 +00:00
|
|
|
} else {
|
2019-07-20 15:57:00 +00:00
|
|
|
vec![self.clif_type(return_ty).unwrap()]
|
2018-07-30 13:58:46 +00:00
|
|
|
};
|
2019-07-20 15:57:00 +00:00
|
|
|
let ret_vals = self.lib_call(name, input_tys, return_tys, &args);
|
|
|
|
match *ret_vals {
|
|
|
|
[] => CValue::by_ref(
|
2020-07-14 14:38:50 +00:00
|
|
|
Pointer::const_addr(self, i64::from(self.pointer_type.bytes())),
|
2019-02-21 14:06:09 +00:00
|
|
|
return_layout,
|
2019-07-20 15:57:00 +00:00
|
|
|
),
|
|
|
|
[val] => CValue::by_val(val, return_layout),
|
|
|
|
[val, extra] => CValue::by_val_pair(val, extra, return_layout),
|
|
|
|
_ => unreachable!(),
|
2018-07-30 13:58:46 +00:00
|
|
|
}
|
2018-07-30 13:34:34 +00:00
|
|
|
}
|
2018-07-19 17:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:13:49 +00:00
|
|
|
/// Make a [`CPlace`] capable of holding value of the specified type.
|
2020-09-16 16:45:19 +00:00
|
|
|
fn make_local_place<'tcx>(
|
2020-10-01 08:38:23 +00:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Module>,
|
2018-12-25 15:47:33 +00:00
|
|
|
local: Local,
|
2020-03-30 17:00:24 +00:00
|
|
|
layout: TyAndLayout<'tcx>,
|
2018-12-25 15:47:33 +00:00
|
|
|
is_ssa: bool,
|
|
|
|
) -> CPlace<'tcx> {
|
|
|
|
let place = if is_ssa {
|
2020-07-02 22:23:21 +00:00
|
|
|
if let rustc_target::abi::Abi::ScalarPair(_, _) = layout.abi {
|
|
|
|
CPlace::new_var_pair(fx, local, layout)
|
|
|
|
} else {
|
|
|
|
CPlace::new_var(fx, local, layout)
|
|
|
|
}
|
2018-12-25 15:47:33 +00:00
|
|
|
} else {
|
2020-01-15 12:17:09 +00:00
|
|
|
CPlace::new_stack_slot(fx, layout)
|
2018-12-25 15:47:33 +00:00
|
|
|
};
|
|
|
|
|
2019-08-30 12:21:24 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
self::comments::add_local_place_comments(fx, place, local);
|
|
|
|
|
2020-09-16 16:45:19 +00:00
|
|
|
place
|
2018-12-25 15:47:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 11:48:26 +00:00
|
|
|
pub(crate) fn codegen_fn_prelude<'tcx>(
|
2020-10-01 08:38:23 +00:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Module>,
|
2020-04-05 11:48:26 +00:00
|
|
|
start_block: Block,
|
|
|
|
) {
|
2018-08-09 08:46:56 +00:00
|
|
|
let ssa_analyzed = crate::analyze::analyze(fx);
|
2018-12-28 16:07:40 +00:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
2019-08-30 13:07:15 +00:00
|
|
|
self::comments::add_args_header_comment(fx);
|
2018-12-27 09:59:01 +00:00
|
|
|
|
2020-09-16 16:45:19 +00:00
|
|
|
let ret_place = self::returning::codegen_return_param(fx, &ssa_analyzed, start_block);
|
|
|
|
assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE);
|
2019-08-30 10:30:57 +00:00
|
|
|
|
2019-03-26 18:53:04 +00:00
|
|
|
// None means pass_mode == NoPass
|
2018-12-27 09:59:01 +00:00
|
|
|
enum ArgKind<'tcx> {
|
2019-03-26 18:53:04 +00:00
|
|
|
Normal(Option<CValue<'tcx>>),
|
|
|
|
Spread(Vec<Option<CValue<'tcx>>>),
|
2018-07-27 17:27:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 10:13:07 +00:00
|
|
|
let func_params = fx
|
|
|
|
.mir
|
|
|
|
.args_iter()
|
|
|
|
.map(|local| {
|
|
|
|
let arg_ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
|
|
|
|
|
|
|
|
// Adapted from https://github.com/rust-lang/rust/blob/145155dc96757002c7b2e9de8489416e2fdbbd57/src/librustc_codegen_llvm/mir/mod.rs#L442-L482
|
|
|
|
if Some(local) == fx.mir.spread_arg {
|
|
|
|
// This argument (e.g. the last argument in the "rust-call" ABI)
|
|
|
|
// is a tuple that was spread at the ABI level and now we have
|
|
|
|
// to reconstruct it into a tuple local variable, from multiple
|
|
|
|
// individual function arguments.
|
|
|
|
|
2020-09-05 08:38:49 +00:00
|
|
|
let tupled_arg_tys = match arg_ty.kind() {
|
2018-08-24 12:51:02 +00:00
|
|
|
ty::Tuple(ref tys) => tys,
|
2018-08-14 10:13:07 +00:00
|
|
|
_ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty),
|
|
|
|
};
|
|
|
|
|
2018-12-27 09:59:01 +00:00
|
|
|
let mut params = Vec::new();
|
2019-04-28 18:37:44 +00:00
|
|
|
for (i, arg_ty) in tupled_arg_tys.types().enumerate() {
|
2020-02-14 17:23:29 +00:00
|
|
|
let param = cvalue_for_param(fx, start_block, Some(local), Some(i), arg_ty);
|
2018-12-27 09:59:01 +00:00
|
|
|
params.push(param);
|
2018-08-14 10:13:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:59:01 +00:00
|
|
|
(local, ArgKind::Spread(params), arg_ty)
|
2018-08-14 10:13:07 +00:00
|
|
|
} else {
|
2020-02-14 17:23:29 +00:00
|
|
|
let param = cvalue_for_param(fx, start_block, Some(local), None, arg_ty);
|
2019-02-21 14:06:09 +00:00
|
|
|
(local, ArgKind::Normal(param), arg_ty)
|
2018-07-27 17:27:20 +00:00
|
|
|
}
|
2018-10-10 17:07:13 +00:00
|
|
|
})
|
2020-04-05 11:48:26 +00:00
|
|
|
.collect::<Vec<(Local, ArgKind<'tcx>, Ty<'tcx>)>>();
|
2018-07-27 17:27:20 +00:00
|
|
|
|
2020-01-11 15:49:42 +00:00
|
|
|
assert!(fx.caller_location.is_none());
|
2020-08-22 14:47:31 +00:00
|
|
|
if fx.instance.def.requires_caller_location(fx.tcx) {
|
2020-01-11 15:49:42 +00:00
|
|
|
// Store caller location for `#[track_caller]`.
|
2020-08-28 10:10:48 +00:00
|
|
|
fx.caller_location = Some(
|
|
|
|
cvalue_for_param(fx, start_block, None, None, fx.tcx.caller_location_ty()).unwrap(),
|
|
|
|
);
|
2020-01-11 15:49:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 17:23:29 +00:00
|
|
|
fx.bcx.switch_to_block(start_block);
|
2019-12-23 15:48:43 +00:00
|
|
|
fx.bcx.ins().nop();
|
2018-08-14 16:52:43 +00:00
|
|
|
|
2019-08-30 13:07:15 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
self::comments::add_locals_header_comment(fx);
|
|
|
|
|
2018-07-27 17:27:20 +00:00
|
|
|
for (local, arg_kind, ty) in func_params {
|
|
|
|
let layout = fx.layout_of(ty);
|
2018-08-09 08:46:56 +00:00
|
|
|
|
2019-12-17 16:49:12 +00:00
|
|
|
let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
|
2018-12-25 15:47:33 +00:00
|
|
|
|
2020-01-25 15:23:54 +00:00
|
|
|
// While this is normally an optimization to prevent an unnecessary copy when an argument is
|
|
|
|
// not mutated by the current function, this is necessary to support unsized arguments.
|
2020-11-03 10:00:04 +00:00
|
|
|
if let ArgKind::Normal(Some(val)) = arg_kind {
|
|
|
|
if let Some((addr, meta)) = val.try_to_ptr() {
|
|
|
|
let local_decl = &fx.mir.local_decls[local];
|
|
|
|
// v this ! is important
|
|
|
|
let internally_mutable = !val.layout().ty.is_freeze(
|
|
|
|
fx.tcx.at(local_decl.source_info.span),
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
);
|
|
|
|
if local_decl.mutability == mir::Mutability::Not && !internally_mutable {
|
|
|
|
// We wont mutate this argument, so it is fine to borrow the backing storage
|
|
|
|
// of this argument, to prevent a copy.
|
|
|
|
|
|
|
|
let place = if let Some(meta) = meta {
|
|
|
|
CPlace::for_ptr_with_extra(addr, meta, val.layout())
|
|
|
|
} else {
|
|
|
|
CPlace::for_ptr(addr, val.layout())
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
self::comments::add_local_place_comments(fx, place, local);
|
|
|
|
|
|
|
|
assert_eq!(fx.local_map.push(place), local);
|
|
|
|
continue;
|
2019-08-30 13:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:45:19 +00:00
|
|
|
let place = make_local_place(fx, local, layout, is_ssa);
|
|
|
|
assert_eq!(fx.local_map.push(place), local);
|
2018-12-26 10:15:42 +00:00
|
|
|
|
2018-12-22 16:42:38 +00:00
|
|
|
match arg_kind {
|
2018-12-27 09:59:01 +00:00
|
|
|
ArgKind::Normal(param) => {
|
2019-03-26 18:53:04 +00:00
|
|
|
if let Some(param) = param {
|
|
|
|
place.write_cvalue(fx, param);
|
|
|
|
}
|
2018-12-22 16:42:38 +00:00
|
|
|
}
|
2018-12-27 09:59:01 +00:00
|
|
|
ArgKind::Spread(params) => {
|
|
|
|
for (i, param) in params.into_iter().enumerate() {
|
2019-03-26 18:53:04 +00:00
|
|
|
if let Some(param) = param {
|
|
|
|
place
|
|
|
|
.place_field(fx, mir::Field::new(i))
|
|
|
|
.write_cvalue(fx, param);
|
|
|
|
}
|
2018-07-27 17:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-19 17:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:54:11 +00:00
|
|
|
for local in fx.mir.vars_and_temps_iter() {
|
|
|
|
let ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
|
|
|
|
let layout = fx.layout_of(ty);
|
2018-08-09 08:46:56 +00:00
|
|
|
|
2020-06-24 09:54:11 +00:00
|
|
|
let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
|
2018-08-09 08:46:56 +00:00
|
|
|
|
2020-09-16 16:45:19 +00:00
|
|
|
let place = make_local_place(fx, local, layout, is_ssa);
|
|
|
|
assert_eq!(fx.local_map.push(place), local);
|
2018-07-19 17:33:42 +00:00
|
|
|
}
|
2018-08-14 16:52:43 +00:00
|
|
|
|
|
|
|
fx.bcx
|
|
|
|
.ins()
|
2020-02-14 17:23:29 +00:00
|
|
|
.jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]);
|
2018-07-19 17:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn codegen_terminator_call<'tcx>(
|
2020-10-01 08:38:23 +00:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Module>,
|
2020-01-11 15:49:42 +00:00
|
|
|
span: Span,
|
2020-05-29 09:06:29 +00:00
|
|
|
current_block: Block,
|
2018-07-19 17:33:42 +00:00
|
|
|
func: &Operand<'tcx>,
|
|
|
|
args: &[Operand<'tcx>],
|
2020-04-02 15:23:15 +00:00
|
|
|
destination: Option<(Place<'tcx>, BasicBlock)>,
|
2018-07-20 11:51:34 +00:00
|
|
|
) {
|
2020-08-22 14:47:31 +00:00
|
|
|
let fn_ty = fx.monomorphize(&func.ty(fx.mir, fx.tcx));
|
2020-04-13 17:12:44 +00:00
|
|
|
let fn_sig = fx
|
2020-08-22 14:47:31 +00:00
|
|
|
.tcx
|
|
|
|
.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &fn_ty.fn_sig(fx.tcx));
|
2018-07-21 16:38:08 +00:00
|
|
|
|
2020-11-03 10:00:04 +00:00
|
|
|
let destination = destination.map(|(place, bb)| (codegen_place(fx, place), bb));
|
2018-09-11 17:27:57 +00:00
|
|
|
|
2020-04-13 17:12:44 +00:00
|
|
|
// Handle special calls like instrinsics and empty drop glue.
|
2020-09-05 08:38:49 +00:00
|
|
|
let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() {
|
2020-08-22 14:47:31 +00:00
|
|
|
let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
|
2020-04-25 09:42:46 +00:00
|
|
|
.unwrap()
|
2020-07-23 16:07:38 +00:00
|
|
|
.unwrap()
|
2020-08-22 14:47:31 +00:00
|
|
|
.polymorphize(fx.tcx);
|
2018-12-20 13:57:21 +00:00
|
|
|
|
2020-08-22 14:47:31 +00:00
|
|
|
if fx.tcx.symbol_name(instance).name.starts_with("llvm.") {
|
2020-03-27 11:14:45 +00:00
|
|
|
crate::intrinsics::codegen_llvm_intrinsic_call(
|
2019-08-31 17:28:09 +00:00
|
|
|
fx,
|
2020-08-22 14:47:31 +00:00
|
|
|
&fx.tcx.symbol_name(instance).name,
|
2019-08-31 17:28:09 +00:00
|
|
|
substs,
|
|
|
|
args,
|
|
|
|
destination,
|
|
|
|
);
|
2019-07-28 07:54:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-20 13:57:21 +00:00
|
|
|
match instance.def {
|
|
|
|
InstanceDef::Intrinsic(_) => {
|
2019-11-09 10:14:18 +00:00
|
|
|
crate::intrinsics::codegen_intrinsic_call(fx, instance, args, destination, span);
|
2018-12-20 13:57:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
InstanceDef::DropGlue(_, None) => {
|
|
|
|
// empty drop glue - a nop.
|
|
|
|
let (_, dest) = destination.expect("Non terminating drop_in_place_real???");
|
2020-02-14 17:23:29 +00:00
|
|
|
let ret_block = fx.get_block(dest);
|
|
|
|
fx.bcx.ins().jump(ret_block, &[]);
|
2018-12-20 13:57:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-28 10:10:48 +00:00
|
|
|
_ => Some(instance),
|
2018-09-11 17:27:57 +00:00
|
|
|
}
|
2020-04-13 17:12:44 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-10-03 16:21:52 +00:00
|
|
|
|
2020-08-28 10:10:48 +00:00
|
|
|
let is_cold = instance
|
|
|
|
.map(|inst| {
|
|
|
|
fx.tcx
|
|
|
|
.codegen_fn_attrs(inst.def_id())
|
|
|
|
.flags
|
|
|
|
.contains(CodegenFnAttrFlags::COLD)
|
|
|
|
})
|
|
|
|
.unwrap_or(false);
|
2020-05-29 09:06:29 +00:00
|
|
|
if is_cold {
|
|
|
|
fx.cold_blocks.insert(current_block);
|
|
|
|
}
|
|
|
|
|
2019-07-28 09:24:33 +00:00
|
|
|
// Unpack arguments tuple for closures
|
2020-04-13 17:12:44 +00:00
|
|
|
let args = if fn_sig.abi == Abi::RustCall {
|
2019-07-28 09:24:33 +00:00
|
|
|
assert_eq!(args.len(), 2, "rust-call abi requires two arguments");
|
2020-11-03 10:00:04 +00:00
|
|
|
let self_arg = codegen_operand(fx, &args[0]);
|
|
|
|
let pack_arg = codegen_operand(fx, &args[1]);
|
2020-04-13 17:12:44 +00:00
|
|
|
|
2020-09-05 08:38:49 +00:00
|
|
|
let tupled_arguments = match pack_arg.layout().ty.kind() {
|
2020-08-28 10:10:48 +00:00
|
|
|
ty::Tuple(ref tupled_arguments) => tupled_arguments,
|
2019-07-28 09:24:33 +00:00
|
|
|
_ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
|
2020-04-13 17:12:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut args = Vec::with_capacity(1 + tupled_arguments.len());
|
|
|
|
args.push(self_arg);
|
|
|
|
for i in 0..tupled_arguments.len() {
|
|
|
|
args.push(pack_arg.value_field(fx, mir::Field::new(i)));
|
2019-07-28 09:24:33 +00:00
|
|
|
}
|
|
|
|
args
|
|
|
|
} else {
|
2020-11-03 10:00:04 +00:00
|
|
|
args.iter()
|
|
|
|
.map(|arg| codegen_operand(fx, arg))
|
2019-07-28 09:24:33 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
};
|
|
|
|
|
2019-06-16 10:54:37 +00:00
|
|
|
// | indirect call target
|
|
|
|
// | | the first argument to be passed
|
|
|
|
// v v v virtual calls are special cased below
|
|
|
|
let (func_ref, first_arg, is_virtual_call) = match instance {
|
2019-02-07 19:28:55 +00:00
|
|
|
// Trait object call
|
|
|
|
Some(Instance {
|
2018-09-08 16:00:06 +00:00
|
|
|
def: InstanceDef::Virtual(_, idx),
|
|
|
|
..
|
2019-02-07 19:28:55 +00:00
|
|
|
}) => {
|
2019-06-16 12:47:01 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
let nop_inst = fx.bcx.ins().nop();
|
|
|
|
fx.add_comment(
|
|
|
|
nop_inst,
|
2019-08-31 17:28:09 +00:00
|
|
|
format!(
|
|
|
|
"virtual call; self arg pass mode: {:?}",
|
2020-08-22 14:47:31 +00:00
|
|
|
get_pass_mode(fx.tcx, args[0].layout())
|
2019-08-31 17:28:09 +00:00
|
|
|
),
|
2019-06-16 12:47:01 +00:00
|
|
|
);
|
|
|
|
}
|
2018-09-08 16:00:06 +00:00
|
|
|
let (ptr, method) = crate::vtable::get_ptr_and_method_ref(fx, args[0], idx);
|
2019-06-16 10:54:37 +00:00
|
|
|
(Some(method), Single(ptr), true)
|
2019-02-07 19:28:55 +00:00
|
|
|
}
|
2018-09-08 16:00:06 +00:00
|
|
|
|
2019-02-07 19:28:55 +00:00
|
|
|
// Normal call
|
2019-08-31 17:28:09 +00:00
|
|
|
Some(_) => (
|
|
|
|
None,
|
|
|
|
args.get(0)
|
|
|
|
.map(|arg| adjust_arg_for_abi(fx, *arg))
|
|
|
|
.unwrap_or(Empty),
|
|
|
|
false,
|
|
|
|
),
|
2019-02-07 19:28:55 +00:00
|
|
|
|
|
|
|
// Indirect call
|
|
|
|
None => {
|
2019-06-16 12:47:01 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
let nop_inst = fx.bcx.ins().nop();
|
|
|
|
fx.add_comment(nop_inst, "indirect call");
|
|
|
|
}
|
2020-11-03 10:00:04 +00:00
|
|
|
let func = codegen_operand(fx, func).load_scalar(fx);
|
2019-02-21 14:06:09 +00:00
|
|
|
(
|
|
|
|
Some(func),
|
2019-08-31 17:28:09 +00:00
|
|
|
args.get(0)
|
|
|
|
.map(|arg| adjust_arg_for_abi(fx, *arg))
|
|
|
|
.unwrap_or(Empty),
|
2019-06-16 10:54:37 +00:00
|
|
|
false,
|
2019-02-21 14:06:09 +00:00
|
|
|
)
|
2018-10-10 17:07:13 +00:00
|
|
|
}
|
2018-09-08 16:00:06 +00:00
|
|
|
};
|
|
|
|
|
2020-04-13 15:38:17 +00:00
|
|
|
let ret_place = destination.map(|(place, _)| place);
|
2019-08-31 17:28:09 +00:00
|
|
|
let (call_inst, call_args) =
|
|
|
|
self::returning::codegen_with_call_return_arg(fx, fn_sig, ret_place, |fx, return_ptr| {
|
2020-01-11 15:49:42 +00:00
|
|
|
let mut call_args: Vec<Value> = return_ptr
|
2019-08-31 17:28:09 +00:00
|
|
|
.into_iter()
|
|
|
|
.chain(first_arg.into_iter())
|
|
|
|
.chain(
|
|
|
|
args.into_iter()
|
|
|
|
.skip(1)
|
|
|
|
.map(|arg| adjust_arg_for_abi(fx, arg).into_iter())
|
|
|
|
.flatten(),
|
|
|
|
)
|
|
|
|
.collect::<Vec<_>>();
|
2019-08-30 10:30:57 +00:00
|
|
|
|
2020-08-28 10:10:48 +00:00
|
|
|
if instance
|
|
|
|
.map(|inst| inst.def.requires_caller_location(fx.tcx))
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
2020-01-11 15:49:42 +00:00
|
|
|
// Pass the caller location for `#[track_caller]`.
|
|
|
|
let caller_location = fx.get_caller_location(span);
|
|
|
|
call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
|
|
|
|
}
|
|
|
|
|
2019-08-31 17:28:09 +00:00
|
|
|
let call_inst = if let Some(func_ref) = func_ref {
|
2020-01-11 15:49:42 +00:00
|
|
|
let sig = clif_sig_from_fn_sig(
|
2020-08-22 14:47:31 +00:00
|
|
|
fx.tcx,
|
2020-01-11 15:49:42 +00:00
|
|
|
fx.triple(),
|
|
|
|
fn_sig,
|
2020-06-20 17:03:54 +00:00
|
|
|
span,
|
2020-01-11 15:49:42 +00:00
|
|
|
is_virtual_call,
|
|
|
|
false, // calls through function pointers never pass the caller location
|
|
|
|
);
|
2019-12-17 14:03:32 +00:00
|
|
|
let sig = fx.bcx.import_signature(sig);
|
2019-08-31 17:28:09 +00:00
|
|
|
fx.bcx.ins().call_indirect(sig, func_ref, &call_args)
|
|
|
|
} else {
|
|
|
|
let func_ref =
|
|
|
|
fx.get_function_ref(instance.expect("non-indirect call on non-FnDef type"));
|
|
|
|
fx.bcx.ins().call(func_ref, &call_args)
|
|
|
|
};
|
2019-08-30 10:30:57 +00:00
|
|
|
|
2019-08-31 17:28:09 +00:00
|
|
|
(call_inst, call_args)
|
|
|
|
});
|
2018-08-11 10:34:46 +00:00
|
|
|
|
2019-02-11 18:18:52 +00:00
|
|
|
// FIXME find a cleaner way to support varargs
|
2019-03-01 17:55:20 +00:00
|
|
|
if fn_sig.c_variadic {
|
2019-02-11 18:18:52 +00:00
|
|
|
if fn_sig.abi != Abi::C {
|
2020-08-28 10:10:48 +00:00
|
|
|
fx.tcx.sess.span_fatal(
|
|
|
|
span,
|
|
|
|
&format!("Variadic call for non-C abi {:?}", fn_sig.abi),
|
|
|
|
);
|
2019-02-11 18:18:52 +00:00
|
|
|
}
|
|
|
|
let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap();
|
2019-02-21 14:06:09 +00:00
|
|
|
let abi_params = call_args
|
|
|
|
.into_iter()
|
|
|
|
.map(|arg| {
|
|
|
|
let ty = fx.bcx.func.dfg.value_type(arg);
|
|
|
|
if !ty.is_int() {
|
|
|
|
// FIXME set %al to upperbound on float args once floats are supported
|
2020-08-28 10:10:48 +00:00
|
|
|
fx.tcx
|
|
|
|
.sess
|
|
|
|
.span_fatal(span, &format!("Non int ty {:?} for variadic call", ty));
|
2019-02-21 14:06:09 +00:00
|
|
|
}
|
|
|
|
AbiParam::new(ty)
|
|
|
|
})
|
|
|
|
.collect::<Vec<AbiParam>>();
|
2019-02-11 18:18:52 +00:00
|
|
|
fx.bcx.func.dfg.signatures[sig_ref].params = abi_params;
|
|
|
|
}
|
2020-04-13 15:38:17 +00:00
|
|
|
|
|
|
|
if let Some((_, dest)) = destination {
|
|
|
|
let ret_block = fx.get_block(dest);
|
|
|
|
fx.bcx.ins().jump(ret_block, &[]);
|
|
|
|
} else {
|
|
|
|
trap_unreachable(fx, "[corruption] Diverging function returned");
|
|
|
|
}
|
2018-07-19 17:33:42 +00:00
|
|
|
}
|
2018-08-10 17:20:13 +00:00
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn codegen_drop<'tcx>(
|
2020-10-01 08:38:23 +00:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Module>,
|
2020-01-11 15:49:42 +00:00
|
|
|
span: Span,
|
|
|
|
drop_place: CPlace<'tcx>,
|
|
|
|
) {
|
2019-06-16 13:57:53 +00:00
|
|
|
let ty = drop_place.layout().ty;
|
2020-08-22 14:47:31 +00:00
|
|
|
let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx);
|
2019-02-07 19:45:15 +00:00
|
|
|
|
2019-06-16 13:57:53 +00:00
|
|
|
if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
|
|
|
|
// we don't actually need to drop anything
|
|
|
|
} else {
|
2020-08-22 14:47:31 +00:00
|
|
|
let drop_fn_ty = drop_fn.ty(fx.tcx, ParamEnv::reveal_all());
|
|
|
|
let fn_sig = fx.tcx.normalize_erasing_late_bound_regions(
|
2020-04-13 15:31:35 +00:00
|
|
|
ParamEnv::reveal_all(),
|
2020-08-22 14:47:31 +00:00
|
|
|
&drop_fn_ty.fn_sig(fx.tcx),
|
2020-04-13 15:31:35 +00:00
|
|
|
);
|
2020-08-22 14:47:31 +00:00
|
|
|
assert_eq!(fn_sig.output(), fx.tcx.mk_unit());
|
2020-04-13 15:31:35 +00:00
|
|
|
|
2020-09-05 08:38:49 +00:00
|
|
|
match ty.kind() {
|
2019-06-16 13:57:53 +00:00
|
|
|
ty::Dynamic(..) => {
|
2020-03-29 09:51:43 +00:00
|
|
|
let (ptr, vtable) = drop_place.to_ptr_maybe_unsized();
|
2019-12-20 18:10:08 +00:00
|
|
|
let ptr = ptr.get_addr(fx);
|
2019-06-16 13:57:53 +00:00
|
|
|
let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
|
2019-02-07 19:45:15 +00:00
|
|
|
|
2020-01-11 15:49:42 +00:00
|
|
|
let sig = clif_sig_from_fn_sig(
|
2020-08-22 14:47:31 +00:00
|
|
|
fx.tcx,
|
2020-01-11 15:49:42 +00:00
|
|
|
fx.triple(),
|
|
|
|
fn_sig,
|
2020-06-20 17:03:54 +00:00
|
|
|
span,
|
2020-01-11 15:49:42 +00:00
|
|
|
true,
|
|
|
|
false, // `drop_in_place` is never `#[track_caller]`
|
|
|
|
);
|
2019-12-17 14:03:32 +00:00
|
|
|
let sig = fx.bcx.import_signature(sig);
|
2019-06-16 13:57:53 +00:00
|
|
|
fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
|
|
|
|
}
|
|
|
|
_ => {
|
2020-07-23 16:07:38 +00:00
|
|
|
assert!(!matches!(drop_fn.def, InstanceDef::Virtual(_, _)));
|
2020-04-13 15:31:35 +00:00
|
|
|
|
2020-08-28 10:10:48 +00:00
|
|
|
let arg_value = drop_place.place_ref(
|
|
|
|
fx,
|
|
|
|
fx.layout_of(fx.tcx.mk_ref(
|
|
|
|
&ty::RegionKind::ReErased,
|
|
|
|
TypeAndMut {
|
|
|
|
ty,
|
|
|
|
mutbl: crate::rustc_hir::Mutability::Mut,
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
);
|
2020-04-13 15:31:35 +00:00
|
|
|
let arg_value = adjust_arg_for_abi(fx, arg_value);
|
|
|
|
|
|
|
|
let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>();
|
|
|
|
|
2020-08-22 14:47:31 +00:00
|
|
|
if drop_fn.def.requires_caller_location(fx.tcx) {
|
2020-04-13 15:31:35 +00:00
|
|
|
// Pass the caller location for `#[track_caller]`.
|
|
|
|
let caller_location = fx.get_caller_location(span);
|
|
|
|
call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:07:38 +00:00
|
|
|
let func_ref = fx.get_function_ref(drop_fn);
|
2020-04-13 15:31:35 +00:00
|
|
|
fx.bcx.ins().call(func_ref, &call_args);
|
2019-06-16 13:57:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 19:45:15 +00:00
|
|
|
}
|