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 layout = ccx.layout_of(ty);
89 let (ix0, ix1) = if let Layout::Univariant { ref variant, .. } = *layout {
90 (adt::struct_llfields_index(variant, 0),
91 adt::struct_llfields_index(variant, 1))
95 let fields = llty.field_types();
96 OperandValue::Pair(C_null(fields[ix0]), C_null(fields[ix1]))
98 OperandValue::Immediate(C_null(llty))
106 /// Asserts that this operand refers to a scalar and returns
107 /// a reference to its value.
108 pub fn immediate(self) -> ValueRef {
110 OperandValue::Immediate(s) => s,
111 _ => bug!("not immediate: {:?}", self)
115 pub fn deref(self) -> LvalueRef<'tcx> {
116 let projected_ty = self.ty.builtin_deref(true, ty::NoPreference)
117 .unwrap_or_else(|| bug!("deref of non-pointer {:?}", self)).ty;
118 let (llptr, llextra) = match self.val {
119 OperandValue::Immediate(llptr) => (llptr, ptr::null_mut()),
120 OperandValue::Pair(llptr, llextra) => (llptr, llextra),
121 OperandValue::Ref(..) => bug!("Deref of by-Ref operand {:?}", self)
126 ty: LvalueTy::from_ty(projected_ty),
127 alignment: Alignment::AbiAligned,
131 /// If this operand is a Pair, we return an
132 /// Immediate aggregate with the two values.
133 pub fn pack_if_pair(mut self, bcx: &Builder<'a, 'tcx>) -> OperandRef<'tcx> {
134 if let OperandValue::Pair(a, b) = self.val {
135 // Reconstruct the immediate aggregate.
136 let llty = type_of::type_of(bcx.ccx, self.ty);
137 let mut llpair = common::C_undef(llty);
140 let mut elem = elems[i];
141 // Extend boolean i1's to i8.
142 if common::val_ty(elem) == Type::i1(bcx.ccx) {
143 elem = bcx.zext(elem, Type::i8(bcx.ccx));
145 let layout = bcx.ccx.layout_of(self.ty);
146 let i = if let Layout::Univariant { ref variant, .. } = *layout {
147 adt::struct_llfields_index(variant, i)
151 llpair = bcx.insert_value(llpair, elem, i);
153 self.val = OperandValue::Immediate(llpair);
158 /// If this operand is a pair in an Immediate,
159 /// we return a Pair with the two halves.
160 pub fn unpack_if_pair(mut self, bcx: &Builder<'a, 'tcx>) -> OperandRef<'tcx> {
161 if let OperandValue::Immediate(llval) = self.val {
162 // Deconstruct the immediate aggregate.
163 if common::type_is_imm_pair(bcx.ccx, self.ty) {
164 debug!("Operand::unpack_if_pair: unpacking {:?}", self);
166 let layout = bcx.ccx.layout_of(self.ty);
167 let (ix0, ix1) = if let Layout::Univariant { ref variant, .. } = *layout {
168 (adt::struct_llfields_index(variant, 0),
169 adt::struct_llfields_index(variant, 1))
174 let mut a = bcx.extract_value(llval, ix0);
175 let mut b = bcx.extract_value(llval, ix1);
177 let pair_fields = common::type_pair_fields(bcx.ccx, self.ty);
178 if let Some([a_ty, b_ty]) = pair_fields {
180 a = bcx.trunc(a, Type::i1(bcx.ccx));
183 b = bcx.trunc(b, Type::i1(bcx.ccx));
187 self.val = OperandValue::Pair(a, b);
194 impl<'a, 'tcx> MirContext<'a, 'tcx> {
195 pub fn trans_load(&mut self,
196 bcx: &Builder<'a, 'tcx>,
202 debug!("trans_load: {:?} @ {:?}", Value(llval), ty);
204 let val = if common::type_is_fat_ptr(bcx.ccx, ty) {
205 let (lldata, llextra) = base::load_fat_ptr(bcx, llval, align, ty);
206 OperandValue::Pair(lldata, llextra)
207 } else if common::type_is_imm_pair(bcx.ccx, ty) {
208 let (ix0, ix1, f_align) = match *bcx.ccx.layout_of(ty) {
209 Layout::Univariant { ref variant, .. } => {
210 (adt::struct_llfields_index(variant, 0),
211 adt::struct_llfields_index(variant, 1),
212 Alignment::from_packed(variant.packed) | align)
216 let [a_ty, b_ty] = common::type_pair_fields(bcx.ccx, ty).unwrap();
217 let a_ptr = bcx.struct_gep(llval, ix0);
218 let b_ptr = bcx.struct_gep(llval, ix1);
221 base::load_ty(bcx, a_ptr, f_align, a_ty),
222 base::load_ty(bcx, b_ptr, f_align, b_ty)
224 } else if common::type_is_immediate(bcx.ccx, ty) {
225 OperandValue::Immediate(base::load_ty(bcx, llval, align, ty))
227 OperandValue::Ref(llval, align)
230 OperandRef { val: val, ty: ty }
233 pub fn trans_consume(&mut self,
234 bcx: &Builder<'a, 'tcx>,
235 lvalue: &mir::Lvalue<'tcx>)
238 debug!("trans_consume(lvalue={:?})", lvalue);
240 // watch out for locals that do not have an
241 // alloca; they are handled somewhat differently
242 if let mir::Lvalue::Local(index) = *lvalue {
243 match self.locals[index] {
244 LocalRef::Operand(Some(o)) => {
247 LocalRef::Operand(None) => {
248 bug!("use of {:?} before def", lvalue);
250 LocalRef::Lvalue(..) => {
256 // Moves out of pair fields are trivial.
257 if let &mir::Lvalue::Projection(ref proj) = lvalue {
258 if let mir::Lvalue::Local(index) = proj.base {
259 if let LocalRef::Operand(Some(o)) = self.locals[index] {
260 match (o.val, &proj.elem) {
261 (OperandValue::Pair(a, b),
262 &mir::ProjectionElem::Field(ref f, ty)) => {
263 let llval = [a, b][f.index()];
264 let op = OperandRef {
265 val: OperandValue::Immediate(llval),
266 ty: self.monomorphize(&ty)
269 // Handle nested pairs.
270 return op.unpack_if_pair(bcx);
278 // for most lvalues, to consume them we just load them
279 // out from their home
280 let tr_lvalue = self.trans_lvalue(bcx, lvalue);
281 let ty = tr_lvalue.ty.to_ty(bcx.tcx());
282 self.trans_load(bcx, tr_lvalue.llval, tr_lvalue.alignment, ty)
285 pub fn trans_operand(&mut self,
286 bcx: &Builder<'a, 'tcx>,
287 operand: &mir::Operand<'tcx>)
290 debug!("trans_operand(operand={:?})", operand);
293 mir::Operand::Consume(ref lvalue) => {
294 self.trans_consume(bcx, lvalue)
297 mir::Operand::Constant(ref constant) => {
298 let val = self.trans_constant(&bcx, constant);
299 let operand = val.to_operand(bcx.ccx);
300 if let OperandValue::Ref(ptr, align) = operand.val {
301 // If this is a OperandValue::Ref to an immediate constant, load it.
302 self.trans_load(bcx, ptr, align, operand.ty)
310 pub fn store_operand(&mut self,
311 bcx: &Builder<'a, 'tcx>,
314 operand: OperandRef<'tcx>) {
315 debug!("store_operand: operand={:?}, align={:?}", operand, align);
316 // Avoid generating stores of zero-sized values, because the only way to have a zero-sized
317 // value is through `undef`, and store itself is useless.
318 if common::type_is_zero_size(bcx.ccx, operand.ty) {
322 OperandValue::Ref(r, Alignment::Packed) =>
323 base::memcpy_ty(bcx, lldest, r, operand.ty, Some(1)),
324 OperandValue::Ref(r, Alignment::AbiAligned) =>
325 base::memcpy_ty(bcx, lldest, r, operand.ty, align),
326 OperandValue::Immediate(s) => {
327 bcx.store(base::from_immediate(bcx, s), lldest, align);
329 OperandValue::Pair(a, b) => {
330 let (ix0, ix1, f_align) = match *bcx.ccx.layout_of(operand.ty) {
331 Layout::Univariant { ref variant, .. } => {
332 (adt::struct_llfields_index(variant, 0),
333 adt::struct_llfields_index(variant, 1),
334 if variant.packed { Some(1) } else { None })
339 let a = base::from_immediate(bcx, a);
340 let b = base::from_immediate(bcx, b);
342 // See comment above about zero-sized values.
343 let (a_zst, b_zst) = common::type_pair_fields(bcx.ccx, operand.ty)
344 .map_or((false, false), |[a_ty, b_ty]| {
345 (common::type_is_zero_size(bcx.ccx, a_ty),
346 common::type_is_zero_size(bcx.ccx, b_ty))
350 bcx.store(a, bcx.struct_gep(lldest, ix0), f_align);
353 bcx.store(b, bcx.struct_gep(lldest, ix1), f_align);