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