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.
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.
11 //! # Translation of inline assembly.
18 use trans::cleanup::CleanupMethods;
21 use trans::type_::Type;
24 use std::ffi::CString;
25 use libc::{c_uint, c_char};
27 // Take an inline assembly expression and splat it out via LLVM
28 pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ia: &ast::InlineAsm)
29 -> Block<'blk, 'tcx> {
32 let mut constraints = Vec::new();
33 let mut output_types = Vec::new();
35 let temp_scope = fcx.push_custom_cleanup_scope();
37 let mut ext_inputs = Vec::new();
38 let mut ext_constraints = Vec::new();
40 // Prepare the output operands
41 let outputs = ia.outputs.iter().enumerate().map(|(i, &(ref c, ref out, is_rw))| {
42 constraints.push((*c).clone());
44 let out_datum = unpack_datum!(bcx, expr::trans(bcx, &**out));
45 output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
46 let val = out_datum.val;
48 bcx = callee::trans_arg_datum(bcx,
51 cleanup::CustomScope(temp_scope),
52 callee::DontAutorefArg,
54 ext_constraints.push(i.to_string());
58 }).collect::<Vec<_>>();
60 // Now the input operands
61 let mut inputs = Vec::new();
62 for &(ref c, ref input) in &ia.inputs {
63 constraints.push((*c).clone());
65 let in_datum = unpack_datum!(bcx, expr::trans(bcx, &**input));
66 bcx = callee::trans_arg_datum(bcx,
67 expr_ty(bcx, &**input),
69 cleanup::CustomScope(temp_scope),
70 callee::DontAutorefArg,
73 inputs.push_all(&ext_inputs[..]);
75 // no failure occurred preparing operands, no need to cleanup
76 fcx.pop_custom_cleanup_scope(temp_scope);
78 let clobbers = ia.clobbers.iter()
79 .map(|s| format!("~{{{}}}", &s));
81 // Default per-arch clobbers
82 // Basically what clang does
83 let arch_clobbers = match &bcx.sess().target.target.arch[..] {
84 "x86" | "x86_64" => vec!("~{dirflag}", "~{fpsr}", "~{flags}"),
88 let all_constraints= constraints.iter()
89 .map(|s| s.to_string())
90 .chain(ext_constraints)
92 .chain(arch_clobbers.iter()
93 .map(|s| s.to_string()))
94 .collect::<Vec<String>>()
97 debug!("Asm Constraints: {}", &all_constraints[..]);
99 // Depending on how many outputs we have, the return type is different
100 let num_outputs = outputs.len();
101 let output_type = match num_outputs {
102 0 => Type::void(bcx.ccx()),
103 1 => output_types[0],
104 _ => Type::struct_(bcx.ccx(), &output_types[..], false)
107 let dialect = match ia.dialect {
108 ast::AsmAtt => llvm::AD_ATT,
109 ast::AsmIntel => llvm::AD_Intel
112 let asm = CString::new(ia.asm.as_bytes()).unwrap();
113 let constraint_cstr = CString::new(all_constraints).unwrap();
114 let r = InlineAsmCall(bcx,
116 constraint_cstr.as_ptr(),
123 // Again, based on how many outputs we have
124 if num_outputs == 1 {
125 Store(bcx, r, outputs[0]);
127 for (i, o) in outputs.iter().enumerate() {
128 let v = ExtractValue(bcx, r, i);
133 // Store expn_id in a metadata node so we can map LLVM errors
134 // back to source locations. See #17552.
137 let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx().llcx(),
138 key.as_ptr() as *const c_char, key.len() as c_uint);
140 let val: llvm::ValueRef = C_i32(bcx.ccx(), ia.expn_id.into_u32() as i32);
142 llvm::LLVMSetMetadata(r, kind,
143 llvm::LLVMMDNodeInContext(bcx.ccx().llcx(), &val, 1));