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.
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.
12 use rustc::middle::ty::{self, Ty, HasTypeFlags};
13 use rustc_mir::repr as mir;
14 use rustc_mir::tcx::LvalueTy;
18 use trans::common::{self, Block};
19 use trans::debuginfo::DebugLoc;
24 use super::{MirContext, TempRef};
26 #[derive(Copy, Clone)]
27 pub struct LvalueRef<'tcx> {
28 /// Pointer to the contents of the lvalue
31 /// This lvalue's extra data if it is unsized, or null
32 pub llextra: ValueRef,
34 /// Monomorphized type of this lvalue, including variant information
35 pub ty: LvalueTy<'tcx>,
38 impl<'tcx> LvalueRef<'tcx> {
39 pub fn new_sized(llval: ValueRef, lvalue_ty: LvalueTy<'tcx>) -> LvalueRef<'tcx> {
40 LvalueRef { llval: llval, llextra: ptr::null_mut(), ty: lvalue_ty }
43 pub fn alloca<'bcx>(bcx: Block<'bcx, 'tcx>,
48 assert!(!ty.has_erasable_regions());
49 let lltemp = base::alloc_ty(bcx, ty, name);
50 LvalueRef::new_sized(lltemp, LvalueTy::from_ty(ty))
54 impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
55 pub fn lvalue_len(&mut self,
56 bcx: Block<'bcx, 'tcx>,
57 lvalue: LvalueRef<'tcx>)
59 match lvalue.ty.to_ty(bcx.tcx()).sty {
60 ty::TyArray(_, n) => common::C_uint(bcx.ccx(), n),
61 ty::TySlice(_) | ty::TyStr => {
62 assert!(lvalue.llextra != ptr::null_mut());
65 _ => bcx.sess().bug("unexpected type in get_base_and_len"),
69 pub fn trans_lvalue(&mut self,
70 bcx: Block<'bcx, 'tcx>,
71 lvalue: &mir::Lvalue<'tcx>)
73 debug!("trans_lvalue(lvalue={:?})", lvalue);
79 mir::Lvalue::Var(index) => self.vars[index as usize],
80 mir::Lvalue::Temp(index) => match self.temps[index as usize] {
81 TempRef::Lvalue(lvalue) =>
83 TempRef::Operand(..) =>
84 tcx.sess.bug(&format!("using operand temp {:?} as lvalue", lvalue)),
86 mir::Lvalue::Arg(index) => self.args[index as usize],
87 mir::Lvalue::Static(def_id) => {
88 let const_ty = self.mir.lvalue_ty(tcx, lvalue);
90 common::get_static_val(ccx, def_id, const_ty.to_ty(tcx)),
93 mir::Lvalue::ReturnPointer => {
94 let return_ty = bcx.monomorphize(&self.mir.return_ty);
95 let llval = fcx.get_ret_slot(bcx, return_ty, "return");
96 LvalueRef::new_sized(llval, LvalueTy::from_ty(return_ty.unwrap()))
98 mir::Lvalue::Projection(ref projection) => {
99 let tr_base = self.trans_lvalue(bcx, &projection.base);
100 let projected_ty = tr_base.ty.projection_ty(tcx, &projection.elem);
101 let (llprojected, llextra) = match projection.elem {
102 mir::ProjectionElem::Deref => {
103 let base_ty = tr_base.ty.to_ty(tcx);
104 if common::type_is_sized(tcx, projected_ty.to_ty(tcx)) {
105 (base::load_ty(bcx, tr_base.llval, base_ty),
108 base::load_fat_ptr(bcx, tr_base.llval, base_ty)
111 mir::ProjectionElem::Field(ref field) => {
112 let base_ty = tr_base.ty.to_ty(tcx);
113 let base_repr = adt::represent_type(ccx, base_ty);
114 let discr = match tr_base.ty {
115 LvalueTy::Ty { .. } => 0,
116 LvalueTy::Downcast { adt_def: _, substs: _, variant_index: v } => v,
118 let discr = discr as u64;
119 (adt::trans_field_ptr(bcx, &base_repr, tr_base.llval, discr, field.index()),
120 if common::type_is_sized(tcx, projected_ty.to_ty(tcx)) {
126 mir::ProjectionElem::Index(ref index) => {
127 let index = self.trans_operand(bcx, index);
128 let llindex = self.prepare_index(bcx, index.immediate());
129 (build::InBoundsGEP(bcx, tr_base.llval, &[llindex]),
132 mir::ProjectionElem::ConstantIndex { offset,
135 let lloffset = common::C_u32(bcx.ccx(), offset);
136 let llindex = self.prepare_index(bcx, lloffset);
137 (build::InBoundsGEP(bcx, tr_base.llval, &[llindex]),
140 mir::ProjectionElem::ConstantIndex { offset,
143 let lloffset = common::C_u32(bcx.ccx(), offset);
144 let lllen = self.lvalue_len(bcx, tr_base);
145 let llindex = build::Sub(bcx, lllen, lloffset, DebugLoc::None);
146 let llindex = self.prepare_index(bcx, llindex);
147 (build::InBoundsGEP(bcx, tr_base.llval, &[llindex]),
150 mir::ProjectionElem::Downcast(..) => {
151 (tr_base.llval, tr_base.llextra)
163 /// Adjust the bitwidth of an index since LLVM is less forgiving
166 /// nmatsakis: is this still necessary? Not sure.
167 fn prepare_index(&mut self,
168 bcx: Block<'bcx, 'tcx>,
173 let index_size = machine::llbitsize_of_real(bcx.ccx(), common::val_ty(llindex));
174 let int_size = machine::llbitsize_of_real(bcx.ccx(), ccx.int_type());
175 if index_size < int_size {
176 build::ZExt(bcx, llindex, ccx.int_type())
177 } else if index_size > int_size {
178 build::Trunc(bcx, llindex, ccx.int_type())