Files
rust/src/librustc_codegen_ssa/mir/constant.rs

104 lines
4.4 KiB
Rust
Raw Normal View History

2019-12-22 17:42:04 -05:00
use crate::mir::operand::OperandRef;
use crate::traits::*;
2016-09-19 23:50:00 +03:00
use rustc::mir;
2019-12-22 17:42:04 -05:00
use rustc::mir::interpret::ErrorHandled;
use rustc::ty::layout::{self, HasTyCtxt};
2019-12-22 17:42:04 -05:00
use rustc::ty::{self, Ty};
use rustc_index::vec::Idx;
2018-08-18 12:14:03 +02:00
use syntax::source_map::Span;
use super::FunctionCx;
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
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 {
// 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.
2019-12-22 17:42:04 -05:00
ty::ConstKind::Unevaluated(def_id, substs) if self.cx.tcx().is_static(def_id) => {
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))
}
_ => {
let val = self.eval_mir_constant(constant)?;
Ok(OperandRef::from_const(bx, val))
}
}
}
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>,
) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> {
2019-03-14 10:19:31 +01:00
match constant.literal.val {
2019-11-11 12:49:21 +01:00
ty::ConstKind::Unevaluated(def_id, substs) => {
let substs = self.monomorphize(&substs);
2019-12-22 17:42:04 -05:00
self.cx
.tcx()
.const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, None)
.map_err(|err| {
2019-12-22 17:42:04 -05:00
self.cx
.tcx()
.sess
.span_err(constant.span, "erroneous constant encountered");
err
})
2019-12-22 17:42:04 -05:00
}
_ => Ok(self.monomorphize(&constant.literal)),
2017-01-02 11:00:42 -07:00
}
}
/// process constant containing SIMD shuffle indices
pub fn simd_shuffle_indices(
&mut self,
bx: &Bx,
span: Span,
ty: Ty<'tcx>,
constant: Result<&'tcx ty::Const<'tcx>, ErrorHandled>,
) -> (Bx::Value, Ty<'tcx>) {
constant
.map(|c| {
2018-06-25 20:53:02 +02:00
let field_ty = c.ty.builtin_index().unwrap();
2019-09-16 19:08:35 +01:00
let fields = match c.ty.kind {
ty::Array(_, n) => n.eval_usize(bx.tcx(), ty::ParamEnv::reveal_all()),
_ => bug!("invalid simd shuffle type: {}", c.ty),
2018-01-16 09:31:48 +01:00
};
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))),
);
if let Some(prim) = field.val.try_to_scalar() {
let layout = bx.layout_of(field_ty);
let scalar = match layout.abi {
layout::Abi::Scalar(ref x) => x,
_ => 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();
let llval = bx.const_struct(&values, false);
(llval, c.ty)
2018-01-16 09:31:48 +01: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.
let ty = self.monomorphize(&ty);
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
}
}