qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: Octavian Purdila <tavip@google.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [RFC PATCH 03/23] scripts: add script to generate C header files from SVD XML files
Date: Tue, 13 Aug 2024 08:43:37 +1000	[thread overview]
Message-ID: <f73d3122-28f1-42dc-afaa-453cd2cfb6b7@linaro.org> (raw)
In-Reply-To: <CAGWr4cSiNOm1mL6ZC+Dr8bFHz9Dx2R78_we9wL=W6f3FExb-kQ@mail.gmail.com>

On 8/13/24 03:56, Octavian Purdila wrote:
>>>    typedef struct {
>>>      ...
>>>      union {
>>>        uint32_t PSELID;              /* 0x00000FF8 Peripheral Select and
>>>                                       * Flexcomm module ID */
>>>        struct {
>>>          uint32_t PERSEL : 3;        /* [2..0] Peripheral Select */
>>>          uint32_t LOCK : 1;          /* [3..3] Lock the peripheral select */
>>>          uint32_t USARTPRESENT : 1;  /* [4..4] USART present indicator */
>>>          uint32_t SPIPRESENT : 1;    /* [5..5] SPI present indicator */
>>>          uint32_t I2CPRESENT : 1;    /* [6..6] I2C present indicator */
>>>          uint32_t I2SPRESENT : 1;    /* [7..7] I2S Present */
>>>          uint32_t : 4;
>>>          uint32_t ID : 20;           /* [31..12] Flexcomm ID */
>>>        } PSELID_b;
>>>      };
>>
>> Bitfield layout in C isn't portable, so don't generate this kind
>> of union-of-a-integer-and-some-bitfields, please. You can
>> generate FIELD() macro invocations (see include/hw/registerfields.h)
>> which define shift/mask/length macros that can be used with
>> FIELD_EX*/FIELD_DP* to do extract/deposit operations.
>>
> 
> I see that C bitfields are already used in a few places in qemu. Could
> you please elaborate on the portability issue?

Bitfields are fine, so long as you're only using them for storage compression and do not 
care about the underlying layout.

The moment you put them into a union with an integer, clearly you are expecting the 
bitfields to be in some particular order with respect to the integer, and that is the 
portability issue.

In particular, big-endian hosts will generally flip the order, layout starting at the most 
signifiacnt bit and work down.  Other compilers will pad bits for alignment in ways that 
you do not expect.

Just Don't Do It.


r~


  reply	other threads:[~2024-08-12 22:44 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-05 20:16 [RFC PATCH 00/23] NXP i.MX RT595, ARM SVD and device model unit tests Octavian Purdila
2024-08-05 20:16 ` [RFC PATCH 01/23] fifo32: add peek function Octavian Purdila
2024-08-05 20:16 ` [RFC PATCH 02/23] tests/unit: add fifo test Octavian Purdila
2024-08-05 20:16 ` [RFC PATCH 03/23] scripts: add script to generate C header files from SVD XML files Octavian Purdila
2024-08-08 21:56   ` John Snow
2024-08-08 22:30     ` Octavian Purdila
2024-08-08 23:06       ` John Snow
2024-08-09  9:30       ` Philippe Mathieu-Daudé
2024-08-09  9:42         ` Paolo Bonzini
2024-08-09  9:59           ` Daniel P. Berrangé
2024-08-13  8:32             ` Philippe Mathieu-Daudé
2024-08-09  6:34     ` Paolo Bonzini
2024-08-09 19:28       ` Octavian Purdila
2024-08-12 15:27   ` Peter Maydell
2024-08-12 17:56     ` Octavian Purdila
2024-08-12 22:43       ` Richard Henderson [this message]
2024-08-13 15:47         ` Octavian Purdila
2024-08-05 20:16 ` [RFC PATCH 04/23] hw/arm: add SVD file for NXP i.MX RT595 Octavian Purdila
2024-08-06 14:06   ` Alex Bennée
2024-08-06 20:31     ` Octavian Purdila
2024-08-07 11:24       ` Philippe Mathieu-Daudé
2024-08-07 16:36         ` Octavian Purdila
2024-08-09  9:13       ` Daniel P. Berrangé
2024-08-09 22:40         ` Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 05/23] hw: add register access utility functions Octavian Purdila
2024-08-12 15:32   ` Peter Maydell
2024-08-12 21:14     ` Octavian Purdila
2024-08-12 22:35       ` Richard Henderson
2024-08-13  8:28       ` Philippe Mathieu-Daudé
2024-08-05 20:17 ` [RFC PATCH 06/23] hw/misc: add basic flexcomm device model Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 07/23] tests/unit: add system bus mock Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 08/23] test/unit: add register access macros and functions Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 09/23] test/unit: add flexcomm unit test Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 10/23] hw/char: add support for flexcomm usart Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 11/23] test/unit: add flexcomm usart unit test Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 12/23] hw/i2c: add support for flexcomm i2c Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 13/23] test/unit: add i2c-tester Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 14/23] test/unit: add unit tests for flexcomm i2c Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 15/23] hw/ssi: add support for flexcomm spi Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 16/23] test/unit: add spi-tester Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 17/23] test/unit: add unit tests for flexcomm spi Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 18/23] hw/misc: add support for RT500's clock controller Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 19/23] test/unit: add unit tests " Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 20/23] hw/ssi: add support for flexspi Octavian Purdila
2024-08-08  5:11   ` Philippe Mathieu-Daudé
2024-08-08 21:31     ` Octavian Purdila
2024-08-09  8:54       ` Philippe Mathieu-Daudé
2024-08-05 20:17 ` [RFC PATCH 21/23] hw/misc: add support for RT500 reset controller Octavian Purdila
2024-08-08  5:00   ` Philippe Mathieu-Daudé
2024-08-05 20:17 ` [RFC PATCH 22/23] hw/arm: add basic support for the RT500 SoC Octavian Purdila
2024-08-06 14:51   ` Philippe Mathieu-Daudé
2024-08-07 23:57     ` Octavian Purdila
2024-08-05 20:17 ` [RFC PATCH 23/23] hw/arm: add RT595-EVK board Octavian Purdila
2024-08-12 16:10 ` [RFC PATCH 00/23] NXP i.MX RT595, ARM SVD and device model unit tests Peter Maydell
2024-08-12 16:22   ` Daniel P. Berrangé
2024-08-12 18:39     ` Octavian Purdila

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=f73d3122-28f1-42dc-afaa-453cd2cfb6b7@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=tavip@google.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).