1 use gccjit::{ToRValue, Type};
2 use rustc_codegen_ssa::traits::{AbiBuilderMethods, BaseTypeMethods};
4 use rustc_middle::ty::Ty;
5 use rustc_target::abi::call::{CastTarget, FnAbi, PassMode, Reg, RegKind};
7 use crate::builder::Builder;
8 use crate::context::CodegenCx;
9 use crate::intrinsic::ArgAbiExt;
10 use crate::type_of::LayoutGccExt;
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) {
15 //fn_abi.apply_attrs_callsite(self, callsite)
18 fn get_param(&self, index: usize) -> Self::Value {
19 self.cx.current_func.borrow().expect("current func")
20 .get_param(index as i32)
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 {
33 (self.rest.total.bytes() / self.rest.unit.size.bytes(), self.rest.total.bytes() % self.rest.unit.size.bytes())
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 {
42 // Simplify to array when all chunks are the same size and type
44 return cx.type_array(rest_gcc_unit, rest_count);
48 // Create list of fields in the main structure
49 let mut args: Vec<_> = self
52 .flat_map(|option_kind| {
53 option_kind.map(|kind| Reg { kind, size: self.prefix_chunk_size }.gcc_type(cx))
55 .chain((0..rest_count).map(|_| rest_gcc_unit))
58 // Append final integer
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));
65 cx.type_struct(&args, false)
70 fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc>;
73 impl GccType for Reg {
74 fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
76 RegKind::Integer => cx.type_ix(self.size.bits()),
78 match self.size.bits() {
81 _ => bug!("unsupported float: {:?}", self),
84 RegKind::Vector => unimplemented!(), //cx.type_vector(cx.type_i8(), self.size.bytes()),
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);*/
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() {
107 if let PassMode::Pair(_, _) = arg.mode {
113 let mut argument_tys = Vec::with_capacity(
114 if let PassMode::Indirect { .. } = self.ret.mode {
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)));
133 for arg in &self.args {
135 if let Some(ty) = arg.pad {
136 argument_tys.push(ty.gcc_type(cx));
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));
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));*/
155 PassMode::Cast(cast) => cast.gcc_type(cx),
156 PassMode::Indirect { extra_attrs: None, .. } => cx.type_ptr_to(arg.memory_ty(cx)),
158 argument_tys.push(arg_ty);
161 (return_ty, argument_tys, self.c_variadic)
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, ¶ms, variadic);
170 /*fn llvm_cconv(&self) -> llvm::CallConv {
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,
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);
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);
199 let mut apply = |attrs: &ArgAttributes, ty: Option<&Type>| {
200 attrs.apply_llfn(llvm::AttributePlace::Argument(i), llfn, ty);
203 match self.ret.mode {
204 PassMode::Direct(ref attrs) => {
205 attrs.apply_llfn(llvm::AttributePlace::ReturnValue, llfn, None);
207 PassMode::Indirect(ref attrs, _) => apply(attrs, Some(self.ret.layout.gcc_type(cx))),
210 for arg in &self.args {
211 if arg.pad.is_some() {
212 apply(&ArgAttributes::new(), None);
215 PassMode::Ignore => {}
216 PassMode::Direct(ref attrs) | PassMode::Indirect(ref attrs, None) => {
217 apply(attrs, Some(arg.layout.gcc_type(cx)))
219 PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => {
221 apply(extra_attrs, None);
223 PassMode::Pair(ref a, ref b) => {
227 PassMode::Cast(_) => apply(&ArgAttributes::new(), None),
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.
236 let mut apply = |attrs: &ArgAttributes, ty: Option<&Type>| {
237 attrs.apply_callsite(llvm::AttributePlace::Argument(i), callsite, ty);
240 match self.ret.mode {
241 PassMode::Direct(ref attrs) => {
242 attrs.apply_callsite(llvm::AttributePlace::ReturnValue, callsite, None);
244 PassMode::Indirect(ref attrs, _) => apply(attrs, Some(self.ret.layout.gcc_type(bx))),
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);
260 for arg in &self.args {
261 if arg.pad.is_some() {
262 apply(&ArgAttributes::new(), None);
265 PassMode::Ignore => {}
266 PassMode::Direct(ref attrs) | PassMode::Indirect(ref attrs, None) => {
267 apply(attrs, Some(arg.layout.gcc_type(bx)))
269 PassMode::Indirect(ref attrs, Some(ref extra_attrs)) => {
271 apply(extra_attrs, None);
273 PassMode::Pair(ref a, ref b) => {
277 PassMode::Cast(_) => apply(&ArgAttributes::new(), None),
281 let cconv = self.llvm_cconv();
282 if cconv != llvm::CCallConv {
283 llvm::SetInstructionCallConv(callsite, cconv);