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