]> git.lizzy.rs Git - rust.git/blob - src/abi/returning.rs
Merge commit '9a0c32934ebe376128230aa8da3275697b2053e7' into sync_cg_clif-2021-03-05
[rust.git] / src / abi / returning.rs
1 //! Return value handling
2
3 use crate::prelude::*;
4
5 use rustc_middle::ty::layout::FnAbiExt;
6 use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode};
7 use smallvec::{smallvec, SmallVec};
8
9 /// Can the given type be returned into an ssa var or does it need to be returned on the stack.
10 pub(crate) fn can_return_to_ssa_var<'tcx>(
11     fx: &FunctionCx<'_, '_, 'tcx>,
12     func: &mir::Operand<'tcx>,
13     args: &[mir::Operand<'tcx>],
14 ) -> bool {
15     let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx));
16     let fn_sig =
17         fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx));
18
19     // Handle special calls like instrinsics and empty drop glue.
20     let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() {
21         let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
22             .unwrap()
23             .unwrap()
24             .polymorphize(fx.tcx);
25
26         match instance.def {
27             InstanceDef::Intrinsic(_) | InstanceDef::DropGlue(_, _) => {
28                 return true;
29             }
30             _ => Some(instance),
31         }
32     } else {
33         None
34     };
35
36     let extra_args = &args[fn_sig.inputs().len()..];
37     let extra_args = extra_args
38         .iter()
39         .map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx)))
40         .collect::<Vec<_>>();
41     let fn_abi = if let Some(instance) = instance {
42         FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), instance, &extra_args)
43     } else {
44         FnAbi::of_fn_ptr(&RevealAllLayoutCx(fx.tcx), fn_ty.fn_sig(fx.tcx), &extra_args)
45     };
46     match fn_abi.ret.mode {
47         PassMode::Ignore | PassMode::Direct(_) | PassMode::Pair(_, _) => true,
48         // FIXME Make it possible to return Cast and Indirect to an ssa var.
49         PassMode::Cast(_) | PassMode::Indirect { .. } => false,
50     }
51 }
52
53 /// Return a place where the return value of the current function can be written to. If necessary
54 /// this adds an extra parameter pointing to where the return value needs to be stored.
55 pub(super) fn codegen_return_param<'tcx>(
56     fx: &mut FunctionCx<'_, '_, 'tcx>,
57     ssa_analyzed: &rustc_index::vec::IndexVec<Local, crate::analyze::SsaKind>,
58     block_params_iter: &mut impl Iterator<Item = Value>,
59 ) -> CPlace<'tcx> {
60     let (ret_place, ret_param): (_, SmallVec<[_; 2]>) = match fx.fn_abi.as_ref().unwrap().ret.mode {
61         PassMode::Ignore => (CPlace::no_place(fx.fn_abi.as_ref().unwrap().ret.layout), smallvec![]),
62         PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(_) => {
63             let is_ssa = ssa_analyzed[RETURN_PLACE] == crate::analyze::SsaKind::Ssa;
64             (
65                 super::make_local_place(
66                     fx,
67                     RETURN_PLACE,
68                     fx.fn_abi.as_ref().unwrap().ret.layout,
69                     is_ssa,
70                 ),
71                 smallvec![],
72             )
73         }
74         PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
75             let ret_param = block_params_iter.next().unwrap();
76             assert_eq!(fx.bcx.func.dfg.value_type(ret_param), pointer_ty(fx.tcx));
77             (
78                 CPlace::for_ptr(Pointer::new(ret_param), fx.fn_abi.as_ref().unwrap().ret.layout),
79                 smallvec![ret_param],
80             )
81         }
82         PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
83             unreachable!("unsized return value")
84         }
85     };
86
87     #[cfg(not(debug_assertions))]
88     let _ = ret_param;
89
90     #[cfg(debug_assertions)]
91     crate::abi::comments::add_arg_comment(
92         fx,
93         "ret",
94         Some(RETURN_PLACE),
95         None,
96         &ret_param,
97         fx.fn_abi.as_ref().unwrap().ret.mode,
98         fx.fn_abi.as_ref().unwrap().ret.layout,
99     );
100
101     ret_place
102 }
103
104 /// Invokes the closure with if necessary a value representing the return pointer. When the closure
105 /// returns the call return value(s) if any are written to the correct place.
106 pub(super) fn codegen_with_call_return_arg<'tcx, T>(
107     fx: &mut FunctionCx<'_, '_, 'tcx>,
108     ret_arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
109     ret_place: Option<CPlace<'tcx>>,
110     f: impl FnOnce(&mut FunctionCx<'_, '_, 'tcx>, Option<Value>) -> (Inst, T),
111 ) -> (Inst, T) {
112     let return_ptr = match ret_arg_abi.mode {
113         PassMode::Ignore => None,
114         PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => match ret_place {
115             Some(ret_place) => Some(ret_place.to_ptr().get_addr(fx)),
116             None => Some(fx.bcx.ins().iconst(fx.pointer_type, 43)), // FIXME allocate temp stack slot
117         },
118         PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
119             unreachable!("unsized return value")
120         }
121         PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(_) => None,
122     };
123
124     let (call_inst, meta) = f(fx, return_ptr);
125
126     match ret_arg_abi.mode {
127         PassMode::Ignore => {}
128         PassMode::Direct(_) => {
129             if let Some(ret_place) = ret_place {
130                 let ret_val = fx.bcx.inst_results(call_inst)[0];
131                 ret_place.write_cvalue(fx, CValue::by_val(ret_val, ret_arg_abi.layout));
132             }
133         }
134         PassMode::Pair(_, _) => {
135             if let Some(ret_place) = ret_place {
136                 let ret_val_a = fx.bcx.inst_results(call_inst)[0];
137                 let ret_val_b = fx.bcx.inst_results(call_inst)[1];
138                 ret_place.write_cvalue(
139                     fx,
140                     CValue::by_val_pair(ret_val_a, ret_val_b, ret_arg_abi.layout),
141                 );
142             }
143         }
144         PassMode::Cast(cast) => {
145             if let Some(ret_place) = ret_place {
146                 let results = fx
147                     .bcx
148                     .inst_results(call_inst)
149                     .into_iter()
150                     .copied()
151                     .collect::<SmallVec<[Value; 2]>>();
152                 let result =
153                     super::pass_mode::from_casted_value(fx, &results, ret_place.layout(), cast);
154                 ret_place.write_cvalue(fx, result);
155             }
156         }
157         PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {}
158         PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
159             unreachable!("unsized return value")
160         }
161     }
162
163     (call_inst, meta)
164 }
165
166 /// Codegen a return instruction with the right return value(s) if any.
167 pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, '_>) {
168     match fx.fn_abi.as_ref().unwrap().ret.mode {
169         PassMode::Ignore | PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
170             fx.bcx.ins().return_(&[]);
171         }
172         PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
173             unreachable!("unsized return value")
174         }
175         PassMode::Direct(_) => {
176             let place = fx.get_local_place(RETURN_PLACE);
177             let ret_val = place.to_cvalue(fx).load_scalar(fx);
178             fx.bcx.ins().return_(&[ret_val]);
179         }
180         PassMode::Pair(_, _) => {
181             let place = fx.get_local_place(RETURN_PLACE);
182             let (ret_val_a, ret_val_b) = place.to_cvalue(fx).load_scalar_pair(fx);
183             fx.bcx.ins().return_(&[ret_val_a, ret_val_b]);
184         }
185         PassMode::Cast(cast) => {
186             let place = fx.get_local_place(RETURN_PLACE);
187             let ret_val = place.to_cvalue(fx);
188             let ret_vals = super::pass_mode::to_casted_value(fx, ret_val, cast);
189             fx.bcx.ins().return_(&ret_vals);
190         }
191     }
192 }