2018-10-03 13:49:57 +02:00
|
|
|
use rustc::mir::interpret::ErrorHandled;
|
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;
|
2018-01-25 16:44:45 +01:00
|
|
|
use rustc::ty::{self, Ty};
|
2018-12-02 13:57:41 +01:00
|
|
|
use rustc::ty::layout::{self, HasTyCtxt};
|
2018-08-18 12:14:03 +02:00
|
|
|
use syntax::source_map::Span;
|
2019-02-09 23:31:47 +09:00
|
|
|
use crate::traits::*;
|
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-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>,
|
2019-04-03 15:29:31 +02:00
|
|
|
) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> {
|
2019-03-14 10:19:31 +01:00
|
|
|
match constant.literal.val {
|
|
|
|
|
mir::interpret::ConstValue::Unevaluated(def_id, ref substs) => {
|
|
|
|
|
let substs = self.monomorphize(substs);
|
|
|
|
|
let instance = ty::Instance::resolve(
|
2018-12-02 13:57:41 +01:00
|
|
|
self.cx.tcx(), ty::ParamEnv::reveal_all(), def_id, substs,
|
2019-03-14 10:19:31 +01:00
|
|
|
).unwrap();
|
|
|
|
|
let cid = mir::interpret::GlobalId {
|
2018-01-16 09:31:48 +01:00
|
|
|
instance,
|
|
|
|
|
promoted: None,
|
2017-09-24 12:12:26 +03:00
|
|
|
};
|
2018-12-02 13:57:41 +01:00
|
|
|
self.cx.tcx().const_eval(ty::ParamEnv::reveal_all().and(cid))
|
2018-01-16 09:31:48 +01:00
|
|
|
},
|
2019-04-03 15:29:31 +02:00
|
|
|
_ => Ok(self.monomorphize(&constant.literal)),
|
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>,
|
2019-04-03 15:29:31 +02:00
|
|
|
constant: Result<&'tcx ty::Const<'tcx>, ErrorHandled>,
|
2018-09-20 15:47:22 +02:00
|
|
|
) -> (Bx::Value, Ty<'tcx>) {
|
2018-07-22 01:01:07 +02:00
|
|
|
constant
|
2019-02-16 20:51:43 +01:00
|
|
|
.map(|c| {
|
2018-06-25 20:53:02 +02:00
|
|
|
let field_ty = c.ty.builtin_index().unwrap();
|
|
|
|
|
let fields = match c.ty.sty {
|
2019-03-22 18:30:35 +01:00
|
|
|
ty::Array(_, n) => n.eval_usize(bx.tcx()),
|
2018-12-07 19:14:30 +02:00
|
|
|
_ => bug!("invalid simd shuffle type: {}", c.ty),
|
2018-01-16 09:31:48 +01:00
|
|
|
};
|
2019-02-16 20:51:43 +01:00
|
|
|
let values: Vec<_> = (0..fields).map(|field| {
|
2019-05-24 14:58:37 -05:00
|
|
|
let field = bx.tcx().const_field(
|
|
|
|
|
ty::ParamEnv::reveal_all().and((&c, mir::Field::new(field as usize)))
|
2019-02-16 20:51:43 +01:00
|
|
|
);
|
2018-07-24 18:28:53 +02:00
|
|
|
if let Some(prim) = field.val.try_to_scalar() {
|
2018-11-27 19:00:25 +01:00
|
|
|
let layout = bx.layout_of(field_ty);
|
2018-04-26 09:18:19 +02:00
|
|
|
let scalar = match layout.abi {
|
|
|
|
|
layout::Abi::Scalar(ref x) => x,
|
|
|
|
|
_ => bug!("from_const: invalid ByVal layout: {:#?}", layout)
|
|
|
|
|
};
|
2019-02-16 20:51:43 +01:00
|
|
|
bx.scalar_to_backend(
|
2018-09-20 15:47:22 +02:00
|
|
|
prim, scalar,
|
2018-11-27 19:00:25 +01:00
|
|
|
bx.immediate_backend_type(layout),
|
2019-02-16 20:51:43 +01:00
|
|
|
)
|
2018-04-26 09:18:19 +02:00
|
|
|
} else {
|
|
|
|
|
bug!("simd shuffle field {:?}", field)
|
2018-01-29 09:58:28 +01:00
|
|
|
}
|
|
|
|
|
}).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(|_| {
|
|
|
|
|
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.
|
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
|
|
|
}
|
|
|
|
|
}
|