]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/asm.rs
auto merge of #17654 : gereeter/rust/no-unnecessary-cell, r=alexcrichton
[rust.git] / src / librustc / middle / trans / asm.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 /*!
12 # Translation of inline assembly.
13 */
14
15 use llvm;
16 use middle::trans::build::*;
17 use middle::trans::callee;
18 use middle::trans::common::*;
19 use middle::trans::cleanup;
20 use middle::trans::cleanup::CleanupMethods;
21 use middle::trans::expr;
22 use middle::trans::type_of;
23 use middle::trans::type_::Type;
24
25 use std::c_str::ToCStr;
26 use std::string::String;
27 use syntax::ast;
28 use libc::{c_uint, c_char};
29
30 // Take an inline assembly expression and splat it out via LLVM
31 pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ia: &ast::InlineAsm)
32                                     -> Block<'blk, 'tcx> {
33     let fcx = bcx.fcx;
34     let mut bcx = bcx;
35     let mut constraints = Vec::new();
36     let mut output_types = Vec::new();
37
38     let temp_scope = fcx.push_custom_cleanup_scope();
39
40     let mut ext_inputs = Vec::new();
41     let mut ext_constraints = Vec::new();
42
43     // Prepare the output operands
44     let outputs = ia.outputs.iter().enumerate().map(|(i, &(ref c, ref out, is_rw))| {
45         constraints.push((*c).clone());
46
47         let out_datum = unpack_datum!(bcx, expr::trans(bcx, &**out));
48         output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
49         let val = out_datum.val;
50         if is_rw {
51             ext_inputs.push(unpack_result!(bcx, {
52                 callee::trans_arg_datum(bcx,
53                                        expr_ty(bcx, &**out),
54                                        out_datum,
55                                        cleanup::CustomScope(temp_scope),
56                                        callee::DontAutorefArg)
57             }));
58             ext_constraints.push(i.to_string());
59         }
60         val
61
62     }).collect::<Vec<_>>();
63
64     // Now the input operands
65     let inputs = ia.inputs.iter().map(|&(ref c, ref input)| {
66         constraints.push((*c).clone());
67
68         let in_datum = unpack_datum!(bcx, expr::trans(bcx, &**input));
69         unpack_result!(bcx, {
70             callee::trans_arg_datum(bcx,
71                                     expr_ty(bcx, &**input),
72                                     in_datum,
73                                     cleanup::CustomScope(temp_scope),
74                                     callee::DontAutorefArg)
75         })
76     }).collect::<Vec<_>>().append(ext_inputs.as_slice());
77
78     // no failure occurred preparing operands, no need to cleanup
79     fcx.pop_custom_cleanup_scope(temp_scope);
80
81     let mut constraints =
82         String::from_str(constraints.iter()
83                                     .map(|s| s.get().to_string())
84                                     .chain(ext_constraints.into_iter())
85                                     .collect::<Vec<String>>()
86                                     .connect(",")
87                                     .as_slice());
88
89     let mut clobbers = get_clobbers();
90     if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {
91         clobbers = format!("{},{}", ia.clobbers.get(), clobbers);
92     } else {
93         clobbers.push_str(ia.clobbers.get());
94     }
95
96     // Add the clobbers to our constraints list
97     if clobbers.len() != 0 && constraints.len() != 0 {
98         constraints.push_char(',');
99         constraints.push_str(clobbers.as_slice());
100     } else {
101         constraints.push_str(clobbers.as_slice());
102     }
103
104     debug!("Asm Constraints: {:?}", constraints.as_slice());
105
106     let num_outputs = outputs.len();
107
108     // Depending on how many outputs we have, the return type is different
109     let output_type = if num_outputs == 0 {
110         Type::void(bcx.ccx())
111     } else if num_outputs == 1 {
112         *output_types.get(0)
113     } else {
114         Type::struct_(bcx.ccx(), output_types.as_slice(), false)
115     };
116
117     let dialect = match ia.dialect {
118         ast::AsmAtt   => llvm::AD_ATT,
119         ast::AsmIntel => llvm::AD_Intel
120     };
121
122     let r = ia.asm.get().with_c_str(|a| {
123         constraints.as_slice().with_c_str(|c| {
124             InlineAsmCall(bcx,
125                           a,
126                           c,
127                           inputs.as_slice(),
128                           output_type,
129                           ia.volatile,
130                           ia.alignstack,
131                           dialect)
132         })
133     });
134
135     // Again, based on how many outputs we have
136     if num_outputs == 1 {
137         Store(bcx, r, *outputs.get(0));
138     } else {
139         for (i, o) in outputs.iter().enumerate() {
140             let v = ExtractValue(bcx, r, i);
141             Store(bcx, v, *o);
142         }
143     }
144
145     // Store expn_id in a metadata node so we can map LLVM errors
146     // back to source locations.  See #17552.
147     unsafe {
148         let key = "srcloc";
149         let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx().llcx(),
150             key.as_ptr() as *const c_char, key.len() as c_uint);
151
152         let val: llvm::ValueRef = C_i32(bcx.ccx(), ia.expn_id.to_llvm_cookie());
153
154         llvm::LLVMSetMetadata(r, kind,
155             llvm::LLVMMDNodeInContext(bcx.ccx().llcx(), &val, 1));
156     }
157
158     return bcx;
159
160 }
161
162 // Default per-arch clobbers
163 // Basically what clang does
164
165 #[cfg(any(target_arch = "arm",
166           target_arch = "mips",
167           target_arch = "mipsel"))]
168 fn get_clobbers() -> String {
169     "".to_string()
170 }
171
172 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
173 fn get_clobbers() -> String {
174     "~{dirflag},~{fpsr},~{flags}".to_string()
175 }