]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/asm.rs
15eceaf405f7f01ad585597ba3b5ea658598b2cb
[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_of::LayoutLlvmExt;
14 use builder::Builder;
15 use value::Value;
16
17 use rustc::hir;
18 use interfaces::{BuilderMethods, CommonMethods, TypeMethods};
19
20 use mir::place::PlaceRef;
21 use mir::operand::OperandValue;
22
23 use std::ffi::CString;
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(
28     bx: &Builder<'a, 'll, 'tcx>,
29     ia: &hir::InlineAsm,
30     outputs: Vec<PlaceRef<'tcx, &'ll Value>>,
31     mut inputs: Vec<&'ll Value>
32 ) -> bool {
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 => bx.cx().void(),
79         1 => output_types[0],
80         _ => bx.cx().struct_(&output_types, false)
81     };
82
83     let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
84     let constraint_cstr = CString::new(all_constraints).unwrap();
85     let r = bx.inline_asm_call(
86         asm.as_ptr(),
87         constraint_cstr.as_ptr(),
88         &inputs,
89         output_type,
90         ia.volatile,
91         ia.alignstack,
92         ia.dialect
93     );
94     if r.is_none() {
95         return false;
96     }
97     let r = r.unwrap();
98
99     // Again, based on how many outputs we have
100     let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
101     for (i, (_, &place)) in outputs.enumerate() {
102         let v = if num_outputs == 1 { r } else { bx.extract_value(r, i as u64) };
103         OperandValue::Immediate(v).store(bx, place);
104     }
105
106     // Store mark in a metadata node so we can map LLVM errors
107     // back to source locations.  See #17552.
108     unsafe {
109         let key = "srcloc";
110         let kind = llvm::LLVMGetMDKindIDInContext(bx.cx().llcx,
111             key.as_ptr() as *const c_char, key.len() as c_uint);
112
113         let val: &'ll Value = bx.cx().c_i32(ia.ctxt.outer().as_u32() as i32);
114
115         llvm::LLVMSetMetadata(r, kind,
116             llvm::LLVMMDNodeInContext(bx.cx().llcx, &val, 1));
117     }
118
119     return true;
120 }
121
122 pub fn codegen_global_asm<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
123                                   ga: &hir::GlobalAsm) {
124     let asm = CString::new(ga.asm.as_str().as_bytes()).unwrap();
125     unsafe {
126         llvm::LLVMRustAppendModuleInlineAsm(cx.llmod, asm.as_ptr());
127     }
128 }