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};
16 impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
17 fn codegen_inline_asm(
19 ia: &hir::InlineAsmInner,
20 outputs: Vec<PlaceRef<'tcx, &'ll Value>>,
21 mut inputs: Vec<&'ll Value>,
24 let mut ext_constraints = vec![];
25 let mut output_types = vec![];
27 // Prepare the output operands
28 let mut indirect_outputs = vec![];
29 for (i, (out, &place)) in ia.outputs.iter().zip(&outputs).enumerate() {
31 let operand = self.load_operand(place);
32 if let OperandValue::Immediate(_) = operand.val {
33 inputs.push(operand.immediate());
35 ext_constraints.push(i.to_string());
38 let operand = self.load_operand(place);
39 if let OperandValue::Immediate(_) = operand.val {
40 indirect_outputs.push(operand.immediate());
43 output_types.push(place.layout.llvm_type(self.cx()));
46 if !indirect_outputs.is_empty() {
47 indirect_outputs.extend_from_slice(&inputs);
48 inputs = indirect_outputs;
51 let clobbers = ia.clobbers.iter().map(|s| format!("~{{{}}}", &s));
53 // Default per-arch clobbers
54 // Basically what clang does
55 let arch_clobbers = match &self.sess().target.target.arch[..] {
56 "x86" | "x86_64" => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
57 "mips" | "mips64" => vec!["~{$1}"],
61 let all_constraints = ia
64 .map(|out| out.constraint.to_string())
65 .chain(ia.inputs.iter().map(|s| s.to_string()))
66 .chain(ext_constraints)
68 .chain(arch_clobbers.iter().map(|s| (*s).to_string()))
69 .collect::<Vec<String>>()
72 debug!("Asm Constraints: {}", &all_constraints);
74 // Depending on how many outputs we have, the return type is different
75 let num_outputs = output_types.len();
76 let output_type = match num_outputs {
77 0 => self.type_void(),
79 _ => self.type_struct(&output_types, false),
82 let asm = ia.asm.as_str();
83 let r = inline_asm_call(
98 // Again, based on how many outputs we have
99 let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
100 for (i, (_, &place)) in outputs.enumerate() {
101 let v = if num_outputs == 1 { r } else { self.extract_value(r, i as u64) };
102 OperandValue::Immediate(v).store(self, place);
105 // Store mark in a metadata node so we can map LLVM errors
106 // back to source locations. See #17552.
109 let kind = llvm::LLVMGetMDKindIDInContext(
111 key.as_ptr() as *const c_char,
115 let val: &'ll Value = self.const_i32(span.ctxt().outer_expn().as_u32() as i32);
117 llvm::LLVMSetMetadata(r, kind, llvm::LLVMMDNodeInContext(self.llcx, &val, 1));
124 impl AsmMethods for CodegenCx<'ll, 'tcx> {
125 fn codegen_global_asm(&self, ga: &hir::GlobalAsm) {
126 let asm = ga.asm.as_str();
128 llvm::LLVMRustAppendModuleInlineAsm(self.llmod, asm.as_ptr().cast(), asm.len());
134 bx: &mut Builder<'a, 'll, 'tcx>,
137 inputs: &[&'ll Value],
138 output: &'ll llvm::Type,
141 dia: ::rustc_ast::ast::AsmDialect,
142 ) -> Option<&'ll Value> {
143 let volatile = if volatile { llvm::True } else { llvm::False };
144 let alignstack = if alignstack { llvm::True } else { llvm::False };
149 debug!("Asm Input Type: {:?}", *v);
152 .collect::<Vec<_>>();
154 debug!("Asm Output Type: {:?}", output);
155 let fty = bx.cx.type_func(&argtys[..], output);
157 // Ask LLVM to verify that the constraints are well-formed.
158 let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons.as_ptr().cast(), cons.len());
159 debug!("constraint verification result: {:?}", constraints_ok);
161 let v = llvm::LLVMRustInlineAsm(
165 cons.as_ptr().cast(),
169 llvm::AsmDialect::from_generic(dia),
171 Some(bx.call(v, inputs, None))
173 // LLVM has detected an issue with our constraints, bail out