1 use crate::abi::pass_mode::*;
4 fn return_layout<'a, 'tcx>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> TyLayout<'tcx> {
5 fx.layout_of(fx.monomorphize(&fx.mir.local_decls[RETURN_PLACE].ty))
8 pub fn can_return_to_ssa_var<'tcx>(tcx: TyCtxt<'tcx>, dest_layout: TyLayout<'tcx>) -> bool {
9 match get_pass_mode(tcx, dest_layout) {
10 PassMode::NoPass | PassMode::ByVal(_) => true,
11 // FIXME Make it possible to return ByValPair and ByRef to an ssa var.
12 PassMode::ByValPair(_, _) | PassMode::ByRef => false
16 pub fn codegen_return_param(
17 fx: &mut FunctionCx<impl Backend>,
18 ssa_analyzed: &rustc_index::vec::IndexVec<Local, crate::analyze::SsaKind>,
21 let ret_layout = return_layout(fx);
22 let ret_pass_mode = get_pass_mode(fx.tcx, ret_layout);
23 let ret_param = match ret_pass_mode {
26 .insert(RETURN_PLACE, CPlace::no_place(ret_layout));
29 PassMode::ByVal(_) | PassMode::ByValPair(_, _) => {
30 let is_ssa = ssa_analyzed[RETURN_PLACE] == crate::analyze::SsaKind::Ssa;
32 super::local_place(fx, RETURN_PLACE, ret_layout, is_ssa);
37 let ret_param = fx.bcx.append_ebb_param(start_ebb, fx.pointer_type);
39 .insert(RETURN_PLACE, CPlace::for_ptr(Pointer::new(ret_param), ret_layout));
45 #[cfg(debug_assertions)]
46 crate::abi::comments::add_arg_comment(
57 pub fn codegen_with_call_return_arg<'tcx, B: Backend, T>(
58 fx: &mut FunctionCx<'_, 'tcx, B>,
60 ret_place: Option<CPlace<'tcx>>,
61 f: impl FnOnce(&mut FunctionCx<'_, 'tcx, B>, Option<Value>) -> (Inst, T),
63 let ret_layout = fx.layout_of(fn_sig.output());
65 let output_pass_mode = get_pass_mode(fx.tcx, ret_layout);
66 let return_ptr = match output_pass_mode {
67 PassMode::NoPass => None,
68 PassMode::ByRef => match ret_place {
69 Some(ret_place) => Some(ret_place.to_ptr(fx).get_addr(fx)),
70 None => Some(fx.bcx.ins().iconst(fx.pointer_type, 43)),
72 PassMode::ByVal(_) | PassMode::ByValPair(_, _) => None,
75 let (call_inst, meta) = f(fx, return_ptr);
77 match output_pass_mode {
78 PassMode::NoPass => {}
79 PassMode::ByVal(_) => {
80 if let Some(ret_place) = ret_place {
81 let ret_val = fx.bcx.inst_results(call_inst)[0];
82 ret_place.write_cvalue(fx, CValue::by_val(ret_val, ret_layout));
85 PassMode::ByValPair(_, _) => {
86 if let Some(ret_place) = ret_place {
87 let ret_val_a = fx.bcx.inst_results(call_inst)[0];
88 let ret_val_b = fx.bcx.inst_results(call_inst)[1];
89 ret_place.write_cvalue(fx, CValue::by_val_pair(ret_val_a, ret_val_b, ret_layout));
98 pub fn codegen_return(fx: &mut FunctionCx<impl Backend>) {
99 match get_pass_mode(fx.tcx, return_layout(fx)) {
100 PassMode::NoPass | PassMode::ByRef => {
101 fx.bcx.ins().return_(&[]);
103 PassMode::ByVal(_) => {
104 let place = fx.get_local_place(RETURN_PLACE);
105 let ret_val = place.to_cvalue(fx).load_scalar(fx);
106 fx.bcx.ins().return_(&[ret_val]);
108 PassMode::ByValPair(_, _) => {
109 let place = fx.get_local_place(RETURN_PLACE);
110 let (ret_val_a, ret_val_b) = place.to_cvalue(fx).load_scalar_pair(fx);
111 fx.bcx.ins().return_(&[ret_val_a, ret_val_b]);