]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/asm.rs
9b499b6d1a147ca9088dd661e249e88fd13da3fb
[rust.git] / src / librustc_trans / 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 trans::build::*;
17 use trans::callee;
18 use trans::common::*;
19 use trans::cleanup;
20 use trans::cleanup::CleanupMethods;
21 use trans::expr;
22 use trans::type_of;
23 use 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 mut 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<_>>();
77     inputs.push_all(ext_inputs.as_slice());
78
79     // no failure occurred preparing operands, no need to cleanup
80     fcx.pop_custom_cleanup_scope(temp_scope);
81
82     let mut constraints =
83         String::from_str(constraints.iter()
84                                     .map(|s| s.get().to_string())
85                                     .chain(ext_constraints.into_iter())
86                                     .collect::<Vec<String>>()
87                                     .connect(",")
88                                     .as_slice());
89
90     let mut clobbers = get_clobbers();
91     if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {
92         clobbers = format!("{},{}", ia.clobbers.get(), clobbers);
93     } else {
94         clobbers.push_str(ia.clobbers.get());
95     }
96
97     // Add the clobbers to our constraints list
98     if clobbers.len() != 0 && constraints.len() != 0 {
99         constraints.push(',');
100         constraints.push_str(clobbers.as_slice());
101     } else {
102         constraints.push_str(clobbers.as_slice());
103     }
104
105     debug!("Asm Constraints: {}", constraints.as_slice());
106
107     let num_outputs = outputs.len();
108
109     // Depending on how many outputs we have, the return type is different
110     let output_type = if num_outputs == 0 {
111         Type::void(bcx.ccx())
112     } else if num_outputs == 1 {
113         output_types[0]
114     } else {
115         Type::struct_(bcx.ccx(), output_types.as_slice(), false)
116     };
117
118     let dialect = match ia.dialect {
119         ast::AsmAtt   => llvm::AD_ATT,
120         ast::AsmIntel => llvm::AD_Intel
121     };
122
123     let r = ia.asm.get().with_c_str(|a| {
124         constraints.as_slice().with_c_str(|c| {
125             InlineAsmCall(bcx,
126                           a,
127                           c,
128                           inputs.as_slice(),
129                           output_type,
130                           ia.volatile,
131                           ia.alignstack,
132                           dialect)
133         })
134     });
135
136     // Again, based on how many outputs we have
137     if num_outputs == 1 {
138         Store(bcx, r, outputs[0]);
139     } else {
140         for (i, o) in outputs.iter().enumerate() {
141             let v = ExtractValue(bcx, r, i);
142             Store(bcx, v, *o);
143         }
144     }
145
146     // Store expn_id in a metadata node so we can map LLVM errors
147     // back to source locations.  See #17552.
148     unsafe {
149         let key = "srcloc";
150         let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx().llcx(),
151             key.as_ptr() as *const c_char, key.len() as c_uint);
152
153         let val: llvm::ValueRef = C_i32(bcx.ccx(), ia.expn_id.to_llvm_cookie());
154
155         llvm::LLVMSetMetadata(r, kind,
156             llvm::LLVMMDNodeInContext(bcx.ccx().llcx(), &val, 1));
157     }
158
159     return bcx;
160
161 }
162
163 // Default per-arch clobbers
164 // Basically what clang does
165
166 #[cfg(any(target_arch = "arm",
167           target_arch = "mips",
168           target_arch = "mipsel"))]
169 fn get_clobbers() -> String {
170     "".to_string()
171 }
172
173 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
174 fn get_clobbers() -> String {
175     "~{dirflag},~{fpsr},~{flags}".to_string()
176 }