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-19 08:32:08 -06:00
|
|
|
use back::abi;
|
2015-12-28 13:18:24 -06:00
|
|
|
use llvm::ValueRef;
|
|
|
|
use middle::subst::Substs;
|
2015-11-16 11:41:16 -06:00
|
|
|
use middle::ty::{Ty, HasTypeFlags};
|
2015-10-21 16:42:25 -05:00
|
|
|
use rustc::middle::const_eval::ConstVal;
|
2015-11-19 09:37:34 -06:00
|
|
|
use rustc::mir::repr as mir;
|
2015-12-28 13:18:24 -06:00
|
|
|
use trans::common::{self, Block, C_bool, C_bytes, C_floating_f64, C_integral, C_str_slice};
|
|
|
|
use trans::consts::{self, TrueConst};
|
|
|
|
use trans::{type_of, expr};
|
2015-10-21 16:42:25 -05:00
|
|
|
|
2015-11-16 11:57:57 -06:00
|
|
|
|
|
|
|
use super::operand::{OperandRef, OperandValue};
|
2015-10-21 16:42:25 -05:00
|
|
|
use super::MirContext;
|
|
|
|
|
|
|
|
impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
|
2015-11-08 12:11:11 -06:00
|
|
|
pub fn trans_constval(&mut self,
|
|
|
|
bcx: Block<'bcx, 'tcx>,
|
|
|
|
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();
|
2015-12-28 13:18:24 -06:00
|
|
|
let val = self.trans_constval_inner(bcx, cv, ty, bcx.fcx.param_substs);
|
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) {
|
|
|
|
let data = common::const_get_elt(ccx, val, &[abi::FAT_PTR_ADDR as u32]);
|
|
|
|
let extra = common::const_get_elt(ccx, val, &[abi::FAT_PTR_EXTRA as u32]);
|
|
|
|
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,
|
|
|
|
bcx: common::Block<'bcx, 'tcx>,
|
|
|
|
cv: &ConstVal,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
param_substs: &'tcx Substs<'tcx>)
|
|
|
|
-> 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),
|
|
|
|
ConstVal::Int(v) => C_integral(llty, v as u64, true),
|
|
|
|
ConstVal::Uint(v) => C_integral(llty, v, false),
|
|
|
|
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"),
|
|
|
|
ConstVal::Struct(id) | ConstVal::Tuple(id) => {
|
|
|
|
let expr = bcx.tcx().map.expect_expr(id);
|
|
|
|
match consts::const_expr(ccx, expr, param_substs, None, TrueConst::Yes) {
|
|
|
|
Ok((val, _)) => val,
|
|
|
|
Err(e) => panic!("const eval failure: {}", e.description()),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ConstVal::Array(id, _) | ConstVal::Repeat(id, _) => {
|
|
|
|
let expr = bcx.tcx().map.expect_expr(id);
|
|
|
|
expr::trans(bcx, expr).datum.val
|
|
|
|
},
|
|
|
|
ConstVal::Function(did) =>
|
|
|
|
self.trans_fn_ref(bcx, ty, param_substs, did).immediate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:42:25 -05:00
|
|
|
pub fn trans_constant(&mut self,
|
|
|
|
bcx: Block<'bcx, 'tcx>,
|
|
|
|
constant: &mir::Constant<'tcx>)
|
2015-11-10 14:05:11 -06:00
|
|
|
-> OperandRef<'tcx>
|
2015-10-21 16:42:25 -05:00
|
|
|
{
|
2015-11-16 11:57:57 -06:00
|
|
|
let ty = bcx.monomorphize(&constant.ty);
|
2015-10-21 16:42:25 -05:00
|
|
|
match constant.literal {
|
2015-11-16 11:57:57 -06:00
|
|
|
mir::Literal::Item { def_id, kind, substs } =>
|
|
|
|
self.trans_item_ref(bcx, ty, kind, substs, def_id),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|