1 use super::LoweringContext;
4 use rustc_data_structures::fx::FxHashMap;
5 use rustc_data_structures::stable_set::FxHashSet;
6 use rustc_errors::struct_span_err;
8 use rustc_session::parse::feature_err;
9 use rustc_span::{sym, Span};
10 use rustc_target::asm;
11 use std::collections::hash_map::Entry;
14 impl<'a, 'hir> LoweringContext<'a, 'hir> {
15 crate fn lower_inline_asm(&mut self, sp: Span, asm: &InlineAsm) -> &'hir hir::InlineAsm<'hir> {
16 // Rustdoc needs to support asm! from foreign architectures: don't try
17 // lowering the register constraints in this case.
18 let asm_arch = if self.sess.opts.actually_rustdoc { None } else { self.sess.asm_arch };
19 if asm_arch.is_none() && !self.sess.opts.actually_rustdoc {
20 struct_span_err!(self.sess, sp, E0472, "inline assembly is unsupported on this target")
23 if let Some(asm_arch) = asm_arch {
24 // Inline assembly is currently only stable for these architectures.
25 let is_stable = matches!(
27 asm::InlineAsmArch::X86
28 | asm::InlineAsmArch::X86_64
29 | asm::InlineAsmArch::Arm
30 | asm::InlineAsmArch::AArch64
31 | asm::InlineAsmArch::RiscV32
32 | asm::InlineAsmArch::RiscV64
34 if !is_stable && !self.sess.features_untracked().asm_experimental_arch {
36 &self.sess.parse_sess,
37 sym::asm_experimental_arch,
39 "inline assembly is not stable yet on this architecture",
44 if asm.options.contains(InlineAsmOptions::ATT_SYNTAX)
45 && !matches!(asm_arch, Some(asm::InlineAsmArch::X86 | asm::InlineAsmArch::X86_64))
46 && !self.sess.opts.actually_rustdoc
49 .struct_span_err(sp, "the `att_syntax` option is only supported on x86")
52 if asm.options.contains(InlineAsmOptions::MAY_UNWIND)
53 && !self.sess.features_untracked().asm_unwind
56 &self.sess.parse_sess,
59 "the `may_unwind` option is unstable",
64 let mut clobber_abis = FxHashMap::default();
65 if let Some(asm_arch) = asm_arch {
66 for (abi_name, abi_span) in &asm.clobber_abis {
67 match asm::InlineAsmClobberAbi::parse(asm_arch, &self.sess.target, *abi_name) {
69 // If the abi was already in the list, emit an error
70 match clobber_abis.get(&abi) {
71 Some((prev_name, prev_sp)) => {
72 let mut err = self.sess.struct_span_err(
74 &format!("`{}` ABI specified multiple times", prev_name),
76 err.span_label(*prev_sp, "previously specified here");
78 // Multiple different abi names may actually be the same ABI
79 // If the specified ABIs are not the same name, alert the user that they resolve to the same ABI
80 let source_map = self.sess.source_map();
81 if source_map.span_to_snippet(*prev_sp)
82 != source_map.span_to_snippet(*abi_span)
84 err.note("these ABIs are equivalent on the current target");
90 clobber_abis.insert(abi, (abi_name, *abi_span));
98 "`clobber_abi` is not supported on this target",
102 Err(supported_abis) => {
104 self.sess.struct_span_err(*abi_span, "invalid ABI for `clobber_abi`");
105 let mut abis = format!("`{}`", supported_abis[0]);
106 for m in &supported_abis[1..] {
107 let _ = write!(abis, ", `{}`", m);
110 "the following ABIs are supported on this target: {}",
119 // Lower operands to HIR. We use dummy register classes if an error
120 // occurs during lowering because we still need to be able to produce a
122 let sess = self.sess;
123 let mut operands: Vec<_> = asm
127 let lower_reg = |reg| match reg {
128 InlineAsmRegOrRegClass::Reg(s) => {
129 asm::InlineAsmRegOrRegClass::Reg(if let Some(asm_arch) = asm_arch {
130 asm::InlineAsmReg::parse(asm_arch, s).unwrap_or_else(|e| {
131 let msg = format!("invalid register `{}`: {}", s.as_str(), e);
132 sess.struct_span_err(*op_sp, &msg).emit();
133 asm::InlineAsmReg::Err
136 asm::InlineAsmReg::Err
139 InlineAsmRegOrRegClass::RegClass(s) => {
140 asm::InlineAsmRegOrRegClass::RegClass(if let Some(asm_arch) = asm_arch {
141 asm::InlineAsmRegClass::parse(asm_arch, s).unwrap_or_else(|e| {
142 let msg = format!("invalid register class `{}`: {}", s.as_str(), e);
143 sess.struct_span_err(*op_sp, &msg).emit();
144 asm::InlineAsmRegClass::Err
147 asm::InlineAsmRegClass::Err
153 InlineAsmOperand::In { reg, ref expr } => hir::InlineAsmOperand::In {
155 expr: self.lower_expr_mut(expr),
157 InlineAsmOperand::Out { reg, late, ref expr } => hir::InlineAsmOperand::Out {
160 expr: expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
162 InlineAsmOperand::InOut { reg, late, ref expr } => {
163 hir::InlineAsmOperand::InOut {
166 expr: self.lower_expr_mut(expr),
169 InlineAsmOperand::SplitInOut { reg, late, ref in_expr, ref out_expr } => {
170 hir::InlineAsmOperand::SplitInOut {
173 in_expr: self.lower_expr_mut(in_expr),
174 out_expr: out_expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
177 InlineAsmOperand::Const { ref anon_const } => {
178 if !self.sess.features_untracked().asm_const {
180 &self.sess.parse_sess,
183 "const operands for inline assembly are unstable",
187 hir::InlineAsmOperand::Const {
188 anon_const: self.lower_anon_const(anon_const),
191 InlineAsmOperand::Sym { ref expr } => {
192 if !self.sess.features_untracked().asm_sym {
194 &self.sess.parse_sess,
197 "sym operands for inline assembly are unstable",
201 hir::InlineAsmOperand::Sym { expr: self.lower_expr_mut(expr) }
204 (op, self.lower_span(*op_sp))
208 // Validate template modifiers against the register classes for the operands
209 for p in &asm.template {
210 if let InlineAsmTemplatePiece::Placeholder {
212 modifier: Some(modifier),
213 span: placeholder_span,
216 let op_sp = asm.operands[operand_idx].1;
217 match &operands[operand_idx].0 {
218 hir::InlineAsmOperand::In { reg, .. }
219 | hir::InlineAsmOperand::Out { reg, .. }
220 | hir::InlineAsmOperand::InOut { reg, .. }
221 | hir::InlineAsmOperand::SplitInOut { reg, .. } => {
222 let class = reg.reg_class();
223 if class == asm::InlineAsmRegClass::Err {
226 let valid_modifiers = class.valid_modifiers(asm_arch.unwrap());
227 if !valid_modifiers.contains(&modifier) {
228 let mut err = sess.struct_span_err(
230 "invalid asm template modifier for this register class",
232 err.span_label(placeholder_span, "template modifier");
233 err.span_label(op_sp, "argument");
234 if !valid_modifiers.is_empty() {
235 let mut mods = format!("`{}`", valid_modifiers[0]);
236 for m in &valid_modifiers[1..] {
237 let _ = write!(mods, ", `{}`", m);
240 "the `{}` register class supports \
241 the following template modifiers: {}",
247 "the `{}` register class does not support template modifiers",
254 hir::InlineAsmOperand::Const { .. } => {
255 let mut err = sess.struct_span_err(
257 "asm template modifiers are not allowed for `const` arguments",
259 err.span_label(placeholder_span, "template modifier");
260 err.span_label(op_sp, "argument");
263 hir::InlineAsmOperand::Sym { .. } => {
264 let mut err = sess.struct_span_err(
266 "asm template modifiers are not allowed for `sym` arguments",
268 err.span_label(placeholder_span, "template modifier");
269 err.span_label(op_sp, "argument");
276 let mut used_input_regs = FxHashMap::default();
277 let mut used_output_regs = FxHashMap::default();
279 for (idx, &(ref op, op_sp)) in operands.iter().enumerate() {
280 if let Some(reg) = op.reg() {
281 let reg_class = reg.reg_class();
282 if reg_class == asm::InlineAsmRegClass::Err {
286 // Some register classes can only be used as clobbers. This
287 // means that we disallow passing a value in/out of the asm and
288 // require that the operand name an explicit register, not a
290 if reg_class.is_clobber_only(asm_arch.unwrap()) && !op.is_clobber() {
292 "register class `{}` can only be used as a clobber, \
293 not as an input or output",
296 sess.struct_span_err(op_sp, &msg).emit();
300 // Check for conflicts between explicit register operands.
301 if let asm::InlineAsmRegOrRegClass::Reg(reg) = reg {
302 let (input, output) = match op {
303 hir::InlineAsmOperand::In { .. } => (true, false),
305 // Late output do not conflict with inputs, but normal outputs do
306 hir::InlineAsmOperand::Out { late, .. } => (!late, true),
308 hir::InlineAsmOperand::InOut { .. }
309 | hir::InlineAsmOperand::SplitInOut { .. } => (true, true),
311 hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::Sym { .. } => {
316 // Flag to output the error only once per operand
317 let mut skip = false;
318 reg.overlapping_regs(|r| {
319 let mut check = |used_regs: &mut FxHashMap<asm::InlineAsmReg, usize>,
321 match used_regs.entry(r) {
322 Entry::Occupied(o) => {
329 let &(ref op2, op_sp2) = &operands[idx2];
330 let Some(asm::InlineAsmRegOrRegClass::Reg(reg2)) = op2.reg() else {
335 "register `{}` conflicts with register `{}`",
339 let mut err = sess.struct_span_err(op_sp, &msg);
340 err.span_label(op_sp, &format!("register `{}`", reg.name()));
341 err.span_label(op_sp2, &format!("register `{}`", reg2.name()));
345 hir::InlineAsmOperand::In { .. },
346 hir::InlineAsmOperand::Out { late, .. },
349 hir::InlineAsmOperand::Out { late, .. },
350 hir::InlineAsmOperand::In { .. },
353 let out_op_sp = if input { op_sp2 } else { op_sp };
354 let msg = "use `lateout` instead of \
355 `out` to avoid conflict";
356 err.span_help(out_op_sp, msg);
363 Entry::Vacant(v) => {
371 check(&mut used_input_regs, true);
374 check(&mut used_output_regs, false);
381 // If a clobber_abi is specified, add the necessary clobbers to the
383 let mut clobbered = FxHashSet::default();
384 for (abi, (_, abi_span)) in clobber_abis {
385 for &clobber in abi.clobbered_regs() {
386 // Don't emit a clobber for a register already clobbered
387 if clobbered.contains(&clobber) {
391 let mut output_used = false;
392 clobber.overlapping_regs(|reg| {
393 if used_output_regs.contains_key(®) {
400 hir::InlineAsmOperand::Out {
401 reg: asm::InlineAsmRegOrRegClass::Reg(clobber),
405 self.lower_span(abi_span),
407 clobbered.insert(clobber);
412 let operands = self.arena.alloc_from_iter(operands);
413 let template = self.arena.alloc_from_iter(asm.template.iter().cloned());
414 let template_strs = self.arena.alloc_from_iter(
417 .map(|(sym, snippet, span)| (*sym, *snippet, self.lower_span(*span))),
420 self.arena.alloc_from_iter(asm.line_spans.iter().map(|span| self.lower_span(*span)));
422 hir::InlineAsm { template, template_strs, operands, options: asm.options, line_spans };
423 self.arena.alloc(hir_asm)