2015-10-21 17:42:25 -04:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2018-08-26 15:19:34 +02:00
|
|
|
use rustc::mir::interpret::{ErrorHandled, read_target_uint};
|
2018-09-20 10:17:14 +02:00
|
|
|
use rustc_mir::const_eval::const_field;
|
2016-04-21 16:15:56 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
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-09-20 15:47:22 +02:00
|
|
|
use rustc::mir::interpret::{GlobalId, Pointer, Allocation, ConstValue};
|
2018-01-25 16:44:45 +01:00
|
|
|
use rustc::ty::{self, Ty};
|
2018-09-20 15:47:22 +02:00
|
|
|
use rustc::ty::layout::{self, HasDataLayout, LayoutOf, Size};
|
|
|
|
|
use common::CodegenCx;
|
2018-08-18 12:14:03 +02:00
|
|
|
use syntax::source_map::Span;
|
2018-07-10 13:28:39 +03:00
|
|
|
use value::Value;
|
2018-09-20 15:47:22 +02:00
|
|
|
use interfaces::*;
|
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
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {
|
2018-01-16 09:31:48 +01:00
|
|
|
let mut llvals = Vec::with_capacity(alloc.relocations.len() + 1);
|
2018-11-03 22:57:53 +02:00
|
|
|
let dl = cx.data_layout();
|
|
|
|
|
let pointer_size = dl.pointer_size.bytes() as usize;
|
2018-01-16 09:31:48 +01:00
|
|
|
|
|
|
|
|
let mut next_offset = 0;
|
2018-09-21 23:32:59 +02:00
|
|
|
for &(offset, ((), alloc_id)) in alloc.relocations.iter() {
|
2018-05-19 16:37:29 +02:00
|
|
|
let offset = offset.bytes();
|
2018-01-16 09:31:48 +01:00
|
|
|
assert_eq!(offset as usize as u64, offset);
|
|
|
|
|
let offset = offset as usize;
|
|
|
|
|
if offset > next_offset {
|
2018-09-06 11:57:42 -07:00
|
|
|
llvals.push(cx.const_bytes(&alloc.bytes[next_offset..offset]));
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
|
|
|
|
let ptr_offset = read_target_uint(
|
2018-11-03 22:57:53 +02:00
|
|
|
dl.endian,
|
2018-01-16 09:31:48 +01:00
|
|
|
&alloc.bytes[offset..(offset + pointer_size)],
|
2018-04-26 09:18:19 +02:00
|
|
|
).expect("const_alloc_to_llvm: could not read relocation pointer") as u64;
|
2018-09-20 15:47:22 +02:00
|
|
|
llvals.push(cx.scalar_to_backend(
|
2018-09-21 23:32:59 +02:00
|
|
|
Pointer::new(alloc_id, Size::from_bytes(ptr_offset)).into(),
|
2018-05-21 00:00:38 +02:00
|
|
|
&layout::Scalar {
|
2018-01-16 09:31:48 +01:00
|
|
|
value: layout::Primitive::Pointer,
|
|
|
|
|
valid_range: 0..=!0
|
|
|
|
|
},
|
2018-09-06 13:52:15 -07:00
|
|
|
cx.type_i8p()
|
2018-01-16 09:31:48 +01:00
|
|
|
));
|
|
|
|
|
next_offset = offset + pointer_size;
|
|
|
|
|
}
|
|
|
|
|
if alloc.bytes.len() >= next_offset {
|
2018-09-06 11:57:42 -07:00
|
|
|
llvals.push(cx.const_bytes(&alloc.bytes[next_offset ..]));
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-06 11:57:42 -07:00
|
|
|
cx.const_struct(&llvals, true)
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
|
2018-07-10 13:28:39 +03:00
|
|
|
pub fn codegen_static_initializer(
|
|
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
2018-07-22 01:01:07 +02:00
|
|
|
def_id: DefId,
|
2018-08-26 15:19:34 +02:00
|
|
|
) -> Result<(&'ll Value, &'tcx Allocation), ErrorHandled> {
|
2018-01-16 10:16:38 +01:00
|
|
|
let instance = ty::Instance::mono(cx.tcx, def_id);
|
2018-01-16 09:31:48 +01:00
|
|
|
let cid = GlobalId {
|
|
|
|
|
instance,
|
2018-07-22 01:01:07 +02:00
|
|
|
promoted: None,
|
2017-01-02 11:00:42 -07:00
|
|
|
};
|
2018-02-10 13:18:02 -05:00
|
|
|
let param_env = ty::ParamEnv::reveal_all();
|
2018-04-10 16:25:10 +02:00
|
|
|
let static_ = cx.tcx.const_eval(param_env.and(cid))?;
|
2018-01-16 09:31:48 +01:00
|
|
|
|
2018-05-12 18:47:20 +02:00
|
|
|
let alloc = match static_.val {
|
2018-08-23 19:04:33 +02:00
|
|
|
ConstValue::ByRef(_, alloc, n) if n.bytes() == 0 => alloc,
|
2018-04-10 16:25:10 +02:00
|
|
|
_ => bug!("static const eval returned {:#?}", static_),
|
|
|
|
|
};
|
2018-06-28 06:24:09 +08:00
|
|
|
Ok((const_alloc_to_llvm(cx, alloc), alloc))
|
2018-01-16 09:31:48 +01:00
|
|
|
}
|
2017-09-26 21:34:10 +03:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
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,
|
2018-09-20 15:47:22 +02:00
|
|
|
bx: &Bx,
|
2018-01-16 09:31:48 +01:00
|
|
|
constant: &'tcx ty::Const<'tcx>,
|
2018-08-26 15:19:34 +02:00
|
|
|
) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> {
|
2018-01-16 09:31:48 +01:00
|
|
|
match constant.val {
|
2018-06-25 20:53:02 +02:00
|
|
|
ConstValue::Unevaluated(def_id, ref substs) => {
|
2018-01-16 10:16:38 +01:00
|
|
|
let tcx = bx.tcx();
|
2018-02-10 13:18:02 -05:00
|
|
|
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,
|
2017-09-24 12:12:26 +03:00
|
|
|
};
|
2018-06-25 20:53:02 +02:00
|
|
|
tcx.const_eval(param_env.and(cid))
|
2018-01-16 09:31:48 +01:00
|
|
|
},
|
2018-06-25 20:53:02 +02:00
|
|
|
_ => 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,
|
2018-09-20 15:47:22 +02:00
|
|
|
bx: &Bx,
|
2018-01-16 09:31:48 +01:00
|
|
|
constant: &mir::Constant<'tcx>,
|
2018-08-26 15:19:34 +02:00
|
|
|
) -> Result<&'tcx ty::Const<'tcx>, ErrorHandled> {
|
2018-07-22 01:01:07 +02:00
|
|
|
let c = self.monomorphize(&constant.literal);
|
|
|
|
|
self.fully_evaluate(bx, c)
|
2017-09-10 17:15:29 +03: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>,
|
2018-08-26 15:19:34 +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
|
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-08-22 01:35:02 +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
|
|
|
};
|
2018-08-26 15:19:34 +02:00
|
|
|
let values: Result<Vec<_>, ErrorHandled> = (0..fields).map(|field| {
|
2018-08-13 16:14:22 +02:00
|
|
|
let field = const_field(
|
2018-01-29 09:58:28 +01:00
|
|
|
bx.tcx(),
|
2018-02-10 13:18:02 -05:00
|
|
|
ty::ParamEnv::reveal_all(),
|
2018-01-29 09:58:28 +01:00
|
|
|
self.instance,
|
|
|
|
|
None,
|
|
|
|
|
mir::Field::new(field as usize),
|
|
|
|
|
c,
|
|
|
|
|
)?;
|
2018-07-24 18:28:53 +02:00
|
|
|
if let Some(prim) = field.val.try_to_scalar() {
|
2018-08-28 17:50:57 +02:00
|
|
|
let layout = bx.cx().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)
|
|
|
|
|
};
|
2018-09-20 15:47:22 +02:00
|
|
|
Ok(bx.cx().scalar_to_backend(
|
|
|
|
|
prim, scalar,
|
|
|
|
|
bx.cx().immediate_backend_type(layout),
|
2018-04-26 09:18:19 +02:00
|
|
|
))
|
|
|
|
|
} else {
|
|
|
|
|
bug!("simd shuffle field {:?}", field)
|
2018-01-29 09:58:28 +01:00
|
|
|
}
|
|
|
|
|
}).collect();
|
2018-09-06 11:57:42 -07:00
|
|
|
let llval = bx.cx().const_struct(&values?, false);
|
2018-06-25 20:53:02 +02:00
|
|
|
Ok((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-09-20 15:47:22 +02:00
|
|
|
let llty = bx.cx().backend_type(bx.cx().layout_of(ty));
|
2018-09-06 11:57:42 -07:00
|
|
|
(bx.cx().const_undef(llty), ty)
|
2018-01-16 09:31:48 +01:00
|
|
|
})
|
2017-01-02 11:00:42 -07:00
|
|
|
}
|
|
|
|
|
}
|