1 use crate::builder::Builder;
2 use crate::context::CodegenCx;
4 use crate::type_of::LayoutLlvmExt;
5 use crate::value::Value;
7 use rustc_codegen_ssa::mir::operand::OperandValue;
8 use rustc_codegen_ssa::mir::place::PlaceRef;
9 use rustc_codegen_ssa::traits::*;
13 use libc::{c_char, c_uint};
15 use std::ffi::{CStr, CString};
17 impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
18 fn codegen_inline_asm(
20 ia: &hir::InlineAsmInner,
21 outputs: Vec<PlaceRef<'tcx, &'ll Value>>,
22 mut inputs: Vec<&'ll Value>,
25 let mut ext_constraints = vec![];
26 let mut output_types = vec![];
28 // Prepare the output operands
29 let mut indirect_outputs = vec![];
30 for (i, (out, &place)) in ia.outputs.iter().zip(&outputs).enumerate() {
32 let operand = self.load_operand(place);
33 if let OperandValue::Immediate(_) = operand.val {
34 inputs.push(operand.immediate());
36 ext_constraints.push(i.to_string());
39 let operand = self.load_operand(place);
40 if let OperandValue::Immediate(_) = operand.val {
41 indirect_outputs.push(operand.immediate());
44 output_types.push(place.layout.llvm_type(self.cx()));
47 if !indirect_outputs.is_empty() {
48 indirect_outputs.extend_from_slice(&inputs);
49 inputs = indirect_outputs;
52 let clobbers = ia.clobbers.iter().map(|s| format!("~{{{}}}", &s));
54 // Default per-arch clobbers
55 // Basically what clang does
56 let arch_clobbers = match &self.sess().target.target.arch[..] {
57 "x86" | "x86_64" => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
58 "mips" | "mips64" => vec!["~{$1}"],
62 let all_constraints = ia
65 .map(|out| out.constraint.to_string())
66 .chain(ia.inputs.iter().map(|s| s.to_string()))
67 .chain(ext_constraints)
69 .chain(arch_clobbers.iter().map(|s| (*s).to_string()))
70 .collect::<Vec<String>>()
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 => self.type_void(),
80 _ => self.type_struct(&output_types, false),
83 let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
84 let constraint_cstr = CString::new(all_constraints).unwrap();
85 let r = inline_asm_call(
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(
113 key.as_ptr() as *const c_char,
117 let val: &'ll Value = self.const_i32(span.ctxt().outer_expn().as_u32() as i32);
119 llvm::LLVMSetMetadata(r, kind, llvm::LLVMMDNodeInContext(self.llcx, &val, 1));
126 impl AsmMethods for CodegenCx<'ll, 'tcx> {
127 fn codegen_global_asm(&self, ga: &hir::GlobalAsm) {
128 let asm = CString::new(ga.asm.as_str().as_bytes()).unwrap();
130 llvm::LLVMRustAppendModuleInlineAsm(self.llmod, asm.as_ptr());
136 bx: &mut Builder<'a, 'll, 'tcx>,
139 inputs: &[&'ll Value],
140 output: &'ll llvm::Type,
143 dia: ::rustc_ast::ast::AsmDialect,
144 ) -> Option<&'ll Value> {
145 let volatile = if volatile { llvm::True } else { llvm::False };
146 let alignstack = if alignstack { llvm::True } else { llvm::False };
151 debug!("Asm Input Type: {:?}", *v);
154 .collect::<Vec<_>>();
156 debug!("Asm Output Type: {:?}", output);
157 let fty = bx.cx.type_func(&argtys[..], output);
159 // Ask LLVM to verify that the constraints are well-formed.
160 let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons.as_ptr());
161 debug!("constraint verification result: {:?}", constraints_ok);
163 let v = llvm::LLVMRustInlineAsm(
169 llvm::AsmDialect::from_generic(dia),
171 Some(bx.call(v, inputs, None))
173 // LLVM has detected an issue with our constraints, bail out