Skip to content

Commit ad5f1fb

Browse files
committed
Add asm label support to AST and HIR
1 parent f8fe517 commit ad5f1fb

File tree

23 files changed

+134
-12
lines changed

23 files changed

+134
-12
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,9 @@ pub enum InlineAsmOperand {
22722272
Sym {
22732273
sym: InlineAsmSym,
22742274
},
2275+
Label {
2276+
block: P<Block>,
2277+
},
22752278
}
22762279

22772280
impl InlineAsmOperand {
@@ -2281,7 +2284,7 @@ impl InlineAsmOperand {
22812284
| Self::Out { reg, .. }
22822285
| Self::InOut { reg, .. }
22832286
| Self::SplitInOut { reg, .. } => Some(reg),
2284-
Self::Const { .. } | Self::Sym { .. } => None,
2287+
Self::Const { .. } | Self::Sym { .. } | Self::Label { .. } => None,
22852288
}
22862289
}
22872290
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,7 @@ pub fn noop_visit_inline_asm<T: MutVisitor>(asm: &mut InlineAsm, vis: &mut T) {
13081308
}
13091309
InlineAsmOperand::Const { anon_const } => vis.visit_anon_const(anon_const),
13101310
InlineAsmOperand::Sym { sym } => vis.visit_inline_asm_sym(sym),
1311+
InlineAsmOperand::Label { block } => vis.visit_block(block),
13111312
}
13121313
}
13131314
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ pub fn walk_inline_asm<'a, V: Visitor<'a>>(visitor: &mut V, asm: &'a InlineAsm)
763763
}
764764
InlineAsmOperand::Const { anon_const, .. } => visitor.visit_anon_const(anon_const),
765765
InlineAsmOperand::Sym { sym } => visitor.visit_inline_asm_sym(sym),
766+
InlineAsmOperand::Label { block } => visitor.visit_block(block),
766767
}
767768
}
768769
}

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ ast_lowering_invalid_abi_suggestion = did you mean
7979
ast_lowering_invalid_asm_template_modifier_const =
8080
asm template modifiers are not allowed for `const` arguments
8181
82+
ast_lowering_invalid_asm_template_modifier_label =
83+
asm template modifiers are not allowed for `label` arguments
84+
8285
ast_lowering_invalid_asm_template_modifier_reg_class =
8386
invalid asm template modifier for this register class
8487

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::{ImplTraitContext, ImplTraitPosition, ParamMode, ResolverAstLoweringE
33
use super::errors::{
44
AbiSpecifiedMultipleTimes, AttSyntaxOnlyX86, ClobberAbiNotSupported,
55
InlineAsmUnsupportedTarget, InvalidAbiClobberAbi, InvalidAsmTemplateModifierConst,
6-
InvalidAsmTemplateModifierRegClass, InvalidAsmTemplateModifierRegClassSub,
7-
InvalidAsmTemplateModifierSym, InvalidRegister, InvalidRegisterClass, RegisterClassOnlyClobber,
8-
RegisterConflict,
6+
InvalidAsmTemplateModifierLabel, InvalidAsmTemplateModifierRegClass,
7+
InvalidAsmTemplateModifierRegClassSub, InvalidAsmTemplateModifierSym, InvalidRegister,
8+
InvalidRegisterClass, RegisterClassOnlyClobber, RegisterConflict,
99
};
1010
use super::LoweringContext;
1111

@@ -237,6 +237,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
237237
}
238238
}
239239
}
240+
InlineAsmOperand::Label { block } => {
241+
if !self.tcx.features().asm_goto {
242+
feature_err(
243+
&sess.parse_sess,
244+
sym::asm_goto,
245+
*op_sp,
246+
"label operands for inline assembly are unstable",
247+
)
248+
.emit();
249+
}
250+
hir::InlineAsmOperand::Label { block: self.lower_block(block, false) }
251+
}
240252
};
241253
(op, self.lower_span(*op_sp))
242254
})
@@ -296,6 +308,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
296308
op_span: op_sp,
297309
});
298310
}
311+
hir::InlineAsmOperand::Label { .. } => {
312+
sess.emit_err(InvalidAsmTemplateModifierLabel {
313+
placeholder_span,
314+
op_span: op_sp,
315+
});
316+
}
299317
}
300318
}
301319
}
@@ -335,7 +353,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
335353

336354
hir::InlineAsmOperand::Const { .. }
337355
| hir::InlineAsmOperand::SymFn { .. }
338-
| hir::InlineAsmOperand::SymStatic { .. } => {
356+
| hir::InlineAsmOperand::SymStatic { .. }
357+
| hir::InlineAsmOperand::Label { .. } => {
339358
unreachable!("{op:?} is not a register operand");
340359
}
341360
};

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ pub struct InvalidAsmTemplateModifierSym {
267267
pub op_span: Span,
268268
}
269269

270+
#[derive(Diagnostic, Clone, Copy)]
271+
#[diag(ast_lowering_invalid_asm_template_modifier_label)]
272+
pub struct InvalidAsmTemplateModifierLabel {
273+
#[primary_span]
274+
#[label(ast_lowering_template_modifier)]
275+
pub placeholder_span: Span,
276+
#[label(ast_lowering_argument)]
277+
pub op_span: Span,
278+
}
279+
270280
#[derive(Diagnostic, Clone, Copy)]
271281
#[diag(ast_lowering_register_class_only_clobber)]
272282
pub struct RegisterClassOnlyClobber {

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,10 @@ impl<'a> State<'a> {
12981298
s.print_path(&sym.path, true, 0);
12991299
}
13001300
}
1301+
InlineAsmOperand::Label { block } => {
1302+
s.head("label");
1303+
s.print_block(block);
1304+
}
13011305
}
13021306
}
13031307
AsmArg::ClobberAbi(abi) => {

compiler/rustc_builtin_macros/src/asm.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ pub fn parse_asm_args<'a>(
166166
path: path.clone(),
167167
};
168168
ast::InlineAsmOperand::Sym { sym }
169+
} else if p.eat_keyword(sym::label) {
170+
let block = p.parse_block()?;
171+
ast::InlineAsmOperand::Label { block }
169172
} else if allow_templates {
170173
let template = p.parse_expr()?;
171174
// If it can't possibly expand to a string, provide diagnostics here to include other

compiler/rustc_codegen_ssa/src/mono_item.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
7676
hir::InlineAsmOperand::In { .. }
7777
| hir::InlineAsmOperand::Out { .. }
7878
| hir::InlineAsmOperand::InOut { .. }
79-
| hir::InlineAsmOperand::SplitInOut { .. } => {
79+
| hir::InlineAsmOperand::SplitInOut { .. }
80+
| hir::InlineAsmOperand::Label { .. } => {
8081
span_bug!(*op_sp, "invalid operand type for global_asm!")
8182
}
8283
})

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ declare_features! (
345345
(unstable, asm_const, "1.58.0", Some(93332)),
346346
/// Enables experimental inline assembly support for additional architectures.
347347
(unstable, asm_experimental_arch, "1.58.0", Some(93335)),
348+
/// Allows using `label` operands in inline assembly.
349+
(unstable, asm_goto, "CURRENT_RUSTC_VERSION", Some(119364)),
348350
/// Allows the `may_unwind` option in inline assembly.
349351
(unstable, asm_unwind, "1.58.0", Some(93334)),
350352
/// Allows users to enforce equality of associated constants `TraitImpl<AssocConst=3>`.

0 commit comments

Comments
 (0)