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.
|
|
|
|
|
2015-11-16 18:41:16 +01:00
|
|
|
use middle::ty::{Ty, HasTypeFlags};
|
2015-10-21 17:42:25 -04:00
|
|
|
use rustc::middle::const_eval::ConstVal;
|
|
|
|
use rustc_mir::repr as mir;
|
|
|
|
use trans::consts::{self, TrueConst};
|
|
|
|
use trans::common::{self, Block};
|
2015-11-10 22:05:11 +02:00
|
|
|
use trans::common::{C_bool, C_bytes, C_floating_f64, C_integral, C_str_slice};
|
2015-10-21 17:42:25 -04:00
|
|
|
use trans::type_of;
|
|
|
|
|
2015-11-13 00:12:50 +02:00
|
|
|
use super::operand::OperandRef;
|
2015-10-21 17:42:25 -04:00
|
|
|
use super::MirContext;
|
|
|
|
|
|
|
|
impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
|
2015-11-08 19:11:11 +01:00
|
|
|
pub fn trans_constval(&mut self,
|
|
|
|
bcx: Block<'bcx, 'tcx>,
|
|
|
|
cv: &ConstVal,
|
|
|
|
ty: Ty<'tcx>)
|
2015-11-10 22:05:11 +02:00
|
|
|
-> OperandRef<'tcx>
|
2015-11-08 19:11:11 +01:00
|
|
|
{
|
2015-11-13 00:12:50 +02:00
|
|
|
use super::operand::OperandValue::{Ref, Immediate};
|
|
|
|
|
2015-11-08 19:11:11 +01:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let llty = type_of::type_of(ccx, ty);
|
2015-11-10 22:05:11 +02:00
|
|
|
let val = match *cv {
|
2015-11-13 00:12:50 +02:00
|
|
|
ConstVal::Float(v) => Immediate(C_floating_f64(v, llty)),
|
|
|
|
ConstVal::Bool(v) => Immediate(C_bool(ccx, v)),
|
|
|
|
ConstVal::Int(v) => Immediate(C_integral(llty, v as u64, true)),
|
|
|
|
ConstVal::Uint(v) => Immediate(C_integral(llty, v, false)),
|
|
|
|
ConstVal::Str(ref v) => Immediate(C_str_slice(ccx, v.clone())),
|
2015-11-10 22:05:11 +02:00
|
|
|
ConstVal::ByteStr(ref v) => {
|
2015-11-13 00:12:50 +02:00
|
|
|
Immediate(consts::addr_of(ccx,
|
|
|
|
C_bytes(ccx, v),
|
|
|
|
1,
|
|
|
|
"byte_str"))
|
2015-11-10 22:05:11 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 19:11:11 +01:00
|
|
|
ConstVal::Struct(id) | ConstVal::Tuple(id) => {
|
|
|
|
let expr = bcx.tcx().map.expect_expr(id);
|
|
|
|
let (llval, _) = match consts::const_expr(ccx,
|
|
|
|
expr,
|
|
|
|
bcx.fcx.param_substs,
|
|
|
|
None,
|
|
|
|
TrueConst::Yes) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(_) => panic!("constant eval failure"),
|
|
|
|
};
|
2015-11-10 22:05:11 +02:00
|
|
|
if common::type_is_immediate(bcx.ccx(), ty) {
|
2015-11-13 00:12:50 +02:00
|
|
|
Immediate(llval)
|
2015-11-10 22:05:11 +02:00
|
|
|
} else {
|
2015-11-13 00:12:50 +02:00
|
|
|
Ref(llval)
|
2015-11-10 22:05:11 +02:00
|
|
|
}
|
2015-11-08 19:11:11 +01:00
|
|
|
}
|
|
|
|
ConstVal::Function(_) => {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2015-11-10 22:05:11 +02:00
|
|
|
};
|
2015-11-16 18:41:16 +01:00
|
|
|
|
|
|
|
assert!(!ty.has_erasable_regions());
|
|
|
|
|
2015-11-10 22:05:11 +02:00
|
|
|
OperandRef {
|
|
|
|
ty: ty,
|
|
|
|
val: val
|
2015-11-08 19:11:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:42:25 -04:00
|
|
|
pub fn trans_constant(&mut self,
|
|
|
|
bcx: Block<'bcx, 'tcx>,
|
|
|
|
constant: &mir::Constant<'tcx>)
|
2015-11-10 22:05:11 +02:00
|
|
|
-> OperandRef<'tcx>
|
2015-10-21 17:42:25 -04:00
|
|
|
{
|
|
|
|
let constant_ty = bcx.monomorphize(&constant.ty);
|
|
|
|
match constant.literal {
|
|
|
|
mir::Literal::Item { .. } => {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
mir::Literal::Value { ref value } => {
|
2015-11-08 19:11:11 +01:00
|
|
|
self.trans_constval(bcx, value, constant_ty)
|
2015-10-21 17:42:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|