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

95 lines
3.4 KiB
Rust
Raw Normal View History

use rustc::mir::interpret::ErrorHandled;
use rustc_mir::const_eval::const_field;
2016-09-19 23:50:00 +03:00
use rustc::mir;
2018-01-16 09:31:48 +01:00
use rustc_data_structures::indexed_vec::Idx;
use rustc::mir::interpret::GlobalId;
2018-01-25 16:44:45 +01:00
use rustc::ty::{self, Ty};
use rustc::ty::layout;
2018-08-18 12:14:03 +02:00
use syntax::source_map::Span;
use traits::*;
use super::FunctionCx;
impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2018-06-25 20:53:02 +02:00
fn fully_evaluate(
2018-01-16 09:31:48 +01:00
&mut self,
bx: &Bx,
constant: &'tcx ty::LazyConst<'tcx>,
2018-12-13 11:11:12 +01:00
) -> Result<ty::Const<'tcx>, ErrorHandled> {
match *constant {
ty::LazyConst::Unevaluated(def_id, ref substs) => {
let tcx = bx.tcx();
let param_env = ty::ParamEnv::reveal_all();
2018-01-16 09:31:48 +01:00
let instance = ty::Instance::resolve(tcx, param_env, def_id, substs).unwrap();
let cid = GlobalId {
instance,
promoted: None,
};
2018-06-25 20:53:02 +02:00
tcx.const_eval(param_env.and(cid))
2018-01-16 09:31:48 +01:00
},
ty::LazyConst::Evaluated(constant) => Ok(constant),
2017-01-02 11:00:42 -07:00
}
}
2018-06-25 20:53:02 +02:00
pub fn eval_mir_constant(
2018-01-16 09:31:48 +01:00
&mut self,
bx: &Bx,
2018-01-16 09:31:48 +01:00
constant: &mir::Constant<'tcx>,
2018-12-13 11:11:12 +01:00
) -> Result<ty::Const<'tcx>, ErrorHandled> {
let c = self.monomorphize(&constant.literal);
self.fully_evaluate(bx, c)
}
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>,
2018-12-13 11:11:12 +01:00
constant: Result<ty::Const<'tcx>, ErrorHandled>,
) -> (Bx::Value, Ty<'tcx>) {
constant
2018-01-16 09:31:48 +01:00
.and_then(|c| {
2018-06-25 20:53:02 +02:00
let field_ty = c.ty.builtin_index().unwrap();
let fields = match c.ty.sty {
2018-12-13 09:34:23 +01:00
ty::Array(_, n) => n.unwrap_usize(bx.tcx()),
2018-01-29 09:58:28 +01:00
ref other => bug!("invalid simd shuffle type: {}", other),
2018-01-16 09:31:48 +01:00
};
let values: Result<Vec<_>, ErrorHandled> = (0..fields).map(|field| {
let field = const_field(
2018-01-29 09:58:28 +01:00
bx.tcx(),
ty::ParamEnv::reveal_all(),
2018-01-29 09:58:28 +01:00
None,
mir::Field::new(field as usize),
c,
)?;
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)
};
Ok(bx.scalar_to_backend(
prim, scalar,
bx.immediate_backend_type(layout),
))
} else {
bug!("simd shuffle field {:?}", field)
2018-01-29 09:58:28 +01:00
}
}).collect();
let llval = bx.const_struct(&values?, false);
2018-06-25 20:53:02 +02:00
Ok((llval, c.ty))
2018-01-16 09:31:48 +01:00
})
.unwrap_or_else(|_| {
bx.tcx().sess.span_err(
span,
2018-06-02 23:38:57 +02:00
"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
}
}