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