]> git.proxmox.com Git - rustc.git/blob - src/librustc_codegen_ssa/mir/constant.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_codegen_ssa / mir / constant.rs
1 use crate::mir::operand::OperandRef;
2 use crate::traits::*;
3 use rustc_index::vec::Idx;
4 use rustc_middle::mir;
5 use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
6 use rustc_middle::ty::layout::HasTyCtxt;
7 use rustc_middle::ty::{self, Ty};
8 use rustc_span::source_map::Span;
9 use rustc_target::abi::Abi;
10
11 use super::FunctionCx;
12
13 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14 pub fn eval_mir_constant_to_operand(
15 &mut self,
16 bx: &mut Bx,
17 constant: &mir::Constant<'tcx>,
18 ) -> Result<OperandRef<'tcx, Bx::Value>, ErrorHandled> {
19 match constant.literal.val {
20 // Special case unevaluated statics, because statics have an identity and thus should
21 // use `get_static` to get at their id.
22 // FIXME(oli-obk): can we unify this somehow, maybe by making const eval of statics
23 // always produce `&STATIC`. This may also simplify how const eval works with statics.
24 ty::ConstKind::Unevaluated(def_id, substs, None) if self.cx.tcx().is_static(def_id) => {
25 assert!(substs.is_empty(), "we don't support generic statics yet");
26 let static_ = bx.get_static(def_id);
27 // we treat operands referring to statics as if they were `&STATIC` instead
28 let ptr_ty = self.cx.tcx().mk_mut_ptr(self.monomorphize(&constant.literal.ty));
29 let layout = bx.layout_of(ptr_ty);
30 Ok(OperandRef::from_immediate_or_packed_pair(bx, static_, layout))
31 }
32 _ => {
33 let val = self.eval_mir_constant(constant)?;
34 let ty = self.monomorphize(&constant.literal.ty);
35 Ok(OperandRef::from_const(bx, val, ty))
36 }
37 }
38 }
39
40 pub fn eval_mir_constant(
41 &mut self,
42 constant: &mir::Constant<'tcx>,
43 ) -> Result<ConstValue<'tcx>, ErrorHandled> {
44 match self.monomorphize(&constant.literal).val {
45 ty::ConstKind::Unevaluated(def_id, substs, promoted) => self
46 .cx
47 .tcx()
48 .const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None)
49 .map_err(|err| {
50 if promoted.is_none() {
51 self.cx
52 .tcx()
53 .sess
54 .span_err(constant.span, "erroneous constant encountered");
55 }
56 err
57 }),
58 ty::ConstKind::Value(value) => Ok(value),
59 err => span_bug!(
60 constant.span,
61 "encountered bad ConstKind after monomorphizing: {:?}",
62 err
63 ),
64 }
65 }
66
67 /// process constant containing SIMD shuffle indices
68 pub fn simd_shuffle_indices(
69 &mut self,
70 bx: &Bx,
71 span: Span,
72 ty: Ty<'tcx>,
73 constant: Result<ConstValue<'tcx>, ErrorHandled>,
74 ) -> (Bx::Value, Ty<'tcx>) {
75 constant
76 .map(|val| {
77 let field_ty = ty.builtin_index().unwrap();
78 let fields = match ty.kind {
79 ty::Array(_, n) => n.eval_usize(bx.tcx(), ty::ParamEnv::reveal_all()),
80 _ => bug!("invalid simd shuffle type: {}", ty),
81 };
82 let c = ty::Const::from_value(bx.tcx(), val, ty);
83 let values: Vec<_> = (0..fields)
84 .map(|field| {
85 let field = bx.tcx().const_field(
86 ty::ParamEnv::reveal_all().and((&c, mir::Field::new(field as usize))),
87 );
88 if let Some(prim) = field.try_to_scalar() {
89 let layout = bx.layout_of(field_ty);
90 let scalar = match layout.abi {
91 Abi::Scalar(ref x) => x,
92 _ => bug!("from_const: invalid ByVal layout: {:#?}", layout),
93 };
94 bx.scalar_to_backend(prim, scalar, bx.immediate_backend_type(layout))
95 } else {
96 bug!("simd shuffle field {:?}", field)
97 }
98 })
99 .collect();
100 let llval = bx.const_struct(&values, false);
101 (llval, c.ty)
102 })
103 .unwrap_or_else(|_| {
104 bx.tcx().sess.span_err(span, "could not evaluate shuffle_indices at compile time");
105 // We've errored, so we don't have to produce working code.
106 let ty = self.monomorphize(&ty);
107 let llty = bx.backend_type(bx.layout_of(ty));
108 (bx.const_undef(llty), ty)
109 })
110 }
111 }