]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/asm.rs
Generalized base.rs#call_memcpy and everything that it uses
[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 common::*;
13 use type_::Type;
14 use type_of::LayoutLlvmExt;
15 use builder::Builder;
16 use value::Value;
17
18 use rustc::hir;
19 use traits::BuilderMethods;
20
21 use mir::place::PlaceRef;
22 use mir::operand::OperandValue;
23
24 use std::ffi::CString;
25 use syntax::ast::AsmDialect;
26 use libc::{c_uint, c_char};
27
28 // Take an inline assembly expression and splat it out via LLVM
29 pub fn codegen_inline_asm(
30     bx: &Builder<'a, 'll, 'tcx>,
31     ia: &hir::InlineAsm,
32     outputs: Vec<PlaceRef<'tcx, &'ll Value>>,
33     mut inputs: Vec<&'ll Value>
34 ) -> bool {
35     let mut ext_constraints = vec![];
36     let mut output_types = vec![];
37
38     // Prepare the output operands
39     let mut indirect_outputs = vec![];
40     for (i, (out, place)) in ia.outputs.iter().zip(&outputs).enumerate() {
41         if out.is_rw {
42             inputs.push(place.load(bx).immediate());
43             ext_constraints.push(i.to_string());
44         }
45         if out.is_indirect {
46             indirect_outputs.push(place.load(bx).immediate());
47         } else {
48             output_types.push(place.layout.llvm_type(bx.cx));
49         }
50     }
51     if !indirect_outputs.is_empty() {
52         indirect_outputs.extend_from_slice(&inputs);
53         inputs = indirect_outputs;
54     }
55
56     let clobbers = ia.clobbers.iter()
57                               .map(|s| format!("~{{{}}}", &s));
58
59     // Default per-arch clobbers
60     // Basically what clang does
61     let arch_clobbers = match &bx.sess().target.target.arch[..] {
62         "x86" | "x86_64"  => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
63         "mips" | "mips64" => vec!["~{$1}"],
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(bx.cx),
81         1 => output_types[0],
82         _ => Type::struct_(bx.cx, &output_types, false)
83     };
84
85     let dialect = match ia.dialect {
86         AsmDialect::Att   => llvm::AsmDialect::Att,
87         AsmDialect::Intel => llvm::AsmDialect::Intel,
88     };
89
90     let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
91     let constraint_cstr = CString::new(all_constraints).unwrap();
92     let r = bx.inline_asm_call(
93         asm.as_ptr(),
94         constraint_cstr.as_ptr(),
95         &inputs,
96         output_type,
97         ia.volatile,
98         ia.alignstack,
99         dialect
100     );
101     if r.is_none() {
102         return false;
103     }
104     let r = r.unwrap();
105
106     // Again, based on how many outputs we have
107     let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
108     for (i, (_, &place)) in outputs.enumerate() {
109         let v = if num_outputs == 1 { r } else { bx.extract_value(r, i as u64) };
110         OperandValue::Immediate(v).store(bx, place);
111     }
112
113     // Store mark in a metadata node so we can map LLVM errors
114     // back to source locations.  See #17552.
115     unsafe {
116         let key = "srcloc";
117         let kind = llvm::LLVMGetMDKindIDInContext(bx.cx.llcx,
118             key.as_ptr() as *const c_char, key.len() as c_uint);
119
120         let val: &'ll Value = C_i32(bx.cx, ia.ctxt.outer().as_u32() as i32);
121
122         llvm::LLVMSetMetadata(r, kind,
123             llvm::LLVMMDNodeInContext(bx.cx.llcx, &val, 1));
124     }
125
126     return true;
127 }
128
129 pub fn codegen_global_asm<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
130                                   ga: &hir::GlobalAsm) {
131     let asm = CString::new(ga.asm.as_str().as_bytes()).unwrap();
132     unsafe {
133         llvm::LLVMRustAppendModuleInlineAsm(cx.llmod, asm.as_ptr());
134     }
135 }