]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/asm.rs
New upstream version 1.14.0+dfsg1
[rustc.git] / src / librustc_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 base;
15 use build::*;
16 use common::*;
17 use type_of;
18 use type_::Type;
19
20 use rustc::hir;
21 use rustc::ty::Ty;
22
23 use std::ffi::CString;
24 use syntax::ast::AsmDialect;
25 use libc::{c_uint, c_char};
26
27 // Take an inline assembly expression and splat it out via LLVM
28 pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
29 ia: &hir::InlineAsm,
30 outputs: Vec<(ValueRef, Ty<'tcx>)>,
31 mut inputs: Vec<ValueRef>) {
32 let mut ext_constraints = vec![];
33 let mut output_types = vec![];
34
35 // Prepare the output operands
36 let mut indirect_outputs = vec![];
37 for (i, (out, &(val, ty))) in ia.outputs.iter().zip(&outputs).enumerate() {
38 let val = if out.is_rw || out.is_indirect {
39 Some(base::load_ty(bcx, val, ty))
40 } else {
41 None
42 };
43 if out.is_rw {
44 inputs.push(val.unwrap());
45 ext_constraints.push(i.to_string());
46 }
47 if out.is_indirect {
48 indirect_outputs.push(val.unwrap());
49 } else {
50 output_types.push(type_of::type_of(bcx.ccx(), ty));
51 }
52 }
53 if !indirect_outputs.is_empty() {
54 indirect_outputs.extend_from_slice(&inputs);
55 inputs = indirect_outputs;
56 }
57
58 let clobbers = ia.clobbers.iter()
59 .map(|s| format!("~{{{}}}", &s));
60
61 // Default per-arch clobbers
62 // Basically what clang does
63 let arch_clobbers = match &bcx.sess().target.target.arch[..] {
64 "x86" | "x86_64" => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
65 _ => Vec::new()
66 };
67
68 let all_constraints =
69 ia.outputs.iter().map(|out| out.constraint.to_string())
70 .chain(ia.inputs.iter().map(|s| s.to_string()))
71 .chain(ext_constraints)
72 .chain(clobbers)
73 .chain(arch_clobbers.iter().map(|s| s.to_string()))
74 .collect::<Vec<String>>().join(",");
75
76 debug!("Asm Constraints: {}", &all_constraints[..]);
77
78 // Depending on how many outputs we have, the return type is different
79 let num_outputs = output_types.len();
80 let output_type = match num_outputs {
81 0 => Type::void(bcx.ccx()),
82 1 => output_types[0],
83 _ => Type::struct_(bcx.ccx(), &output_types[..], false)
84 };
85
86 let dialect = match ia.dialect {
87 AsmDialect::Att => llvm::AsmDialect::Att,
88 AsmDialect::Intel => llvm::AsmDialect::Intel,
89 };
90
91 let asm = CString::new(ia.asm.as_bytes()).unwrap();
92 let constraint_cstr = CString::new(all_constraints).unwrap();
93 let r = InlineAsmCall(bcx,
94 asm.as_ptr(),
95 constraint_cstr.as_ptr(),
96 &inputs,
97 output_type,
98 ia.volatile,
99 ia.alignstack,
100 dialect);
101
102 // Again, based on how many outputs we have
103 let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
104 for (i, (_, &(val, _))) in outputs.enumerate() {
105 let v = if num_outputs == 1 { r } else { ExtractValue(bcx, r, i) };
106 Store(bcx, v, val);
107 }
108
109 // Store expn_id in a metadata node so we can map LLVM errors
110 // back to source locations. See #17552.
111 unsafe {
112 let key = "srcloc";
113 let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx().llcx(),
114 key.as_ptr() as *const c_char, key.len() as c_uint);
115
116 let val: llvm::ValueRef = C_i32(bcx.ccx(), ia.expn_id.into_u32() as i32);
117
118 llvm::LLVMSetMetadata(r, kind,
119 llvm::LLVMMDNodeInContext(bcx.ccx().llcx(), &val, 1));
120 }
121 }