]> git.lizzy.rs Git - rust.git/blob - src/abi/returning.rs
Remove bitflags dependency
[rust.git] / src / abi / returning.rs
1 use crate::abi::pass_mode::*;
2 use crate::prelude::*;
3
4 pub fn codegen_return_param(
5     fx: &mut FunctionCx<impl Backend>,
6     ssa_analyzed: &HashMap<Local, crate::analyze::SsaKind>,
7     start_ebb: Ebb,
8 ) {
9     let ret_layout = fx.return_layout();
10     let output_pass_mode = get_pass_mode(fx.tcx, fx.return_layout());
11
12     let ret_param = match output_pass_mode {
13         PassMode::NoPass => {
14             fx.local_map
15                 .insert(RETURN_PLACE, CPlace::no_place(ret_layout));
16             Empty
17         }
18         PassMode::ByVal(_) | PassMode::ByValPair(_, _) => {
19             let is_ssa =
20                 *ssa_analyzed.get(&RETURN_PLACE).unwrap() == crate::analyze::SsaKind::Ssa;
21
22             super::local_place(fx, RETURN_PLACE, ret_layout, is_ssa);
23
24             Empty
25         }
26         PassMode::ByRef => {
27             let ret_param = fx.bcx.append_ebb_param(start_ebb, fx.pointer_type);
28             fx.local_map
29                 .insert(RETURN_PLACE, CPlace::for_addr(ret_param, ret_layout));
30
31             Single(ret_param)
32         }
33     };
34
35     #[cfg(debug_assertions)]
36     crate::abi::comments::add_arg_comment(
37         fx,
38         "ret",
39         RETURN_PLACE,
40         None,
41         ret_param,
42         output_pass_mode,
43         ret_layout.ty,
44     );
45 }
46
47 pub fn codegen_with_call_return_arg<'tcx, B: Backend, T>(
48     fx: &mut FunctionCx<'_, 'tcx, B>,
49     fn_sig: FnSig<'tcx>,
50     ret_place: Option<CPlace<'tcx>>,
51     f: impl FnOnce(&mut FunctionCx<'_, 'tcx, B>, Option<Value>) -> (Inst, T),
52 ) -> (Inst, T) {
53     let ret_layout = fx.layout_of(fn_sig.output());
54
55     let output_pass_mode = get_pass_mode(fx.tcx, ret_layout);
56     let return_ptr = match output_pass_mode {
57         PassMode::NoPass => None,
58         PassMode::ByRef => match ret_place {
59             Some(ret_place) => Some(ret_place.to_addr(fx)),
60             None => Some(fx.bcx.ins().iconst(fx.pointer_type, 43)),
61         },
62         PassMode::ByVal(_) | PassMode::ByValPair(_, _) => None,
63     };
64
65     let (call_inst, meta) = f(fx, return_ptr);
66
67     match output_pass_mode {
68         PassMode::NoPass => {}
69         PassMode::ByVal(_) => {
70             if let Some(ret_place) = ret_place {
71                 let ret_val = fx.bcx.inst_results(call_inst)[0];
72                 ret_place.write_cvalue(fx, CValue::by_val(ret_val, ret_layout));
73             }
74         }
75         PassMode::ByValPair(_, _) => {
76             if let Some(ret_place) = ret_place {
77                 let ret_val_a = fx.bcx.inst_results(call_inst)[0];
78                 let ret_val_b = fx.bcx.inst_results(call_inst)[1];
79                 ret_place.write_cvalue(fx, CValue::by_val_pair(ret_val_a, ret_val_b, ret_layout));
80             }
81         }
82         PassMode::ByRef => {}
83     }
84
85     (call_inst, meta)
86 }
87
88 pub fn codegen_return(fx: &mut FunctionCx<impl Backend>) {
89     match get_pass_mode(fx.tcx, fx.return_layout()) {
90         PassMode::NoPass | PassMode::ByRef => {
91             fx.bcx.ins().return_(&[]);
92         }
93         PassMode::ByVal(_) => {
94             let place = fx.get_local_place(RETURN_PLACE);
95             let ret_val = place.to_cvalue(fx).load_scalar(fx);
96             fx.bcx.ins().return_(&[ret_val]);
97         }
98         PassMode::ByValPair(_, _) => {
99             let place = fx.get_local_place(RETURN_PLACE);
100             let (ret_val_a, ret_val_b) = place.to_cvalue(fx).load_scalar_pair(fx);
101             fx.bcx.ins().return_(&[ret_val_a, ret_val_b]);
102         }
103     }
104 }