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;
19 use traits::BuilderMethods;
21 use mir::place::PlaceRef;
22 use mir::operand::OperandValue;
24 use std::ffi::CString;
25 use syntax::ast::AsmDialect;
26 use libc::{c_uint, c_char};
28 // Take an inline assembly expression and splat it out via LLVM
29 pub fn codegen_inline_asm(
30 bx: &Builder<'a, 'll, 'tcx>,
32 outputs: Vec<PlaceRef<'tcx, &'ll Value>>,
33 mut inputs: Vec<&'ll Value>
35 let mut ext_constraints = vec![];
36 let mut output_types = vec![];
38 // Prepare the output operands
39 let mut indirect_outputs = vec![];
40 for (i, (out, place)) in ia.outputs.iter().zip(&outputs).enumerate() {
42 inputs.push(place.load(bx).immediate());
43 ext_constraints.push(i.to_string());
46 indirect_outputs.push(place.load(bx).immediate());
48 output_types.push(place.layout.llvm_type(bx.cx));
51 if !indirect_outputs.is_empty() {
52 indirect_outputs.extend_from_slice(&inputs);
53 inputs = indirect_outputs;
56 let clobbers = ia.clobbers.iter()
57 .map(|s| format!("~{{{}}}", &s));
59 // Default per-arch clobbers
60 // Basically what clang does
61 let arch_clobbers = match &bx.sess().target.target.arch[..] {
62 "x86" | "x86_64" => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
63 "mips" | "mips64" => vec!["~{$1}"],
68 ia.outputs.iter().map(|out| out.constraint.to_string())
69 .chain(ia.inputs.iter().map(|s| s.to_string()))
70 .chain(ext_constraints)
72 .chain(arch_clobbers.iter().map(|s| s.to_string()))
73 .collect::<Vec<String>>().join(",");
75 debug!("Asm Constraints: {}", &all_constraints);
77 // Depending on how many outputs we have, the return type is different
78 let num_outputs = output_types.len();
79 let output_type = match num_outputs {
80 0 => Type::void(bx.cx),
82 _ => Type::struct_(bx.cx, &output_types, false)
85 let dialect = match ia.dialect {
86 AsmDialect::Att => llvm::AsmDialect::Att,
87 AsmDialect::Intel => llvm::AsmDialect::Intel,
90 let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
91 let constraint_cstr = CString::new(all_constraints).unwrap();
92 let r = bx.inline_asm_call(
94 constraint_cstr.as_ptr(),
106 // Again, based on how many outputs we have
107 let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
108 for (i, (_, &place)) in outputs.enumerate() {
109 let v = if num_outputs == 1 { r } else { bx.extract_value(r, i as u64) };
110 OperandValue::Immediate(v).store(bx, place);
113 // Store mark in a metadata node so we can map LLVM errors
114 // back to source locations. See #17552.
117 let kind = llvm::LLVMGetMDKindIDInContext(bx.cx.llcx,
118 key.as_ptr() as *const c_char, key.len() as c_uint);
120 let val: &'ll Value = C_i32(bx.cx, ia.ctxt.outer().as_u32() as i32);
122 llvm::LLVMSetMetadata(r, kind,
123 llvm::LLVMMDNodeInContext(bx.cx.llcx, &val, 1));
129 pub fn codegen_global_asm<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
130 ga: &hir::GlobalAsm) {
131 let asm = CString::new(ga.asm.as_str().as_bytes()).unwrap();
133 llvm::LLVMRustAppendModuleInlineAsm(cx.llmod, asm.as_ptr());