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::abi::{
12    AddressSpace, ArgAbi, CallConvention, FieldsShape, FloatLength, FnAbi, IntegerLength,
13    IntegerType, Layout, LayoutShape, NumScalableVectors, PassMode, Primitive, ReprFlags,
14    ReprOptions, Scalar, TagEncoding, TyAndLayout, ValueAbi, VariantFields, VariantsShape,
15    WrappingRange,
16};
17use crate::compiler_interface::BridgeTys;
18use crate::target::MachineSize as Size;
19use crate::ty::{Align, VariantIdx};
20use crate::unstable::Stable;
21use crate::{IndexedVal, opaque};
22
23impl<'tcx> Stable<'tcx> for rustc_abi::VariantIdx {
24    type T = VariantIdx;
25    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
26        VariantIdx::to_val(self.as_usize())
27    }
28}
29
30impl<'tcx> Stable<'tcx> for rustc_abi::Endian {
31    type T = crate::target::Endian;
32
33    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
34        match self {
35            rustc_abi::Endian::Little => crate::target::Endian::Little,
36            rustc_abi::Endian::Big => crate::target::Endian::Big,
37        }
38    }
39}
40
41impl<'tcx> Stable<'tcx> for rustc_abi::TyAndLayout<'tcx, ty::Ty<'tcx>> {
42    type T = TyAndLayout;
43
44    fn stable<'cx>(
45        &self,
46        tables: &mut Tables<'cx, BridgeTys>,
47        cx: &CompilerCtxt<'cx, BridgeTys>,
48    ) -> Self::T {
49        TyAndLayout { ty: self.ty.stable(tables, cx), layout: self.layout.stable(tables, cx) }
50    }
51}
52
53impl<'tcx> Stable<'tcx> for rustc_abi::Layout<'tcx> {
54    type T = Layout;
55
56    fn stable<'cx>(
57        &self,
58        tables: &mut Tables<'cx, BridgeTys>,
59        cx: &CompilerCtxt<'cx, BridgeTys>,
60    ) -> Self::T {
61        tables.layout_id(cx.lift(*self).unwrap())
62    }
63}
64
65impl<'tcx> Stable<'tcx> for rustc_abi::LayoutData<rustc_abi::FieldIdx, rustc_abi::VariantIdx> {
66    type T = LayoutShape;
67
68    fn stable<'cx>(
69        &self,
70        tables: &mut Tables<'cx, BridgeTys>,
71        cx: &CompilerCtxt<'cx, BridgeTys>,
72    ) -> Self::T {
73        LayoutShape {
74            fields: self.fields.stable(tables, cx),
75            variants: self.variants.stable(tables, cx),
76            abi: self.backend_repr.stable(tables, cx),
77            abi_align: self.align.abi.stable(tables, cx),
78            size: self.size.stable(tables, cx),
79        }
80    }
81}
82
83impl<'tcx> Stable<'tcx> for callconv::FnAbi<'tcx, ty::Ty<'tcx>> {
84    type T = FnAbi;
85
86    fn stable<'cx>(
87        &self,
88        tables: &mut Tables<'cx, BridgeTys>,
89        cx: &CompilerCtxt<'cx, BridgeTys>,
90    ) -> Self::T {
91        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);
92        if !(!self.c_variadic ||
            #[allow(non_exhaustive_omitted_patterns)] match self.conv {
                CanonAbi::C => true,
                _ => false,
            }) {
    ::core::panicking::panic("assertion failed: !self.c_variadic || matches!(self.conv, CanonAbi::C)")
};assert!(!self.c_variadic || matches!(self.conv, CanonAbi::C));
93        FnAbi {
94            args: self.args.as_ref().stable(tables, cx),
95            ret: self.ret.stable(tables, cx),
96            fixed_count: self.fixed_count,
97            conv: self.conv.stable(tables, cx),
98            c_variadic: self.c_variadic,
99        }
100    }
101}
102
103impl<'tcx> Stable<'tcx> for callconv::ArgAbi<'tcx, ty::Ty<'tcx>> {
104    type T = ArgAbi;
105
106    fn stable<'cx>(
107        &self,
108        tables: &mut Tables<'cx, BridgeTys>,
109        cx: &CompilerCtxt<'cx, BridgeTys>,
110    ) -> Self::T {
111        ArgAbi {
112            ty: self.layout.ty.stable(tables, cx),
113            layout: self.layout.layout.stable(tables, cx),
114            mode: self.mode.stable(tables, cx),
115        }
116    }
117}
118
119impl<'tcx> Stable<'tcx> for CanonAbi {
120    type T = CallConvention;
121
122    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
123        match self {
124            CanonAbi::C => CallConvention::C,
125            CanonAbi::Rust => CallConvention::Rust,
126            CanonAbi::RustCold => CallConvention::Cold,
127            CanonAbi::RustPreserveNone => CallConvention::PreserveNone,
128            CanonAbi::Custom => CallConvention::Custom,
129            CanonAbi::Arm(arm_call) => match arm_call {
130                ArmCall::Aapcs => CallConvention::ArmAapcs,
131                ArmCall::CCmseNonSecureCall => CallConvention::CCmseNonSecureCall,
132                ArmCall::CCmseNonSecureEntry => CallConvention::CCmseNonSecureEntry,
133            },
134            CanonAbi::GpuKernel => CallConvention::GpuKernel,
135            CanonAbi::Interrupt(interrupt_kind) => match interrupt_kind {
136                InterruptKind::Avr => CallConvention::AvrInterrupt,
137                InterruptKind::AvrNonBlocking => CallConvention::AvrNonBlockingInterrupt,
138                InterruptKind::Msp430 => CallConvention::Msp430Intr,
139                InterruptKind::RiscvMachine | InterruptKind::RiscvSupervisor => {
140                    CallConvention::RiscvInterrupt
141                }
142                InterruptKind::X86 => CallConvention::X86Intr,
143            },
144            CanonAbi::X86(x86_call) => match x86_call {
145                X86Call::Fastcall => CallConvention::X86Fastcall,
146                X86Call::Stdcall => CallConvention::X86Stdcall,
147                X86Call::SysV64 => CallConvention::X86_64SysV,
148                X86Call::Thiscall => CallConvention::X86ThisCall,
149                X86Call::Vectorcall => CallConvention::X86VectorCall,
150                X86Call::Win64 => CallConvention::X86_64Win64,
151            },
152        }
153    }
154}
155
156impl<'tcx> Stable<'tcx> for callconv::PassMode {
157    type T = PassMode;
158
159    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
160        match self {
161            callconv::PassMode::Ignore => PassMode::Ignore,
162            callconv::PassMode::Direct(attr) => PassMode::Direct(opaque(attr)),
163            callconv::PassMode::Pair(first, second) => {
164                PassMode::Pair(opaque(first), opaque(second))
165            }
166            callconv::PassMode::Cast { pad_i32, cast } => {
167                PassMode::Cast { pad_i32: *pad_i32, cast: opaque(cast) }
168            }
169            callconv::PassMode::Indirect { attrs, meta_attrs, on_stack } => PassMode::Indirect {
170                attrs: opaque(attrs),
171                meta_attrs: opaque(meta_attrs),
172                on_stack: *on_stack,
173            },
174        }
175    }
176}
177
178impl<'tcx> Stable<'tcx> for rustc_abi::FieldsShape<rustc_abi::FieldIdx> {
179    type T = FieldsShape;
180
181    fn stable<'cx>(
182        &self,
183        tables: &mut Tables<'cx, BridgeTys>,
184        cx: &CompilerCtxt<'cx, BridgeTys>,
185    ) -> Self::T {
186        match self {
187            rustc_abi::FieldsShape::Primitive => FieldsShape::Primitive,
188            rustc_abi::FieldsShape::Union(count) => FieldsShape::Union(*count),
189            rustc_abi::FieldsShape::Array { stride, count } => {
190                FieldsShape::Array { stride: stride.stable(tables, cx), count: *count }
191            }
192            rustc_abi::FieldsShape::Arbitrary { offsets, .. } => {
193                FieldsShape::Arbitrary { offsets: offsets.iter().as_slice().stable(tables, cx) }
194            }
195        }
196    }
197}
198
199impl<'tcx> Stable<'tcx> for rustc_abi::Variants<rustc_abi::FieldIdx, rustc_abi::VariantIdx> {
200    type T = VariantsShape;
201
202    fn stable<'cx>(
203        &self,
204        tables: &mut Tables<'cx, BridgeTys>,
205        cx: &CompilerCtxt<'cx, BridgeTys>,
206    ) -> Self::T {
207        match self {
208            rustc_abi::Variants::Single { index } => {
209                VariantsShape::Single { index: index.stable(tables, cx) }
210            }
211            rustc_abi::Variants::Empty => VariantsShape::Empty,
212            rustc_abi::Variants::Multiple { tag, tag_encoding, tag_field, variants } => {
213                VariantsShape::Multiple {
214                    tag: tag.stable(tables, cx),
215                    tag_encoding: tag_encoding.stable(tables, cx),
216                    tag_field: tag_field.stable(tables, cx),
217                    variants: variants
218                        .iter()
219                        .map(|v| match &v.fields {
220                            rustc_abi::FieldsShape::Arbitrary { offsets, .. } => VariantFields {
221                                offsets: offsets.iter().as_slice().stable(tables, cx),
222                            },
223                            _ => {
    ::core::panicking::panic_fmt(format_args!("variant layout should be Arbitrary"));
}panic!("variant layout should be Arbitrary"),
224                        })
225                        .collect(),
226                }
227            }
228        }
229    }
230}
231
232impl<'tcx> Stable<'tcx> for rustc_abi::TagEncoding<rustc_abi::VariantIdx> {
233    type T = TagEncoding;
234
235    fn stable<'cx>(
236        &self,
237        tables: &mut Tables<'cx, BridgeTys>,
238        cx: &CompilerCtxt<'cx, BridgeTys>,
239    ) -> Self::T {
240        match self {
241            rustc_abi::TagEncoding::Direct => TagEncoding::Direct,
242            rustc_abi::TagEncoding::Niche { untagged_variant, niche_variants, niche_start } => {
243                TagEncoding::Niche {
244                    untagged_variant: untagged_variant.stable(tables, cx),
245                    niche_variants: niche_variants.stable(tables, cx),
246                    niche_start: *niche_start,
247                }
248            }
249        }
250    }
251}
252
253impl<'tcx> Stable<'tcx> for rustc_abi::NumScalableVectors {
254    type T = NumScalableVectors;
255
256    fn stable<'cx>(
257        &self,
258        _tables: &mut Tables<'cx, BridgeTys>,
259        _cx: &CompilerCtxt<'cx, BridgeTys>,
260    ) -> Self::T {
261        NumScalableVectors(self.0)
262    }
263}
264
265impl<'tcx> Stable<'tcx> for rustc_abi::BackendRepr {
266    type T = ValueAbi;
267
268    fn stable<'cx>(
269        &self,
270        tables: &mut Tables<'cx, BridgeTys>,
271        cx: &CompilerCtxt<'cx, BridgeTys>,
272    ) -> Self::T {
273        match *self {
274            rustc_abi::BackendRepr::Scalar(scalar) => ValueAbi::Scalar(scalar.stable(tables, cx)),
275            rustc_abi::BackendRepr::ScalarPair(first, second) => {
276                ValueAbi::ScalarPair(first.stable(tables, cx), second.stable(tables, cx))
277            }
278            rustc_abi::BackendRepr::SimdVector { element, count } => {
279                ValueAbi::Vector { element: element.stable(tables, cx), count }
280            }
281            rustc_abi::BackendRepr::SimdScalableVector { element, count, number_of_vectors } => {
282                ValueAbi::ScalableVector {
283                    element: element.stable(tables, cx),
284                    count,
285                    number_of_vectors: number_of_vectors.stable(tables, cx),
286                }
287            }
288            rustc_abi::BackendRepr::Memory { sized } => ValueAbi::Aggregate { sized },
289        }
290    }
291}
292
293impl<'tcx> Stable<'tcx> for rustc_abi::Size {
294    type T = Size;
295
296    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
297        Size::from_bits(self.bits_usize())
298    }
299}
300
301impl<'tcx> Stable<'tcx> for rustc_abi::Align {
302    type T = Align;
303
304    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
305        self.bytes()
306    }
307}
308
309impl<'tcx> Stable<'tcx> for rustc_abi::Scalar {
310    type T = Scalar;
311
312    fn stable<'cx>(
313        &self,
314        tables: &mut Tables<'cx, BridgeTys>,
315        cx: &CompilerCtxt<'cx, BridgeTys>,
316    ) -> Self::T {
317        match self {
318            rustc_abi::Scalar::Initialized { value, valid_range } => Scalar::Initialized {
319                value: value.stable(tables, cx),
320                valid_range: valid_range.stable(tables, cx),
321            },
322            rustc_abi::Scalar::Union { value } => Scalar::Union { value: value.stable(tables, cx) },
323        }
324    }
325}
326
327impl<'tcx> Stable<'tcx> for rustc_abi::Primitive {
328    type T = Primitive;
329
330    fn stable<'cx>(
331        &self,
332        tables: &mut Tables<'cx, BridgeTys>,
333        cx: &CompilerCtxt<'cx, BridgeTys>,
334    ) -> Self::T {
335        match self {
336            rustc_abi::Primitive::Int(length, signed) => {
337                Primitive::Int { length: length.stable(tables, cx), signed: *signed }
338            }
339            rustc_abi::Primitive::Float(length) => {
340                Primitive::Float { length: length.stable(tables, cx) }
341            }
342            rustc_abi::Primitive::Pointer(space) => Primitive::Pointer(space.stable(tables, cx)),
343        }
344    }
345}
346
347impl<'tcx> Stable<'tcx> for rustc_abi::AddressSpace {
348    type T = AddressSpace;
349
350    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
351        AddressSpace(self.0)
352    }
353}
354
355impl<'tcx> Stable<'tcx> for rustc_abi::Integer {
356    type T = IntegerLength;
357
358    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
359        match self {
360            rustc_abi::Integer::I8 => IntegerLength::I8,
361            rustc_abi::Integer::I16 => IntegerLength::I16,
362            rustc_abi::Integer::I32 => IntegerLength::I32,
363            rustc_abi::Integer::I64 => IntegerLength::I64,
364            rustc_abi::Integer::I128 => IntegerLength::I128,
365        }
366    }
367}
368
369impl<'tcx> Stable<'tcx> for rustc_abi::Float {
370    type T = FloatLength;
371
372    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
373        match self {
374            rustc_abi::Float::F16 => FloatLength::F16,
375            rustc_abi::Float::F32 => FloatLength::F32,
376            rustc_abi::Float::F64 => FloatLength::F64,
377            rustc_abi::Float::F128 => FloatLength::F128,
378        }
379    }
380}
381
382impl<'tcx> Stable<'tcx> for rustc_abi::WrappingRange {
383    type T = WrappingRange;
384
385    fn stable(&self, _: &mut Tables<'_, BridgeTys>, _: &CompilerCtxt<'_, BridgeTys>) -> Self::T {
386        WrappingRange { start: self.start, end: self.end }
387    }
388}
389
390impl<'tcx> Stable<'tcx> for rustc_abi::ReprFlags {
391    type T = ReprFlags;
392
393    fn stable<'cx>(
394        &self,
395        _tables: &mut Tables<'cx, BridgeTys>,
396        _cx: &CompilerCtxt<'cx, BridgeTys>,
397    ) -> Self::T {
398        ReprFlags {
399            is_simd: self.intersects(Self::IS_SIMD),
400            is_c: self.intersects(Self::IS_C),
401            is_transparent: self.intersects(Self::IS_TRANSPARENT),
402            is_linear: self.intersects(Self::IS_LINEAR),
403        }
404    }
405}
406
407impl<'tcx> Stable<'tcx> for rustc_abi::IntegerType {
408    type T = IntegerType;
409
410    fn stable<'cx>(
411        &self,
412        tables: &mut Tables<'cx, BridgeTys>,
413        cx: &CompilerCtxt<'cx, BridgeTys>,
414    ) -> Self::T {
415        match self {
416            rustc_abi::IntegerType::Pointer(signed) => IntegerType::Pointer { is_signed: *signed },
417            rustc_abi::IntegerType::Fixed(integer, signed) => {
418                IntegerType::Fixed { length: integer.stable(tables, cx), is_signed: *signed }
419            }
420        }
421    }
422}
423
424impl<'tcx> Stable<'tcx> for rustc_abi::ReprOptions {
425    type T = ReprOptions;
426
427    fn stable<'cx>(
428        &self,
429        tables: &mut Tables<'cx, BridgeTys>,
430        cx: &CompilerCtxt<'cx, BridgeTys>,
431    ) -> Self::T {
432        ReprOptions {
433            int: self.int.map(|int| int.stable(tables, cx)),
434            align: self.align.map(|align| align.stable(tables, cx)),
435            pack: self.pack.map(|pack| pack.stable(tables, cx)),
436            flags: self.flags.stable(tables, cx),
437        }
438    }
439}