rust/src/abi/mod.rs

557 lines
20 KiB
Rust
Raw Normal View History

//! Handling of everything related to the calling convention. Also fills `fx.local_map`.
mod comments;
mod pass_mode;
2019-08-31 17:28:09 +00:00
mod returning;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::ty::layout::FnAbiExt;
use rustc_target::abi::call::{Conv, FnAbi};
use rustc_target::spec::abi::Abi;
2018-07-19 17:33:42 +00:00
use cranelift_codegen::ir::AbiParam;
use smallvec::smallvec;
use self::pass_mode::*;
2019-08-31 17:28:09 +00:00
use crate::prelude::*;
2018-09-08 16:00:06 +00:00
pub(crate) use self::returning::{can_return_to_ssa_var, codegen_return};
fn clif_sig_from_fn_abi<'tcx>(
2019-08-31 17:28:09 +00:00
tcx: TyCtxt<'tcx>,
triple: &target_lexicon::Triple,
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
2019-08-31 17:28:09 +00:00
) -> Signature {
let call_conv = match fn_abi.conv {
Conv::Rust | Conv::C => CallConv::triple_default(triple),
Conv::X86_64SysV => CallConv::SystemV,
Conv::X86_64Win64 => CallConv::WindowsFastcall,
Conv::ArmAapcs
| Conv::CCmseNonSecureCall
| Conv::Msp430Intr
| Conv::PtxKernel
| Conv::X86Fastcall
| Conv::X86Intr
| Conv::X86Stdcall
| Conv::X86ThisCall
| Conv::X86VectorCall
| Conv::AmdGpuKernel
| Conv::AvrInterrupt
| Conv::AvrNonBlockingInterrupt => todo!("{:?}", fn_abi.conv),
2018-07-19 17:33:42 +00:00
};
let inputs = fn_abi.args.iter().map(|arg_abi| arg_abi.get_abi_param(tcx).into_iter()).flatten();
2019-06-16 10:54:37 +00:00
let (return_ptr, returns) = fn_abi.ret.get_abi_return(tcx);
// Sometimes the first param is an pointer to the place where the return value needs to be stored.
let params: Vec<_> = return_ptr.into_iter().chain(inputs).collect();
2020-01-11 15:49:42 +00:00
Signature { params, returns, call_conv }
2018-07-19 17:33:42 +00:00
}
pub(crate) fn get_function_sig<'tcx>(
tcx: TyCtxt<'tcx>,
triple: &target_lexicon::Triple,
2018-08-11 11:59:08 +00:00
inst: Instance<'tcx>,
) -> Signature {
assert!(!inst.substs.needs_infer());
clif_sig_from_fn_abi(tcx, triple, &FnAbi::of_instance(&RevealAllLayoutCx(tcx), inst, &[]))
2018-08-11 11:59:08 +00:00
}
2019-01-02 11:20:32 +00:00
/// Instance must be monomorphized
pub(crate) fn import_function<'tcx>(
tcx: TyCtxt<'tcx>,
module: &mut dyn Module,
2019-01-02 11:20:32 +00:00
inst: Instance<'tcx>,
) -> FuncId {
let name = tcx.symbol_name(inst).name.to_string();
let sig = get_function_sig(tcx, module.isa().triple(), inst);
module.declare_function(&name, Linkage::Import, &sig).unwrap()
2019-01-02 11:20:32 +00:00
}
2018-09-08 16:00:06 +00:00
impl<'tcx> FunctionCx<'_, '_, 'tcx> {
2018-09-08 16:00:06 +00:00
/// Instance must be monomorphized
pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
let func_id = import_function(self.tcx, self.cx.module, inst);
let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func);
2018-12-28 16:07:40 +00:00
if self.clif_comments.enabled() {
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
}
pub(crate) fn lib_call(
2018-07-30 13:34:34 +00:00
&mut self,
name: &str,
params: Vec<AbiParam>,
returns: Vec<AbiParam>,
2018-07-30 13:34:34 +00:00
args: &[Value],
) -> &[Value] {
let sig = Signature { params, returns, call_conv: CallConv::triple_default(self.triple()) };
let func_id = self.cx.module.declare_function(&name, Linkage::Import, &sig).unwrap();
let func_ref = self.cx.module.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);
if self.clif_comments.enabled() {
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);
assert!(results.len() <= 2, "{}", results.len());
results
2018-07-30 13:34:34 +00:00
}
pub(crate) fn easy_call(
&mut self,
name: &str,
args: &[CValue<'tcx>],
return_ty: Ty<'tcx>,
) -> CValue<'tcx> {
let (input_tys, args): (Vec<_>, Vec<_>) = args
.iter()
.map(|arg| {
(AbiParam::new(self.clif_type(arg.layout().ty).unwrap()), arg.load_scalar(self))
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);
let return_tys = if let ty::Tuple(tup) = return_ty.kind() {
tup.types().map(|ty| AbiParam::new(self.clif_type(ty).unwrap())).collect()
} else {
vec![AbiParam::new(self.clif_type(return_ty).unwrap())]
};
let ret_vals = self.lib_call(name, input_tys, return_tys, &args);
match *ret_vals {
[] => CValue::by_ref(
Pointer::const_addr(self, i64::from(self.pointer_type.bytes())),
2019-02-21 14:06:09 +00:00
return_layout,
),
[val] => CValue::by_val(val, return_layout),
[val, extra] => CValue::by_val_pair(val, extra, return_layout),
_ => unreachable!(),
}
2018-07-30 13:34:34 +00:00
}
2018-07-19 17:33:42 +00:00
}
/// Make a [`CPlace`] capable of holding value of the specified type.
fn make_local_place<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
2018-12-25 15:47:33 +00:00
local: Local,
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 {
CPlace::new_stack_slot(fx, layout)
2018-12-25 15:47:33 +00:00
};
self::comments::add_local_place_comments(fx, place, local);
place
2018-12-25 15:47:33 +00:00
}
pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_block: Block) {
fx.bcx.append_block_params_for_function_params(start_block);
fx.bcx.switch_to_block(start_block);
fx.bcx.ins().nop();
2018-08-09 08:46:56 +00:00
let ssa_analyzed = crate::analyze::analyze(fx);
2018-12-28 16:07:40 +00:00
2019-08-30 13:07:15 +00:00
self::comments::add_args_header_comment(fx);
2018-12-27 09:59:01 +00:00
let mut block_params_iter = fx.bcx.func.dfg.block_params(start_block).to_vec().into_iter();
let ret_place =
self::returning::codegen_return_param(fx, &ssa_analyzed, &mut block_params_iter);
assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE);
// None means pass_mode == NoPass
2018-12-27 09:59:01 +00:00
enum ArgKind<'tcx> {
Normal(Option<CValue<'tcx>>),
Spread(Vec<Option<CValue<'tcx>>>),
}
let fn_abi = fx.fn_abi.take().unwrap();
let mut arg_abis_iter = fn_abi.args.iter();
2018-08-14 10:13:07 +00:00
let func_params = fx
.mir
.args_iter()
.map(|local| {
2020-10-28 07:25:06 +00:00
let arg_ty = fx.monomorphize(fx.mir.local_decls[local].ty);
2018-08-14 10:13:07 +00:00
// 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.
let tupled_arg_tys = match arg_ty.kind() {
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();
for (i, _arg_ty) in tupled_arg_tys.types().enumerate() {
let arg_abi = arg_abis_iter.next().unwrap();
let param =
cvalue_for_param(fx, Some(local), Some(i), arg_abi, &mut block_params_iter);
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 {
let arg_abi = arg_abis_iter.next().unwrap();
let param =
cvalue_for_param(fx, Some(local), None, arg_abi, &mut block_params_iter);
2019-02-21 14:06:09 +00:00
(local, ArgKind::Normal(param), arg_ty)
}
2018-10-10 17:07:13 +00:00
})
.collect::<Vec<(Local, ArgKind<'tcx>, Ty<'tcx>)>>();
2020-01-11 15:49:42 +00:00
assert!(fx.caller_location.is_none());
if fx.instance.def.requires_caller_location(fx.tcx) {
2020-01-11 15:49:42 +00:00
// Store caller location for `#[track_caller]`.
let arg_abi = arg_abis_iter.next().unwrap();
fx.caller_location =
Some(cvalue_for_param(fx, None, None, arg_abi, &mut block_params_iter).unwrap());
2020-01-11 15:49:42 +00:00
}
assert!(arg_abis_iter.next().is_none(), "ArgAbi left behind");
fx.fn_abi = Some(fn_abi);
assert!(block_params_iter.next().is_none(), "arg_value left behind");
2018-08-14 16:52:43 +00:00
2019-08-30 13:07:15 +00:00
self::comments::add_locals_header_comment(fx);
for (local, arg_kind, ty) in func_params {
let layout = fx.layout_of(ty);
2018-08-09 08:46:56 +00:00
let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
2018-12-25 15:47:33 +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.
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())
};
self::comments::add_local_place_comments(fx, place, local);
assert_eq!(fx.local_map.push(place), local);
continue;
}
}
}
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
match arg_kind {
2018-12-27 09:59:01 +00:00
ArgKind::Normal(param) => {
if let Some(param) = param {
place.write_cvalue(fx, param);
}
}
2018-12-27 09:59:01 +00:00
ArgKind::Spread(params) => {
for (i, param) in params.into_iter().enumerate() {
if let Some(param) = param {
place.place_field(fx, mir::Field::new(i)).write_cvalue(fx, param);
}
}
}
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() {
2020-10-28 07:25:06 +00:00
let ty = fx.monomorphize(fx.mir.local_decls[local].ty);
2020-06-24 09:54:11 +00:00
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
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().jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]);
2018-07-19 17:33:42 +00:00
}
pub(crate) fn codegen_terminator_call<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
2020-01-11 15:49:42 +00:00
span: Span,
current_block: Block,
2018-07-19 17:33:42 +00:00
func: &Operand<'tcx>,
args: &[Operand<'tcx>],
destination: Option<(Place<'tcx>, BasicBlock)>,
2018-07-20 11:51:34 +00:00
) {
2020-10-28 07:25:06 +00:00
let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx));
let fn_sig =
fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx));
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.
let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() {
let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
.unwrap()
2020-07-23 16:07:38 +00:00
.unwrap()
.polymorphize(fx.tcx);
if fx.tcx.symbol_name(instance).name.starts_with("llvm.") {
crate::intrinsics::codegen_llvm_intrinsic_call(
2019-08-31 17:28:09 +00:00
fx,
&fx.tcx.symbol_name(instance).name,
2019-08-31 17:28:09 +00:00
substs,
args,
destination,
);
return;
}
match instance.def {
InstanceDef::Intrinsic(_) => {
crate::intrinsics::codegen_intrinsic_call(fx, instance, args, destination, span);
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, &[]);
return;
}
_ => Some(instance),
2018-09-11 17:27:57 +00:00
}
2020-04-13 17:12:44 +00:00
} else {
None
};
let extra_args = &args[fn_sig.inputs().len()..];
let extra_args = extra_args
.iter()
.map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx)))
.collect::<Vec<_>>();
let fn_abi = if let Some(instance) = instance {
FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), instance, &extra_args)
} else {
FnAbi::of_fn_ptr(&RevealAllLayoutCx(fx.tcx), fn_ty.fn_sig(fx.tcx), &extra_args)
};
let is_cold = instance
.map(|inst| fx.tcx.codegen_fn_attrs(inst.def_id()).flags.contains(CodegenFnAttrFlags::COLD))
.unwrap_or(false);
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");
let self_arg = codegen_operand(fx, &args[0]);
let pack_arg = codegen_operand(fx, &args[1]);
2020-04-13 17:12:44 +00:00
let tupled_arguments = match pack_arg.layout().ty.kind() {
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 {
args.iter().map(|arg| codegen_operand(fx, arg)).collect::<Vec<_>>()
2019-07-28 09:24:33 +00:00
};
2019-06-16 10:54:37 +00:00
// | indirect call target
// | | the first argument to be passed
// v v
let (func_ref, first_arg) = match instance {
// Trait object call
Some(Instance { def: InstanceDef::Virtual(_, idx), .. }) => {
if fx.clif_comments.enabled() {
2019-06-16 12:47:01 +00:00
let nop_inst = fx.bcx.ins().nop();
fx.add_comment(
nop_inst,
format!("virtual call; self arg pass mode: {:?}", &fn_abi.args[0],),
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);
(Some(method), smallvec![ptr])
}
2018-09-08 16:00:06 +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, &fn_abi.args[0]))
.unwrap_or(smallvec![]),
2019-08-31 17:28:09 +00:00
),
// Indirect call
None => {
if fx.clif_comments.enabled() {
2019-06-16 12:47:01 +00:00
let nop_inst = fx.bcx.ins().nop();
fx.add_comment(nop_inst, "indirect call");
}
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, &fn_abi.args[0]))
.unwrap_or(smallvec![]),
2019-02-21 14:06:09 +00:00
)
2018-10-10 17:07:13 +00:00
}
2018-09-08 16:00:06 +00:00
};
let ret_place = destination.map(|(place, _)| place);
let (call_inst, call_args) = self::returning::codegen_with_call_return_arg(
fx,
&fn_abi.ret,
ret_place,
|fx, return_ptr| {
let regular_args_count = args.len();
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()
.enumerate()
2019-08-31 17:28:09 +00:00
.skip(1)
.map(|(i, arg)| adjust_arg_for_abi(fx, arg, &fn_abi.args[i]).into_iter())
2019-08-31 17:28:09 +00:00
.flatten(),
)
.collect::<Vec<_>>();
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, &fn_abi.args[regular_args_count])
.into_iter(),
);
assert_eq!(fn_abi.args.len(), regular_args_count + 1);
} else {
assert_eq!(fn_abi.args.len(), regular_args_count);
2020-01-11 15:49:42 +00:00
}
2019-08-31 17:28:09 +00:00
let call_inst = if let Some(func_ref) = func_ref {
let sig = clif_sig_from_fn_abi(fx.tcx, fx.triple(), &fn_abi);
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-31 17:28:09 +00:00
(call_inst, call_args)
},
);
2019-02-11 18:18:52 +00:00
// FIXME find a cleaner way to support varargs
if fn_sig.c_variadic {
rustc_target: add "unwind" payloads to `Abi` ### Overview This commit begins the implementation work for RFC 2945. For more information, see the rendered RFC [1] and tracking issue [2]. A boolean `unwind` payload is added to the `C`, `System`, `Stdcall`, and `Thiscall` variants, marking whether unwinding across FFI boundaries is acceptable. The cases where each of these variants' `unwind` member is true correspond with the `C-unwind`, `system-unwind`, `stdcall-unwind`, and `thiscall-unwind` ABI strings introduced in RFC 2945 [3]. ### Feature Gate and Unstable Book This commit adds a `c_unwind` feature gate for the new ABI strings. Tests for this feature gate are included in `src/test/ui/c-unwind/`, which ensure that this feature gate works correctly for each of the new ABIs. A new language features entry in the unstable book is added as well. ### Further Work To Be Done This commit does not proceed to implement the new unwinding ABIs, and is intentionally scoped specifically to *defining* the ABIs and their feature flag. ### One Note on Test Churn This will lead to some test churn, in re-blessing hash tests, as the deleted comment in `src/librustc_target/spec/abi.rs` mentioned, because we can no longer guarantee the ordering of the `Abi` variants. While this is a downside, this decision was made bearing in mind that RFC 2945 states the following, in the "Other `unwind` Strings" section [3]: > More unwind variants of existing ABI strings may be introduced, > with the same semantics, without an additional RFC. Adding a new variant for each of these cases, rather than specifying a payload for a given ABI, would quickly become untenable, and make working with the `Abi` enum prone to mistakes. This approach encodes the unwinding information *into* a given ABI, to account for the future possibility of other `-unwind` ABI strings. ### Ignore Directives `ignore-*` directives are used in two of our `*-unwind` ABI test cases. Specifically, the `stdcall-unwind` and `thiscall-unwind` test cases ignore architectures that do not support `stdcall` and `thiscall`, respectively. These directives are cribbed from `src/test/ui/c-variadic/variadic-ffi-1.rs` for `stdcall`, and `src/test/ui/extern/extern-thiscall.rs` for `thiscall`. This would otherwise fail on some targets, see: https://github.com/rust-lang-ci/rust/commit/fcf697f90206e9c87b39d494f94ab35d976bfc60 ### Footnotes [1]: https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md [2]: https://github.com/rust-lang/rust/issues/74990 [3]: https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md#other-unwind-abi-strings
2020-08-27 15:49:18 +00:00
if !matches!(fn_sig.abi, Abi::C { .. }) {
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
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;
}
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
pub(crate) fn codegen_drop<'tcx>(
fx: &mut FunctionCx<'_, '_, 'tcx>,
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;
let drop_instance = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx);
2019-02-07 19:45:15 +00:00
if let ty::InstanceDef::DropGlue(_, None) = drop_instance.def {
2019-06-16 13:57:53 +00:00
// we don't actually need to drop anything
} else {
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();
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
// FIXME(eddyb) perhaps move some of this logic into
// `Instance::resolve_drop_in_place`?
let virtual_drop = Instance {
def: ty::InstanceDef::Virtual(drop_instance.def_id(), 0),
substs: drop_instance.substs,
};
let fn_abi = FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), virtual_drop, &[]);
let sig = clif_sig_from_fn_abi(fx.tcx, fx.triple(), &fn_abi);
let sig = fx.bcx.import_signature(sig);
2019-06-16 13:57:53 +00:00
fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
}
_ => {
assert!(!matches!(drop_instance.def, InstanceDef::Virtual(_, _)));
let fn_abi = FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), drop_instance, &[]);
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 },
)),
);
let arg_value = adjust_arg_for_abi(fx, arg_value, &fn_abi.args[0]);
let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>();
if drop_instance.def.requires_caller_location(fx.tcx) {
// 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, &fn_abi.args[1]).into_iter(),
);
}
let func_ref = fx.get_function_ref(drop_instance);
fx.bcx.ins().call(func_ref, &call_args);
2019-06-16 13:57:53 +00:00
}
}
}
2019-02-07 19:45:15 +00:00
}