All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Vallejo <alejandro.vallejo@cloud.com>
To: xen-devel@lists.xenproject.org
Cc: Alejandro Vallejo <alejandro.vallejo@cloud.com>,
	Anthony PERARD <anthony.perard@vates.tech>,
	Teddy Astie <teddy.astie@vates.tech>,
	Yann Dirson <yann.dirson@vates.tech>
Subject: [RFC PATCH 09/25] tools/xenbindgen: Add support for bitmaps in TOML specs
Date: Fri, 15 Nov 2024 11:51:38 +0000	[thread overview]
Message-ID: <20241115115200.2824-10-alejandro.vallejo@cloud.com> (raw)
In-Reply-To: <20241115115200.2824-1-alejandro.vallejo@cloud.com>

Signed-off-by: Alejandro Vallejo <alejandro.vallejo@cloud.com>
---
 tools/rust/xenbindgen/src/c_lang.rs | 62 ++++++++++++++++++++++++++++-
 tools/rust/xenbindgen/src/spec.rs   | 51 ++++++++++++++++++++++--
 2 files changed, 108 insertions(+), 5 deletions(-)

diff --git a/tools/rust/xenbindgen/src/c_lang.rs b/tools/rust/xenbindgen/src/c_lang.rs
index f15feca8df91..bba310233e60 100644
--- a/tools/rust/xenbindgen/src/c_lang.rs
+++ b/tools/rust/xenbindgen/src/c_lang.rs
@@ -17,7 +17,7 @@
 
 use std::fmt::Write;
 
-use crate::spec::{EnumDef, OutFileDef, StructDef, Typ};
+use crate::spec::{BitmapDef, EnumDef, OutFileDef, StructDef, Typ};
 
 use convert_case::{Case, Casing};
 use log::{debug, error, trace};
@@ -51,6 +51,20 @@ fn structfield(filedef: &OutFileDef, typ: &Typ, name: &str) -> String {
             )
         }
         Typ::Struct(x) => format!("struct {x} {name}"),
+        Typ::Bitmap(x) => {
+            // Dealing with bitfields at the ABI boundary is a
+            // pain, so we just use the underlying type instead.
+            let Some(e) = filedef.bitmaps.iter().find(|y| *x == y.name) else {
+                error!("Can't find bitmap {x}. Typo?");
+                trace!("{filedef:#?}");
+                std::process::exit(1);
+            };
+            format!(
+                "{} /* See {} */",
+                structfield(filedef, &e.typ, name),
+                e.name
+            )
+        }
         Typ::Enum(x) => {
             // C can't use an enum as a field and fix its width. Look for its
             // underlying layout and use that type instead.
@@ -137,6 +151,48 @@ fn enumgen(out: &mut String, def: &EnumDef) {
     writeln!(out).unwrap();
 }
 
+/// Write a C-compatible enum onto `out`
+fn bitmapgen(out: &mut String, def: &BitmapDef) {
+    debug!("bitmap {}", def.name);
+
+    comment(out, &def.description, Indentation(0));
+    writeln!(out, "struct {} {{}}; /* GREP FODDER */", def.name).unwrap();
+
+    let mut mask = 0;
+    for f in &def.bits {
+        trace!("  shift {}={}", f.name, f.shift);
+
+        if (1 << f.shift) & mask != 0 {
+            error!("Bad shift({}) on {}. Shadows another bit.", f.shift, f.name);
+            std::process::exit(1);
+        }
+
+        mask |= 1 << f.shift;
+
+        comment(out, &f.description, Indentation(0));
+        writeln!(
+            out,
+            "#define {}_{} (1U{} << {})",
+            def.name.from_case(Case::Snake).to_case(Case::UpperSnake),
+            f.name.from_case(Case::Snake).to_case(Case::UpperSnake),
+            if def.typ == Typ::U64 { "LL" } else { "" },
+            f.shift
+        )
+        .unwrap();
+    }
+
+    comment(out, "Mask covering all defined bits", Indentation(0));
+    writeln!(
+        out,
+        "#define {}__ALL ({:#X}U{})",
+        def.name.from_case(Case::Snake).to_case(Case::UpperSnake),
+        mask,
+        if def.typ == Typ::U64 { "LL" } else { "" },
+    )
+    .unwrap();
+    writeln!(out).unwrap();
+}
+
 /// Generates a single `.h` file.
 ///
 /// `filedef` is a language-agnostic high level description of what the output
@@ -160,6 +216,10 @@ pub fn parse(filedef: &OutFileDef) -> String {
         enumgen(&mut out, def);
     }
 
+    for def in &filedef.bitmaps {
+        bitmapgen(&mut out, def);
+    }
+
     for def in &filedef.structs {
         structgen(&mut out, filedef, def);
     }
diff --git a/tools/rust/xenbindgen/src/spec.rs b/tools/rust/xenbindgen/src/spec.rs
index f6cfedad2150..4a9c5e7d028b 100644
--- a/tools/rust/xenbindgen/src/spec.rs
+++ b/tools/rust/xenbindgen/src/spec.rs
@@ -28,6 +28,7 @@ use log::{debug, info};
 #[derive(Debug, serde::Deserialize, PartialEq)]
 #[serde(rename_all = "lowercase", tag = "tag", content = "args")]
 pub enum Typ {
+    Bitmap(String),
     Enum(String),
     Struct(String),
     U8,
@@ -72,7 +73,7 @@ pub struct FieldDef {
 pub struct EnumDef {
     /// snake-cased name of this enumeration.
     ///
-    /// Must be converted to whatever is idiomatic in the target language.
+    /// Must be converted to idiomatic casing in the target language.
     pub name: String,
     /// Description of what the type is for.
     ///
@@ -88,11 +89,43 @@ pub struct EnumDef {
     pub variants: Vec<VariantDef>,
 }
 
-/// A lang-agnostic description of a single variant of an enumerated type.
+/// Lang-agnostic description of a bitmap type.
+#[derive(Debug, serde::Deserialize)]
+pub struct BitmapDef {
+    /// Snake-cased name of this bitmap.
+    ///
+    /// Must be converted to idiomatic casing in the target language.
+    pub name: String,
+    /// Description of what the type is for.
+    ///
+    /// Must be turned into documentation in the autogenerated file.
+    pub description: String,
+    /// Width of the type given as an equivalent primitive unsigned integer
+    /// of the same width.
+    pub typ: Typ,
+    /// List of bits in the bitmap with a described meaning. All other bits are
+    /// reserved to zero.
+    pub bits: Vec<BitDef>,
+}
+
+/// Lang-agnostic description of a single bit within a particular bitmap type.
+#[derive(Debug, serde::Deserialize)]
+pub struct BitDef {
+    /// Snake-cased name of this bit. Depending on the backend, the name
+    /// might be prefixed by the name of its type (as is commonly done in C).
+    pub name: String,
+    /// Meaning of this bit in the context of its type.
+    pub description: String,
+    /// Position of the bit in the underlying type, following a little-endian
+    /// convention.
+    pub shift: u8,
+}
+
+/// Lang-agnostic description of a single variant of an enumerated type.
 #[derive(Debug, serde::Deserialize)]
 pub struct VariantDef {
-    /// Name of this variant. Depending on the backend, the name might be
-    /// prefixed by the name of its type (as is commonly done in C).
+    /// Snake-cased name of this variant. Depending on the backend, the name
+    /// might be prefixed by the name of its type (as is commonly done in C).
     pub name: String,
     /// Meaning of this variant in the context of its type.
     pub description: String,
@@ -108,6 +141,8 @@ struct InFileDef {
     structs: Option<Vec<StructDef>>,
     /// List of lang-agnostic enumerated descriptions.
     enums: Option<Vec<EnumDef>>,
+    /// List of lang-agnostic bitmap descriptions.
+    bitmaps: Option<Vec<BitmapDef>>,
 }
 
 /// Description of an abstract output (i.e: `.rs`, `.h`, etc).
@@ -123,6 +158,10 @@ pub struct OutFileDef {
     ///
     /// Implementation is lang-specific.
     pub enums: Vec<EnumDef>,
+    /// List of bitmap descriptions.
+    ///
+    /// Implementation is lang-specific.
+    pub bitmaps: Vec<BitmapDef>,
 }
 
 impl OutFileDef {
@@ -139,6 +178,7 @@ impl OutFileDef {
             name,
             structs: Vec::new(),
             enums: Vec::new(),
+            bitmaps: Vec::new(),
         };
 
         for entry in from_ioerr(dir.read_dir())? {
@@ -152,6 +192,9 @@ impl OutFileDef {
             if let Some(enums) = filedef.enums {
                 ret.enums.extend(enums);
             }
+            if let Some(bitmaps) = filedef.bitmaps {
+                ret.bitmaps.extend(bitmaps);
+            }
         }
 
         Ok(ret)
-- 
2.47.0



  parent reply	other threads:[~2024-11-15 11:53 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-15 11:51 [RFC PATCH 00/25] Introduce xenbindgen to autogen hypercall structs Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 01/25] xen/domctl: Refine grant_opts into max_grant_version Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 02/25] xen/domctl: Replace altp2m_opts with altp2m_mode Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 03/25] tools/xenbindgen: Introduce a Xen hypercall IDL generator Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 04/25] tools/xenbindgen: Add a TOML spec reader Alejandro Vallejo
2024-11-25 15:13   ` Teddy Astie
2024-11-25 16:51     ` Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 05/25] tools/xenbindgen: Add basic plumbing for the C backend Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 06/25] tools/xenbindgen: Add xenbindgen's Cargo.lock file Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 07/25] tools/xenbindgen: Add support for structs in TOML specs Alejandro Vallejo
2024-11-25 12:39   ` Teddy Astie
2024-11-25 17:07     ` Alejandro Vallejo
2024-11-25 15:03   ` Teddy Astie
2024-11-25 17:16     ` Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 08/25] tools/xenbindgen: Add support for enums " Alejandro Vallejo
2024-11-25 16:39   ` Teddy Astie
2024-11-25 17:18     ` Alejandro Vallejo
2024-11-15 11:51 ` Alejandro Vallejo [this message]
2024-11-15 11:51 ` [RFC PATCH 10/25] tools/xenbindgen: Add support for includes in the " Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 11/25] tools/xenbindgen: Validate ABI rules at generation time Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 12/25] xen: Replace sysctl/readconsole with autogenerated version Alejandro Vallejo
2024-11-25 12:05   ` Jan Beulich
2024-11-25 18:51     ` Alejandro Vallejo
2024-11-26  9:40       ` Jan Beulich
2024-11-26 12:27         ` Alejandro Vallejo
2024-11-26 13:20           ` Jan Beulich
2024-11-26 14:36             ` Alejandro Vallejo
2024-11-26 16:30               ` Jan Beulich
2024-11-26 14:39             ` Teddy Astie
2024-11-26 16:28               ` Jan Beulich
2024-11-15 11:51 ` [RFC PATCH 13/25] xen: Replace hand-crafted altp2m_mode descriptions with autogenerated ones Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 14/25] xen: Replace common bitmaps in domctl.createdomain with autogenerated versions Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 15/25] xen/arm: Replace hand-crafted xen_arch_domainconfig with autogenerated one Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 16/25] xen/x86: " Alejandro Vallejo
2024-11-25 12:09   ` Jan Beulich
2024-11-25 18:53     ` Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 17/25] xen/ppc: Replace empty " Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 18/25] xen/riscv: " Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 19/25] xen: Replace hand-crafted domctl/createdomain with autogenerated version Alejandro Vallejo
2024-12-04 14:48   ` Teddy Astie
2024-11-15 11:51 ` [RFC PATCH 20/25] tools/xen-sys: Create a crate with autogenerated Rust constructs Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 21/25] tools/xenbindgen: Add Rust backend to xenbindgen Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 22/25] tools/xen-sys: Add autogenerated Rust files Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 23/25] licence: Add Unicode-DFS-2016 to the list of licences Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 24/25] tools/rust: Add deny.toml Alejandro Vallejo
2024-11-15 11:51 ` [RFC PATCH 25/25] ci: Add a CI checker for Rust-related helpful properties Alejandro Vallejo
2024-11-21 17:46 ` [RFC PATCH 00/25] Introduce xenbindgen to autogen hypercall structs Anthony PERARD
2024-11-22 10:52   ` Teddy Astie
2024-11-22 13:26     ` Alejandro Vallejo
2024-11-22 13:12   ` Alejandro Vallejo
2024-11-22 16:34     ` Anthony PERARD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241115115200.2824-10-alejandro.vallejo@cloud.com \
    --to=alejandro.vallejo@cloud.com \
    --cc=anthony.perard@vates.tech \
    --cc=teddy.astie@vates.tech \
    --cc=xen-devel@lists.xenproject.org \
    --cc=yann.dirson@vates.tech \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.