]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/asm.rs
028596950f5b862732ea4dfb7609d9de24c0f8a4
[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;
12 use context::CodegenCx;
13 use type_::Type;
14 use type_of::LayoutLlvmExt;
15 use builder::Builder;
16 use value::Value;
17
18 use rustc::hir;
19 use interfaces::{BuilderMethods, CommonMethods};
20
21 use mir::place::PlaceRef;
22 use mir::operand::OperandValue;
23
24 use std::ffi::CString;
25 use libc::{c_uint, c_char};
26
27 // Take an inline assembly expression and splat it out via LLVM
28 pub fn codegen_inline_asm(
29     bx: &Builder<'a, 'll, 'tcx>,
30     ia: &hir::InlineAsm,
31     outputs: Vec<PlaceRef<'tcx, &'ll Value>>,
32     mut inputs: Vec<&'ll Value>
33 ) -> bool {
34     let mut ext_constraints = vec![];
35     let mut output_types = vec![];
36
37     // Prepare the output operands
38     let mut indirect_outputs = vec![];
39     for (i, (out, place)) in ia.outputs.iter().zip(&outputs).enumerate() {
40         if out.is_rw {
41             inputs.push(place.load(bx).immediate());
42             ext_constraints.push(i.to_string());
43         }
44         if out.is_indirect {
45             indirect_outputs.push(place.load(bx).immediate());
46         } else {
47             output_types.push(place.layout.llvm_type(bx.cx()));
48         }
49     }
50     if !indirect_outputs.is_empty() {
51         indirect_outputs.extend_from_slice(&inputs);
52         inputs = indirect_outputs;
53     }
54
55     let clobbers = ia.clobbers.iter()
56                               .map(|s| format!("~{{{}}}", &s));
57
58     // Default per-arch clobbers
59     // Basically what clang does
60     let arch_clobbers = match &bx.sess().target.target.arch[..] {
61         "x86" | "x86_64"  => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
62         "mips" | "mips64" => vec!["~{$1}"],
63         _                 => Vec::new()
64     };
65
66     let all_constraints =
67         ia.outputs.iter().map(|out| out.constraint.to_string())
68           .chain(ia.inputs.iter().map(|s| s.to_string()))
69           .chain(ext_constraints)
70           .chain(clobbers)
71           .chain(arch_clobbers.iter().map(|s| s.to_string()))
72           .collect::<Vec<String>>().join(",");
73
74     debug!("Asm Constraints: {}", &all_constraints);
75
76     // Depending on how many outputs we have, the return type is different
77     let num_outputs = output_types.len();
78     let output_type = match num_outputs {
79         0 => Type::void(bx.cx()),
80         1 => output_types[0],
81         _ => Type::struct_(bx.cx(), &output_types, false)
82     };
83
84     let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
85     let constraint_cstr = CString::new(all_constraints).unwrap();
86     let r = bx.inline_asm_call(
87         asm.as_ptr(),
88         constraint_cstr.as_ptr(),
89         &inputs,
90         output_type,
91         ia.volatile,
92         ia.alignstack,
93         ia.dialect
94     );
95     if r.is_none() {
96         return false;
97     }
98     let r = r.unwrap();
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: &'ll Value = CodegenCx::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     return true;
121 }
122
123 pub fn codegen_global_asm<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
124                                   ga: &hir::GlobalAsm) {
125     let asm = CString::new(ga.asm.as_str().as_bytes()).unwrap();
126     unsafe {
127         llvm::LLVMRustAppendModuleInlineAsm(cx.llmod, asm.as_ptr());
128     }
129 }