History

bitfields: Fix MS layout some more

Another corner case: struct foo6_1 { char x; short p:8; short :0; short :0; short p2:8; char y; }; In MS layout the second anon :0 bit-field does _not_ adjust size or alignment of the struct again. The first one does, though.

Michael Matz committed 9 years ago

Commit 23b257a1 parent ed680da1 file · +9 −4Browse files

Unified · Split

tccgen.c+9−4
@@ -3266,7 +3266,7 @@ static void struct_add_offset (Sym *s, int offset) · 3265 unmodified lines
32663266
32673267 static void struct_layout(CType *type, AttributeDef *ad)
32683268 {
3269− int align, maxalign, offset, c, bit_pos, bt, prevbt;
3269+ int align, maxalign, offset, c, bit_pos, bt, prevbt, prev_bit_size;
32703270 int pcc = !tcc_state->ms_bitfields;
32713271 Sym *f;
32723272 if (ad->a.aligned)
@@ -3277,6 +3277,7 @@ static void struct_layout(CType *type, AttributeDef *ad) · 4 unmodified lines
32773277 c = 0;
32783278 bit_pos = 0;
32793279 prevbt = VT_STRUCT; /* make it never match */
3280+ prev_bit_size = 0;
32803281 for (f = type->ref->next; f; f = f->next) {
32813282 int extra_bytes = 0;
32823283 int typealign, bit_size;
@@ -3314,6 +3315,7 @@ static void struct_layout(CType *type, AttributeDef *ad) · 31 unmodified lines
33143315 if (extra_bytes) c += extra_bytes;
33153316 else if (bit_size < 0) {
33163317 prevbt = VT_STRUCT;
3318+ prev_bit_size = 0;
33173319 if (type->ref->type.t == TOK_STRUCT) {
33183320 int addbytes = pcc ? (bit_pos + 7) >> 3 : 0;
33193321 c = (c + addbytes + align - 1) & -align;
@@ -3355,14 +3357,15 @@ static void struct_layout(CType *type, AttributeDef *ad) · 35 unmodified lines
33553357 (bit_pos + bit_size > size * 8)
33563358 ) {*/
33573359 if (bit_size == 0 ||
3358− (typealign != 1 && (ofs2 / (typealign * 8)) > ((size*8)/(typealign*8)))) {
3360+ (typealign != 1 && (ofs2 / (typealign * 8)) > (size/typealign))) {
33593361 c = (c + ((bit_pos + 7) >> 3) + typealign - 1) & -typealign;
33603362 bit_pos = 0;
33613363 }
33623364 offset = c;
33633365 /* In PCC layout named bit-fields influence the alignment
33643366 of the containing struct using the base types alignment,
3365− except for packed fields or zero-width fields. */
3367+ except for packed fields (which here have correct
3368+ align/typealign). */
33663369 if (!(f->v & SYM_FIRST_ANOM)) {
33673370 if (align > maxalign)
33683371 maxalign = align;
@@ -3381,7 +3384,8 @@ static void struct_layout(CType *type, AttributeDef *ad) · 12 unmodified lines
33813384 bit_pos = 0;
33823385 /* In MS bitfield mode a bit-field run always uses
33833386 at least as many bits as the underlying type. */
3384− c += size;
3387+ if (bit_size || prev_bit_size)
3388+ c += size;
33853389 }
33863390 if (bit_size > 0 || prevbt == bt) {
33873391 if (align > maxalign)
@@ -3390,6 +3394,7 @@ static void struct_layout(CType *type, AttributeDef *ad) · 2 unmodified lines
33903394 maxalign = typealign;
33913395 }
33923396 prevbt = bt;
3397+ prev_bit_size = bit_size;
33933398 }
33943399 f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
33953400 | (bit_pos << VT_STRUCT_SHIFT);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t