1 use crate::{ImplTraitContext, ImplTraitPosition, ParamMode, ResolverAstLoweringExt};
3 use super::LoweringContext;
7 use rustc_data_structures::fx::FxHashMap;
8 use rustc_data_structures::stable_set::FxHashSet;
9 use rustc_errors::struct_span_err;
11 use rustc_hir::def::{DefKind, Res};
12 use rustc_hir::definitions::DefPathData;
13 use rustc_session::parse::feature_err;
14 use rustc_span::{sym, Span};
15 use rustc_target::asm;
16 use std::collections::hash_map::Entry;
19 impl<'a, 'hir> LoweringContext<'a, 'hir> {
20 pub(crate) fn lower_inline_asm(
24 ) -> &'hir hir::InlineAsm<'hir> {
25 // Rustdoc needs to support asm! from foreign architectures: don't try
26 // lowering the register constraints in this case.
28 if self.tcx.sess.opts.actually_rustdoc { None } else { self.tcx.sess.asm_arch };
29 if asm_arch.is_none() && !self.tcx.sess.opts.actually_rustdoc {
34 "inline assembly is unsupported on this target"
38 if let Some(asm_arch) = asm_arch {
39 // Inline assembly is currently only stable for these architectures.
40 let is_stable = matches!(
42 asm::InlineAsmArch::X86
43 | asm::InlineAsmArch::X86_64
44 | asm::InlineAsmArch::Arm
45 | asm::InlineAsmArch::AArch64
46 | asm::InlineAsmArch::RiscV32
47 | asm::InlineAsmArch::RiscV64
49 if !is_stable && !self.tcx.features().asm_experimental_arch {
51 &self.tcx.sess.parse_sess,
52 sym::asm_experimental_arch,
54 "inline assembly is not stable yet on this architecture",
59 if asm.options.contains(InlineAsmOptions::ATT_SYNTAX)
60 && !matches!(asm_arch, Some(asm::InlineAsmArch::X86 | asm::InlineAsmArch::X86_64))
61 && !self.tcx.sess.opts.actually_rustdoc
65 .struct_span_err(sp, "the `att_syntax` option is only supported on x86")
68 if asm.options.contains(InlineAsmOptions::MAY_UNWIND) && !self.tcx.features().asm_unwind {
70 &self.tcx.sess.parse_sess,
73 "the `may_unwind` option is unstable",
78 let mut clobber_abis = FxHashMap::default();
79 if let Some(asm_arch) = asm_arch {
80 for (abi_name, abi_span) in &asm.clobber_abis {
81 match asm::InlineAsmClobberAbi::parse(asm_arch, &self.tcx.sess.target, *abi_name) {
83 // If the abi was already in the list, emit an error
84 match clobber_abis.get(&abi) {
85 Some((prev_name, prev_sp)) => {
86 let mut err = self.tcx.sess.struct_span_err(
88 &format!("`{}` ABI specified multiple times", prev_name),
90 err.span_label(*prev_sp, "previously specified here");
92 // Multiple different abi names may actually be the same ABI
93 // If the specified ABIs are not the same name, alert the user that they resolve to the same ABI
94 let source_map = self.tcx.sess.source_map();
95 if source_map.span_to_snippet(*prev_sp)
96 != source_map.span_to_snippet(*abi_span)
98 err.note("these ABIs are equivalent on the current target");
104 clobber_abis.insert(abi, (abi_name, *abi_span));
113 "`clobber_abi` is not supported on this target",
117 Err(supported_abis) => {
121 .struct_span_err(*abi_span, "invalid ABI for `clobber_abi`");
122 let mut abis = format!("`{}`", supported_abis[0]);
123 for m in &supported_abis[1..] {
124 let _ = write!(abis, ", `{}`", m);
127 "the following ABIs are supported on this target: {}",
136 // Lower operands to HIR. We use dummy register classes if an error
137 // occurs during lowering because we still need to be able to produce a
139 let sess = self.tcx.sess;
140 let mut operands: Vec<_> = asm
144 let lower_reg = |reg| match reg {
145 InlineAsmRegOrRegClass::Reg(s) => {
146 asm::InlineAsmRegOrRegClass::Reg(if let Some(asm_arch) = asm_arch {
147 asm::InlineAsmReg::parse(asm_arch, s).unwrap_or_else(|e| {
148 let msg = format!("invalid register `{}`: {}", s.as_str(), e);
149 sess.struct_span_err(*op_sp, &msg).emit();
150 asm::InlineAsmReg::Err
153 asm::InlineAsmReg::Err
156 InlineAsmRegOrRegClass::RegClass(s) => {
157 asm::InlineAsmRegOrRegClass::RegClass(if let Some(asm_arch) = asm_arch {
158 asm::InlineAsmRegClass::parse(asm_arch, s).unwrap_or_else(|e| {
159 let msg = format!("invalid register class `{}`: {}", s.as_str(), e);
160 sess.struct_span_err(*op_sp, &msg).emit();
161 asm::InlineAsmRegClass::Err
164 asm::InlineAsmRegClass::Err
170 InlineAsmOperand::In { reg, ref expr } => hir::InlineAsmOperand::In {
172 expr: self.lower_expr_mut(expr),
174 InlineAsmOperand::Out { reg, late, ref expr } => hir::InlineAsmOperand::Out {
177 expr: expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
179 InlineAsmOperand::InOut { reg, late, ref expr } => {
180 hir::InlineAsmOperand::InOut {
183 expr: self.lower_expr_mut(expr),
186 InlineAsmOperand::SplitInOut { reg, late, ref in_expr, ref out_expr } => {
187 hir::InlineAsmOperand::SplitInOut {
190 in_expr: self.lower_expr_mut(in_expr),
191 out_expr: out_expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
194 InlineAsmOperand::Const { ref anon_const } => {
195 if !self.tcx.features().asm_const {
200 "const operands for inline assembly are unstable",
204 hir::InlineAsmOperand::Const {
205 anon_const: self.lower_anon_const(anon_const),
208 InlineAsmOperand::Sym { ref sym } => {
209 if !self.tcx.features().asm_sym {
214 "sym operands for inline assembly are unstable",
219 let static_def_id = self
221 .get_partial_res(sym.id)
222 .filter(|res| res.unresolved_segments() == 0)
224 if let Res::Def(DefKind::Static(_), def_id) = res.base_res() {
231 if let Some(def_id) = static_def_id {
232 let path = self.lower_qpath(
237 ImplTraitContext::Disallowed(ImplTraitPosition::Path),
239 hir::InlineAsmOperand::SymStatic { path, def_id }
241 // Replace the InlineAsmSym AST node with an
242 // Expr using the name node id.
245 kind: ExprKind::Path(sym.qself.clone(), sym.path.clone()),
247 attrs: AttrVec::new(),
251 // Wrap the expression in an AnonConst.
252 let parent_def_id = self.current_hir_id_owner;
253 let node_id = self.next_node_id();
254 self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
255 let anon_const = AnonConst { id: node_id, value: P(expr) };
256 hir::InlineAsmOperand::SymFn {
257 anon_const: self.lower_anon_const(&anon_const),
262 (op, self.lower_span(*op_sp))
266 // Validate template modifiers against the register classes for the operands
267 for p in &asm.template {
268 if let InlineAsmTemplatePiece::Placeholder {
270 modifier: Some(modifier),
271 span: placeholder_span,
274 let op_sp = asm.operands[operand_idx].1;
275 match &operands[operand_idx].0 {
276 hir::InlineAsmOperand::In { reg, .. }
277 | hir::InlineAsmOperand::Out { reg, .. }
278 | hir::InlineAsmOperand::InOut { reg, .. }
279 | hir::InlineAsmOperand::SplitInOut { reg, .. } => {
280 let class = reg.reg_class();
281 if class == asm::InlineAsmRegClass::Err {
284 let valid_modifiers = class.valid_modifiers(asm_arch.unwrap());
285 if !valid_modifiers.contains(&modifier) {
286 let mut err = sess.struct_span_err(
288 "invalid asm template modifier for this register class",
290 err.span_label(placeholder_span, "template modifier");
291 err.span_label(op_sp, "argument");
292 if !valid_modifiers.is_empty() {
293 let mut mods = format!("`{}`", valid_modifiers[0]);
294 for m in &valid_modifiers[1..] {
295 let _ = write!(mods, ", `{}`", m);
298 "the `{}` register class supports \
299 the following template modifiers: {}",
305 "the `{}` register class does not support template modifiers",
312 hir::InlineAsmOperand::Const { .. } => {
313 let mut err = sess.struct_span_err(
315 "asm template modifiers are not allowed for `const` arguments",
317 err.span_label(placeholder_span, "template modifier");
318 err.span_label(op_sp, "argument");
321 hir::InlineAsmOperand::SymFn { .. }
322 | hir::InlineAsmOperand::SymStatic { .. } => {
323 let mut err = sess.struct_span_err(
325 "asm template modifiers are not allowed for `sym` arguments",
327 err.span_label(placeholder_span, "template modifier");
328 err.span_label(op_sp, "argument");
335 let mut used_input_regs = FxHashMap::default();
336 let mut used_output_regs = FxHashMap::default();
338 for (idx, &(ref op, op_sp)) in operands.iter().enumerate() {
339 if let Some(reg) = op.reg() {
340 let reg_class = reg.reg_class();
341 if reg_class == asm::InlineAsmRegClass::Err {
345 // Some register classes can only be used as clobbers. This
346 // means that we disallow passing a value in/out of the asm and
347 // require that the operand name an explicit register, not a
349 if reg_class.is_clobber_only(asm_arch.unwrap()) && !op.is_clobber() {
351 "register class `{}` can only be used as a clobber, \
352 not as an input or output",
355 sess.struct_span_err(op_sp, &msg).emit();
359 // Check for conflicts between explicit register operands.
360 if let asm::InlineAsmRegOrRegClass::Reg(reg) = reg {
361 let (input, output) = match op {
362 hir::InlineAsmOperand::In { .. } => (true, false),
364 // Late output do not conflict with inputs, but normal outputs do
365 hir::InlineAsmOperand::Out { late, .. } => (!late, true),
367 hir::InlineAsmOperand::InOut { .. }
368 | hir::InlineAsmOperand::SplitInOut { .. } => (true, true),
370 hir::InlineAsmOperand::Const { .. }
371 | hir::InlineAsmOperand::SymFn { .. }
372 | hir::InlineAsmOperand::SymStatic { .. } => {
377 // Flag to output the error only once per operand
378 let mut skip = false;
379 reg.overlapping_regs(|r| {
380 let mut check = |used_regs: &mut FxHashMap<asm::InlineAsmReg, usize>,
382 match used_regs.entry(r) {
383 Entry::Occupied(o) => {
390 let &(ref op2, op_sp2) = &operands[idx2];
391 let Some(asm::InlineAsmRegOrRegClass::Reg(reg2)) = op2.reg() else {
396 "register `{}` conflicts with register `{}`",
400 let mut err = sess.struct_span_err(op_sp, &msg);
401 err.span_label(op_sp, &format!("register `{}`", reg.name()));
402 err.span_label(op_sp2, &format!("register `{}`", reg2.name()));
406 hir::InlineAsmOperand::In { .. },
407 hir::InlineAsmOperand::Out { late, .. },
410 hir::InlineAsmOperand::Out { late, .. },
411 hir::InlineAsmOperand::In { .. },
414 let out_op_sp = if input { op_sp2 } else { op_sp };
415 let msg = "use `lateout` instead of \
416 `out` to avoid conflict";
417 err.span_help(out_op_sp, msg);
424 Entry::Vacant(v) => {
432 check(&mut used_input_regs, true);
435 check(&mut used_output_regs, false);
442 // If a clobber_abi is specified, add the necessary clobbers to the
444 let mut clobbered = FxHashSet::default();
445 for (abi, (_, abi_span)) in clobber_abis {
446 for &clobber in abi.clobbered_regs() {
447 // Don't emit a clobber for a register already clobbered
448 if clobbered.contains(&clobber) {
452 let mut output_used = false;
453 clobber.overlapping_regs(|reg| {
454 if used_output_regs.contains_key(®) {
461 hir::InlineAsmOperand::Out {
462 reg: asm::InlineAsmRegOrRegClass::Reg(clobber),
466 self.lower_span(abi_span),
468 clobbered.insert(clobber);
473 let operands = self.arena.alloc_from_iter(operands);
474 let template = self.arena.alloc_from_iter(asm.template.iter().cloned());
475 let template_strs = self.arena.alloc_from_iter(
478 .map(|(sym, snippet, span)| (*sym, *snippet, self.lower_span(*span))),
481 self.arena.alloc_from_iter(asm.line_spans.iter().map(|span| self.lower_span(*span)));
483 hir::InlineAsm { template, template_strs, operands, options: asm.options, line_spans };
484 self.arena.alloc(hir_asm)