]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/abi.rs
Rollup merge of #95040 - frank-king:fix/94981, r=Mark-Simulacrum
[rust.git] / compiler / rustc_codegen_gcc / src / abi.rs
1 use gccjit::{ToLValue, ToRValue, Type};
2 use rustc_codegen_ssa::traits::{AbiBuilderMethods, BaseTypeMethods};
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_middle::bug;
5 use rustc_middle::ty::Ty;
6 use rustc_target::abi::call::{CastTarget, FnAbi, PassMode, Reg, RegKind};
7
8 use crate::builder::Builder;
9 use crate::context::CodegenCx;
10 use crate::intrinsic::ArgAbiExt;
11 use crate::type_of::LayoutGccExt;
12
13 impl<'a, 'gcc, 'tcx> AbiBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
14     fn apply_attrs_callsite(&mut self, _fn_abi: &FnAbi<'tcx, Ty<'tcx>>, _callsite: Self::Value) {
15         // TODO(antoyo)
16     }
17
18     fn get_param(&mut self, index: usize) -> Self::Value {
19         let func = self.current_func();
20         let param = func.get_param(index as i32);
21         let on_stack =
22             if let Some(on_stack_param_indices) = self.on_stack_function_params.borrow().get(&func) {
23                 on_stack_param_indices.contains(&index)
24             }
25             else {
26                 false
27             };
28         if on_stack {
29             param.to_lvalue().get_address(None)
30         }
31         else {
32             param.to_rvalue()
33         }
34     }
35 }
36
37 impl GccType for CastTarget {
38     fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
39         let rest_gcc_unit = self.rest.unit.gcc_type(cx);
40         let (rest_count, rem_bytes) =
41             if self.rest.unit.size.bytes() == 0 {
42                 (0, 0)
43             }
44             else {
45                 (self.rest.total.bytes() / self.rest.unit.size.bytes(), self.rest.total.bytes() % self.rest.unit.size.bytes())
46             };
47
48         if self.prefix.iter().all(|x| x.is_none()) {
49             // Simplify to a single unit when there is no prefix and size <= unit size
50             if self.rest.total <= self.rest.unit.size {
51                 return rest_gcc_unit;
52             }
53
54             // Simplify to array when all chunks are the same size and type
55             if rem_bytes == 0 {
56                 return cx.type_array(rest_gcc_unit, rest_count);
57             }
58         }
59
60         // Create list of fields in the main structure
61         let mut args: Vec<_> = self
62             .prefix
63             .iter()
64             .flat_map(|option_reg| {
65                 option_reg.map(|reg| reg.gcc_type(cx))
66             })
67             .chain((0..rest_count).map(|_| rest_gcc_unit))
68             .collect();
69
70         // Append final integer
71         if rem_bytes != 0 {
72             // Only integers can be really split further.
73             assert_eq!(self.rest.unit.kind, RegKind::Integer);
74             args.push(cx.type_ix(rem_bytes * 8));
75         }
76
77         cx.type_struct(&args, false)
78     }
79 }
80
81 pub trait GccType {
82     fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc>;
83 }
84
85 impl GccType for Reg {
86     fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
87         match self.kind {
88             RegKind::Integer => cx.type_ix(self.size.bits()),
89             RegKind::Float => {
90                 match self.size.bits() {
91                     32 => cx.type_f32(),
92                     64 => cx.type_f64(),
93                     _ => bug!("unsupported float: {:?}", self),
94                 }
95             },
96             RegKind::Vector => unimplemented!(), //cx.type_vector(cx.type_i8(), self.size.bytes()),
97         }
98     }
99 }
100
101 pub trait FnAbiGccExt<'gcc, 'tcx> {
102     // TODO(antoyo): return a function pointer type instead?
103     fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool, FxHashSet<usize>);
104     fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
105 }
106
107 impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
108     fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool, FxHashSet<usize>) {
109         let mut on_stack_param_indices = FxHashSet::default();
110         let args_capacity: usize = self.args.iter().map(|arg|
111             if arg.pad.is_some() {
112                 1
113             }
114             else {
115                 0
116             } +
117             if let PassMode::Pair(_, _) = arg.mode {
118                 2
119             } else {
120                 1
121             }
122         ).sum();
123         let mut argument_tys = Vec::with_capacity(
124             if let PassMode::Indirect { .. } = self.ret.mode {
125                 1
126             }
127             else {
128                 0
129             } + args_capacity,
130         );
131
132         let return_ty =
133             match self.ret.mode {
134                 PassMode::Ignore => cx.type_void(),
135                 PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
136                 PassMode::Cast(cast) => cast.gcc_type(cx),
137                 PassMode::Indirect { .. } => {
138                     argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
139                     cx.type_void()
140                 }
141             };
142
143         for arg in &self.args {
144             // add padding
145             if let Some(ty) = arg.pad {
146                 argument_tys.push(ty.gcc_type(cx));
147             }
148
149             let arg_ty = match arg.mode {
150                 PassMode::Ignore => continue,
151                 PassMode::Direct(_) => arg.layout.immediate_gcc_type(cx),
152                 PassMode::Pair(..) => {
153                     argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 0, true));
154                     argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 1, true));
155                     continue;
156                 }
157                 PassMode::Indirect { extra_attrs: Some(_), .. } => {
158                     unimplemented!();
159                 }
160                 PassMode::Cast(cast) => cast.gcc_type(cx),
161                 PassMode::Indirect { extra_attrs: None, on_stack: true, .. } => {
162                     on_stack_param_indices.insert(argument_tys.len());
163                     arg.memory_ty(cx)
164                 },
165                 PassMode::Indirect { extra_attrs: None, on_stack: false, .. } => cx.type_ptr_to(arg.memory_ty(cx)),
166             };
167             argument_tys.push(arg_ty);
168         }
169
170         (return_ty, argument_tys, self.c_variadic, on_stack_param_indices)
171     }
172
173     fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
174         let (return_type, params, variadic, on_stack_param_indices) = self.gcc_type(cx);
175         let pointer_type = cx.context.new_function_pointer_type(None, return_type, &params, variadic);
176         cx.on_stack_params.borrow_mut().insert(pointer_type.dyncast_function_ptr_type().expect("function ptr type"), on_stack_param_indices);
177         pointer_type
178     }
179 }