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.
|
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
use llvm::{self, ValueRef};
|
2016-03-30 13:43:36 +02:00
|
|
|
use rustc::middle::const_val::ConstVal;
|
2016-08-16 17:41:38 +03:00
|
|
|
use rustc_const_eval::{ErrKind, ConstEvalErr, report_const_eval_err};
|
2016-03-15 12:33:13 +01:00
|
|
|
use rustc_const_math::ConstInt::*;
|
2016-06-01 12:22:07 +03:00
|
|
|
use rustc_const_math::ConstFloat::*;
|
2016-08-16 17:41:38 +03:00
|
|
|
use rustc_const_math::{ConstInt, ConstIsize, ConstUsize, ConstMathErr};
|
2016-04-21 16:15:56 +03:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-05-11 04:14:41 +03:00
|
|
|
use rustc::infer::TransNormalize;
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir;
|
2016-04-21 16:15:56 +03:00
|
|
|
use rustc::mir::tcx::LvalueTy;
|
|
|
|
use rustc::traits;
|
2016-05-27 14:40:05 +03:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
|
2016-04-21 16:15:56 +03:00
|
|
|
use rustc::ty::cast::{CastTy, IntTy};
|
|
|
|
use rustc::ty::subst::Substs;
|
2016-06-07 17:28:36 +03:00
|
|
|
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
|
2016-08-28 20:44:19 -04:00
|
|
|
use {abi, adt, base, Disr, machine};
|
2016-04-21 16:15:56 +03:00
|
|
|
use callee::Callee;
|
2016-12-18 11:50:07 -07:00
|
|
|
use common::{self, BlockAndBuilder, CrateContext, const_get_elt, val_ty};
|
2016-04-21 16:15:56 +03:00
|
|
|
use common::{C_array, C_bool, C_bytes, C_floating_f64, C_integral};
|
|
|
|
use common::{C_null, C_struct, C_str_slice, C_undef, C_uint};
|
2016-08-16 17:41:38 +03:00
|
|
|
use common::{const_to_opt_int, const_to_opt_uint};
|
|
|
|
use consts;
|
2016-04-21 16:15:56 +03:00
|
|
|
use monomorphize::{self, Instance};
|
2016-03-22 19:23:36 +02:00
|
|
|
use type_of;
|
|
|
|
use type_::Type;
|
2016-04-21 16:15:56 +03:00
|
|
|
use value::Value;
|
2015-11-16 19:57:57 +02:00
|
|
|
|
2016-08-16 17:41:38 +03:00
|
|
|
use syntax::ast;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2015-10-21 17:42:25 -04:00
|
|
|
|
2016-09-12 01:53:43 +03:00
|
|
|
use std::fmt;
|
2016-04-21 16:15:56 +03:00
|
|
|
use std::ptr;
|
2016-01-08 01:15:59 +02:00
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
use super::operand::{OperandRef, OperandValue};
|
2016-05-03 00:26:41 +03:00
|
|
|
use super::MirContext;
|
2015-11-16 18:41:16 +01:00
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
/// A sized constant rvalue.
|
|
|
|
/// The LLVM type might not be the same for a single Rust type,
|
|
|
|
/// e.g. each enum variant would have its own LLVM struct type.
|
|
|
|
#[derive(Copy, Clone)]
|
2016-04-26 23:54:38 +03:00
|
|
|
pub struct Const<'tcx> {
|
|
|
|
pub llval: ValueRef,
|
|
|
|
pub ty: Ty<'tcx>
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
2015-11-16 18:41:16 +01:00
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
impl<'tcx> Const<'tcx> {
|
2016-04-26 23:54:38 +03:00
|
|
|
pub fn new(llval: ValueRef, ty: Ty<'tcx>) -> Const<'tcx> {
|
2016-04-21 16:15:56 +03:00
|
|
|
Const {
|
|
|
|
llval: llval,
|
|
|
|
ty: ty
|
2015-11-08 19:11:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
/// Translate ConstVal into a LLVM constant value.
|
2016-04-26 23:54:38 +03:00
|
|
|
pub fn from_constval<'a>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
cv: ConstVal,
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
-> Const<'tcx> {
|
2015-12-28 21:18:24 +02:00
|
|
|
let llty = type_of::type_of(ccx, ty);
|
2016-04-21 16:15:56 +03:00
|
|
|
let val = match cv {
|
2016-06-01 12:22:07 +03:00
|
|
|
ConstVal::Float(F32(v)) => C_floating_f64(v as f64, llty),
|
|
|
|
ConstVal::Float(F64(v)) => C_floating_f64(v, llty),
|
|
|
|
ConstVal::Float(FInfer {..}) => bug!("MIR must not use `{:?}`", cv),
|
2015-12-28 21:18:24 +02:00
|
|
|
ConstVal::Bool(v) => C_bool(ccx, v),
|
2015-12-16 18:44:15 +01:00
|
|
|
ConstVal::Integral(I8(v)) => C_integral(Type::i8(ccx), v as u64, true),
|
|
|
|
ConstVal::Integral(I16(v)) => C_integral(Type::i16(ccx), v as u64, true),
|
|
|
|
ConstVal::Integral(I32(v)) => C_integral(Type::i32(ccx), v as u64, true),
|
|
|
|
ConstVal::Integral(I64(v)) => C_integral(Type::i64(ccx), v as u64, true),
|
|
|
|
ConstVal::Integral(Isize(v)) => {
|
|
|
|
let i = v.as_i64(ccx.tcx().sess.target.int_type);
|
|
|
|
C_integral(Type::int(ccx), i as u64, true)
|
|
|
|
},
|
|
|
|
ConstVal::Integral(U8(v)) => C_integral(Type::i8(ccx), v as u64, false),
|
|
|
|
ConstVal::Integral(U16(v)) => C_integral(Type::i16(ccx), v as u64, false),
|
|
|
|
ConstVal::Integral(U32(v)) => C_integral(Type::i32(ccx), v as u64, false),
|
|
|
|
ConstVal::Integral(U64(v)) => C_integral(Type::i64(ccx), v, false),
|
|
|
|
ConstVal::Integral(Usize(v)) => {
|
|
|
|
let u = v.as_u64(ccx.tcx().sess.target.uint_type);
|
|
|
|
C_integral(Type::int(ccx), u, false)
|
|
|
|
},
|
2016-06-01 12:22:07 +03:00
|
|
|
ConstVal::Integral(Infer(_)) |
|
|
|
|
ConstVal::Integral(InferSigned(_)) => bug!("MIR must not use `{:?}`", cv),
|
2015-12-28 21:18:24 +02:00
|
|
|
ConstVal::Str(ref v) => C_str_slice(ccx, v.clone()),
|
|
|
|
ConstVal::ByteStr(ref v) => consts::addr_of(ccx, C_bytes(ccx, v), 1, "byte_str"),
|
2016-03-08 14:15:23 +02:00
|
|
|
ConstVal::Struct(_) | ConstVal::Tuple(_) |
|
|
|
|
ConstVal::Array(..) | ConstVal::Repeat(..) |
|
|
|
|
ConstVal::Function(_) => {
|
2016-06-01 12:22:07 +03:00
|
|
|
bug!("MIR must not use `{:?}` (which refers to a local ID)", cv)
|
2016-03-08 14:15:23 +02:00
|
|
|
}
|
2015-12-16 18:44:15 +01:00
|
|
|
ConstVal::Char(c) => C_integral(Type::char(ccx), c as u64, false),
|
2016-03-29 01:46:02 +02:00
|
|
|
ConstVal::Dummy => bug!(),
|
2016-04-21 16:15:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
assert!(!ty.has_erasable_regions());
|
|
|
|
|
|
|
|
Const::new(val, ty)
|
|
|
|
}
|
|
|
|
|
2016-05-25 11:55:44 +03:00
|
|
|
fn get_pair(&self) -> (ValueRef, ValueRef) {
|
|
|
|
(const_get_elt(self.llval, &[0]),
|
|
|
|
const_get_elt(self.llval, &[1]))
|
|
|
|
}
|
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
fn get_fat_ptr(&self) -> (ValueRef, ValueRef) {
|
2016-05-25 11:55:44 +03:00
|
|
|
assert_eq!(abi::FAT_PTR_ADDR, 0);
|
|
|
|
assert_eq!(abi::FAT_PTR_EXTRA, 1);
|
|
|
|
self.get_pair()
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn as_lvalue(&self) -> ConstLvalue<'tcx> {
|
|
|
|
ConstLvalue {
|
|
|
|
base: Base::Value(self.llval),
|
|
|
|
llextra: ptr::null_mut(),
|
|
|
|
ty: self.ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 23:54:38 +03:00
|
|
|
pub fn to_operand<'a>(&self, ccx: &CrateContext<'a, 'tcx>) -> OperandRef<'tcx> {
|
2016-04-21 16:15:56 +03:00
|
|
|
let llty = type_of::immediate_type_of(ccx, self.ty);
|
|
|
|
let llvalty = val_ty(self.llval);
|
|
|
|
|
2016-06-07 02:21:58 +03:00
|
|
|
let val = if llty == llvalty && common::type_is_imm_pair(ccx, self.ty) {
|
2016-05-25 11:55:44 +03:00
|
|
|
let (a, b) = self.get_pair();
|
|
|
|
OperandValue::Pair(a, b)
|
2016-06-07 02:21:58 +03:00
|
|
|
} else if llty == llvalty && common::type_is_immediate(ccx, self.ty) {
|
2016-04-21 16:15:56 +03:00
|
|
|
// If the types match, we can use the value directly.
|
|
|
|
OperandValue::Immediate(self.llval)
|
|
|
|
} else {
|
|
|
|
// Otherwise, or if the value is not immediate, we create
|
|
|
|
// a constant LLVM global and cast its address if necessary.
|
|
|
|
let align = type_of::align_of(ccx, self.ty);
|
|
|
|
let ptr = consts::addr_of(ccx, self.llval, align, "const");
|
|
|
|
OperandValue::Ref(consts::ptrcast(ptr, llty.ptr_to()))
|
|
|
|
};
|
|
|
|
|
|
|
|
OperandRef {
|
|
|
|
val: val,
|
|
|
|
ty: self.ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 01:53:43 +03:00
|
|
|
impl<'tcx> fmt::Debug for Const<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "Const({:?}: {:?})", Value(self.llval), self.ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum Base {
|
|
|
|
/// A constant value without an unique address.
|
|
|
|
Value(ValueRef),
|
|
|
|
|
2016-05-08 00:28:52 +03:00
|
|
|
/// String literal base pointer (cast from array).
|
|
|
|
Str(ValueRef),
|
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
/// The address of a static.
|
|
|
|
Static(ValueRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An lvalue as seen from a constant.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct ConstLvalue<'tcx> {
|
|
|
|
base: Base,
|
|
|
|
llextra: ValueRef,
|
|
|
|
ty: Ty<'tcx>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ConstLvalue<'tcx> {
|
|
|
|
fn to_const(&self, span: Span) -> Const<'tcx> {
|
|
|
|
match self.base {
|
|
|
|
Base::Value(val) => Const::new(val, self.ty),
|
2016-05-08 00:28:52 +03:00
|
|
|
Base::Str(ptr) => {
|
|
|
|
span_bug!(span, "loading from `str` ({:?}) in constant",
|
|
|
|
Value(ptr))
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
Base::Static(val) => {
|
|
|
|
span_bug!(span, "loading from `static` ({:?}) in constant",
|
|
|
|
Value(val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len<'a>(&self, ccx: &CrateContext<'a, 'tcx>) -> ValueRef {
|
|
|
|
match self.ty.sty {
|
|
|
|
ty::TyArray(_, n) => C_uint(ccx, n),
|
|
|
|
ty::TySlice(_) | ty::TyStr => {
|
|
|
|
assert!(self.llextra != ptr::null_mut());
|
|
|
|
self.llextra
|
|
|
|
}
|
|
|
|
_ => bug!("unexpected type `{}` in ConstLvalue::len", self.ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Machinery for translating a constant's MIR to LLVM values.
|
|
|
|
/// FIXME(eddyb) use miri and lower its allocations to LLVM.
|
|
|
|
struct MirConstContext<'a, 'tcx: 'a> {
|
|
|
|
ccx: &'a CrateContext<'a, 'tcx>,
|
2016-05-03 00:26:41 +03:00
|
|
|
mir: &'a mir::Mir<'tcx>,
|
2016-04-21 16:15:56 +03:00
|
|
|
|
|
|
|
/// Type parameters for const fn and associated constants.
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
|
|
|
|
2016-06-20 23:55:14 +03:00
|
|
|
/// Values of locals in a constant or const fn.
|
|
|
|
locals: IndexVec<mir::Local, Option<Const<'tcx>>>
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
|
|
|
|
fn new(ccx: &'a CrateContext<'a, 'tcx>,
|
2016-05-03 00:26:41 +03:00
|
|
|
mir: &'a mir::Mir<'tcx>,
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
2016-09-25 01:38:27 +02:00
|
|
|
args: IndexVec<mir::Local, Const<'tcx>>)
|
2016-04-21 16:15:56 +03:00
|
|
|
-> MirConstContext<'a, 'tcx> {
|
2016-06-20 23:55:14 +03:00
|
|
|
let mut context = MirConstContext {
|
2016-05-03 00:26:41 +03:00
|
|
|
ccx: ccx,
|
|
|
|
mir: mir,
|
|
|
|
substs: substs,
|
2016-09-25 01:38:27 +02:00
|
|
|
locals: (0..mir.local_decls.len()).map(|_| None).collect(),
|
2016-06-20 23:55:14 +03:00
|
|
|
};
|
|
|
|
for (i, arg) in args.into_iter().enumerate() {
|
2016-09-29 01:11:54 +02:00
|
|
|
// Locals after local 0 are the function arguments
|
2016-09-25 01:38:27 +02:00
|
|
|
let index = mir::Local::new(i + 1);
|
2016-06-20 23:55:14 +03:00
|
|
|
context.locals[index] = Some(arg);
|
2016-05-03 00:26:41 +03:00
|
|
|
}
|
2016-06-20 23:55:14 +03:00
|
|
|
context
|
2016-05-03 00:26:41 +03:00
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
|
2016-05-03 00:26:41 +03:00
|
|
|
fn trans_def(ccx: &'a CrateContext<'a, 'tcx>,
|
|
|
|
mut instance: Instance<'tcx>,
|
2016-09-25 01:38:27 +02:00
|
|
|
args: IndexVec<mir::Local, Const<'tcx>>)
|
2016-08-16 17:41:38 +03:00
|
|
|
-> Result<Const<'tcx>, ConstEvalErr> {
|
2016-04-21 16:15:56 +03:00
|
|
|
// Try to resolve associated constants.
|
2016-08-08 23:39:49 +03:00
|
|
|
if let Some(trait_id) = ccx.tcx().trait_of_item(instance.def) {
|
|
|
|
let trait_ref = ty::TraitRef::new(trait_id, instance.substs);
|
|
|
|
let trait_ref = ty::Binder(trait_ref);
|
2016-05-09 16:15:04 -04:00
|
|
|
let vtable = common::fulfill_obligation(ccx.shared(), DUMMY_SP, trait_ref);
|
2016-04-21 16:15:56 +03:00
|
|
|
if let traits::VtableImpl(vtable_impl) = vtable {
|
|
|
|
let name = ccx.tcx().item_name(instance.def);
|
2016-11-10 02:06:34 +02:00
|
|
|
let ac = ccx.tcx().associated_items(vtable_impl.impl_def_id)
|
|
|
|
.find(|item| item.kind == ty::AssociatedKind::Const && item.name == name);
|
2016-09-19 23:49:01 +03:00
|
|
|
if let Some(ac) = ac {
|
2016-09-05 10:54:38 +03:00
|
|
|
instance = Instance::new(ac.def_id, vtable_impl.substs);
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 13:55:49 +03:00
|
|
|
let mir = ccx.tcx().item_mir(instance.def);
|
2016-05-03 00:26:41 +03:00
|
|
|
MirConstContext::new(ccx, &mir, instance.substs, args).trans()
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn monomorphize<T>(&self, value: &T) -> T
|
2016-05-11 04:14:41 +03:00
|
|
|
where T: TransNormalize<'tcx>
|
2016-04-21 16:15:56 +03:00
|
|
|
{
|
2016-08-08 20:50:19 -04:00
|
|
|
monomorphize::apply_param_substs(self.ccx.shared(),
|
2016-04-21 16:15:56 +03:00
|
|
|
self.substs,
|
|
|
|
value)
|
|
|
|
}
|
|
|
|
|
2016-08-16 17:41:38 +03:00
|
|
|
fn trans(&mut self) -> Result<Const<'tcx>, ConstEvalErr> {
|
2016-04-21 16:15:56 +03:00
|
|
|
let tcx = self.ccx.tcx();
|
|
|
|
let mut bb = mir::START_BLOCK;
|
2016-06-05 14:38:29 +03:00
|
|
|
|
|
|
|
// Make sure to evaluate all statemenets to
|
|
|
|
// report as many errors as we possibly can.
|
|
|
|
let mut failure = Ok(());
|
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
loop {
|
2016-06-07 21:20:50 +03:00
|
|
|
let data = &self.mir[bb];
|
2016-04-21 16:15:56 +03:00
|
|
|
for statement in &data.statements {
|
2016-06-07 19:21:56 +03:00
|
|
|
let span = statement.source_info.span;
|
2016-04-21 16:15:56 +03:00
|
|
|
match statement.kind {
|
|
|
|
mir::StatementKind::Assign(ref dest, ref rvalue) => {
|
2016-08-05 15:59:51 -07:00
|
|
|
let ty = dest.ty(self.mir, tcx);
|
2016-04-21 16:15:56 +03:00
|
|
|
let ty = self.monomorphize(&ty).to_ty(tcx);
|
2016-06-07 19:21:56 +03:00
|
|
|
match self.const_rvalue(rvalue, ty, span) {
|
|
|
|
Ok(value) => self.store(dest, value, span),
|
2016-06-05 14:38:29 +03:00
|
|
|
Err(err) => if failure.is_ok() { failure = Err(err); }
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
2016-08-14 06:34:14 +03:00
|
|
|
mir::StatementKind::StorageLive(_) |
|
2016-09-15 18:17:58 -07:00
|
|
|
mir::StatementKind::StorageDead(_) |
|
|
|
|
mir::StatementKind::Nop => {}
|
2016-08-04 16:14:33 -07:00
|
|
|
mir::StatementKind::SetDiscriminant{ .. } => {
|
|
|
|
span_bug!(span, "SetDiscriminant should not appear in constants?");
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let terminator = data.terminator();
|
2016-06-07 19:21:56 +03:00
|
|
|
let span = terminator.source_info.span;
|
2016-04-21 16:15:56 +03:00
|
|
|
bb = match terminator.kind {
|
|
|
|
mir::TerminatorKind::Drop { target, .. } | // No dropping.
|
|
|
|
mir::TerminatorKind::Goto { target } => target,
|
|
|
|
mir::TerminatorKind::Return => {
|
2016-06-05 14:38:29 +03:00
|
|
|
failure?;
|
2016-09-25 01:38:27 +02:00
|
|
|
return Ok(self.locals[mir::RETURN_POINTER].unwrap_or_else(|| {
|
2016-04-21 16:15:56 +03:00
|
|
|
span_bug!(span, "no returned value in constant");
|
2016-06-20 23:55:14 +03:00
|
|
|
}));
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
2016-05-25 08:39:32 +03:00
|
|
|
mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, .. } => {
|
2016-04-21 16:15:56 +03:00
|
|
|
let cond = self.const_operand(cond, span)?;
|
2016-05-25 08:39:32 +03:00
|
|
|
let cond_bool = common::const_to_uint(cond.llval) != 0;
|
|
|
|
if cond_bool != expected {
|
|
|
|
let err = match *msg {
|
2016-05-26 22:09:48 +03:00
|
|
|
mir::AssertMessage::BoundsCheck { ref len, ref index } => {
|
|
|
|
let len = self.const_operand(len, span)?;
|
|
|
|
let index = self.const_operand(index, span)?;
|
|
|
|
ErrKind::IndexOutOfBounds {
|
|
|
|
len: common::const_to_uint(len.llval),
|
|
|
|
index: common::const_to_uint(index.llval)
|
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
}
|
|
|
|
mir::AssertMessage::Math(ref err) => {
|
|
|
|
ErrKind::Math(err.clone())
|
|
|
|
}
|
|
|
|
};
|
2016-08-16 17:41:38 +03:00
|
|
|
|
|
|
|
let err = ConstEvalErr{ span: span, kind: err };
|
|
|
|
report_const_eval_err(tcx, &err, span, "expression").emit();
|
|
|
|
failure = Err(err);
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
2016-05-25 08:39:32 +03:00
|
|
|
target
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::TerminatorKind::Call { ref func, ref args, ref destination, .. } => {
|
2016-08-08 13:35:10 -07:00
|
|
|
let fn_ty = func.ty(self.mir, tcx);
|
2016-04-21 16:15:56 +03:00
|
|
|
let fn_ty = self.monomorphize(&fn_ty);
|
|
|
|
let instance = match fn_ty.sty {
|
|
|
|
ty::TyFnDef(def_id, substs, _) => {
|
|
|
|
Instance::new(def_id, substs)
|
|
|
|
}
|
|
|
|
_ => span_bug!(span, "calling {:?} (of type {}) in constant",
|
|
|
|
func, fn_ty)
|
|
|
|
};
|
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
let mut const_args = IndexVec::with_capacity(args.len());
|
2016-06-05 14:38:29 +03:00
|
|
|
for arg in args {
|
|
|
|
match self.const_operand(arg, span) {
|
2016-06-07 17:28:36 +03:00
|
|
|
Ok(arg) => { const_args.push(arg); },
|
2016-06-05 14:38:29 +03:00
|
|
|
Err(err) => if failure.is_ok() { failure = Err(err); }
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
if let Some((ref dest, target)) = *destination {
|
2016-06-05 14:38:29 +03:00
|
|
|
match MirConstContext::trans_def(self.ccx, instance, const_args) {
|
|
|
|
Ok(value) => self.store(dest, value, span),
|
|
|
|
Err(err) => if failure.is_ok() { failure = Err(err); }
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
target
|
|
|
|
} else {
|
2016-06-05 14:38:29 +03:00
|
|
|
span_bug!(span, "diverging {:?} in constant", terminator.kind);
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => span_bug!(span, "{:?} in constant", terminator.kind)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn store(&mut self, dest: &mir::Lvalue<'tcx>, value: Const<'tcx>, span: Span) {
|
2016-09-25 01:38:27 +02:00
|
|
|
if let mir::Lvalue::Local(index) = *dest {
|
2016-06-20 23:55:14 +03:00
|
|
|
self.locals[index] = Some(value);
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "assignment to {:?} in constant", dest);
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn const_lvalue(&self, lvalue: &mir::Lvalue<'tcx>, span: Span)
|
2016-08-16 17:41:38 +03:00
|
|
|
-> Result<ConstLvalue<'tcx>, ConstEvalErr> {
|
2016-04-21 16:15:56 +03:00
|
|
|
let tcx = self.ccx.tcx();
|
2016-06-20 23:55:14 +03:00
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
if let mir::Lvalue::Local(index) = *lvalue {
|
2016-06-20 23:55:14 +03:00
|
|
|
return Ok(self.locals[index].unwrap_or_else(|| {
|
|
|
|
span_bug!(span, "{:?} not initialized", lvalue)
|
|
|
|
}).as_lvalue());
|
|
|
|
}
|
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
let lvalue = match *lvalue {
|
2016-09-25 01:38:27 +02:00
|
|
|
mir::Lvalue::Local(_) => bug!(), // handled above
|
2016-04-21 16:15:56 +03:00
|
|
|
mir::Lvalue::Static(def_id) => {
|
|
|
|
ConstLvalue {
|
2016-08-16 17:41:38 +03:00
|
|
|
base: Base::Static(consts::get_static(self.ccx, def_id)),
|
2016-04-21 16:15:56 +03:00
|
|
|
llextra: ptr::null_mut(),
|
2016-08-05 15:59:51 -07:00
|
|
|
ty: lvalue.ty(self.mir, tcx).to_ty(tcx)
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mir::Lvalue::Projection(ref projection) => {
|
|
|
|
let tr_base = self.const_lvalue(&projection.base, span)?;
|
|
|
|
let projected_ty = LvalueTy::Ty { ty: tr_base.ty }
|
|
|
|
.projection_ty(tcx, &projection.elem);
|
|
|
|
let base = tr_base.to_const(span);
|
|
|
|
let projected_ty = self.monomorphize(&projected_ty).to_ty(tcx);
|
2016-12-18 11:50:07 -07:00
|
|
|
let is_sized = self.ccx.shared().type_is_sized(projected_ty);
|
2016-04-21 16:15:56 +03:00
|
|
|
|
|
|
|
let (projected, llextra) = match projection.elem {
|
|
|
|
mir::ProjectionElem::Deref => {
|
|
|
|
let (base, extra) = if is_sized {
|
|
|
|
(base.llval, ptr::null_mut())
|
|
|
|
} else {
|
|
|
|
base.get_fat_ptr()
|
|
|
|
};
|
|
|
|
if self.ccx.statics().borrow().contains_key(&base) {
|
|
|
|
(Base::Static(base), extra)
|
2016-05-08 00:28:52 +03:00
|
|
|
} else if let ty::TyStr = projected_ty.sty {
|
|
|
|
(Base::Str(base), extra)
|
2016-04-21 16:15:56 +03:00
|
|
|
} else {
|
2016-08-16 17:41:38 +03:00
|
|
|
let v = base;
|
|
|
|
let v = self.ccx.const_unsized().borrow().get(&v).map_or(v, |&v| v);
|
|
|
|
let mut val = unsafe { llvm::LLVMGetInitializer(v) };
|
2016-04-21 16:15:56 +03:00
|
|
|
if val.is_null() {
|
|
|
|
span_bug!(span, "dereference of non-constant pointer `{:?}`",
|
|
|
|
Value(base));
|
|
|
|
}
|
2016-08-16 17:41:38 +03:00
|
|
|
if projected_ty.is_bool() {
|
|
|
|
unsafe {
|
|
|
|
val = llvm::LLVMConstTrunc(val, Type::i1(self.ccx).to_ref());
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
(Base::Value(val), extra)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mir::ProjectionElem::Field(ref field, _) => {
|
2016-08-28 20:44:19 -04:00
|
|
|
let llprojected = adt::const_get_field(self.ccx, tr_base.ty, base.llval,
|
2016-04-21 16:15:56 +03:00
|
|
|
Disr(0), field.index());
|
|
|
|
let llextra = if is_sized {
|
|
|
|
ptr::null_mut()
|
|
|
|
} else {
|
|
|
|
tr_base.llextra
|
|
|
|
};
|
|
|
|
(Base::Value(llprojected), llextra)
|
|
|
|
}
|
|
|
|
mir::ProjectionElem::Index(ref index) => {
|
|
|
|
let llindex = self.const_operand(index, span)?.llval;
|
|
|
|
|
|
|
|
let iv = if let Some(iv) = common::const_to_opt_uint(llindex) {
|
|
|
|
iv
|
|
|
|
} else {
|
|
|
|
span_bug!(span, "index is not an integer-constant expression")
|
|
|
|
};
|
2016-06-05 14:38:29 +03:00
|
|
|
|
|
|
|
// Produce an undef instead of a LLVM assertion on OOB.
|
|
|
|
let len = common::const_to_uint(tr_base.len(self.ccx));
|
|
|
|
let llelem = if iv < len {
|
|
|
|
const_get_elt(base.llval, &[iv as u32])
|
|
|
|
} else {
|
|
|
|
C_undef(type_of::type_of(self.ccx, projected_ty))
|
|
|
|
};
|
|
|
|
|
|
|
|
(Base::Value(llelem), ptr::null_mut())
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
_ => span_bug!(span, "{:?} in constant", projection.elem)
|
|
|
|
};
|
|
|
|
ConstLvalue {
|
|
|
|
base: projected,
|
|
|
|
llextra: llextra,
|
|
|
|
ty: projected_ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(lvalue)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn const_operand(&self, operand: &mir::Operand<'tcx>, span: Span)
|
2016-08-16 17:41:38 +03:00
|
|
|
-> Result<Const<'tcx>, ConstEvalErr> {
|
2016-09-12 01:53:43 +03:00
|
|
|
debug!("const_operand({:?} @ {:?})", operand, span);
|
|
|
|
let result = match *operand {
|
2016-04-21 16:15:56 +03:00
|
|
|
mir::Operand::Consume(ref lvalue) => {
|
|
|
|
Ok(self.const_lvalue(lvalue, span)?.to_const(span))
|
|
|
|
}
|
|
|
|
|
|
|
|
mir::Operand::Constant(ref constant) => {
|
|
|
|
let ty = self.monomorphize(&constant.ty);
|
|
|
|
match constant.literal.clone() {
|
|
|
|
mir::Literal::Item { def_id, substs } => {
|
|
|
|
// Shortcut for zero-sized types, including function item
|
|
|
|
// types, which would not work with MirConstContext.
|
|
|
|
if common::type_is_zero_size(self.ccx, ty) {
|
|
|
|
let llty = type_of::type_of(self.ccx, ty);
|
|
|
|
return Ok(Const::new(C_null(llty), ty));
|
|
|
|
}
|
|
|
|
|
2016-04-29 08:30:54 +03:00
|
|
|
let substs = self.monomorphize(&substs);
|
2016-04-21 16:15:56 +03:00
|
|
|
let instance = Instance::new(def_id, substs);
|
2016-06-07 17:28:36 +03:00
|
|
|
MirConstContext::trans_def(self.ccx, instance, IndexVec::new())
|
2016-05-03 00:26:41 +03:00
|
|
|
}
|
|
|
|
mir::Literal::Promoted { index } => {
|
|
|
|
let mir = &self.mir.promoted[index];
|
2016-06-07 17:28:36 +03:00
|
|
|
MirConstContext::new(self.ccx, mir, self.substs, IndexVec::new()).trans()
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
mir::Literal::Value { value } => {
|
|
|
|
Ok(Const::from_constval(self.ccx, value, ty))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 01:53:43 +03:00
|
|
|
};
|
|
|
|
debug!("const_operand({:?} @ {:?}) = {:?}", operand, span,
|
|
|
|
result.as_ref().ok());
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn const_array(&self, array_ty: Ty<'tcx>, fields: &[ValueRef])
|
|
|
|
-> Const<'tcx>
|
|
|
|
{
|
|
|
|
let elem_ty = array_ty.builtin_index().unwrap_or_else(|| {
|
|
|
|
bug!("bad array type {:?}", array_ty)
|
|
|
|
});
|
|
|
|
let llunitty = type_of::type_of(self.ccx, elem_ty);
|
|
|
|
// If the array contains enums, an LLVM array won't work.
|
|
|
|
let val = if fields.iter().all(|&f| val_ty(f) == llunitty) {
|
|
|
|
C_array(llunitty, fields)
|
|
|
|
} else {
|
|
|
|
C_struct(self.ccx, fields, false)
|
|
|
|
};
|
|
|
|
Const::new(val, array_ty)
|
2015-12-28 21:18:24 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
fn const_rvalue(&self, rvalue: &mir::Rvalue<'tcx>,
|
|
|
|
dest_ty: Ty<'tcx>, span: Span)
|
2016-08-16 17:41:38 +03:00
|
|
|
-> Result<Const<'tcx>, ConstEvalErr> {
|
2016-04-21 16:15:56 +03:00
|
|
|
let tcx = self.ccx.tcx();
|
2016-09-12 01:53:43 +03:00
|
|
|
debug!("const_rvalue({:?}: {:?} @ {:?})", rvalue, dest_ty, span);
|
2016-04-21 16:15:56 +03:00
|
|
|
let val = match *rvalue {
|
|
|
|
mir::Rvalue::Use(ref operand) => self.const_operand(operand, span)?,
|
|
|
|
|
|
|
|
mir::Rvalue::Repeat(ref elem, ref count) => {
|
|
|
|
let elem = self.const_operand(elem, span)?;
|
|
|
|
let size = count.value.as_u64(tcx.sess.target.uint_type);
|
|
|
|
let fields = vec![elem.llval; size as usize];
|
2016-09-12 01:53:43 +03:00
|
|
|
self.const_array(dest_ty, &fields)
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::Rvalue::Aggregate(ref kind, ref operands) => {
|
2016-06-05 14:38:29 +03:00
|
|
|
// Make sure to evaluate all operands to
|
|
|
|
// report as many errors as we possibly can.
|
|
|
|
let mut fields = Vec::with_capacity(operands.len());
|
|
|
|
let mut failure = Ok(());
|
|
|
|
for operand in operands {
|
|
|
|
match self.const_operand(operand, span) {
|
|
|
|
Ok(val) => fields.push(val.llval),
|
|
|
|
Err(err) => if failure.is_ok() { failure = Err(err); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
failure?;
|
2016-04-21 16:15:56 +03:00
|
|
|
|
2016-09-12 01:53:43 +03:00
|
|
|
match *kind {
|
2016-09-20 02:14:46 +02:00
|
|
|
mir::AggregateKind::Array => {
|
2016-09-12 01:53:43 +03:00
|
|
|
self.const_array(dest_ty, &fields)
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
2016-09-12 01:53:43 +03:00
|
|
|
mir::AggregateKind::Adt(..) |
|
|
|
|
mir::AggregateKind::Closure(..) |
|
|
|
|
mir::AggregateKind::Tuple => {
|
|
|
|
let disr = match *kind {
|
|
|
|
mir::AggregateKind::Adt(adt_def, index, _, _) => {
|
|
|
|
Disr::from(adt_def.variants[index].disr_val)
|
|
|
|
}
|
|
|
|
_ => Disr(0)
|
|
|
|
};
|
|
|
|
Const::new(
|
2016-08-28 20:44:19 -04:00
|
|
|
adt::trans_const(self.ccx, dest_ty, disr, &fields),
|
2016-09-12 01:53:43 +03:00
|
|
|
dest_ty
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::Rvalue::Cast(ref kind, ref source, cast_ty) => {
|
|
|
|
let operand = self.const_operand(source, span)?;
|
|
|
|
let cast_ty = self.monomorphize(&cast_ty);
|
|
|
|
|
|
|
|
let val = match *kind {
|
|
|
|
mir::CastKind::ReifyFnPointer => {
|
|
|
|
match operand.ty.sty {
|
|
|
|
ty::TyFnDef(def_id, substs, _) => {
|
|
|
|
Callee::def(self.ccx, def_id, substs)
|
2016-08-16 17:41:38 +03:00
|
|
|
.reify(self.ccx)
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
span_bug!(span, "{} cannot be reified to a fn ptr",
|
|
|
|
operand.ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mir::CastKind::UnsafeFnPointer => {
|
|
|
|
// this is a no-op at the LLVM level
|
|
|
|
operand.llval
|
|
|
|
}
|
|
|
|
mir::CastKind::Unsize => {
|
|
|
|
// unsize targets other than to a fat pointer currently
|
|
|
|
// can't be in constants.
|
2016-12-18 11:50:07 -07:00
|
|
|
assert!(common::type_is_fat_ptr(self.ccx, cast_ty));
|
2016-04-21 16:15:56 +03:00
|
|
|
|
|
|
|
let pointee_ty = operand.ty.builtin_deref(true, ty::NoPreference)
|
|
|
|
.expect("consts: unsizing got non-pointer type").ty;
|
2016-12-18 11:50:07 -07:00
|
|
|
let (base, old_info) = if !self.ccx.shared().type_is_sized(pointee_ty) {
|
2016-04-21 16:15:56 +03:00
|
|
|
// Normally, the source is a thin pointer and we are
|
|
|
|
// adding extra info to make a fat pointer. The exception
|
|
|
|
// is when we are upcasting an existing object fat pointer
|
|
|
|
// to use a different vtable. In that case, we want to
|
|
|
|
// load out the original data pointer so we can repackage
|
|
|
|
// it.
|
|
|
|
let (base, extra) = operand.get_fat_ptr();
|
|
|
|
(base, Some(extra))
|
|
|
|
} else {
|
|
|
|
(operand.llval, None)
|
|
|
|
};
|
|
|
|
|
|
|
|
let unsized_ty = cast_ty.builtin_deref(true, ty::NoPreference)
|
|
|
|
.expect("consts: unsizing got non-pointer target type").ty;
|
|
|
|
let ptr_ty = type_of::in_memory_type_of(self.ccx, unsized_ty).ptr_to();
|
|
|
|
let base = consts::ptrcast(base, ptr_ty);
|
|
|
|
let info = base::unsized_info(self.ccx, pointee_ty,
|
|
|
|
unsized_ty, old_info);
|
|
|
|
|
|
|
|
if old_info.is_none() {
|
|
|
|
let prev_const = self.ccx.const_unsized().borrow_mut()
|
|
|
|
.insert(base, operand.llval);
|
|
|
|
assert!(prev_const.is_none() || prev_const == Some(operand.llval));
|
|
|
|
}
|
|
|
|
assert_eq!(abi::FAT_PTR_ADDR, 0);
|
|
|
|
assert_eq!(abi::FAT_PTR_EXTRA, 1);
|
|
|
|
C_struct(self.ccx, &[base, info], false)
|
|
|
|
}
|
|
|
|
mir::CastKind::Misc if common::type_is_immediate(self.ccx, operand.ty) => {
|
|
|
|
debug_assert!(common::type_is_immediate(self.ccx, cast_ty));
|
|
|
|
let r_t_in = CastTy::from_ty(operand.ty).expect("bad input type for cast");
|
|
|
|
let r_t_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
|
|
|
|
let ll_t_out = type_of::immediate_type_of(self.ccx, cast_ty);
|
|
|
|
let llval = operand.llval;
|
|
|
|
let signed = if let CastTy::Int(IntTy::CEnum) = r_t_in {
|
2016-08-28 20:44:19 -04:00
|
|
|
let l = self.ccx.layout_of(operand.ty);
|
|
|
|
adt::is_discr_signed(&l)
|
2016-04-21 16:15:56 +03:00
|
|
|
} else {
|
|
|
|
operand.ty.is_signed()
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
match (r_t_in, r_t_out) {
|
|
|
|
(CastTy::Int(_), CastTy::Int(_)) => {
|
|
|
|
let s = signed as llvm::Bool;
|
|
|
|
llvm::LLVMConstIntCast(llval, ll_t_out.to_ref(), s)
|
|
|
|
}
|
|
|
|
(CastTy::Int(_), CastTy::Float) => {
|
|
|
|
if signed {
|
|
|
|
llvm::LLVMConstSIToFP(llval, ll_t_out.to_ref())
|
|
|
|
} else {
|
|
|
|
llvm::LLVMConstUIToFP(llval, ll_t_out.to_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(CastTy::Float, CastTy::Float) => {
|
|
|
|
llvm::LLVMConstFPCast(llval, ll_t_out.to_ref())
|
|
|
|
}
|
|
|
|
(CastTy::Float, CastTy::Int(IntTy::I)) => {
|
|
|
|
llvm::LLVMConstFPToSI(llval, ll_t_out.to_ref())
|
|
|
|
}
|
|
|
|
(CastTy::Float, CastTy::Int(_)) => {
|
|
|
|
llvm::LLVMConstFPToUI(llval, ll_t_out.to_ref())
|
|
|
|
}
|
|
|
|
(CastTy::Ptr(_), CastTy::Ptr(_)) |
|
|
|
|
(CastTy::FnPtr, CastTy::Ptr(_)) |
|
|
|
|
(CastTy::RPtr(_), CastTy::Ptr(_)) => {
|
|
|
|
consts::ptrcast(llval, ll_t_out)
|
|
|
|
}
|
|
|
|
(CastTy::Int(_), CastTy::Ptr(_)) => {
|
|
|
|
llvm::LLVMConstIntToPtr(llval, ll_t_out.to_ref())
|
|
|
|
}
|
|
|
|
(CastTy::Ptr(_), CastTy::Int(_)) |
|
|
|
|
(CastTy::FnPtr, CastTy::Int(_)) => {
|
|
|
|
llvm::LLVMConstPtrToInt(llval, ll_t_out.to_ref())
|
|
|
|
}
|
|
|
|
_ => bug!("unsupported cast: {:?} to {:?}", operand.ty, cast_ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mir::CastKind::Misc => { // Casts from a fat-ptr.
|
|
|
|
let ll_cast_ty = type_of::immediate_type_of(self.ccx, cast_ty);
|
|
|
|
let ll_from_ty = type_of::immediate_type_of(self.ccx, operand.ty);
|
2016-12-18 11:50:07 -07:00
|
|
|
if common::type_is_fat_ptr(self.ccx, operand.ty) {
|
2016-04-21 16:15:56 +03:00
|
|
|
let (data_ptr, meta_ptr) = operand.get_fat_ptr();
|
2016-12-18 11:50:07 -07:00
|
|
|
if common::type_is_fat_ptr(self.ccx, cast_ty) {
|
2016-04-21 16:15:56 +03:00
|
|
|
let ll_cft = ll_cast_ty.field_types();
|
|
|
|
let ll_fft = ll_from_ty.field_types();
|
|
|
|
let data_cast = consts::ptrcast(data_ptr, ll_cft[0]);
|
|
|
|
assert_eq!(ll_cft[1].kind(), ll_fft[1].kind());
|
|
|
|
C_struct(self.ccx, &[data_cast, meta_ptr], false)
|
|
|
|
} else { // cast to thin-ptr
|
|
|
|
// Cast of fat-ptr to thin-ptr is an extraction of data-ptr and
|
|
|
|
// pointer-cast of that pointer to desired pointer type.
|
|
|
|
consts::ptrcast(data_ptr, ll_cast_ty)
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-25 11:55:44 +03:00
|
|
|
bug!("Unexpected non-fat-pointer operand")
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Const::new(val, cast_ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
mir::Rvalue::Ref(_, bk, ref lvalue) => {
|
|
|
|
let tr_lvalue = self.const_lvalue(lvalue, span)?;
|
|
|
|
|
|
|
|
let ty = tr_lvalue.ty;
|
2016-06-01 15:10:44 +03:00
|
|
|
let ref_ty = tcx.mk_ref(tcx.mk_region(ty::ReErased),
|
2016-04-21 16:15:56 +03:00
|
|
|
ty::TypeAndMut { ty: ty, mutbl: bk.to_mutbl_lossy() });
|
|
|
|
|
|
|
|
let base = match tr_lvalue.base {
|
|
|
|
Base::Value(llval) => {
|
2016-09-25 19:55:43 -04:00
|
|
|
// FIXME: may be wrong for &*(&simd_vec as &fmt::Debug)
|
2016-12-18 11:50:07 -07:00
|
|
|
let align = if self.ccx.shared().type_is_sized(ty) {
|
2016-08-28 20:44:19 -04:00
|
|
|
type_of::align_of(self.ccx, ty)
|
|
|
|
} else {
|
|
|
|
self.ccx.tcx().data_layout.pointer_align.abi() as machine::llalign
|
|
|
|
};
|
2016-04-21 16:15:56 +03:00
|
|
|
if bk == mir::BorrowKind::Mut {
|
|
|
|
consts::addr_of_mut(self.ccx, llval, align, "ref_mut")
|
|
|
|
} else {
|
|
|
|
consts::addr_of(self.ccx, llval, align, "ref")
|
|
|
|
}
|
|
|
|
}
|
2016-05-08 00:28:52 +03:00
|
|
|
Base::Str(llval) |
|
2016-04-21 16:15:56 +03:00
|
|
|
Base::Static(llval) => llval
|
|
|
|
};
|
|
|
|
|
2016-12-18 11:50:07 -07:00
|
|
|
let ptr = if self.ccx.shared().type_is_sized(ty) {
|
2016-04-21 16:15:56 +03:00
|
|
|
base
|
|
|
|
} else {
|
|
|
|
C_struct(self.ccx, &[base, tr_lvalue.llextra], false)
|
|
|
|
};
|
|
|
|
Const::new(ptr, ref_ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
mir::Rvalue::Len(ref lvalue) => {
|
|
|
|
let tr_lvalue = self.const_lvalue(lvalue, span)?;
|
|
|
|
Const::new(tr_lvalue.len(self.ccx), tcx.types.usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
mir::Rvalue::BinaryOp(op, ref lhs, ref rhs) => {
|
|
|
|
let lhs = self.const_operand(lhs, span)?;
|
|
|
|
let rhs = self.const_operand(rhs, span)?;
|
|
|
|
let ty = lhs.ty;
|
2016-08-08 13:35:10 -07:00
|
|
|
let binop_ty = op.ty(tcx, lhs.ty, rhs.ty);
|
2016-04-21 16:15:56 +03:00
|
|
|
let (lhs, rhs) = (lhs.llval, rhs.llval);
|
2016-05-27 14:40:05 +03:00
|
|
|
Const::new(const_scalar_binop(op, lhs, rhs, ty), binop_ty)
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
|
2016-05-27 14:40:05 +03:00
|
|
|
mir::Rvalue::CheckedBinaryOp(op, ref lhs, ref rhs) => {
|
|
|
|
let lhs = self.const_operand(lhs, span)?;
|
|
|
|
let rhs = self.const_operand(rhs, span)?;
|
|
|
|
let ty = lhs.ty;
|
2016-08-08 13:35:10 -07:00
|
|
|
let val_ty = op.ty(tcx, lhs.ty, rhs.ty);
|
2016-10-24 18:23:29 -06:00
|
|
|
let binop_ty = tcx.intern_tup(&[val_ty, tcx.types.bool]);
|
2016-05-27 14:40:05 +03:00
|
|
|
let (lhs, rhs) = (lhs.llval, rhs.llval);
|
|
|
|
assert!(!ty.is_fp());
|
2016-04-21 16:15:56 +03:00
|
|
|
|
2016-05-27 14:40:05 +03:00
|
|
|
match const_scalar_checked_binop(tcx, op, lhs, rhs, ty) {
|
|
|
|
Some((llval, of)) => {
|
|
|
|
let llof = C_bool(self.ccx, of);
|
|
|
|
Const::new(C_struct(self.ccx, &[llval, llof], false), binop_ty)
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
2016-05-27 14:40:05 +03:00
|
|
|
None => {
|
|
|
|
span_bug!(span, "{:?} got non-integer operands: {:?} and {:?}",
|
|
|
|
rvalue, Value(lhs), Value(rhs));
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mir::Rvalue::UnaryOp(op, ref operand) => {
|
|
|
|
let operand = self.const_operand(operand, span)?;
|
|
|
|
let lloperand = operand.llval;
|
|
|
|
let llval = match op {
|
|
|
|
mir::UnOp::Not => {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstNot(lloperand)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mir::UnOp::Neg => {
|
|
|
|
let is_float = operand.ty.is_fp();
|
|
|
|
unsafe {
|
|
|
|
if is_float {
|
|
|
|
llvm::LLVMConstFNeg(lloperand)
|
|
|
|
} else {
|
|
|
|
llvm::LLVMConstNeg(lloperand)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Const::new(llval, operand.ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => span_bug!(span, "{:?} in constant", rvalue)
|
|
|
|
};
|
|
|
|
|
2016-09-12 01:53:43 +03:00
|
|
|
debug!("const_rvalue({:?}: {:?} @ {:?}) = {:?}", rvalue, dest_ty, span, val);
|
|
|
|
|
2016-04-21 16:15:56 +03:00
|
|
|
Ok(val)
|
|
|
|
}
|
2016-05-27 14:40:05 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-16 17:41:38 +03:00
|
|
|
fn to_const_int(value: ValueRef, t: Ty, tcx: TyCtxt) -> Option<ConstInt> {
|
|
|
|
match t.sty {
|
|
|
|
ty::TyInt(int_type) => const_to_opt_int(value).and_then(|input| match int_type {
|
|
|
|
ast::IntTy::I8 => {
|
|
|
|
assert_eq!(input as i8 as i64, input);
|
|
|
|
Some(ConstInt::I8(input as i8))
|
|
|
|
},
|
|
|
|
ast::IntTy::I16 => {
|
|
|
|
assert_eq!(input as i16 as i64, input);
|
|
|
|
Some(ConstInt::I16(input as i16))
|
|
|
|
},
|
|
|
|
ast::IntTy::I32 => {
|
|
|
|
assert_eq!(input as i32 as i64, input);
|
|
|
|
Some(ConstInt::I32(input as i32))
|
|
|
|
},
|
|
|
|
ast::IntTy::I64 => {
|
|
|
|
Some(ConstInt::I64(input))
|
|
|
|
},
|
|
|
|
ast::IntTy::Is => {
|
|
|
|
ConstIsize::new(input, tcx.sess.target.int_type)
|
|
|
|
.ok().map(ConstInt::Isize)
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
ty::TyUint(uint_type) => const_to_opt_uint(value).and_then(|input| match uint_type {
|
|
|
|
ast::UintTy::U8 => {
|
|
|
|
assert_eq!(input as u8 as u64, input);
|
|
|
|
Some(ConstInt::U8(input as u8))
|
|
|
|
},
|
|
|
|
ast::UintTy::U16 => {
|
|
|
|
assert_eq!(input as u16 as u64, input);
|
|
|
|
Some(ConstInt::U16(input as u16))
|
|
|
|
},
|
|
|
|
ast::UintTy::U32 => {
|
|
|
|
assert_eq!(input as u32 as u64, input);
|
|
|
|
Some(ConstInt::U32(input as u32))
|
|
|
|
},
|
|
|
|
ast::UintTy::U64 => {
|
|
|
|
Some(ConstInt::U64(input))
|
|
|
|
},
|
|
|
|
ast::UintTy::Us => {
|
|
|
|
ConstUsize::new(input, tcx.sess.target.uint_type)
|
|
|
|
.ok().map(ConstInt::Usize)
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 14:40:05 +03:00
|
|
|
pub fn const_scalar_binop(op: mir::BinOp,
|
|
|
|
lhs: ValueRef,
|
|
|
|
rhs: ValueRef,
|
|
|
|
input_ty: Ty) -> ValueRef {
|
|
|
|
assert!(!input_ty.is_simd());
|
|
|
|
let is_float = input_ty.is_fp();
|
|
|
|
let signed = input_ty.is_signed();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
match op {
|
|
|
|
mir::BinOp::Add if is_float => llvm::LLVMConstFAdd(lhs, rhs),
|
|
|
|
mir::BinOp::Add => llvm::LLVMConstAdd(lhs, rhs),
|
|
|
|
|
|
|
|
mir::BinOp::Sub if is_float => llvm::LLVMConstFSub(lhs, rhs),
|
|
|
|
mir::BinOp::Sub => llvm::LLVMConstSub(lhs, rhs),
|
|
|
|
|
|
|
|
mir::BinOp::Mul if is_float => llvm::LLVMConstFMul(lhs, rhs),
|
|
|
|
mir::BinOp::Mul => llvm::LLVMConstMul(lhs, rhs),
|
|
|
|
|
|
|
|
mir::BinOp::Div if is_float => llvm::LLVMConstFDiv(lhs, rhs),
|
|
|
|
mir::BinOp::Div if signed => llvm::LLVMConstSDiv(lhs, rhs),
|
|
|
|
mir::BinOp::Div => llvm::LLVMConstUDiv(lhs, rhs),
|
|
|
|
|
|
|
|
mir::BinOp::Rem if is_float => llvm::LLVMConstFRem(lhs, rhs),
|
|
|
|
mir::BinOp::Rem if signed => llvm::LLVMConstSRem(lhs, rhs),
|
|
|
|
mir::BinOp::Rem => llvm::LLVMConstURem(lhs, rhs),
|
|
|
|
|
|
|
|
mir::BinOp::BitXor => llvm::LLVMConstXor(lhs, rhs),
|
|
|
|
mir::BinOp::BitAnd => llvm::LLVMConstAnd(lhs, rhs),
|
|
|
|
mir::BinOp::BitOr => llvm::LLVMConstOr(lhs, rhs),
|
|
|
|
mir::BinOp::Shl => {
|
|
|
|
let rhs = base::cast_shift_const_rhs(op.to_hir_binop(), lhs, rhs);
|
|
|
|
llvm::LLVMConstShl(lhs, rhs)
|
|
|
|
}
|
|
|
|
mir::BinOp::Shr => {
|
|
|
|
let rhs = base::cast_shift_const_rhs(op.to_hir_binop(), lhs, rhs);
|
|
|
|
if signed { llvm::LLVMConstAShr(lhs, rhs) }
|
|
|
|
else { llvm::LLVMConstLShr(lhs, rhs) }
|
|
|
|
}
|
|
|
|
mir::BinOp::Eq | mir::BinOp::Ne |
|
|
|
|
mir::BinOp::Lt | mir::BinOp::Le |
|
|
|
|
mir::BinOp::Gt | mir::BinOp::Ge => {
|
|
|
|
if is_float {
|
|
|
|
let cmp = base::bin_op_to_fcmp_predicate(op.to_hir_binop());
|
2016-08-03 00:25:19 +03:00
|
|
|
llvm::LLVMConstFCmp(cmp, lhs, rhs)
|
2016-05-27 14:40:05 +03:00
|
|
|
} else {
|
|
|
|
let cmp = base::bin_op_to_icmp_predicate(op.to_hir_binop(),
|
|
|
|
signed);
|
2016-08-03 00:25:19 +03:00
|
|
|
llvm::LLVMConstICmp(cmp, lhs, rhs)
|
2016-05-27 14:40:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn const_scalar_checked_binop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
op: mir::BinOp,
|
|
|
|
lllhs: ValueRef,
|
|
|
|
llrhs: ValueRef,
|
|
|
|
input_ty: Ty<'tcx>)
|
|
|
|
-> Option<(ValueRef, bool)> {
|
|
|
|
if let (Some(lhs), Some(rhs)) = (to_const_int(lllhs, input_ty, tcx),
|
|
|
|
to_const_int(llrhs, input_ty, tcx)) {
|
|
|
|
let result = match op {
|
|
|
|
mir::BinOp::Add => lhs + rhs,
|
|
|
|
mir::BinOp::Sub => lhs - rhs,
|
|
|
|
mir::BinOp::Mul => lhs * rhs,
|
|
|
|
mir::BinOp::Shl => lhs << rhs,
|
|
|
|
mir::BinOp::Shr => lhs >> rhs,
|
|
|
|
_ => {
|
|
|
|
bug!("Operator `{:?}` is not a checkable operator", op)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let of = match result {
|
|
|
|
Ok(_) => false,
|
|
|
|
Err(ConstMathErr::Overflow(_)) |
|
|
|
|
Err(ConstMathErr::ShiftNegative) => true,
|
|
|
|
Err(err) => {
|
|
|
|
bug!("Operator `{:?}` on `{:?}` and `{:?}` errored: {}",
|
|
|
|
op, lhs, rhs, err.description());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Some((const_scalar_binop(op, lllhs, llrhs, input_ty), of))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|
|
|
|
|
2016-12-17 19:54:32 -07:00
|
|
|
impl<'a, 'tcx> MirContext<'a, 'tcx> {
|
2015-10-21 17:42:25 -04:00
|
|
|
pub fn trans_constant(&mut self,
|
2016-12-17 19:54:32 -07:00
|
|
|
bcx: &BlockAndBuilder<'a, 'tcx>,
|
2015-10-21 17:42:25 -04:00
|
|
|
constant: &mir::Constant<'tcx>)
|
2016-04-26 23:54:38 +03:00
|
|
|
-> Const<'tcx>
|
2015-10-21 17:42:25 -04:00
|
|
|
{
|
2016-09-12 01:53:43 +03:00
|
|
|
debug!("trans_constant({:?})", constant);
|
2016-12-18 16:05:40 -07:00
|
|
|
let ty = self.monomorphize(&constant.ty);
|
2016-05-03 00:26:41 +03:00
|
|
|
let result = match constant.literal.clone() {
|
2016-03-06 17:32:47 +02:00
|
|
|
mir::Literal::Item { def_id, substs } => {
|
|
|
|
// Shortcut for zero-sized types, including function item
|
2016-04-21 16:15:56 +03:00
|
|
|
// types, which would not work with MirConstContext.
|
2016-03-06 17:32:47 +02:00
|
|
|
if common::type_is_zero_size(bcx.ccx(), ty) {
|
|
|
|
let llty = type_of::type_of(bcx.ccx(), ty);
|
2016-04-26 23:54:38 +03:00
|
|
|
return Const::new(C_null(llty), ty);
|
2016-03-06 17:32:47 +02:00
|
|
|
}
|
|
|
|
|
2016-12-18 16:05:40 -07:00
|
|
|
let substs = self.monomorphize(&substs);
|
2016-04-21 16:15:56 +03:00
|
|
|
let instance = Instance::new(def_id, substs);
|
2016-06-07 17:28:36 +03:00
|
|
|
MirConstContext::trans_def(bcx.ccx(), instance, IndexVec::new())
|
2016-05-03 00:26:41 +03:00
|
|
|
}
|
|
|
|
mir::Literal::Promoted { index } => {
|
|
|
|
let mir = &self.mir.promoted[index];
|
2016-12-18 16:05:40 -07:00
|
|
|
MirConstContext::new(bcx.ccx(), mir, self.param_substs, IndexVec::new()).trans()
|
2016-01-05 12:29:50 -05:00
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
mir::Literal::Value { value } => {
|
2016-05-03 00:26:41 +03:00
|
|
|
Ok(Const::from_constval(bcx.ccx(), value, ty))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-12 01:53:43 +03:00
|
|
|
let result = result.unwrap_or_else(|_| {
|
2016-08-16 17:41:38 +03:00
|
|
|
// We've errored, so we don't have to produce working code.
|
|
|
|
let llty = type_of::type_of(bcx.ccx(), ty);
|
|
|
|
Const::new(C_undef(llty), ty)
|
2016-09-12 01:53:43 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
debug!("trans_constant({:?}) = {:?}", constant, result);
|
|
|
|
result
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-21 16:15:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
pub fn trans_static_initializer(ccx: &CrateContext, def_id: DefId)
|
2016-08-16 17:41:38 +03:00
|
|
|
-> Result<ValueRef, ConstEvalErr> {
|
2016-05-11 17:11:20 -04:00
|
|
|
let instance = Instance::mono(ccx.shared(), def_id);
|
2016-06-07 17:28:36 +03:00
|
|
|
MirConstContext::trans_def(ccx, instance, IndexVec::new()).map(|c| c.llval)
|
2016-04-21 16:15:56 +03:00
|
|
|
}
|