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.
11 use llvm::{self, ValueRef};
14 use type_of::LayoutLlvmExt;
19 use mir::place::PlaceRef;
20 use mir::operand::OperandValue;
22 use std::ffi::CString;
23 use syntax::ast::AsmDialect;
24 use libc::{c_uint, c_char};
26 // Take an inline assembly expression and splat it out via LLVM
27 pub fn codegen_inline_asm<'a, 'tcx>(
28 bx: &Builder<'a, 'tcx>,
30 outputs: Vec<PlaceRef<'tcx>>,
31 mut inputs: Vec<ValueRef>
33 let mut ext_constraints = vec![];
34 let mut output_types = vec![];
36 // Prepare the output operands
37 let mut indirect_outputs = vec![];
38 for (i, (out, place)) in ia.outputs.iter().zip(&outputs).enumerate() {
40 inputs.push(place.load(bx).immediate());
41 ext_constraints.push(i.to_string());
44 indirect_outputs.push(place.load(bx).immediate());
46 output_types.push(place.layout.llvm_type(bx.cx));
49 if !indirect_outputs.is_empty() {
50 indirect_outputs.extend_from_slice(&inputs);
51 inputs = indirect_outputs;
54 let clobbers = ia.clobbers.iter()
55 .map(|s| format!("~{{{}}}", &s));
57 // Default per-arch clobbers
58 // Basically what clang does
59 let arch_clobbers = match &bx.sess().target.target.arch[..] {
60 "x86" | "x86_64" => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
61 "mips" | "mips64" => vec!["~{$1}"],
66 ia.outputs.iter().map(|out| out.constraint.to_string())
67 .chain(ia.inputs.iter().map(|s| s.to_string()))
68 .chain(ext_constraints)
70 .chain(arch_clobbers.iter().map(|s| s.to_string()))
71 .collect::<Vec<String>>().join(",");
73 debug!("Asm Constraints: {}", &all_constraints);
75 // Depending on how many outputs we have, the return type is different
76 let num_outputs = output_types.len();
77 let output_type = match num_outputs {
78 0 => Type::void(bx.cx),
80 _ => Type::struct_(bx.cx, &output_types, false)
83 let dialect = match ia.dialect {
84 AsmDialect::Att => llvm::AsmDialect::Att,
85 AsmDialect::Intel => llvm::AsmDialect::Intel,
88 let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
89 let constraint_cstr = CString::new(all_constraints).unwrap();
90 let r = bx.inline_asm_call(
92 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 { bx.extract_value(r, i as u64) };
104 OperandValue::Immediate(v).store(bx, 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(bx.cx.llcx,
112 key.as_ptr() as *const c_char, key.len() as c_uint);
114 let val: llvm::ValueRef = C_i32(bx.cx, ia.ctxt.outer().as_u32() as i32);
116 llvm::LLVMSetMetadata(r, kind,
117 llvm::LLVMMDNodeInContext(bx.cx.llcx, &val, 1));
121 pub fn codegen_global_asm<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
122 ga: &hir::GlobalAsm) {
123 let asm = CString::new(ga.asm.as_str().as_bytes()).unwrap();
125 llvm::LLVMRustAppendModuleInlineAsm(cx.llmod, asm.as_ptr());