]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/asm.rs
All Builder methods now take &mut self instead of &self
[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 rustc_codegen_ssa::interfaces::*;
19
20 use rustc_codegen_ssa::mir::place::PlaceRef;
21 use rustc_codegen_ssa::mir::operand::OperandValue;
22
23 use std::ffi::CString;
24 use libc::{c_uint, c_char};
25
26
27 impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
28     fn codegen_inline_asm(
29         &mut self,
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(self.load_operand(place).immediate());
42                 ext_constraints.push(i.to_string());
43             }
44             if out.is_indirect {
45                 indirect_outputs.push(self.load_operand(place).immediate());
46             } else {
47                 output_types.push(place.layout.llvm_type(self.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 &self.cx().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 => self.cx().type_void(),
80             1 => output_types[0],
81             _ => self.cx().type_struct(&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 = self.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 { self.extract_value(r, i as u64) };
104             OperandValue::Immediate(v).store(self, 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(self.cx().llcx,
112                 key.as_ptr() as *const c_char, key.len() as c_uint);
113
114             let val: &'ll Value = self.cx().const_i32(ia.ctxt.outer().as_u32() as i32);
115
116             llvm::LLVMSetMetadata(r, kind,
117                 llvm::LLVMMDNodeInContext(self.cx().llcx, &val, 1));
118         }
119
120         true
121     }
122 }
123
124 impl AsmMethods<'tcx> for CodegenCx<'ll, 'tcx> {
125     fn codegen_global_asm(&self, ga: &hir::GlobalAsm) {
126         let asm = CString::new(ga.asm.as_str().as_bytes()).unwrap();
127         unsafe {
128             llvm::LLVMRustAppendModuleInlineAsm(self.llmod, asm.as_ptr());
129         }
130     }
131 }