rust: make usage of bitmasks and bitfields nicer
Subsystems (2)
cover rust: make usage of bitmasks and bitfields nicer
This adds or modifies some of the early choices that were done when writing the pl011 crate. Now that the implementation is safe and readable, it's possible to look at the code as a whole, see what looks ugly and fix it.
To see the effect on the pl011 code, jump at patches 2 and 4. But overall the focus is on "ugly" constant declarations of two kinds.
First, bitmasks. These are currently written like this:
const IRQMASK: [u32; 6] = [
Interrupt::E.0 | Interrupt::MS.0 | Interrupt::RT.0 | Interrupt::TX.0 | Interrupt::RX.0,
...
}Here the issue is threefold: 1) the repeated "Interrupt::"; 2) the ugly ".0" and 3) the type of IRQMASK is not "Interrupt" to avoid further ".0"s elsewhere.
All this can be fixed by abstracting this kind of bitmasks, and the go-to solution for Rust is called "bitflags". However, it does not solve the constant declaration part so I'm rolling my own here, while keeping it similar to bitflags so it's not completely foreign to Rust developers. It's a bit simpler and it has the extra expression parsing functionality, so that the above becomes:
const IRQMASK: [Interrupt; 6] = [
bits!(Interrupt: E | MS | RT | TX | RX)
...
}
(this specific case can also be written simply as "Interrupt::all()", because all bits are set, but still).
Second, while the "bilge" crate is very nice it was written with the expectation that const traits would be stable Real Soon. They didn't and we're left with stuff like
pub const BREAK: Self = Self { value: 1 << 10 };So this series replaces "bilge" with an alternative implementation of bitfields called (less cryptically) "bitfield-struct". Now, there is a trade-off here. It is described more in detail in patch 4, but roughly speaking:
++ bitfield-struct supports "const" well
+ bitfield-struct is much smaller than bilge, so that it is possible to remove a bunch of subprojects
- bitfield-struct requires manual size annotations for anything that is not a primitive type (bool, iNN or uNN); this is especially annoying for enums
It's important that the disadvantages affect only the definition of the type. Code that *uses* the type is the same or better, for example the above const declaration becomes:
pub const BREAK: Self = Self::new().with_errors(Errors::BREAK);
This "with_*()" convention is in fact the same that it's used for VMState declarations, such as in ".with_version_id(N)", so there is some consistency too.
Again, I do like "bilge" and I think it's usage of arbitrary-width types like "u4" is very nice and readable. If it ever grows "const" capabilities it's certainly possible to come back to it, but right now I feel that the trade-off leans towards the switch.
Paolo
Paolo Bonzini (6):
rust: add "bits", a custom bitflags implementation
rust: pl011: use the bits macro
rust: qemu-api-macros: add from_bits and into_bits to
#[derive(TryInto)]
rust: subprojects: add bitfield-struct
rust: pl011: switch from bilge to bitfield-struct
rust: remove bilge crate
rust/Cargo.lock | 75 +-- rust/Cargo.toml | 2 + rust/bits/Cargo.toml | 19 + rust/bits/meson.build | 12 + rust/bits/src/lib.rs | 441 ++++++++++++++++++ rust/hw/char/pl011/Cargo.toml | 4 +- rust/hw/char/pl011/meson.build | 12 +- rust/hw/char/pl011/src/device.rs | 51 +- rust/hw/char/pl011/src/registers.rs | 145 +++--- rust/meson.build | 4 + rust/qemu-api-macros/src/bits.rs | 227 +++++++++ rust/qemu-api-macros/src/lib.rs | 60 ++- rust/qemu-api/src/vmstate.rs | 34 +- subprojects/.gitignore | 8 +- subprojects/arbitrary-int-1-rs.wrap | 10 - subprojects/bilge-0.2-rs.wrap | 10 - subprojects/bilge-impl-0.2-rs.wrap | 10 - subprojects/bitfield-struct-0.9-rs.wrap | 7 + subprojects/either-1-rs.wrap | 10 - subprojects/itertools-0.11-rs.wrap | 10 - .../bitfield-struct-0.9-rs/meson.build | 36 ++ subprojects/proc-macro-error-1-rs.wrap | 10 - subprojects/proc-macro-error-attr-1-rs.wrap | 10 - 23 files changed, 917 insertions(+), 290 deletions(-) create mode 100644 rust/bits/Cargo.toml create mode 100644 rust/bits/meson.build create mode 100644 rust/bits/src/lib.rs create mode 100644 rust/qemu-api-macros/src/bits.rs delete mode 100644 subprojects/arbitrary-int-1-rs.wrap delete mode 100644 subprojects/bilge-0.2-rs.wrap delete mode 100644 subprojects/bilge-impl-0.2-rs.wrap create mode 100644 subprojects/bitfield-struct-0.9-rs.wrap delete mode 100644 subprojects/either-1-rs.wrap delete mode 100644 subprojects/itertools-0.11-rs.wrap create mode 100644 subprojects/packagefiles/bitfield-struct-0.9-rs/meson.build delete mode 100644 subprojects/proc-macro-error-1-rs.wrap delete mode 100644 subprojects/proc-macro-error-attr-1-rs.wrap
Signature · 1 line
2.49.0
RFC PATCH 1/6 rust: add "bits", a custom bitflags implementation
One common thing that device emulation does is manipulate bitmasks, for example to check whether two bitmaps have common bits. One example in the pl011 crate is the checks for pending interrupts, where an interrupt cause corresponds to at least one interrupt source from a fixed set.
Unfortunately, this is one case where Rust *can* provide some kind of abstraction but it does so with a rather Perl-ish There Is More Way To Do It. It is not something where a crate like "bilge" helps, because it only covers the packing of bits in a structure; operations like "are all bits of Y set in X" almost never make sense for bit-packed structs; you need something else, there are several crates that do it and of course we're going to roll our own.
In particular I examined three:
- bitmask (https://docs.rs/bitmask/0.5.0/bitmask/) does not support const at all. This is a showstopper because one of the ugly things in the current pl011 code is the ugliness of code that defines interrupt masks at compile time:
pub const E: Self = Self(Self::OE.0 | Self::BE.0 | Self::PE.0 | Self::FE.0);
or even worse:
const IRQMASK: [u32; 6] = [
Interrupt::E.0 | Interrupt::MS.0 | Interrupt::RT.0 | Interrupt::TX.0 | Interrupt::RX.0,
...
}You would have to use roughly the same code---"bitmask" only helps with defining the struct.
- bitmask_enum (https://docs.rs/bitmask-enum/2.2.5/bitmask_enum/) does not have a good separation of "valid" and "invalid" bits, so for example "!x" will invert all 16 bits if you choose u16 as the representation -- even if you only defined 10 bits. This makes it easier to introduce subtle bugs in comparisons.
- bitflags (https://docs.rs/bitflags/2.6.0/bitflags/) is generally the most used such crate and is the one that I took most inspiration from with respect to the syntax. It's a pretty sophisticated implementation, with a lot of bells and whistles such as an implementation of "Iter" that returns the bits one at a time.
The main thing that all of them lack, however, is a way to simplify the ugly definitions like the above. "bitflags" includes const methods that perform AND/OR/XOR of masks (these are necessary because Rust operator overloading does not support const yet, and therefore overloaded operators cannot be used in the definition of a "static" variable), but they become even more verbose and unmanageable, like
Interrupt::E.union(Interrupt::MS).union(Interrupt::RT).union(Interrupt::TX).union(Interrupt::RX)
This was the main reason to create "bits", which allows something like
bits!(Interrupt: E | MS | RT | TX | RX)
and expands it 1) add "Interrupt::" in front of all identifiers 2) convert operators to the wordy const functions like "union". It supports boolean operators "&", "|", "^", "!" and parentheses, with a relatively simple recursive descent parser that's implemented in qemu_api_macros.
--- rust/Cargo.lock | 7 + rust/Cargo.toml | 1 + rust/bits/Cargo.toml | 19 ++ rust/bits/meson.build | 12 + rust/bits/src/lib.rs | 441 +++++++++++++++++++++++++++++++ rust/meson.build | 1 + rust/qemu-api-macros/src/bits.rs | 227 ++++++++++++++++ rust/qemu-api-macros/src/lib.rs | 12 + 8 files changed, 720 insertions(+) create mode 100644 rust/bits/Cargo.toml create mode 100644 rust/bits/meson.build create mode 100644 rust/bits/src/lib.rs create mode 100644 rust/qemu-api-macros/src/bits.rs
rust/Cargo.lock +7 -0
@@ -31,6 +31,13 @@ dependencies = [ "syn", ] +[[package]] +name = "bits" +version = "0.1.0" +dependencies = [ + "qemu_api_macros", +] + [[package]] name = "either" version = "1.12.0"
rust/Cargo.toml +1 -0
@@ -1,6 +1,7 @@ [workspace] resolver = "2" members = [ + "bits", "qemu-api-macros", "qemu-api", "hw/char/pl011",
rust/bits/Cargo.toml new file +19 -0
@@ -0,0 +1,19 @@ +[package] +name = "bits" +version = "0.1.0" +authors = ["Paolo Bonzini <[email protected]>"] +description = "const-friendly bit flags" +resolver = "2" +publish = false + +edition.workspace = true +homepage.workspace = true +license.workspace = true +repository.workspace = true +rust-version.workspace = true + +[dependencies] +qemu_api_macros = { path = "../qemu-api-macros" } + +[lints] +workspace = true
rust/bits/meson.build new file +12 -0
@@ -0,0 +1,12 @@ +_bits_rs = static_library( + 'bits', + 'src/lib.rs', + override_options: ['rust_std=2021', 'build.rust_std=2021'], + rust_abi: 'rust', + dependencies: [qemu_api_macros], +) + +bits_rs = declare_dependency(link_with: _bits_rs) + +rust.test('rust-bits-tests', _bits_rs, + suite: ['unit', 'rust'])
rust/bits/src/lib.rs new file +441 -0
441 lines not on this page · raw message
rust/meson.build +1 -0
@@ -1,4 +1,5 @@ subdir('qemu-api-macros') +subdir('bits') subdir('qemu-api') subdir('hw')
rust/qemu-api-macros/src/bits.rs new file +227 -0
227 lines not on this page · raw message
rust/qemu-api-macros/src/lib.rs +12 -0
@@ -12,6 +12,9 @@mod utils;use utils::MacroError ; +mod bits; +use bits::BitsConstInternal ; +fn get_fields <'a>( input: &'aDeriveInput , msg: &str,
@@ -219,3 +222,12 @@ pub fn derive_tryinto(input: TokenStream) -> TokenStream {TokenStream ::from (expanded) } + +#[proc_macro] +pub fn bits_const_internal (ts:TokenStream ) ->TokenStream { +let ts = proc_macro2::TokenStream ::from (ts); +let mut it = ts.into_iter (); + +let expanded =BitsConstInternal ::parse (&mut it).unwrap_or_else (Into ::into); +TokenStream ::from (expanded) +}
Signature · 1 line
2.49.0
Re: [RFC PATCH 1/6] rust: add "bits", a custom bitflags implementation
rust/bits/src/lib.rs
Quoted 84 lines · Paolo Bonzini
One common thing that device emulation does is manipulate bitmasks, for example to check whether two bitmaps have common bits. One example in the pl011 crate is the checks for pending interrupts, where an interrupt cause corresponds to at least one interrupt source from a fixed set.
Unfortunately, this is one case where Rust *can* provide some kind of abstraction but it does so with a rather Perl-ish There Is More Way To Do It. It is not something where a crate like "bilge" helps, because it only covers the packing of bits in a structure; operations like "are all bits of Y set in X" almost never make sense for bit-packed structs; you need something else, there are several crates that do it and of course we're going to roll our own.
In particular I examined three:
- bitmask (https://docs.rs/bitmask/0.5.0/bitmask/) does not support const at all. This is a showstopper because one of the ugly things in the current pl011 code is the ugliness of code that defines interrupt masks at compile time:
pub const E: Self = Self(Self::OE.0 | Self::BE.0 | Self::PE.0 | Self::FE.0);or even worse:
const IRQMASK: [u32; 6] = [ Interrupt::E.0 | Interrupt::MS.0 | Interrupt::RT.0 | Interrupt::TX.0 | Interrupt::RX.0, ... }You would have to use roughly the same code---"bitmask" only helps with defining the struct.
-
bitmask_enum(https://docs.rs/bitmask-enum/2.2.5/bitmask_enum/) does not have a good separation of "valid" and "invalid" bits, so for example "!x" will invert all 16 bits if you choose u16 as the representation -- even if you only defined 10 bits. This makes it easier to introduce subtle bugs in comparisons.- bitflags (https://docs.rs/bitflags/2.6.0/bitflags/) is generally the most used such crate and is the one that I took most inspiration from with respect to the syntax. It's a pretty sophisticated implementation, with a lot of bells and whistles such as an implementation of "Iter" that returns the bits one at a time.
The main thing that all of them lack, however, is a way to simplify the ugly definitions like the above. "bitflags" includes const methods that perform AND/OR/XOR of masks (these are necessary because Rust operator overloading does not support const yet, and therefore overloaded operators cannot be used in the definition of a "static" variable), but they become even more verbose and unmanageable, like
Interrupt::E.union(Interrupt::MS).union(Interrupt::RT).union(Interrupt::TX).union(Interrupt::RX)
This was the main reason to create "bits", which allows something like
bits!(Interrupt: E | MS | RT | TX | RX)
and expands it 1) add "Interrupt::" in front of all identifiers 2) convert operators to the wordy const functions like "union". It supports boolean operators "&", "|", "^", "!" and parentheses, with a relatively simple recursive descent parser that's implemented in
qemu_api_macros.Signed-off-by Paolo Bonzini <[email protected]>--- rust/Cargo.lock | 7 + rust/Cargo.toml | 1 + rust/bits/Cargo.toml | 19 ++ rust/bits/meson.build | 12 + rust/bits/src/lib.rs | 441 +++++++++++++++++++++++++++++++ rust/meson.build | 1 + rust/qemu-api-macros/src/bits.rs | 227 ++++++++++++++++ rust/qemu-api-macros/src/lib.rs | 12 + 8 files changed, 720 insertions(+) create mode 100644 rust/bits/Cargo.toml create mode 100644 rust/bits/meson.build create mode 100644 rust/bits/src/lib.rs create mode 100644 rust/qemu-api-macros/src/bits.rsrust/bits/src/lib.rs new file +0
-0@@ -0,0 +1,441 @@
This (and other new .rs files) needs SPDX-License-Identifier
Quoted 7 lines
rust/bits/src/lib.rs new file +7
-0+/// # Definition entry point +/// +/// Define a struct with a single field of type $type. Include public constants +/// for each element listed in braces. +/// +/// The unnamed element at the end, if present, can be used to enlarge the set +/// of valid bits. Bits that are valid but not listed are treated normally for
rust/bits/src/lib.rs line 17
Quoted 10 lines
rust/bits/src/lib.rs new file +10
-0+/// the purpose of arithmetic operations, and are printed with their hexadecimal +/// value. +/// +/// The struct implements the following traits: [`BitAnd`](std::ops::BitAnd), +/// [`BitOr`](std::ops::BitOr), [`BitXor`](std::ops::BitXor), +/// [`Not`](std::ops::Not), [`Sub`](std::ops::Sub); [`Debug`](std::fmt::Debug), +/// [`Display`](std::fmt::Display), [`Binary`](std::fmt::Binary), +/// [`Octal`](std::fmt::Octal), [`LowerHex`](std::fmt::LowerHex), +/// [`UpperHex`](std::fmt::UpperHex); [`From`]`<type>`/[`Into`]`<type>` where +/// type is the type specified in the definition.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [RFC PATCH 1/6] rust: add "bits", a custom bitflags implementation
Quoted 80 lines · Daniel P. Berrangé
Quoted 77 lines · Paolo Bonzini
One common thing that device emulation does is manipulate bitmasks, for example to check whether two bitmaps have common bits. One example in the pl011 crate is the checks for pending interrupts, where an interrupt cause corresponds to at least one interrupt source from a fixed set.
Unfortunately, this is one case where Rust *can* provide some kind of abstraction but it does so with a rather Perl-ish There Is More Way To Do It. It is not something where a crate like "bilge" helps, because it only covers the packing of bits in a structure; operations like "are all bits of Y set in X" almost never make sense for bit-packed structs; you need something else, there are several crates that do it and of course we're going to roll our own.
In particular I examined three:
- bitmask (https://docs.rs/bitmask/0.5.0/bitmask/) does not support const at all. This is a showstopper because one of the ugly things in the current pl011 code is the ugliness of code that defines interrupt masks at compile time:
pub const E: Self = Self(Self::OE.0 | Self::BE.0 | Self::PE.0 | Self::FE.0);or even worse:
const IRQMASK: [u32; 6] = [ Interrupt::E.0 | Interrupt::MS.0 | Interrupt::RT.0 | Interrupt::TX.0 | Interrupt::RX.0, ... }You would have to use roughly the same code---"bitmask" only helps with defining the struct.
-
bitmask_enum(https://docs.rs/bitmask-enum/2.2.5/bitmask_enum/) does not have a good separation of "valid" and "invalid" bits, so for example "!x" will invert all 16 bits if you choose u16 as the representation -- even if you only defined 10 bits. This makes it easier to introduce subtle bugs in comparisons.- bitflags (https://docs.rs/bitflags/2.6.0/bitflags/) is generally the most used such crate and is the one that I took most inspiration from with respect to the syntax. It's a pretty sophisticated implementation, with a lot of bells and whistles such as an implementation of "Iter" that returns the bits one at a time.
The main thing that all of them lack, however, is a way to simplify the ugly definitions like the above. "bitflags" includes const methods that perform AND/OR/XOR of masks (these are necessary because Rust operator overloading does not support const yet, and therefore overloaded operators cannot be used in the definition of a "static" variable), but they become even more verbose and unmanageable, like
Interrupt::E.union(Interrupt::MS).union(Interrupt::RT).union(Interrupt::TX).union(Interrupt::RX)
This was the main reason to create "bits", which allows something like
bits!(Interrupt: E | MS | RT | TX | RX)
and expands it 1) add "Interrupt::" in front of all identifiers 2) convert operators to the wordy const functions like "union". It supports boolean operators "&", "|", "^", "!" and parentheses, with a relatively simple recursive descent parser that's implemented in
qemu_api_macros.Signed-off-by Paolo Bonzini <[email protected]>--- rust/Cargo.lock | 7 + rust/Cargo.toml | 1 + rust/bits/Cargo.toml | 19 ++ rust/bits/meson.build | 12 + rust/bits/src/lib.rs | 441 +++++++++++++++++++++++++++++++ rust/meson.build | 1 + rust/qemu-api-macros/src/bits.rs | 227 ++++++++++++++++ rust/qemu-api-macros/src/lib.rs | 12 + 8 files changed, 720 insertions(+) create mode 100644 rust/bits/Cargo.toml create mode 100644 rust/bits/meson.build create mode 100644 rust/bits/src/lib.rs create mode 100644 rust/qemu-api-macros/src/bits.rs
Quoted 8 lines
rust/bits/src/lib.rs
Quoted 6 lines
rust/bits/src/lib.rs new file +0
-0@@ -0,0 +1,441 @@This (and other new .rs files) needs SPDX-License-Identifier
We should probably lint for this in .rs files.
Signature · 2 lines
Manos Pitsidianakis Emulation and Virtualization Engineer at Linaro Ltd
Re: [RFC PATCH 1/6] rust: add "bits", a custom bitflags implementation
Quoted 90 lines · Manos Pitsidianakis
Quoted 80 lines · Daniel P. Berrangé
Quoted 77 lines · Paolo Bonzini
One common thing that device emulation does is manipulate bitmasks, for example to check whether two bitmaps have common bits. One example in the pl011 crate is the checks for pending interrupts, where an interrupt cause corresponds to at least one interrupt source from a fixed set.
Unfortunately, this is one case where Rust *can* provide some kind of abstraction but it does so with a rather Perl-ish There Is More Way To Do It. It is not something where a crate like "bilge" helps, because it only covers the packing of bits in a structure; operations like "are all bits of Y set in X" almost never make sense for bit-packed structs; you need something else, there are several crates that do it and of course we're going to roll our own.
In particular I examined three:
- bitmask (https://docs.rs/bitmask/0.5.0/bitmask/) does not support const at all. This is a showstopper because one of the ugly things in the current pl011 code is the ugliness of code that defines interrupt masks at compile time:
pub const E: Self = Self(Self::OE.0 | Self::BE.0 | Self::PE.0 | Self::FE.0);or even worse:
const IRQMASK: [u32; 6] = [ Interrupt::E.0 | Interrupt::MS.0 | Interrupt::RT.0 | Interrupt::TX.0 | Interrupt::RX.0, ... }You would have to use roughly the same code---"bitmask" only helps with defining the struct.
-
bitmask_enum(https://docs.rs/bitmask-enum/2.2.5/bitmask_enum/) does not have a good separation of "valid" and "invalid" bits, so for example "!x" will invert all 16 bits if you choose u16 as the representation -- even if you only defined 10 bits. This makes it easier to introduce subtle bugs in comparisons.- bitflags (https://docs.rs/bitflags/2.6.0/bitflags/) is generally the most used such crate and is the one that I took most inspiration from with respect to the syntax. It's a pretty sophisticated implementation, with a lot of bells and whistles such as an implementation of "Iter" that returns the bits one at a time.
The main thing that all of them lack, however, is a way to simplify the ugly definitions like the above. "bitflags" includes const methods that perform AND/OR/XOR of masks (these are necessary because Rust operator overloading does not support const yet, and therefore overloaded operators cannot be used in the definition of a "static" variable), but they become even more verbose and unmanageable, like
Interrupt::E.union(Interrupt::MS).union(Interrupt::RT).union(Interrupt::TX).union(Interrupt::RX)
This was the main reason to create "bits", which allows something like
bits!(Interrupt: E | MS | RT | TX | RX)
and expands it 1) add "Interrupt::" in front of all identifiers 2) convert operators to the wordy const functions like "union". It supports boolean operators "&", "|", "^", "!" and parentheses, with a relatively simple recursive descent parser that's implemented in
qemu_api_macros.Signed-off-by Paolo Bonzini <[email protected]>--- rust/Cargo.lock | 7 + rust/Cargo.toml | 1 + rust/bits/Cargo.toml | 19 ++ rust/bits/meson.build | 12 + rust/bits/src/lib.rs | 441 +++++++++++++++++++++++++++++++ rust/meson.build | 1 + rust/qemu-api-macros/src/bits.rs | 227 ++++++++++++++++ rust/qemu-api-macros/src/lib.rs | 12 + 8 files changed, 720 insertions(+) create mode 100644 rust/bits/Cargo.toml create mode 100644 rust/bits/meson.build create mode 100644 rust/bits/src/lib.rs create mode 100644 rust/qemu-api-macros/src/bits.rsQuoted 8 lines
rust/bits/src/lib.rs
Quoted 6 lines
rust/bits/src/lib.rs new file +0
-0@@ -0,0 +1,441 @@This (and other new .rs files) needs SPDX-License-Identifier
Quoted 1 line
We should probably lint for this in .rs files.
Strengthened from a recommendation to a mandate in:
https://lists.nongnu.org/archive/html/qemu-devel/2025-05/msg04968.html
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
RFC PATCH 2/6 rust: pl011: use the bits macro
In reply to Paolo BonziniThis avoids the repeated ".0" when using the Interrupt struct.
--- rust/Cargo.lock | 1 + rust/hw/char/pl011/Cargo.toml | 1 + rust/hw/char/pl011/meson.build | 1 + rust/hw/char/pl011/src/device.rs | 51 ++++++++++++++--------------- rust/hw/char/pl011/src/registers.rs | 39 ++++++++++++---------- 5 files changed, 49 insertions(+), 44 deletions(-)
rust/Cargo.lock +1 -0
@@ -73,6 +73,7 @@ version = "0.1.0" dependencies = [ "bilge", "bilge-impl", + "bits", "qemu_api", "qemu_api_macros", ]
rust/hw/char/pl011/Cargo.toml +1 -0
@@ -18,6 +18,7 @@ crate-type = ["staticlib"] [dependencies] bilge = { version = "0.2.0" } bilge-impl = { version = "0.2.0" } +bits = { path = "../../../bits" } qemu_api = { path = "../../../qemu-api" } qemu_api_macros = { path = "../../../qemu-api-macros" }
rust/hw/char/pl011/meson.build +1 -0
@@ -12,6 +12,7 @@ _libpl011_rs = static_library( dependencies: [ bilge_dep, bilge_impl_dep, + bits_rs, qemu_api, qemu_api_macros, ],
rust/hw/char/pl011/src/device.rs +25 -26
124 lines not on this page · raw message
rust/hw/char/pl011/src/registers.rs +21 -18
@@ -9,7 +9,8 @@// https://developer.arm.com/documentation/ddi0183/latest/ use bilge::prelude::*; -use qemu_api::impl_vmstate_bitsized; +use bits::bits; +use qemu_api::{impl_vmstate_bitsized, impl_vmstate_forward};/// Offset of each register from the base memory address of the device. #[doc(alias = "offset" )]
@@ -326,22 +327,24 @@ fn default() -> Self { } } -/// Interrupt status bits in UARTRIS, UARTMIS, UARTIMSC -pub struct Interrupt (pub u32 ); +bits! { +/// Interrupt status bits in UARTRIS, UARTMIS, UARTIMSC +#[derive( Default )] +pub struct Interrupt (u32 ) { +OE =1 <<10 , +BE =1 <<9 , +PE =1 <<8 , +FE =1 <<7 , +RT =1 <<6 , +TX =1 <<5 , +RX =1 <<4 , +DSR =1 <<3 , +DCD =1 <<2 , +CTS =1 <<1 , +RI =1 <<0 , -impl Interrupt { -pub const OE :Self =Self (1 <<10 ); -pub const BE :Self =Self (1 <<9 ); -pub const PE :Self =Self (1 <<8 ); -pub const FE :Self =Self (1 <<7 ); -pub const RT :Self =Self (1 <<6 ); -pub const TX :Self =Self (1 <<5 ); -pub const RX :Self =Self (1 <<4 ); -pub const DSR :Self =Self (1 <<3 ); -pub const DCD :Self =Self (1 <<2 ); -pub const CTS :Self =Self (1 <<1 ); -pub const RI :Self =Self (1 <<0 ); - -pub const E :Self =Self (Self ::OE .0 |Self ::BE .0 |Self ::PE .0 |Self ::FE .0 ); -pub const MS :Self =Self (Self ::RI .0 |Self ::DSR .0 |Self ::DCD .0 |Self ::CTS .0 ); +E =bits! (Self as u32 :OE |BE |PE |FE ), +MS =bits! (Self as u32 :RI |DSR |DCD |CTS ), + } } +impl_vmstate_forward! (Interrupt );
Signature · 1 line
2.49.0
RFC PATCH 3/6 rust: qemu-api-macros: add from_bits and into_bits to #[derive(TryInto)]
In reply to Paolo BonziniThese const functions make it possible to use enums easily together with the bitfield-struct crate.
--- rust/qemu-api-macros/src/lib.rs | 48 ++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 10 deletions(-)
rust/qemu-api-macros/src/lib.rs +38 -10
@@ -193,23 +193,51 @@ fn get_variants(input: &DeriveInput) -> Result<&Punctuated<Variant, Comma>, Macr }#[rustfmt::skip::macros(quote)] -fn derive_tryinto_or_error (input:DeriveInput ) ->Result <proc_macro2::TokenStream ,MacroError > { -let repr =get_repr_uN (&input,"#[derive(TryInto)]" )?; - -let name = &input.ident; -let variants =get_variants (&input)?; +fn derive_tryinto_body ( + name: &Ident , + variants: &Punctuated <Variant ,Comma >, + repr: &Path , +) ->Result <proc_macro2::TokenStream ,MacroError > {let discriminants:Vec <&Ident > = variants.iter ().map (|f| &f.ident).collect ();Ok (quote! { + #(const #discriminants: #repr = #name::#discriminantsas #repr ;)*; +match value { + #(#discriminants =>Ok (#name::#discriminants),)* + _ =>Err (value), + } + }) +} + +#[rustfmt::skip::macros(quote)] +fn derive_tryinto_or_error (input:DeriveInput ) ->Result <proc_macro2::TokenStream ,MacroError > { +let repr =get_repr_uN (&input,"#[derive(TryInto)]" )?; +let name = &input.ident; +let body =derive_tryinto_body (name,get_variants (&input)?, &repr)?; +let errmsg =format! ("invalid value for {name}" ); + +Ok (quote! { +impl #name { +#[allow(dead_code)] +pub const fn into_bits (self) -> #repr { + selfas #repr + } + +#[allow(dead_code)] +pub const fn from_bits (value: #repr ) ->Self { +match ({ + #body + }) { +Ok (x) => x, +Err (_) =>panic! (#errmsg) + } + } + }impl core::convert::TryFrom <#repr >for #name {type Error = #repr ;fn try_from (value: #repr ) ->Result <Self ,Self ::Error > { - #(const #discriminants: #repr = #name::#discriminantsas #repr ;)*; -match value { - #(#discriminants =>Ok (Self ::#discriminants),)* - _ =>Err (value), - } + #body } } })
Signature · 1 line
2.49.0
RFC PATCH 4/6 rust: subprojects: add bitfield-struct
In reply to Paolo BonziniThe bitfield-struct is much smaller than "bilge" and supports "const" expressions better. The main disadvantage is that it does not let you annotate enums as bitfields and does not integrate with arbitrary-int, thus requiring manual size annotations for anything that is not a bool, iNN or uNN.
Lack of support for arbitrary-int is a very minor change; enums are a bit more annoying because they require manual implementation of two functions from_bits() and into_bits(); however, that is already handled by QEMU's bits! and #[derive(qemu_api_macros::TryInto)] utilities. Together, these basically make the manual work go away.
--- rust/meson.build | 3 ++ subprojects/.gitignore | 1 + subprojects/bitfield-struct-0.9-rs.wrap | 7 ++++ .../bitfield-struct-0.9-rs/meson.build | 36 +++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 subprojects/bitfield-struct-0.9-rs.wrap create mode 100644 subprojects/packagefiles/bitfield-struct-0.9-rs/meson.build
rust/meson.build +3 -0
@@ -1,3 +1,6 @@ +subproject('bitfield-struct-0.9-rs', required: true) +bitfield_struct_dep = dependency('bitfield-struct-0.9-rs') + subdir('qemu-api-macros') subdir('bits') subdir('qemu-api')
subprojects/.gitignore +1 -0
@@ -9,6 +9,7 @@ /arbitrary-int-1.2.7 /bilge-0.2.0 /bilge-impl-0.2.0 +/bitfield-struct-0.9.5 /either-1.12.0 /itertools-0.11.0 /libc-0.2.162
subprojects/bitfield-struct-0.9-rs.wrap new file +7 -0
@@ -0,0 +1,7 @@ +[wrap-file] +directory = bitfield-struct-0.9.5 +source_url = https://crates.io/api/v1/crates/bitfield-struct/0.9.5/download +source_filename = bitfield-struct-0.9.5.tar.gz +source_hash = b2869c63ccf4f8bf0d485070b880e60e097fb7aeea80ee82a0a94a957e372a0b +#method = cargo +patch_directory = bitfield-struct-0.9-rs
subprojects/packagefiles/bitfield-struct-0.9-rs/meson.build new file +36 -0
@@ -0,0 +1,36 @@ +project('bitfield-struct-0.9-rs', 'rust', + meson_version: '>=1.5.0', + version: '0.9.5', + license: 'MIT', + default_options: []) + +subproject('quote-1-rs', required: true) +subproject('syn-2-rs', required: true) +subproject('proc-macro2-1-rs', required: true) + +quote_dep = dependency('quote-1-rs', native: true) +syn_dep = dependency('syn-2-rs', native: true) +proc_macro2_dep = dependency('proc-macro2-1-rs', native: true) + +rust = import('rust') + +_bitfield_struct_rs = rust.proc_macro( + 'bitfield_struct', + files('src/lib.rs'), + override_options: ['rust_std=2021', 'build.rust_std=2021'], + rust_args: [ + '--cap-lints', 'allow', + '--cfg', 'use_fallback', + ], + dependencies: [ + quote_dep, + syn_dep, + proc_macro2_dep, + ], +) + +bitfield_struct_dep = declare_dependency( + link_with: _bitfield_struct_rs, +) + +meson.override_dependency('bitfield-struct-0.9-rs', bitfield_struct_dep)
Signature · 1 line
2.49.0
RFC PATCH 5/6 rust: pl011: switch from bilge to bitfield-struct
In reply to Paolo BonziniThe bilge crate, while very nice and espressive, is heavily reliant on traits; because trait functions are never const, bilge and const mix about as well as water and oil.
Try using the bitfield-struct crate instead. It is built to support const very well and the only downside is that more manual annotations are needed (for enums and non-full-byte members). Otherwise, the use is pretty much the same and in fact device code does not change at all, only register declarations.
Recent versions want to use Rust 1.83, so this uses a slightly older version with basically no lost functionality; but anyway, I want to switch to 1.83 for QEMU as well due to improved "const" support in the compiler.
--- rust/Cargo.toml | 1 + rust/hw/char/pl011/Cargo.toml | 3 +- rust/hw/char/pl011/meson.build | 11 +-- rust/hw/char/pl011/src/registers.rs | 108 ++++++++++++++-------------- 4 files changed, 56 insertions(+), 67 deletions(-)
rust/Cargo.toml +1 -0
@@ -97,5 +97,6 @@ used_underscore_binding = "deny" #wildcard_imports = "deny" # still have many bindings::* imports # these may have false positives +enum_variant_names = "allow" #option_if_let_else = "deny" cognitive_complexity = "deny"
rust/hw/char/pl011/Cargo.toml +1 -2
@@ -16,8 +16,7 @@ rust-version.workspace = true crate-type = ["staticlib"] [dependencies] -bilge = { version = "0.2.0" } -bilge-impl = { version = "0.2.0" } +bitfield-struct = { version = "0.9" } bits = { path = "../../../bits" } qemu_api = { path = "../../../qemu-api" } qemu_api_macros = { path = "../../../qemu-api-macros" }
rust/hw/char/pl011/meson.build +2 -9
@@ -1,17 +1,10 @@ -subproject('bilge-0.2-rs', required: true) -subproject('bilge-impl-0.2-rs', required: true) - -bilge_dep = dependency('bilge-0.2-rs') -bilge_impl_dep = dependency('bilge-impl-0.2-rs') - _libpl011_rs = static_library( 'pl011', files('src/lib.rs'), override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', dependencies: [ - bilge_dep, - bilge_impl_dep, + bitfield_struct_dep, bits_rs, qemu_api, qemu_api_macros,
@@ -22,6 +15,6 @@ rust_devices_ss.add(when: 'CONFIG_X_PL011_RUST', if_true: [declare_dependency( link_whole: [_libpl011_rs], # Putting proc macro crates in `dependencies` is necessary for Meson to find # them when compiling the root per-target static rust lib. - dependencies: [bilge_impl_dep, qemu_api_macros], + dependencies: [bitfield_struct_dep, qemu_api_macros], variables: {'crate': 'pl011'}, )])
rust/hw/char/pl011/src/registers.rs +52 -56
244 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [RFC PATCH 5/6] rust: pl011: switch from bilge to bitfield-struct
Quoted 340 lines · Paolo Bonzini
The bilge crate, while very nice and espressive, is heavily reliant on traits; because trait functions are never const, bilge and const mix about as well as water and oil.
Try using the bitfield-struct crate instead. It is built to support const very well and the only downside is that more manual annotations are needed (for enums and non-full-byte members). Otherwise, the use is pretty much the same and in fact device code does not change at all, only register declarations.
Recent versions want to use Rust 1.83, so this uses a slightly older version with basically no lost functionality; but anyway, I want to switch to 1.83 for QEMU as well due to improved "const" support in the compiler.
Signed-off-by Paolo Bonzini <[email protected]>--- rust/Cargo.toml | 1 + rust/hw/char/pl011/Cargo.toml | 3 +- rust/hw/char/pl011/meson.build | 11 +-- rust/hw/char/pl011/src/registers.rs | 108 ++++++++++++++-------------- 4 files changed, 56 insertions(+), 67 deletions(-)rust/Cargo.toml +1
-0@@ -97,5 +97,6 @@ used_underscore_binding = "deny" #wildcard_imports = "deny" # still have many bindings::* imports # these may have false positives +enum_variant_names = "allow" #option_if_let_else = "deny" cognitive_complexity = "deny"rust/hw/char/pl011/Cargo.toml +1
-2@@ -16,8 +16,7 @@ rust-version.workspace = true crate-type = ["staticlib"] [dependencies] -bilge = { version = "0.2.0" } -bilge-impl = { version = "0.2.0" } +bitfield-struct = { version = "0.9" } bits = { path = "../../../bits" } qemu_api = { path = "../../../qemu-api" } qemu_api_macros = { path = "../../../qemu-api-macros" }rust/hw/char/pl011/meson.build +2
-9@@ -1,17 +1,10 @@ -subproject('bilge-0.2-rs', required: true) -subproject('bilge-impl-0.2-rs', required: true) - -bilge_dep = dependency('bilge-0.2-rs') -bilge_impl_dep = dependency('bilge-impl-0.2-rs') - _libpl011_rs = static_library( 'pl011', files('src/lib.rs'), override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', dependencies: [ - bilge_dep, - bilge_impl_dep, + bitfield_struct_dep, bits_rs, qemu_api, qemu_api_macros,@@ -22,6 +15,6 @@ rust_devices_ss.add(when: 'CONFIG_X_PL011_RUST', if_true: [declare_dependency( link_whole: [_libpl011_rs], # Putting proc macro crates in `dependencies` is necessary for Meson to find # them when compiling the root per-target static rust lib. - dependencies: [bilge_impl_dep, qemu_api_macros], + dependencies: [bitfield_struct_dep, qemu_api_macros], variables: {'crate': 'pl011'}, )])rust/hw/char/pl011/src/registers.rs +52
-57@@ -5,12 +5,16 @@//! Device registers exposed as typed structs which are backed by arbitrary //! integer bitmaps. [`Data`], [`Control`], [`LineControl`], etc. +// rustc prefers "constant-like" enums to use upper case names, but that +// is inconsistent in its own way. +#![allow(non_upper_case_globals)] +// For more detail see the PL011 Technical Reference Manual DDI0183: // https://developer.arm.com/documentation/ddi0183/latest/ -use bilge::prelude::*; +use bitfield_struct::bitfield;use bits::bits; -use qemu_api::{impl_vmstate_bitsized, impl_vmstate_forward}; +use qemu_api::impl_vmstate_forward;/// Offset of each register from the base memory address of the device. #[doc(alias = "offset" )] @@ -78,14 +82,18 @@ pub enum RegisterOffset {/// The `UARTRSR` register is updated only when a read occurs /// from the `UARTDR` register with the same status information /// that can also be obtained by reading the `UARTDR` register -#[bitsize( 8 )] -#[derive( Clone , Copy , Default , DebugBits , FromBits )] +#[bitfield( u8 )] pub struct Errors {pub framing_error:bool ,pub parity_error:bool ,pub break_error:bool ,pub overrun_error:bool , - _reserved_unpredictable:u4 , +#[bits( 4 )] + _reserved_unpredictable:u8 , +} + +impl Errors { +pub const BREAK :Self =Errors ::new ().with_break_error (true ); }/// Data Register, `UARTDR` @@ -93,19 +101,18 @@ pub struct Errors {/// The `UARTDR` register is the data register; write for TX and /// read for RX. It is a 12-bit register, where bits 7..0 are the /// character and bits 11..8 are error bits. -#[bitsize( 32 )] -#[derive( Clone , Copy , Default , DebugBits , FromBits )] +#[bitfield( u32 )] #[doc(alias = "UARTDR" )] pub struct Data {pub data:u8 , +#[bits( 8 )] pub errors:Errors , _reserved:u16 , } -impl_vmstate_bitsized! (Data ); +impl_vmstate_forward! (Data );impl Data { -// bilge is not very const-friendly, unfortunately -pub const BREAK :Self =Self { value:1 <<10 }; +pub const BREAK :Self =Self ::new ().with_errors (Errors ::BREAK ); }/// Receive Status Register / Error Clear Register, `UARTRSR/UARTECR` @@ -119,13 +126,14 @@ impl Data {/// and UARTECR for writes, but really it's a single error status /// register where writing anything to the register clears the error /// bits. -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 )] pub struct ReceiveStatusErrorClear { +#[bits( 8 )] pub errors:Errors , - _reserved_unpredictable:u24 , +#[bits( 24 )] + _reserved_unpredictable:u32 , } -impl_vmstate_bitsized! (ReceiveStatusErrorClear ); +impl_vmstate_forward! (ReceiveStatusErrorClear );impl ReceiveStatusErrorClear {pub fn set_from_data (&mut self, data:Data ) {@@ -138,14 +146,7 @@ pub fn reset(&mut self) { } } -impl Default for ReceiveStatusErrorClear { -fn default () ->Self { -0 .into () - } -} - -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 , default = false )] /// Flag Register, `UARTFR` /// /// This has the usual inbound RS232 modem-control signals, plus flags @@ -171,9 +172,10 @@ pub struct Flags { pub transmit_fifo_empty: bool,/// RI: Ring indicator pub ring_indicator: bool, - _reserved_zero_no_modify: u23, +#[bits( 23 )] + _reserved_zero_no_modify: u32, } -impl_vmstate_bitsized! (Flags ); +impl_vmstate_forward! (Flags );impl Flags {pub fn reset (&mut self) {@@ -183,16 +185,14 @@ pub fn reset(&mut self) {impl Default for Flags {fn default () ->Self { -let mut ret:Self =0 .into ();// After reset TXFF, RXFF, and BUSY are 0, and TXFE and RXFE are 1 - ret.set_receive_fifo_empty (true ); - ret.set_transmit_fifo_empty (true ); - ret +Self ::from (0 ) + .with_receive_fifo_empty (true ) + .with_transmit_fifo_empty (true ) } } -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 )] /// Line Control Register, `UARTLCR_H` #[doc(alias = "UARTLCR_H" )] pub struct LineControl {@@ -201,48 +201,46 @@ pub struct LineControl {/// PEN: Parity enable pub parity_enabled: bool,/// EPS: Even parity select +#[bits( 1 )] pub parity: Parity,/// STP2: Two stop bits select pub two_stops_bits: bool,/// FEN: Enable FIFOs +#[bits( 1 )] pub fifos_enabled: Mode,/// WLEN: Word length in bits /// b11 = 8 bits /// b10 = 7 bits /// b01 = 6 bits /// b00 = 5 bits. +#[bits( 2 )] pub word_length: WordLength,/// SPS Stick parity select pub sticky_parity: bool,/// 31:8 - Reserved, do not modify, read as zero. - _reserved_zero_no_modify: u24, +#[bits( 24 )] + _reserved_zero_no_modify: u32, } -impl_vmstate_bitsized! (LineControl ); +impl_vmstate_forward! (LineControl );impl LineControl {pub fn reset (&mut self) {// All the bits are cleared to 0 when reset. - *self =0 .into (); + *self =Self ::default (); } } -impl Default for LineControl { -fn default () ->Self { -0 .into () - } -} - -#[bitsize( 1 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] /// `EPS` "Even parity select", field of [Line Control /// register](LineControl). +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] pub enum Parity {Odd =0 ,Even =1 , } -#[bitsize( 1 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] /// `FEN` "Enable FIFOs" or Device mode, field of [Line Control /// register](LineControl). pub enum Mode {@@ -253,8 +251,8 @@ pub enum Mode { FIFO = 1, } -#[bitsize( 2 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] /// `WLEN` Word length, field of [Line Control register](LineControl). /// /// These bits indicate the number of data bits transmitted or received in a @@ -275,9 +273,8 @@ pub enum WordLength {/// The `UARTCR` register is the control register. It contains various /// enable bits, and the bits to write to set the usual outbound RS232 /// modem control signals. All bits reset to 0 except TXE and RXE. -#[bitsize( 32 )] +#[bitfield( u32 , default = false )] #[doc(alias = "UARTCR" )] -#[derive( Clone , Copy , DebugBits , FromBits )] pub struct Control {/// `UARTEN` UART enable: 0 = UART is disabled. pub enable_uart:bool ,@@ -285,9 +282,10 @@ pub struct Control {/// QEMU does not model this. pub enable_sir: bool,/// `SIRLP` SIR low-power IrDA mode. QEMU does not model this. - pub sir_lowpower_irda_mode: u1, + pub sir_lowpower_irda_mode: bool,/// Reserved, do not modify, read as zero. - _reserved_zero_no_modify: u4, +#[bits( 4 )] + _reserved_zero_no_modify: u8,/// `LBE` Loopback enable: feed UART output back to the input pub enable_loopback: bool,/// `TXE` Transmit enable @@ -309,21 +307,19 @@ pub struct Control {/// 31:16 - Reserved, do not modify, read as zero. _reserved_zero_no_modify2: u16, } -impl_vmstate_bitsized! (Control ); +impl_vmstate_forward! (Control );impl Control {pub fn reset (&mut self) { - *self =0 .into (); - self.set_enable_receive (true ); - self.set_enable_transmit (true ); + *self =Self ::default (); } }impl Default for Control {fn default () ->Self { -let mut ret:Self =0 .into (); - ret.reset (); - ret +Self ::from (0 ) + .with_enable_receive (true ) + .with_enable_transmit (true ) } } --
Quoted 2 lines
2.49.0
Perhaps it'd be simpler to contribute const-ability to upstream bilge? Is From/Into the only problem trait? I was thinking we can generate from/into associated methods for each type that are const. It'd not even be a big change and we can carry it as a patch until we can catchup with upstream crates.io version in subprojects/. WDYT?
Signature · 2 lines
Manos Pitsidianakis Emulation and Virtualization Engineer at Linaro Ltd
Re: [RFC PATCH 5/6] rust: pl011: switch from bilge to bitfield-struct
Quoted 341 lines · Manos Pitsidianakis
Quoted 339 lines · Paolo Bonzini
The bilge crate, while very nice and espressive, is heavily reliant on traits; because trait functions are never const, bilge and const mix about as well as water and oil.
Try using the bitfield-struct crate instead. It is built to support const very well and the only downside is that more manual annotations are needed (for enums and non-full-byte members). Otherwise, the use is pretty much the same and in fact device code does not change at all, only register declarations.
Recent versions want to use Rust 1.83, so this uses a slightly older version with basically no lost functionality; but anyway, I want to switch to 1.83 for QEMU as well due to improved "const" support in the compiler.
Signed-off-by Paolo Bonzini <[email protected]>--- rust/Cargo.toml | 1 + rust/hw/char/pl011/Cargo.toml | 3 +- rust/hw/char/pl011/meson.build | 11 +-- rust/hw/char/pl011/src/registers.rs | 108 ++++++++++++++-------------- 4 files changed, 56 insertions(+), 67 deletions(-)rust/Cargo.toml +1
-0@@ -97,5 +97,6 @@ used_underscore_binding = "deny" #wildcard_imports = "deny" # still have many bindings::* imports # these may have false positives +enum_variant_names = "allow" #option_if_let_else = "deny" cognitive_complexity = "deny"rust/hw/char/pl011/Cargo.toml +1
-2@@ -16,8 +16,7 @@ rust-version.workspace = true crate-type = ["staticlib"] [dependencies] -bilge = { version = "0.2.0" } -bilge-impl = { version = "0.2.0" } +bitfield-struct = { version = "0.9" } bits = { path = "../../../bits" } qemu_api = { path = "../../../qemu-api" } qemu_api_macros = { path = "../../../qemu-api-macros" }rust/hw/char/pl011/meson.build +2
-9@@ -1,17 +1,10 @@ -subproject('bilge-0.2-rs', required: true) -subproject('bilge-impl-0.2-rs', required: true) - -bilge_dep = dependency('bilge-0.2-rs') -bilge_impl_dep = dependency('bilge-impl-0.2-rs') - _libpl011_rs = static_library( 'pl011', files('src/lib.rs'), override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', dependencies: [ - bilge_dep, - bilge_impl_dep, + bitfield_struct_dep, bits_rs, qemu_api, qemu_api_macros,@@ -22,6 +15,6 @@ rust_devices_ss.add(when: 'CONFIG_X_PL011_RUST', if_true: [declare_dependency( link_whole: [_libpl011_rs], # Putting proc macro crates in `dependencies` is necessary for Meson to find # them when compiling the root per-target static rust lib. - dependencies: [bilge_impl_dep, qemu_api_macros], + dependencies: [bitfield_struct_dep, qemu_api_macros], variables: {'crate': 'pl011'}, )])rust/hw/char/pl011/src/registers.rs +52
-56@@ -5,12 +5,16 @@//! Device registers exposed as typed structs which are backed by arbitrary //! integer bitmaps. [`Data`], [`Control`], [`LineControl`], etc. +// rustc prefers "constant-like" enums to use upper case names, but that +// is inconsistent in its own way. +#![allow(non_upper_case_globals)] +// For more detail see the PL011 Technical Reference Manual DDI0183: // https://developer.arm.com/documentation/ddi0183/latest/ -use bilge::prelude::*; +use bitfield_struct::bitfield;use bits::bits; -use qemu_api::{impl_vmstate_bitsized, impl_vmstate_forward}; +use qemu_api::impl_vmstate_forward;/// Offset of each register from the base memory address of the device. #[doc(alias = "offset" )] @@ -78,14 +82,18 @@ pub enum RegisterOffset {/// The `UARTRSR` register is updated only when a read occurs /// from the `UARTDR` register with the same status information /// that can also be obtained by reading the `UARTDR` register -#[bitsize( 8 )] -#[derive( Clone , Copy , Default , DebugBits , FromBits )] +#[bitfield( u8 )] pub struct Errors {pub framing_error:bool ,pub parity_error:bool ,pub break_error:bool ,pub overrun_error:bool , - _reserved_unpredictable:u4 , +#[bits( 4 )] + _reserved_unpredictable:u8 , +} + +impl Errors { +pub const BREAK :Self =Errors ::new ().with_break_error (true ); }/// Data Register, `UARTDR` @@ -93,19 +101,18 @@ pub struct Errors {/// The `UARTDR` register is the data register; write for TX and /// read for RX. It is a 12-bit register, where bits 7..0 are the /// character and bits 11..8 are error bits. -#[bitsize( 32 )] -#[derive( Clone , Copy , Default , DebugBits , FromBits )] +#[bitfield( u32 )] #[doc(alias = "UARTDR" )] pub struct Data {pub data:u8 , +#[bits( 8 )] pub errors:Errors , _reserved:u16 , } -impl_vmstate_bitsized! (Data ); +impl_vmstate_forward! (Data );impl Data { -// bilge is not very const-friendly, unfortunately -pub const BREAK :Self =Self { value:1 <<10 }; +pub const BREAK :Self =Self ::new ().with_errors (Errors ::BREAK ); }/// Receive Status Register / Error Clear Register, `UARTRSR/UARTECR` @@ -119,13 +126,14 @@ impl Data {/// and UARTECR for writes, but really it's a single error status /// register where writing anything to the register clears the error /// bits. -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 )] pub struct ReceiveStatusErrorClear { +#[bits( 8 )] pub errors:Errors , - _reserved_unpredictable:u24 , +#[bits( 24 )] + _reserved_unpredictable:u32 , } -impl_vmstate_bitsized! (ReceiveStatusErrorClear ); +impl_vmstate_forward! (ReceiveStatusErrorClear );impl ReceiveStatusErrorClear {pub fn set_from_data (&mut self, data:Data ) {@@ -138,14 +146,7 @@ pub fn reset(&mut self) { } } -impl Default for ReceiveStatusErrorClear { -fn default () ->Self { -0 .into () - } -} - -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 , default = false )] /// Flag Register, `UARTFR` /// /// This has the usual inbound RS232 modem-control signals, plus flags @@ -171,9 +172,10 @@ pub struct Flags { pub transmit_fifo_empty: bool,/// RI: Ring indicator pub ring_indicator: bool, - _reserved_zero_no_modify: u23, +#[bits( 23 )] + _reserved_zero_no_modify: u32, } -impl_vmstate_bitsized! (Flags ); +impl_vmstate_forward! (Flags );impl Flags {pub fn reset (&mut self) {@@ -183,16 +185,14 @@ pub fn reset(&mut self) {impl Default for Flags {fn default () ->Self { -let mut ret:Self =0 .into ();// After reset TXFF, RXFF, and BUSY are 0, and TXFE and RXFE are 1 - ret.set_receive_fifo_empty (true ); - ret.set_transmit_fifo_empty (true ); - ret +Self ::from (0 ) + .with_receive_fifo_empty (true ) + .with_transmit_fifo_empty (true ) } } -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 )] /// Line Control Register, `UARTLCR_H` #[doc(alias = "UARTLCR_H" )] pub struct LineControl {@@ -201,48 +201,46 @@ pub struct LineControl {/// PEN: Parity enable pub parity_enabled: bool,/// EPS: Even parity select +#[bits( 1 )] pub parity: Parity,/// STP2: Two stop bits select pub two_stops_bits: bool,/// FEN: Enable FIFOs +#[bits( 1 )] pub fifos_enabled: Mode,/// WLEN: Word length in bits /// b11 = 8 bits /// b10 = 7 bits /// b01 = 6 bits /// b00 = 5 bits. +#[bits( 2 )] pub word_length: WordLength,/// SPS Stick parity select pub sticky_parity: bool,/// 31:8 - Reserved, do not modify, read as zero. - _reserved_zero_no_modify: u24, +#[bits( 24 )] + _reserved_zero_no_modify: u32, } -impl_vmstate_bitsized! (LineControl ); +impl_vmstate_forward! (LineControl );impl LineControl {pub fn reset (&mut self) {// All the bits are cleared to 0 when reset. - *self =0 .into (); + *self =Self ::default (); } } -impl Default for LineControl { -fn default () ->Self { -0 .into () - } -} - -#[bitsize( 1 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] /// `EPS` "Even parity select", field of [Line Control /// register](LineControl). +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] pub enum Parity {Odd =0 ,Even =1 , } -#[bitsize( 1 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] /// `FEN` "Enable FIFOs" or Device mode, field of [Line Control /// register](LineControl). pub enum Mode {@@ -253,8 +251,8 @@ pub enum Mode { FIFO = 1, } -#[bitsize( 2 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] /// `WLEN` Word length, field of [Line Control register](LineControl). /// /// These bits indicate the number of data bits transmitted or received in a @@ -275,9 +273,8 @@ pub enum WordLength {/// The `UARTCR` register is the control register. It contains various /// enable bits, and the bits to write to set the usual outbound RS232 /// modem control signals. All bits reset to 0 except TXE and RXE. -#[bitsize( 32 )] +#[bitfield( u32 , default = false )] #[doc(alias = "UARTCR" )] -#[derive( Clone , Copy , DebugBits , FromBits )] pub struct Control {/// `UARTEN` UART enable: 0 = UART is disabled. pub enable_uart:bool ,@@ -285,9 +282,10 @@ pub struct Control {/// QEMU does not model this. pub enable_sir: bool,/// `SIRLP` SIR low-power IrDA mode. QEMU does not model this. - pub sir_lowpower_irda_mode: u1, + pub sir_lowpower_irda_mode: bool,/// Reserved, do not modify, read as zero. - _reserved_zero_no_modify: u4, +#[bits( 4 )] + _reserved_zero_no_modify: u8,/// `LBE` Loopback enable: feed UART output back to the input pub enable_loopback: bool,/// `TXE` Transmit enable @@ -309,21 +307,19 @@ pub struct Control {/// 31:16 - Reserved, do not modify, read as zero. _reserved_zero_no_modify2: u16, } -impl_vmstate_bitsized! (Control ); +impl_vmstate_forward! (Control );impl Control {pub fn reset (&mut self) { - *self =0 .into (); - self.set_enable_receive (true ); - self.set_enable_transmit (true ); + *self =Self ::default (); } }impl Default for Control {fn default () ->Self { -let mut ret:Self =0 .into (); - ret.reset (); - ret +Self ::from (0 ) + .with_enable_receive (true ) + .with_enable_transmit (true ) } }
Quoted 9 lines
Quoted 3 lines
--
2.49.0Perhaps it'd be simpler to contribute const-ability to upstream bilge? Is From/Into the only problem trait? I was thinking we can generate from/into associated methods for each type that are const. It'd not even be a big change and we can carry it as a patch until we can catchup with upstream crates.io version in subprojects/. WDYT?
I see that https://github.com/danlehmann/arbitrary-int for example says almost everything can be used in const context. If we just skip using traits for generated register types and use associated const methods (and even implement From/Into that call these internally) maybe it's not really a problem anymore.
Signature · 2 lines
Manos Pitsidianakis Emulation and Virtualization Engineer at Linaro Ltd
Re: [RFC PATCH 5/6] rust: pl011: switch from bilge to bitfield-struct
In reply to Manos PitsidianakisIl mer 21 mag 2025, 11:22 Manos Pitsidianakis <
[email protected]> ha scritto:
Quoted 2 lines
Perhaps it'd be simpler to contribute const-ability to upstream bilge? Is From/Into the only problem trait?
Probably.
I was thinking we can generate
Quoted 1 line
from/into associated methods for each type that are const
Yes, that's what bitfield-struct does too.
. It'd not
Quoted 3 lines
even be a big change and we can carry it as a patch until we can catchup with upstream crates.io version in subprojects/. WDYT?
Sure, I have no idea how hard it is but I think they'd accept it? The bitflags/bits part is more important; I put the two together just for convenience, as a "let's define our standard toolbox" kind of series, because they conflict. But it's possible to merge just the first part.
Paolo
Quoted 5 lines
--
Manos Pitsidianakis
Emulation and Virtualization Engineer at Linaro Ltd
Re: [RFC PATCH 5/6] rust: pl011: switch from bilge to bitfield-struct
In reply to Paolo BonziniQuoted 106 lines · Paolo Bonzini
The bilge crate, while very nice and espressive, is heavily reliant on traits; because trait functions are never const, bilge and const mix about as well as water and oil.
Try using the bitfield-struct crate instead. It is built to support const very well and the only downside is that more manual annotations are needed (for enums and non-full-byte members). Otherwise, the use is pretty much the same and in fact device code does not change at all, only register declarations.
Recent versions want to use Rust 1.83, so this uses a slightly older version with basically no lost functionality; but anyway, I want to switch to 1.83 for QEMU as well due to improved "const" support in the compiler.
Signed-off-by Paolo Bonzini <[email protected]>--- rust/Cargo.toml | 1 + rust/hw/char/pl011/Cargo.toml | 3 +- rust/hw/char/pl011/meson.build | 11 +-- rust/hw/char/pl011/src/registers.rs | 108 ++++++++++++++-------------- 4 files changed, 56 insertions(+), 67 deletions(-)rust/Cargo.toml +1
-0@@ -97,5 +97,6 @@ used_underscore_binding = "deny" #wildcard_imports = "deny" # still have many bindings::* imports # these may have false positives +enum_variant_names = "allow" #option_if_let_else = "deny" cognitive_complexity = "deny"rust/hw/char/pl011/Cargo.toml +1
-2@@ -16,8 +16,7 @@ rust-version.workspace = true crate-type = ["staticlib"] [dependencies] -bilge = { version = "0.2.0" } -bilge-impl = { version = "0.2.0" } +bitfield-struct = { version = "0.9" } bits = { path = "../../../bits" } qemu_api = { path = "../../../qemu-api" } qemu_api_macros = { path = "../../../qemu-api-macros" }rust/hw/char/pl011/meson.build +2
-9@@ -1,17 +1,10 @@ -subproject('bilge-0.2-rs', required: true) -subproject('bilge-impl-0.2-rs', required: true) - -bilge_dep = dependency('bilge-0.2-rs') -bilge_impl_dep = dependency('bilge-impl-0.2-rs') - _libpl011_rs = static_library( 'pl011', files('src/lib.rs'), override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', dependencies: [ - bilge_dep, - bilge_impl_dep, + bitfield_struct_dep, bits_rs, qemu_api, qemu_api_macros,@@ -22,6 +15,6 @@ rust_devices_ss.add(when: 'CONFIG_X_PL011_RUST', if_true: [declare_dependency( link_whole: [_libpl011_rs], # Putting proc macro crates in `dependencies` is necessary for Meson to find # them when compiling the root per-target static rust lib. - dependencies: [bilge_impl_dep, qemu_api_macros], + dependencies: [bitfield_struct_dep, qemu_api_macros], variables: {'crate': 'pl011'}, )])rust/hw/char/pl011/src/registers.rs +6
-3@@ -5,12 +5,16 @@//! Device registers exposed as typed structs which are backed by arbitrary //! integer bitmaps. [`Data`], [`Control`], [`LineControl`], etc. +// rustc prefers "constant-like" enums to use upper case names, but that +// is inconsistent in its own way. +#![allow(non_upper_case_globals)] +// For more detail see the PL011 Technical Reference Manual DDI0183: // https://developer.arm.com/documentation/ddi0183/latest/ -use bilge::prelude::*; +use bitfield_struct::bitfield;use bits::bits; -use qemu_api::{impl_vmstate_bitsized, impl_vmstate_forward}; +use qemu_api::impl_vmstate_forward;/// Offset of each register from the base memory address of the device. #[doc(alias = "offset" )] @@ -78,14 +82,18 @@ pub enum RegisterOffset {/// The `UARTRSR` register is updated only when a read occurs /// from the `UARTDR` register with the same status information /// that can also be obtained by reading the `UARTDR` register -#[bitsize( 8 )]
rust/hw/char/pl011/src/registers.rs pub enum RegisterOffset { · line 92
Quoted 10 lines
rust/hw/char/pl011/src/registers.rs +3
-2-#[derive( Clone , Copy , Default , DebugBits , FromBits )] +#[bitfield( u8 )] pub struct Errors {pub framing_error:bool ,pub parity_error:bool ,pub break_error:bool ,pub overrun_error:bool , - _reserved_unpredictable:u4 , +#[bits( 4 )] + _reserved_unpredictable:u8 ,
This does come off as a little janky - effectively casting the u8 to only cover 4 bits. Is this not something we can derive from the type? I see lower down...
Quoted 9 lines
rust/hw/char/pl011/src/registers.rs +4
-0+} + +impl Errors { +pub const BREAK :Self =Errors ::new ().with_break_error (true ); }/// Data Register, `UARTDR` @@ -93,19 +101,18 @@ pub struct Errors {/// The `UARTDR` register is the data register; write for TX and
rust/hw/char/pl011/src/registers.rs pub struct Errors { · line 109
Quoted 10 lines
rust/hw/char/pl011/src/registers.rs +2
-2/// read for RX. It is a 12-bit register, where bits 7..0 are the /// character and bits 11..8 are error bits. -#[bitsize( 32 )] -#[derive( Clone , Copy , Default , DebugBits , FromBits )] +#[bitfield( u32 )] #[doc(alias = "UARTDR" )] pub struct Data {pub data:u8 , +#[bits( 8 )] pub errors:Errors ,
We should be able to derive that Errors fits into 8 bits as defined above.
rust/hw/char/pl011/src/registers.rs pub struct Errors { · line 116
Quoted 10 lines
rust/hw/char/pl011/src/registers.rs +2
-3_reserved: u16, } -impl_vmstate_bitsized! (Data ); +impl_vmstate_forward! (Data );impl Data { -// bilge is not very const-friendly, unfortunately -pub const BREAK :Self =Self { value:1 <<10 }; +pub const BREAK :Self =Self ::new ().with_errors (Errors ::BREAK ); }
I guess this flys a little over my head, is the effect only seen in the generated code?
Quoted 52 lines
rust/hw/char/pl011/src/registers.rs +9
-14/// Receive Status Register / Error Clear Register, `UARTRSR/UARTECR` @@ -119,13 +126,14 @@ impl Data {/// and UARTECR for writes, but really it's a single error status /// register where writing anything to the register clears the error /// bits. -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 )] pub struct ReceiveStatusErrorClear { +#[bits( 8 )] pub errors:Errors , - _reserved_unpredictable:u24 , +#[bits( 24 )] + _reserved_unpredictable:u32 , } -impl_vmstate_bitsized! (ReceiveStatusErrorClear ); +impl_vmstate_forward! (ReceiveStatusErrorClear );impl ReceiveStatusErrorClear {pub fn set_from_data (&mut self, data:Data ) {@@ -138,14 +146,7 @@ pub fn reset(&mut self) { } } -impl Default for ReceiveStatusErrorClear { -fn default () ->Self { -0 .into () - } -} - -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 , default = false )] /// Flag Register, `UARTFR` /// /// This has the usual inbound RS232 modem-control signals, plus flags @@ -171,9 +172,10 @@ pub struct Flags { pub transmit_fifo_empty: bool,/// RI: Ring indicator pub ring_indicator: bool, - _reserved_zero_no_modify: u23, +#[bits( 23 )] + _reserved_zero_no_modify: u32, } -impl_vmstate_bitsized! (Flags ); +impl_vmstate_forward! (Flags );impl Flags {pub fn reset (&mut self) {@@ -183,16 +185,14 @@ pub fn reset(&mut self) {
rust/hw/char/pl011/src/registers.rs pub fn reset(&mut self) { · line 191
Quoted 10 lines
rust/hw/char/pl011/src/registers.rs +3
-4impl Default for Flags {fn default () ->Self { -let mut ret:Self =0 .into ();// After reset TXFF, RXFF, and BUSY are 0, and TXFE and RXFE are 1 - ret.set_receive_fifo_empty (true ); - ret.set_transmit_fifo_empty (true ); - ret +Self ::from (0 ) + .with_receive_fifo_empty (true ) + .with_transmit_fifo_empty (true )
I guess skipping the mut is the advantage of being able to const eval.
Quoted 94 lines
rust/hw/char/pl011/src/registers.rs +15
-19} } -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 )] /// Line Control Register, `UARTLCR_H` #[doc(alias = "UARTLCR_H" )] pub struct LineControl {@@ -201,48 +201,46 @@ pub struct LineControl {/// PEN: Parity enable pub parity_enabled: bool,/// EPS: Even parity select +#[bits( 1 )] pub parity: Parity,/// STP2: Two stop bits select pub two_stops_bits: bool,/// FEN: Enable FIFOs +#[bits( 1 )] pub fifos_enabled: Mode,/// WLEN: Word length in bits /// b11 = 8 bits /// b10 = 7 bits /// b01 = 6 bits /// b00 = 5 bits. +#[bits( 2 )] pub word_length: WordLength,/// SPS Stick parity select pub sticky_parity: bool,/// 31:8 - Reserved, do not modify, read as zero. - _reserved_zero_no_modify: u24, +#[bits( 24 )] + _reserved_zero_no_modify: u32, } -impl_vmstate_bitsized! (LineControl ); +impl_vmstate_forward! (LineControl );impl LineControl {pub fn reset (&mut self) {// All the bits are cleared to 0 when reset. - *self =0 .into (); + *self =Self ::default (); } } -impl Default for LineControl { -fn default () ->Self { -0 .into () - } -} - -#[bitsize( 1 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] /// `EPS` "Even parity select", field of [Line Control /// register](LineControl). +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] pub enum Parity {Odd =0 ,Even =1 , } -#[bitsize( 1 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] /// `FEN` "Enable FIFOs" or Device mode, field of [Line Control /// register](LineControl). pub enum Mode {@@ -253,8 +251,8 @@ pub enum Mode { FIFO = 1, } -#[bitsize( 2 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] /// `WLEN` Word length, field of [Line Control register](LineControl). /// /// These bits indicate the number of data bits transmitted or received in a @@ -275,9 +273,8 @@ pub enum WordLength {/// The `UARTCR` register is the control register. It contains various /// enable bits, and the bits to write to set the usual outbound RS232 /// modem control signals. All bits reset to 0 except TXE and RXE. -#[bitsize( 32 )] +#[bitfield( u32 , default = false )] #[doc(alias = "UARTCR" )] -#[derive( Clone , Copy , DebugBits , FromBits )] pub struct Control {/// `UARTEN` UART enable: 0 = UART is disabled. pub enable_uart:bool ,@@ -285,9 +282,10 @@ pub struct Control {/// QEMU does not model this. pub enable_sir: bool,
rust/hw/char/pl011/src/registers.rs pub struct Control { · line 291
Quoted 10 lines
rust/hw/char/pl011/src/registers.rs +3
-2/// `SIRLP` SIR low-power IrDA mode. QEMU does not model this. - pub sir_lowpower_irda_mode: u1, + pub sir_lowpower_irda_mode: bool,/// Reserved, do not modify, read as zero. - _reserved_zero_no_modify: u4, +#[bits( 4 )] + _reserved_zero_no_modify: u8,/// `LBE` Loopback enable: feed UART output back to the input pub enable_loopback: bool,/// `TXE` Transmit enable
<snip>
I guess I'm not seeing a massive difference here. I guess the const eval is nice but there is cognitive dissonance having annotations not match types. It would be nice to have the best of both worlds.
For now I don't see a compelling reason to change from a standard crate (which I guess is the reason this is an RFC ;-)
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Re: [RFC PATCH 5/6] rust: pl011: switch from bilge to bitfield-struct
Quoted 115 lines · Alex Bennée
Quoted 112 lines · Paolo Bonzini
The bilge crate, while very nice and espressive, is heavily reliant on traits; because trait functions are never const, bilge and const mix about as well as water and oil.
Try using the bitfield-struct crate instead. It is built to support const very well and the only downside is that more manual annotations are needed (for enums and non-full-byte members). Otherwise, the use is pretty much the same and in fact device code does not change at all, only register declarations.
Recent versions want to use Rust 1.83, so this uses a slightly older version with basically no lost functionality; but anyway, I want to switch to 1.83 for QEMU as well due to improved "const" support in the compiler.
Signed-off-by Paolo Bonzini <[email protected]>--- rust/Cargo.toml | 1 + rust/hw/char/pl011/Cargo.toml | 3 +- rust/hw/char/pl011/meson.build | 11 +-- rust/hw/char/pl011/src/registers.rs | 108 ++++++++++++++-------------- 4 files changed, 56 insertions(+), 67 deletions(-)rust/Cargo.toml +1
-0@@ -97,5 +97,6 @@ used_underscore_binding = "deny" #wildcard_imports = "deny" # still have many bindings::* imports # these may have false positives +enum_variant_names = "allow" #option_if_let_else = "deny" cognitive_complexity = "deny"rust/hw/char/pl011/Cargo.toml +1
-2@@ -16,8 +16,7 @@ rust-version.workspace = true crate-type = ["staticlib"] [dependencies] -bilge = { version = "0.2.0" } -bilge-impl = { version = "0.2.0" } +bitfield-struct = { version = "0.9" } bits = { path = "../../../bits" } qemu_api = { path = "../../../qemu-api" } qemu_api_macros = { path = "../../../qemu-api-macros" }rust/hw/char/pl011/meson.build +2
-9@@ -1,17 +1,10 @@ -subproject('bilge-0.2-rs', required: true) -subproject('bilge-impl-0.2-rs', required: true) - -bilge_dep = dependency('bilge-0.2-rs') -bilge_impl_dep = dependency('bilge-impl-0.2-rs') - _libpl011_rs = static_library( 'pl011', files('src/lib.rs'), override_options: ['rust_std=2021', 'build.rust_std=2021'], rust_abi: 'rust', dependencies: [ - bilge_dep, - bilge_impl_dep, + bitfield_struct_dep, bits_rs, qemu_api, qemu_api_macros,@@ -22,6 +15,6 @@ rust_devices_ss.add(when: 'CONFIG_X_PL011_RUST', if_true: [declare_dependency( link_whole: [_libpl011_rs], # Putting proc macro crates in `dependencies` is necessary for Meson to find # them when compiling the root per-target static rust lib. - dependencies: [bilge_impl_dep, qemu_api_macros], + dependencies: [bitfield_struct_dep, qemu_api_macros], variables: {'crate': 'pl011'}, )])rust/hw/char/pl011/src/registers.rs +7
-4@@ -5,12 +5,16 @@//! Device registers exposed as typed structs which are backed by arbitrary //! integer bitmaps. [`Data`], [`Control`], [`LineControl`], etc. +// rustc prefers "constant-like" enums to use upper case names, but that +// is inconsistent in its own way. +#![allow(non_upper_case_globals)] +// For more detail see the PL011 Technical Reference Manual DDI0183: // https://developer.arm.com/documentation/ddi0183/latest/ -use bilge::prelude::*; +use bitfield_struct::bitfield;use bits::bits; -use qemu_api::{impl_vmstate_bitsized, impl_vmstate_forward}; +use qemu_api::impl_vmstate_forward;/// Offset of each register from the base memory address of the device. #[doc(alias = "offset" )] @@ -78,14 +82,18 @@ pub enum RegisterOffset {/// The `UARTRSR` register is updated only when a read occurs /// from the `UARTDR` register with the same status information /// that can also be obtained by reading the `UARTDR` register -#[bitsize( 8 )] -#[derive( Clone , Copy , Default , DebugBits , FromBits )] +#[bitfield( u8 )] pub struct Errors {pub framing_error:bool ,pub parity_error:bool ,pub break_error:bool ,
Quoted 8 lines
rust/hw/char/pl011/src/registers.rs pub enum RegisterOffset { · line 92
Quoted 4 lines
rust/hw/char/pl011/src/registers.rs +2
-1pub overrun_error: bool, - _reserved_unpredictable: u4, +#[bits( 4 )] + _reserved_unpredictable: u8,This does come off as a little janky - effectively casting the u8 to only cover 4 bits. Is this not something we can derive from the type? I see lower down...
Also, I wonder, does bitfield_struct also use 1 bit to represent bool?
Quoted 28 lines
Quoted 10 lines
rust/hw/char/pl011/src/registers.rs +4
-0+} + +impl Errors { +pub const BREAK :Self =Errors ::new ().with_break_error (true ); }/// Data Register, `UARTDR` @@ -93,19 +101,18 @@ pub struct Errors {/// The `UARTDR` register is the data register; write for TX and rust/hw/char/pl011/src/registers.rs pub struct Errors { · line 109
Quoted 10 lines
rust/hw/char/pl011/src/registers.rs +2
-2/// read for RX. It is a 12-bit register, where bits 7..0 are the /// character and bits 11..8 are error bits. -#[bitsize( 32 )] -#[derive( Clone , Copy , Default , DebugBits , FromBits )] +#[bitfield( u32 )] #[doc(alias = "UARTDR" )] pub struct Data {pub data:u8 , +#[bits( 8 )] pub errors:Errors ,We should be able to derive that Errors fits into 8 bits as defined above.
Quoted 5 lines
rust/hw/char/pl011/src/registers.rs +1
-1_reserved: u16, } -impl_vmstate_bitsized! (Data ); +impl_vmstate_forward! (Data );
Quoted 8 lines
rust/hw/char/pl011/src/registers.rs pub struct Errors { · line 116
Quoted 5 lines
rust/hw/char/pl011/src/registers.rs +1
-2impl Data { -// bilge is not very const-friendly, unfortunately -pub const BREAK :Self =Self { value:1 <<10 }; +pub const BREAK :Self =Self ::new ().with_errors (Errors ::BREAK ); }I guess this flys a little over my head, is the effect only seen in the generated code?
Because these functions are const, they can be evaluated at compile time, so this would be replaced with a constant value when compiled.
Quoted 53 lines
Quoted 53 lines
rust/hw/char/pl011/src/registers.rs +9
-14/// Receive Status Register / Error Clear Register, `UARTRSR/UARTECR` @@ -119,13 +126,14 @@ impl Data {/// and UARTECR for writes, but really it's a single error status /// register where writing anything to the register clears the error /// bits. -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 )] pub struct ReceiveStatusErrorClear { +#[bits( 8 )] pub errors:Errors , - _reserved_unpredictable:u24 , +#[bits( 24 )] + _reserved_unpredictable:u32 , } -impl_vmstate_bitsized! (ReceiveStatusErrorClear ); +impl_vmstate_forward! (ReceiveStatusErrorClear );impl ReceiveStatusErrorClear {pub fn set_from_data (&mut self, data:Data ) {@@ -138,14 +146,7 @@ pub fn reset(&mut self) { } } -impl Default for ReceiveStatusErrorClear { -fn default () ->Self { -0 .into () - } -} - -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 , default = false )] /// Flag Register, `UARTFR` /// /// This has the usual inbound RS232 modem-control signals, plus flags @@ -171,9 +172,10 @@ pub struct Flags { pub transmit_fifo_empty: bool,/// RI: Ring indicator pub ring_indicator: bool, - _reserved_zero_no_modify: u23, +#[bits( 23 )] + _reserved_zero_no_modify: u32, } -impl_vmstate_bitsized! (Flags ); +impl_vmstate_forward! (Flags );impl Flags {pub fn reset (&mut self) {@@ -183,16 +185,14 @@ pub fn reset(&mut self) {
Quoted 12 lines
rust/hw/char/pl011/src/registers.rs pub fn reset(&mut self) { · line 191
Quoted 10 lines
rust/hw/char/pl011/src/registers.rs +3
-4impl Default for Flags {fn default () ->Self { -let mut ret:Self =0 .into ();// After reset TXFF, RXFF, and BUSY are 0, and TXFE and RXFE are 1 - ret.set_receive_fifo_empty (true ); - ret.set_transmit_fifo_empty (true ); - ret +Self ::from (0 ) + .with_receive_fifo_empty (true ) + .with_transmit_fifo_empty (true )I guess skipping the mut is the advantage of being able to const eval.
No you can actually have mut in const-eval. What you can't have is heap allocations and calling non-const functions, and some other things. But in this case it doesn't matter, because this is the Default trait and it's not const, because it's a trait method.
Quoted 117 lines
Quoted 95 lines
rust/hw/char/pl011/src/registers.rs +15
-19} } -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 )] /// Line Control Register, `UARTLCR_H` #[doc(alias = "UARTLCR_H" )] pub struct LineControl {@@ -201,48 +201,46 @@ pub struct LineControl {/// PEN: Parity enable pub parity_enabled: bool,/// EPS: Even parity select +#[bits( 1 )] pub parity: Parity,/// STP2: Two stop bits select pub two_stops_bits: bool,/// FEN: Enable FIFOs +#[bits( 1 )] pub fifos_enabled: Mode,/// WLEN: Word length in bits /// b11 = 8 bits /// b10 = 7 bits /// b01 = 6 bits /// b00 = 5 bits. +#[bits( 2 )] pub word_length: WordLength,/// SPS Stick parity select pub sticky_parity: bool,/// 31:8 - Reserved, do not modify, read as zero. - _reserved_zero_no_modify: u24, +#[bits( 24 )] + _reserved_zero_no_modify: u32, } -impl_vmstate_bitsized! (LineControl ); +impl_vmstate_forward! (LineControl );impl LineControl {pub fn reset (&mut self) {// All the bits are cleared to 0 when reset. - *self =0 .into (); + *self =Self ::default (); } } -impl Default for LineControl { -fn default () ->Self { -0 .into () - } -} - -#[bitsize( 1 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] /// `EPS` "Even parity select", field of [Line Control /// register](LineControl). +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] pub enum Parity {Odd =0 ,Even =1 , } -#[bitsize( 1 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] /// `FEN` "Enable FIFOs" or Device mode, field of [Line Control /// register](LineControl). pub enum Mode {@@ -253,8 +251,8 @@ pub enum Mode { FIFO = 1, } -#[bitsize( 2 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] /// `WLEN` Word length, field of [Line Control register](LineControl). /// /// These bits indicate the number of data bits transmitted or received in a @@ -275,9 +273,8 @@ pub enum WordLength {/// The `UARTCR` register is the control register. It contains various /// enable bits, and the bits to write to set the usual outbound RS232 /// modem control signals. All bits reset to 0 except TXE and RXE. -#[bitsize( 32 )] +#[bitfield( u32 , default = false )] #[doc(alias = "UARTCR" )] -#[derive( Clone , Copy , DebugBits , FromBits )] pub struct Control {/// `UARTEN` UART enable: 0 = UART is disabled. pub enable_uart:bool ,@@ -285,9 +282,10 @@ pub struct Control {/// QEMU does not model this. pub enable_sir: bool,rust/hw/char/pl011/src/registers.rs pub struct Control { · line 291
Quoted 10 lines
rust/hw/char/pl011/src/registers.rs +3
-2/// `SIRLP` SIR low-power IrDA mode. QEMU does not model this. - pub sir_lowpower_irda_mode: u1, + pub sir_lowpower_irda_mode: bool,/// Reserved, do not modify, read as zero. - _reserved_zero_no_modify: u4, +#[bits( 4 )] + _reserved_zero_no_modify: u8,/// `LBE` Loopback enable: feed UART output back to the input pub enable_loopback: bool,/// `TXE` Transmit enable <snip>
I guess I'm not seeing a massive difference here. I guess the const eval is nice but there is cognitive dissonance having annotations not match types. It would be nice to have the best of both worlds.
For now I don't see a compelling reason to change from a standard crate (which I guess is the reason this is an RFC ;-)
--
Alex Bennée
Virtualisation Tech Lead @ Linaro
Signature · 2 lines
Manos Pitsidianakis Emulation and Virtualization Engineer at Linaro Ltd
Re: [RFC PATCH 5/6] rust: pl011: switch from bilge to bitfield-struct
Il mer 21 mag 2025, 13:12 Manos Pitsidianakis <
[email protected]> ha scritto:
Quoted 11 lines
On Wed, May 21, 2025 at 12:50 PM Alex
Quoted 7 lines
rust/hw/char/pl011/src/registers.rs pub enum RegisterOffset { · line 92
Quoted 3 lines
rust/hw/char/pl011/src/registers.rs +2
-1- _reserved_unpredictable: u4, +#[bits( 4 )] + _reserved_unpredictable: u8,This does come off as a little janky - effectively casting the u8 to only cover 4 bits. Is this not something we can derive from the type? I see lower down...
Also, I wonder, does
bitfield_structalso use 1 bit to represent bool?
Yes.
Also, to answer Alex, note that u4 is not a standard Rust type, it comes from the arbitrary-int crate.
Paolo
Quoted 238 lines
Quoted 8 lines
Quoted 8 lines
rust/hw/char/pl011/src/registers.rs +4
-0+} + +impl Errors { +pub const BREAK :Self =Errors ::new ().with_break_error (true ); }Quoted 15 lines
rust/hw/char/pl011/src/registers.rs pub struct Errors { · line 109
Quoted 13 lines
/// Data Register,
UARTDRrust/hw/char/pl011/src/registers.rs +2
-2@@ -93,19 +101,18 @@ pub struct Errors {/// The `UARTDR` register is the data register; write for TX and /// read for RX. It is a 12-bit register, where bits 7..0 are the /// character and bits 11..8 are error bits. -#[bitsize( 32 )] -#[derive( Clone , Copy , Default , DebugBits , FromBits )] +#[bitfield( u32 )] #[doc(alias = "UARTDR" )] pub struct Data {pub data:u8 , +#[bits( 8 )] pub errors:Errors ,We should be able to derive that Errors fits into 8 bits as defined
above.
Quoted 6 lines
Quoted 6 lines
rust/hw/char/pl011/src/registers.rs +1
-1_reserved: u16, } -impl_vmstate_bitsized! (Data ); +impl_vmstate_forward! (Data );Quoted 8 lines
rust/hw/char/pl011/src/registers.rs pub struct Errors { · line 116
Quoted 5 lines
rust/hw/char/pl011/src/registers.rs +1
-2impl Data { -// bilge is not very const-friendly, unfortunately -pub const BREAK :Self =Self { value:1 <<10 }; +pub const BREAK :Self =Self ::new ().with_errors (Errors ::BREAK ); }I guess this flys a little over my head, is the effect only seen in the generated code?
Because these functions are const, they can be evaluated at compile time, so this would be replaced with a constant value when compiled.
Quoted 49 lines
Quoted 49 lines
rust/hw/char/pl011/src/registers.rs +9
-14/// Receive Status Register / Error Clear Register, `UARTRSR/UARTECR` @@ -119,13 +126,14 @@ impl Data {/// and UARTECR for writes, but really it's a single error status /// register where writing anything to the register clears the error /// bits. -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 )] pub struct ReceiveStatusErrorClear { +#[bits( 8 )] pub errors:Errors , - _reserved_unpredictable:u24 , +#[bits( 24 )] + _reserved_unpredictable:u32 , } -impl_vmstate_bitsized! (ReceiveStatusErrorClear ); +impl_vmstate_forward! (ReceiveStatusErrorClear );impl ReceiveStatusErrorClear {pub fn set_from_data (&mut self, data:Data ) {@@ -138,14 +146,7 @@ pub fn reset(&mut self) { } } -impl Default for ReceiveStatusErrorClear { -fn default () ->Self { -0 .into () - } -} - -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 , default = false )] /// Flag Register, `UARTFR` /// /// This has the usual inbound RS232 modem-control signals, plus flags @@ -171,9 +172,10 @@ pub struct Flags { pub transmit_fifo_empty: bool,/// RI: Ring indicator pub ring_indicator: bool, - _reserved_zero_no_modify: u23, +#[bits( 23 )] + _reserved_zero_no_modify: u32, } -impl_vmstate_bitsized! (Flags ); +impl_vmstate_forward! (Flags );Quoted 8 lines
rust/hw/char/pl011/src/registers.rs pub fn reset(&mut self) { · line 188
Quoted 8 lines
rust/hw/char/pl011/src/registers.rs +0
-1impl Flags {pub fn reset (&mut self) {@@ -183,16 +185,14 @@ pub fn reset(&mut self) {impl Default for Flags {fn default () ->Self { -let mut ret:Self =0 .into ();// After reset TXFF, RXFF, and BUSY are 0, and TXFE and RXFE are 1
Quoted 8 lines
rust/hw/char/pl011/src/registers.rs pub fn reset(&mut self) { · line 191
Quoted 6 lines
rust/hw/char/pl011/src/registers.rs +3
-3- ret.set_receive_fifo_empty (true ); - ret.set_transmit_fifo_empty (true ); - ret +Self ::from (0 ) + .with_receive_fifo_empty (true ) + .with_transmit_fifo_empty (true )I guess skipping the mut is the advantage of being able to const eval.
No you can actually have mut in const-eval. What you can't have is heap allocations and calling non-const functions, and some other things. But in this case it doesn't matter, because this is the Default trait and it's not const, because it's a trait method.
Quoted 74 lines
Quoted 74 lines
rust/hw/char/pl011/src/registers.rs +12
-15} } -#[bitsize( 32 )] -#[derive( Clone , Copy , DebugBits , FromBits )] +#[bitfield( u32 )] /// Line Control Register, `UARTLCR_H` #[doc(alias = "UARTLCR_H" )] pub struct LineControl {@@ -201,48 +201,46 @@ pub struct LineControl {/// PEN: Parity enable pub parity_enabled: bool,/// EPS: Even parity select +#[bits( 1 )] pub parity: Parity,/// STP2: Two stop bits select pub two_stops_bits: bool,/// FEN: Enable FIFOs +#[bits( 1 )] pub fifos_enabled: Mode,/// WLEN: Word length in bits /// b11 = 8 bits /// b10 = 7 bits /// b01 = 6 bits /// b00 = 5 bits. +#[bits( 2 )] pub word_length: WordLength,/// SPS Stick parity select pub sticky_parity: bool,/// 31:8 - Reserved, do not modify, read as zero. - _reserved_zero_no_modify: u24, +#[bits( 24 )] + _reserved_zero_no_modify: u32, } -impl_vmstate_bitsized! (LineControl ); +impl_vmstate_forward! (LineControl );impl LineControl {pub fn reset (&mut self) {// All the bits are cleared to 0 when reset. - *self =0 .into (); + *self =Self ::default (); } } -impl Default for LineControl { -fn default () ->Self { -0 .into () - } -} - -#[bitsize( 1 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] /// `EPS` "Even parity select", field of [Line Control /// register](LineControl). +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] pub enum Parity {Odd =0 ,Even =1 , } -#[bitsize( 1 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] /// `FEN` "Enable FIFOs" or Device mode, field of [Line Control /// register](LineControl). pub enum Mode {@@ -253,8 +251,8 @@ pub enum Mode { FIFO = 1, }Quoted 7 lines
rust/hw/char/pl011/src/registers.rs pub enum Mode {
Quoted 7 lines
rust/hw/char/pl011/src/registers.rs +2
-2-#[bitsize( 2 )] -#[derive( Clone , Copy , Debug , Eq , FromBits , PartialEq )] +#[repr( u8 )] +#[derive( Clone , Copy , Debug , Eq , PartialEq , qemu_api_macros:: TryInto )] /// `WLEN` Word length, field of [Line Control register](LineControl). /// /// These bits indicate the number of data bits transmitted or received in a
Quoted 30 lines
Quoted 14 lines
rust/hw/char/pl011/src/registers.rs +1
-2@@ -275,9 +273,8 @@ pub enum WordLength {/// The `UARTCR` register is the control register. It contains various /// enable bits, and the bits to write to set the usual outbound RS232 /// modem control signals. All bits reset to 0 except TXE and RXE. -#[bitsize( 32 )] +#[bitfield( u32 , default = false )] #[doc(alias = "UARTCR" )] -#[derive( Clone , Copy , DebugBits , FromBits )] pub struct Control {/// `UARTEN` UART enable: 0 = UART is disabled. pub enable_uart:bool ,@@ -285,9 +282,10 @@ pub struct Control {/// QEMU does not model this. pub enable_sir: bool,rust/hw/char/pl011/src/registers.rs pub struct Control { · line 291
Quoted 10 lines
rust/hw/char/pl011/src/registers.rs +3
-2/// `SIRLP` SIR low-power IrDA mode. QEMU does not model this. - pub sir_lowpower_irda_mode: u1, + pub sir_lowpower_irda_mode: bool,/// Reserved, do not modify, read as zero. - _reserved_zero_no_modify: u4, +#[bits( 4 )] + _reserved_zero_no_modify: u8,/// `LBE` Loopback enable: feed UART output back to the input pub enable_loopback: bool,/// `TXE` Transmit enable <snip>
I guess I'm not seeing a massive difference here. I guess the const eval is nice but there is cognitive dissonance having annotations not match types. It would be nice to have the best of both worlds.
Quoted 6 lines
For now I don't see a compelling reason to change from a standard crate (which I guess is the reason this is an RFC ;-)
--
Alex Bennée
Virtualisation Tech Lead @ Linaro--
Manos Pitsidianakis
Emulation and Virtualization Engineer at Linaro Ltd
RFC PATCH 6/6 rust: remove bilge crate
In reply to Paolo Bonzini--- rust/Cargo.lock | 71 ++------------------- rust/qemu-api/src/vmstate.rs | 34 ++-------- subprojects/.gitignore | 7 -- subprojects/arbitrary-int-1-rs.wrap | 10 --- subprojects/bilge-0.2-rs.wrap | 10 --- subprojects/bilge-impl-0.2-rs.wrap | 10 --- subprojects/either-1-rs.wrap | 10 --- subprojects/itertools-0.11-rs.wrap | 10 --- subprojects/proc-macro-error-1-rs.wrap | 10 --- subprojects/proc-macro-error-attr-1-rs.wrap | 10 --- 10 files changed, 10 insertions(+), 172 deletions(-) delete mode 100644 subprojects/arbitrary-int-1-rs.wrap delete mode 100644 subprojects/bilge-0.2-rs.wrap delete mode 100644 subprojects/bilge-impl-0.2-rs.wrap delete mode 100644 subprojects/either-1-rs.wrap delete mode 100644 subprojects/itertools-0.11-rs.wrap delete mode 100644 subprojects/proc-macro-error-1-rs.wrap delete mode 100644 subprojects/proc-macro-error-attr-1-rs.wrap
rust/Cargo.lock +4 -67
@@ -3,29 +3,11 @@ version = 3 [[package]] -name = "arbitrary-int" -version = "1.2.7" +name = "bitfield-struct" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c84fc003e338a6f69fbd4f7fe9f92b535ff13e9af8997f3b14b6ddff8b1df46d" - -[[package]] -name = "bilge" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc707ed8ebf81de5cd6c7f48f54b4c8621760926cdf35a57000747c512e67b57" +checksum = "b2869c63ccf4f8bf0d485070b880e60e097fb7aeea80ee82a0a94a957e372a0b" dependencies = [ - "arbitrary-int", - "bilge-impl", -] - -[[package]] -name = "bilge-impl" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "feb11e002038ad243af39c2068c8a72bcf147acf05025dcdb916fcc000adb2d8" -dependencies = [ - "itertools", - "proc-macro-error", "proc-macro2", "quote", "syn",
@@ -38,12 +20,6 @@ dependencies = [ "qemu_api_macros", ] -[[package]] -name = "either" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" - [[package]] name = "hpet" version = "0.1.0"
@@ -52,15 +28,6 @@ dependencies = [ "qemu_api_macros", ] -[[package]] -name = "itertools" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" -dependencies = [ - "either", -] - [[package]] name = "libc" version = "0.2.162"
@@ -71,36 +38,12 @@ checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" name = "pl011" version = "0.1.0" dependencies = [ - "bilge", - "bilge-impl", + "bitfield-struct", "bits", "qemu_api", "qemu_api_macros", ] -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro2" version = "1.0.84"
@@ -152,9 +95,3 @@ name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" - -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
rust/qemu-api/src/vmstate.rs +6 -28
@@ -11,10 +11,9 @@//! migration format for a struct. This is based on the [`VMState`] trait, //! which is defined by all migrateable types. //! -//! * [`impl_vmstate_forward`](crate::impl_vmstate_forward) and -//! [`impl_vmstate_bitsized`](crate::impl_vmstate_bitsized), which help with -//! the definition of the [`VMState`] trait (respectively for transparent -//! structs and for `bilge`-defined types) +//! * [`impl_vmstate_forward`](crate::impl_vmstate_forward), which help with the +//! definition of the [`VMState`] trait (respectively for transparent structs +//! and for `bilge`-defined types) //! //! * helper macros to declare a device model state struct, in particular //! [`vmstate_subsections`](crate::vmstate_subsections) and
@@ -141,7 +140,7 @@ macro_rules! info_enum_to_ref {/// The contents of this trait go straight into structs that are parsed by C /// code and used to introspect into other structs. Generally, you don't need /// to implement it except via macros that do it for you, such as -/// `impl_vmstate_bitsized!`. +/// `impl_vmstate_forward!`. pub unsafe trait VMState {/// The `info` member of a `VMStateField` is a pointer and as such cannot /// yet be included in the [`BASE`](VMState::BASE) associated constant;
@@ -195,9 +194,8 @@ pub const fn vmstate_varray_flag<T: VMState>(_: PhantomData<T>) -> VMStateFlags/// * an array of any of the above /// /// In order to support other types, the trait `VMState` must be implemented -/// for them. The macros -/// [`impl_vmstate_bitsized!`](crate::impl_vmstate_bitsized) -/// and [`impl_vmstate_forward!`](crate::impl_vmstate_forward) help with this. +/// for them. The macro +/// [`impl_vmstate_forward!`](crate::impl_vmstate_forward) helps with this. #[macro_export] macro_rules! vmstate_of { ($struct_name:ty, $field_name:ident $([0 .. $num:ident $(* $factor:expr)?])? $(, $test_fn:expr)? $(,)?) => {
@@ -345,26 +343,6 @@ unsafe impl<$base> VMState for $type where $base: VMState $($where)* {impl_vmstate_transparent! (crate ::cell::BqlRefCell <T >where T :VMState );impl_vmstate_transparent! (crate ::cell::Opaque <T >where T :VMState ); -#[macro_export] -macro_rules! impl_vmstate_bitsized { - ($type:ty) => { -unsafe impl $crate::vmstate::VMState for $type { -const SCALAR_TYPE : $crate::vmstate::VMStateFieldType = - <<<$typeas ::bilge::prelude::Bitsized >::ArbitraryInt -as ::bilge::prelude::Number >::UnderlyingType -as $crate::vmstate::VMState >::SCALAR_TYPE ; -const BASE : $crate::bindings::VMStateField = - <<<$typeas ::bilge::prelude::Bitsized >::ArbitraryInt -as ::bilge::prelude::Number >::UnderlyingType -as $crate::vmstate::VMState >::BASE ; -const VARRAY_FLAG : $crate::bindings::VMStateFlags = - <<<$typeas ::bilge::prelude::Bitsized >::ArbitraryInt -as ::bilge::prelude::Number >::UnderlyingType -as $crate::vmstate::VMState >::VARRAY_FLAG ; - } - }; -} -// Scalar types using predefined VMStateInfos macro_rules! impl_vmstate_scalar {
subprojects/.gitignore +0 -7
@@ -6,15 +6,8 @@ /keycodemapdb /libvfio-user /slirp -/arbitrary-int-1.2.7 -/bilge-0.2.0 -/bilge-impl-0.2.0 /bitfield-struct-0.9.5 -/either-1.12.0 -/itertools-0.11.0 /libc-0.2.162 -/proc-macro-error-1.0.4 -/proc-macro-error-attr-1.0.4 /proc-macro2-1.0.84 /quote-1.0.36 /syn-2.0.66
subprojects/arbitrary-int-1-rs.wrap deleted +0 -10
@@ -1,10 +0,0 @@ -[wrap-file] -directory = arbitrary-int-1.2.7 -source_url = https://crates.io/api/v1/crates/arbitrary-int/1.2.7/download -source_filename = arbitrary-int-1.2.7.tar.gz -source_hash = c84fc003e338a6f69fbd4f7fe9f92b535ff13e9af8997f3b14b6ddff8b1df46d -#method = cargo -patch_directory = arbitrary-int-1-rs - -# bump this version number on every change to meson.build or the patches: -# v2
subprojects/bilge-0.2-rs.wrap deleted +0 -10
@@ -1,10 +0,0 @@ -[wrap-file] -directory = bilge-0.2.0 -source_url = https://crates.io/api/v1/crates/bilge/0.2.0/download -source_filename = bilge-0.2.0.tar.gz -source_hash = dc707ed8ebf81de5cd6c7f48f54b4c8621760926cdf35a57000747c512e67b57 -#method = cargo -patch_directory = bilge-0.2-rs - -# bump this version number on every change to meson.build or the patches: -# v2
subprojects/bilge-impl-0.2-rs.wrap deleted +0 -10
@@ -1,10 +0,0 @@ -[wrap-file] -directory = bilge-impl-0.2.0 -source_url = https://crates.io/api/v1/crates/bilge-impl/0.2.0/download -source_filename = bilge-impl-0.2.0.tar.gz -source_hash = feb11e002038ad243af39c2068c8a72bcf147acf05025dcdb916fcc000adb2d8 -#method = cargo -patch_directory = bilge-impl-0.2-rs - -# bump this version number on every change to meson.build or the patches: -# v2
subprojects/either-1-rs.wrap deleted +0 -10
@@ -1,10 +0,0 @@ -[wrap-file] -directory = either-1.12.0 -source_url = https://crates.io/api/v1/crates/either/1.12.0/download -source_filename = either-1.12.0.tar.gz -source_hash = 3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b -#method = cargo -patch_directory = either-1-rs - -# bump this version number on every change to meson.build or the patches: -# v2
subprojects/itertools-0.11-rs.wrap deleted +0 -10
@@ -1,10 +0,0 @@ -[wrap-file] -directory = itertools-0.11.0 -source_url = https://crates.io/api/v1/crates/itertools/0.11.0/download -source_filename = itertools-0.11.0.tar.gz -source_hash = b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57 -#method = cargo -patch_directory = itertools-0.11-rs - -# bump this version number on every change to meson.build or the patches: -# v2
subprojects/proc-macro-error-1-rs.wrap deleted +0 -10
@@ -1,10 +0,0 @@ -[wrap-file] -directory = proc-macro-error-1.0.4 -source_url = https://crates.io/api/v1/crates/proc-macro-error/1.0.4/download -source_filename = proc-macro-error-1.0.4.tar.gz -source_hash = da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c -#method = cargo -patch_directory = proc-macro-error-1-rs - -# bump this version number on every change to meson.build or the patches: -# v2
subprojects/proc-macro-error-attr-1-rs.wrap deleted +0 -10
@@ -1,10 +0,0 @@ -[wrap-file] -directory = proc-macro-error-attr-1.0.4 -source_url = https://crates.io/api/v1/crates/proc-macro-error-attr/1.0.4/download -source_filename = proc-macro-error-attr-1.0.4.tar.gz -source_hash = a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869 -#method = cargo -patch_directory = proc-macro-error-attr-1-rs - -# bump this version number on every change to meson.build or the patches: -# v2
Signature · 1 line
2.49.0