Skip to content

Commit 93fa857

Browse files
committed
Add asm label support to AST and HIR
1 parent 8f359be commit 93fa857

File tree

25 files changed

+142
-20
lines changed

25 files changed

+142
-20
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,9 @@ pub enum InlineAsmOperand {
22942294
Sym {
22952295
sym: InlineAsmSym,
22962296
},
2297+
Label {
2298+
block: P<Block>,
2299+
},
22972300
}
22982301

22992302
impl InlineAsmOperand {
@@ -2303,7 +2306,7 @@ impl InlineAsmOperand {
23032306
| Self::Out { reg, .. }
23042307
| Self::InOut { reg, .. }
23052308
| Self::SplitInOut { reg, .. } => Some(reg),
2306-
Self::Const { .. } | Self::Sym { .. } => None,
2309+
Self::Const { .. } | Self::Sym { .. } | Self::Label { .. } => None,
23072310
}
23082311
}
23092312
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,7 @@ pub fn noop_visit_inline_asm<T: MutVisitor>(asm: &mut InlineAsm, vis: &mut T) {
13301330
}
13311331
InlineAsmOperand::Const { anon_const } => vis.visit_anon_const(anon_const),
13321332
InlineAsmOperand::Sym { sym } => vis.visit_inline_asm_sym(sym),
1333+
InlineAsmOperand::Label { block } => vis.visit_block(block),
13331334
}
13341335
}
13351336
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ pub fn walk_inline_asm<'a, V: Visitor<'a>>(visitor: &mut V, asm: &'a InlineAsm)
885885
try_visit!(visitor.visit_anon_const(anon_const))
886886
}
887887
InlineAsmOperand::Sym { sym } => try_visit!(visitor.visit_inline_asm_sym(sym)),
888+
InlineAsmOperand::Label { block } => try_visit!(visitor.visit_block(block)),
888889
}
889890
}
890891
V::Result::output()

compiler/rustc_ast_lowering/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ ast_lowering_invalid_abi_suggestion = did you mean
8888
ast_lowering_invalid_asm_template_modifier_const =
8989
asm template modifiers are not allowed for `const` arguments
9090
91+
ast_lowering_invalid_asm_template_modifier_label =
92+
asm template modifiers are not allowed for `label` arguments
93+
9194
ast_lowering_invalid_asm_template_modifier_reg_class =
9295
invalid asm template modifier for this register class
9396

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

@@ -236,6 +236,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
236236
}
237237
}
238238
}
239+
InlineAsmOperand::Label { block } => {
240+
if !self.tcx.features().asm_goto {
241+
feature_err(
242+
sess,
243+
sym::asm_goto,
244+
*op_sp,
245+
"label operands for inline assembly are unstable",
246+
)
247+
.emit();
248+
}
249+
hir::InlineAsmOperand::Label { block: self.lower_block(block, false) }
250+
}
239251
};
240252
(op, self.lower_span(*op_sp))
241253
})
@@ -295,6 +307,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
295307
op_span: op_sp,
296308
});
297309
}
310+
hir::InlineAsmOperand::Label { .. } => {
311+
self.dcx().emit_err(InvalidAsmTemplateModifierLabel {
312+
placeholder_span,
313+
op_span: op_sp,
314+
});
315+
}
298316
}
299317
}
300318
}
@@ -334,7 +352,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
334352

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

compiler/rustc_ast_lowering/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ pub struct InvalidAsmTemplateModifierSym {
262262
pub op_span: Span,
263263
}
264264

265+
#[derive(Diagnostic, Clone, Copy)]
266+
#[diag(ast_lowering_invalid_asm_template_modifier_label)]
267+
pub struct InvalidAsmTemplateModifierLabel {
268+
#[primary_span]
269+
#[label(ast_lowering_template_modifier)]
270+
pub placeholder_span: Span,
271+
#[label(ast_lowering_argument)]
272+
pub op_span: Span,
273+
}
274+
265275
#[derive(Diagnostic, Clone, Copy)]
266276
#[diag(ast_lowering_register_class_only_clobber)]
267277
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
@@ -1331,6 +1331,10 @@ impl<'a> State<'a> {
13311331
s.print_path(&sym.path, true, 0);
13321332
}
13331333
}
1334+
InlineAsmOperand::Label { block } => {
1335+
s.head("label");
1336+
s.print_block(block);
1337+
}
13341338
}
13351339
}
13361340
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 !is_global_asm && 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)