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.
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.
12 use context::CodegenCx;
13 use type_of::LayoutLlvmExt;
18 use rustc_codegen_ssa::interfaces::*;
20 use rustc_codegen_ssa::mir::place::PlaceRef;
21 use rustc_codegen_ssa::mir::operand::OperandValue;
23 use std::ffi::CString;
24 use libc::{c_uint, c_char};
27 impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
28 fn codegen_inline_asm(
31 outputs: Vec<PlaceRef<'tcx, &'ll Value>>,
32 mut inputs: Vec<&'ll Value>
34 let mut ext_constraints = vec![];
35 let mut output_types = vec![];
37 // Prepare the output operands
38 let mut indirect_outputs = vec![];
39 for (i, (out, &place)) in ia.outputs.iter().zip(&outputs).enumerate() {
41 inputs.push(self.load_operand(place).immediate());
42 ext_constraints.push(i.to_string());
45 indirect_outputs.push(self.load_operand(place).immediate());
47 output_types.push(place.layout.llvm_type(self.cx()));
50 if !indirect_outputs.is_empty() {
51 indirect_outputs.extend_from_slice(&inputs);
52 inputs = indirect_outputs;
55 let clobbers = ia.clobbers.iter()
56 .map(|s| format!("~{{{}}}", &s));
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}"],
67 ia.outputs.iter().map(|out| out.constraint.to_string())
68 .chain(ia.inputs.iter().map(|s| s.to_string()))
69 .chain(ext_constraints)
71 .chain(arch_clobbers.iter().map(|s| s.to_string()))
72 .collect::<Vec<String>>().join(",");
74 debug!("Asm Constraints: {}", &all_constraints);
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(),
81 _ => self.cx().type_struct(&output_types, false)
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(
88 constraint_cstr.as_ptr(),
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);
107 // Store mark in a metadata node so we can map LLVM errors
108 // back to source locations. See #17552.
111 let kind = llvm::LLVMGetMDKindIDInContext(self.cx().llcx,
112 key.as_ptr() as *const c_char, key.len() as c_uint);
114 let val: &'ll Value = self.cx().const_i32(ia.ctxt.outer().as_u32() as i32);
116 llvm::LLVMSetMetadata(r, kind,
117 llvm::LLVMMDNodeInContext(self.cx().llcx, &val, 1));
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();
128 llvm::LLVMRustAppendModuleInlineAsm(self.llmod, asm.as_ptr());