1 use gccjit::{ToLValue, ToRValue, Type};
2 use rustc_codegen_ssa::traits::{AbiBuilderMethods, BaseTypeMethods};
3 use rustc_data_structures::stable_set::FxHashSet;
5 use rustc_middle::ty::Ty;
6 use rustc_target::abi::call::{CastTarget, FnAbi, PassMode, Reg, RegKind};
8 use crate::builder::Builder;
9 use crate::context::CodegenCx;
10 use crate::intrinsic::ArgAbiExt;
11 use crate::type_of::LayoutGccExt;
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) {
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);
22 if let Some(on_stack_param_indices) = self.on_stack_function_params.borrow().get(&func) {
23 on_stack_param_indices.contains(&index)
29 param.to_lvalue().get_address(None)
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 {
45 (self.rest.total.bytes() / self.rest.unit.size.bytes(), self.rest.total.bytes() % self.rest.unit.size.bytes())
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 {
54 // Simplify to array when all chunks are the same size and type
56 return cx.type_array(rest_gcc_unit, rest_count);
60 // Create list of fields in the main structure
61 let mut args: Vec<_> = self
64 .flat_map(|option_reg| {
65 option_reg.map(|reg| reg.gcc_type(cx))
67 .chain((0..rest_count).map(|_| rest_gcc_unit))
70 // Append final integer
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));
77 cx.type_struct(&args, false)
82 fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc>;
85 impl GccType for Reg {
86 fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
88 RegKind::Integer => cx.type_ix(self.size.bits()),
90 match self.size.bits() {
93 _ => bug!("unsupported float: {:?}", self),
96 RegKind::Vector => unimplemented!(), //cx.type_vector(cx.type_i8(), self.size.bytes()),
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>;
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() {
117 if let PassMode::Pair(_, _) = arg.mode {
123 let mut argument_tys = Vec::with_capacity(
124 if let PassMode::Indirect { .. } = self.ret.mode {
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)));
143 for arg in &self.args {
145 if let Some(ty) = arg.pad {
146 argument_tys.push(ty.gcc_type(cx));
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));
157 PassMode::Indirect { extra_attrs: Some(_), .. } => {
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());
165 PassMode::Indirect { extra_attrs: None, on_stack: false, .. } => cx.type_ptr_to(arg.memory_ty(cx)),
167 argument_tys.push(arg_ty);
170 (return_ty, argument_tys, self.c_variadic, on_stack_param_indices)
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, ¶ms, variadic);
176 cx.on_stack_params.borrow_mut().insert(pointer_type.dyncast_function_ptr_type().expect("function ptr type"), on_stack_param_indices);