1 //! Handling of everything related to the calling convention. Also fills `fx.local_map`.
3 #[cfg(debug_assertions)]
8 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
9 use rustc_target::spec::abi::Abi;
11 use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};
13 use self::pass_mode::*;
14 use crate::prelude::*;
16 pub(crate) use self::returning::{can_return_to_ssa_var, codegen_return};
18 // Copied from https://github.com/rust-lang/rust/blob/f52c72948aa1dd718cc1f168d21c91c584c0a662/src/librustc_middle/ty/layout.rs#L2301
20 pub(crate) fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> ty::PolyFnSig<'tcx> {
21 use rustc_middle::ty::subst::Subst;
23 // FIXME(davidtwco,eddyb): A `ParamEnv` should be passed through to this function.
24 let ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
27 // HACK(davidtwco,eddyb): This is a workaround for polymorphization considering
28 // parameters unused if they show up in the signature, but not in the `mir::Body`
29 // (i.e. due to being inside a projection that got normalized, see
30 // `src/test/ui/polymorphization/normalized_sig_types.rs`), and codegen not keeping
31 // track of a polymorphization `ParamEnv` to allow normalizing later.
32 let mut sig = match *ty.kind() {
33 ty::FnDef(def_id, substs) => tcx
34 .normalize_erasing_regions(tcx.param_env(def_id), tcx.fn_sig(def_id))
39 if let ty::InstanceDef::VtableShim(..) = instance.def {
40 // Modify `fn(self, ...)` to `fn(self: *mut Self, ...)`.
41 sig = sig.map_bound(|mut sig| {
42 let mut inputs_and_output = sig.inputs_and_output.to_vec();
43 inputs_and_output[0] = tcx.mk_mut_ptr(inputs_and_output[0]);
44 sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output);
50 ty::Closure(def_id, substs) => {
51 let sig = substs.as_closure().sig();
53 let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
56 std::iter::once(env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
64 ty::Generator(_, substs, _) => {
65 let sig = substs.as_generator().poly_sig();
67 let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
68 let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
70 let pin_did = tcx.require_lang_item(rustc_hir::LangItem::Pin, None);
71 let pin_adt_ref = tcx.adt_def(pin_did);
72 let pin_substs = tcx.intern_substs(&[env_ty.into()]);
73 let env_ty = tcx.mk_adt(pin_adt_ref, pin_substs);
76 let state_did = tcx.require_lang_item(rustc_hir::LangItem::GeneratorState, None);
77 let state_adt_ref = tcx.adt_def(state_did);
79 tcx.intern_substs(&[sig.yield_ty.into(), sig.return_ty.into()]);
80 let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
83 [env_ty, sig.resume_ty].iter(),
86 rustc_hir::Unsafety::Normal,
87 rustc_target::spec::abi::Abi::Rust,
91 _ => bug!("unexpected type {:?} in Instance::fn_sig", ty),
95 fn clif_sig_from_fn_sig<'tcx>(
97 triple: &target_lexicon::Triple,
101 requires_caller_location: bool,
103 let abi = match sig.abi {
104 Abi::System => Abi::C,
107 let (call_conv, inputs, output): (CallConv, Vec<Ty<'tcx>>, Ty<'tcx>) = match abi {
109 CallConv::triple_default(triple),
110 sig.inputs().to_vec(),
113 Abi::C | Abi::Unadjusted => (
114 CallConv::triple_default(triple),
115 sig.inputs().to_vec(),
118 Abi::SysV64 => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
120 assert_eq!(sig.inputs().len(), 2);
121 let extra_args = match sig.inputs().last().unwrap().kind() {
122 ty::Tuple(ref tupled_arguments) => tupled_arguments,
123 _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
125 let mut inputs: Vec<Ty<'tcx>> = vec![sig.inputs()[0]];
126 inputs.extend(extra_args.types());
127 (CallConv::triple_default(triple), inputs, sig.output())
129 Abi::System => unreachable!(),
130 Abi::RustIntrinsic => (
131 CallConv::triple_default(triple),
132 sig.inputs().to_vec(),
135 _ => unimplemented!("unsupported abi {:?}", sig.abi),
142 let mut layout = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap();
143 if i == 0 && is_vtable_fn {
144 // Virtual calls turn their self param into a thin pointer.
145 // See https://github.com/rust-lang/rust/blob/37b6a5e5e82497caf5353d9d856e4eb5d14cbe06/src/librustc/ty/layout.rs#L2519-L2572 for more info
147 .layout_of(ParamEnv::reveal_all().and(tcx.mk_mut_ptr(tcx.mk_unit())))
150 let pass_mode = get_pass_mode(tcx, layout);
151 if abi != Abi::Rust && abi != Abi::RustCall && abi != Abi::RustIntrinsic {
153 PassMode::NoPass | PassMode::ByVal(_) => {}
154 PassMode::ByRef { size: Some(size) } => {
155 let purpose = ArgumentPurpose::StructArgument(u32::try_from(size.bytes()).expect("struct too big to pass on stack"));
156 return EmptySinglePair::Single(AbiParam::special(pointer_ty(tcx), purpose)).into_iter();
158 PassMode::ByValPair(_, _) | PassMode::ByRef { size: None } => {
162 "Argument of type `{:?}` with pass mode `{:?}` is not yet supported \
163 for non-rust abi `{}`. Calling this function may result in a crash.",
172 pass_mode.get_param_ty(tcx).map(AbiParam::new).into_iter()
176 let (mut params, returns): (Vec<_>, Vec<_>) = match get_pass_mode(
178 tcx.layout_of(ParamEnv::reveal_all().and(output)).unwrap(),
180 PassMode::NoPass => (inputs.collect(), vec![]),
181 PassMode::ByVal(ret_ty) => (inputs.collect(), vec![AbiParam::new(ret_ty)]),
182 PassMode::ByValPair(ret_ty_a, ret_ty_b) => (
184 vec![AbiParam::new(ret_ty_a), AbiParam::new(ret_ty_b)],
186 PassMode::ByRef { size: Some(_) } => {
188 Some(pointer_ty(tcx)) // First param is place to put return val
190 .map(|ty| AbiParam::special(ty, ArgumentPurpose::StructReturn))
196 PassMode::ByRef { size: None } => todo!(),
199 if requires_caller_location {
200 params.push(AbiParam::new(pointer_ty(tcx)));
210 pub(crate) fn get_function_name_and_sig<'tcx>(
212 triple: &target_lexicon::Triple,
213 inst: Instance<'tcx>,
214 support_vararg: bool,
215 ) -> (String, Signature) {
216 assert!(!inst.substs.needs_infer());
217 let fn_sig = tcx.normalize_erasing_late_bound_regions(
218 ParamEnv::reveal_all(),
219 &fn_sig_for_fn_abi(tcx, inst),
221 if fn_sig.c_variadic && !support_vararg {
223 tcx.def_span(inst.def_id()),
224 "Variadic function definitions are not yet supported",
227 let sig = clif_sig_from_fn_sig(
231 tcx.def_span(inst.def_id()),
233 inst.def.requires_caller_location(tcx),
235 (tcx.symbol_name(inst).name.to_string(), sig)
238 /// Instance must be monomorphized
239 pub(crate) fn import_function<'tcx>(
241 module: &mut impl Module,
242 inst: Instance<'tcx>,
244 let (name, sig) = get_function_name_and_sig(tcx, module.isa().triple(), inst, true);
246 .declare_function(&name, Linkage::Import, &sig)
250 impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> {
251 /// Instance must be monomorphized
252 pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
253 let func_id = import_function(self.tcx, &mut self.cx.module, inst);
257 .declare_func_in_func(func_id, &mut self.bcx.func);
259 #[cfg(debug_assertions)]
260 self.add_comment(func_ref, format!("{:?}", inst));
265 pub(crate) fn lib_call(
268 input_tys: Vec<types::Type>,
269 output_tys: Vec<types::Type>,
272 let sig = Signature {
273 params: input_tys.iter().cloned().map(AbiParam::new).collect(),
274 returns: output_tys.iter().cloned().map(AbiParam::new).collect(),
275 call_conv: CallConv::triple_default(self.triple()),
280 .declare_function(&name, Linkage::Import, &sig)
285 .declare_func_in_func(func_id, &mut self.bcx.func);
286 let call_inst = self.bcx.ins().call(func_ref, args);
287 #[cfg(debug_assertions)]
289 self.add_comment(call_inst, format!("easy_call {}", name));
291 let results = self.bcx.inst_results(call_inst);
292 assert!(results.len() <= 2, "{}", results.len());
296 pub(crate) fn easy_call(
299 args: &[CValue<'tcx>],
302 let (input_tys, args): (Vec<_>, Vec<_>) = args
306 self.clif_type(arg.layout().ty).unwrap(),
307 arg.load_scalar(self),
311 let return_layout = self.layout_of(return_ty);
312 let return_tys = if let ty::Tuple(tup) = return_ty.kind() {
313 tup.types().map(|ty| self.clif_type(ty).unwrap()).collect()
315 vec![self.clif_type(return_ty).unwrap()]
317 let ret_vals = self.lib_call(name, input_tys, return_tys, &args);
319 [] => CValue::by_ref(
320 Pointer::const_addr(self, i64::from(self.pointer_type.bytes())),
323 [val] => CValue::by_val(val, return_layout),
324 [val, extra] => CValue::by_val_pair(val, extra, return_layout),
330 /// Make a [`CPlace`] capable of holding value of the specified type.
331 fn make_local_place<'tcx>(
332 fx: &mut FunctionCx<'_, 'tcx, impl Module>,
334 layout: TyAndLayout<'tcx>,
337 let place = if is_ssa {
338 if let rustc_target::abi::Abi::ScalarPair(_, _) = layout.abi {
339 CPlace::new_var_pair(fx, local, layout)
341 CPlace::new_var(fx, local, layout)
344 CPlace::new_stack_slot(fx, layout)
347 #[cfg(debug_assertions)]
348 self::comments::add_local_place_comments(fx, place, local);
353 pub(crate) fn codegen_fn_prelude<'tcx>(
354 fx: &mut FunctionCx<'_, 'tcx, impl Module>,
357 let ssa_analyzed = crate::analyze::analyze(fx);
359 #[cfg(debug_assertions)]
360 self::comments::add_args_header_comment(fx);
362 let ret_place = self::returning::codegen_return_param(fx, &ssa_analyzed, start_block);
363 assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE);
365 // None means pass_mode == NoPass
367 Normal(Option<CValue<'tcx>>),
368 Spread(Vec<Option<CValue<'tcx>>>),
375 let arg_ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
377 // Adapted from https://github.com/rust-lang/rust/blob/145155dc96757002c7b2e9de8489416e2fdbbd57/src/librustc_codegen_llvm/mir/mod.rs#L442-L482
378 if Some(local) == fx.mir.spread_arg {
379 // This argument (e.g. the last argument in the "rust-call" ABI)
380 // is a tuple that was spread at the ABI level and now we have
381 // to reconstruct it into a tuple local variable, from multiple
382 // individual function arguments.
384 let tupled_arg_tys = match arg_ty.kind() {
385 ty::Tuple(ref tys) => tys,
386 _ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty),
389 let mut params = Vec::new();
390 for (i, arg_ty) in tupled_arg_tys.types().enumerate() {
391 let param = cvalue_for_param(fx, start_block, Some(local), Some(i), arg_ty);
395 (local, ArgKind::Spread(params), arg_ty)
397 let param = cvalue_for_param(fx, start_block, Some(local), None, arg_ty);
398 (local, ArgKind::Normal(param), arg_ty)
401 .collect::<Vec<(Local, ArgKind<'tcx>, Ty<'tcx>)>>();
403 assert!(fx.caller_location.is_none());
404 if fx.instance.def.requires_caller_location(fx.tcx) {
405 // Store caller location for `#[track_caller]`.
406 fx.caller_location = Some(
407 cvalue_for_param(fx, start_block, None, None, fx.tcx.caller_location_ty()).unwrap(),
411 fx.bcx.switch_to_block(start_block);
414 #[cfg(debug_assertions)]
415 self::comments::add_locals_header_comment(fx);
417 for (local, arg_kind, ty) in func_params {
418 let layout = fx.layout_of(ty);
420 let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
422 // While this is normally an optimization to prevent an unnecessary copy when an argument is
423 // not mutated by the current function, this is necessary to support unsized arguments.
425 ArgKind::Normal(Some(val)) => {
426 if let Some((addr, meta)) = val.try_to_ptr() {
427 let local_decl = &fx.mir.local_decls[local];
428 // v this ! is important
429 let internally_mutable = !val.layout().ty.is_freeze(
430 fx.tcx.at(local_decl.source_info.span),
431 ParamEnv::reveal_all(),
433 if local_decl.mutability == mir::Mutability::Not && !internally_mutable {
434 // We wont mutate this argument, so it is fine to borrow the backing storage
435 // of this argument, to prevent a copy.
437 let place = if let Some(meta) = meta {
438 CPlace::for_ptr_with_extra(addr, meta, val.layout())
440 CPlace::for_ptr(addr, val.layout())
443 #[cfg(debug_assertions)]
444 self::comments::add_local_place_comments(fx, place, local);
446 assert_eq!(fx.local_map.push(place), local);
454 let place = make_local_place(fx, local, layout, is_ssa);
455 assert_eq!(fx.local_map.push(place), local);
458 ArgKind::Normal(param) => {
459 if let Some(param) = param {
460 place.write_cvalue(fx, param);
463 ArgKind::Spread(params) => {
464 for (i, param) in params.into_iter().enumerate() {
465 if let Some(param) = param {
467 .place_field(fx, mir::Field::new(i))
468 .write_cvalue(fx, param);
475 for local in fx.mir.vars_and_temps_iter() {
476 let ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
477 let layout = fx.layout_of(ty);
479 let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
481 let place = make_local_place(fx, local, layout, is_ssa);
482 assert_eq!(fx.local_map.push(place), local);
487 .jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]);
490 pub(crate) fn codegen_terminator_call<'tcx>(
491 fx: &mut FunctionCx<'_, 'tcx, impl Module>,
493 current_block: Block,
494 func: &Operand<'tcx>,
495 args: &[Operand<'tcx>],
496 destination: Option<(Place<'tcx>, BasicBlock)>,
498 let fn_ty = fx.monomorphize(&func.ty(fx.mir, fx.tcx));
501 .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &fn_ty.fn_sig(fx.tcx));
503 let destination = destination.map(|(place, bb)| (trans_place(fx, place), bb));
505 // Handle special calls like instrinsics and empty drop glue.
506 let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() {
507 let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
510 .polymorphize(fx.tcx);
512 if fx.tcx.symbol_name(instance).name.starts_with("llvm.") {
513 crate::intrinsics::codegen_llvm_intrinsic_call(
515 &fx.tcx.symbol_name(instance).name,
524 InstanceDef::Intrinsic(_) => {
525 crate::intrinsics::codegen_intrinsic_call(fx, instance, args, destination, span);
528 InstanceDef::DropGlue(_, None) => {
529 // empty drop glue - a nop.
530 let (_, dest) = destination.expect("Non terminating drop_in_place_real???");
531 let ret_block = fx.get_block(dest);
532 fx.bcx.ins().jump(ret_block, &[]);
541 let is_cold = instance
544 .codegen_fn_attrs(inst.def_id())
546 .contains(CodegenFnAttrFlags::COLD)
550 fx.cold_blocks.insert(current_block);
553 // Unpack arguments tuple for closures
554 let args = if fn_sig.abi == Abi::RustCall {
555 assert_eq!(args.len(), 2, "rust-call abi requires two arguments");
556 let self_arg = trans_operand(fx, &args[0]);
557 let pack_arg = trans_operand(fx, &args[1]);
559 let tupled_arguments = match pack_arg.layout().ty.kind() {
560 ty::Tuple(ref tupled_arguments) => tupled_arguments,
561 _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
564 let mut args = Vec::with_capacity(1 + tupled_arguments.len());
566 for i in 0..tupled_arguments.len() {
567 args.push(pack_arg.value_field(fx, mir::Field::new(i)));
572 .map(|arg| trans_operand(fx, arg))
576 // | indirect call target
577 // | | the first argument to be passed
578 // v v v virtual calls are special cased below
579 let (func_ref, first_arg, is_virtual_call) = match instance {
582 def: InstanceDef::Virtual(_, idx),
585 #[cfg(debug_assertions)]
587 let nop_inst = fx.bcx.ins().nop();
591 "virtual call; self arg pass mode: {:?}",
592 get_pass_mode(fx.tcx, args[0].layout())
596 let (ptr, method) = crate::vtable::get_ptr_and_method_ref(fx, args[0], idx);
597 (Some(method), Single(ptr), true)
604 .map(|arg| adjust_arg_for_abi(fx, *arg))
611 #[cfg(debug_assertions)]
613 let nop_inst = fx.bcx.ins().nop();
614 fx.add_comment(nop_inst, "indirect call");
616 let func = trans_operand(fx, func).load_scalar(fx);
620 .map(|arg| adjust_arg_for_abi(fx, *arg))
627 let ret_place = destination.map(|(place, _)| place);
628 let (call_inst, call_args) =
629 self::returning::codegen_with_call_return_arg(fx, fn_sig, ret_place, |fx, return_ptr| {
630 let mut call_args: Vec<Value> = return_ptr
632 .chain(first_arg.into_iter())
636 .map(|arg| adjust_arg_for_abi(fx, arg).into_iter())
639 .collect::<Vec<_>>();
642 .map(|inst| inst.def.requires_caller_location(fx.tcx))
645 // Pass the caller location for `#[track_caller]`.
646 let caller_location = fx.get_caller_location(span);
647 call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
650 let call_inst = if let Some(func_ref) = func_ref {
651 let sig = clif_sig_from_fn_sig(
657 false, // calls through function pointers never pass the caller location
659 let sig = fx.bcx.import_signature(sig);
660 fx.bcx.ins().call_indirect(sig, func_ref, &call_args)
663 fx.get_function_ref(instance.expect("non-indirect call on non-FnDef type"));
664 fx.bcx.ins().call(func_ref, &call_args)
667 (call_inst, call_args)
670 // FIXME find a cleaner way to support varargs
671 if fn_sig.c_variadic {
672 if fn_sig.abi != Abi::C {
673 fx.tcx.sess.span_fatal(
675 &format!("Variadic call for non-C abi {:?}", fn_sig.abi),
678 let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap();
679 let abi_params = call_args
682 let ty = fx.bcx.func.dfg.value_type(arg);
684 // FIXME set %al to upperbound on float args once floats are supported
687 .span_fatal(span, &format!("Non int ty {:?} for variadic call", ty));
691 .collect::<Vec<AbiParam>>();
692 fx.bcx.func.dfg.signatures[sig_ref].params = abi_params;
695 if let Some((_, dest)) = destination {
696 let ret_block = fx.get_block(dest);
697 fx.bcx.ins().jump(ret_block, &[]);
699 trap_unreachable(fx, "[corruption] Diverging function returned");
703 pub(crate) fn codegen_drop<'tcx>(
704 fx: &mut FunctionCx<'_, 'tcx, impl Module>,
706 drop_place: CPlace<'tcx>,
708 let ty = drop_place.layout().ty;
709 let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx);
711 if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
712 // we don't actually need to drop anything
714 let drop_fn_ty = drop_fn.ty(fx.tcx, ParamEnv::reveal_all());
715 let fn_sig = fx.tcx.normalize_erasing_late_bound_regions(
716 ParamEnv::reveal_all(),
717 &drop_fn_ty.fn_sig(fx.tcx),
719 assert_eq!(fn_sig.output(), fx.tcx.mk_unit());
723 let (ptr, vtable) = drop_place.to_ptr_maybe_unsized();
724 let ptr = ptr.get_addr(fx);
725 let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
727 let sig = clif_sig_from_fn_sig(
733 false, // `drop_in_place` is never `#[track_caller]`
735 let sig = fx.bcx.import_signature(sig);
736 fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
739 assert!(!matches!(drop_fn.def, InstanceDef::Virtual(_, _)));
741 let arg_value = drop_place.place_ref(
743 fx.layout_of(fx.tcx.mk_ref(
744 &ty::RegionKind::ReErased,
747 mutbl: crate::rustc_hir::Mutability::Mut,
751 let arg_value = adjust_arg_for_abi(fx, arg_value);
753 let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>();
755 if drop_fn.def.requires_caller_location(fx.tcx) {
756 // Pass the caller location for `#[track_caller]`.
757 let caller_location = fx.get_caller_location(span);
758 call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
761 let func_ref = fx.get_function_ref(drop_fn);
762 fx.bcx.ins().call(func_ref, &call_args);