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