Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 99311c1

Browse files
committed
Auto merge of rust-lang#119365 - nbdd0121:asm-goto, r=Amanieu
Add asm goto support to `asm!` Tracking issue: rust-lang#119364 This PR implements asm-goto support, using the syntax described in "future possibilities" section of [RFC2873](https://rust-lang.github.io/rfcs/2873-inline-asm.html#asm-goto). Currently I have only implemented the `label` part, not the `fallthrough` part (i.e. fallthrough is implicit). This doesn't reduce the expressive though, since you can use label-break to get arbitrary control flow or simply set a value and rely on jump threading optimisation to get the desired control flow. I can add that later if deemed necessary. r? `@Amanieu` cc `@ojeda`
2 parents 0250ef2 + 8971894 commit 99311c1

File tree

76 files changed

+855
-198
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+855
-198
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_borrowck/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ impl<'cx, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx, R> for MirBorro
723723
operands,
724724
options: _,
725725
line_spans: _,
726-
destination: _,
726+
targets: _,
727727
unwind: _,
728728
} => {
729729
for op in operands {
@@ -749,7 +749,8 @@ impl<'cx, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx, R> for MirBorro
749749
}
750750
InlineAsmOperand::Const { value: _ }
751751
| InlineAsmOperand::SymFn { value: _ }
752-
| InlineAsmOperand::SymStatic { def_id: _ } => {}
752+
| InlineAsmOperand::SymStatic { def_id: _ }
753+
| InlineAsmOperand::Label { target_index: _ } => {}
753754
}
754755
}
755756
}

compiler/rustc_borrowck/src/polonius/loan_invalidations.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'cx, 'tcx> {
161161
operands,
162162
options: _,
163163
line_spans: _,
164-
destination: _,
164+
targets: _,
165165
unwind: _,
166166
} => {
167167
for op in operands {
@@ -182,7 +182,8 @@ impl<'cx, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'cx, 'tcx> {
182182
}
183183
InlineAsmOperand::Const { value: _ }
184184
| InlineAsmOperand::SymFn { value: _ }
185-
| InlineAsmOperand::SymStatic { def_id: _ } => {}
185+
| InlineAsmOperand::SymStatic { def_id: _ }
186+
| InlineAsmOperand::Label { target_index: _ } => {}
186187
}
187188
}
188189
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,8 +1771,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
17711771
self.assert_iscleanup(body, block_data, real_target, is_cleanup);
17721772
self.assert_iscleanup_unwind(body, block_data, unwind, is_cleanup);
17731773
}
1774-
TerminatorKind::InlineAsm { destination, unwind, .. } => {
1775-
if let Some(target) = destination {
1774+
TerminatorKind::InlineAsm { ref targets, unwind, .. } => {
1775+
for &target in targets {
17761776
self.assert_iscleanup(body, block_data, target, is_cleanup);
17771777
}
17781778
self.assert_iscleanup_unwind(body, block_data, unwind, is_cleanup);

0 commit comments

Comments
 (0)