]> git.lizzy.rs Git - rust.git/blob - src/discriminant.rs
Rollup merge of #81618 - bjorn3:sync_cg_clif-2021-02-01, r=bjorn3
[rust.git] / src / discriminant.rs
1 //! Handling of enum discriminants
2 //!
3 //! Adapted from <https://github.com/rust-lang/rust/blob/d760df5aea483aae041c9a241e7acacf48f75035/src/librustc_codegen_ssa/mir/place.rs>
4
5 use rustc_target::abi::{Int, TagEncoding, Variants};
6
7 use crate::prelude::*;
8
9 pub(crate) fn codegen_set_discriminant<'tcx>(
10     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
11     place: CPlace<'tcx>,
12     variant_index: VariantIdx,
13 ) {
14     let layout = place.layout();
15     if layout.for_variant(fx, variant_index).abi.is_uninhabited() {
16         return;
17     }
18     match layout.variants {
19         Variants::Single { index } => {
20             assert_eq!(index, variant_index);
21         }
22         Variants::Multiple {
23             tag: _,
24             tag_field,
25             tag_encoding: TagEncoding::Direct,
26             variants: _,
27         } => {
28             let ptr = place.place_field(fx, mir::Field::new(tag_field));
29             let to = layout
30                 .ty
31                 .discriminant_for_variant(fx.tcx, variant_index)
32                 .unwrap()
33                 .val;
34             let to = if ptr.layout().abi.is_signed() {
35                 ty::ScalarInt::try_from_int(
36                     ptr.layout().size.sign_extend(to) as i128,
37                     ptr.layout().size,
38                 )
39                 .unwrap()
40             } else {
41                 ty::ScalarInt::try_from_uint(to, ptr.layout().size).unwrap()
42             };
43             let discr = CValue::const_val(fx, ptr.layout(), to);
44             ptr.write_cvalue(fx, discr);
45         }
46         Variants::Multiple {
47             tag: _,
48             tag_field,
49             tag_encoding:
50                 TagEncoding::Niche {
51                     dataful_variant,
52                     ref niche_variants,
53                     niche_start,
54                 },
55             variants: _,
56         } => {
57             if variant_index != dataful_variant {
58                 let niche = place.place_field(fx, mir::Field::new(tag_field));
59                 let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
60                 let niche_value = ty::ScalarInt::try_from_uint(
61                     u128::from(niche_value).wrapping_add(niche_start),
62                     niche.layout().size,
63                 )
64                 .unwrap();
65                 let niche_llval = CValue::const_val(fx, niche.layout(), niche_value);
66                 niche.write_cvalue(fx, niche_llval);
67             }
68         }
69     }
70 }
71
72 pub(crate) fn codegen_get_discriminant<'tcx>(
73     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
74     value: CValue<'tcx>,
75     dest_layout: TyAndLayout<'tcx>,
76 ) -> CValue<'tcx> {
77     let layout = value.layout();
78
79     if layout.abi == Abi::Uninhabited {
80         return trap_unreachable_ret_value(
81             fx,
82             dest_layout,
83             "[panic] Tried to get discriminant for uninhabited type.",
84         );
85     }
86
87     let (tag_scalar, tag_field, tag_encoding) = match &layout.variants {
88         Variants::Single { index } => {
89             let discr_val = layout
90                 .ty
91                 .discriminant_for_variant(fx.tcx, *index)
92                 .map_or(u128::from(index.as_u32()), |discr| discr.val);
93             let discr_val = if dest_layout.abi.is_signed() {
94                 ty::ScalarInt::try_from_int(
95                     dest_layout.size.sign_extend(discr_val) as i128,
96                     dest_layout.size,
97                 )
98                 .unwrap()
99             } else {
100                 ty::ScalarInt::try_from_uint(discr_val, dest_layout.size).unwrap()
101             };
102             return CValue::const_val(fx, dest_layout, discr_val);
103         }
104         Variants::Multiple {
105             tag,
106             tag_field,
107             tag_encoding,
108             variants: _,
109         } => (tag, *tag_field, tag_encoding),
110     };
111
112     let cast_to = fx.clif_type(dest_layout.ty).unwrap();
113
114     // Read the tag/niche-encoded discriminant from memory.
115     let tag = value.value_field(fx, mir::Field::new(tag_field));
116     let tag = tag.load_scalar(fx);
117
118     // Decode the discriminant (specifically if it's niche-encoded).
119     match *tag_encoding {
120         TagEncoding::Direct => {
121             let signed = match tag_scalar.value {
122                 Int(_, signed) => signed,
123                 _ => false,
124             };
125             let val = clif_intcast(fx, tag, cast_to, signed);
126             CValue::by_val(val, dest_layout)
127         }
128         TagEncoding::Niche {
129             dataful_variant,
130             ref niche_variants,
131             niche_start,
132         } => {
133             // Rebase from niche values to discriminants, and check
134             // whether the result is in range for the niche variants.
135
136             // We first compute the "relative discriminant" (wrt `niche_variants`),
137             // that is, if `n = niche_variants.end() - niche_variants.start()`,
138             // we remap `niche_start..=niche_start + n` (which may wrap around)
139             // to (non-wrap-around) `0..=n`, to be able to check whether the
140             // discriminant corresponds to a niche variant with one comparison.
141             // We also can't go directly to the (variant index) discriminant
142             // and check that it is in the range `niche_variants`, because
143             // that might not fit in the same type, on top of needing an extra
144             // comparison (see also the comment on `let niche_discr`).
145             let relative_discr = if niche_start == 0 {
146                 tag
147             } else {
148                 // FIXME handle niche_start > i64::MAX
149                 fx.bcx
150                     .ins()
151                     .iadd_imm(tag, -i64::try_from(niche_start).unwrap())
152             };
153             let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
154             let is_niche = {
155                 codegen_icmp_imm(
156                     fx,
157                     IntCC::UnsignedLessThanOrEqual,
158                     relative_discr,
159                     i128::from(relative_max),
160                 )
161             };
162
163             // NOTE(eddyb) this addition needs to be performed on the final
164             // type, in case the niche itself can't represent all variant
165             // indices (e.g. `u8` niche with more than `256` variants,
166             // but enough uninhabited variants so that the remaining variants
167             // fit in the niche).
168             // In other words, `niche_variants.end - niche_variants.start`
169             // is representable in the niche, but `niche_variants.end`
170             // might not be, in extreme cases.
171             let niche_discr = {
172                 let relative_discr = if relative_max == 0 {
173                     // HACK(eddyb) since we have only one niche, we know which
174                     // one it is, and we can avoid having a dynamic value here.
175                     fx.bcx.ins().iconst(cast_to, 0)
176                 } else {
177                     clif_intcast(fx, relative_discr, cast_to, false)
178                 };
179                 fx.bcx
180                     .ins()
181                     .iadd_imm(relative_discr, i64::from(niche_variants.start().as_u32()))
182             };
183
184             let dataful_variant = fx
185                 .bcx
186                 .ins()
187                 .iconst(cast_to, i64::from(dataful_variant.as_u32()));
188             let discr = fx.bcx.ins().select(is_niche, niche_discr, dataful_variant);
189             CValue::by_val(discr, dest_layout)
190         }
191     }
192 }