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