1 use crate::mir
::operand
::OperandRef
;
3 use rustc_index
::vec
::Idx
;
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
;
11 use super::FunctionCx
;
13 impl<'a
, 'tcx
, Bx
: BuilderMethods
<'a
, 'tcx
>> FunctionCx
<'a
, 'tcx
, Bx
> {
14 pub fn eval_mir_constant_to_operand(
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
))
33 let val
= self.eval_mir_constant(constant
)?
;
34 let ty
= self.monomorphize(&constant
.literal
.ty
);
35 Ok(OperandRef
::from_const(bx
, val
, ty
))
40 pub fn eval_mir_constant(
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
48 .const_eval_resolve(ty
::ParamEnv
::reveal_all(), def_id
, substs
, promoted
, None
)
50 if promoted
.is_none() {
54 .span_err(constant
.span
, "erroneous constant encountered");
58 ty
::ConstKind
::Value(value
) => Ok(value
),
61 "encountered bad ConstKind after monomorphizing: {:?}",
67 /// process constant containing SIMD shuffle indices
68 pub fn simd_shuffle_indices(
73 constant
: Result
<ConstValue
<'tcx
>, ErrorHandled
>,
74 ) -> (Bx
::Value
, Ty
<'tcx
>) {
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
),
82 let c
= ty
::Const
::from_value(bx
.tcx(), val
, ty
);
83 let values
: Vec
<_
> = (0..fields
)
85 let field
= bx
.tcx().const_field(
86 ty
::ParamEnv
::reveal_all().and((&c
, mir
::Field
::new(field
as usize))),
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
),
94 bx
.scalar_to_backend(prim
, scalar
, bx
.immediate_backend_type(layout
))
96 bug
!("simd shuffle field {:?}", field
)
100 let llval
= bx
.const_struct(&values
, false);
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
)