2019-12-22 17:42:04 -05:00
|
|
|
use crate::mir::operand::OperandRef;
|
|
|
|
|
use crate::traits::*;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_index::vec::Idx;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir;
|
|
|
|
|
use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_middle::ty::layout::HasTyCtxt;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2020-01-01 19:25:28 +01:00
|
|
|
use rustc_span::source_map::Span;
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_target::abi::Abi;
|
2015-11-16 19:57:57 +02:00
|
|
|
|
2018-01-16 10:16:38 +01:00
|
|
|
use super::FunctionCx;
|
2016-05-27 14:40:05 +03:00
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
2019-11-11 12:53:31 +01:00
|
|
|
pub fn eval_mir_constant_to_operand(
|
|
|
|
|
&mut self,
|
|
|
|
|
bx: &mut Bx,
|
|
|
|
|
constant: &mir::Constant<'tcx>,
|
|
|
|
|
) -> Result<OperandRef<'tcx, Bx::Value>, ErrorHandled> {
|
|
|
|
|
match constant.literal.val {
|
2019-12-08 01:55:14 +01:00
|
|
|
// Special case unevaluated statics, because statics have an identity and thus should
|
|
|
|
|
// use `get_static` to get at their id.
|
|
|
|
|
// FIXME(oli-obk): can we unify this somehow, maybe by making const eval of statics
|
|
|
|
|
// always produce `&STATIC`. This may also simplify how const eval works with statics.
|
2020-01-08 21:31:08 +01:00
|
|
|
ty::ConstKind::Unevaluated(def_id, substs, None) if self.cx.tcx().is_static(def_id) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
assert!(substs.is_empty(), "we don't support generic statics yet");
|
|
|
|
|
let static_ = bx.get_static(def_id);
|
|
|
|
|
// we treat operands referring to statics as if they were `&STATIC` instead
|
|
|
|
|
let ptr_ty = self.cx.tcx().mk_mut_ptr(self.monomorphize(&constant.literal.ty));
|
|
|
|
|
let layout = bx.layout_of(ptr_ty);
|
|
|
|
|
Ok(OperandRef::from_immediate_or_packed_pair(bx, static_, layout))
|
|
|
|
|
}
|
2019-11-11 12:53:31 +01:00
|
|
|
_ => {
|
|
|
|
|
let val = self.eval_mir_constant(constant)?;
|
2020-02-15 17:43:14 +13:00
|
|
|
let ty = self.monomorphize(&constant.literal.ty);
|
2020-02-24 14:48:40 +01:00
|
|
|
Ok(OperandRef::from_const(bx, val, ty))
|
2019-11-11 12:53:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 10:19:31 +01:00
|
|
|
pub fn eval_mir_constant(
|
2018-01-16 09:31:48 +01:00
|
|
|
&mut self,
|
2019-03-14 10:19:31 +01:00
|
|
|
constant: &mir::Constant<'tcx>,
|
2020-02-15 12:57:46 +13:00
|
|
|
) -> Result<ConstValue<'tcx>, ErrorHandled> {
|
2020-03-22 11:53:13 +01:00
|
|
|
match self.monomorphize(&constant.literal).val {
|
2020-03-22 11:34:42 +01:00
|
|
|
ty::ConstKind::Unevaluated(def_id, substs, promoted) => self
|
|
|
|
|
.cx
|
|
|
|
|
.tcx()
|
|
|
|
|
.const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None)
|
|
|
|
|
.map_err(|err| {
|
|
|
|
|
if promoted.is_none() {
|
|
|
|
|
self.cx
|
|
|
|
|
.tcx()
|
|
|
|
|
.sess
|
|
|
|
|
.span_err(constant.span, "erroneous constant encountered");
|
|
|
|
|
}
|
|
|
|
|
err
|
|
|
|
|
}),
|
2020-02-15 12:57:46 +13:00
|
|
|
ty::ConstKind::Value(value) => Ok(value),
|
2020-03-22 11:53:13 +01:00
|
|
|
err => span_bug!(
|
2020-03-22 11:34:42 +01:00
|
|
|
constant.span,
|
|
|
|
|
"encountered bad ConstKind after monomorphizing: {:?}",
|
2020-03-22 11:53:13 +01:00
|
|
|
err
|
2020-03-22 11:34:42 +01:00
|
|
|
),
|
2017-01-02 11:00:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 13:45:41 +01:00
|
|
|
/// process constant containing SIMD shuffle indices
|
|
|
|
|
pub fn simd_shuffle_indices(
|
|
|
|
|
&mut self,
|
2018-09-20 15:47:22 +02:00
|
|
|
bx: &Bx,
|
2018-07-22 01:01:07 +02:00
|
|
|
span: Span,
|
|
|
|
|
ty: Ty<'tcx>,
|
2020-02-15 12:57:46 +13:00
|
|
|
constant: Result<ConstValue<'tcx>, ErrorHandled>,
|
2018-09-20 15:47:22 +02:00
|
|
|
) -> (Bx::Value, Ty<'tcx>) {
|
2018-07-22 01:01:07 +02:00
|
|
|
constant
|
2020-02-15 12:57:46 +13:00
|
|
|
.map(|val| {
|
|
|
|
|
let field_ty = ty.builtin_index().unwrap();
|
|
|
|
|
let fields = match ty.kind {
|
2019-03-26 00:13:09 +01:00
|
|
|
ty::Array(_, n) => n.eval_usize(bx.tcx(), ty::ParamEnv::reveal_all()),
|
2020-02-15 12:57:46 +13:00
|
|
|
_ => bug!("invalid simd shuffle type: {}", ty),
|
2018-01-16 09:31:48 +01:00
|
|
|
};
|
2020-02-16 09:59:01 +13:00
|
|
|
let c = ty::Const::from_value(bx.tcx(), val, ty);
|
2019-12-22 17:42:04 -05:00
|
|
|
let values: Vec<_> = (0..fields)
|
|
|
|
|
.map(|field| {
|
|
|
|
|
let field = bx.tcx().const_field(
|
|
|
|
|
ty::ParamEnv::reveal_all().and((&c, mir::Field::new(field as usize))),
|
|
|
|
|
);
|
2020-02-15 12:57:46 +13:00
|
|
|
if let Some(prim) = field.try_to_scalar() {
|
2019-12-22 17:42:04 -05:00
|
|
|
let layout = bx.layout_of(field_ty);
|
|
|
|
|
let scalar = match layout.abi {
|
2020-03-31 18:16:47 +02:00
|
|
|
Abi::Scalar(ref x) => x,
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => bug!("from_const: invalid ByVal layout: {:#?}", layout),
|
|
|
|
|
};
|
|
|
|
|
bx.scalar_to_backend(prim, scalar, bx.immediate_backend_type(layout))
|
|
|
|
|
} else {
|
|
|
|
|
bug!("simd shuffle field {:?}", field)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2019-02-16 20:51:43 +01:00
|
|
|
let llval = bx.const_struct(&values, false);
|
|
|
|
|
(llval, c.ty)
|
2018-01-16 09:31:48 +01:00
|
|
|
})
|
2018-08-26 15:19:34 +02:00
|
|
|
.unwrap_or_else(|_| {
|
2019-12-22 17:42:04 -05:00
|
|
|
bx.tcx().sess.span_err(span, "could not evaluate shuffle_indices at compile time");
|
2018-01-16 09:31:48 +01:00
|
|
|
// We've errored, so we don't have to produce working code.
|
2018-07-22 01:01:07 +02:00
|
|
|
let ty = self.monomorphize(&ty);
|
2018-11-27 19:00:25 +01:00
|
|
|
let llty = bx.backend_type(bx.layout_of(ty));
|
|
|
|
|
(bx.const_undef(llty), ty)
|
2018-01-16 09:31:48 +01:00
|
|
|
})
|
2017-01-02 11:00:42 -07:00
|
|
|
}
|
|
|
|
|
}
|