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.
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.
12 # Translation of inline assembly.
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;
25 use std::c_str::ToCStr;
26 use std::string::String;
28 use libc::{c_uint, c_char};
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> {
35 let mut constraints = Vec::new();
36 let mut output_types = Vec::new();
38 let temp_scope = fcx.push_custom_cleanup_scope();
40 let mut ext_inputs = Vec::new();
41 let mut ext_constraints = Vec::new();
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());
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;
51 ext_inputs.push(unpack_result!(bcx, {
52 callee::trans_arg_datum(bcx,
55 cleanup::CustomScope(temp_scope),
56 callee::DontAutorefArg)
58 ext_constraints.push(i.to_string());
62 }).collect::<Vec<_>>();
64 // Now the input operands
65 let inputs = ia.inputs.iter().map(|&(ref c, ref input)| {
66 constraints.push((*c).clone());
68 let in_datum = unpack_datum!(bcx, expr::trans(bcx, &**input));
70 callee::trans_arg_datum(bcx,
71 expr_ty(bcx, &**input),
73 cleanup::CustomScope(temp_scope),
74 callee::DontAutorefArg)
76 }).collect::<Vec<_>>().append(ext_inputs.as_slice());
78 // no failure occurred preparing operands, no need to cleanup
79 fcx.pop_custom_cleanup_scope(temp_scope);
82 String::from_str(constraints.iter()
83 .map(|s| s.get().to_string())
84 .chain(ext_constraints.into_iter())
85 .collect::<Vec<String>>()
89 let mut clobbers = get_clobbers();
90 if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {
91 clobbers = format!("{},{}", ia.clobbers.get(), clobbers);
93 clobbers.push_str(ia.clobbers.get());
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());
101 constraints.push_str(clobbers.as_slice());
104 debug!("Asm Constraints: {:?}", constraints.as_slice());
106 let num_outputs = outputs.len();
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 {
114 Type::struct_(bcx.ccx(), output_types.as_slice(), false)
117 let dialect = match ia.dialect {
118 ast::AsmAtt => llvm::AD_ATT,
119 ast::AsmIntel => llvm::AD_Intel
122 let r = ia.asm.get().with_c_str(|a| {
123 constraints.as_slice().with_c_str(|c| {
135 // Again, based on how many outputs we have
136 if num_outputs == 1 {
137 Store(bcx, r, *outputs.get(0));
139 for (i, o) in outputs.iter().enumerate() {
140 let v = ExtractValue(bcx, r, i);
145 // Store expn_id in a metadata node so we can map LLVM errors
146 // back to source locations. See #17552.
149 let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx().llcx(),
150 key.as_ptr() as *const c_char, key.len() as c_uint);
152 let val: llvm::ValueRef = C_i32(bcx.ccx(), ia.expn_id.to_llvm_cookie());
154 llvm::LLVMSetMetadata(r, kind,
155 llvm::LLVMMDNodeInContext(bcx.ccx().llcx(), &val, 1));
162 // Default per-arch clobbers
163 // Basically what clang does
165 #[cfg(any(target_arch = "arm",
166 target_arch = "mips",
167 target_arch = "mipsel"))]
168 fn get_clobbers() -> String {
172 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
173 fn get_clobbers() -> String {
174 "~{dirflag},~{fpsr},~{flags}".to_string()