1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-07 23:29:39 +00:00

Pad bit-fields only to the next byte

Fixes #1054.

Previously, bit-fields followed by another field were aligned
to two bytes.  Bit-fields ending the struct were (and continue
to be) aligned only to a single byte.

```
struct s {
  unsigned int x : 4;
};

struct t {
  unsigned int x : 4;
  unsigned int y;
};
```

Before: `sizeof(struct s) == 1`, sizeof(struct t) == 4`
After: `sizeof(struct s) == 1` sizeof(struct t) == 3`
This commit is contained in:
Jesse Rosenstock 2020-06-21 21:28:32 +02:00 committed by Oliver Schmidt
parent 9e881a497e
commit 9858e47dfd

View File

@ -772,17 +772,19 @@ static SymEntry* ParseStructDecl (const char* Name)
*/
if (BitOffs > 0) {
if (FieldWidth <= 0 || (BitOffs + FieldWidth) > INT_BITS) {
/* Bits needed to byte-align the next field. */
unsigned PaddingBitWidth = -BitOffs % CHAR_BITS;
/* We need an anonymous name */
AnonName (Ident, "bit-field");
/* Add an anonymous bit-field that aligns to the next
** storage unit.
** byte.
*/
AddBitField (Ident, StructSize, BitOffs, INT_BITS - BitOffs);
AddBitField (Ident, StructSize, BitOffs, PaddingBitWidth);
/* No bits left */
StructSize += SIZEOF_INT;
StructSize += (BitOffs + PaddingBitWidth) / CHAR_BITS;
BitOffs = 0;
}
}