]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/mir/statement.rs
Rollup merge of #56979 - VardhanThigle:Vardhan/rust-sgx-unwind-support, r=alexcrichton
[rust.git] / src / librustc_codegen_ssa / mir / statement.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 rustc::mir;
12
13 use traits::BuilderMethods;
14 use super::FunctionCx;
15 use super::LocalRef;
16 use super::OperandValue;
17 use traits::*;
18
19 impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
20     pub fn codegen_statement(
21         &mut self,
22         mut bx: Bx,
23         statement: &mir::Statement<'tcx>
24     ) -> Bx {
25         debug!("codegen_statement(statement={:?})", statement);
26
27         self.set_debug_loc(&mut bx, statement.source_info);
28         match statement.kind {
29             mir::StatementKind::Assign(ref place, ref rvalue) => {
30                 if let mir::Place::Local(index) = *place {
31                     match self.locals[index] {
32                         LocalRef::Place(cg_dest) => {
33                             self.codegen_rvalue(bx, cg_dest, rvalue)
34                         }
35                         LocalRef::UnsizedPlace(cg_indirect_dest) => {
36                             self.codegen_rvalue_unsized(bx, cg_indirect_dest, rvalue)
37                         }
38                         LocalRef::Operand(None) => {
39                             let (bx, operand) = self.codegen_rvalue_operand(bx, rvalue);
40                             self.locals[index] = LocalRef::Operand(Some(operand));
41                             bx
42                         }
43                         LocalRef::Operand(Some(op)) => {
44                             if !op.layout.is_zst() {
45                                 span_bug!(statement.source_info.span,
46                                           "operand {:?} already assigned",
47                                           rvalue);
48                             }
49
50                             // If the type is zero-sized, it's already been set here,
51                             // but we still need to make sure we codegen the operand
52                             self.codegen_rvalue_operand(bx, rvalue).0
53                         }
54                     }
55                 } else {
56                     let cg_dest = self.codegen_place(&mut bx, place);
57                     self.codegen_rvalue(bx, cg_dest, rvalue)
58                 }
59             }
60             mir::StatementKind::SetDiscriminant{ref place, variant_index} => {
61                 self.codegen_place(&mut bx, place)
62                     .codegen_set_discr(&mut bx, variant_index);
63                 bx
64             }
65             mir::StatementKind::StorageLive(local) => {
66                 if let LocalRef::Place(cg_place) = self.locals[local] {
67                     cg_place.storage_live(&mut bx);
68                 } else if let LocalRef::UnsizedPlace(cg_indirect_place) = self.locals[local] {
69                     cg_indirect_place.storage_live(&mut bx);
70                 }
71                 bx
72             }
73             mir::StatementKind::StorageDead(local) => {
74                 if let LocalRef::Place(cg_place) = self.locals[local] {
75                     cg_place.storage_dead(&mut bx);
76                 } else if let LocalRef::UnsizedPlace(cg_indirect_place) = self.locals[local] {
77                     cg_indirect_place.storage_dead(&mut bx);
78                 }
79                 bx
80             }
81             mir::StatementKind::InlineAsm { ref asm, ref outputs, ref inputs } => {
82                 let outputs = outputs.iter().map(|output| {
83                     self.codegen_place(&mut bx, output)
84                 }).collect();
85
86                 let input_vals = inputs.iter()
87                     .fold(Vec::with_capacity(inputs.len()), |mut acc, (span, input)| {
88                         let op = self.codegen_operand(&mut bx, input);
89                         if let OperandValue::Immediate(_) = op.val {
90                             acc.push(op.immediate());
91                         } else {
92                             span_err!(bx.sess(), span.to_owned(), E0669,
93                                      "invalid value for constraint in inline assembly");
94                         }
95                         acc
96                 });
97
98                 if input_vals.len() == inputs.len() {
99                     let res = bx.codegen_inline_asm(asm, outputs, input_vals);
100                     if !res {
101                         span_err!(bx.sess(), statement.source_info.span, E0668,
102                                   "malformed inline assembly");
103                     }
104                 }
105                 bx
106             }
107             mir::StatementKind::FakeRead(..) |
108             mir::StatementKind::Retag { .. } |
109             mir::StatementKind::AscribeUserType(..) |
110             mir::StatementKind::Nop => bx,
111         }
112     }
113 }