1 //! Handling of enum discriminants
3 //! Adapted from <https://github.com/rust-lang/rust/blob/d760df5aea483aae041c9a241e7acacf48f75035/src/librustc_codegen_ssa/mir/place.rs>
5 use rustc_target::abi::{Int, TagEncoding, Variants};
9 pub(crate) fn codegen_set_discriminant<'tcx>(
10 fx: &mut FunctionCx<'_, '_, 'tcx>,
12 variant_index: VariantIdx,
14 let layout = place.layout();
15 if layout.for_variant(fx, variant_index).abi.is_uninhabited() {
18 match layout.variants {
19 Variants::Single { index } => {
20 assert_eq!(index, variant_index);
25 tag_encoding: TagEncoding::Direct,
28 let ptr = place.place_field(fx, mir::Field::new(tag_field));
29 let to = layout.ty.discriminant_for_variant(fx.tcx, variant_index).unwrap().val;
30 let to = if ptr.layout().abi.is_signed() {
31 ty::ScalarInt::try_from_int(
32 ptr.layout().size.sign_extend(to) as i128,
37 ty::ScalarInt::try_from_uint(to, ptr.layout().size).unwrap()
39 let discr = CValue::const_val(fx, ptr.layout(), to);
40 ptr.write_cvalue(fx, discr);
45 tag_encoding: TagEncoding::Niche { dataful_variant, ref niche_variants, niche_start },
48 if variant_index != dataful_variant {
49 let niche = place.place_field(fx, mir::Field::new(tag_field));
50 let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
51 let niche_value = ty::ScalarInt::try_from_uint(
52 u128::from(niche_value).wrapping_add(niche_start),
56 let niche_llval = CValue::const_val(fx, niche.layout(), niche_value);
57 niche.write_cvalue(fx, niche_llval);
63 pub(crate) fn codegen_get_discriminant<'tcx>(
64 fx: &mut FunctionCx<'_, '_, 'tcx>,
66 dest_layout: TyAndLayout<'tcx>,
68 let layout = value.layout();
70 if layout.abi == Abi::Uninhabited {
71 let true_ = fx.bcx.ins().iconst(types::I32, 1);
72 fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached);
73 // Return a dummy value
74 return CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout);
77 let (tag_scalar, tag_field, tag_encoding) = match &layout.variants {
78 Variants::Single { index } => {
79 let discr_val = layout
81 .discriminant_for_variant(fx.tcx, *index)
82 .map_or(u128::from(index.as_u32()), |discr| discr.val);
83 let discr_val = if dest_layout.abi.is_signed() {
84 ty::ScalarInt::try_from_int(
85 dest_layout.size.sign_extend(discr_val) as i128,
90 ty::ScalarInt::try_from_uint(discr_val, dest_layout.size).unwrap()
92 return CValue::const_val(fx, dest_layout, discr_val);
94 Variants::Multiple { tag, tag_field, tag_encoding, variants: _ } => {
95 (tag, *tag_field, tag_encoding)
99 let cast_to = fx.clif_type(dest_layout.ty).unwrap();
101 // Read the tag/niche-encoded discriminant from memory.
102 let tag = value.value_field(fx, mir::Field::new(tag_field));
103 let tag = tag.load_scalar(fx);
105 // Decode the discriminant (specifically if it's niche-encoded).
106 match *tag_encoding {
107 TagEncoding::Direct => {
108 let signed = match tag_scalar.primitive() {
109 Int(_, signed) => signed,
112 let val = clif_intcast(fx, tag, cast_to, signed);
113 CValue::by_val(val, dest_layout)
115 TagEncoding::Niche { dataful_variant, ref niche_variants, niche_start } => {
116 // Rebase from niche values to discriminants, and check
117 // whether the result is in range for the niche variants.
119 // We first compute the "relative discriminant" (wrt `niche_variants`),
120 // that is, if `n = niche_variants.end() - niche_variants.start()`,
121 // we remap `niche_start..=niche_start + n` (which may wrap around)
122 // to (non-wrap-around) `0..=n`, to be able to check whether the
123 // discriminant corresponds to a niche variant with one comparison.
124 // We also can't go directly to the (variant index) discriminant
125 // and check that it is in the range `niche_variants`, because
126 // that might not fit in the same type, on top of needing an extra
127 // comparison (see also the comment on `let niche_discr`).
128 let relative_discr = if niche_start == 0 {
131 // FIXME handle niche_start > i64::MAX
132 fx.bcx.ins().iadd_imm(tag, -i64::try_from(niche_start).unwrap())
134 let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
138 IntCC::UnsignedLessThanOrEqual,
140 i128::from(relative_max),
144 // NOTE(eddyb) this addition needs to be performed on the final
145 // type, in case the niche itself can't represent all variant
146 // indices (e.g. `u8` niche with more than `256` variants,
147 // but enough uninhabited variants so that the remaining variants
148 // fit in the niche).
149 // In other words, `niche_variants.end - niche_variants.start`
150 // is representable in the niche, but `niche_variants.end`
151 // might not be, in extreme cases.
153 let relative_discr = if relative_max == 0 {
154 // HACK(eddyb) since we have only one niche, we know which
155 // one it is, and we can avoid having a dynamic value here.
156 fx.bcx.ins().iconst(cast_to, 0)
158 clif_intcast(fx, relative_discr, cast_to, false)
160 fx.bcx.ins().iadd_imm(relative_discr, i64::from(niche_variants.start().as_u32()))
163 let dataful_variant = fx.bcx.ins().iconst(cast_to, i64::from(dataful_variant.as_u32()));
164 let discr = fx.bcx.ins().select(is_niche, niche_discr, dataful_variant);
165 CValue::by_val(discr, dest_layout)