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