rustc_builtin_macros/
asm.rs

1use ast::token::IdentIsRaw;
2use lint::BuiltinLintDiag;
3use rustc_ast::ptr::P;
4use rustc_ast::tokenstream::TokenStream;
5use rustc_ast::{AsmMacro, token};
6use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
7use rustc_errors::PResult;
8use rustc_expand::base::*;
9use rustc_index::bit_set::GrowableBitSet;
10use rustc_parse::exp;
11use rustc_parse::parser::{ExpKeywordPair, Parser};
12use rustc_session::lint;
13use rustc_span::{ErrorGuaranteed, Ident, InnerSpan, Span, Symbol, kw};
14use rustc_target::asm::InlineAsmArch;
15use smallvec::smallvec;
16use {rustc_ast as ast, rustc_parse_format as parse};
17
18use crate::errors;
19use crate::util::{ExprToSpannedString, expr_to_spanned_string};
20
21pub struct AsmArgs {
22    pub templates: Vec<P<ast::Expr>>,
23    pub operands: Vec<(ast::InlineAsmOperand, Span)>,
24    named_args: FxIndexMap<Symbol, usize>,
25    reg_args: GrowableBitSet<usize>,
26    pub clobber_abis: Vec<(Symbol, Span)>,
27    options: ast::InlineAsmOptions,
28    pub options_spans: Vec<Span>,
29}
30
31/// Used for better error messages when operand types are used that are not
32/// supported by the current macro (e.g. `in` or `out` for `global_asm!`)
33///
34/// returns
35///
36/// - `Ok(true)` if the current token matches the keyword, and was expected
37/// - `Ok(false)` if the current token does not match the keyword
38/// - `Err(_)` if the current token matches the keyword, but was not expected
39fn eat_operand_keyword<'a>(
40    p: &mut Parser<'a>,
41    exp: ExpKeywordPair,
42    asm_macro: AsmMacro,
43) -> PResult<'a, bool> {
44    if matches!(asm_macro, AsmMacro::Asm) {
45        Ok(p.eat_keyword(exp))
46    } else {
47        let span = p.token.span;
48        if p.eat_keyword_noexpect(exp.kw) {
49            // in gets printed as `r#in` otherwise
50            let symbol = if exp.kw == kw::In { "in" } else { exp.kw.as_str() };
51            Err(p.dcx().create_err(errors::AsmUnsupportedOperand {
52                span,
53                symbol,
54                macro_name: asm_macro.macro_name(),
55            }))
56        } else {
57            Ok(false)
58        }
59    }
60}
61
62fn parse_args<'a>(
63    ecx: &ExtCtxt<'a>,
64    sp: Span,
65    tts: TokenStream,
66    asm_macro: AsmMacro,
67) -> PResult<'a, AsmArgs> {
68    let mut p = ecx.new_parser_from_tts(tts);
69    parse_asm_args(&mut p, sp, asm_macro)
70}
71
72// Primarily public for rustfmt consumption.
73// Internal consumers should continue to leverage `expand_asm`/`expand__global_asm`
74pub fn parse_asm_args<'a>(
75    p: &mut Parser<'a>,
76    sp: Span,
77    asm_macro: AsmMacro,
78) -> PResult<'a, AsmArgs> {
79    let dcx = p.dcx();
80
81    if p.token == token::Eof {
82        return Err(dcx.create_err(errors::AsmRequiresTemplate { span: sp }));
83    }
84
85    let first_template = p.parse_expr()?;
86    let mut args = AsmArgs {
87        templates: vec![first_template],
88        operands: vec![],
89        named_args: Default::default(),
90        reg_args: Default::default(),
91        clobber_abis: Vec::new(),
92        options: ast::InlineAsmOptions::empty(),
93        options_spans: vec![],
94    };
95
96    let mut allow_templates = true;
97    while p.token != token::Eof {
98        if !p.eat(exp!(Comma)) {
99            if allow_templates {
100                // After a template string, we always expect *only* a comma...
101                return Err(dcx.create_err(errors::AsmExpectedComma { span: p.token.span }));
102            } else {
103                // ...after that delegate to `expect` to also include the other expected tokens.
104                return Err(p.expect(exp!(Comma)).err().unwrap());
105            }
106        }
107        if p.token == token::Eof {
108            break;
109        } // accept trailing commas
110
111        // Parse clobber_abi
112        if p.eat_keyword(exp!(ClobberAbi)) {
113            parse_clobber_abi(p, &mut args)?;
114            allow_templates = false;
115            continue;
116        }
117
118        // Parse options
119        if p.eat_keyword(exp!(Options)) {
120            parse_options(p, &mut args, asm_macro)?;
121            allow_templates = false;
122            continue;
123        }
124
125        let span_start = p.token.span;
126
127        // Parse operand names
128        let name = if p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq) {
129            let (ident, _) = p.token.ident().unwrap();
130            p.bump();
131            p.expect(exp!(Eq))?;
132            allow_templates = false;
133            Some(ident.name)
134        } else {
135            None
136        };
137
138        let mut explicit_reg = false;
139        let op = if eat_operand_keyword(p, exp!(In), asm_macro)? {
140            let reg = parse_reg(p, &mut explicit_reg)?;
141            if p.eat_keyword(exp!(Underscore)) {
142                let err = dcx.create_err(errors::AsmUnderscoreInput { span: p.token.span });
143                return Err(err);
144            }
145            let expr = p.parse_expr()?;
146            ast::InlineAsmOperand::In { reg, expr }
147        } else if eat_operand_keyword(p, exp!(Out), asm_macro)? {
148            let reg = parse_reg(p, &mut explicit_reg)?;
149            let expr = if p.eat_keyword(exp!(Underscore)) { None } else { Some(p.parse_expr()?) };
150            ast::InlineAsmOperand::Out { reg, expr, late: false }
151        } else if eat_operand_keyword(p, exp!(Lateout), asm_macro)? {
152            let reg = parse_reg(p, &mut explicit_reg)?;
153            let expr = if p.eat_keyword(exp!(Underscore)) { None } else { Some(p.parse_expr()?) };
154            ast::InlineAsmOperand::Out { reg, expr, late: true }
155        } else if eat_operand_keyword(p, exp!(Inout), asm_macro)? {
156            let reg = parse_reg(p, &mut explicit_reg)?;
157            if p.eat_keyword(exp!(Underscore)) {
158                let err = dcx.create_err(errors::AsmUnderscoreInput { span: p.token.span });
159                return Err(err);
160            }
161            let expr = p.parse_expr()?;
162            if p.eat(exp!(FatArrow)) {
163                let out_expr =
164                    if p.eat_keyword(exp!(Underscore)) { None } else { Some(p.parse_expr()?) };
165                ast::InlineAsmOperand::SplitInOut { reg, in_expr: expr, out_expr, late: false }
166            } else {
167                ast::InlineAsmOperand::InOut { reg, expr, late: false }
168            }
169        } else if eat_operand_keyword(p, exp!(Inlateout), asm_macro)? {
170            let reg = parse_reg(p, &mut explicit_reg)?;
171            if p.eat_keyword(exp!(Underscore)) {
172                let err = dcx.create_err(errors::AsmUnderscoreInput { span: p.token.span });
173                return Err(err);
174            }
175            let expr = p.parse_expr()?;
176            if p.eat(exp!(FatArrow)) {
177                let out_expr =
178                    if p.eat_keyword(exp!(Underscore)) { None } else { Some(p.parse_expr()?) };
179                ast::InlineAsmOperand::SplitInOut { reg, in_expr: expr, out_expr, late: true }
180            } else {
181                ast::InlineAsmOperand::InOut { reg, expr, late: true }
182            }
183        } else if eat_operand_keyword(p, exp!(Label), asm_macro)? {
184            let block = p.parse_block()?;
185            ast::InlineAsmOperand::Label { block }
186        } else if p.eat_keyword(exp!(Const)) {
187            let anon_const = p.parse_expr_anon_const()?;
188            ast::InlineAsmOperand::Const { anon_const }
189        } else if p.eat_keyword(exp!(Sym)) {
190            let expr = p.parse_expr()?;
191            let ast::ExprKind::Path(qself, path) = &expr.kind else {
192                let err = dcx.create_err(errors::AsmSymNoPath { span: expr.span });
193                return Err(err);
194            };
195            let sym = ast::InlineAsmSym {
196                id: ast::DUMMY_NODE_ID,
197                qself: qself.clone(),
198                path: path.clone(),
199            };
200            ast::InlineAsmOperand::Sym { sym }
201        } else if allow_templates {
202            let template = p.parse_expr()?;
203            // If it can't possibly expand to a string, provide diagnostics here to include other
204            // things it could have been.
205            match template.kind {
206                ast::ExprKind::Lit(token_lit)
207                    if matches!(
208                        token_lit.kind,
209                        token::LitKind::Str | token::LitKind::StrRaw(_)
210                    ) => {}
211                ast::ExprKind::MacCall(..) => {}
212                _ => {
213                    let err = dcx.create_err(errors::AsmExpectedOther {
214                        span: template.span,
215                        is_inline_asm: matches!(asm_macro, AsmMacro::Asm),
216                    });
217                    return Err(err);
218                }
219            }
220            args.templates.push(template);
221            continue;
222        } else {
223            p.unexpected_any()?
224        };
225
226        allow_templates = false;
227        let span = span_start.to(p.prev_token.span);
228        let slot = args.operands.len();
229        args.operands.push((op, span));
230
231        // Validate the order of named, positional & explicit register operands and
232        // clobber_abi/options. We do this at the end once we have the full span
233        // of the argument available.
234        if explicit_reg {
235            if name.is_some() {
236                dcx.emit_err(errors::AsmExplicitRegisterName { span });
237            }
238            args.reg_args.insert(slot);
239        } else if let Some(name) = name {
240            if let Some(&prev) = args.named_args.get(&name) {
241                dcx.emit_err(errors::AsmDuplicateArg { span, name, prev: args.operands[prev].1 });
242                continue;
243            }
244            args.named_args.insert(name, slot);
245        } else if !args.named_args.is_empty() || !args.reg_args.is_empty() {
246            let named = args.named_args.values().map(|p| args.operands[*p].1).collect();
247            let explicit = args.reg_args.iter().map(|p| args.operands[p].1).collect();
248
249            dcx.emit_err(errors::AsmPositionalAfter { span, named, explicit });
250        }
251    }
252
253    if args.options.contains(ast::InlineAsmOptions::NOMEM)
254        && args.options.contains(ast::InlineAsmOptions::READONLY)
255    {
256        let spans = args.options_spans.clone();
257        dcx.emit_err(errors::AsmMutuallyExclusive { spans, opt1: "nomem", opt2: "readonly" });
258    }
259    if args.options.contains(ast::InlineAsmOptions::PURE)
260        && args.options.contains(ast::InlineAsmOptions::NORETURN)
261    {
262        let spans = args.options_spans.clone();
263        dcx.emit_err(errors::AsmMutuallyExclusive { spans, opt1: "pure", opt2: "noreturn" });
264    }
265    if args.options.contains(ast::InlineAsmOptions::PURE)
266        && !args.options.intersects(ast::InlineAsmOptions::NOMEM | ast::InlineAsmOptions::READONLY)
267    {
268        let spans = args.options_spans.clone();
269        dcx.emit_err(errors::AsmPureCombine { spans });
270    }
271
272    let mut have_real_output = false;
273    let mut outputs_sp = vec![];
274    let mut regclass_outputs = vec![];
275    let mut labels_sp = vec![];
276    for (op, op_sp) in &args.operands {
277        match op {
278            ast::InlineAsmOperand::Out { reg, expr, .. }
279            | ast::InlineAsmOperand::SplitInOut { reg, out_expr: expr, .. } => {
280                outputs_sp.push(*op_sp);
281                have_real_output |= expr.is_some();
282                if let ast::InlineAsmRegOrRegClass::RegClass(_) = reg {
283                    regclass_outputs.push(*op_sp);
284                }
285            }
286            ast::InlineAsmOperand::InOut { reg, .. } => {
287                outputs_sp.push(*op_sp);
288                have_real_output = true;
289                if let ast::InlineAsmRegOrRegClass::RegClass(_) = reg {
290                    regclass_outputs.push(*op_sp);
291                }
292            }
293            ast::InlineAsmOperand::Label { .. } => {
294                labels_sp.push(*op_sp);
295            }
296            _ => {}
297        }
298    }
299    if args.options.contains(ast::InlineAsmOptions::PURE) && !have_real_output {
300        dcx.emit_err(errors::AsmPureNoOutput { spans: args.options_spans.clone() });
301    }
302    if args.options.contains(ast::InlineAsmOptions::NORETURN)
303        && !outputs_sp.is_empty()
304        && labels_sp.is_empty()
305    {
306        let err = dcx.create_err(errors::AsmNoReturn { outputs_sp });
307        // Bail out now since this is likely to confuse MIR
308        return Err(err);
309    }
310    if args.options.contains(ast::InlineAsmOptions::MAY_UNWIND) && !labels_sp.is_empty() {
311        dcx.emit_err(errors::AsmMayUnwind { labels_sp });
312    }
313
314    if !args.clobber_abis.is_empty() {
315        match asm_macro {
316            AsmMacro::GlobalAsm | AsmMacro::NakedAsm => {
317                let err = dcx.create_err(errors::AsmUnsupportedClobberAbi {
318                    spans: args.clobber_abis.iter().map(|(_, span)| *span).collect(),
319                    macro_name: asm_macro.macro_name(),
320                });
321
322                // Bail out now since this is likely to confuse later stages
323                return Err(err);
324            }
325            AsmMacro::Asm => {
326                if !regclass_outputs.is_empty() {
327                    dcx.emit_err(errors::AsmClobberNoReg {
328                        spans: regclass_outputs,
329                        clobbers: args.clobber_abis.iter().map(|(_, span)| *span).collect(),
330                    });
331                }
332            }
333        }
334    }
335
336    Ok(args)
337}
338
339/// Report a duplicate option error.
340///
341/// This function must be called immediately after the option token is parsed.
342/// Otherwise, the suggestion will be incorrect.
343fn err_duplicate_option(p: &Parser<'_>, symbol: Symbol, span: Span) {
344    // Tool-only output
345    let full_span = if p.token == token::Comma { span.to(p.token.span) } else { span };
346    p.dcx().emit_err(errors::AsmOptAlreadyprovided { span, symbol, full_span });
347}
348
349/// Report an invalid option error.
350///
351/// This function must be called immediately after the option token is parsed.
352/// Otherwise, the suggestion will be incorrect.
353fn err_unsupported_option(p: &Parser<'_>, asm_macro: AsmMacro, symbol: Symbol, span: Span) {
354    // Tool-only output
355    let full_span = if p.token == token::Comma { span.to(p.token.span) } else { span };
356    p.dcx().emit_err(errors::AsmUnsupportedOption {
357        span,
358        symbol,
359        full_span,
360        macro_name: asm_macro.macro_name(),
361    });
362}
363
364/// Try to set the provided option in the provided `AsmArgs`.
365/// If it is already set, report a duplicate option error.
366///
367/// This function must be called immediately after the option token is parsed.
368/// Otherwise, the error will not point to the correct spot.
369fn try_set_option<'a>(
370    p: &Parser<'a>,
371    args: &mut AsmArgs,
372    asm_macro: AsmMacro,
373    symbol: Symbol,
374    option: ast::InlineAsmOptions,
375) {
376    if !asm_macro.is_supported_option(option) {
377        err_unsupported_option(p, asm_macro, symbol, p.prev_token.span);
378    } else if args.options.contains(option) {
379        err_duplicate_option(p, symbol, p.prev_token.span);
380    } else {
381        args.options |= option;
382    }
383}
384
385fn parse_options<'a>(
386    p: &mut Parser<'a>,
387    args: &mut AsmArgs,
388    asm_macro: AsmMacro,
389) -> PResult<'a, ()> {
390    let span_start = p.prev_token.span;
391
392    p.expect(exp!(OpenParen))?;
393
394    while !p.eat(exp!(CloseParen)) {
395        const OPTIONS: [(ExpKeywordPair, ast::InlineAsmOptions); ast::InlineAsmOptions::COUNT] = [
396            (exp!(Pure), ast::InlineAsmOptions::PURE),
397            (exp!(Nomem), ast::InlineAsmOptions::NOMEM),
398            (exp!(Readonly), ast::InlineAsmOptions::READONLY),
399            (exp!(PreservesFlags), ast::InlineAsmOptions::PRESERVES_FLAGS),
400            (exp!(Noreturn), ast::InlineAsmOptions::NORETURN),
401            (exp!(Nostack), ast::InlineAsmOptions::NOSTACK),
402            (exp!(MayUnwind), ast::InlineAsmOptions::MAY_UNWIND),
403            (exp!(AttSyntax), ast::InlineAsmOptions::ATT_SYNTAX),
404            (exp!(Raw), ast::InlineAsmOptions::RAW),
405        ];
406
407        'blk: {
408            for (exp, option) in OPTIONS {
409                let kw_matched = if asm_macro.is_supported_option(option) {
410                    p.eat_keyword(exp)
411                } else {
412                    p.eat_keyword_noexpect(exp.kw)
413                };
414
415                if kw_matched {
416                    try_set_option(p, args, asm_macro, exp.kw, option);
417                    break 'blk;
418                }
419            }
420
421            return p.unexpected();
422        }
423
424        // Allow trailing commas
425        if p.eat(exp!(CloseParen)) {
426            break;
427        }
428        p.expect(exp!(Comma))?;
429    }
430
431    let new_span = span_start.to(p.prev_token.span);
432    args.options_spans.push(new_span);
433
434    Ok(())
435}
436
437fn parse_clobber_abi<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> PResult<'a, ()> {
438    let span_start = p.prev_token.span;
439
440    p.expect(exp!(OpenParen))?;
441
442    if p.eat(exp!(CloseParen)) {
443        return Err(p.dcx().create_err(errors::NonABI { span: p.token.span }));
444    }
445
446    let mut new_abis = Vec::new();
447    while !p.eat(exp!(CloseParen)) {
448        match p.parse_str_lit() {
449            Ok(str_lit) => {
450                new_abis.push((str_lit.symbol_unescaped, str_lit.span));
451            }
452            Err(opt_lit) => {
453                let span = opt_lit.map_or(p.token.span, |lit| lit.span);
454                return Err(p.dcx().create_err(errors::AsmExpectedStringLiteral { span }));
455            }
456        };
457
458        // Allow trailing commas
459        if p.eat(exp!(CloseParen)) {
460            break;
461        }
462        p.expect(exp!(Comma))?;
463    }
464
465    let full_span = span_start.to(p.prev_token.span);
466
467    match &new_abis[..] {
468        // should have errored above during parsing
469        [] => unreachable!(),
470        [(abi, _span)] => args.clobber_abis.push((*abi, full_span)),
471        abis => {
472            for (abi, span) in abis {
473                args.clobber_abis.push((*abi, *span));
474            }
475        }
476    }
477
478    Ok(())
479}
480
481fn parse_reg<'a>(
482    p: &mut Parser<'a>,
483    explicit_reg: &mut bool,
484) -> PResult<'a, ast::InlineAsmRegOrRegClass> {
485    p.expect(exp!(OpenParen))?;
486    let result = match p.token.uninterpolate().kind {
487        token::Ident(name, IdentIsRaw::No) => ast::InlineAsmRegOrRegClass::RegClass(name),
488        token::Literal(token::Lit { kind: token::LitKind::Str, symbol, suffix: _ }) => {
489            *explicit_reg = true;
490            ast::InlineAsmRegOrRegClass::Reg(symbol)
491        }
492        _ => {
493            return Err(p.dcx().create_err(errors::ExpectedRegisterClassOrExplicitRegister {
494                span: p.token.span,
495            }));
496        }
497    };
498    p.bump();
499    p.expect(exp!(CloseParen))?;
500    Ok(result)
501}
502
503fn expand_preparsed_asm(
504    ecx: &mut ExtCtxt<'_>,
505    asm_macro: AsmMacro,
506    args: AsmArgs,
507) -> ExpandResult<Result<ast::InlineAsm, ErrorGuaranteed>, ()> {
508    let mut template = vec![];
509    // Register operands are implicitly used since they are not allowed to be
510    // referenced in the template string.
511    let mut used = vec![false; args.operands.len()];
512    for pos in args.reg_args.iter() {
513        used[pos] = true;
514    }
515    let named_pos: FxHashMap<usize, Symbol> =
516        args.named_args.iter().map(|(&sym, &idx)| (idx, sym)).collect();
517    let mut line_spans = Vec::with_capacity(args.templates.len());
518    let mut curarg = 0;
519
520    let mut template_strs = Vec::with_capacity(args.templates.len());
521
522    for (i, template_expr) in args.templates.into_iter().enumerate() {
523        if i != 0 {
524            template.push(ast::InlineAsmTemplatePiece::String("\n".into()));
525        }
526
527        let msg = "asm template must be a string literal";
528        let template_sp = template_expr.span;
529        let template_is_mac_call = matches!(template_expr.kind, ast::ExprKind::MacCall(_));
530        let ExprToSpannedString {
531            symbol: template_str,
532            style: template_style,
533            span: template_span,
534            ..
535        } = {
536            let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, template_expr, msg) else {
537                return ExpandResult::Retry(());
538            };
539            match mac {
540                Ok(template_part) => template_part,
541                Err(err) => {
542                    return ExpandResult::Ready(Err(match err {
543                        Ok((err, _)) => err.emit(),
544                        Err(guar) => guar,
545                    }));
546                }
547            }
548        };
549
550        let str_style = match template_style {
551            ast::StrStyle::Cooked => None,
552            ast::StrStyle::Raw(raw) => Some(raw as usize),
553        };
554
555        let template_snippet = ecx.source_map().span_to_snippet(template_sp).ok();
556        template_strs.push((
557            template_str,
558            template_snippet.as_deref().map(Symbol::intern),
559            template_sp,
560        ));
561        let template_str = template_str.as_str();
562
563        if let Some(InlineAsmArch::X86 | InlineAsmArch::X86_64) = ecx.sess.asm_arch {
564            let find_span = |needle: &str| -> Span {
565                if let Some(snippet) = &template_snippet {
566                    if let Some(pos) = snippet.find(needle) {
567                        let end = pos
568                            + snippet[pos..]
569                                .find(|c| matches!(c, '\n' | ';' | '\\' | '"'))
570                                .unwrap_or(snippet[pos..].len() - 1);
571                        let inner = InnerSpan::new(pos, end);
572                        return template_sp.from_inner(inner);
573                    }
574                }
575                template_sp
576            };
577
578            if template_str.contains(".intel_syntax") {
579                ecx.psess().buffer_lint(
580                    lint::builtin::BAD_ASM_STYLE,
581                    find_span(".intel_syntax"),
582                    ecx.current_expansion.lint_node_id,
583                    BuiltinLintDiag::AvoidUsingIntelSyntax,
584                );
585            }
586            if template_str.contains(".att_syntax") {
587                ecx.psess().buffer_lint(
588                    lint::builtin::BAD_ASM_STYLE,
589                    find_span(".att_syntax"),
590                    ecx.current_expansion.lint_node_id,
591                    BuiltinLintDiag::AvoidUsingAttSyntax,
592                );
593            }
594        }
595
596        // Don't treat raw asm as a format string.
597        if args.options.contains(ast::InlineAsmOptions::RAW) {
598            template.push(ast::InlineAsmTemplatePiece::String(template_str.to_string().into()));
599            let template_num_lines = 1 + template_str.matches('\n').count();
600            line_spans.extend(std::iter::repeat(template_sp).take(template_num_lines));
601            continue;
602        }
603
604        let mut parser = parse::Parser::new(
605            template_str,
606            str_style,
607            template_snippet,
608            false,
609            parse::ParseMode::InlineAsm,
610        );
611        parser.curarg = curarg;
612
613        let mut unverified_pieces = Vec::new();
614        while let Some(piece) = parser.next() {
615            if !parser.errors.is_empty() {
616                break;
617            } else {
618                unverified_pieces.push(piece);
619            }
620        }
621
622        if !parser.errors.is_empty() {
623            let err = parser.errors.remove(0);
624            let err_sp = if template_is_mac_call {
625                // If the template is a macro call we can't reliably point to the error's
626                // span so just use the template's span as the error span (fixes #129503)
627                template_span
628            } else {
629                template_span.from_inner(InnerSpan::new(err.span.start, err.span.end))
630            };
631
632            let msg = format!("invalid asm template string: {}", err.description);
633            let mut e = ecx.dcx().struct_span_err(err_sp, msg);
634            e.span_label(err_sp, err.label + " in asm template string");
635            if let Some(note) = err.note {
636                e.note(note);
637            }
638            if let Some((label, span)) = err.secondary_label {
639                let err_sp = template_span.from_inner(InnerSpan::new(span.start, span.end));
640                e.span_label(err_sp, label);
641            }
642            let guar = e.emit();
643            return ExpandResult::Ready(Err(guar));
644        }
645
646        curarg = parser.curarg;
647
648        let mut arg_spans = parser
649            .arg_places
650            .iter()
651            .map(|span| template_span.from_inner(InnerSpan::new(span.start, span.end)));
652        for piece in unverified_pieces {
653            match piece {
654                parse::Piece::Lit(s) => {
655                    template.push(ast::InlineAsmTemplatePiece::String(s.to_string().into()))
656                }
657                parse::Piece::NextArgument(arg) => {
658                    let span = arg_spans.next().unwrap_or(template_sp);
659
660                    let operand_idx = match arg.position {
661                        parse::ArgumentIs(idx) | parse::ArgumentImplicitlyIs(idx) => {
662                            if idx >= args.operands.len()
663                                || named_pos.contains_key(&idx)
664                                || args.reg_args.contains(idx)
665                            {
666                                let msg = format!("invalid reference to argument at index {idx}");
667                                let mut err = ecx.dcx().struct_span_err(span, msg);
668                                err.span_label(span, "from here");
669
670                                let positional_args = args.operands.len()
671                                    - args.named_args.len()
672                                    - args.reg_args.len();
673                                let positional = if positional_args != args.operands.len() {
674                                    "positional "
675                                } else {
676                                    ""
677                                };
678                                let msg = match positional_args {
679                                    0 => format!("no {positional}arguments were given"),
680                                    1 => format!("there is 1 {positional}argument"),
681                                    x => format!("there are {x} {positional}arguments"),
682                                };
683                                err.note(msg);
684
685                                if named_pos.contains_key(&idx) {
686                                    err.span_label(args.operands[idx].1, "named argument");
687                                    err.span_note(
688                                        args.operands[idx].1,
689                                        "named arguments cannot be referenced by position",
690                                    );
691                                } else if args.reg_args.contains(idx) {
692                                    err.span_label(
693                                        args.operands[idx].1,
694                                        "explicit register argument",
695                                    );
696                                    err.span_note(
697                                        args.operands[idx].1,
698                                        "explicit register arguments cannot be used in the asm template",
699                                    );
700                                    err.span_help(
701                                        args.operands[idx].1,
702                                        "use the register name directly in the assembly code",
703                                    );
704                                }
705                                err.emit();
706                                None
707                            } else {
708                                Some(idx)
709                            }
710                        }
711                        parse::ArgumentNamed(name) => {
712                            match args.named_args.get(&Symbol::intern(name)) {
713                                Some(&idx) => Some(idx),
714                                None => {
715                                    let span = arg.position_span;
716                                    ecx.dcx()
717                                        .create_err(errors::AsmNoMatchedArgumentName {
718                                            name: name.to_owned(),
719                                            span: template_span
720                                                .from_inner(InnerSpan::new(span.start, span.end)),
721                                        })
722                                        .emit();
723                                    None
724                                }
725                            }
726                        }
727                    };
728
729                    let mut chars = arg.format.ty.chars();
730                    let mut modifier = chars.next();
731                    if chars.next().is_some() {
732                        let span = arg
733                            .format
734                            .ty_span
735                            .map(|sp| template_sp.from_inner(InnerSpan::new(sp.start, sp.end)))
736                            .unwrap_or(template_sp);
737                        ecx.dcx().emit_err(errors::AsmModifierInvalid { span });
738                        modifier = None;
739                    }
740
741                    if let Some(operand_idx) = operand_idx {
742                        used[operand_idx] = true;
743                        template.push(ast::InlineAsmTemplatePiece::Placeholder {
744                            operand_idx,
745                            modifier,
746                            span,
747                        });
748                    }
749                }
750            }
751        }
752
753        if parser.line_spans.is_empty() {
754            let template_num_lines = 1 + template_str.matches('\n').count();
755            line_spans.extend(std::iter::repeat(template_sp).take(template_num_lines));
756        } else {
757            line_spans.extend(
758                parser
759                    .line_spans
760                    .iter()
761                    .map(|span| template_span.from_inner(InnerSpan::new(span.start, span.end))),
762            );
763        };
764    }
765
766    let mut unused_operands = vec![];
767    let mut help_str = String::new();
768    for (idx, used) in used.into_iter().enumerate() {
769        if !used {
770            let msg = if let Some(sym) = named_pos.get(&idx) {
771                help_str.push_str(&format!(" {{{}}}", sym));
772                "named argument never used"
773            } else {
774                help_str.push_str(&format!(" {{{}}}", idx));
775                "argument never used"
776            };
777            unused_operands.push((args.operands[idx].1, msg));
778        }
779    }
780    match unused_operands[..] {
781        [] => {}
782        [(sp, msg)] => {
783            ecx.dcx()
784                .struct_span_err(sp, msg)
785                .with_span_label(sp, msg)
786                .with_help(format!(
787                    "if this argument is intentionally unused, \
788                     consider using it in an asm comment: `\"/*{help_str} */\"`"
789                ))
790                .emit();
791        }
792        _ => {
793            let mut err = ecx.dcx().struct_span_err(
794                unused_operands.iter().map(|&(sp, _)| sp).collect::<Vec<Span>>(),
795                "multiple unused asm arguments",
796            );
797            for (sp, msg) in unused_operands {
798                err.span_label(sp, msg);
799            }
800            err.help(format!(
801                "if these arguments are intentionally unused, \
802                 consider using them in an asm comment: `\"/*{help_str} */\"`"
803            ));
804            err.emit();
805        }
806    }
807
808    ExpandResult::Ready(Ok(ast::InlineAsm {
809        asm_macro,
810        template,
811        template_strs: template_strs.into_boxed_slice(),
812        operands: args.operands,
813        clobber_abis: args.clobber_abis,
814        options: args.options,
815        line_spans,
816    }))
817}
818
819pub(super) fn expand_asm<'cx>(
820    ecx: &'cx mut ExtCtxt<'_>,
821    sp: Span,
822    tts: TokenStream,
823) -> MacroExpanderResult<'cx> {
824    ExpandResult::Ready(match parse_args(ecx, sp, tts, AsmMacro::Asm) {
825        Ok(args) => {
826            let ExpandResult::Ready(mac) = expand_preparsed_asm(ecx, AsmMacro::Asm, args) else {
827                return ExpandResult::Retry(());
828            };
829            let expr = match mac {
830                Ok(inline_asm) => P(ast::Expr {
831                    id: ast::DUMMY_NODE_ID,
832                    kind: ast::ExprKind::InlineAsm(P(inline_asm)),
833                    span: sp,
834                    attrs: ast::AttrVec::new(),
835                    tokens: None,
836                }),
837                Err(guar) => DummyResult::raw_expr(sp, Some(guar)),
838            };
839            MacEager::expr(expr)
840        }
841        Err(err) => {
842            let guar = err.emit();
843            DummyResult::any(sp, guar)
844        }
845    })
846}
847
848pub(super) fn expand_naked_asm<'cx>(
849    ecx: &'cx mut ExtCtxt<'_>,
850    sp: Span,
851    tts: TokenStream,
852) -> MacroExpanderResult<'cx> {
853    ExpandResult::Ready(match parse_args(ecx, sp, tts, AsmMacro::NakedAsm) {
854        Ok(args) => {
855            let ExpandResult::Ready(mac) = expand_preparsed_asm(ecx, AsmMacro::NakedAsm, args)
856            else {
857                return ExpandResult::Retry(());
858            };
859            let expr = match mac {
860                Ok(inline_asm) => P(ast::Expr {
861                    id: ast::DUMMY_NODE_ID,
862                    kind: ast::ExprKind::InlineAsm(P(inline_asm)),
863                    span: sp,
864                    attrs: ast::AttrVec::new(),
865                    tokens: None,
866                }),
867                Err(guar) => DummyResult::raw_expr(sp, Some(guar)),
868            };
869            MacEager::expr(expr)
870        }
871        Err(err) => {
872            let guar = err.emit();
873            DummyResult::any(sp, guar)
874        }
875    })
876}
877
878pub(super) fn expand_global_asm<'cx>(
879    ecx: &'cx mut ExtCtxt<'_>,
880    sp: Span,
881    tts: TokenStream,
882) -> MacroExpanderResult<'cx> {
883    ExpandResult::Ready(match parse_args(ecx, sp, tts, AsmMacro::GlobalAsm) {
884        Ok(args) => {
885            let ExpandResult::Ready(mac) = expand_preparsed_asm(ecx, AsmMacro::GlobalAsm, args)
886            else {
887                return ExpandResult::Retry(());
888            };
889            match mac {
890                Ok(inline_asm) => MacEager::items(smallvec![P(ast::Item {
891                    ident: Ident::empty(),
892                    attrs: ast::AttrVec::new(),
893                    id: ast::DUMMY_NODE_ID,
894                    kind: ast::ItemKind::GlobalAsm(Box::new(inline_asm)),
895                    vis: ast::Visibility {
896                        span: sp.shrink_to_lo(),
897                        kind: ast::VisibilityKind::Inherited,
898                        tokens: None,
899                    },
900                    span: sp,
901                    tokens: None,
902                })]),
903                Err(guar) => DummyResult::any(sp, guar),
904            }
905        }
906        Err(err) => {
907            let guar = err.emit();
908            DummyResult::any(sp, guar)
909        }
910    })
911}