Skip to content

Commit

Permalink
Rollup merge of rust-lang#121940 - veera-sivarajan:bugfix-121593, r=f…
Browse files Browse the repository at this point in the history
…mease

Mention Register Size in `#[warn(asm_sub_register)]`

Fixes rust-lang#121593

Displays the register size information obtained from `suggest_modifier()` and `default_modifier()`.
  • Loading branch information
workingjubilee authored Mar 23, 2024
2 parents c3b05c6 + afc99cc commit 6f2f720
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 124 deletions.
22 changes: 15 additions & 7 deletions compiler/rustc_hir_analysis/src/check/intrinsicck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use rustc_session::lint;
use rustc_span::def_id::LocalDefId;
use rustc_span::Symbol;
use rustc_target::abi::FieldIdx;
use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
use rustc_target::asm::{
InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType, ModifierInfo,
};

pub struct InlineAsmCtxt<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -251,8 +253,11 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
}

// Check whether a modifier is suggested for using this type.
if let Some((suggested_modifier, suggested_result)) =
reg_class.suggest_modifier(asm_arch, asm_ty)
if let Some(ModifierInfo {
modifier: suggested_modifier,
result: suggested_result,
size: suggested_size,
}) = reg_class.suggest_modifier(asm_arch, asm_ty)
{
// Search for any use of this operand without a modifier and emit
// the suggestion for them.
Expand All @@ -266,8 +271,11 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
}
}
if !spans.is_empty() {
let (default_modifier, default_result) =
reg_class.default_modifier(asm_arch).unwrap();
let ModifierInfo {
modifier: default_modifier,
result: default_result,
size: default_size,
} = reg_class.default_modifier(asm_arch).unwrap();
self.tcx.node_span_lint(
lint::builtin::ASM_SUB_REGISTER,
expr.hir_id,
Expand All @@ -276,10 +284,10 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
|lint| {
lint.span_label(expr.span, "for this argument");
lint.help(format!(
"use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}`",
"use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}` (for {suggested_size}-bit values)",
));
lint.help(format!(
"or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}`",
"or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}` (for {default_size}-bit values)",
));
},
);
Expand Down
26 changes: 11 additions & 15 deletions compiler/rustc_target/src/asm/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use crate::spec::{RelocModel, Target};
use rustc_data_structures::fx::FxIndexSet;
use rustc_macros::HashStable_Generic;
Expand Down Expand Up @@ -27,32 +27,28 @@ impl AArch64InlineAsmRegClass {
None
}

pub fn suggest_modifier(
self,
_arch: InlineAsmArch,
ty: InlineAsmType,
) -> Option<(char, &'static str)> {
pub fn suggest_modifier(self, _arch: InlineAsmArch, ty: InlineAsmType) -> Option<ModifierInfo> {
match self {
Self::reg => match ty.size().bits() {
64 => None,
_ => Some(('w', "w0")),
_ => Some(('w', "w0", 32).into()),
},
Self::vreg | Self::vreg_low16 => match ty.size().bits() {
8 => Some(('b', "b0")),
16 => Some(('h', "h0")),
32 => Some(('s', "s0")),
64 => Some(('d', "d0")),
128 => Some(('q', "q0")),
8 => Some(('b', "b0", 8).into()),
16 => Some(('h', "h0", 16).into()),
32 => Some(('s', "s0", 32).into()),
64 => Some(('d', "d0", 64).into()),
128 => Some(('q', "q0", 128).into()),
_ => None,
},
Self::preg => None,
}
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
match self {
Self::reg => Some(('x', "x0")),
Self::vreg | Self::vreg_low16 => Some(('v', "v0")),
Self::reg => Some(('x', "x0", 64).into()),
Self::vreg | Self::vreg_low16 => Some(('v', "v0", 128).into()),
Self::preg => None,
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/arm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use crate::spec::{RelocModel, Target};
use rustc_data_structures::fx::FxIndexSet;
use rustc_macros::HashStable_Generic;
Expand Down Expand Up @@ -35,11 +35,11 @@ impl ArmInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/avr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand Down Expand Up @@ -29,11 +29,11 @@ impl AvrInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/bpf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -23,11 +23,11 @@ impl BpfInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/csky.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -23,11 +23,11 @@ impl CSKYInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/hexagon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -22,11 +22,11 @@ impl HexagonInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/loongarch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -23,11 +23,11 @@ impl LoongArchInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/m68k.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -24,11 +24,11 @@ impl M68kInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/mips.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -23,11 +23,11 @@ impl MipsInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
20 changes: 14 additions & 6 deletions compiler/rustc_target/src/asm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ use rustc_span::Symbol;
use std::fmt;
use std::str::FromStr;

pub struct ModifierInfo {
pub modifier: char,
pub result: &'static str,
pub size: u64,
}

impl From<(char, &'static str, u64)> for ModifierInfo {
fn from((modifier, result, size): (char, &'static str, u64)) -> Self {
Self { modifier, result, size }
}
}

macro_rules! def_reg_class {
($arch:ident $arch_regclass:ident {
$(
Expand Down Expand Up @@ -512,11 +524,7 @@ impl InlineAsmRegClass {
/// Such suggestions are useful if a type smaller than the full register
/// size is used and a modifier can be used to point to the subregister of
/// the correct size.
pub fn suggest_modifier(
self,
arch: InlineAsmArch,
ty: InlineAsmType,
) -> Option<(char, &'static str)> {
pub fn suggest_modifier(self, arch: InlineAsmArch, ty: InlineAsmType) -> Option<ModifierInfo> {
match self {
Self::X86(r) => r.suggest_modifier(arch, ty),
Self::Arm(r) => r.suggest_modifier(arch, ty),
Expand Down Expand Up @@ -545,7 +553,7 @@ impl InlineAsmRegClass {
/// This is only needed when the register class can suggest a modifier, so
/// that the user can be shown how to get the default behavior without a
/// warning.
pub fn default_modifier(self, arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, arch: InlineAsmArch) -> Option<ModifierInfo> {
match self {
Self::X86(r) => r.default_modifier(arch),
Self::Arm(r) => r.default_modifier(arch),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/msp430.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -22,11 +22,11 @@ impl Msp430InlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/nvptx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;

Expand All @@ -23,11 +23,11 @@ impl NvptxInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/powerpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand Down Expand Up @@ -26,11 +26,11 @@ impl PowerPCInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/riscv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use crate::spec::{RelocModel, Target};
use rustc_data_structures::fx::FxIndexSet;
use rustc_macros::HashStable_Generic;
Expand Down Expand Up @@ -26,11 +26,11 @@ impl RiscVInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
Loading

0 comments on commit 6f2f720

Please sign in to comment.