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.
20 use trans::cleanup::CleanupMethods;
23 use 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 mut 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<_>>();
77 inputs.push_all(ext_inputs.as_slice());
79 // no failure occurred preparing operands, no need to cleanup
80 fcx.pop_custom_cleanup_scope(temp_scope);
83 String::from_str(constraints.iter()
84 .map(|s| s.get().to_string())
85 .chain(ext_constraints.into_iter())
86 .collect::<Vec<String>>()
90 let mut clobbers = get_clobbers();
91 if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {
92 clobbers = format!("{},{}", ia.clobbers.get(), clobbers);
94 clobbers.push_str(ia.clobbers.get());
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());
102 constraints.push_str(clobbers.as_slice());
105 debug!("Asm Constraints: {}", constraints.as_slice());
107 let num_outputs = outputs.len();
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 {
115 Type::struct_(bcx.ccx(), output_types.as_slice(), false)
118 let dialect = match ia.dialect {
119 ast::AsmAtt => llvm::AD_ATT,
120 ast::AsmIntel => llvm::AD_Intel
123 let r = ia.asm.get().with_c_str(|a| {
124 constraints.as_slice().with_c_str(|c| {
136 // Again, based on how many outputs we have
137 if num_outputs == 1 {
138 Store(bcx, r, outputs[0]);
140 for (i, o) in outputs.iter().enumerate() {
141 let v = ExtractValue(bcx, r, i);
146 // Store expn_id in a metadata node so we can map LLVM errors
147 // back to source locations. See #17552.
150 let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx().llcx(),
151 key.as_ptr() as *const c_char, key.len() as c_uint);
153 let val: llvm::ValueRef = C_i32(bcx.ccx(), ia.expn_id.to_llvm_cookie());
155 llvm::LLVMSetMetadata(r, kind,
156 llvm::LLVMMDNodeInContext(bcx.ccx().llcx(), &val, 1));
163 // Default per-arch clobbers
164 // Basically what clang does
166 #[cfg(any(target_arch = "arm",
167 target_arch = "mips",
168 target_arch = "mipsel"))]
169 fn get_clobbers() -> String {
173 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
174 fn get_clobbers() -> String {
175 "~{dirflag},~{fpsr},~{flags}".to_string()