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;
19 use common::{self, CrateContext, C_null};
28 use super::{MirContext, LocalRef};
29 use super::lvalue::{Alignment, LvalueRef};
31 /// The representation of a Rust value. The enum variant is in fact
32 /// uniquely determined by the value's type, but is kept as a
34 #[derive(Copy, Clone)]
35 pub enum OperandValue {
36 /// A reference to the actual operand. The data is guaranteed
37 /// to be valid for the operand's lifetime.
38 Ref(ValueRef, Alignment),
39 /// A single LLVM value.
41 /// A pair of immediate LLVM values. Used by fat pointers too.
42 Pair(ValueRef, ValueRef)
45 /// An `OperandRef` is an "SSA" reference to a Rust value, along with
48 /// NOTE: unless you know a value's type exactly, you should not
49 /// generate LLVM opcodes acting on it and instead act via methods,
50 /// to avoid nasty edge cases. In particular, using `Builder.store`
51 /// directly is sure to cause problems -- use `MirContext.store_operand`
53 #[derive(Copy, Clone)]
54 pub struct OperandRef<'tcx> {
56 pub val: OperandValue,
58 // The type of value being returned.
62 impl<'tcx> fmt::Debug for OperandRef<'tcx> {
63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 OperandValue::Ref(r, align) => {
66 write!(f, "OperandRef(Ref({:?}, {:?}) @ {:?})",
67 Value(r), align, self.ty)
69 OperandValue::Immediate(i) => {
70 write!(f, "OperandRef(Immediate({:?}) @ {:?})",
73 OperandValue::Pair(a, b) => {
74 write!(f, "OperandRef(Pair({:?}, {:?}) @ {:?})",
75 Value(a), Value(b), self.ty)
81 impl<'a, 'tcx> OperandRef<'tcx> {
82 pub fn new_zst(ccx: &CrateContext<'a, 'tcx>,
83 ty: Ty<'tcx>) -> OperandRef<'tcx> {
84 assert!(common::type_is_zero_size(ccx, ty));
85 let llty = type_of::type_of(ccx, ty);
86 let val = if common::type_is_imm_pair(ccx, ty) {
87 let fields = llty.field_types();
88 OperandValue::Pair(C_null(fields[0]), C_null(fields[1]))
90 OperandValue::Immediate(C_null(llty))
98 /// Asserts that this operand refers to a scalar and returns
99 /// a reference to its value.
100 pub fn immediate(self) -> ValueRef {
102 OperandValue::Immediate(s) => s,
103 _ => bug!("not immediate: {:?}", self)
107 pub fn deref(self) -> LvalueRef<'tcx> {
108 let projected_ty = self.ty.builtin_deref(true, ty::NoPreference)
110 let (llptr, llextra) = match self.val {
111 OperandValue::Immediate(llptr) => (llptr, ptr::null_mut()),
112 OperandValue::Pair(llptr, llextra) => (llptr, llextra),
113 OperandValue::Ref(..) => bug!("Deref of by-Ref operand {:?}", self)
118 ty: LvalueTy::from_ty(projected_ty),
119 alignment: Alignment::AbiAligned,
123 /// If this operand is a Pair, we return an
124 /// Immediate aggregate with the two values.
125 pub fn pack_if_pair(mut self, bcx: &Builder<'a, 'tcx>) -> OperandRef<'tcx> {
126 if let OperandValue::Pair(a, b) = self.val {
127 // Reconstruct the immediate aggregate.
128 let llty = type_of::type_of(bcx.ccx, self.ty);
129 let mut llpair = common::C_undef(llty);
132 let mut elem = elems[i];
133 // Extend boolean i1's to i8.
134 if common::val_ty(elem) == Type::i1(bcx.ccx) {
135 elem = bcx.zext(elem, Type::i8(bcx.ccx));
137 llpair = bcx.insert_value(llpair, elem, i);
139 self.val = OperandValue::Immediate(llpair);
144 /// If this operand is a pair in an Immediate,
145 /// we return a Pair with the two halves.
146 pub fn unpack_if_pair(mut self, bcx: &Builder<'a, 'tcx>) -> OperandRef<'tcx> {
147 if let OperandValue::Immediate(llval) = self.val {
148 // Deconstruct the immediate aggregate.
149 if common::type_is_imm_pair(bcx.ccx, self.ty) {
150 debug!("Operand::unpack_if_pair: unpacking {:?}", self);
152 let mut a = bcx.extract_value(llval, 0);
153 let mut b = bcx.extract_value(llval, 1);
155 let pair_fields = common::type_pair_fields(bcx.ccx, self.ty);
156 if let Some([a_ty, b_ty]) = pair_fields {
158 a = bcx.trunc(a, Type::i1(bcx.ccx));
161 b = bcx.trunc(b, Type::i1(bcx.ccx));
165 self.val = OperandValue::Pair(a, b);
172 impl<'a, 'tcx> MirContext<'a, 'tcx> {
173 pub fn trans_load(&mut self,
174 bcx: &Builder<'a, 'tcx>,
180 debug!("trans_load: {:?} @ {:?}", Value(llval), ty);
182 let val = if common::type_is_fat_ptr(bcx.ccx, ty) {
183 let (lldata, llextra) = base::load_fat_ptr(bcx, llval, align, ty);
184 OperandValue::Pair(lldata, llextra)
185 } else if common::type_is_imm_pair(bcx.ccx, ty) {
186 let f_align = match *bcx.ccx.layout_of(ty) {
187 Layout::Univariant { ref variant, .. } =>
188 Alignment::from_packed(variant.packed) | align,
191 let [a_ty, b_ty] = common::type_pair_fields(bcx.ccx, ty).unwrap();
192 let a_ptr = bcx.struct_gep(llval, 0);
193 let b_ptr = bcx.struct_gep(llval, 1);
196 base::load_ty(bcx, a_ptr, f_align, a_ty),
197 base::load_ty(bcx, b_ptr, f_align, b_ty)
199 } else if common::type_is_immediate(bcx.ccx, ty) {
200 OperandValue::Immediate(base::load_ty(bcx, llval, align, ty))
202 OperandValue::Ref(llval, align)
205 OperandRef { val: val, ty: ty }
208 pub fn trans_consume(&mut self,
209 bcx: &Builder<'a, 'tcx>,
210 lvalue: &mir::Lvalue<'tcx>)
213 debug!("trans_consume(lvalue={:?})", lvalue);
215 // watch out for locals that do not have an
216 // alloca; they are handled somewhat differently
217 if let mir::Lvalue::Local(index) = *lvalue {
218 match self.locals[index] {
219 LocalRef::Operand(Some(o)) => {
222 LocalRef::Operand(None) => {
223 bug!("use of {:?} before def", lvalue);
225 LocalRef::Lvalue(..) => {
231 // Moves out of pair fields are trivial.
232 if let &mir::Lvalue::Projection(ref proj) = lvalue {
233 if let mir::Lvalue::Local(index) = proj.base {
234 if let LocalRef::Operand(Some(o)) = self.locals[index] {
235 match (o.val, &proj.elem) {
236 (OperandValue::Pair(a, b),
237 &mir::ProjectionElem::Field(ref f, ty)) => {
238 let llval = [a, b][f.index()];
239 let op = OperandRef {
240 val: OperandValue::Immediate(llval),
241 ty: self.monomorphize(&ty)
244 // Handle nested pairs.
245 return op.unpack_if_pair(bcx);
253 // for most lvalues, to consume them we just load them
254 // out from their home
255 let tr_lvalue = self.trans_lvalue(bcx, lvalue);
256 let ty = tr_lvalue.ty.to_ty(bcx.tcx());
257 self.trans_load(bcx, tr_lvalue.llval, tr_lvalue.alignment, ty)
260 pub fn trans_operand(&mut self,
261 bcx: &Builder<'a, 'tcx>,
262 operand: &mir::Operand<'tcx>)
265 debug!("trans_operand(operand={:?})", operand);
268 mir::Operand::Consume(ref lvalue) => {
269 self.trans_consume(bcx, lvalue)
272 mir::Operand::Constant(ref constant) => {
273 let val = self.trans_constant(&bcx, constant);
274 let operand = val.to_operand(bcx.ccx);
275 if let OperandValue::Ref(ptr, align) = operand.val {
276 // If this is a OperandValue::Ref to an immediate constant, load it.
277 self.trans_load(bcx, ptr, align, operand.ty)
285 pub fn store_operand(&mut self,
286 bcx: &Builder<'a, 'tcx>,
289 operand: OperandRef<'tcx>) {
290 debug!("store_operand: operand={:?}, align={:?}", operand, align);
291 // Avoid generating stores of zero-sized values, because the only way to have a zero-sized
292 // value is through `undef`, and store itself is useless.
293 if common::type_is_zero_size(bcx.ccx, operand.ty) {
297 OperandValue::Ref(r, Alignment::Packed) =>
298 base::memcpy_ty(bcx, lldest, r, operand.ty, Some(1)),
299 OperandValue::Ref(r, Alignment::AbiAligned) =>
300 base::memcpy_ty(bcx, lldest, r, operand.ty, align),
301 OperandValue::Immediate(s) => {
302 bcx.store(base::from_immediate(bcx, s), lldest, align);
304 OperandValue::Pair(a, b) => {
305 let f_align = match *bcx.ccx.layout_of(operand.ty) {
306 Layout::Univariant { ref variant, .. } if variant.packed => {
312 let a = base::from_immediate(bcx, a);
313 let b = base::from_immediate(bcx, b);
314 bcx.store(a, bcx.struct_gep(lldest, 0), f_align);
315 bcx.store(b, bcx.struct_gep(lldest, 1), f_align);