|
| 1 | +use crate::common; |
| 2 | +use crate::mir::FunctionCx; |
| 3 | +use crate::traits::{AsmMethods, BuilderMethods, GlobalAsmOperandRef}; |
| 4 | +use rustc_middle::bug; |
| 5 | +use rustc_middle::mir::InlineAsmOperand; |
| 6 | +use rustc_middle::ty; |
| 7 | +use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf}; |
| 8 | +use rustc_middle::ty::{Instance, TyCtxt}; |
| 9 | + |
| 10 | +use rustc_span::sym; |
| 11 | +use rustc_target::asm::InlineAsmArch; |
| 12 | + |
| 13 | +impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { |
| 14 | + pub fn codegen_naked_asm(&self, instance: Instance<'tcx>) { |
| 15 | + let cx = &self.cx; |
| 16 | + |
| 17 | + let rustc_middle::mir::TerminatorKind::InlineAsm { |
| 18 | + asm_macro: _, |
| 19 | + template, |
| 20 | + ref operands, |
| 21 | + options, |
| 22 | + line_spans, |
| 23 | + targets: _, |
| 24 | + unwind: _, |
| 25 | + } = self.mir.basic_blocks.iter().next().unwrap().terminator().kind |
| 26 | + else { |
| 27 | + bug!("#[naked] functions should always terminate with an asm! block") |
| 28 | + }; |
| 29 | + |
| 30 | + let operands: Vec<_> = |
| 31 | + operands.iter().map(|op| self.inline_to_global_operand(op)).collect(); |
| 32 | + |
| 33 | + let (begin, end) = crate::mir::naked_asm::prefix_and_suffix(cx.tcx(), instance); |
| 34 | + |
| 35 | + let mut template_vec = Vec::new(); |
| 36 | + template_vec.push(rustc_ast::ast::InlineAsmTemplatePiece::String(begin)); |
| 37 | + template_vec.extend(template.iter().cloned()); |
| 38 | + template_vec.push(rustc_ast::ast::InlineAsmTemplatePiece::String(end)); |
| 39 | + |
| 40 | + cx.codegen_global_asm(&template_vec, &operands, options, line_spans); |
| 41 | + } |
| 42 | + |
| 43 | + fn inline_to_global_operand(&self, op: &InlineAsmOperand<'tcx>) -> GlobalAsmOperandRef<'tcx> { |
| 44 | + match op { |
| 45 | + InlineAsmOperand::Const { value } => { |
| 46 | + let const_value = self.eval_mir_constant(value); |
| 47 | + let string = common::asm_const_to_str( |
| 48 | + self.cx.tcx(), |
| 49 | + value.span, |
| 50 | + const_value, |
| 51 | + self.cx.layout_of(value.ty()), |
| 52 | + ); |
| 53 | + GlobalAsmOperandRef::Const { string } |
| 54 | + } |
| 55 | + InlineAsmOperand::SymFn { value } => { |
| 56 | + let instance = match value.ty().kind() { |
| 57 | + &ty::FnDef(def_id, args) => Instance::new(def_id, args), |
| 58 | + _ => bug!("asm sym is not a function"), |
| 59 | + }; |
| 60 | + |
| 61 | + GlobalAsmOperandRef::SymFn { instance } |
| 62 | + } |
| 63 | + InlineAsmOperand::SymStatic { def_id } => { |
| 64 | + GlobalAsmOperandRef::SymStatic { def_id: *def_id } |
| 65 | + } |
| 66 | + InlineAsmOperand::In { .. } |
| 67 | + | InlineAsmOperand::Out { .. } |
| 68 | + | InlineAsmOperand::InOut { .. } |
| 69 | + | InlineAsmOperand::Label { .. } => { |
| 70 | + bug!("invalid operand type for naked_asm!") |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +enum AsmBinaryFormat { |
| 77 | + Elf, |
| 78 | + Macho, |
| 79 | + Coff, |
| 80 | +} |
| 81 | + |
| 82 | +impl AsmBinaryFormat { |
| 83 | + fn from_target(target: &rustc_target::spec::Target) -> Self { |
| 84 | + if target.is_like_windows { |
| 85 | + Self::Coff |
| 86 | + } else if target.options.vendor == "apple" { |
| 87 | + Self::Macho |
| 88 | + } else { |
| 89 | + Self::Elf |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +fn prefix_and_suffix<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> (String, String) { |
| 95 | + use std::fmt::Write; |
| 96 | + |
| 97 | + let target = &tcx.sess.target; |
| 98 | + let target_arch = tcx.sess.asm_arch; |
| 99 | + |
| 100 | + let is_arm = target.arch == "arm"; |
| 101 | + let is_thumb = is_arm && target.llvm_target.contains("thumb"); |
| 102 | + |
| 103 | + let mangle = (target.is_like_windows && matches!(target_arch, Some(InlineAsmArch::X86))) |
| 104 | + || target.options.vendor == "apple"; |
| 105 | + |
| 106 | + let asm_name = format!("{}{}", if mangle { "_" } else { "" }, tcx.symbol_name(instance).name); |
| 107 | + |
| 108 | + let opt_section = tcx |
| 109 | + .get_attr(instance.def.def_id(), sym::link_section) |
| 110 | + .and_then(|attr| attr.value_str()) |
| 111 | + .map(|attr| attr.as_str().to_string()); |
| 112 | + |
| 113 | + let instruction_set = |
| 114 | + tcx.get_attr(instance.def.def_id(), sym::instruction_set).and_then(|attr| attr.value_str()); |
| 115 | + |
| 116 | + let (arch_prefix, arch_suffix) = if is_arm { |
| 117 | + ( |
| 118 | + match instruction_set { |
| 119 | + None => match is_thumb { |
| 120 | + true => ".thumb\n.thumb_func", |
| 121 | + false => ".arm", |
| 122 | + }, |
| 123 | + Some(sym::a32) => ".arm", |
| 124 | + Some(sym::t32) => ".thumb\n.thumb_func", |
| 125 | + Some(other) => bug!("invalid instruction set: {other}"), |
| 126 | + }, |
| 127 | + match is_thumb { |
| 128 | + true => ".thumb", |
| 129 | + false => ".arm", |
| 130 | + }, |
| 131 | + ) |
| 132 | + } else { |
| 133 | + ("", "") |
| 134 | + }; |
| 135 | + |
| 136 | + let mut begin = String::new(); |
| 137 | + let mut end = String::new(); |
| 138 | + match AsmBinaryFormat::from_target(&tcx.sess.target) { |
| 139 | + AsmBinaryFormat::Elf => { |
| 140 | + let section = opt_section.unwrap_or(format!(".text.{asm_name}")); |
| 141 | + |
| 142 | + let progbits = match is_arm { |
| 143 | + true => "%progbits", |
| 144 | + false => "@progbits", |
| 145 | + }; |
| 146 | + |
| 147 | + let function = match is_arm { |
| 148 | + true => "%function", |
| 149 | + false => "@function", |
| 150 | + }; |
| 151 | + |
| 152 | + writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap(); |
| 153 | + writeln!(begin, ".balign 4").unwrap(); |
| 154 | + writeln!(begin, ".globl {asm_name}").unwrap(); |
| 155 | + writeln!(begin, ".hidden {asm_name}").unwrap(); |
| 156 | + writeln!(begin, ".type {asm_name}, {function}").unwrap(); |
| 157 | + if let Some(instruction_set) = instruction_set { |
| 158 | + writeln!(begin, "{}", instruction_set.as_str()).unwrap(); |
| 159 | + } |
| 160 | + if !arch_prefix.is_empty() { |
| 161 | + writeln!(begin, "{}", arch_prefix).unwrap(); |
| 162 | + } |
| 163 | + writeln!(begin, "{asm_name}:").unwrap(); |
| 164 | + |
| 165 | + writeln!(end).unwrap(); |
| 166 | + writeln!(end, ".size {asm_name}, . - {asm_name}").unwrap(); |
| 167 | + writeln!(end, ".popsection").unwrap(); |
| 168 | + if !arch_suffix.is_empty() { |
| 169 | + writeln!(end, "{}", arch_suffix).unwrap(); |
| 170 | + } |
| 171 | + } |
| 172 | + AsmBinaryFormat::Macho => { |
| 173 | + let section = opt_section.unwrap_or("__TEXT,__text".to_string()); |
| 174 | + writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap(); |
| 175 | + writeln!(begin, ".balign 4").unwrap(); |
| 176 | + writeln!(begin, ".globl {asm_name}").unwrap(); |
| 177 | + writeln!(begin, ".private_extern {asm_name}").unwrap(); |
| 178 | + if let Some(instruction_set) = instruction_set { |
| 179 | + writeln!(begin, "{}", instruction_set.as_str()).unwrap(); |
| 180 | + } |
| 181 | + writeln!(begin, "{asm_name}:").unwrap(); |
| 182 | + |
| 183 | + writeln!(end).unwrap(); |
| 184 | + writeln!(end, ".popsection").unwrap(); |
| 185 | + if !arch_suffix.is_empty() { |
| 186 | + writeln!(end, "{}", arch_suffix).unwrap(); |
| 187 | + } |
| 188 | + } |
| 189 | + AsmBinaryFormat::Coff => { |
| 190 | + let section = opt_section.unwrap_or(format!(".text.{asm_name}")); |
| 191 | + writeln!(begin, ".pushsection {},\"xr\"", section).unwrap(); |
| 192 | + writeln!(begin, ".balign 4").unwrap(); |
| 193 | + writeln!(begin, ".globl {asm_name}").unwrap(); |
| 194 | + writeln!(begin, ".def {asm_name}").unwrap(); |
| 195 | + writeln!(begin, ".scl 2").unwrap(); |
| 196 | + writeln!(begin, ".type 32").unwrap(); |
| 197 | + writeln!(begin, ".endef {asm_name}").unwrap(); |
| 198 | + if let Some(instruction_set) = instruction_set { |
| 199 | + writeln!(begin, "{}", instruction_set.as_str()).unwrap(); |
| 200 | + } |
| 201 | + writeln!(begin, "{asm_name}:").unwrap(); |
| 202 | + |
| 203 | + writeln!(end).unwrap(); |
| 204 | + writeln!(end, ".popsection").unwrap(); |
| 205 | + if !arch_suffix.is_empty() { |
| 206 | + writeln!(end, "{}", arch_suffix).unwrap(); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + (begin, end) |
| 212 | +} |
0 commit comments