]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/mir/operand.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_trans / mir / operand.rs
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::ty::Ty;
13 use rustc::mir::repr as mir;
14 use base;
15 use common::{self, Block, BlockAndBuilder};
16 use datum;
17 use value::Value;
18 use glue;
19
20 use std::fmt;
21
22 use super::lvalue::load_fat_ptr;
23 use super::{MirContext, TempRef, drop};
24
25 /// The representation of a Rust value. The enum variant is in fact
26 /// uniquely determined by the value's type, but is kept as a
27 /// safety check.
28 #[derive(Copy, Clone)]
29 pub enum OperandValue {
30 /// A reference to the actual operand. The data is guaranteed
31 /// to be valid for the operand's lifetime.
32 Ref(ValueRef),
33 /// A single LLVM value.
34 Immediate(ValueRef),
35 /// A fat pointer. The first ValueRef is the data and the second
36 /// is the extra.
37 FatPtr(ValueRef, ValueRef)
38 }
39
40 /// An `OperandRef` is an "SSA" reference to a Rust value, along with
41 /// its type.
42 ///
43 /// NOTE: unless you know a value's type exactly, you should not
44 /// generate LLVM opcodes acting on it and instead act via methods,
45 /// to avoid nasty edge cases. In particular, using `Builder.store`
46 /// directly is sure to cause problems -- use `MirContext.store_operand`
47 /// instead.
48 #[derive(Copy, Clone)]
49 pub struct OperandRef<'tcx> {
50 // The value.
51 pub val: OperandValue,
52
53 // The type of value being returned.
54 pub ty: Ty<'tcx>
55 }
56
57 impl<'tcx> fmt::Debug for OperandRef<'tcx> {
58 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59 match self.val {
60 OperandValue::Ref(r) => {
61 write!(f, "OperandRef(Ref({:?}) @ {:?})",
62 Value(r), self.ty)
63 }
64 OperandValue::Immediate(i) => {
65 write!(f, "OperandRef(Immediate({:?}) @ {:?})",
66 Value(i), self.ty)
67 }
68 OperandValue::FatPtr(a, d) => {
69 write!(f, "OperandRef(FatPtr({:?}, {:?}) @ {:?})",
70 Value(a), Value(d), self.ty)
71 }
72 }
73 }
74 }
75
76 impl<'tcx> OperandRef<'tcx> {
77 /// Asserts that this operand refers to a scalar and returns
78 /// a reference to its value.
79 pub fn immediate(self) -> ValueRef {
80 match self.val {
81 OperandValue::Immediate(s) => s,
82 _ => bug!()
83 }
84 }
85 }
86
87 impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
88 pub fn trans_load(&mut self,
89 bcx: &BlockAndBuilder<'bcx, 'tcx>,
90 llval: ValueRef,
91 ty: Ty<'tcx>)
92 -> OperandRef<'tcx>
93 {
94 debug!("trans_load: {:?} @ {:?}", Value(llval), ty);
95
96 let val = match datum::appropriate_rvalue_mode(bcx.ccx(), ty) {
97 datum::ByValue => {
98 OperandValue::Immediate(base::load_ty_builder(bcx, llval, ty))
99 }
100 datum::ByRef if common::type_is_fat_ptr(bcx.tcx(), ty) => {
101 let (lldata, llextra) = load_fat_ptr(bcx, llval);
102 OperandValue::FatPtr(lldata, llextra)
103 }
104 datum::ByRef => OperandValue::Ref(llval)
105 };
106
107 OperandRef { val: val, ty: ty }
108 }
109
110 pub fn trans_operand(&mut self,
111 bcx: &BlockAndBuilder<'bcx, 'tcx>,
112 operand: &mir::Operand<'tcx>)
113 -> OperandRef<'tcx>
114 {
115 debug!("trans_operand(operand={:?})", operand);
116
117 match *operand {
118 mir::Operand::Consume(ref lvalue) => {
119 // watch out for temporaries that do not have an
120 // alloca; they are handled somewhat differently
121 if let &mir::Lvalue::Temp(index) = lvalue {
122 match self.temps[index as usize] {
123 TempRef::Operand(Some(o)) => {
124 return o;
125 }
126 TempRef::Operand(None) => {
127 bug!("use of {:?} before def", lvalue);
128 }
129 TempRef::Lvalue(..) => {
130 // use path below
131 }
132 }
133 }
134
135 // for most lvalues, to consume them we just load them
136 // out from their home
137 let tr_lvalue = self.trans_lvalue(bcx, lvalue);
138 let ty = tr_lvalue.ty.to_ty(bcx.tcx());
139 self.trans_load(bcx, tr_lvalue.llval, ty)
140 }
141
142 mir::Operand::Constant(ref constant) => {
143 self.trans_constant(bcx, constant)
144 }
145 }
146 }
147
148 pub fn store_operand(&mut self,
149 bcx: &BlockAndBuilder<'bcx, 'tcx>,
150 lldest: ValueRef,
151 operand: OperandRef<'tcx>)
152 {
153 debug!("store_operand: operand={:?}", operand);
154 bcx.with_block(|bcx| self.store_operand_direct(bcx, lldest, operand))
155 }
156
157 pub fn store_operand_direct(&mut self,
158 bcx: Block<'bcx, 'tcx>,
159 lldest: ValueRef,
160 operand: OperandRef<'tcx>)
161 {
162 // Avoid generating stores of zero-sized values, because the only way to have a zero-sized
163 // value is through `undef`, and store itself is useless.
164 if common::type_is_zero_size(bcx.ccx(), operand.ty) {
165 return;
166 }
167 match operand.val {
168 OperandValue::Ref(r) => base::memcpy_ty(bcx, lldest, r, operand.ty),
169 OperandValue::Immediate(s) => base::store_ty(bcx, s, lldest, operand.ty),
170 OperandValue::FatPtr(data, extra) => {
171 base::store_fat_ptr(bcx, data, extra, lldest, operand.ty);
172 }
173 }
174 }
175
176 pub fn set_operand_dropped(&mut self,
177 bcx: &BlockAndBuilder<'bcx, 'tcx>,
178 operand: &mir::Operand<'tcx>) {
179 match *operand {
180 mir::Operand::Constant(_) => return,
181 mir::Operand::Consume(ref lvalue) => {
182 if let mir::Lvalue::Temp(idx) = *lvalue {
183 if let TempRef::Operand(..) = self.temps[idx as usize] {
184 // All lvalues which have an associated drop are promoted to an alloca
185 // beforehand. If this is an operand, it is safe to say this is never
186 // dropped and there’s no reason for us to zero this out at all.
187 return
188 }
189 }
190 let lvalue = self.trans_lvalue(bcx, lvalue);
191 let ty = lvalue.ty.to_ty(bcx.tcx());
192 if !glue::type_needs_drop(bcx.tcx(), ty) {
193 return
194 } else {
195 drop::drop_fill(bcx, lvalue.llval, ty);
196 }
197 }
198 }
199 }
200 }