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.
13 use llvm::{self, ValueRef};
17 use trans::cleanup::CleanupMethods;
18 use trans::datum::{Datum, Expr};
21 use trans::type_::Type;
23 use rustc_front::hir as ast;
24 use std::ffi::CString;
25 use syntax::ast::AsmDialect;
26 use libc::{c_uint, c_char};
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> {
33 let mut constraints = Vec::new();
34 let mut output_types = Vec::new();
36 let temp_scope = fcx.push_custom_cleanup_scope();
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
44 let arg_datum = unpack_datum!(
45 bcx, arg_datum.to_rvalue_datum(bcx, "arg"));
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));
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));
61 let mut ext_inputs = Vec::new();
62 let mut ext_constraints = Vec::new();
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());
70 let out_datum = unpack_datum!(bcx, expr::trans(bcx, &out.expr));
72 bcx = take_datum(bcx, out_datum, &mut inputs);
74 ext_inputs.push(*inputs.last().unwrap());
75 ext_constraints.push(i.to_string());
78 output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
79 outputs.push(out_datum.val);
81 bcx = take_datum(bcx, out_datum, &mut ext_inputs);
82 ext_constraints.push(i.to_string());
87 // Now the input operands
88 for &(ref c, ref input) in &ia.inputs {
89 constraints.push((*c).clone());
91 let in_datum = unpack_datum!(bcx, expr::trans(bcx, &input));
92 bcx = take_datum(bcx, in_datum, &mut inputs);
94 inputs.extend_from_slice(&ext_inputs[..]);
96 // no failure occurred preparing operands, no need to cleanup
97 fcx.pop_custom_cleanup_scope(temp_scope);
99 let clobbers = ia.clobbers.iter()
100 .map(|s| format!("~{{{}}}", &s));
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}"),
109 let all_constraints= constraints.iter()
110 .map(|s| s.to_string())
111 .chain(ext_constraints)
113 .chain(arch_clobbers.iter()
114 .map(|s| s.to_string()))
115 .collect::<Vec<String>>()
118 debug!("Asm Constraints: {}", &all_constraints[..]);
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)
128 let dialect = match ia.dialect {
129 AsmDialect::Att => llvm::AD_ATT,
130 AsmDialect::Intel => llvm::AD_Intel
133 let asm = CString::new(ia.asm.as_bytes()).unwrap();
134 let constraint_cstr = CString::new(all_constraints).unwrap();
135 let r = InlineAsmCall(bcx,
137 constraint_cstr.as_ptr(),
144 // Again, based on how many outputs we have
145 if num_outputs == 1 {
146 Store(bcx, r, outputs[0]);
148 for (i, o) in outputs.iter().enumerate() {
149 let v = ExtractValue(bcx, r, i);
154 // Store expn_id in a metadata node so we can map LLVM errors
155 // back to source locations. See #17552.
158 let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx().llcx(),
159 key.as_ptr() as *const c_char, key.len() as c_uint);
161 let val: llvm::ValueRef = C_i32(bcx.ccx(), ia.expn_id.into_u32() as i32);
163 llvm::LLVMSetMetadata(r, kind,
164 llvm::LLVMMDNodeInContext(bcx.ccx().llcx(), &val, 1));