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

Commit 5fc60d1

Browse files
committed
various fixes for naked_asm! implementation
- fix for divergence - fix error message - fix another cranelift test - fix some cranelift things - don't set the NORETURN option for naked asm - fix use of naked_asm! in doc comment - fix use of naked_asm! in run-make test - use `span_bug` in unreachable branch
1 parent 10fa482 commit 5fc60d1

File tree

29 files changed

+116
-73
lines changed

29 files changed

+116
-73
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,21 +2435,29 @@ pub enum AsmMacro {
24352435
}
24362436

24372437
impl AsmMacro {
2438-
pub const fn macro_name(&self) -> &'static str {
2438+
pub const fn macro_name(self) -> &'static str {
24392439
match self {
24402440
AsmMacro::Asm => "asm",
24412441
AsmMacro::GlobalAsm => "global_asm",
24422442
AsmMacro::NakedAsm => "naked_asm",
24432443
}
24442444
}
24452445

2446-
pub const fn is_supported_option(&self, option: InlineAsmOptions) -> bool {
2446+
pub const fn is_supported_option(self, option: InlineAsmOptions) -> bool {
24472447
match self {
24482448
AsmMacro::Asm => true,
24492449
AsmMacro::GlobalAsm => InlineAsmOptions::GLOBAL_OPTIONS.contains(option),
24502450
AsmMacro::NakedAsm => InlineAsmOptions::NAKED_OPTIONS.contains(option),
24512451
}
24522452
}
2453+
2454+
pub const fn diverges(self, options: InlineAsmOptions) -> bool {
2455+
match self {
2456+
AsmMacro::Asm => options.contains(InlineAsmOptions::NORETURN),
2457+
AsmMacro::GlobalAsm => true,
2458+
AsmMacro::NakedAsm => true,
2459+
}
2460+
}
24532461
}
24542462

24552463
/// Inline assembly.

compiler/rustc_borrowck/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ impl<'a, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'a, 'tcx, R>
742742
}
743743

744744
TerminatorKind::InlineAsm {
745+
asm_macro: _,
745746
template: _,
746747
operands,
747748
options: _,

compiler/rustc_borrowck/src/polonius/loan_invalidations.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
169169
}
170170
}
171171
TerminatorKind::InlineAsm {
172+
asm_macro: _,
172173
template: _,
173174
operands,
174175
options: _,

compiler/rustc_builtin_macros/src/asm.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -850,22 +850,13 @@ pub(super) fn expand_naked_asm<'cx>(
850850
return ExpandResult::Retry(());
851851
};
852852
let expr = match mac {
853-
Ok(mut inline_asm) => {
854-
// for future compatibility, we always set the NORETURN option.
855-
//
856-
// When we turn `asm!` into `naked_asm!` with this implementation, we can drop
857-
// the `options(noreturn)`, which makes the upgrade smooth when `naked_asm!`
858-
// starts disallowing the `noreturn` option in the future
859-
inline_asm.options |= ast::InlineAsmOptions::NORETURN;
860-
861-
P(ast::Expr {
862-
id: ast::DUMMY_NODE_ID,
863-
kind: ast::ExprKind::InlineAsm(P(inline_asm)),
864-
span: sp,
865-
attrs: ast::AttrVec::new(),
866-
tokens: None,
867-
})
868-
}
853+
Ok(inline_asm) => P(ast::Expr {
854+
id: ast::DUMMY_NODE_ID,
855+
kind: ast::ExprKind::InlineAsm(P(inline_asm)),
856+
span: sp,
857+
attrs: ast::AttrVec::new(),
858+
tokens: None,
859+
}),
869860
Err(guar) => DummyResult::raw_expr(sp, Some(guar)),
870861
};
871862
MacEager::expr(expr)

compiler/rustc_codegen_cranelift/example/mini_core.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,12 @@ pub macro global_asm() {
726726
/* compiler built-in */
727727
}
728728

729+
#[rustc_builtin_macro]
730+
#[rustc_macro_transparency = "semitransparent"]
731+
pub macro naked_asm() {
732+
/* compiler built-in */
733+
}
734+
729735
pub static A_STATIC: u8 = 42;
730736

731737
#[lang = "panic_location"]

compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ global_asm! {
390390
#[naked]
391391
extern "C" fn naked_test() {
392392
unsafe {
393-
asm!("ret", options(noreturn));
393+
naked_asm!("ret");
394394
}
395395
}
396396

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_ast::InlineAsmOptions;
88
use rustc_codegen_ssa::base::is_call_from_compiler_builtins_to_upstream_monomorphization;
99
use rustc_index::IndexVec;
1010
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
11+
use rustc_middle::mir::InlineAsmMacro;
1112
use rustc_middle::ty::TypeVisitableExt;
1213
use rustc_middle::ty::adjustment::PointerCoercion;
1314
use rustc_middle::ty::layout::FnAbiOf;
@@ -57,6 +58,7 @@ pub(crate) fn codegen_fn<'tcx>(
5758

5859
match &mir.basic_blocks[START_BLOCK].terminator().kind {
5960
TerminatorKind::InlineAsm {
61+
asm_macro: InlineAsmMacro::NakedAsm,
6062
template,
6163
operands,
6264
options,
@@ -498,6 +500,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
498500
"tail calls are not yet supported in `rustc_codegen_cranelift` backend"
499501
),
500502
TerminatorKind::InlineAsm {
503+
asm_macro: _,
501504
template,
502505
operands,
503506
options,

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::cmp;
33
use rustc_ast as ast;
44
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
55
use rustc_hir::lang_items::LangItem;
6-
use rustc_middle::mir::{self, AssertKind, BasicBlock, SwitchTargets, UnwindTerminateReason};
6+
use rustc_middle::mir::{
7+
self, AssertKind, BasicBlock, InlineAsmMacro, SwitchTargets, UnwindTerminateReason,
8+
};
79
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, ValidityRequirement};
810
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
911
use rustc_middle::ty::{self, Instance, Ty};
@@ -1133,6 +1135,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11331135
&mut self,
11341136
helper: TerminatorCodegenHelper<'tcx>,
11351137
bx: &mut Bx,
1138+
asm_macro: InlineAsmMacro,
11361139
terminator: &mir::Terminator<'tcx>,
11371140
template: &[ast::InlineAsmTemplatePiece],
11381141
operands: &[mir::InlineAsmOperand<'tcx>],
@@ -1203,11 +1206,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12031206
&operands,
12041207
options,
12051208
line_spans,
1206-
if options.contains(InlineAsmOptions::NORETURN) {
1207-
None
1208-
} else {
1209-
targets.get(0).copied()
1210-
},
1209+
if asm_macro.diverges(options) { None } else { targets.get(0).copied() },
12111210
unwind,
12121211
instance,
12131212
mergeable_succ,
@@ -1381,6 +1380,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13811380
}
13821381

13831382
mir::TerminatorKind::InlineAsm {
1383+
asm_macro,
13841384
template,
13851385
ref operands,
13861386
options,
@@ -1390,6 +1390,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13901390
} => self.codegen_asm_terminator(
13911391
helper,
13921392
bx,
1393+
asm_macro,
13931394
terminator,
13941395
template,
13951396
operands,

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ pub trait Machine<'tcx>: Sized {
395395
///
396396
/// This should take care of jumping to the next block (one of `targets`) when asm goto
397397
/// is triggered, `targets[0]` when the assembly falls through, or diverge in case of
398-
/// `InlineAsmOptions::NORETURN` being set.
398+
/// naked_asm! or `InlineAsmOptions::NORETURN` being set.
399399
fn eval_inline_asm(
400400
_ecx: &mut InterpCx<'tcx, Self>,
401401
_template: &'tcx [InlineAsmTemplatePiece],

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,11 +3507,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
35073507
}
35083508

35093509
fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>) -> Ty<'tcx> {
3510-
let mut diverge = match asm.asm_macro {
3511-
rustc_ast::AsmMacro::Asm => asm.options.contains(ast::InlineAsmOptions::NORETURN),
3512-
rustc_ast::AsmMacro::GlobalAsm => true,
3513-
rustc_ast::AsmMacro::NakedAsm => true,
3514-
};
3510+
let mut diverge = asm.asm_macro.diverges(asm.options);
35153511

35163512
for (op, _op_sp) in asm.operands {
35173513
match op {

0 commit comments

Comments
 (0)