2015-10-21 16:42:25 -05: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.
|
|
|
|
|
2015-12-28 13:18:24 -06:00
|
|
|
use llvm::ValueRef;
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::{Ty, TypeFoldable};
|
2016-03-30 06:43:36 -05:00
|
|
|
use rustc::middle::const_val::ConstVal;
|
2016-03-15 06:33:13 -05:00
|
|
|
use rustc_const_math::ConstInt::*;
|
2016-03-30 06:43:36 -05:00
|
|
|
use rustc_const_eval::lookup_const_by_id;
|
2015-11-19 09:37:34 -06:00
|
|
|
use rustc::mir::repr as mir;
|
2016-03-22 12:23:36 -05:00
|
|
|
use abi;
|
|
|
|
use common::{self, BlockAndBuilder, C_bool, C_bytes, C_floating_f64, C_integral,
|
2016-03-08 06:15:23 -06:00
|
|
|
C_str_slice, C_undef};
|
2016-03-22 12:23:36 -05:00
|
|
|
use consts;
|
|
|
|
use datum;
|
|
|
|
use expr;
|
|
|
|
use type_of;
|
|
|
|
use type_::Type;
|
2015-11-16 11:57:57 -06:00
|
|
|
|
|
|
|
use super::operand::{OperandRef, OperandValue};
|
2015-10-21 16:42:25 -05:00
|
|
|
use super::MirContext;
|
|
|
|
|
2016-01-07 17:15:59 -06:00
|
|
|
|
2015-10-21 16:42:25 -05:00
|
|
|
impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
|
2015-11-08 12:11:11 -06:00
|
|
|
pub fn trans_constval(&mut self,
|
2016-02-01 04:04:46 -06:00
|
|
|
bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
2015-11-08 12:11:11 -06:00
|
|
|
cv: &ConstVal,
|
|
|
|
ty: Ty<'tcx>)
|
2015-11-10 14:05:11 -06:00
|
|
|
-> OperandRef<'tcx>
|
2015-11-08 12:11:11 -06:00
|
|
|
{
|
|
|
|
let ccx = bcx.ccx();
|
2016-03-06 09:32:47 -06:00
|
|
|
let val = self.trans_constval_inner(bcx, cv, ty);
|
2015-11-27 10:18:28 -06:00
|
|
|
let val = if common::type_is_immediate(ccx, ty) {
|
2015-12-05 12:35:00 -06:00
|
|
|
OperandValue::Immediate(val)
|
2015-12-19 08:32:08 -06:00
|
|
|
} else if common::type_is_fat_ptr(bcx.tcx(), ty) {
|
2016-02-18 11:49:45 -06:00
|
|
|
let data = common::const_get_elt(val, &[abi::FAT_PTR_ADDR as u32]);
|
|
|
|
let extra = common::const_get_elt(val, &[abi::FAT_PTR_EXTRA as u32]);
|
2015-12-19 08:32:08 -06:00
|
|
|
OperandValue::FatPtr(data, extra)
|
2015-11-27 10:18:28 -06:00
|
|
|
} else {
|
2015-12-05 12:35:00 -06:00
|
|
|
OperandValue::Ref(val)
|
2015-11-10 14:05:11 -06:00
|
|
|
};
|
2015-11-16 11:41:16 -06:00
|
|
|
|
|
|
|
assert!(!ty.has_erasable_regions());
|
|
|
|
|
2015-11-10 14:05:11 -06:00
|
|
|
OperandRef {
|
|
|
|
ty: ty,
|
|
|
|
val: val
|
2015-11-08 12:11:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 13:18:24 -06:00
|
|
|
/// Translate ConstVal into a bare LLVM ValueRef.
|
|
|
|
fn trans_constval_inner(&mut self,
|
2016-02-01 04:04:46 -06:00
|
|
|
bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
2015-12-28 13:18:24 -06:00
|
|
|
cv: &ConstVal,
|
2016-03-06 09:32:47 -06:00
|
|
|
ty: Ty<'tcx>)
|
2015-12-28 13:18:24 -06:00
|
|
|
-> ValueRef
|
|
|
|
{
|
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let llty = type_of::type_of(ccx, ty);
|
|
|
|
match *cv {
|
|
|
|
ConstVal::Float(v) => C_floating_f64(v, llty),
|
|
|
|
ConstVal::Bool(v) => C_bool(ccx, v),
|
2015-12-16 11:44:15 -06: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)
|
|
|
|
},
|
|
|
|
ConstVal::Integral(Infer(v)) => C_integral(llty, v as u64, false),
|
|
|
|
ConstVal::Integral(InferSigned(v)) => C_integral(llty, v as u64, true),
|
2015-12-28 13:18:24 -06: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 06:15:23 -06:00
|
|
|
ConstVal::Struct(_) | ConstVal::Tuple(_) |
|
|
|
|
ConstVal::Array(..) | ConstVal::Repeat(..) |
|
|
|
|
ConstVal::Function(_) => {
|
2016-03-28 18:46:02 -05:00
|
|
|
bug!("MIR must not use {:?} (which refers to a local ID)", cv)
|
2016-03-08 06:15:23 -06:00
|
|
|
}
|
2015-12-16 11:44:15 -06:00
|
|
|
ConstVal::Char(c) => C_integral(Type::char(ccx), c as u64, false),
|
2016-03-28 18:46:02 -05:00
|
|
|
ConstVal::Dummy => bug!(),
|
2015-12-28 13:18:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:42:25 -05:00
|
|
|
pub fn trans_constant(&mut self,
|
2016-02-01 04:04:46 -06:00
|
|
|
bcx: &BlockAndBuilder<'bcx, 'tcx>,
|
2015-10-21 16:42:25 -05:00
|
|
|
constant: &mir::Constant<'tcx>)
|
2015-11-10 14:05:11 -06:00
|
|
|
-> OperandRef<'tcx>
|
2015-10-21 16:42:25 -05:00
|
|
|
{
|
2016-03-06 09:32:47 -06:00
|
|
|
let ty = bcx.monomorphize(&constant.ty);
|
2015-10-21 16:42:25 -05:00
|
|
|
match constant.literal {
|
2016-03-06 09:32:47 -06:00
|
|
|
mir::Literal::Item { def_id, substs } => {
|
|
|
|
// Shortcut for zero-sized types, including function item
|
|
|
|
// types, which would not work with lookup_const_by_id.
|
|
|
|
if common::type_is_zero_size(bcx.ccx(), ty) {
|
|
|
|
let llty = type_of::type_of(bcx.ccx(), ty);
|
|
|
|
return OperandRef {
|
|
|
|
val: OperandValue::Immediate(C_undef(llty)),
|
|
|
|
ty: ty
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:04:55 -06:00
|
|
|
let substs = Some(bcx.monomorphize(substs));
|
2016-03-30 06:43:36 -05:00
|
|
|
let expr = lookup_const_by_id(bcx.tcx(), def_id, substs)
|
2015-12-16 11:44:15 -06:00
|
|
|
.expect("def was const, but lookup_const_by_id failed").0;
|
2016-03-06 09:32:47 -06:00
|
|
|
// FIXME: this is falling back to translating from HIR. This is not easy to fix,
|
|
|
|
// because we would have somehow adapt const_eval to work on MIR rather than HIR.
|
|
|
|
let d = bcx.with_block(|bcx| {
|
|
|
|
expr::trans(bcx, expr)
|
|
|
|
});
|
2016-03-08 06:23:23 -06:00
|
|
|
|
|
|
|
let datum = d.datum.to_rvalue_datum(d.bcx, "").datum;
|
|
|
|
|
|
|
|
match datum.kind.mode {
|
|
|
|
datum::RvalueMode::ByValue => {
|
|
|
|
OperandRef {
|
|
|
|
ty: datum.ty,
|
|
|
|
val: OperandValue::Immediate(datum.val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
datum::RvalueMode::ByRef => self.trans_load(bcx, datum.val, datum.ty)
|
|
|
|
}
|
2016-01-05 11:29:50 -06:00
|
|
|
}
|
2015-10-21 16:42:25 -05:00
|
|
|
mir::Literal::Value { ref value } => {
|
2015-11-16 11:57:57 -06:00
|
|
|
self.trans_constval(bcx, value, ty)
|
2015-10-21 16:42:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|