]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/abi/mod.rs
Auto merge of #78147 - tmiasko:validate-storage, r=jonas-schievink
[rust.git] / compiler / rustc_codegen_cranelift / src / abi / mod.rs
1 //! Handling of everything related to the calling convention. Also fills `fx.local_map`.
2
3 #[cfg(debug_assertions)]
4 mod comments;
5 mod pass_mode;
6 mod returning;
7
8 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
9 use rustc_target::spec::abi::Abi;
10
11 use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};
12
13 use self::pass_mode::*;
14 use crate::prelude::*;
15
16 pub(crate) use self::returning::{can_return_to_ssa_var, codegen_return};
17
18 // Copied from https://github.com/rust-lang/rust/blob/f52c72948aa1dd718cc1f168d21c91c584c0a662/src/librustc_middle/ty/layout.rs#L2301
19 #[rustfmt::skip]
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;
22
23     // FIXME(davidtwco,eddyb): A `ParamEnv` should be passed through to this function.
24     let ty = instance.ty(tcx, ty::ParamEnv::reveal_all());
25     match *ty.kind() {
26         ty::FnDef(..) => {
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))
35                     .subst(tcx, substs),
36                 _ => unreachable!(),
37             };
38
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);
45                     sig
46                 });
47             }
48             sig
49         }
50         ty::Closure(def_id, substs) => {
51             let sig = substs.as_closure().sig();
52
53             let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
54             sig.map_bound(|sig| {
55                 tcx.mk_fn_sig(
56                     std::iter::once(env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
57                     sig.output(),
58                     sig.c_variadic,
59                     sig.unsafety,
60                     sig.abi,
61                 )
62             })
63         }
64         ty::Generator(_, substs, _) => {
65             let sig = substs.as_generator().poly_sig();
66
67             let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
68             let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
69
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);
74
75             sig.map_bound(|sig| {
76                 let state_did = tcx.require_lang_item(rustc_hir::LangItem::GeneratorState, None);
77                 let state_adt_ref = tcx.adt_def(state_did);
78                 let state_substs =
79                     tcx.intern_substs(&[sig.yield_ty.into(), sig.return_ty.into()]);
80                 let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
81
82                 tcx.mk_fn_sig(
83                     [env_ty, sig.resume_ty].iter(),
84                     &ret_ty,
85                     false,
86                     rustc_hir::Unsafety::Normal,
87                     rustc_target::spec::abi::Abi::Rust,
88                 )
89             })
90         }
91         _ => bug!("unexpected type {:?} in Instance::fn_sig", ty),
92     }
93 }
94
95 fn clif_sig_from_fn_sig<'tcx>(
96     tcx: TyCtxt<'tcx>,
97     triple: &target_lexicon::Triple,
98     sig: FnSig<'tcx>,
99     span: Span,
100     is_vtable_fn: bool,
101     requires_caller_location: bool,
102 ) -> Signature {
103     let abi = match sig.abi {
104         Abi::System => Abi::C,
105         abi => abi,
106     };
107     let (call_conv, inputs, output): (CallConv, Vec<Ty<'tcx>>, Ty<'tcx>) = match abi {
108         Abi::Rust => (
109             CallConv::triple_default(triple),
110             sig.inputs().to_vec(),
111             sig.output(),
112         ),
113         Abi::C | Abi::Unadjusted => (
114             CallConv::triple_default(triple),
115             sig.inputs().to_vec(),
116             sig.output(),
117         ),
118         Abi::SysV64 => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
119         Abi::RustCall => {
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"),
124             };
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())
128         }
129         Abi::System => unreachable!(),
130         Abi::RustIntrinsic => (
131             CallConv::triple_default(triple),
132             sig.inputs().to_vec(),
133             sig.output(),
134         ),
135         _ => unimplemented!("unsupported abi {:?}", sig.abi),
136     };
137
138     let inputs = inputs
139         .into_iter()
140         .enumerate()
141         .map(|(i, ty)| {
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
146                 layout = tcx
147                     .layout_of(ParamEnv::reveal_all().and(tcx.mk_mut_ptr(tcx.mk_unit())))
148                     .unwrap();
149             }
150             let pass_mode = get_pass_mode(tcx, layout);
151             if abi != Abi::Rust && abi != Abi::RustCall && abi != Abi::RustIntrinsic {
152                 match pass_mode {
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();
157                     }
158                     PassMode::ByValPair(_, _) | PassMode::ByRef { size: None } => {
159                         tcx.sess.span_warn(
160                             span,
161                             &format!(
162                                 "Argument of type `{:?}` with pass mode `{:?}` is not yet supported \
163                                 for non-rust abi `{}`. Calling this function may result in a crash.",
164                                 layout.ty,
165                                 pass_mode,
166                                 abi,
167                             ),
168                         );
169                     }
170                 }
171             }
172             pass_mode.get_param_ty(tcx).map(AbiParam::new).into_iter()
173         })
174         .flatten();
175
176     let (mut params, returns): (Vec<_>, Vec<_>) = match get_pass_mode(
177         tcx,
178         tcx.layout_of(ParamEnv::reveal_all().and(output)).unwrap(),
179     ) {
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) => (
183             inputs.collect(),
184             vec![AbiParam::new(ret_ty_a), AbiParam::new(ret_ty_b)],
185         ),
186         PassMode::ByRef { size: Some(_) } => {
187             (
188                 Some(pointer_ty(tcx)) // First param is place to put return val
189                     .into_iter()
190                     .map(|ty| AbiParam::special(ty, ArgumentPurpose::StructReturn))
191                     .chain(inputs)
192                     .collect(),
193                 vec![],
194             )
195         }
196         PassMode::ByRef { size: None } => todo!(),
197     };
198
199     if requires_caller_location {
200         params.push(AbiParam::new(pointer_ty(tcx)));
201     }
202
203     Signature {
204         params,
205         returns,
206         call_conv,
207     }
208 }
209
210 pub(crate) fn get_function_name_and_sig<'tcx>(
211     tcx: TyCtxt<'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),
220     );
221     if fn_sig.c_variadic && !support_vararg {
222         tcx.sess.span_fatal(
223             tcx.def_span(inst.def_id()),
224             "Variadic function definitions are not yet supported",
225         );
226     }
227     let sig = clif_sig_from_fn_sig(
228         tcx,
229         triple,
230         fn_sig,
231         tcx.def_span(inst.def_id()),
232         false,
233         inst.def.requires_caller_location(tcx),
234     );
235     (tcx.symbol_name(inst).name.to_string(), sig)
236 }
237
238 /// Instance must be monomorphized
239 pub(crate) fn import_function<'tcx>(
240     tcx: TyCtxt<'tcx>,
241     module: &mut impl Module,
242     inst: Instance<'tcx>,
243 ) -> FuncId {
244     let (name, sig) = get_function_name_and_sig(tcx, module.isa().triple(), inst, true);
245     module
246         .declare_function(&name, Linkage::Import, &sig)
247         .unwrap()
248 }
249
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);
254         let func_ref = self
255             .cx
256             .module
257             .declare_func_in_func(func_id, &mut self.bcx.func);
258
259         #[cfg(debug_assertions)]
260         self.add_comment(func_ref, format!("{:?}", inst));
261
262         func_ref
263     }
264
265     pub(crate) fn lib_call(
266         &mut self,
267         name: &str,
268         input_tys: Vec<types::Type>,
269         output_tys: Vec<types::Type>,
270         args: &[Value],
271     ) -> &[Value] {
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()),
276         };
277         let func_id = self
278             .cx
279             .module
280             .declare_function(&name, Linkage::Import, &sig)
281             .unwrap();
282         let func_ref = self
283             .cx
284             .module
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)]
288         {
289             self.add_comment(call_inst, format!("easy_call {}", name));
290         }
291         let results = self.bcx.inst_results(call_inst);
292         assert!(results.len() <= 2, "{}", results.len());
293         results
294     }
295
296     pub(crate) fn easy_call(
297         &mut self,
298         name: &str,
299         args: &[CValue<'tcx>],
300         return_ty: Ty<'tcx>,
301     ) -> CValue<'tcx> {
302         let (input_tys, args): (Vec<_>, Vec<_>) = args
303             .into_iter()
304             .map(|arg| {
305                 (
306                     self.clif_type(arg.layout().ty).unwrap(),
307                     arg.load_scalar(self),
308                 )
309             })
310             .unzip();
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()
314         } else {
315             vec![self.clif_type(return_ty).unwrap()]
316         };
317         let ret_vals = self.lib_call(name, input_tys, return_tys, &args);
318         match *ret_vals {
319             [] => CValue::by_ref(
320                 Pointer::const_addr(self, i64::from(self.pointer_type.bytes())),
321                 return_layout,
322             ),
323             [val] => CValue::by_val(val, return_layout),
324             [val, extra] => CValue::by_val_pair(val, extra, return_layout),
325             _ => unreachable!(),
326         }
327     }
328 }
329
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>,
333     local: Local,
334     layout: TyAndLayout<'tcx>,
335     is_ssa: bool,
336 ) -> CPlace<'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)
340         } else {
341             CPlace::new_var(fx, local, layout)
342         }
343     } else {
344         CPlace::new_stack_slot(fx, layout)
345     };
346
347     #[cfg(debug_assertions)]
348     self::comments::add_local_place_comments(fx, place, local);
349
350     place
351 }
352
353 pub(crate) fn codegen_fn_prelude<'tcx>(
354     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
355     start_block: Block,
356 ) {
357     let ssa_analyzed = crate::analyze::analyze(fx);
358
359     #[cfg(debug_assertions)]
360     self::comments::add_args_header_comment(fx);
361
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);
364
365     // None means pass_mode == NoPass
366     enum ArgKind<'tcx> {
367         Normal(Option<CValue<'tcx>>),
368         Spread(Vec<Option<CValue<'tcx>>>),
369     }
370
371     let func_params = fx
372         .mir
373         .args_iter()
374         .map(|local| {
375             let arg_ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
376
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.
383
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),
387                 };
388
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);
392                     params.push(param);
393                 }
394
395                 (local, ArgKind::Spread(params), arg_ty)
396             } else {
397                 let param = cvalue_for_param(fx, start_block, Some(local), None, arg_ty);
398                 (local, ArgKind::Normal(param), arg_ty)
399             }
400         })
401         .collect::<Vec<(Local, ArgKind<'tcx>, Ty<'tcx>)>>();
402
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(),
408         );
409     }
410
411     fx.bcx.switch_to_block(start_block);
412     fx.bcx.ins().nop();
413
414     #[cfg(debug_assertions)]
415     self::comments::add_locals_header_comment(fx);
416
417     for (local, arg_kind, ty) in func_params {
418         let layout = fx.layout_of(ty);
419
420         let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
421
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.
424         match arg_kind {
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(),
432                     );
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.
436
437                         let place = if let Some(meta) = meta {
438                             CPlace::for_ptr_with_extra(addr, meta, val.layout())
439                         } else {
440                             CPlace::for_ptr(addr, val.layout())
441                         };
442
443                         #[cfg(debug_assertions)]
444                         self::comments::add_local_place_comments(fx, place, local);
445
446                         assert_eq!(fx.local_map.push(place), local);
447                         continue;
448                     }
449                 }
450             }
451             _ => {}
452         }
453
454         let place = make_local_place(fx, local, layout, is_ssa);
455         assert_eq!(fx.local_map.push(place), local);
456
457         match arg_kind {
458             ArgKind::Normal(param) => {
459                 if let Some(param) = param {
460                     place.write_cvalue(fx, param);
461                 }
462             }
463             ArgKind::Spread(params) => {
464                 for (i, param) in params.into_iter().enumerate() {
465                     if let Some(param) = param {
466                         place
467                             .place_field(fx, mir::Field::new(i))
468                             .write_cvalue(fx, param);
469                     }
470                 }
471             }
472         }
473     }
474
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);
478
479         let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
480
481         let place = make_local_place(fx, local, layout, is_ssa);
482         assert_eq!(fx.local_map.push(place), local);
483     }
484
485     fx.bcx
486         .ins()
487         .jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]);
488 }
489
490 pub(crate) fn codegen_terminator_call<'tcx>(
491     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
492     span: Span,
493     current_block: Block,
494     func: &Operand<'tcx>,
495     args: &[Operand<'tcx>],
496     destination: Option<(Place<'tcx>, BasicBlock)>,
497 ) {
498     let fn_ty = fx.monomorphize(&func.ty(fx.mir, fx.tcx));
499     let fn_sig = fx
500         .tcx
501         .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &fn_ty.fn_sig(fx.tcx));
502
503     let destination = destination.map(|(place, bb)| (trans_place(fx, place), bb));
504
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)
508             .unwrap()
509             .unwrap()
510             .polymorphize(fx.tcx);
511
512         if fx.tcx.symbol_name(instance).name.starts_with("llvm.") {
513             crate::intrinsics::codegen_llvm_intrinsic_call(
514                 fx,
515                 &fx.tcx.symbol_name(instance).name,
516                 substs,
517                 args,
518                 destination,
519             );
520             return;
521         }
522
523         match instance.def {
524             InstanceDef::Intrinsic(_) => {
525                 crate::intrinsics::codegen_intrinsic_call(fx, instance, args, destination, span);
526                 return;
527             }
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, &[]);
533                 return;
534             }
535             _ => Some(instance),
536         }
537     } else {
538         None
539     };
540
541     let is_cold = instance
542         .map(|inst| {
543             fx.tcx
544                 .codegen_fn_attrs(inst.def_id())
545                 .flags
546                 .contains(CodegenFnAttrFlags::COLD)
547         })
548         .unwrap_or(false);
549     if is_cold {
550         fx.cold_blocks.insert(current_block);
551     }
552
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]);
558
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"),
562         };
563
564         let mut args = Vec::with_capacity(1 + tupled_arguments.len());
565         args.push(self_arg);
566         for i in 0..tupled_arguments.len() {
567             args.push(pack_arg.value_field(fx, mir::Field::new(i)));
568         }
569         args
570     } else {
571         args.into_iter()
572             .map(|arg| trans_operand(fx, arg))
573             .collect::<Vec<_>>()
574     };
575
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 {
580         // Trait object call
581         Some(Instance {
582             def: InstanceDef::Virtual(_, idx),
583             ..
584         }) => {
585             #[cfg(debug_assertions)]
586             {
587                 let nop_inst = fx.bcx.ins().nop();
588                 fx.add_comment(
589                     nop_inst,
590                     format!(
591                         "virtual call; self arg pass mode: {:?}",
592                         get_pass_mode(fx.tcx, args[0].layout())
593                     ),
594                 );
595             }
596             let (ptr, method) = crate::vtable::get_ptr_and_method_ref(fx, args[0], idx);
597             (Some(method), Single(ptr), true)
598         }
599
600         // Normal call
601         Some(_) => (
602             None,
603             args.get(0)
604                 .map(|arg| adjust_arg_for_abi(fx, *arg))
605                 .unwrap_or(Empty),
606             false,
607         ),
608
609         // Indirect call
610         None => {
611             #[cfg(debug_assertions)]
612             {
613                 let nop_inst = fx.bcx.ins().nop();
614                 fx.add_comment(nop_inst, "indirect call");
615             }
616             let func = trans_operand(fx, func).load_scalar(fx);
617             (
618                 Some(func),
619                 args.get(0)
620                     .map(|arg| adjust_arg_for_abi(fx, *arg))
621                     .unwrap_or(Empty),
622                 false,
623             )
624         }
625     };
626
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
631                 .into_iter()
632                 .chain(first_arg.into_iter())
633                 .chain(
634                     args.into_iter()
635                         .skip(1)
636                         .map(|arg| adjust_arg_for_abi(fx, arg).into_iter())
637                         .flatten(),
638                 )
639                 .collect::<Vec<_>>();
640
641             if instance
642                 .map(|inst| inst.def.requires_caller_location(fx.tcx))
643                 .unwrap_or(false)
644             {
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());
648             }
649
650             let call_inst = if let Some(func_ref) = func_ref {
651                 let sig = clif_sig_from_fn_sig(
652                     fx.tcx,
653                     fx.triple(),
654                     fn_sig,
655                     span,
656                     is_virtual_call,
657                     false, // calls through function pointers never pass the caller location
658                 );
659                 let sig = fx.bcx.import_signature(sig);
660                 fx.bcx.ins().call_indirect(sig, func_ref, &call_args)
661             } else {
662                 let func_ref =
663                     fx.get_function_ref(instance.expect("non-indirect call on non-FnDef type"));
664                 fx.bcx.ins().call(func_ref, &call_args)
665             };
666
667             (call_inst, call_args)
668         });
669
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(
674                 span,
675                 &format!("Variadic call for non-C abi {:?}", fn_sig.abi),
676             );
677         }
678         let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap();
679         let abi_params = call_args
680             .into_iter()
681             .map(|arg| {
682                 let ty = fx.bcx.func.dfg.value_type(arg);
683                 if !ty.is_int() {
684                     // FIXME set %al to upperbound on float args once floats are supported
685                     fx.tcx
686                         .sess
687                         .span_fatal(span, &format!("Non int ty {:?} for variadic call", ty));
688                 }
689                 AbiParam::new(ty)
690             })
691             .collect::<Vec<AbiParam>>();
692         fx.bcx.func.dfg.signatures[sig_ref].params = abi_params;
693     }
694
695     if let Some((_, dest)) = destination {
696         let ret_block = fx.get_block(dest);
697         fx.bcx.ins().jump(ret_block, &[]);
698     } else {
699         trap_unreachable(fx, "[corruption] Diverging function returned");
700     }
701 }
702
703 pub(crate) fn codegen_drop<'tcx>(
704     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
705     span: Span,
706     drop_place: CPlace<'tcx>,
707 ) {
708     let ty = drop_place.layout().ty;
709     let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx);
710
711     if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
712         // we don't actually need to drop anything
713     } else {
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),
718         );
719         assert_eq!(fn_sig.output(), fx.tcx.mk_unit());
720
721         match ty.kind() {
722             ty::Dynamic(..) => {
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());
726
727                 let sig = clif_sig_from_fn_sig(
728                     fx.tcx,
729                     fx.triple(),
730                     fn_sig,
731                     span,
732                     true,
733                     false, // `drop_in_place` is never `#[track_caller]`
734                 );
735                 let sig = fx.bcx.import_signature(sig);
736                 fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
737             }
738             _ => {
739                 assert!(!matches!(drop_fn.def, InstanceDef::Virtual(_, _)));
740
741                 let arg_value = drop_place.place_ref(
742                     fx,
743                     fx.layout_of(fx.tcx.mk_ref(
744                         &ty::RegionKind::ReErased,
745                         TypeAndMut {
746                             ty,
747                             mutbl: crate::rustc_hir::Mutability::Mut,
748                         },
749                     )),
750                 );
751                 let arg_value = adjust_arg_for_abi(fx, arg_value);
752
753                 let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>();
754
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());
759                 }
760
761                 let func_ref = fx.get_function_ref(drop_fn);
762                 fx.bcx.ins().call(func_ref, &call_args);
763             }
764         }
765     }
766 }