]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/asm.rs
27c3dfc4c974d0987ffb33b7f3d94856804e2e38
[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::{self, ValueRef};
14 use trans::build::*;
15 use trans::common::*;
16 use trans::cleanup;
17 use trans::cleanup::CleanupMethods;
18 use trans::datum::{Datum, Expr};
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 take_datum = |mut bcx: Block<'blk, 'tcx>,
39                       arg_datum: Datum<'tcx, Expr>,
40                       llargs: &mut Vec<ValueRef>|
41                       -> Block<'blk, 'tcx> {
42         // Make this an rvalue, since we are going to be
43         // passing ownership.
44         let arg_datum = unpack_datum!(
45             bcx, arg_datum.to_rvalue_datum(bcx, "arg"));
46
47         // Now that arg_datum is owned, get it into the appropriate
48         // mode (ref vs value).
49         let arg_datum = unpack_datum!(
50             bcx, arg_datum.to_appropriate_datum(bcx));
51
52         // Technically, ownership of val passes to the callee.
53         // However, we must cleanup should we panic before the
54         // callee is actually invoked.
55         let val = arg_datum.add_clean(bcx.fcx,
56             cleanup::CustomScope(temp_scope));
57         llargs.push(val);
58         bcx
59     };
60
61     let mut ext_inputs = Vec::new();
62     let mut ext_constraints = Vec::new();
63
64     // Prepare the output operands
65     let mut outputs = Vec::new();
66     let mut inputs = Vec::new();
67     for (i, out) in ia.outputs.iter().enumerate() {
68         constraints.push(out.constraint.clone());
69
70         let out_datum = unpack_datum!(bcx, expr::trans(bcx, &out.expr));
71         if out.is_indirect {
72             bcx = take_datum(bcx, out_datum, &mut inputs);
73             if out.is_rw {
74                 ext_inputs.push(*inputs.last().unwrap());
75                 ext_constraints.push(i.to_string());
76             }
77         } else {
78             output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
79             outputs.push(out_datum.val);
80             if out.is_rw {
81                 bcx = take_datum(bcx, out_datum, &mut ext_inputs);
82                 ext_constraints.push(i.to_string());
83             }
84         }
85     }
86
87     // Now the input operands
88     for &(ref c, ref input) in &ia.inputs {
89         constraints.push((*c).clone());
90
91         let in_datum = unpack_datum!(bcx, expr::trans(bcx, &input));
92         bcx = take_datum(bcx, in_datum, &mut inputs);
93     }
94     inputs.extend_from_slice(&ext_inputs[..]);
95
96     // no failure occurred preparing operands, no need to cleanup
97     fcx.pop_custom_cleanup_scope(temp_scope);
98
99     let clobbers = ia.clobbers.iter()
100                               .map(|s| format!("~{{{}}}", &s));
101
102     // Default per-arch clobbers
103     // Basically what clang does
104     let arch_clobbers = match &bcx.sess().target.target.arch[..] {
105         "x86" | "x86_64" => vec!("~{dirflag}", "~{fpsr}", "~{flags}"),
106         _                => Vec::new()
107     };
108
109     let all_constraints= constraints.iter()
110                                     .map(|s| s.to_string())
111                                     .chain(ext_constraints)
112                                     .chain(clobbers)
113                                     .chain(arch_clobbers.iter()
114                                                .map(|s| s.to_string()))
115                                     .collect::<Vec<String>>()
116                                     .join(",");
117
118     debug!("Asm Constraints: {}", &all_constraints[..]);
119
120     // Depending on how many outputs we have, the return type is different
121     let num_outputs = outputs.len();
122     let output_type = match num_outputs {
123         0 => Type::void(bcx.ccx()),
124         1 => output_types[0],
125         _ => Type::struct_(bcx.ccx(), &output_types[..], false)
126     };
127
128     let dialect = match ia.dialect {
129         AsmDialect::Att   => llvm::AD_ATT,
130         AsmDialect::Intel => llvm::AD_Intel
131     };
132
133     let asm = CString::new(ia.asm.as_bytes()).unwrap();
134     let constraint_cstr = CString::new(all_constraints).unwrap();
135     let r = InlineAsmCall(bcx,
136                           asm.as_ptr(),
137                           constraint_cstr.as_ptr(),
138                           &inputs,
139                           output_type,
140                           ia.volatile,
141                           ia.alignstack,
142                           dialect);
143
144     // Again, based on how many outputs we have
145     if num_outputs == 1 {
146         Store(bcx, r, outputs[0]);
147     } else {
148         for (i, o) in outputs.iter().enumerate() {
149             let v = ExtractValue(bcx, r, i);
150             Store(bcx, v, *o);
151         }
152     }
153
154     // Store expn_id in a metadata node so we can map LLVM errors
155     // back to source locations.  See #17552.
156     unsafe {
157         let key = "srcloc";
158         let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx().llcx(),
159             key.as_ptr() as *const c_char, key.len() as c_uint);
160
161         let val: llvm::ValueRef = C_i32(bcx.ccx(), ia.expn_id.into_u32() as i32);
162
163         llvm::LLVMSetMetadata(r, kind,
164             llvm::LLVMMDNodeInContext(bcx.ccx().llcx(), &val, 1));
165     }
166
167     return bcx;
168
169 }