]> git.lizzy.rs Git - rust.git/blob - src/abi/mod.rs
Rename trans to codegen
[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::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             .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         if let ArgKind::Normal(Some(val)) = arg_kind {
425             if let Some((addr, meta)) = val.try_to_ptr() {
426                 let local_decl = &fx.mir.local_decls[local];
427                 //                       v this ! is important
428                 let internally_mutable = !val.layout().ty.is_freeze(
429                     fx.tcx.at(local_decl.source_info.span),
430                     ParamEnv::reveal_all(),
431                 );
432                 if local_decl.mutability == mir::Mutability::Not && !internally_mutable {
433                     // We wont mutate this argument, so it is fine to borrow the backing storage
434                     // of this argument, to prevent a copy.
435
436                     let place = if let Some(meta) = meta {
437                         CPlace::for_ptr_with_extra(addr, meta, val.layout())
438                     } else {
439                         CPlace::for_ptr(addr, val.layout())
440                     };
441
442                     #[cfg(debug_assertions)]
443                     self::comments::add_local_place_comments(fx, place, local);
444
445                     assert_eq!(fx.local_map.push(place), local);
446                     continue;
447                 }
448             }
449         }
450
451         let place = make_local_place(fx, local, layout, is_ssa);
452         assert_eq!(fx.local_map.push(place), local);
453
454         match arg_kind {
455             ArgKind::Normal(param) => {
456                 if let Some(param) = param {
457                     place.write_cvalue(fx, param);
458                 }
459             }
460             ArgKind::Spread(params) => {
461                 for (i, param) in params.into_iter().enumerate() {
462                     if let Some(param) = param {
463                         place
464                             .place_field(fx, mir::Field::new(i))
465                             .write_cvalue(fx, param);
466                     }
467                 }
468             }
469         }
470     }
471
472     for local in fx.mir.vars_and_temps_iter() {
473         let ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
474         let layout = fx.layout_of(ty);
475
476         let is_ssa = ssa_analyzed[local] == crate::analyze::SsaKind::Ssa;
477
478         let place = make_local_place(fx, local, layout, is_ssa);
479         assert_eq!(fx.local_map.push(place), local);
480     }
481
482     fx.bcx
483         .ins()
484         .jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]);
485 }
486
487 pub(crate) fn codegen_terminator_call<'tcx>(
488     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
489     span: Span,
490     current_block: Block,
491     func: &Operand<'tcx>,
492     args: &[Operand<'tcx>],
493     destination: Option<(Place<'tcx>, BasicBlock)>,
494 ) {
495     let fn_ty = fx.monomorphize(&func.ty(fx.mir, fx.tcx));
496     let fn_sig = fx
497         .tcx
498         .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &fn_ty.fn_sig(fx.tcx));
499
500     let destination = destination.map(|(place, bb)| (codegen_place(fx, place), bb));
501
502     // Handle special calls like instrinsics and empty drop glue.
503     let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() {
504         let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
505             .unwrap()
506             .unwrap()
507             .polymorphize(fx.tcx);
508
509         if fx.tcx.symbol_name(instance).name.starts_with("llvm.") {
510             crate::intrinsics::codegen_llvm_intrinsic_call(
511                 fx,
512                 &fx.tcx.symbol_name(instance).name,
513                 substs,
514                 args,
515                 destination,
516             );
517             return;
518         }
519
520         match instance.def {
521             InstanceDef::Intrinsic(_) => {
522                 crate::intrinsics::codegen_intrinsic_call(fx, instance, args, destination, span);
523                 return;
524             }
525             InstanceDef::DropGlue(_, None) => {
526                 // empty drop glue - a nop.
527                 let (_, dest) = destination.expect("Non terminating drop_in_place_real???");
528                 let ret_block = fx.get_block(dest);
529                 fx.bcx.ins().jump(ret_block, &[]);
530                 return;
531             }
532             _ => Some(instance),
533         }
534     } else {
535         None
536     };
537
538     let is_cold = instance
539         .map(|inst| {
540             fx.tcx
541                 .codegen_fn_attrs(inst.def_id())
542                 .flags
543                 .contains(CodegenFnAttrFlags::COLD)
544         })
545         .unwrap_or(false);
546     if is_cold {
547         fx.cold_blocks.insert(current_block);
548     }
549
550     // Unpack arguments tuple for closures
551     let args = if fn_sig.abi == Abi::RustCall {
552         assert_eq!(args.len(), 2, "rust-call abi requires two arguments");
553         let self_arg = codegen_operand(fx, &args[0]);
554         let pack_arg = codegen_operand(fx, &args[1]);
555
556         let tupled_arguments = match pack_arg.layout().ty.kind() {
557             ty::Tuple(ref tupled_arguments) => tupled_arguments,
558             _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
559         };
560
561         let mut args = Vec::with_capacity(1 + tupled_arguments.len());
562         args.push(self_arg);
563         for i in 0..tupled_arguments.len() {
564             args.push(pack_arg.value_field(fx, mir::Field::new(i)));
565         }
566         args
567     } else {
568         args.iter()
569             .map(|arg| codegen_operand(fx, arg))
570             .collect::<Vec<_>>()
571     };
572
573     //   | indirect call target
574     //   |         | the first argument to be passed
575     //   v         v          v virtual calls are special cased below
576     let (func_ref, first_arg, is_virtual_call) = match instance {
577         // Trait object call
578         Some(Instance {
579             def: InstanceDef::Virtual(_, idx),
580             ..
581         }) => {
582             #[cfg(debug_assertions)]
583             {
584                 let nop_inst = fx.bcx.ins().nop();
585                 fx.add_comment(
586                     nop_inst,
587                     format!(
588                         "virtual call; self arg pass mode: {:?}",
589                         get_pass_mode(fx.tcx, args[0].layout())
590                     ),
591                 );
592             }
593             let (ptr, method) = crate::vtable::get_ptr_and_method_ref(fx, args[0], idx);
594             (Some(method), Single(ptr), true)
595         }
596
597         // Normal call
598         Some(_) => (
599             None,
600             args.get(0)
601                 .map(|arg| adjust_arg_for_abi(fx, *arg))
602                 .unwrap_or(Empty),
603             false,
604         ),
605
606         // Indirect call
607         None => {
608             #[cfg(debug_assertions)]
609             {
610                 let nop_inst = fx.bcx.ins().nop();
611                 fx.add_comment(nop_inst, "indirect call");
612             }
613             let func = codegen_operand(fx, func).load_scalar(fx);
614             (
615                 Some(func),
616                 args.get(0)
617                     .map(|arg| adjust_arg_for_abi(fx, *arg))
618                     .unwrap_or(Empty),
619                 false,
620             )
621         }
622     };
623
624     let ret_place = destination.map(|(place, _)| place);
625     let (call_inst, call_args) =
626         self::returning::codegen_with_call_return_arg(fx, fn_sig, ret_place, |fx, return_ptr| {
627             let mut call_args: Vec<Value> = return_ptr
628                 .into_iter()
629                 .chain(first_arg.into_iter())
630                 .chain(
631                     args.into_iter()
632                         .skip(1)
633                         .map(|arg| adjust_arg_for_abi(fx, arg).into_iter())
634                         .flatten(),
635                 )
636                 .collect::<Vec<_>>();
637
638             if instance
639                 .map(|inst| inst.def.requires_caller_location(fx.tcx))
640                 .unwrap_or(false)
641             {
642                 // Pass the caller location for `#[track_caller]`.
643                 let caller_location = fx.get_caller_location(span);
644                 call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
645             }
646
647             let call_inst = if let Some(func_ref) = func_ref {
648                 let sig = clif_sig_from_fn_sig(
649                     fx.tcx,
650                     fx.triple(),
651                     fn_sig,
652                     span,
653                     is_virtual_call,
654                     false, // calls through function pointers never pass the caller location
655                 );
656                 let sig = fx.bcx.import_signature(sig);
657                 fx.bcx.ins().call_indirect(sig, func_ref, &call_args)
658             } else {
659                 let func_ref =
660                     fx.get_function_ref(instance.expect("non-indirect call on non-FnDef type"));
661                 fx.bcx.ins().call(func_ref, &call_args)
662             };
663
664             (call_inst, call_args)
665         });
666
667     // FIXME find a cleaner way to support varargs
668     if fn_sig.c_variadic {
669         if fn_sig.abi != Abi::C {
670             fx.tcx.sess.span_fatal(
671                 span,
672                 &format!("Variadic call for non-C abi {:?}", fn_sig.abi),
673             );
674         }
675         let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap();
676         let abi_params = call_args
677             .into_iter()
678             .map(|arg| {
679                 let ty = fx.bcx.func.dfg.value_type(arg);
680                 if !ty.is_int() {
681                     // FIXME set %al to upperbound on float args once floats are supported
682                     fx.tcx
683                         .sess
684                         .span_fatal(span, &format!("Non int ty {:?} for variadic call", ty));
685                 }
686                 AbiParam::new(ty)
687             })
688             .collect::<Vec<AbiParam>>();
689         fx.bcx.func.dfg.signatures[sig_ref].params = abi_params;
690     }
691
692     if let Some((_, dest)) = destination {
693         let ret_block = fx.get_block(dest);
694         fx.bcx.ins().jump(ret_block, &[]);
695     } else {
696         trap_unreachable(fx, "[corruption] Diverging function returned");
697     }
698 }
699
700 pub(crate) fn codegen_drop<'tcx>(
701     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
702     span: Span,
703     drop_place: CPlace<'tcx>,
704 ) {
705     let ty = drop_place.layout().ty;
706     let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx);
707
708     if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
709         // we don't actually need to drop anything
710     } else {
711         let drop_fn_ty = drop_fn.ty(fx.tcx, ParamEnv::reveal_all());
712         let fn_sig = fx.tcx.normalize_erasing_late_bound_regions(
713             ParamEnv::reveal_all(),
714             &drop_fn_ty.fn_sig(fx.tcx),
715         );
716         assert_eq!(fn_sig.output(), fx.tcx.mk_unit());
717
718         match ty.kind() {
719             ty::Dynamic(..) => {
720                 let (ptr, vtable) = drop_place.to_ptr_maybe_unsized();
721                 let ptr = ptr.get_addr(fx);
722                 let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
723
724                 let sig = clif_sig_from_fn_sig(
725                     fx.tcx,
726                     fx.triple(),
727                     fn_sig,
728                     span,
729                     true,
730                     false, // `drop_in_place` is never `#[track_caller]`
731                 );
732                 let sig = fx.bcx.import_signature(sig);
733                 fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]);
734             }
735             _ => {
736                 assert!(!matches!(drop_fn.def, InstanceDef::Virtual(_, _)));
737
738                 let arg_value = drop_place.place_ref(
739                     fx,
740                     fx.layout_of(fx.tcx.mk_ref(
741                         &ty::RegionKind::ReErased,
742                         TypeAndMut {
743                             ty,
744                             mutbl: crate::rustc_hir::Mutability::Mut,
745                         },
746                     )),
747                 );
748                 let arg_value = adjust_arg_for_abi(fx, arg_value);
749
750                 let mut call_args: Vec<Value> = arg_value.into_iter().collect::<Vec<_>>();
751
752                 if drop_fn.def.requires_caller_location(fx.tcx) {
753                     // Pass the caller location for `#[track_caller]`.
754                     let caller_location = fx.get_caller_location(span);
755                     call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter());
756                 }
757
758                 let func_ref = fx.get_function_ref(drop_fn);
759                 fx.bcx.ins().call(func_ref, &call_args);
760             }
761         }
762     }
763 }