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.
14 use type_of::LayoutLlvmExt;
20 use mir::place::PlaceRef;
21 use mir::operand::OperandValue;
23 use std::ffi::CString;
24 use syntax::ast::AsmDialect;
25 use libc::{c_uint, c_char};
27 // Take an inline assembly expression and splat it out via LLVM
28 pub fn codegen_inline_asm(
29 bx: &Builder<'a, 'll, 'tcx>,
31 outputs: Vec<PlaceRef<'ll, 'tcx>>,
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(place.load(bx).immediate());
42 ext_constraints.push(i.to_string());
45 indirect_outputs.push(place.load(bx).immediate());
47 output_types.push(place.layout.llvm_type(bx.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 &bx.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 => Type::void(bx.cx),
81 _ => Type::struct_(bx.cx, &output_types, false)
84 let dialect = match ia.dialect {
85 AsmDialect::Att => llvm::AsmDialect::Att,
86 AsmDialect::Intel => llvm::AsmDialect::Intel,
89 let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
90 let constraint_cstr = CString::new(all_constraints).unwrap();
91 let r = bx.inline_asm_call(
93 constraint_cstr.as_ptr(),
101 // Again, based on how many outputs we have
102 let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
103 for (i, (_, &place)) in outputs.enumerate() {
104 let v = if num_outputs == 1 { r } else { bx.extract_value(r, i as u64) };
105 OperandValue::Immediate(v).store(bx, place);
108 // Store mark in a metadata node so we can map LLVM errors
109 // back to source locations. See #17552.
112 let kind = llvm::LLVMGetMDKindIDInContext(bx.cx.llcx,
113 key.as_ptr() as *const c_char, key.len() as c_uint);
115 let val: &'ll Value = C_i32(bx.cx, ia.ctxt.outer().as_u32() as i32);
117 llvm::LLVMSetMetadata(r, kind,
118 llvm::LLVMMDNodeInContext(bx.cx.llcx, &val, 1));
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();
126 llvm::LLVMRustAppendModuleInlineAsm(cx.llmod, asm.as_ptr());