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::ty::{self, Ty};
13 use rustc::ty::layout::{Layout, LayoutTyper};
15 use rustc::mir::tcx::LvalueTy;
16 use rustc_data_structures::indexed_vec::Idx;
20 use common::{self, CrateContext, C_null};
29 use super::{MirContext, LocalRef};
30 use super::lvalue::{Alignment, LvalueRef};
32 /// The representation of a Rust value. The enum variant is in fact
33 /// uniquely determined by the value's type, but is kept as a
35 #[derive(Copy, Clone)]
36 pub enum OperandValue {
37 /// A reference to the actual operand. The data is guaranteed
38 /// to be valid for the operand's lifetime.
39 Ref(ValueRef, Alignment),
40 /// A single LLVM value.
42 /// A pair of immediate LLVM values. Used by fat pointers too.
43 Pair(ValueRef, ValueRef)
46 /// An `OperandRef` is an "SSA" reference to a Rust value, along with
49 /// NOTE: unless you know a value's type exactly, you should not
50 /// generate LLVM opcodes acting on it and instead act via methods,
51 /// to avoid nasty edge cases. In particular, using `Builder.store`
52 /// directly is sure to cause problems -- use `MirContext.store_operand`
54 #[derive(Copy, Clone)]
55 pub struct OperandRef<'tcx> {
57 pub val: OperandValue,
59 // The type of value being returned.
63 impl<'tcx> fmt::Debug for OperandRef<'tcx> {
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66 OperandValue::Ref(r, align) => {
67 write!(f, "OperandRef(Ref({:?}, {:?}) @ {:?})",
68 Value(r), align, self.ty)
70 OperandValue::Immediate(i) => {
71 write!(f, "OperandRef(Immediate({:?}) @ {:?})",
74 OperandValue::Pair(a, b) => {
75 write!(f, "OperandRef(Pair({:?}, {:?}) @ {:?})",
76 Value(a), Value(b), self.ty)
82 impl<'a, 'tcx> OperandRef<'tcx> {
83 pub fn new_zst(ccx: &CrateContext<'a, 'tcx>,
84 ty: Ty<'tcx>) -> OperandRef<'tcx> {
85 assert!(common::type_is_zero_size(ccx, ty));
86 let llty = type_of::type_of(ccx, ty);
87 let val = if common::type_is_imm_pair(ccx, ty) {
88 let fields = llty.field_types();
89 OperandValue::Pair(C_null(fields[0]), C_null(fields[1]))
91 OperandValue::Immediate(C_null(llty))
99 /// Asserts that this operand refers to a scalar and returns
100 /// a reference to its value.
101 pub fn immediate(self) -> ValueRef {
103 OperandValue::Immediate(s) => s,
104 _ => bug!("not immediate: {:?}", self)
108 pub fn deref(self) -> LvalueRef<'tcx> {
109 let projected_ty = self.ty.builtin_deref(true, ty::NoPreference)
111 let (llptr, llextra) = match self.val {
112 OperandValue::Immediate(llptr) => (llptr, ptr::null_mut()),
113 OperandValue::Pair(llptr, llextra) => (llptr, llextra),
114 OperandValue::Ref(..) => bug!("Deref of by-Ref operand {:?}", self)
119 ty: LvalueTy::from_ty(projected_ty),
120 alignment: Alignment::AbiAligned,
124 /// If this operand is a Pair, we return an
125 /// Immediate aggregate with the two values.
126 pub fn pack_if_pair(mut self, bcx: &Builder<'a, 'tcx>) -> OperandRef<'tcx> {
127 if let OperandValue::Pair(a, b) = self.val {
128 // Reconstruct the immediate aggregate.
129 let llty = type_of::type_of(bcx.ccx, self.ty);
130 let mut llpair = common::C_undef(llty);
133 let mut elem = elems[i];
134 // Extend boolean i1's to i8.
135 if common::val_ty(elem) == Type::i1(bcx.ccx) {
136 elem = bcx.zext(elem, Type::i8(bcx.ccx));
138 let layout = bcx.ccx.layout_of(self.ty);
139 let i = if let Layout::Univariant { ref variant, .. } = *layout {
140 adt::struct_llfields_index(variant, i)
144 llpair = bcx.insert_value(llpair, elem, i);
146 self.val = OperandValue::Immediate(llpair);
151 /// If this operand is a pair in an Immediate,
152 /// we return a Pair with the two halves.
153 pub fn unpack_if_pair(mut self, bcx: &Builder<'a, 'tcx>) -> OperandRef<'tcx> {
154 if let OperandValue::Immediate(llval) = self.val {
155 // Deconstruct the immediate aggregate.
156 if common::type_is_imm_pair(bcx.ccx, self.ty) {
157 debug!("Operand::unpack_if_pair: unpacking {:?}", self);
159 let mut a = bcx.extract_value(llval, 0);
160 let mut b = bcx.extract_value(llval, 1);
162 let pair_fields = common::type_pair_fields(bcx.ccx, self.ty);
163 if let Some([a_ty, b_ty]) = pair_fields {
165 a = bcx.trunc(a, Type::i1(bcx.ccx));
168 b = bcx.trunc(b, Type::i1(bcx.ccx));
172 self.val = OperandValue::Pair(a, b);
179 impl<'a, 'tcx> MirContext<'a, 'tcx> {
180 pub fn trans_load(&mut self,
181 bcx: &Builder<'a, 'tcx>,
187 debug!("trans_load: {:?} @ {:?}", Value(llval), ty);
189 let val = if common::type_is_fat_ptr(bcx.ccx, ty) {
190 let (lldata, llextra) = base::load_fat_ptr(bcx, llval, align, ty);
191 OperandValue::Pair(lldata, llextra)
192 } else if common::type_is_imm_pair(bcx.ccx, ty) {
193 let (ix0, ix1, f_align) = match *bcx.ccx.layout_of(ty) {
194 Layout::Univariant { ref variant, .. } => {
195 (adt::struct_llfields_index(variant, 0),
196 adt::struct_llfields_index(variant, 1),
197 Alignment::from_packed(variant.packed) | align)
201 let [a_ty, b_ty] = common::type_pair_fields(bcx.ccx, ty).unwrap();
202 let a_ptr = bcx.struct_gep(llval, ix0);
203 let b_ptr = bcx.struct_gep(llval, ix1);
206 base::load_ty(bcx, a_ptr, f_align, a_ty),
207 base::load_ty(bcx, b_ptr, f_align, b_ty)
209 } else if common::type_is_immediate(bcx.ccx, ty) {
210 OperandValue::Immediate(base::load_ty(bcx, llval, align, ty))
212 OperandValue::Ref(llval, align)
215 OperandRef { val: val, ty: ty }
218 pub fn trans_consume(&mut self,
219 bcx: &Builder<'a, 'tcx>,
220 lvalue: &mir::Lvalue<'tcx>)
223 debug!("trans_consume(lvalue={:?})", lvalue);
225 // watch out for locals that do not have an
226 // alloca; they are handled somewhat differently
227 if let mir::Lvalue::Local(index) = *lvalue {
228 match self.locals[index] {
229 LocalRef::Operand(Some(o)) => {
232 LocalRef::Operand(None) => {
233 bug!("use of {:?} before def", lvalue);
235 LocalRef::Lvalue(..) => {
241 // Moves out of pair fields are trivial.
242 if let &mir::Lvalue::Projection(ref proj) = lvalue {
243 if let mir::Lvalue::Local(index) = proj.base {
244 if let LocalRef::Operand(Some(o)) = self.locals[index] {
245 match (o.val, &proj.elem) {
246 (OperandValue::Pair(a, b),
247 &mir::ProjectionElem::Field(ref f, ty)) => {
248 let llval = [a, b][f.index()];
249 let op = OperandRef {
250 val: OperandValue::Immediate(llval),
251 ty: self.monomorphize(&ty)
254 // Handle nested pairs.
255 return op.unpack_if_pair(bcx);
263 // for most lvalues, to consume them we just load them
264 // out from their home
265 let tr_lvalue = self.trans_lvalue(bcx, lvalue);
266 let ty = tr_lvalue.ty.to_ty(bcx.tcx());
267 self.trans_load(bcx, tr_lvalue.llval, tr_lvalue.alignment, ty)
270 pub fn trans_operand(&mut self,
271 bcx: &Builder<'a, 'tcx>,
272 operand: &mir::Operand<'tcx>)
275 debug!("trans_operand(operand={:?})", operand);
278 mir::Operand::Consume(ref lvalue) => {
279 self.trans_consume(bcx, lvalue)
282 mir::Operand::Constant(ref constant) => {
283 let val = self.trans_constant(&bcx, constant);
284 let operand = val.to_operand(bcx.ccx);
285 if let OperandValue::Ref(ptr, align) = operand.val {
286 // If this is a OperandValue::Ref to an immediate constant, load it.
287 self.trans_load(bcx, ptr, align, operand.ty)
295 pub fn store_operand(&mut self,
296 bcx: &Builder<'a, 'tcx>,
299 operand: OperandRef<'tcx>) {
300 debug!("store_operand: operand={:?}, align={:?}", operand, align);
301 // Avoid generating stores of zero-sized values, because the only way to have a zero-sized
302 // value is through `undef`, and store itself is useless.
303 if common::type_is_zero_size(bcx.ccx, operand.ty) {
307 OperandValue::Ref(r, Alignment::Packed) =>
308 base::memcpy_ty(bcx, lldest, r, operand.ty, Some(1)),
309 OperandValue::Ref(r, Alignment::AbiAligned) =>
310 base::memcpy_ty(bcx, lldest, r, operand.ty, align),
311 OperandValue::Immediate(s) => {
312 bcx.store(base::from_immediate(bcx, s), lldest, align);
314 OperandValue::Pair(a, b) => {
315 let (ix0, ix1, f_align) = match *bcx.ccx.layout_of(operand.ty) {
316 Layout::Univariant { ref variant, .. } => {
317 (adt::struct_llfields_index(variant, 0),
318 adt::struct_llfields_index(variant, 1),
319 if variant.packed { Some(1) } else { None })
324 let a = base::from_immediate(bcx, a);
325 let b = base::from_immediate(bcx, b);
326 bcx.store(a, bcx.struct_gep(lldest, ix0), f_align);
327 bcx.store(b, bcx.struct_gep(lldest, ix1), f_align);