]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/asm.rs
Auto merge of #50603 - eddyb:issue-49955, r=nikomatsakis
[rust.git] / src / librustc_codegen_llvm / 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 use llvm::{self, ValueRef};
12 use common::*;
13 use type_::Type;
14 use type_of::LayoutLlvmExt;
15 use builder::Builder;
16
17 use rustc::hir;
18
19 use mir::place::PlaceRef;
20 use mir::operand::OperandValue;
21
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 codegen_inline_asm<'a, 'tcx>(
28     bx: &Builder<'a, 'tcx>,
29     ia: &hir::InlineAsm,
30     outputs: Vec<PlaceRef<'tcx>>,
31     mut inputs: Vec<ValueRef>
32 ) {
33     let mut ext_constraints = vec![];
34     let mut output_types = vec![];
35
36     // Prepare the output operands
37     let mut indirect_outputs = vec![];
38     for (i, (out, place)) in ia.outputs.iter().zip(&outputs).enumerate() {
39         if out.is_rw {
40             inputs.push(place.load(bx).immediate());
41             ext_constraints.push(i.to_string());
42         }
43         if out.is_indirect {
44             indirect_outputs.push(place.load(bx).immediate());
45         } else {
46             output_types.push(place.layout.llvm_type(bx.cx));
47         }
48     }
49     if !indirect_outputs.is_empty() {
50         indirect_outputs.extend_from_slice(&inputs);
51         inputs = indirect_outputs;
52     }
53
54     let clobbers = ia.clobbers.iter()
55                               .map(|s| format!("~{{{}}}", &s));
56
57     // Default per-arch clobbers
58     // Basically what clang does
59     let arch_clobbers = match &bx.sess().target.target.arch[..] {
60         "x86" | "x86_64"  => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
61         "mips" | "mips64" => vec!["~{$1}"],
62         _                 => Vec::new()
63     };
64
65     let all_constraints =
66         ia.outputs.iter().map(|out| out.constraint.to_string())
67           .chain(ia.inputs.iter().map(|s| s.to_string()))
68           .chain(ext_constraints)
69           .chain(clobbers)
70           .chain(arch_clobbers.iter().map(|s| s.to_string()))
71           .collect::<Vec<String>>().join(",");
72
73     debug!("Asm Constraints: {}", &all_constraints);
74
75     // Depending on how many outputs we have, the return type is different
76     let num_outputs = output_types.len();
77     let output_type = match num_outputs {
78         0 => Type::void(bx.cx),
79         1 => output_types[0],
80         _ => Type::struct_(bx.cx, &output_types, false)
81     };
82
83     let dialect = match ia.dialect {
84         AsmDialect::Att   => llvm::AsmDialect::Att,
85         AsmDialect::Intel => llvm::AsmDialect::Intel,
86     };
87
88     let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
89     let constraint_cstr = CString::new(all_constraints).unwrap();
90     let r = bx.inline_asm_call(
91         asm.as_ptr(),
92         constraint_cstr.as_ptr(),
93         &inputs,
94         output_type,
95         ia.volatile,
96         ia.alignstack,
97         dialect
98     );
99
100     // Again, based on how many outputs we have
101     let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
102     for (i, (_, &place)) in outputs.enumerate() {
103         let v = if num_outputs == 1 { r } else { bx.extract_value(r, i as u64) };
104         OperandValue::Immediate(v).store(bx, place);
105     }
106
107     // Store mark in a metadata node so we can map LLVM errors
108     // back to source locations.  See #17552.
109     unsafe {
110         let key = "srcloc";
111         let kind = llvm::LLVMGetMDKindIDInContext(bx.cx.llcx,
112             key.as_ptr() as *const c_char, key.len() as c_uint);
113
114         let val: llvm::ValueRef = C_i32(bx.cx, ia.ctxt.outer().as_u32() as i32);
115
116         llvm::LLVMSetMetadata(r, kind,
117             llvm::LLVMMDNodeInContext(bx.cx.llcx, &val, 1));
118     }
119 }
120
121 pub fn codegen_global_asm<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
122                                   ga: &hir::GlobalAsm) {
123     let asm = CString::new(ga.asm.as_str().as_bytes()).unwrap();
124     unsafe {
125         llvm::LLVMRustAppendModuleInlineAsm(cx.llmod, asm.as_ptr());
126     }
127 }