Skip to main content

rustc_public/unstable/convert/stable/
abi.rs

1//! Conversion of internal Rust compiler `rustc_target` and `rustc_abi` items to stable ones.
2
3#![allow(rustc::usage_of_qualified_ty)]
4
5use rustc_abi::{ArmCall, CanonAbi, InterruptKind, X86Call};
6use rustc_middle::ty;
7use rustc_public_bridge::Tables;
8use rustc_public_bridge::context::CompilerCtxt;
9use rustc_target::callconv;
10
11use crate::IndexedVal;
12use crate::abi::{
13    AddressSpace, ArgAbi, ArgAttributeFlags, ArgAttributes, ArgExtension, CallConvention,
14    CastTarget, FieldsShape, FloatLength, FnAbi, IndirectMode, IntegerLength, IntegerType, Layout,
15    LayoutShape, NumScalableVectors, PassMode, Primitive, Reg, RegKind, ReprFlags, ReprOptions,
16    Scalar, TagEncoding, TyAndLayout, Uniform, ValueRepr, VariantFields, VariantsShape,
17    WrappingRange,
18};
19use crate::compiler_interface::BridgeTys;
20use crate::target::MachineSize as Size;
21use crate::ty::{Align, VariantIdx};
22use crate::unstable::Stable;
23
24impl<'tcx> Stable<'tcx> for rustc_abi::VariantIdx {
25    type T = VariantIdx;
26    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
27        VariantIdx::to_val(self.as_usize())
28    }
29}
30
31impl<'tcx> Stable<'tcx> for rustc_abi::Endian {
32    type T = crate::target::Endian;
33
34    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
35        match self {
36            rustc_abi::Endian::Little => crate::target::Endian::Little,
37            rustc_abi::Endian::Big => crate::target::Endian::Big,
38        }
39    }
40}
41
42impl<'tcx> Stable<'tcx> for rustc_abi::TyAndLayout<'tcx, ty::Ty<'tcx>> {
43    type T = TyAndLayout;
44
45    fn stable<'cx>(
46        &self,
47        tables: &mut Tables<'cx, BridgeTys>,
48        cx: &CompilerCtxt<'cx, BridgeTys>,
49    ) -> Self::T {
50        TyAndLayout { ty: self.ty.stable(tables, cx), layout: self.layout.stable(tables, cx) }
51    }
52}
53
54impl<'tcx> Stable<'tcx> for rustc_abi::Layout<'tcx> {
55    type T = Layout;
56
57    fn stable<'cx>(
58        &self,
59        tables: &mut Tables<'cx, BridgeTys>,
60        cx: &CompilerCtxt<'cx, BridgeTys>,
61    ) -> Self::T {
62        tables.layout_id(cx.lift(*self))
63    }
64}
65
66impl<'tcx> Stable<'tcx> for rustc_abi::LayoutData<rustc_abi::FieldIdx, rustc_abi::VariantIdx> {
67    type T = LayoutShape;
68
69    fn stable<'cx>(
70        &self,
71        tables: &mut Tables<'cx, BridgeTys>,
72        cx: &CompilerCtxt<'cx, BridgeTys>,
73    ) -> Self::T {
74        LayoutShape {
75            fields: self.fields.stable(tables, cx),
76            variants: self.variants.stable(tables, cx),
77            value_repr: self.backend_repr.stable(tables, cx),
78            abi_align: self.align.stable(tables, cx),
79            size: self.size.stable(tables, cx),
80        }
81    }
82}
83
84impl<'tcx> Stable<'tcx> for callconv::FnAbi<'tcx, ty::Ty<'tcx>> {
85    type T = FnAbi;
86
87    fn stable<'cx>(
88        &self,
89        tables: &mut Tables<'cx, BridgeTys>,
90        cx: &CompilerCtxt<'cx, BridgeTys>,
91    ) -> Self::T {
92        if !(self.args.len() >= self.fixed_count as usize) {
    ::core::panicking::panic("assertion failed: self.args.len() >= self.fixed_count as usize")
};assert!(self.args.len() >= self.fixed_count as usize);
93        if !(!self.c_variadic ||
            #[allow(non_exhaustive_omitted_patterns)] match self.conv {
                CanonAbi::C | CanonAbi::X86(X86Call::SysV64 | X86Call::Win64)
                    | CanonAbi::Arm(ArmCall::Aapcs) => true,
                _ => false,
            }) {
    ::core::panicking::panic("assertion failed: !self.c_variadic ||\n    matches!(self.conv, CanonAbi::C |\n        CanonAbi::X86(X86Call::SysV64 | X86Call::Win64) |\n        CanonAbi::Arm(ArmCall::Aapcs))")
};assert!(
94            !self.c_variadic
95                || matches!(
96                    self.conv,
97                    CanonAbi::C
98                        | CanonAbi::X86(X86Call::SysV64 | X86Call::Win64)
99                        | CanonAbi::Arm(ArmCall::Aapcs)
100                )
101        );
102        FnAbi {
103            args: self.args.as_ref().stable(tables, cx),
104            ret: self.ret.stable(tables, cx),
105            fixed_count: self.fixed_count,
106            conv: self.conv.stable(tables, cx),
107            c_variadic: self.c_variadic,
108        }
109    }
110}
111
112impl<'tcx> Stable<'tcx> for callconv::ArgAbi<'tcx, ty::Ty<'tcx>> {
113    type T = ArgAbi;
114
115    fn stable<'cx>(
116        &self,
117        tables: &mut Tables<'cx, BridgeTys>,
118        cx: &CompilerCtxt<'cx, BridgeTys>,
119    ) -> Self::T {
120        ArgAbi {
121            ty: self.layout.ty.stable(tables, cx),
122            layout: self.layout.layout.stable(tables, cx),
123            mode: self.mode.stable(tables, cx),
124        }
125    }
126}
127
128impl<'tcx> Stable<'tcx> for CanonAbi {
129    type T = CallConvention;
130
131    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
132        match self {
133            CanonAbi::C => CallConvention::C,
134            CanonAbi::Rust => CallConvention::Rust,
135            CanonAbi::RustCold => CallConvention::Cold,
136            CanonAbi::RustPreserveNone => CallConvention::PreserveNone,
137            CanonAbi::RustTail => CallConvention::Tail,
138            CanonAbi::Custom => CallConvention::Custom,
139            CanonAbi::Swift => CallConvention::Swift,
140            CanonAbi::Arm(arm_call) => match arm_call {
141                ArmCall::Aapcs => CallConvention::ArmAapcs,
142                ArmCall::CCmseNonSecureCall => CallConvention::CCmseNonSecureCall,
143                ArmCall::CCmseNonSecureEntry => CallConvention::CCmseNonSecureEntry,
144            },
145            CanonAbi::GpuKernel => CallConvention::GpuKernel,
146            CanonAbi::Interrupt(interrupt_kind) => match interrupt_kind {
147                InterruptKind::Avr => CallConvention::AvrInterrupt,
148                InterruptKind::AvrNonBlocking => CallConvention::AvrNonBlockingInterrupt,
149                InterruptKind::Msp430 => CallConvention::Msp430Intr,
150                InterruptKind::RiscvMachine | InterruptKind::RiscvSupervisor => {
151                    CallConvention::RiscvInterrupt
152                }
153                InterruptKind::X86 => CallConvention::X86Intr,
154            },
155            CanonAbi::X86(x86_call) => match x86_call {
156                X86Call::Fastcall => CallConvention::X86Fastcall,
157                X86Call::Stdcall => CallConvention::X86Stdcall,
158                X86Call::SysV64 => CallConvention::X86_64SysV,
159                X86Call::Thiscall => CallConvention::X86ThisCall,
160                X86Call::Vectorcall => CallConvention::X86VectorCall,
161                X86Call::Win64 => CallConvention::X86_64Win64,
162            },
163        }
164    }
165}
166
167impl<'tcx> Stable<'tcx> for callconv::IndirectMode {
168    type T = IndirectMode;
169
170    fn stable<'cx>(
171        &self,
172        _tables: &mut Tables<'cx, BridgeTys>,
173        _cx: &CompilerCtxt<'cx, BridgeTys>,
174    ) -> Self::T {
175        match self {
176            callconv::IndirectMode::Pointer => IndirectMode::Pointer,
177            callconv::IndirectMode::OnStack => IndirectMode::OnStack,
178            callconv::IndirectMode::AmdgpuKernelArg => IndirectMode::AmdgpuKernelArg,
179        }
180    }
181}
182
183impl<'tcx> Stable<'tcx> for callconv::PassMode {
184    type T = PassMode;
185
186    fn stable<'cx>(
187        &self,
188        tables: &mut Tables<'cx, BridgeTys>,
189        cx: &CompilerCtxt<'cx, BridgeTys>,
190    ) -> Self::T {
191        match self {
192            callconv::PassMode::Ignore => PassMode::Ignore,
193            callconv::PassMode::Direct(attr) => PassMode::Direct(attr.stable(tables, cx)),
194            callconv::PassMode::Pair(first, second) => {
195                PassMode::Pair(first.stable(tables, cx), second.stable(tables, cx))
196            }
197            callconv::PassMode::Cast { pad_i32_count, cast } => {
198                PassMode::Cast { pad_i32_count: *pad_i32_count, cast: cast.stable(tables, cx) }
199            }
200            callconv::PassMode::Indirect { attrs, address_space, mode } => PassMode::Indirect {
201                attrs: attrs.stable(tables, cx),
202                address_space: address_space.stable(tables, cx),
203                mode: mode.stable(tables, cx),
204            },
205            callconv::PassMode::IndirectUnsized { attrs, meta_attrs } => {
206                PassMode::IndirectUnsized {
207                    attrs: attrs.stable(tables, cx),
208                    meta_attrs: meta_attrs.stable(tables, cx),
209                }
210            }
211        }
212    }
213}
214
215impl<'tcx> Stable<'tcx> for callconv::CastTarget {
216    type T = CastTarget;
217
218    fn stable<'cx>(
219        &self,
220        tables: &mut Tables<'cx, BridgeTys>,
221        cx: &CompilerCtxt<'cx, BridgeTys>,
222    ) -> Self::T {
223        CastTarget {
224            prefix: self.prefix.iter().map(|reg| reg.stable(tables, cx)).collect(),
225            rest_offset: self.rest_offset.map(|offset| Size::from_bits(offset.bits_usize())),
226            rest: self.rest.stable(tables, cx),
227        }
228    }
229}
230
231impl<'tcx> Stable<'tcx> for callconv::Uniform {
232    type T = Uniform;
233
234    fn stable<'cx>(
235        &self,
236        tables: &mut Tables<'cx, BridgeTys>,
237        cx: &CompilerCtxt<'cx, BridgeTys>,
238    ) -> Self::T {
239        Uniform {
240            unit: self.unit.stable(tables, cx),
241            total: Size::from_bits(self.total.bits_usize()),
242            is_consecutive: self.is_consecutive,
243        }
244    }
245}
246
247impl<'tcx> Stable<'tcx> for rustc_abi::Reg {
248    type T = Reg;
249
250    fn stable<'cx>(
251        &self,
252        _: &mut Tables<'cx, BridgeTys>,
253        _: &CompilerCtxt<'cx, BridgeTys>,
254    ) -> Self::T {
255        Reg {
256            kind: match self.kind {
257                rustc_abi::RegKind::Integer => RegKind::Integer,
258                rustc_abi::RegKind::Float => RegKind::Float,
259                rustc_abi::RegKind::PpcF128 => RegKind::Float,
260                rustc_abi::RegKind::Vector { .. } => RegKind::Vector,
261            },
262            size: Size::from_bits(self.size.bits_usize()),
263        }
264    }
265}
266
267impl<'tcx> Stable<'tcx> for callconv::ArgAttributes {
268    type T = ArgAttributes;
269
270    fn stable<'cx>(
271        &self,
272        _: &mut Tables<'cx, BridgeTys>,
273        _: &CompilerCtxt<'cx, BridgeTys>,
274    ) -> Self::T {
275        ArgAttributes {
276            regular: ArgAttributeFlags {
277                captures_none: self.regular.contains(callconv::ArgAttribute::CapturesNone),
278                captures_address: self.regular.contains(callconv::ArgAttribute::CapturesAddress),
279                captures_read_only: self.regular.contains(callconv::ArgAttribute::CapturesReadOnly),
280                no_alias: self.regular.contains(callconv::ArgAttribute::NoAlias),
281                non_null: self.regular.contains(callconv::ArgAttribute::NonNull),
282                read_only: self.regular.contains(callconv::ArgAttribute::ReadOnly),
283                in_reg: self.regular.contains(callconv::ArgAttribute::InReg),
284                no_undef: self.regular.contains(callconv::ArgAttribute::NoUndef),
285                writable: self.regular.contains(callconv::ArgAttribute::Writable),
286                no_free: self.regular.contains(callconv::ArgAttribute::NoFree),
287            },
288            arg_ext: match self.arg_ext {
289                callconv::ArgExtension::None => ArgExtension::None,
290                callconv::ArgExtension::Zext => ArgExtension::Zext,
291                callconv::ArgExtension::Sext => ArgExtension::Sext,
292            },
293            pointee_size: Size::from_bits(self.pointee_size.bits_usize()),
294            pointee_align: self.pointee_align.map(|a| a.bytes()),
295        }
296    }
297}
298
299impl<'tcx> Stable<'tcx> for rustc_abi::FieldsShape<rustc_abi::FieldIdx> {
300    type T = FieldsShape;
301
302    fn stable<'cx>(
303        &self,
304        tables: &mut Tables<'cx, BridgeTys>,
305        cx: &CompilerCtxt<'cx, BridgeTys>,
306    ) -> Self::T {
307        match self {
308            rustc_abi::FieldsShape::Primitive => FieldsShape::Primitive,
309            rustc_abi::FieldsShape::Union(count) => FieldsShape::Union(*count),
310            rustc_abi::FieldsShape::Array { stride, count } => {
311                FieldsShape::Array { stride: stride.stable(tables, cx), count: *count }
312            }
313            rustc_abi::FieldsShape::Arbitrary { offsets, .. } => {
314                FieldsShape::Arbitrary { offsets: offsets.iter().as_slice().stable(tables, cx) }
315            }
316        }
317    }
318}
319
320impl<'tcx> Stable<'tcx> for rustc_abi::Variants<rustc_abi::FieldIdx, rustc_abi::VariantIdx> {
321    type T = VariantsShape;
322
323    fn stable<'cx>(
324        &self,
325        tables: &mut Tables<'cx, BridgeTys>,
326        cx: &CompilerCtxt<'cx, BridgeTys>,
327    ) -> Self::T {
328        match self {
329            rustc_abi::Variants::Single { index } => {
330                VariantsShape::Single { index: index.stable(tables, cx) }
331            }
332            rustc_abi::Variants::Empty => VariantsShape::Empty,
333            rustc_abi::Variants::Multiple { tag, tag_encoding, tag_field, variants } => {
334                VariantsShape::Multiple {
335                    tag: tag.stable(tables, cx),
336                    tag_encoding: tag_encoding.stable(tables, cx),
337                    tag_field: tag_field.stable(tables, cx),
338                    variants: variants
339                        .iter()
340                        .map(|v| VariantFields {
341                            offsets: v.field_offsets.iter().as_slice().stable(tables, cx),
342                        })
343                        .collect(),
344                }
345            }
346        }
347    }
348}
349
350impl<'tcx> Stable<'tcx> for rustc_abi::TagEncoding<rustc_abi::VariantIdx> {
351    type T = TagEncoding;
352
353    fn stable<'cx>(
354        &self,
355        tables: &mut Tables<'cx, BridgeTys>,
356        cx: &CompilerCtxt<'cx, BridgeTys>,
357    ) -> Self::T {
358        match self {
359            rustc_abi::TagEncoding::Direct => TagEncoding::Direct,
360            rustc_abi::TagEncoding::Niche { untagged_variant, niche_variants, niche_start } => {
361                TagEncoding::Niche {
362                    untagged_variant: untagged_variant.stable(tables, cx),
363                    niche_variants: niche_variants.stable(tables, cx),
364                    niche_start: *niche_start,
365                }
366            }
367        }
368    }
369}
370
371impl<'tcx> Stable<'tcx> for rustc_abi::NumScalableVectors {
372    type T = NumScalableVectors;
373
374    fn stable<'cx>(
375        &self,
376        _tables: &mut Tables<'cx, BridgeTys>,
377        _cx: &CompilerCtxt<'cx, BridgeTys>,
378    ) -> Self::T {
379        NumScalableVectors(self.0)
380    }
381}
382
383impl<'tcx> Stable<'tcx> for rustc_abi::BackendLaneCount {
384    type T = u64;
385
386    fn stable<'cx>(
387        &self,
388        _tables: &mut Tables<'cx, BridgeTys>,
389        _cx: &CompilerCtxt<'cx, BridgeTys>,
390    ) -> Self::T {
391        self.as_u64()
392    }
393}
394
395impl<'tcx> Stable<'tcx> for rustc_abi::BackendRepr {
396    type T = ValueRepr;
397
398    fn stable<'cx>(
399        &self,
400        tables: &mut Tables<'cx, BridgeTys>,
401        cx: &CompilerCtxt<'cx, BridgeTys>,
402    ) -> Self::T {
403        match *self {
404            rustc_abi::BackendRepr::Scalar(scalar) => ValueRepr::Scalar(scalar.stable(tables, cx)),
405            rustc_abi::BackendRepr::ScalarPair { a: first, b: second, b_offset: second_offset } => {
406                ValueRepr::ScalarPair {
407                    a: first.stable(tables, cx),
408                    b: second.stable(tables, cx),
409                    b_offset: second_offset.stable(tables, cx),
410                }
411            }
412            rustc_abi::BackendRepr::SimdVector { element, count } => ValueRepr::Vector {
413                element: element.stable(tables, cx),
414                count: count.stable(tables, cx),
415            },
416            rustc_abi::BackendRepr::SimdScalableVector { element, count, number_of_vectors } => {
417                ValueRepr::ScalableVector {
418                    element: element.stable(tables, cx),
419                    count: count.stable(tables, cx),
420                    number_of_vectors: number_of_vectors.stable(tables, cx),
421                }
422            }
423            rustc_abi::BackendRepr::Memory { sized } => ValueRepr::Aggregate { sized },
424        }
425    }
426}
427
428impl<'tcx> Stable<'tcx> for rustc_abi::Size {
429    type T = Size;
430
431    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
432        Size::from_bits(self.bits_usize())
433    }
434}
435
436impl<'tcx> Stable<'tcx> for rustc_abi::Align {
437    type T = Align;
438
439    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
440        self.bytes()
441    }
442}
443
444impl<'tcx> Stable<'tcx> for rustc_abi::Scalar {
445    type T = Scalar;
446
447    fn stable<'cx>(
448        &self,
449        tables: &mut Tables<'cx, BridgeTys>,
450        cx: &CompilerCtxt<'cx, BridgeTys>,
451    ) -> Self::T {
452        match self {
453            rustc_abi::Scalar::Initialized { value, valid_range } => Scalar::Initialized {
454                value: value.stable(tables, cx),
455                valid_range: valid_range.stable(tables, cx),
456            },
457            rustc_abi::Scalar::Union { value } => Scalar::Union { value: value.stable(tables, cx) },
458        }
459    }
460}
461
462impl<'tcx> Stable<'tcx> for rustc_abi::Primitive {
463    type T = Primitive;
464
465    fn stable<'cx>(
466        &self,
467        tables: &mut Tables<'cx, BridgeTys>,
468        cx: &CompilerCtxt<'cx, BridgeTys>,
469    ) -> Self::T {
470        match self {
471            rustc_abi::Primitive::Int(length, signed) => {
472                Primitive::Int { length: length.stable(tables, cx), signed: *signed }
473            }
474            rustc_abi::Primitive::Float(length) => {
475                Primitive::Float { length: length.stable(tables, cx) }
476            }
477            rustc_abi::Primitive::Pointer(space) => Primitive::Pointer(space.stable(tables, cx)),
478        }
479    }
480}
481
482impl<'tcx> Stable<'tcx> for rustc_abi::AddressSpace {
483    type T = AddressSpace;
484
485    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
486        AddressSpace(self.0)
487    }
488}
489
490impl<'tcx> Stable<'tcx> for rustc_abi::Integer {
491    type T = IntegerLength;
492
493    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
494        match self {
495            rustc_abi::Integer::I8 => IntegerLength::I8,
496            rustc_abi::Integer::I16 => IntegerLength::I16,
497            rustc_abi::Integer::I32 => IntegerLength::I32,
498            rustc_abi::Integer::I64 => IntegerLength::I64,
499            rustc_abi::Integer::I128 => IntegerLength::I128,
500        }
501    }
502}
503
504impl<'tcx> Stable<'tcx> for rustc_abi::Float {
505    type T = FloatLength;
506
507    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
508        match self {
509            rustc_abi::Float::F16 => FloatLength::F16,
510            rustc_abi::Float::F16B => FloatLength::F16B,
511            rustc_abi::Float::F32 => FloatLength::F32,
512            rustc_abi::Float::F64 => FloatLength::F64,
513            rustc_abi::Float::X87F80 => FloatLength::X87F80,
514            rustc_abi::Float::F128 => FloatLength::F128,
515            rustc_abi::Float::PpcF128 => FloatLength::PpcF128,
516        }
517    }
518}
519
520impl<'tcx> Stable<'tcx> for rustc_abi::WrappingRange {
521    type T = WrappingRange;
522
523    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
524        WrappingRange { start: self.start, end: self.end }
525    }
526}
527
528impl<'tcx> Stable<'tcx> for rustc_abi::ReprFlags {
529    type T = ReprFlags;
530
531    fn stable<'cx>(
532        &self,
533        _tables: &mut Tables<'cx, BridgeTys>,
534        _cx: &CompilerCtxt<'cx, BridgeTys>,
535    ) -> Self::T {
536        ReprFlags {
537            is_simd: self.intersects(Self::IS_SIMD),
538            is_c: self.intersects(Self::IS_C),
539            is_transparent: self.intersects(Self::IS_TRANSPARENT),
540            is_linear: self.intersects(Self::IS_LINEAR),
541        }
542    }
543}
544
545impl<'tcx> Stable<'tcx> for rustc_abi::IntegerType {
546    type T = IntegerType;
547
548    fn stable<'cx>(
549        &self,
550        tables: &mut Tables<'cx, BridgeTys>,
551        cx: &CompilerCtxt<'cx, BridgeTys>,
552    ) -> Self::T {
553        match self {
554            rustc_abi::IntegerType::Pointer(signed) => IntegerType::Pointer { is_signed: *signed },
555            rustc_abi::IntegerType::Fixed(integer, signed) => {
556                IntegerType::Fixed { length: integer.stable(tables, cx), is_signed: *signed }
557            }
558        }
559    }
560}
561
562impl<'tcx> Stable<'tcx> for rustc_abi::ReprOptions {
563    type T = ReprOptions;
564
565    fn stable<'cx>(
566        &self,
567        tables: &mut Tables<'cx, BridgeTys>,
568        cx: &CompilerCtxt<'cx, BridgeTys>,
569    ) -> Self::T {
570        ReprOptions {
571            int: self.int.map(|int| int.stable(tables, cx)),
572            align: self.align.map(|align| align.stable(tables, cx)),
573            pack: self.pack.map(|pack| pack.stable(tables, cx)),
574            flags: self.flags.stable(tables, cx),
575        }
576    }
577}