|
| 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 llvm::ValueRef; |
| 12 | +use rustc::middle::ty::Ty; |
| 13 | +use rustc_mir::repr as mir; |
| 14 | +use rustc_mir::tcx::LvalueTy; |
| 15 | +use trans::adt; |
| 16 | +use trans::base; |
| 17 | +use trans::build; |
| 18 | +use trans::common::{self, Block}; |
| 19 | +use trans::debuginfo::DebugLoc; |
| 20 | +use trans::machine; |
| 21 | +use trans::tvec; |
| 22 | + |
| 23 | +use super::MirContext; |
| 24 | + |
| 25 | +#[derive(Copy, Clone)] |
| 26 | +pub struct LvalueRef<'tcx> { |
| 27 | + /// Pointer to the contents of the lvalue |
| 28 | + pub llval: ValueRef, |
| 29 | + |
| 30 | + /// Monomorphized type of this lvalue, including variant information |
| 31 | + pub ty: LvalueTy<'tcx>, |
| 32 | +} |
| 33 | + |
| 34 | +impl<'tcx> LvalueRef<'tcx> { |
| 35 | + pub fn new(llval: ValueRef, lvalue_ty: LvalueTy<'tcx>) -> LvalueRef<'tcx> { |
| 36 | + LvalueRef { llval: llval, ty: lvalue_ty } |
| 37 | + } |
| 38 | + |
| 39 | + pub fn alloca<'bcx>(bcx: Block<'bcx, 'tcx>, |
| 40 | + ty: Ty<'tcx>, |
| 41 | + name: &str) |
| 42 | + -> LvalueRef<'tcx> |
| 43 | + { |
| 44 | + let lltemp = base::alloc_ty(bcx, ty, name); |
| 45 | + LvalueRef::new(lltemp, LvalueTy::from_ty(ty)) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { |
| 50 | + pub fn trans_lvalue(&mut self, |
| 51 | + bcx: Block<'bcx, 'tcx>, |
| 52 | + lvalue: &mir::Lvalue<'tcx>) |
| 53 | + -> LvalueRef<'tcx> { |
| 54 | + debug!("trans_lvalue(lvalue={:?})", lvalue); |
| 55 | + |
| 56 | + let fcx = bcx.fcx; |
| 57 | + let ccx = fcx.ccx; |
| 58 | + let tcx = bcx.tcx(); |
| 59 | + match *lvalue { |
| 60 | + mir::Lvalue::Var(index) => self.vars[index as usize], |
| 61 | + mir::Lvalue::Temp(index) => self.temps[index as usize], |
| 62 | + mir::Lvalue::Arg(index) => self.args[index as usize], |
| 63 | + mir::Lvalue::Static(_def_id) => unimplemented!(), |
| 64 | + mir::Lvalue::ReturnPointer => { |
| 65 | + let return_ty = bcx.monomorphize(&self.mir.return_ty); |
| 66 | + let llval = fcx.get_ret_slot(bcx, return_ty, "return"); |
| 67 | + LvalueRef::new(llval, LvalueTy::from_ty(return_ty.unwrap())) |
| 68 | + } |
| 69 | + mir::Lvalue::Projection(ref projection) => { |
| 70 | + let tr_base = self.trans_lvalue(bcx, &projection.base); |
| 71 | + let projected_ty = tr_base.ty.projection_ty(tcx, &projection.elem); |
| 72 | + let llprojected = match projection.elem { |
| 73 | + mir::ProjectionElem::Deref => { |
| 74 | + let base_ty = tr_base.ty.to_ty(tcx); |
| 75 | + base::load_ty(bcx, tr_base.llval, base_ty) |
| 76 | + } |
| 77 | + mir::ProjectionElem::Field(ref field) => { |
| 78 | + let base_ty = tr_base.ty.to_ty(tcx); |
| 79 | + let base_repr = adt::represent_type(ccx, base_ty); |
| 80 | + let discr = match tr_base.ty { |
| 81 | + LvalueTy::Ty { .. } => 0, |
| 82 | + LvalueTy::Downcast { adt_def: _, substs: _, variant_index: v } => v, |
| 83 | + }; |
| 84 | + let discr = discr as u64; |
| 85 | + adt::trans_field_ptr(bcx, &base_repr, tr_base.llval, discr, field.index()) |
| 86 | + } |
| 87 | + mir::ProjectionElem::Index(ref index) => { |
| 88 | + let base_ty = tr_base.ty.to_ty(tcx); |
| 89 | + let index = self.trans_operand(bcx, index); |
| 90 | + let llindex = self.prepare_index(bcx, index.llval); |
| 91 | + let (llbase, _) = tvec::get_base_and_len(bcx, tr_base.llval, base_ty); |
| 92 | + build::InBoundsGEP(bcx, llbase, &[llindex]) |
| 93 | + } |
| 94 | + mir::ProjectionElem::ConstantIndex { offset, |
| 95 | + from_end: false, |
| 96 | + min_length: _ } => { |
| 97 | + let base_ty = tr_base.ty.to_ty(tcx); |
| 98 | + let lloffset = common::C_u32(bcx.ccx(), offset); |
| 99 | + let llindex = self.prepare_index(bcx, lloffset); |
| 100 | + let (llbase, _) = tvec::get_base_and_len(bcx, |
| 101 | + tr_base.llval, |
| 102 | + base_ty); |
| 103 | + build::InBoundsGEP(bcx, llbase, &[llindex]) |
| 104 | + } |
| 105 | + mir::ProjectionElem::ConstantIndex { offset, |
| 106 | + from_end: true, |
| 107 | + min_length: _ } => { |
| 108 | + let lloffset = common::C_u32(bcx.ccx(), offset); |
| 109 | + let base_ty = tr_base.ty.to_ty(tcx); |
| 110 | + let (llbase, lllen) = tvec::get_base_and_len(bcx, |
| 111 | + tr_base.llval, |
| 112 | + base_ty); |
| 113 | + let llindex = build::Sub(bcx, lllen, lloffset, DebugLoc::None); |
| 114 | + let llindex = self.prepare_index(bcx, llindex); |
| 115 | + build::InBoundsGEP(bcx, llbase, &[llindex]) |
| 116 | + } |
| 117 | + mir::ProjectionElem::Downcast(..) => { |
| 118 | + tr_base.llval |
| 119 | + } |
| 120 | + }; |
| 121 | + LvalueRef { |
| 122 | + llval: llprojected, |
| 123 | + ty: projected_ty, |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /// Adjust the bitwidth of an index since LLVM is less forgiving |
| 130 | + /// than we are. |
| 131 | + /// |
| 132 | + /// nmatsakis: is this still necessary? Not sure. |
| 133 | + fn prepare_index(&mut self, |
| 134 | + bcx: Block<'bcx, 'tcx>, |
| 135 | + llindex: ValueRef) |
| 136 | + -> ValueRef |
| 137 | + { |
| 138 | + let ccx = bcx.ccx(); |
| 139 | + let index_size = machine::llbitsize_of_real(bcx.ccx(), common::val_ty(llindex)); |
| 140 | + let int_size = machine::llbitsize_of_real(bcx.ccx(), ccx.int_type()); |
| 141 | + if index_size < int_size { |
| 142 | + build::ZExt(bcx, llindex, ccx.int_type()) |
| 143 | + } else if index_size > int_size { |
| 144 | + build::Trunc(bcx, llindex, ccx.int_type()) |
| 145 | + } else { |
| 146 | + llindex |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments