Re: [PATCH v3 1/5] rust: extract `bitfield!` macro from `register!`
From: Gary Guo
Date: Mon May 11 2026 - 13:32:43 EST
On Fri May 1, 2026 at 7:03 AM BST, Alexandre Courbot wrote:
> + // Private field accessors working with the exact `Bounded` type for the field.
> + (
> + @private_field_accessors $vis:vis $name:ident $storage:ty : $hi:tt:$lo:tt $field:ident
> + ) => {
> + ::kernel::macros::paste!(
> + $vis const [<$field:upper _RANGE>]: ::core::ops::RangeInclusive<u8> = $lo..=$hi;
> + $vis const [<$field:upper _MASK>]: $storage =
> + ((((1 << $hi) - 1) << 1) + 1) - ((1 << $lo) - 1);
> + $vis const [<$field:upper _SHIFT>]: u32 = $lo;
> + );
> +
> + ::kernel::macros::paste!(
> + fn [<__ $field>](self) ->
This and the __with_ methods are missing inline annotations. I'm witnessing
these functions showing up in the vmlinux uninlined. (To be fair, this is an
existing issue because the `register!` macro generated onces are also showing up).
Best,
Gary
> + ::kernel::num::Bounded<$storage, { $hi + 1 - $lo }> {
> + // Left shift to align the field's MSB with the storage MSB.
> + const ALIGN_TOP: u32 = $storage::BITS - ($hi + 1);
> + // Right shift to move the top-aligned field to bit 0 of the storage.
> + const ALIGN_BOTTOM: u32 = ALIGN_TOP + $lo;
> +
> + // Extract the field using two shifts. `Bounded::shr` produces the correctly-sized
> + // output type.
> + let val = ::kernel::num::Bounded::<$storage, { $storage::BITS }>::from(
> + self.inner << ALIGN_TOP
> + );
> + val.shr::<ALIGN_BOTTOM, { $hi + 1 - $lo } >()
> + }
> +
> + const fn [<__with_ $field>](
> + mut self,
> + value: ::kernel::num::Bounded<$storage, { $hi + 1 - $lo }>,
> + ) -> Self
> + {
> + const MASK: $storage = <$name>::[<$field:upper _MASK>];
> + const SHIFT: u32 = <$name>::[<$field:upper _SHIFT>];
> +
> + let value = value.get() << SHIFT;
> + self.inner = (self.inner & !MASK) | value;
> +
> + self
> + }
> + );
> + };