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