4 use crate::value_and_place::assert_assignable;
6 use cranelift_codegen::ir::{ArgumentExtension, ArgumentPurpose};
7 use rustc_target::abi::call::{
8 ArgAbi, ArgAttributes, ArgExtension as RustcArgExtension, CastTarget, PassMode, Reg, RegKind,
10 use smallvec::{smallvec, SmallVec};
12 pub(super) trait ArgAbiExt<'tcx> {
13 fn get_abi_param(&self, tcx: TyCtxt<'tcx>) -> SmallVec<[AbiParam; 2]>;
14 fn get_abi_return(&self, tcx: TyCtxt<'tcx>) -> (Option<AbiParam>, Vec<AbiParam>);
17 fn reg_to_abi_param(reg: Reg) -> AbiParam {
18 let clif_ty = match (reg.kind, reg.size.bytes()) {
19 (RegKind::Integer, 1) => types::I8,
20 (RegKind::Integer, 2) => types::I16,
21 (RegKind::Integer, 3..=4) => types::I32,
22 (RegKind::Integer, 5..=8) => types::I64,
23 (RegKind::Integer, 9..=16) => types::I128,
24 (RegKind::Float, 4) => types::F32,
25 (RegKind::Float, 8) => types::F64,
26 (RegKind::Vector, size) => types::I8.by(u32::try_from(size).unwrap()).unwrap(),
27 _ => unreachable!("{:?}", reg),
29 AbiParam::new(clif_ty)
32 fn apply_arg_attrs_to_abi_param(mut param: AbiParam, arg_attrs: ArgAttributes) -> AbiParam {
33 match arg_attrs.arg_ext {
34 RustcArgExtension::None => {}
35 RustcArgExtension::Zext => param.extension = ArgumentExtension::Uext,
36 RustcArgExtension::Sext => param.extension = ArgumentExtension::Sext,
41 fn cast_target_to_abi_params(cast: &CastTarget) -> SmallVec<[AbiParam; 2]> {
42 let (rest_count, rem_bytes) = if cast.rest.unit.size.bytes() == 0 {
46 cast.rest.total.bytes() / cast.rest.unit.size.bytes(),
47 cast.rest.total.bytes() % cast.rest.unit.size.bytes(),
51 // Note: Unlike the LLVM equivalent of this code we don't have separate branches for when there
52 // is no prefix as a single unit, an array and a heterogeneous struct are not represented using
53 // different types in Cranelift IR. Instead a single array of primitive types is used.
55 // Create list of fields in the main structure
60 .map(|®| reg_to_abi_param(reg))
61 .chain((0..rest_count).map(|_| reg_to_abi_param(cast.rest.unit)))
62 .collect::<SmallVec<_>>();
64 // Append final integer
66 // Only integers can be really split further.
67 assert_eq!(cast.rest.unit.kind, RegKind::Integer);
68 args.push(reg_to_abi_param(Reg {
69 kind: RegKind::Integer,
70 size: Size::from_bytes(rem_bytes),
77 impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
78 fn get_abi_param(&self, tcx: TyCtxt<'tcx>) -> SmallVec<[AbiParam; 2]> {
80 PassMode::Ignore => smallvec![],
81 PassMode::Direct(attrs) => match self.layout.abi {
82 Abi::Scalar(scalar) => smallvec![apply_arg_attrs_to_abi_param(
83 AbiParam::new(scalar_to_clif_type(tcx, scalar)),
86 Abi::Vector { .. } => {
87 let vector_ty = crate::intrinsics::clif_vector_type(tcx, self.layout).unwrap();
88 smallvec![AbiParam::new(vector_ty)]
90 _ => unreachable!("{:?}", self.layout.abi),
92 PassMode::Pair(attrs_a, attrs_b) => match self.layout.abi {
93 Abi::ScalarPair(a, b) => {
94 let a = scalar_to_clif_type(tcx, a);
95 let b = scalar_to_clif_type(tcx, b);
97 apply_arg_attrs_to_abi_param(AbiParam::new(a), attrs_a),
98 apply_arg_attrs_to_abi_param(AbiParam::new(b), attrs_b),
101 _ => unreachable!("{:?}", self.layout.abi),
103 PassMode::Cast(ref cast, pad_i32) => {
104 assert!(!pad_i32, "padding support not yet implemented");
105 cast_target_to_abi_params(cast)
107 PassMode::Indirect { attrs, extra_attrs: None, on_stack } => {
109 // Abi requires aligning struct size to pointer size
110 let size = self.layout.size.align_to(tcx.data_layout.pointer_align.abi);
111 let size = u32::try_from(size.bytes()).unwrap();
112 smallvec![apply_arg_attrs_to_abi_param(
113 AbiParam::special(pointer_ty(tcx), ArgumentPurpose::StructArgument(size),),
117 smallvec![apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs)]
120 PassMode::Indirect { attrs, extra_attrs: Some(extra_attrs), on_stack } => {
123 apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs),
124 apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), extra_attrs),
130 fn get_abi_return(&self, tcx: TyCtxt<'tcx>) -> (Option<AbiParam>, Vec<AbiParam>) {
132 PassMode::Ignore => (None, vec![]),
133 PassMode::Direct(_) => match self.layout.abi {
134 Abi::Scalar(scalar) => {
135 (None, vec![AbiParam::new(scalar_to_clif_type(tcx, scalar))])
137 Abi::Vector { .. } => {
138 let vector_ty = crate::intrinsics::clif_vector_type(tcx, self.layout).unwrap();
139 (None, vec![AbiParam::new(vector_ty)])
141 _ => unreachable!("{:?}", self.layout.abi),
143 PassMode::Pair(_, _) => match self.layout.abi {
144 Abi::ScalarPair(a, b) => {
145 let a = scalar_to_clif_type(tcx, a);
146 let b = scalar_to_clif_type(tcx, b);
147 (None, vec![AbiParam::new(a), AbiParam::new(b)])
149 _ => unreachable!("{:?}", self.layout.abi),
151 PassMode::Cast(ref cast, _) => {
152 (None, cast_target_to_abi_params(cast).into_iter().collect())
154 PassMode::Indirect { attrs: _, extra_attrs: None, on_stack } => {
156 (Some(AbiParam::special(pointer_ty(tcx), ArgumentPurpose::StructReturn)), vec![])
158 PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
159 unreachable!("unsized return value")
165 pub(super) fn to_casted_value<'tcx>(
166 fx: &mut FunctionCx<'_, '_, 'tcx>,
169 ) -> SmallVec<[Value; 2]> {
170 let (ptr, meta) = arg.force_stack(fx);
171 assert!(meta.is_none());
173 cast_target_to_abi_params(cast)
176 let val = ptr.offset_i64(fx, offset).load(fx, param.value_type, MemFlags::new());
177 offset += i64::from(param.value_type.bytes());
183 pub(super) fn from_casted_value<'tcx>(
184 fx: &mut FunctionCx<'_, '_, 'tcx>,
185 block_params: &[Value],
186 layout: TyAndLayout<'tcx>,
189 let abi_params = cast_target_to_abi_params(cast);
190 let abi_param_size: u32 = abi_params.iter().map(|param| param.value_type.bytes()).sum();
191 let layout_size = u32::try_from(layout.size.bytes()).unwrap();
192 let stack_slot = fx.bcx.create_sized_stack_slot(StackSlotData {
193 kind: StackSlotKind::ExplicitSlot,
194 // FIXME Don't force the size to a multiple of 16 bytes once Cranelift gets a way to
195 // specify stack slot alignment.
196 // Stack slot size may be bigger for for example `[u8; 3]` which is packed into an `i32`.
197 // It may also be smaller for example when the type is a wrapper around an integer with a
198 // larger alignment than the integer.
199 size: (std::cmp::max(abi_param_size, layout_size) + 15) / 16 * 16,
201 let ptr = Pointer::stack_slot(stack_slot);
203 let mut block_params_iter = block_params.iter().copied();
204 for param in abi_params {
205 let val = ptr.offset_i64(fx, offset).store(
207 block_params_iter.next().unwrap(),
210 offset += i64::from(param.value_type.bytes());
213 assert_eq!(block_params_iter.next(), None, "Leftover block param");
214 CValue::by_ref(ptr, layout)
217 /// Get a set of values to be passed as function arguments.
218 pub(super) fn adjust_arg_for_abi<'tcx>(
219 fx: &mut FunctionCx<'_, '_, 'tcx>,
221 arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
223 ) -> SmallVec<[Value; 2]> {
224 assert_assignable(fx, arg.layout().ty, arg_abi.layout.ty, 16);
226 PassMode::Ignore => smallvec![],
227 PassMode::Direct(_) => smallvec![arg.load_scalar(fx)],
228 PassMode::Pair(_, _) => {
229 let (a, b) = arg.load_scalar_pair(fx);
232 PassMode::Cast(ref cast, _) => to_casted_value(fx, arg, cast),
233 PassMode::Indirect { .. } => {
235 match arg.force_stack(fx) {
236 (ptr, None) => smallvec![ptr.get_addr(fx)],
237 (ptr, Some(meta)) => smallvec![ptr.get_addr(fx), meta],
240 // Ownership of the value at the backing storage for an argument is passed to the
241 // callee per the ABI, so we must make a copy of the argument unless the argument
243 let place = CPlace::new_stack_slot(fx, arg.layout());
244 place.write_cvalue(fx, arg);
245 smallvec![place.to_ptr().get_addr(fx)]
251 /// Create a [`CValue`] containing the value of a function parameter adding clif function parameters
253 pub(super) fn cvalue_for_param<'tcx>(
254 fx: &mut FunctionCx<'_, '_, 'tcx>,
255 local: Option<mir::Local>,
256 local_field: Option<usize>,
257 arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
258 block_params_iter: &mut impl Iterator<Item = Value>,
259 ) -> Option<CValue<'tcx>> {
260 let block_params = arg_abi
261 .get_abi_param(fx.tcx)
264 let block_param = block_params_iter.next().unwrap();
265 assert_eq!(fx.bcx.func.dfg.value_type(block_param), abi_param.value_type);
268 .collect::<SmallVec<[_; 2]>>();
270 crate::abi::comments::add_arg_comment(
281 PassMode::Ignore => None,
282 PassMode::Direct(_) => {
283 assert_eq!(block_params.len(), 1, "{:?}", block_params);
284 Some(CValue::by_val(block_params[0], arg_abi.layout))
286 PassMode::Pair(_, _) => {
287 assert_eq!(block_params.len(), 2, "{:?}", block_params);
288 Some(CValue::by_val_pair(block_params[0], block_params[1], arg_abi.layout))
290 PassMode::Cast(ref cast, _) => {
291 Some(from_casted_value(fx, &block_params, arg_abi.layout, cast))
293 PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
294 assert_eq!(block_params.len(), 1, "{:?}", block_params);
295 Some(CValue::by_ref(Pointer::new(block_params[0]), arg_abi.layout))
297 PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
298 assert_eq!(block_params.len(), 2, "{:?}", block_params);
299 Some(CValue::by_ref_unsized(
300 Pointer::new(block_params[0]),