Skip to content

Commit 30ba978

Browse files
committed
Add asm label support to AST and HIR
1 parent 88d69b7 commit 30ba978

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
@@ -81,6 +81,9 @@ ast_lowering_invalid_abi_suggestion = did you mean
8181
ast_lowering_invalid_asm_template_modifier_const =
8282
asm template modifiers are not allowed for `const` arguments
8383
84+
ast_lowering_invalid_asm_template_modifier_label =
85+
asm template modifiers are not allowed for `label` arguments
86+
8487
ast_lowering_invalid_asm_template_modifier_reg_class =
8588
invalid asm template modifier for this register class
8689

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

@@ -241,6 +241,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
241241
}
242242
}
243243
}
244+
InlineAsmOperand::Label { block } => {
245+
if !self.tcx.features().asm_goto {
246+
feature_err(
247+
&sess.parse_sess,
248+
sym::asm_goto,
249+
*op_sp,
250+
"label operands for inline assembly are unstable",
251+
)
252+
.emit();
253+
}
254+
hir::InlineAsmOperand::Label { block: self.lower_block(block, false) }
255+
}
244256
};
245257
(op, self.lower_span(*op_sp))
246258
})
@@ -300,6 +312,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
300312
op_span: op_sp,
301313
});
302314
}
315+
hir::InlineAsmOperand::Label { .. } => {
316+
self.dcx().emit_err(InvalidAsmTemplateModifierLabel {
317+
placeholder_span,
318+
op_span: op_sp,
319+
});
320+
}
303321
}
304322
}
305323
}
@@ -339,7 +357,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
339357

340358
hir::InlineAsmOperand::Const { .. }
341359
| hir::InlineAsmOperand::SymFn { .. }
342-
| hir::InlineAsmOperand::SymStatic { .. } => {
360+
| hir::InlineAsmOperand::SymStatic { .. }
361+
| hir::InlineAsmOperand::Label { .. } => {
343362
unreachable!("{op:?} is not a register operand");
344363
}
345364
};

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)