]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/asm.rs
Use ast::AsmDialect's variants qualified, and drop the pointless prefix.
[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 outputs = ia.outputs.iter().enumerate().map(|(i, &(ref c, ref out, is_rw))| {
43         constraints.push((*c).clone());
44
45         let out_datum = unpack_datum!(bcx, expr::trans(bcx, &**out));
46         output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
47         let val = out_datum.val;
48         if is_rw {
49             bcx = callee::trans_arg_datum(bcx,
50                                           expr_ty(bcx, &**out),
51                                           out_datum,
52                                           cleanup::CustomScope(temp_scope),
53                                           callee::DontAutorefArg,
54                                           &mut ext_inputs);
55             ext_constraints.push(i.to_string());
56         }
57         val
58
59     }).collect::<Vec<_>>();
60
61     // Now the input operands
62     let mut inputs = Vec::new();
63     for &(ref c, ref input) in &ia.inputs {
64         constraints.push((*c).clone());
65
66         let in_datum = unpack_datum!(bcx, expr::trans(bcx, &**input));
67         bcx = callee::trans_arg_datum(bcx,
68                                     expr_ty(bcx, &**input),
69                                     in_datum,
70                                     cleanup::CustomScope(temp_scope),
71                                     callee::DontAutorefArg,
72                                     &mut inputs);
73     }
74     inputs.push_all(&ext_inputs[..]);
75
76     // no failure occurred preparing operands, no need to cleanup
77     fcx.pop_custom_cleanup_scope(temp_scope);
78
79     let clobbers = ia.clobbers.iter()
80                               .map(|s| format!("~{{{}}}", &s));
81
82     // Default per-arch clobbers
83     // Basically what clang does
84     let arch_clobbers = match &bcx.sess().target.target.arch[..] {
85         "x86" | "x86_64" => vec!("~{dirflag}", "~{fpsr}", "~{flags}"),
86         _                => Vec::new()
87     };
88
89     let all_constraints= constraints.iter()
90                                     .map(|s| s.to_string())
91                                     .chain(ext_constraints)
92                                     .chain(clobbers)
93                                     .chain(arch_clobbers.iter()
94                                                .map(|s| s.to_string()))
95                                     .collect::<Vec<String>>()
96                                     .join(",");
97
98     debug!("Asm Constraints: {}", &all_constraints[..]);
99
100     // Depending on how many outputs we have, the return type is different
101     let num_outputs = outputs.len();
102     let output_type = match num_outputs {
103         0 => Type::void(bcx.ccx()),
104         1 => output_types[0],
105         _ => Type::struct_(bcx.ccx(), &output_types[..], false)
106     };
107
108     let dialect = match ia.dialect {
109         AsmDialect::Att   => llvm::AD_ATT,
110         AsmDialect::Intel => llvm::AD_Intel
111     };
112
113     let asm = CString::new(ia.asm.as_bytes()).unwrap();
114     let constraint_cstr = CString::new(all_constraints).unwrap();
115     let r = InlineAsmCall(bcx,
116                           asm.as_ptr(),
117                           constraint_cstr.as_ptr(),
118                           &inputs,
119                           output_type,
120                           ia.volatile,
121                           ia.alignstack,
122                           dialect);
123
124     // Again, based on how many outputs we have
125     if num_outputs == 1 {
126         Store(bcx, r, outputs[0]);
127     } else {
128         for (i, o) in outputs.iter().enumerate() {
129             let v = ExtractValue(bcx, r, i);
130             Store(bcx, v, *o);
131         }
132     }
133
134     // Store expn_id in a metadata node so we can map LLVM errors
135     // back to source locations.  See #17552.
136     unsafe {
137         let key = "srcloc";
138         let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx().llcx(),
139             key.as_ptr() as *const c_char, key.len() as c_uint);
140
141         let val: llvm::ValueRef = C_i32(bcx.ccx(), ia.expn_id.into_u32() as i32);
142
143         llvm::LLVMSetMetadata(r, kind,
144             llvm::LLVMMDNodeInContext(bcx.ccx().llcx(), &val, 1));
145     }
146
147     return bcx;
148
149 }
150