]> git.proxmox.com Git - rustc.git/blob - src/librustc_codegen_llvm/mir/statement.rs
New upstream version 1.31.0+dfsg1
[rustc.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
20 impl FunctionCx<'a, 'll, 'tcx> {
21 pub fn codegen_statement(&mut self,
22 bx: Builder<'a, 'll, 'tcx>,
23 statement: &mir::Statement<'tcx>)
24 -> Builder<'a, 'll, 'tcx> {
25 debug!("codegen_statement(statement={:?})", statement);
26
27 self.set_debug_loc(&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(&bx, place);
57 self.codegen_rvalue(bx, cg_dest, rvalue)
58 }
59 }
60 mir::StatementKind::SetDiscriminant{ref place, variant_index} => {
61 self.codegen_place(&bx, place)
62 .codegen_set_discr(&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(&bx);
68 } else if let LocalRef::UnsizedPlace(cg_indirect_place) = self.locals[local] {
69 cg_indirect_place.storage_live(&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(&bx);
76 } else if let LocalRef::UnsizedPlace(cg_indirect_place) = self.locals[local] {
77 cg_indirect_place.storage_dead(&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(&bx, output)
84 }).collect();
85
86 let input_vals = inputs.iter()
87 .try_fold(Vec::with_capacity(inputs.len()), |mut acc, input| {
88 let op = self.codegen_operand(&bx, input);
89 if let OperandValue::Immediate(_) = op.val {
90 acc.push(op.immediate());
91 Ok(acc)
92 } else {
93 Err(op)
94 }
95 });
96
97 if input_vals.is_err() {
98 span_err!(bx.sess(), statement.source_info.span, E0669,
99 "invalid value for constraint in inline assembly");
100 } else {
101 let input_vals = input_vals.unwrap();
102 let res = asm::codegen_inline_asm(&bx, asm, outputs, input_vals);
103 if !res {
104 span_err!(bx.sess(), statement.source_info.span, E0668,
105 "malformed inline assembly");
106 }
107 }
108 bx
109 }
110 mir::StatementKind::FakeRead(..) |
111 mir::StatementKind::EndRegion(_) |
112 mir::StatementKind::Validate(..) |
113 mir::StatementKind::AscribeUserType(..) |
114 mir::StatementKind::Nop => bx,
115 }
116 }
117 }