From 9a63bb6661929ec7f3dbbd9aefcd7716220e83d6 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 27 Nov 2015 17:18:28 +0100 Subject: [PATCH] move ConstVal -> ValueRef translation to trans::consts --- src/librustc_trans/trans/consts.rs | 34 +++++++++++++++++ src/librustc_trans/trans/mir/constant.rs | 48 +++--------------------- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/librustc_trans/trans/consts.rs b/src/librustc_trans/trans/consts.rs index 6f40283064b..5284911340b 100644 --- a/src/librustc_trans/trans/consts.rs +++ b/src/librustc_trans/trans/consts.rs @@ -31,6 +31,7 @@ use trans::{adt, closure, debuginfo, expr, inline, machine}; use trans::base::{self, push_ctxt}; use trans::common::{self, type_is_sized, ExprOrMethodCall, node_id_substs, C_nil, const_get_elt}; use trans::common::{CrateContext, C_integral, C_floating, C_bool, C_str_slice, C_bytes, val_ty}; +use trans::common::C_floating_f64; use trans::common::{C_struct, C_undef, const_to_opt_int, const_to_opt_uint, VariantInfo, C_uint}; use trans::common::{type_is_fat_ptr, Field, C_vector, C_array, C_null, ExprId, MethodCallKey}; use trans::declare; @@ -107,6 +108,39 @@ pub fn const_lit(cx: &CrateContext, e: &hir::Expr, lit: &ast::Lit) } } +pub fn trans_constval<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, + cv: &ConstVal, + ty: Ty<'tcx>, + param_substs: &'tcx Substs<'tcx>) + -> ValueRef +{ + 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) => addr_of(ccx, C_bytes(ccx, v), 1, "byte_str"), + ConstVal::Struct(id) | ConstVal::Tuple(id) => { + let expr = ccx.tcx().map.expect_expr(id); + match const_expr(ccx, expr, param_substs, None, TrueConst::Yes) { + Ok((val, _)) => val, + Err(e) => panic!("const eval failure: {}", e.description()), + } + }, + ConstVal::Function(_) => { + unimplemented!() + }, + ConstVal::Array(..) => { + unimplemented!() + }, + ConstVal::Repeat(..) => { + unimplemented!() + }, + } +} + pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef { unsafe { llvm::LLVMConstPointerCast(val, ty.to_ref()) diff --git a/src/librustc_trans/trans/mir/constant.rs b/src/librustc_trans/trans/mir/constant.rs index 4de586543e9..9c23d330136 100644 --- a/src/librustc_trans/trans/mir/constant.rs +++ b/src/librustc_trans/trans/mir/constant.rs @@ -11,10 +11,8 @@ use middle::ty::{Ty, HasTypeFlags}; use rustc::middle::const_eval::ConstVal; use rustc::mir::repr as mir; -use trans::consts::{self, TrueConst}; +use trans::consts; use trans::common::{self, Block}; -use trans::common::{C_bool, C_bytes, C_floating_f64, C_integral, C_str_slice}; -use trans::type_of; use super::operand::OperandRef; use super::MirContext; @@ -29,45 +27,11 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { use super::operand::OperandValue::{Ref, Immediate}; let ccx = bcx.ccx(); - let llty = type_of::type_of(ccx, ty); - let val = match *cv { - 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())), - ConstVal::ByteStr(ref v) => { - Immediate(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); - let (llval, _) = match consts::const_expr(ccx, - expr, - bcx.fcx.param_substs, - None, - TrueConst::Yes) { - Ok(v) => v, - Err(_) => panic!("constant eval failure"), - }; - if common::type_is_immediate(bcx.ccx(), ty) { - Immediate(llval) - } else { - Ref(llval) - } - } - ConstVal::Function(_) => { - unimplemented!() - } - ConstVal::Array(..) => { - unimplemented!() - } - ConstVal::Repeat(..) => { - unimplemented!() - } + let val = consts::trans_constval(ccx, cv, ty, bcx.fcx.param_substs); + let val = if common::type_is_immediate(ccx, ty) { + Immediate(val) + } else { + Ref(val) }; assert!(!ty.has_erasable_regions());