]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/trans/mir/constant.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / librustc_trans / trans / mir / constant.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use back::abi;
12 use llvm::ValueRef;
13 use middle::subst::Substs;
14 use middle::ty::{Ty, TypeFoldable};
15 use rustc::middle::const_eval::ConstVal;
16 use rustc::mir::repr as mir;
17 use trans::common::{self, BlockAndBuilder, C_bool, C_bytes, C_floating_f64, C_integral,
18 C_str_slice};
19 use trans::consts;
20 use trans::expr;
21 use trans::type_of;
22
23 use super::operand::{OperandRef, OperandValue};
24 use super::MirContext;
25
26
27 impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
28 pub fn trans_constval(&mut self,
29 bcx: &BlockAndBuilder<'bcx, 'tcx>,
30 cv: &ConstVal,
31 ty: Ty<'tcx>)
32 -> OperandRef<'tcx>
33 {
34 let ccx = bcx.ccx();
35 let val = self.trans_constval_inner(bcx, cv, ty, bcx.fcx().param_substs);
36 let val = if common::type_is_immediate(ccx, ty) {
37 OperandValue::Immediate(val)
38 } else if common::type_is_fat_ptr(bcx.tcx(), ty) {
39 let data = common::const_get_elt(ccx, val, &[abi::FAT_PTR_ADDR as u32]);
40 let extra = common::const_get_elt(ccx, val, &[abi::FAT_PTR_EXTRA as u32]);
41 OperandValue::FatPtr(data, extra)
42 } else {
43 OperandValue::Ref(val)
44 };
45
46 assert!(!ty.has_erasable_regions());
47
48 OperandRef {
49 ty: ty,
50 val: val
51 }
52 }
53
54 /// Translate ConstVal into a bare LLVM ValueRef.
55 fn trans_constval_inner(&mut self,
56 bcx: &BlockAndBuilder<'bcx, 'tcx>,
57 cv: &ConstVal,
58 ty: Ty<'tcx>,
59 param_substs: &'tcx Substs<'tcx>)
60 -> ValueRef
61 {
62 let ccx = bcx.ccx();
63 let llty = type_of::type_of(ccx, ty);
64 match *cv {
65 ConstVal::Float(v) => C_floating_f64(v, llty),
66 ConstVal::Bool(v) => C_bool(ccx, v),
67 ConstVal::Int(v) => C_integral(llty, v as u64, true),
68 ConstVal::Uint(v) => C_integral(llty, v, false),
69 ConstVal::Str(ref v) => C_str_slice(ccx, v.clone()),
70 ConstVal::ByteStr(ref v) => consts::addr_of(ccx, C_bytes(ccx, v), 1, "byte_str"),
71 ConstVal::Struct(id) | ConstVal::Tuple(id) |
72 ConstVal::Array(id, _) | ConstVal::Repeat(id, _) => {
73 let expr = bcx.tcx().map.expect_expr(id);
74 bcx.with_block(|bcx| {
75 expr::trans(bcx, expr).datum.val
76 })
77 },
78 ConstVal::Function(did) =>
79 self.trans_fn_ref(bcx, ty, param_substs, did).immediate()
80 }
81 }
82
83 pub fn trans_constant(&mut self,
84 bcx: &BlockAndBuilder<'bcx, 'tcx>,
85 constant: &mir::Constant<'tcx>)
86 -> OperandRef<'tcx>
87 {
88 match constant.literal {
89 mir::Literal::Item { def_id, kind, substs } => {
90 let substs = bcx.tcx().mk_substs(bcx.monomorphize(&substs));
91 self.trans_item_ref(bcx, constant.ty, kind, substs, def_id)
92 }
93 mir::Literal::Value { ref value } => {
94 let ty = bcx.monomorphize(&constant.ty);
95 self.trans_constval(bcx, value, ty)
96 }
97 }
98 }
99 }