]> git.lizzy.rs Git - rust.git/blob - src/abi.rs
Merge pull request #242 from bjorn3/dependabot/cargo/backtrace-sys-0.1.28
[rust.git] / src / abi.rs
1 use std::borrow::Cow;
2 use std::iter;
3
4 use rustc::hir;
5 use rustc_target::spec::abi::Abi;
6
7 use crate::prelude::*;
8
9 #[derive(Copy, Clone, Debug)]
10 enum PassMode {
11     NoPass,
12     ByVal(Type),
13     ByRef,
14 }
15
16 impl PassMode {
17     fn get_param_ty(self, fx: &FunctionCx<impl Backend>) -> Type {
18         match self {
19             PassMode::NoPass => unimplemented!("pass mode nopass"),
20             PassMode::ByVal(clif_type) => clif_type,
21             PassMode::ByRef => fx.pointer_type,
22         }
23     }
24 }
25
26 fn get_pass_mode<'a, 'tcx: 'a>(
27     tcx: TyCtxt<'a, 'tcx, 'tcx>,
28     abi: Abi,
29     ty: Ty<'tcx>,
30     is_return: bool,
31 ) -> PassMode {
32     assert!(!tcx
33         .layout_of(ParamEnv::reveal_all().and(ty))
34         .unwrap()
35         .is_unsized());
36     if let ty::Never = ty.sty {
37         if is_return {
38             PassMode::NoPass
39         } else {
40             PassMode::ByRef
41         }
42     } else if ty.sty == tcx.mk_unit().sty {
43         if is_return {
44             PassMode::NoPass
45         } else {
46             PassMode::ByRef
47         }
48     } else if let Some(ret_ty) = crate::common::clif_type_from_ty(tcx, ty) {
49         PassMode::ByVal(ret_ty)
50     } else {
51         if abi == Abi::C {
52             unimpl!(
53                 "Non scalars are not yet supported for \"C\" abi ({:?}) is_return: {:?}",
54                 ty,
55                 is_return
56             );
57         }
58         PassMode::ByRef
59     }
60 }
61
62 fn adjust_arg_for_abi<'a, 'tcx: 'a>(
63     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
64     sig: FnSig<'tcx>,
65     arg: CValue<'tcx>,
66 ) -> Value {
67     match get_pass_mode(fx.tcx, sig.abi, arg.layout().ty, false) {
68         PassMode::NoPass => unimplemented!("pass mode nopass"),
69         PassMode::ByVal(_) => arg.load_value(fx),
70         PassMode::ByRef => arg.force_stack(fx),
71     }
72 }
73
74 fn clif_sig_from_fn_ty<'a, 'tcx: 'a>(
75     tcx: TyCtxt<'a, 'tcx, 'tcx>,
76     fn_ty: Ty<'tcx>,
77 ) -> Signature {
78     let sig = ty_fn_sig(tcx, fn_ty);
79     if sig.variadic {
80         unimpl!("Variadic function are not yet supported");
81     }
82     let (call_conv, inputs, output): (CallConv, Vec<Ty>, Ty) = match sig.abi {
83         Abi::Rust => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
84         Abi::C => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
85         Abi::RustCall => {
86             assert_eq!(sig.inputs().len(), 2);
87             let extra_args = match sig.inputs().last().unwrap().sty {
88                 ty::Tuple(ref tupled_arguments) => tupled_arguments,
89                 _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
90             };
91             let mut inputs: Vec<Ty> = vec![sig.inputs()[0]];
92             inputs.extend(extra_args.into_iter());
93             (CallConv::SystemV, inputs, sig.output())
94         }
95         Abi::System => bug!("system abi should be selected elsewhere"),
96         Abi::RustIntrinsic => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()),
97         _ => unimplemented!("unsupported abi {:?}", sig.abi),
98     };
99
100     let inputs = inputs
101         .into_iter()
102         .filter_map(|ty| match get_pass_mode(tcx, sig.abi, ty, false) {
103             PassMode::ByVal(clif_ty) => Some(clif_ty),
104             PassMode::NoPass => unimplemented!("pass mode nopass"),
105             PassMode::ByRef => Some(pointer_ty(tcx)),
106         });
107
108     let (params, returns) = match get_pass_mode(tcx, sig.abi, output, true) {
109         PassMode::NoPass => (inputs.map(AbiParam::new).collect(), vec![]),
110         PassMode::ByVal(ret_ty) => (
111             inputs.map(AbiParam::new).collect(),
112             vec![AbiParam::new(ret_ty)],
113         ),
114         PassMode::ByRef => {
115             (
116                 Some(pointer_ty(tcx)) // First param is place to put return val
117                     .into_iter()
118                     .chain(inputs)
119                     .map(AbiParam::new)
120                     .collect(),
121                 vec![],
122             )
123         }
124     };
125
126     Signature {
127         params,
128         returns,
129         call_conv,
130     }
131 }
132
133 pub fn ty_fn_sig<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> ty::FnSig<'tcx> {
134     let sig = match ty.sty {
135         ty::FnDef(..) |
136         // Shims currently have type TyFnPtr. Not sure this should remain.
137         ty::FnPtr(_) => ty.fn_sig(tcx),
138         ty::Closure(def_id, substs) => {
139             let sig = substs.closure_sig(def_id, tcx);
140
141             let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
142             sig.map_bound(|sig| tcx.mk_fn_sig(
143                 iter::once(*env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
144                 sig.output(),
145                 sig.variadic,
146                 sig.unsafety,
147                 sig.abi
148             ))
149         }
150         ty::Generator(def_id, substs, _) => {
151             let sig = substs.poly_sig(def_id, tcx);
152
153             let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
154             let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty);
155
156             sig.map_bound(|sig| {
157                 let state_did = tcx.lang_items().gen_state().unwrap();
158                 let state_adt_ref = tcx.adt_def(state_did);
159                 let state_substs = tcx.intern_substs(&[
160                     sig.yield_ty.into(),
161                     sig.return_ty.into(),
162                 ]);
163                 let ret_ty = tcx.mk_adt(state_adt_ref, state_substs);
164
165                 tcx.mk_fn_sig(iter::once(env_ty),
166                     ret_ty,
167                     false,
168                     hir::Unsafety::Normal,
169                     Abi::Rust
170                 )
171             })
172         }
173         _ => bug!("unexpected type {:?} to ty_fn_sig", ty)
174     };
175     tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), &sig)
176 }
177
178 pub fn get_function_name_and_sig<'a, 'tcx>(
179     tcx: TyCtxt<'a, 'tcx, 'tcx>,
180     inst: Instance<'tcx>,
181 ) -> (String, Signature) {
182     assert!(!inst.substs.needs_infer() && !inst.substs.has_param_types());
183     let fn_ty = inst.ty(tcx);
184     let sig = clif_sig_from_fn_ty(tcx, fn_ty);
185     (tcx.symbol_name(inst).as_str().to_string(), sig)
186 }
187
188 impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
189     /// Instance must be monomorphized
190     pub fn get_function_id(&mut self, inst: Instance<'tcx>) -> FuncId {
191         let (name, sig) = get_function_name_and_sig(self.tcx, inst);
192         self.module
193             .declare_function(&name, Linkage::Import, &sig)
194             .unwrap()
195     }
196
197     /// Instance must be monomorphized
198     pub fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
199         let func_id = self.get_function_id(inst);
200         self.module
201             .declare_func_in_func(func_id, &mut self.bcx.func)
202     }
203
204     fn lib_call(
205         &mut self,
206         name: &str,
207         input_tys: Vec<types::Type>,
208         output_ty: Option<types::Type>,
209         args: &[Value],
210     ) -> Option<Value> {
211         let sig = Signature {
212             params: input_tys.iter().cloned().map(AbiParam::new).collect(),
213             returns: output_ty
214                 .map(|output_ty| vec![AbiParam::new(output_ty)])
215                 .unwrap_or(Vec::new()),
216             call_conv: CallConv::SystemV,
217         };
218         let func_id = self
219             .module
220             .declare_function(&name, Linkage::Import, &sig)
221             .unwrap();
222         let func_ref = self
223             .module
224             .declare_func_in_func(func_id, &mut self.bcx.func);
225         let call_inst = self.bcx.ins().call(func_ref, args);
226         if output_ty.is_none() {
227             return None;
228         }
229         let results = self.bcx.inst_results(call_inst);
230         assert_eq!(results.len(), 1);
231         Some(results[0])
232     }
233
234     pub fn easy_call(
235         &mut self,
236         name: &str,
237         args: &[CValue<'tcx>],
238         return_ty: Ty<'tcx>,
239     ) -> CValue<'tcx> {
240         let (input_tys, args): (Vec<_>, Vec<_>) = args
241             .into_iter()
242             .map(|arg| {
243                 (
244                     self.clif_type(arg.layout().ty).unwrap(),
245                     arg.load_value(self),
246                 )
247             })
248             .unzip();
249         let return_layout = self.layout_of(return_ty);
250         let return_ty = if let ty::Tuple(tup) = return_ty.sty {
251             if !tup.is_empty() {
252                 bug!("easy_call( (...) -> <non empty tuple> ) is not allowed");
253             }
254             None
255         } else {
256             Some(self.clif_type(return_ty).unwrap())
257         };
258         if let Some(val) = self.lib_call(name, input_tys, return_ty, &args) {
259             CValue::ByVal(val, return_layout)
260         } else {
261             CValue::ByRef(self.bcx.ins().iconst(self.pointer_type, 0), return_layout)
262         }
263     }
264
265     fn self_sig(&self) -> FnSig<'tcx> {
266         ty_fn_sig(self.tcx, self.instance.ty(self.tcx))
267     }
268
269     fn return_type(&self) -> Ty<'tcx> {
270         self.self_sig().output()
271     }
272 }
273
274 fn add_local_comment<'a, 'tcx: 'a>(
275     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
276     msg: &str,
277     local: mir::Local,
278     local_field: Option<usize>,
279     param: Option<Value>,
280     pass_mode: Option<PassMode>,
281     ssa: crate::analyze::Flags,
282     ty: Ty<'tcx>,
283 ) {
284     let local_field = if let Some(local_field) = local_field {
285         Cow::Owned(format!(".{}", local_field))
286     } else {
287         Cow::Borrowed("")
288     };
289     let param = if let Some(param) = param {
290         Cow::Owned(format!("= {:?}", param))
291     } else {
292         Cow::Borrowed("-")
293     };
294     let pass_mode = if let Some(pass_mode) = pass_mode {
295         Cow::Owned(format!("{:?}", pass_mode))
296     } else {
297         Cow::Borrowed("-")
298     };
299     fx.add_global_comment(format!(
300         "{msg:5} {local:>3}{local_field:<5} {param:10} {pass_mode:20} {ssa:10} {ty:?}",
301         msg=msg, local=format!("{:?}", local), local_field=local_field, param=param, pass_mode=pass_mode, ssa=format!("{:?}", ssa), ty=ty,
302     ));
303 }
304
305 fn add_local_header_comment(fx: &mut FunctionCx<impl Backend>) {
306     fx.add_global_comment(format!("msg   loc.idx    param    pass mode            ssa flags  ty"));
307 }
308
309 fn arg_place<'a, 'tcx: 'a>(
310     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
311     local: Local,
312     layout: TyLayout<'tcx>,
313     is_ssa: bool,
314 ) -> CPlace<'tcx> {
315     let place = if is_ssa {
316         fx.bcx.declare_var(mir_var(local), fx.clif_type(layout.ty).unwrap());
317         CPlace::Var(local, layout)
318     } else {
319         let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
320             kind: StackSlotKind::ExplicitSlot,
321             size: layout.size.bytes() as u32,
322             offset: None,
323         });
324
325         CPlace::from_stack_slot(fx, stack_slot, layout.ty)
326     };
327
328     debug_assert!(fx.local_map.insert(local, place).is_none());
329     fx.local_map[&local]
330 }
331
332 fn param_to_cvalue<'a, 'tcx: 'a>(fx: &FunctionCx<'a, 'tcx, impl Backend>, ebb_param: Value, layout: TyLayout<'tcx>) -> CValue<'tcx> {
333     match get_pass_mode(fx.tcx, fx.self_sig().abi, layout.ty, false) {
334         PassMode::NoPass => unimplemented!("pass mode nopass"),
335         PassMode::ByVal(_) => CValue::ByVal(ebb_param, layout),
336         PassMode::ByRef => CValue::ByRef(ebb_param, layout),
337     }
338 }
339
340 pub fn codegen_fn_prelude<'a, 'tcx: 'a>(
341     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
342     start_ebb: Ebb,
343 ) {
344     let ssa_analyzed = crate::analyze::analyze(fx);
345
346     let ret_layout = fx.layout_of(fx.return_type());
347     let output_pass_mode = get_pass_mode(fx.tcx, fx.self_sig().abi, fx.return_type(), true);
348     let ret_param = match output_pass_mode {
349         PassMode::NoPass => None,
350         PassMode::ByVal(_) => None,
351         PassMode::ByRef => Some(fx.bcx.append_ebb_param(start_ebb, fx.pointer_type)),
352     };
353
354     enum ArgKind {
355         Normal(Value),
356         Spread(Vec<Value>),
357     }
358
359     let func_params = fx
360         .mir
361         .args_iter()
362         .map(|local| {
363             let arg_ty = fx.monomorphize(&fx.mir.local_decls[local].ty);
364
365             // Adapted from https://github.com/rust-lang/rust/blob/145155dc96757002c7b2e9de8489416e2fdbbd57/src/librustc_codegen_llvm/mir/mod.rs#L442-L482
366             if Some(local) == fx.mir.spread_arg {
367                 // This argument (e.g. the last argument in the "rust-call" ABI)
368                 // is a tuple that was spread at the ABI level and now we have
369                 // to reconstruct it into a tuple local variable, from multiple
370                 // individual function arguments.
371
372                 let tupled_arg_tys = match arg_ty.sty {
373                     ty::Tuple(ref tys) => tys,
374                     _ => bug!("spread argument isn't a tuple?! but {:?}", arg_ty),
375                 };
376
377                 let mut ebb_params = Vec::new();
378                 for (i, arg_ty) in tupled_arg_tys.iter().enumerate() {
379                     let pass_mode = get_pass_mode(fx.tcx, fx.self_sig().abi, arg_ty, false);;
380                     let clif_type = pass_mode.get_param_ty(fx);
381                     let ebb_param = fx.bcx.append_ebb_param(start_ebb, clif_type);
382                     add_local_comment(fx, "arg", local, Some(i), Some(ebb_param), Some(pass_mode), ssa_analyzed[&local], arg_ty);
383                     ebb_params.push(ebb_param);
384                 }
385
386                 (local, ArgKind::Spread(ebb_params), arg_ty)
387             } else {
388                 let clif_type =
389                     get_pass_mode(fx.tcx, fx.self_sig().abi, arg_ty, false).get_param_ty(fx);
390                 let ebb_param = fx.bcx.append_ebb_param(start_ebb, clif_type);
391                 let pass_mode = get_pass_mode(fx.tcx, fx.self_sig().abi, arg_ty, false);
392                 add_local_comment(fx, "arg", local, None, Some(ebb_param), Some(pass_mode), ssa_analyzed[&local], arg_ty);
393                 (
394                     local,
395                     ArgKind::Normal(ebb_param),
396                     arg_ty,
397                 )
398             }
399         })
400         .collect::<Vec<(Local, ArgKind, Ty)>>();
401
402     fx.bcx.switch_to_block(start_ebb);
403
404     fx.add_global_comment(format!("ssa {:?}", ssa_analyzed));
405
406     match output_pass_mode {
407         PassMode::NoPass => {
408             let null = fx.bcx.ins().iconst(fx.pointer_type, 0);
409             fx.local_map.insert(
410                 RETURN_PLACE,
411                 CPlace::Addr(null, None, fx.layout_of(fx.return_type())),
412             );
413         }
414         PassMode::ByVal(ret_ty) => {
415             fx.bcx.declare_var(mir_var(RETURN_PLACE), ret_ty);
416             fx.local_map
417                 .insert(RETURN_PLACE, CPlace::Var(RETURN_PLACE, ret_layout));
418         }
419         PassMode::ByRef => {
420             fx.local_map.insert(
421                 RETURN_PLACE,
422                 CPlace::Addr(ret_param.unwrap(), None, ret_layout),
423             );
424         }
425     }
426
427     add_local_header_comment(fx);
428     add_local_comment(fx, "ret", RETURN_PLACE, None, ret_param, Some(output_pass_mode), ssa_analyzed[&RETURN_PLACE], ret_layout.ty);
429
430     for (local, arg_kind, ty) in func_params {
431         let layout = fx.layout_of(ty);
432
433         let is_ssa = !ssa_analyzed
434             .get(&local)
435             .unwrap()
436             .contains(crate::analyze::Flags::NOT_SSA);
437
438         match arg_kind {
439             ArgKind::Normal(ebb_param) => {
440                 let cvalue = param_to_cvalue(fx, ebb_param, layout);
441                 arg_place(fx, local, layout, is_ssa).write_cvalue(fx, cvalue);
442             }
443             ArgKind::Spread(ebb_params) => {
444                 let place = arg_place(fx, local, layout, is_ssa);
445
446                 for (i, ebb_param) in ebb_params.into_iter().enumerate() {
447                     let sub_place = place.place_field(fx, mir::Field::new(i));
448                     let cvalue = param_to_cvalue(fx, ebb_param, sub_place.layout());
449                     sub_place.write_cvalue(fx, cvalue);
450                 }
451             }
452         }
453     }
454
455     for local in fx.mir.vars_and_temps_iter() {
456         let ty = fx.mir.local_decls[local].ty;
457         let layout = fx.layout_of(ty);
458
459         add_local_comment(fx, "local", local, None, None, None, ssa_analyzed[&local], ty);
460
461         let place = if ssa_analyzed
462             .get(&local)
463             .unwrap()
464             .contains(crate::analyze::Flags::NOT_SSA)
465         {
466             let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
467                 kind: StackSlotKind::ExplicitSlot,
468                 size: layout.size.bytes() as u32,
469                 offset: None,
470             });
471             CPlace::from_stack_slot(fx, stack_slot, ty)
472         } else {
473             fx.bcx
474                 .declare_var(mir_var(local), fx.clif_type(ty).unwrap());
475             CPlace::Var(local, layout)
476         };
477
478         fx.local_map.insert(local, place);
479     }
480
481     fx.bcx
482         .ins()
483         .jump(*fx.ebb_map.get(&START_BLOCK).unwrap(), &[]);
484 }
485
486 pub fn codegen_terminator_call<'a, 'tcx: 'a>(
487     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
488     func: &Operand<'tcx>,
489     args: &[Operand<'tcx>],
490     destination: &Option<(Place<'tcx>, BasicBlock)>,
491 ) {
492     let fn_ty = fx.monomorphize(&func.ty(fx.mir, fx.tcx));
493     let sig = ty_fn_sig(fx.tcx, fn_ty);
494
495     // Unpack arguments tuple for closures
496     let args = if sig.abi == Abi::RustCall {
497         assert_eq!(args.len(), 2, "rust-call abi requires two arguments");
498         let self_arg = trans_operand(fx, &args[0]);
499         let pack_arg = trans_operand(fx, &args[1]);
500         let mut args = Vec::new();
501         args.push(self_arg);
502         match pack_arg.layout().ty.sty {
503             ty::Tuple(ref tupled_arguments) => {
504                 for (i, _) in tupled_arguments.iter().enumerate() {
505                     args.push(pack_arg.value_field(fx, mir::Field::new(i)));
506                 }
507             }
508             _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"),
509         }
510         args
511     } else {
512         args.into_iter()
513             .map(|arg| trans_operand(fx, arg))
514             .collect::<Vec<_>>()
515     };
516
517     let destination = destination
518         .as_ref()
519         .map(|&(ref place, bb)| (trans_place(fx, place), bb));
520
521     if let ty::FnDef(def_id, substs) = fn_ty.sty {
522         let instance = ty::Instance::resolve(
523             fx.tcx,
524             ty::ParamEnv::reveal_all(),
525             def_id,
526             substs,
527         ).unwrap();
528
529         match instance.def {
530             InstanceDef::Intrinsic(_) => {
531                 crate::intrinsics::codegen_intrinsic_call(fx, def_id, substs, args, destination);
532                 return;
533             }
534             InstanceDef::DropGlue(_, None) => {
535                 // empty drop glue - a nop.
536                 let (_, dest) = destination.expect("Non terminating drop_in_place_real???");
537                 let ret_ebb = fx.get_ebb(dest);
538                 fx.bcx.ins().jump(ret_ebb, &[]);
539                 return;
540             }
541             _ => {}
542         }
543     }
544
545     codegen_call_inner(
546         fx,
547         Some(func),
548         fn_ty,
549         args,
550         destination.map(|(place, _)| place),
551     );
552
553     if let Some((_, dest)) = destination {
554         let ret_ebb = fx.get_ebb(dest);
555         fx.bcx.ins().jump(ret_ebb, &[]);
556     } else {
557         trap_unreachable(&mut fx.bcx);
558     }
559 }
560
561 pub fn codegen_call_inner<'a, 'tcx: 'a>(
562     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
563     func: Option<&Operand<'tcx>>,
564     fn_ty: Ty<'tcx>,
565     args: Vec<CValue<'tcx>>,
566     ret_place: Option<CPlace<'tcx>>,
567 ) {
568     let sig = ty_fn_sig(fx.tcx, fn_ty);
569
570     let ret_layout = fx.layout_of(sig.output());
571
572     let output_pass_mode = get_pass_mode(fx.tcx, sig.abi, sig.output(), true);
573     let return_ptr = match output_pass_mode {
574         PassMode::NoPass => None,
575         PassMode::ByRef => match ret_place {
576             Some(ret_place) => Some(ret_place.expect_addr()),
577             None => Some(fx.bcx.ins().iconst(fx.pointer_type, 0)),
578         },
579         PassMode::ByVal(_) => None,
580     };
581
582     let instance = match fn_ty.sty {
583         ty::FnDef(def_id, substs) => {
584             Some(Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs).unwrap())
585         }
586         _ => None,
587     };
588
589     let func_ref: Option<Value>; // Indirect call target
590
591     let first_arg = {
592         if let Some(Instance {
593             def: InstanceDef::Virtual(_, idx),
594             ..
595         }) = instance
596         {
597             let (ptr, method) = crate::vtable::get_ptr_and_method_ref(fx, args[0], idx);
598             func_ref = Some(method);
599             Some(ptr)
600         } else {
601             func_ref = if instance.is_none() {
602                 let func = trans_operand(fx, func.expect("indirect call without func Operand"));
603                 Some(func.load_value(fx))
604             } else {
605                 None
606             };
607
608             args.get(0).map(|arg| adjust_arg_for_abi(fx, sig, *arg))
609         }
610         .into_iter()
611     };
612
613     let call_args: Vec<Value> = return_ptr
614         .into_iter()
615         .chain(first_arg)
616         .chain(
617             args.into_iter()
618                 .skip(1)
619                 .map(|arg| adjust_arg_for_abi(fx, sig, arg)),
620         )
621         .collect::<Vec<_>>();
622
623     let sig = fx.bcx.import_signature(clif_sig_from_fn_ty(fx.tcx, fn_ty));
624     let call_inst = if let Some(func_ref) = func_ref {
625         fx.bcx.ins().call_indirect(sig, func_ref, &call_args)
626     } else {
627         let func_ref = fx.get_function_ref(instance.expect("non-indirect call on non-FnDef type"));
628         fx.bcx.ins().call(func_ref, &call_args)
629     };
630
631     match output_pass_mode {
632         PassMode::NoPass => {}
633         PassMode::ByVal(_) => {
634             if let Some(ret_place) = ret_place {
635                 let results = fx.bcx.inst_results(call_inst);
636                 ret_place.write_cvalue(fx, CValue::ByVal(results[0], ret_layout));
637             }
638         }
639         PassMode::ByRef => {}
640     }
641 }
642
643 pub fn codegen_return(fx: &mut FunctionCx<impl Backend>) {
644     match get_pass_mode(fx.tcx, fx.self_sig().abi, fx.return_type(), true) {
645         PassMode::NoPass | PassMode::ByRef => {
646             fx.bcx.ins().return_(&[]);
647         }
648         PassMode::ByVal(_) => {
649             let place = fx.get_local_place(RETURN_PLACE);
650             let ret_val = place.to_cvalue(fx).load_value(fx);
651             fx.bcx.ins().return_(&[ret_val]);
652         }
653     }
654 }