]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/abi.rs
Merge commit '0c89065b934397b62838fe3e4ef6f6352fc52daf' into libgccjit-codegen
[rust.git] / compiler / rustc_codegen_gcc / src / abi.rs
1 use gccjit::{ToRValue, Type};
2 use rustc_codegen_ssa::traits::{AbiBuilderMethods, BaseTypeMethods};
3 use rustc_middle::bug;
4 use rustc_middle::ty::Ty;
5 use rustc_target::abi::call::{CastTarget, FnAbi, PassMode, Reg, RegKind};
6
7 use crate::builder::Builder;
8 use crate::context::CodegenCx;
9 use crate::intrinsic::ArgAbiExt;
10 use crate::type_of::LayoutGccExt;
11
12 impl<'a, 'gcc, 'tcx> AbiBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
13     fn apply_attrs_callsite(&mut self, _fn_abi: &FnAbi<'tcx, Ty<'tcx>>, _callsite: Self::Value) {
14         // TODO
15         //fn_abi.apply_attrs_callsite(self, callsite)
16     }
17
18     fn get_param(&self, index: usize) -> Self::Value {
19         self.cx.current_func.borrow().expect("current func")
20             .get_param(index as i32)
21             .to_rvalue()
22     }
23 }
24
25 impl GccType for CastTarget {
26     fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
27         let rest_gcc_unit = self.rest.unit.gcc_type(cx);
28         let (rest_count, rem_bytes) =
29             if self.rest.unit.size.bytes() == 0 {
30                 (0, 0)
31             }
32             else {
33                 (self.rest.total.bytes() / self.rest.unit.size.bytes(), self.rest.total.bytes() % self.rest.unit.size.bytes())
34             };
35
36         if self.prefix.iter().all(|x| x.is_none()) {
37             // Simplify to a single unit when there is no prefix and size <= unit size
38             if self.rest.total <= self.rest.unit.size {
39                 return rest_gcc_unit;
40             }
41
42             // Simplify to array when all chunks are the same size and type
43             if rem_bytes == 0 {
44                 return cx.type_array(rest_gcc_unit, rest_count);
45             }
46         }
47
48         // Create list of fields in the main structure
49         let mut args: Vec<_> = self
50             .prefix
51             .iter()
52             .flat_map(|option_kind| {
53                 option_kind.map(|kind| Reg { kind, size: self.prefix_chunk_size }.gcc_type(cx))
54             })
55             .chain((0..rest_count).map(|_| rest_gcc_unit))
56             .collect();
57
58         // Append final integer
59         if rem_bytes != 0 {
60             // Only integers can be really split further.
61             assert_eq!(self.rest.unit.kind, RegKind::Integer);
62             args.push(cx.type_ix(rem_bytes * 8));
63         }
64
65         cx.type_struct(&args, false)
66     }
67 }
68
69 pub trait GccType {
70     fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc>;
71 }
72
73 impl GccType for Reg {
74     fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
75         match self.kind {
76             RegKind::Integer => cx.type_ix(self.size.bits()),
77             RegKind::Float => {
78                 match self.size.bits() {
79                     32 => cx.type_f32(),
80                     64 => cx.type_f64(),
81                     _ => bug!("unsupported float: {:?}", self),
82                 }
83             },
84             RegKind::Vector => unimplemented!(), //cx.type_vector(cx.type_i8(), self.size.bytes()),
85         }
86     }
87 }
88
89 pub trait FnAbiGccExt<'gcc, 'tcx> {
90     // TODO: return a function pointer type instead?
91     fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool);
92     fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc>;
93     /*fn llvm_cconv(&self) -> llvm::CallConv;
94     fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value);
95     fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value);*/
96 }
97
98 impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
99     fn gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> (Type<'gcc>, Vec<Type<'gcc>>, bool) {
100         let args_capacity: usize = self.args.iter().map(|arg|
101             if arg.pad.is_some() {
102                 1
103             }
104             else {
105                 0
106             } +
107             if let PassMode::Pair(_, _) = arg.mode {
108                 2
109             } else {
110                 1
111             }
112         ).sum();
113         let mut argument_tys = Vec::with_capacity(
114             if let PassMode::Indirect { .. } = self.ret.mode {
115                 1
116             }
117             else {
118                 0
119             } + args_capacity,
120         );
121
122         let return_ty =
123             match self.ret.mode {
124                 PassMode::Ignore => cx.type_void(),
125                 PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
126                 PassMode::Cast(cast) => cast.gcc_type(cx),
127                 PassMode::Indirect { .. } => {
128                     argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
129                     cx.type_void()
130                 }
131             };
132
133         for arg in &self.args {
134             // add padding
135             if let Some(ty) = arg.pad {
136                 argument_tys.push(ty.gcc_type(cx));
137             }
138
139             let arg_ty = match arg.mode {
140                 PassMode::Ignore => continue,
141                 PassMode::Direct(_) => arg.layout.immediate_gcc_type(cx),
142                 PassMode::Pair(..) => {
143                     argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 0, true));
144                     argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 1, true));
145                     continue;
146                 }
147                 PassMode::Indirect { extra_attrs: Some(_), .. } => {
148                     /*let ptr_ty = cx.tcx.mk_mut_ptr(arg.layout.ty);
149                     let ptr_layout = cx.layout_of(ptr_ty);
150                     argument_tys.push(ptr_layout.scalar_pair_element_gcc_type(cx, 0, true));
151                     argument_tys.push(ptr_layout.scalar_pair_element_gcc_type(cx, 1, true));*/
152                     unimplemented!();
153                     //continue;
154                 }
155                 PassMode::Cast(cast) => cast.gcc_type(cx),
156                 PassMode::Indirect { extra_attrs: None, .. } => cx.type_ptr_to(arg.memory_ty(cx)),
157             };
158             argument_tys.push(arg_ty);
159         }
160
161         (return_ty, argument_tys, self.c_variadic)
162     }
163
164     fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
165         let (return_type, params, variadic) = self.gcc_type(cx);
166         let pointer_type = cx.context.new_function_pointer_type(None, return_type, &params, variadic);
167         pointer_type
168     }
169
170     /*fn llvm_cconv(&self) -> llvm::CallConv {
171         match self.conv {
172             Conv::C | Conv::Rust => llvm::CCallConv,
173             Conv::AmdGpuKernel => llvm::AmdGpuKernel,
174             Conv::ArmAapcs => llvm::ArmAapcsCallConv,
175             Conv::Msp430Intr => llvm::Msp430Intr,
176             Conv::PtxKernel => llvm::PtxKernel,
177             Conv::X86Fastcall => llvm::X86FastcallCallConv,
178             Conv::X86Intr => llvm::X86_Intr,
179             Conv::X86Stdcall => llvm::X86StdcallCallConv,
180             Conv::X86ThisCall => llvm::X86_ThisCall,
181             Conv::X86VectorCall => llvm::X86_VectorCall,
182             Conv::X86_64SysV => llvm::X86_64_SysV,
183             Conv::X86_64Win64 => llvm::X86_64_Win64,
184         }
185     }
186
187     fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value) {
188         // FIXME(eddyb) can this also be applied to callsites?
189         if self.ret.layout.abi.is_uninhabited() {
190             llvm::Attribute::NoReturn.apply_llfn(llvm::AttributePlace::Function, llfn);
191         }
192
193         // FIXME(eddyb, wesleywiser): apply this to callsites as well?
194         if !self.can_unwind {
195             llvm::Attribute::NoUnwind.apply_llfn(llvm::AttributePlace::Function, llfn);
196         }
197
198         let mut i = 0;
199         let mut apply = |attrs: &ArgAttributes, ty: Option<&Type>| {
200             attrs.apply_llfn(llvm::AttributePlace::Argument(i), llfn, ty);
201             i += 1;
202         };
203         match self.ret.mode {
204             PassMode::Direct(ref attrs) => {
205                 attrs.apply_llfn(llvm::AttributePlace::ReturnValue, llfn, None);
206             }
207             PassMode::Indirect(ref attrs, _) => apply(attrs, Some(self.ret.layout.gcc_type(cx))),
208             _ => {}
209         }
210         for arg in &self.args {
211             if arg.pad.is_some() {
212                 apply(&ArgAttributes::new(), None);
213             }
214             match arg.mode {
215                 PassMode::Ignore => {}
216                 PassMode::Direct(ref attrs) | PassMode::Indirect(ref attrs, None) => {
217                     apply(attrs, Some(arg.layout.gcc_type(cx)))
218                 }
219                 PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => {
220                     apply(attrs, None);
221                     apply(extra_attrs, None);
222                 }
223                 PassMode::Pair(ref a, ref b) => {
224                     apply(a, None);
225                     apply(b, None);
226                 }
227                 PassMode::Cast(_) => apply(&ArgAttributes::new(), None),
228             }
229         }
230     }
231
232     fn apply_attrs_callsite(&self, bx: &mut Builder<'a, 'll, 'tcx>, callsite: &'ll Value) {
233         // FIXME(wesleywiser, eddyb): We should apply `nounwind` and `noreturn` as appropriate to this callsite.
234
235         let mut i = 0;
236         let mut apply = |attrs: &ArgAttributes, ty: Option<&Type>| {
237             attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite, ty);
238             i += 1;
239         };
240         match self.ret.mode {
241             PassMode::Direct(ref attrs) => {
242                 attrs.apply_callsite(llvm::AttributePlace::ReturnValue, callsite, None);
243             }
244             PassMode::Indirect(ref attrs, _) => apply(attrs, Some(self.ret.layout.gcc_type(bx))),
245             _ => {}
246         }
247         if let abi::Abi::Scalar(ref scalar) = self.ret.layout.abi {
248             // If the value is a boolean, the range is 0..2 and that ultimately
249             // become 0..0 when the type becomes i1, which would be rejected
250             // by the LLVM verifier.
251             if let Int(..) = scalar.value {
252                 if !scalar.is_bool() {
253                     let range = scalar.valid_range_exclusive(bx);
254                     if range.start != range.end {
255                         bx.range_metadata(callsite, range);
256                     }
257                 }
258             }
259         }
260         for arg in &self.args {
261             if arg.pad.is_some() {
262                 apply(&ArgAttributes::new(), None);
263             }
264             match arg.mode {
265                 PassMode::Ignore => {}
266                 PassMode::Direct(ref attrs) | PassMode::Indirect(ref attrs, None) => {
267                     apply(attrs, Some(arg.layout.gcc_type(bx)))
268                 }
269                 PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => {
270                     apply(attrs, None);
271                     apply(extra_attrs, None);
272                 }
273                 PassMode::Pair(ref a, ref b) => {
274                     apply(a, None);
275                     apply(b, None);
276                 }
277                 PassMode::Cast(_) => apply(&ArgAttributes::new(), None),
278             }
279         }
280
281         let cconv = self.llvm_cconv();
282         if cconv != llvm::CCallConv {
283             llvm::SetInstructionCallConv(callsite, cconv);
284         }
285     }*/
286 }