]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/mir/constant.rs
Auto merge of #105812 - ojeda:no-jump-tables, r=nikic
[rust.git] / compiler / rustc_codegen_ssa / src / mir / constant.rs
1 use crate::mir::operand::OperandRef;
2 use crate::traits::*;
3 use rustc_middle::mir;
4 use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
5 use rustc_middle::ty::layout::HasTyCtxt;
6 use rustc_middle::ty::{self, Ty};
7 use rustc_span::source_map::Span;
8 use rustc_target::abi::Abi;
9
10 use super::FunctionCx;
11
12 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13     pub fn eval_mir_constant_to_operand(
14         &self,
15         bx: &mut Bx,
16         constant: &mir::Constant<'tcx>,
17     ) -> Result<OperandRef<'tcx, Bx::Value>, ErrorHandled> {
18         let val = self.eval_mir_constant(constant)?;
19         let ty = self.monomorphize(constant.ty());
20         Ok(OperandRef::from_const(bx, val, ty))
21     }
22
23     pub fn eval_mir_constant(
24         &self,
25         constant: &mir::Constant<'tcx>,
26     ) -> Result<ConstValue<'tcx>, ErrorHandled> {
27         let ct = self.monomorphize(constant.literal);
28         let uv = match ct {
29             mir::ConstantKind::Ty(ct) => match ct.kind() {
30                 ty::ConstKind::Unevaluated(uv) => uv.expand(),
31                 ty::ConstKind::Value(val) => {
32                     return Ok(self.cx.tcx().valtree_to_const_val((ct.ty(), val)));
33                 }
34                 err => span_bug!(
35                     constant.span,
36                     "encountered bad ConstKind after monomorphizing: {:?}",
37                     err
38                 ),
39             },
40             mir::ConstantKind::Unevaluated(uv, _) => uv,
41             mir::ConstantKind::Val(val, _) => return Ok(val),
42         };
43
44         self.cx.tcx().const_eval_resolve(ty::ParamEnv::reveal_all(), uv, None).map_err(|err| {
45             match err {
46                 ErrorHandled::Reported(_) => {
47                     self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered");
48                 }
49                 ErrorHandled::TooGeneric => {
50                     span_bug!(constant.span, "codegen encountered polymorphic constant: {:?}", err);
51                 }
52             }
53             err
54         })
55     }
56
57     /// process constant containing SIMD shuffle indices
58     pub fn simd_shuffle_indices(
59         &mut self,
60         bx: &Bx,
61         span: Span,
62         ty: Ty<'tcx>,
63         constant: Result<ConstValue<'tcx>, ErrorHandled>,
64     ) -> (Bx::Value, Ty<'tcx>) {
65         constant
66             .map(|val| {
67                 let field_ty = ty.builtin_index().unwrap();
68                 let c = mir::ConstantKind::from_value(val, ty);
69                 let values: Vec<_> = bx
70                     .tcx()
71                     .destructure_mir_constant(ty::ParamEnv::reveal_all(), c)
72                     .fields
73                     .iter()
74                     .map(|field| {
75                         if let Some(prim) = field.try_to_scalar() {
76                             let layout = bx.layout_of(field_ty);
77                             let Abi::Scalar(scalar) = layout.abi else {
78                                 bug!("from_const: invalid ByVal layout: {:#?}", layout);
79                             };
80                             bx.scalar_to_backend(prim, scalar, bx.immediate_backend_type(layout))
81                         } else {
82                             bug!("simd shuffle field {:?}", field)
83                         }
84                     })
85                     .collect();
86                 let llval = bx.const_struct(&values, false);
87                 (llval, c.ty())
88             })
89             .unwrap_or_else(|_| {
90                 bx.tcx().sess.span_err(span, "could not evaluate shuffle_indices at compile time");
91                 // We've errored, so we don't have to produce working code.
92                 let ty = self.monomorphize(ty);
93                 let llty = bx.backend_type(bx.layout_of(ty));
94                 (bx.const_undef(llty), ty)
95             })
96     }
97 }