]>
Commit | Line | Data |
---|---|---|
92a42be0 SL |
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 | ||
9cc50fc6 SL |
11 | use back::abi; |
12 | use llvm::ValueRef; | |
13 | use middle::subst::Substs; | |
14 | use middle::ty::{Ty, TypeFoldable}; | |
92a42be0 SL |
15 | use rustc::middle::const_eval::ConstVal; |
16 | use rustc::mir::repr as mir; | |
7453a54e SL |
17 | use trans::common::{self, BlockAndBuilder, C_bool, C_bytes, C_floating_f64, C_integral, |
18 | C_str_slice}; | |
9cc50fc6 SL |
19 | use trans::consts; |
20 | use trans::expr; | |
92a42be0 SL |
21 | use trans::type_of; |
22 | ||
9cc50fc6 | 23 | use super::operand::{OperandRef, OperandValue}; |
92a42be0 SL |
24 | use super::MirContext; |
25 | ||
9cc50fc6 | 26 | |
92a42be0 SL |
27 | impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { |
28 | pub fn trans_constval(&mut self, | |
7453a54e | 29 | bcx: &BlockAndBuilder<'bcx, 'tcx>, |
92a42be0 SL |
30 | cv: &ConstVal, |
31 | ty: Ty<'tcx>) | |
32 | -> OperandRef<'tcx> | |
33 | { | |
92a42be0 | 34 | let ccx = bcx.ccx(); |
7453a54e | 35 | let val = self.trans_constval_inner(bcx, cv, ty, bcx.fcx().param_substs); |
9cc50fc6 SL |
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) | |
92a42be0 SL |
44 | }; |
45 | ||
46 | assert!(!ty.has_erasable_regions()); | |
47 | ||
48 | OperandRef { | |
49 | ty: ty, | |
50 | val: val | |
51 | } | |
52 | } | |
53 | ||
9cc50fc6 SL |
54 | /// Translate ConstVal into a bare LLVM ValueRef. |
55 | fn trans_constval_inner(&mut self, | |
7453a54e | 56 | bcx: &BlockAndBuilder<'bcx, 'tcx>, |
9cc50fc6 SL |
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); | |
7453a54e SL |
74 | bcx.with_block(|bcx| { |
75 | expr::trans(bcx, expr).datum.val | |
76 | }) | |
9cc50fc6 SL |
77 | }, |
78 | ConstVal::Function(did) => | |
79 | self.trans_fn_ref(bcx, ty, param_substs, did).immediate() | |
80 | } | |
81 | } | |
82 | ||
92a42be0 | 83 | pub fn trans_constant(&mut self, |
7453a54e | 84 | bcx: &BlockAndBuilder<'bcx, 'tcx>, |
92a42be0 SL |
85 | constant: &mir::Constant<'tcx>) |
86 | -> OperandRef<'tcx> | |
87 | { | |
92a42be0 | 88 | match constant.literal { |
9cc50fc6 SL |
89 | mir::Literal::Item { def_id, kind, substs } => { |
90 | let substs = bcx.tcx().mk_substs(bcx.monomorphize(&substs)); | |
7453a54e | 91 | self.trans_item_ref(bcx, constant.ty, kind, substs, def_id) |
92a42be0 SL |
92 | } |
93 | mir::Literal::Value { ref value } => { | |
7453a54e | 94 | let ty = bcx.monomorphize(&constant.ty); |
9cc50fc6 | 95 | self.trans_constval(bcx, value, ty) |
92a42be0 SL |
96 | } |
97 | } | |
98 | } | |
99 | } |