]> git.lizzy.rs Git - rust.git/blob - src/abi/mod.rs
Make BoundRegion have a kind of BoungRegionKind
[rust.git] / 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::BoundRegion { kind: 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
218         .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_sig_for_fn_abi(tcx, inst));
219     if fn_sig.c_variadic && !support_vararg {
220         tcx.sess.span_fatal(
221             tcx.def_span(inst.def_id()),
222             "Variadic function definitions are not yet supported",
223         );
224     }
225     let sig = clif_sig_from_fn_sig(
226         tcx,
227         triple,
228         fn_sig,
229         tcx.def_span(inst.def_id()),
230         false,
231         inst.def.requires_caller_location(tcx),
232     );
233     (tcx.symbol_name(inst).name.to_string(), sig)
234 }
235
236 /// Instance must be monomorphized
237 pub(crate) fn import_function<'tcx>(
238     tcx: TyCtxt<'tcx>,
239     module: &mut impl Module,
240     inst: Instance<'tcx>,
241 ) -> FuncId {
242     let (name, sig) = get_function_name_and_sig(tcx, module.isa().triple(), inst, true);
243     module
244         .declare_function(&name, Linkage::Import, &sig)
245         .unwrap()
246 }
247
248 impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> {
249     /// Instance must be monomorphized
250     pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
251         let func_id = import_function(self.tcx, &mut self.cx.module, inst);
252         let func_ref = self
253             .cx
254             .module
255             .declare_func_in_func(func_id, &mut self.bcx.func);
256
257         #[cfg(debug_assertions)]
258         self.add_comment(func_ref, format!("{:?}", inst));
259
260         func_ref
261     }
262
263     pub(crate) fn lib_call(
264         &mut self,
265         name: &str,
266         input_tys: Vec<types::Type>,
267         output_tys: Vec<types::Type>,
268         args: &[Value],
269     ) -> &[Value] {
270         let sig = Signature {
271             params: input_tys.iter().cloned().map(AbiParam::new).collect(),
272             returns: output_tys.iter().cloned().map(AbiParam::new).collect(),
273             call_conv: CallConv::triple_default(self.triple()),
274         };
275         let func_id = self
276             .cx
277             .module
278             .declare_function(&name, Linkage::Import, &sig)
279             .unwrap();
280         let func_ref = self
281             .cx
282             .module
283             .declare_func_in_func(func_id, &mut self.bcx.func);
284         let call_inst = self.bcx.ins().call(func_ref, args);
285         #[cfg(debug_assertions)]
286         {
287             self.add_comment(call_inst, format!("easy_call {}", name));
288         }
289         let results = self.bcx.inst_results(call_inst);
290         assert!(results.len() <= 2, "{}", results.len());
291         results
292     }
293
294     pub(crate) fn easy_call(
295         &mut self,
296         name: &str,
297         args: &[CValue<'tcx>],
298         return_ty: Ty<'tcx>,
299     ) -> CValue<'tcx> {
300         let (input_tys, args): (Vec<_>, Vec<_>) = args
301             .iter()
302             .map(|arg| {
303                 (
304                     self.clif_type(arg.layout().ty).unwrap(),
305                     arg.load_scalar(self),
306                 )
307             })
308             .unzip();
309         let return_layout = self.layout_of(return_ty);
310         let return_tys = if let ty::Tuple(tup) = return_ty.kind() {
311             tup.types().map(|ty| self.clif_type(ty).unwrap()).collect()
312         } else {
313             vec![self.clif_type(return_ty).unwrap()]
314         };
315         let ret_vals = self.lib_call(name, input_tys, return_tys, &args);
316         match *ret_vals {
317             [] => CValue::by_ref(
318                 Pointer::const_addr(self, i64::from(self.pointer_type.bytes())),
319                 return_layout,
320             ),
321             [val] => CValue::by_val(val, return_layout),
322             [val, extra] => CValue::by_val_pair(val, extra, return_layout),
323             _ => unreachable!(),
324         }
325     }
326 }
327
328 /// Make a [`CPlace`] capable of holding value of the specified type.
329 fn make_local_place<'tcx>(
330     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
331     local: Local,
332     layout: TyAndLayout<'tcx>,
333     is_ssa: bool,
334 ) -> CPlace<'tcx> {
335     let place = if is_ssa {
336         if let rustc_target::abi::Abi::ScalarPair(_, _) = layout.abi {
337             CPlace::new_var_pair(fx, local, layout)
338         } else {
339             CPlace::new_var(fx, local, layout)
340         }
341     } else {
342         CPlace::new_stack_slot(fx, layout)
343     };
344
345     #[cfg(debug_assertions)]
346     self::comments::add_local_place_comments(fx, place, local);
347
348     place
349 }
350
351 pub(crate) fn codegen_fn_prelude<'tcx>(
352     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
353     start_block: Block,
354 ) {
355     let ssa_analyzed = crate::analyze::analyze(fx);
356
357     #[cfg(debug_assertions)]
358     self::comments::add_args_header_comment(fx);
359
360     let ret_place = self::returning::codegen_return_param(fx, &ssa_analyzed, start_block);
361     assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE);
362
363     // None means pass_mode == NoPass
364     enum ArgKind<'tcx> {
365         Normal(Option<CValue<'tcx>>),
366         Spread(Vec<Option<CValue<'tcx>>>),
367     }
368
369     let func_params = fx
370         .mir
371         .args_iter()
372         .map(|local| {
373             let arg_ty = fx.monomorphize(fx.mir.local_decls[local].ty);
374
375             // Adapted from https://github.com/rust-lang/rust/blob/145155dc96757002c7b2e9de8489416e2fdbbd57/src/librustc_codegen_llvm/mir/mod.rs#L442-L482
376             if Some(local) == fx.mir.spread_arg {
377                 // This argument (e.g. the last argument in the "rust-call" ABI)
378                 // is a tuple that was spread at the ABI level and now we have
379                 // to reconstruct it into a tuple local variable, from multiple
380                 // individual function arguments.
381
382                 let tupled_arg_tys = match arg_ty.kind() {
383                     ty::Tuple(ref tys) => tys,
384                     _ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty),
385                 };
386
387                 let mut params = Vec::new();
388                 for (i, arg_ty) in tupled_arg_tys.types().enumerate() {
389                     let param = cvalue_for_param(fx, start_block, Some(local), Some(i), arg_ty);
390                     params.push(param);
391                 }
392
393                 (local, ArgKind::Spread(params), arg_ty)
394             } else {
395                 let param = cvalue_for_param(fx, start_block, Some(local), None, arg_ty);
396                 (local, ArgKind::Normal(param), arg_ty)
397             }
398         })
399         .collect::<Vec<(Local, ArgKind<'tcx>, Ty<'tcx>)>>();
400
401     assert!(fx.caller_location.is_none());
402     if fx.instance.def.requires_caller_location(fx.tcx) {
403         // Store caller location for `#[track_caller]`.
404         fx.caller_location = Some(
405             cvalue_for_param(fx, start_block, None, None, fx.tcx.caller_location_ty()).unwrap(),
406         );
407     }
408
409     fx.bcx.switch_to_block(start_block);
410     fx.bcx.ins().nop();
411
412     #[cfg(debug_assertions)]
413     self::comments::add_locals_header_comment(fx);
414
415     for (local, arg_kind, ty) in func_params {
416         let layout = fx.layout_of(ty);
417
418         let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
419
420         // While this is normally an optimization to prevent an unnecessary copy when an argument is
421         // not mutated by the current function, this is necessary to support unsized arguments.
422         if let ArgKind::Normal(Some(val)) = arg_kind {
423             if let Some((addr, meta)) = val.try_to_ptr() {
424                 let local_decl = &fx.mir.local_decls[local];
425                 //                       v this ! is important
426                 let internally_mutable = !val.layout().ty.is_freeze(
427                     fx.tcx.at(local_decl.source_info.span),
428                     ParamEnv::reveal_all(),
429                 );
430                 if local_decl.mutability == mir::Mutability::Not && !internally_mutable {
431                     // We wont mutate this argument, so it is fine to borrow the backing storage
432                     // of this argument, to prevent a copy.
433
434                     let place = if let Some(meta) = meta {
435                         CPlace::for_ptr_with_extra(addr, meta, val.layout())
436                     } else {
437                         CPlace::for_ptr(addr, val.layout())
438                     };
439
440                     #[cfg(debug_assertions)]
441                     self::comments::add_local_place_comments(fx, place, local);
442
443                     assert_eq!(fx.local_map.push(place), local);
444                     continue;
445                 }
446             }
447         }
448
449         let place = make_local_place(fx, local, layout, is_ssa);
450         assert_eq!(fx.local_map.push(place), local);
451
452         match arg_kind {
453             ArgKind::Normal(param) => {
454                 if let Some(param) = param {
455                     place.write_cvalue(fx, param);
456                 }
457             }
458             ArgKind::Spread(params) => {
459                 for (i, param) in params.into_iter().enumerate() {
460                     if let Some(param) = param {
461                         place
462                             .place_field(fx, mir::Field::new(i))
463                             .write_cvalue(fx, param);
464                     }
465                 }
466             }
467         }
468     }
469
470     for local in fx.mir.vars_and_temps_iter() {
471         let ty = fx.monomorphize(fx.mir.local_decls[local].ty);
472         let layout = fx.layout_of(ty);
473
474         let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
475
476         let place = make_local_place(fx, local, layout, is_ssa);
477         assert_eq!(fx.local_map.push(place), local);
478     }
479
480     fx.bcx
481         .ins()
482         .jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]);
483 }
484
485 pub(crate) fn codegen_terminator_call<'tcx>(
486     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
487     span: Span,
488     current_block: Block,
489     func: &Operand<'tcx>,
490     args: &[Operand<'tcx>],
491     destination: Option<(Place<'tcx>, BasicBlock)>,
492 ) {
493     let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx));
494     let fn_sig = fx
495         .tcx
496         .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx));
497
498     let destination = destination.map(|(place, bb)| (codegen_place(fx, place), bb));
499
500     // Handle special calls like instrinsics and empty drop glue.
501     let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() {
502         let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
503             .unwrap()
504             .unwrap()
505             .polymorphize(fx.tcx);
506
507         if fx.tcx.symbol_name(instance).name.starts_with("llvm.") {
508             crate::intrinsics::codegen_llvm_intrinsic_call(
509                 fx,
510                 &fx.tcx.symbol_name(instance).name,
511                 substs,
512                 args,
513                 destination,
514             );
515             return;
516         }
517
518         match instance.def {
519             InstanceDef::Intrinsic(_) => {
520                 crate::intrinsics::codegen_intrinsic_call(fx, instance, args, destination, span);
521                 return;
522             }
523             InstanceDef::DropGlue(_, None) => {
524                 // empty drop glue - a nop.
525                 let (_, dest) = destination.expect("Non terminating drop_in_place_real???");
526                 let ret_block = fx.get_block(dest);
527                 fx.bcx.ins().jump(ret_block, &[]);
528                 return;
529             }
530             _ => Some(instance),
531         }
532     } else {
533         None
534     };
535
536     let is_cold = instance
537         .map(|inst| {
538             fx.tcx
539                 .codegen_fn_attrs(inst.def_id())
540                 .flags
541                 .contains(CodegenFnAttrFlags::COLD)
542         })
543         .unwrap_or(false);
544     if is_cold {
545         fx.cold_blocks.insert(current_block);
546     }
547
548     // Unpack arguments tuple for closures
549     let args = if fn_sig.abi == Abi::RustCall {
550         assert_eq!(args.len(), 2, "rust-call abi requires two arguments");
551         let self_arg = codegen_operand(fx, &args[0]);
552         let pack_arg = codegen_operand(fx, &args[1]);
553
554         let tupled_arguments = match pack_arg.layout().ty.kind() {
555             ty::Tuple(ref tupled_arguments) => tupled_arguments,
556             _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
557         };
558
559         let mut args = Vec::with_capacity(1 + tupled_arguments.len());
560         args.push(self_arg);
561         for i in 0..tupled_arguments.len() {
562             args.push(pack_arg.value_field(fx, mir::Field::new(i)));
563         }
564         args
565     } else {
566         args.iter()
567             .map(|arg| codegen_operand(fx, arg))
568             .collect::<Vec<_>>()
569     };
570
571     //   | indirect call target
572     //   |         | the first argument to be passed
573     //   v         v          v virtual calls are special cased below
574     let (func_ref, first_arg, is_virtual_call) = match instance {
575         // Trait object call
576         Some(Instance {
577             def: InstanceDef::Virtual(_, idx),
578             ..
579         }) => {
580             #[cfg(debug_assertions)]
581             {
582                 let nop_inst = fx.bcx.ins().nop();
583                 fx.add_comment(
584                     nop_inst,
585                     format!(
586                         "virtual call; self arg pass mode: {:?}",
587                         get_pass_mode(fx.tcx, args[0].layout())
588                     ),
589                 );
590             }
591             let (ptr, method) = crate::vtable::get_ptr_and_method_ref(fx, args[0], idx);
592             (Some(method), Single(ptr), true)
593         }
594
595         // Normal call
596         Some(_) => (
597             None,
598             args.get(0)
599                 .map(|arg| adjust_arg_for_abi(fx, *arg))
600                 .unwrap_or(Empty),
601             false,
602         ),
603
604         // Indirect call
605         None => {
606             #[cfg(debug_assertions)]
607             {
608                 let nop_inst = fx.bcx.ins().nop();
609                 fx.add_comment(nop_inst, "indirect call");
610             }
611             let func = codegen_operand(fx, func).load_scalar(fx);
612             (
613                 Some(func),
614                 args.get(0)
615                     .map(|arg| adjust_arg_for_abi(fx, *arg))
616                     .unwrap_or(Empty),
617                 false,
618             )
619         }
620     };
621
622     let ret_place = destination.map(|(place, _)| place);
623     let (call_inst, call_args) =
624         self::returning::codegen_with_call_return_arg(fx, fn_sig, ret_place, |fx, return_ptr| {
625             let mut call_args: Vec<Value> = return_ptr
626                 .into_iter()
627                 .chain(first_arg.into_iter())
628                 .chain(
629                     args.into_iter()
630                         .skip(1)
631                         .map(|arg| adjust_arg_for_abi(fx, arg).into_iter())
632                         .flatten(),
633                 )
634                 .collect::<Vec<_>>();
635
636             if instance
637                 .map(|inst| inst.def.requires_caller_location(fx.tcx))
638                 .unwrap_or(false)
639             {
640                 // Pass the caller location for `#[track_caller]`.
641                 let caller_location = fx.get_caller_location(span);
642                 call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
643             }
644
645             let call_inst = if let Some(func_ref) = func_ref {
646                 let sig = clif_sig_from_fn_sig(
647                     fx.tcx,
648                     fx.triple(),
649                     fn_sig,
650                     span,
651                     is_virtual_call,
652                     false, // calls through function pointers never pass the caller location
653                 );
654                 let sig = fx.bcx.import_signature(sig);
655                 fx.bcx.ins().call_indirect(sig, func_ref, &call_args)
656             } else {
657                 let func_ref =
658                     fx.get_function_ref(instance.expect("non-indirect call on non-FnDef type"));
659                 fx.bcx.ins().call(func_ref, &call_args)
660             };
661
662             (call_inst, call_args)
663         });
664
665     // FIXME find a cleaner way to support varargs
666     if fn_sig.c_variadic {
667         if fn_sig.abi != Abi::C {
668             fx.tcx.sess.span_fatal(
669                 span,
670                 &format!("Variadic call for non-C abi {:?}", fn_sig.abi),
671             );
672         }
673         let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap();
674         let abi_params = call_args
675             .into_iter()
676             .map(|arg| {
677                 let ty = fx.bcx.func.dfg.value_type(arg);
678                 if !ty.is_int() {
679                     // FIXME set %al to upperbound on float args once floats are supported
680                     fx.tcx
681                         .sess
682                         .span_fatal(span, &format!("Non int ty {:?} for variadic call", ty));
683                 }
684                 AbiParam::new(ty)
685             })
686             .collect::<Vec<AbiParam>>();
687         fx.bcx.func.dfg.signatures[sig_ref].params = abi_params;
688     }
689
690     if let Some((_, dest)) = destination {
691         let ret_block = fx.get_block(dest);
692         fx.bcx.ins().jump(ret_block, &[]);
693     } else {
694         trap_unreachable(fx, "[corruption] Diverging function returned");
695     }
696 }
697
698 pub(crate) fn codegen_drop<'tcx>(
699     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
700     span: Span,
701     drop_place: CPlace<'tcx>,
702 ) {
703     let ty = drop_place.layout().ty;
704     let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx);
705
706     if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
707         // we don't actually need to drop anything
708     } else {
709         let drop_fn_ty = drop_fn.ty(fx.tcx, ParamEnv::reveal_all());
710         let fn_sig = fx.tcx.normalize_erasing_late_bound_regions(
711             ParamEnv::reveal_all(),
712             drop_fn_ty.fn_sig(fx.tcx),
713         );
714         assert_eq!(fn_sig.output(), fx.tcx.mk_unit());
715
716         match ty.kind() {
717             ty::Dynamic(..) => {
718                 let (ptr, vtable) = drop_place.to_ptr_maybe_unsized();
719                 let ptr = ptr.get_addr(fx);
720                 let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
721
722                 let sig = clif_sig_from_fn_sig(
723                     fx.tcx,
724                     fx.triple(),
725                     fn_sig,
726                     span,
727                     true,
728                     false, // `drop_in_place` is never `#[track_caller]`
729                 );
730                 let sig = fx.bcx.import_signature(sig);
731                 fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
732             }
733             _ => {
734                 assert!(!matches!(drop_fn.def, InstanceDef::Virtual(_, _)));
735
736                 let arg_value = drop_place.place_ref(
737                     fx,
738                     fx.layout_of(fx.tcx.mk_ref(
739                         &ty::RegionKind::ReErased,
740                         TypeAndMut {
741                             ty,
742                             mutbl: crate::rustc_hir::Mutability::Mut,
743                         },
744                     )),
745                 );
746                 let arg_value = adjust_arg_for_abi(fx, arg_value);
747
748                 let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>();
749
750                 if drop_fn.def.requires_caller_location(fx.tcx) {
751                     // Pass the caller location for `#[track_caller]`.
752                     let caller_location = fx.get_caller_location(span);
753                     call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
754                 }
755
756                 let func_ref = fx.get_function_ref(drop_fn);
757                 fx.bcx.ins().call(func_ref, &call_args);
758             }
759         }
760     }
761 }