1 use gccjit::{ToLValue, ToRValue, Type};
2 use rustc_codegen_ssa::traits::{AbiBuilderMethods, BaseTypeMethods};
3 use rustc_data_structures::fx::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();
111 // This capacity calculation is approximate.
112 let mut argument_tys = Vec::with_capacity(
113 self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 }
117 match self.ret.mode {
118 PassMode::Ignore => cx.type_void(),
119 PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
120 PassMode::Cast(ref cast, _) => cast.gcc_type(cx),
121 PassMode::Indirect { .. } => {
122 argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
127 for arg in self.args.iter() {
128 let arg_ty = match arg.mode {
129 PassMode::Ignore => continue,
130 PassMode::Direct(_) => arg.layout.immediate_gcc_type(cx),
131 PassMode::Pair(..) => {
132 argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 0, true));
133 argument_tys.push(arg.layout.scalar_pair_element_gcc_type(cx, 1, true));
136 PassMode::Indirect { extra_attrs: Some(_), .. } => {
139 PassMode::Cast(ref cast, pad_i32) => {
142 argument_tys.push(Reg::i32().gcc_type(cx));
146 PassMode::Indirect { extra_attrs: None, on_stack: true, .. } => {
147 on_stack_param_indices.insert(argument_tys.len());
150 PassMode::Indirect { extra_attrs: None, on_stack: false, .. } => cx.type_ptr_to(arg.memory_ty(cx)),
152 argument_tys.push(arg_ty);
155 (return_ty, argument_tys, self.c_variadic, on_stack_param_indices)
158 fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
159 let (return_type, params, variadic, on_stack_param_indices) = self.gcc_type(cx);
160 let pointer_type = cx.context.new_function_pointer_type(None, return_type, ¶ms, variadic);
161 cx.on_stack_params.borrow_mut().insert(pointer_type.dyncast_function_ptr_type().expect("function ptr type"), on_stack_param_indices);