stable_mir/abi.rs
1use std::fmt::{self, Debug};
2use std::num::NonZero;
3use std::ops::RangeInclusive;
4
5use serde::Serialize;
6
7use crate::compiler_interface::with;
8use crate::mir::FieldIdx;
9use crate::target::{MachineInfo, MachineSize as Size};
10use crate::ty::{Align, IndexedVal, Ty, VariantIdx};
11use crate::{Error, Opaque, error};
12
13/// A function ABI definition.
14#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
15pub struct FnAbi {
16 /// The types of each argument.
17 pub args: Vec<ArgAbi>,
18
19 /// The expected return type.
20 pub ret: ArgAbi,
21
22 /// The count of non-variadic arguments.
23 ///
24 /// Should only be different from `args.len()` when a function is a C variadic function.
25 pub fixed_count: u32,
26
27 /// The ABI convention.
28 pub conv: CallConvention,
29
30 /// Whether this is a variadic C function,
31 pub c_variadic: bool,
32}
33
34/// Information about the ABI of a function's argument, or return value.
35#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
36pub struct ArgAbi {
37 pub ty: Ty,
38 pub layout: Layout,
39 pub mode: PassMode,
40}
41
42/// How a function argument should be passed in to the target function.
43#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
44pub enum PassMode {
45 /// Ignore the argument.
46 ///
47 /// The argument is either uninhabited or a ZST.
48 Ignore,
49 /// Pass the argument directly.
50 ///
51 /// The argument has a layout abi of `Scalar` or `Vector`.
52 Direct(Opaque),
53 /// Pass a pair's elements directly in two arguments.
54 ///
55 /// The argument has a layout abi of `ScalarPair`.
56 Pair(Opaque, Opaque),
57 /// Pass the argument after casting it.
58 Cast { pad_i32: bool, cast: Opaque },
59 /// Pass the argument indirectly via a hidden pointer.
60 Indirect { attrs: Opaque, meta_attrs: Opaque, on_stack: bool },
61}
62
63/// The layout of a type, alongside the type itself.
64#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize)]
65pub struct TyAndLayout {
66 pub ty: Ty,
67 pub layout: Layout,
68}
69
70/// The layout of a type in memory.
71#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
72pub struct LayoutShape {
73 /// The fields location within the layout
74 pub fields: FieldsShape,
75
76 /// Encodes information about multi-variant layouts.
77 /// Even with `Multiple` variants, a layout still has its own fields! Those are then
78 /// shared between all variants.
79 ///
80 /// To access all fields of this layout, both `fields` and the fields of the active variant
81 /// must be taken into account.
82 pub variants: VariantsShape,
83
84 /// The `abi` defines how this data is passed between functions.
85 pub abi: ValueAbi,
86
87 /// The ABI mandated alignment in bytes.
88 pub abi_align: Align,
89
90 /// The size of this layout in bytes.
91 pub size: Size,
92}
93
94impl LayoutShape {
95 /// Returns `true` if the layout corresponds to an unsized type.
96 #[inline]
97 pub fn is_unsized(&self) -> bool {
98 self.abi.is_unsized()
99 }
100
101 #[inline]
102 pub fn is_sized(&self) -> bool {
103 !self.abi.is_unsized()
104 }
105
106 /// Returns `true` if the type is sized and a 1-ZST (meaning it has size 0 and alignment 1).
107 pub fn is_1zst(&self) -> bool {
108 self.is_sized() && self.size.bits() == 0 && self.abi_align == 1
109 }
110}
111
112#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize)]
113pub struct Layout(usize);
114
115impl Layout {
116 pub fn shape(self) -> LayoutShape {
117 with(|cx| cx.layout_shape(self))
118 }
119}
120
121impl IndexedVal for Layout {
122 fn to_val(index: usize) -> Self {
123 Layout(index)
124 }
125 fn to_index(&self) -> usize {
126 self.0
127 }
128}
129
130/// Describes how the fields of a type are shaped in memory.
131#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
132pub enum FieldsShape {
133 /// Scalar primitives and `!`, which never have fields.
134 Primitive,
135
136 /// All fields start at no offset. The `usize` is the field count.
137 Union(NonZero<usize>),
138
139 /// Array/vector-like placement, with all fields of identical types.
140 Array { stride: Size, count: u64 },
141
142 /// Struct-like placement, with precomputed offsets.
143 ///
144 /// Fields are guaranteed to not overlap, but note that gaps
145 /// before, between and after all the fields are NOT always
146 /// padding, and as such their contents may not be discarded.
147 /// For example, enum variants leave a gap at the start,
148 /// where the discriminant field in the enum layout goes.
149 Arbitrary {
150 /// Offsets for the first byte of each field,
151 /// ordered to match the source definition order.
152 /// I.e.: It follows the same order as [crate::ty::VariantDef::fields()].
153 /// This vector does not go in increasing order.
154 offsets: Vec<Size>,
155 },
156}
157
158impl FieldsShape {
159 pub fn fields_by_offset_order(&self) -> Vec<FieldIdx> {
160 match self {
161 FieldsShape::Primitive => vec![],
162 FieldsShape::Union(_) | FieldsShape::Array { .. } => (0..self.count()).collect(),
163 FieldsShape::Arbitrary { offsets, .. } => {
164 let mut indices = (0..offsets.len()).collect::<Vec<_>>();
165 indices.sort_by_key(|idx| offsets[*idx]);
166 indices
167 }
168 }
169 }
170
171 pub fn count(&self) -> usize {
172 match self {
173 FieldsShape::Primitive => 0,
174 FieldsShape::Union(count) => count.get(),
175 FieldsShape::Array { count, .. } => *count as usize,
176 FieldsShape::Arbitrary { offsets, .. } => offsets.len(),
177 }
178 }
179}
180
181#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
182pub enum VariantsShape {
183 /// A type with no valid variants. Must be uninhabited.
184 Empty,
185
186 /// Single enum variants, structs/tuples, unions, and all non-ADTs.
187 Single { index: VariantIdx },
188
189 /// Enum-likes with more than one inhabited variant: each variant comes with
190 /// a *discriminant* (usually the same as the variant index but the user can
191 /// assign explicit discriminant values). That discriminant is encoded
192 /// as a *tag* on the machine. The layout of each variant is
193 /// a struct, and they all have space reserved for the tag.
194 /// For enums, the tag is the sole field of the layout.
195 Multiple {
196 tag: Scalar,
197 tag_encoding: TagEncoding,
198 tag_field: usize,
199 variants: Vec<LayoutShape>,
200 },
201}
202
203#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
204pub enum TagEncoding {
205 /// The tag directly stores the discriminant, but possibly with a smaller layout
206 /// (so converting the tag to the discriminant can require sign extension).
207 Direct,
208
209 /// Niche (values invalid for a type) encoding the discriminant:
210 /// Discriminant and variant index coincide.
211 /// The variant `untagged_variant` contains a niche at an arbitrary
212 /// offset (field `tag_field` of the enum), which for a variant with
213 /// discriminant `d` is set to
214 /// `(d - niche_variants.start).wrapping_add(niche_start)`.
215 ///
216 /// For example, `Option<(usize, &T)>` is represented such that
217 /// `None` has a null pointer for the second tuple field, and
218 /// `Some` is the identity function (with a non-null reference).
219 Niche {
220 untagged_variant: VariantIdx,
221 niche_variants: RangeInclusive<VariantIdx>,
222 niche_start: u128,
223 },
224}
225
226/// Describes how values of the type are passed by target ABIs,
227/// in terms of categories of C types there are ABI rules for.
228#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
229pub enum ValueAbi {
230 Uninhabited,
231 Scalar(Scalar),
232 ScalarPair(Scalar, Scalar),
233 Vector {
234 element: Scalar,
235 count: u64,
236 },
237 Aggregate {
238 /// If true, the size is exact, otherwise it's only a lower bound.
239 sized: bool,
240 },
241}
242
243impl ValueAbi {
244 /// Returns `true` if the layout corresponds to an unsized type.
245 pub fn is_unsized(&self) -> bool {
246 match *self {
247 ValueAbi::Uninhabited
248 | ValueAbi::Scalar(_)
249 | ValueAbi::ScalarPair(..)
250 | ValueAbi::Vector { .. } => false,
251 ValueAbi::Aggregate { sized } => !sized,
252 }
253 }
254}
255
256/// Information about one scalar component of a Rust type.
257#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize)]
258pub enum Scalar {
259 Initialized {
260 /// The primitive type used to represent this value.
261 value: Primitive,
262 /// The range that represents valid values.
263 /// The range must be valid for the `primitive` size.
264 valid_range: WrappingRange,
265 },
266 Union {
267 /// Unions never have niches, so there is no `valid_range`.
268 /// Even for unions, we need to use the correct registers for the kind of
269 /// values inside the union, so we keep the `Primitive` type around.
270 /// It is also used to compute the size of the scalar.
271 value: Primitive,
272 },
273}
274
275impl Scalar {
276 pub fn has_niche(&self, target: &MachineInfo) -> bool {
277 match self {
278 Scalar::Initialized { value, valid_range } => {
279 !valid_range.is_full(value.size(target)).unwrap()
280 }
281 Scalar::Union { .. } => false,
282 }
283 }
284}
285
286/// Fundamental unit of memory access and layout.
287#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize)]
288pub enum Primitive {
289 /// The `bool` is the signedness of the `Integer` type.
290 ///
291 /// One would think we would not care about such details this low down,
292 /// but some ABIs are described in terms of C types and ISAs where the
293 /// integer arithmetic is done on {sign,zero}-extended registers, e.g.
294 /// a negative integer passed by zero-extension will appear positive in
295 /// the callee, and most operations on it will produce the wrong values.
296 Int {
297 length: IntegerLength,
298 signed: bool,
299 },
300 Float {
301 length: FloatLength,
302 },
303 Pointer(AddressSpace),
304}
305
306impl Primitive {
307 pub fn size(self, target: &MachineInfo) -> Size {
308 match self {
309 Primitive::Int { length, .. } => Size::from_bits(length.bits()),
310 Primitive::Float { length } => Size::from_bits(length.bits()),
311 Primitive::Pointer(_) => target.pointer_width,
312 }
313 }
314}
315
316/// Enum representing the existing integer lengths.
317#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize)]
318pub enum IntegerLength {
319 I8,
320 I16,
321 I32,
322 I64,
323 I128,
324}
325
326/// Enum representing the existing float lengths.
327#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Serialize)]
328pub enum FloatLength {
329 F16,
330 F32,
331 F64,
332 F128,
333}
334
335impl IntegerLength {
336 pub fn bits(self) -> usize {
337 match self {
338 IntegerLength::I8 => 8,
339 IntegerLength::I16 => 16,
340 IntegerLength::I32 => 32,
341 IntegerLength::I64 => 64,
342 IntegerLength::I128 => 128,
343 }
344 }
345}
346
347impl FloatLength {
348 pub fn bits(self) -> usize {
349 match self {
350 FloatLength::F16 => 16,
351 FloatLength::F32 => 32,
352 FloatLength::F64 => 64,
353 FloatLength::F128 => 128,
354 }
355 }
356}
357
358/// An identifier that specifies the address space that some operation
359/// should operate on. Special address spaces have an effect on code generation,
360/// depending on the target and the address spaces it implements.
361#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
362pub struct AddressSpace(pub u32);
363
364impl AddressSpace {
365 /// The default address space, corresponding to data space.
366 pub const DATA: Self = AddressSpace(0);
367}
368
369/// Inclusive wrap-around range of valid values (bitwise representation), that is, if
370/// start > end, it represents `start..=MAX`, followed by `0..=end`.
371///
372/// That is, for an i8 primitive, a range of `254..=2` means following
373/// sequence:
374///
375/// 254 (-2), 255 (-1), 0, 1, 2
376#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)]
377pub struct WrappingRange {
378 pub start: u128,
379 pub end: u128,
380}
381
382impl WrappingRange {
383 /// Returns `true` if `size` completely fills the range.
384 #[inline]
385 pub fn is_full(&self, size: Size) -> Result<bool, Error> {
386 let Some(max_value) = size.unsigned_int_max() else {
387 return Err(error!("Expected size <= 128 bits, but found {} instead", size.bits()));
388 };
389 if self.start <= max_value && self.end <= max_value {
390 Ok(self.start == (self.end.wrapping_add(1) & max_value))
391 } else {
392 Err(error!("Range `{self:?}` out of bounds for size `{}` bits.", size.bits()))
393 }
394 }
395
396 /// Returns `true` if `v` is contained in the range.
397 #[inline(always)]
398 pub fn contains(&self, v: u128) -> bool {
399 if self.wraps_around() {
400 self.start <= v || v <= self.end
401 } else {
402 self.start <= v && v <= self.end
403 }
404 }
405
406 /// Returns `true` if the range wraps around.
407 /// I.e., the range represents the union of `self.start..=MAX` and `0..=self.end`.
408 /// Returns `false` if this is a non-wrapping range, i.e.: `self.start..=self.end`.
409 #[inline]
410 pub fn wraps_around(&self) -> bool {
411 self.start > self.end
412 }
413}
414
415impl Debug for WrappingRange {
416 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
417 if self.start > self.end {
418 write!(fmt, "(..={}) | ({}..)", self.end, self.start)?;
419 } else {
420 write!(fmt, "{}..={}", self.start, self.end)?;
421 }
422 Ok(())
423 }
424}
425
426/// General language calling conventions.
427#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize)]
428pub enum CallConvention {
429 C,
430 Rust,
431
432 Cold,
433 PreserveMost,
434 PreserveAll,
435
436 // Target-specific calling conventions.
437 ArmAapcs,
438 CCmseNonSecureCall,
439 CCmseNonSecureEntry,
440
441 Msp430Intr,
442
443 PtxKernel,
444
445 GpuKernel,
446
447 X86Fastcall,
448 X86Intr,
449 X86Stdcall,
450 X86ThisCall,
451 X86VectorCall,
452
453 X86_64SysV,
454 X86_64Win64,
455
456 AvrInterrupt,
457 AvrNonBlockingInterrupt,
458
459 RiscvInterrupt,
460}