History

bitfields: fix PCC layout

Fixes some corner cases in PCC layout. Testcases coming up.

Michael Matz committed 10 years ago

Commit ed680da1 parent bd69bce1 file · +28 −23Browse files

Unified · Split

tccgen.c+28−23
@@ -3267,6 +3267,7 @@ static void struct_add_offset (Sym *s, int offset) · 3266 unmodified lines
32673267 static void struct_layout(CType *type, AttributeDef *ad)
32683268 {
32693269 int align, maxalign, offset, c, bit_pos, bt, prevbt;
3270+ int pcc = !tcc_state->ms_bitfields;
32703271 Sym *f;
32713272 if (ad->a.aligned)
32723273 maxalign = ad->a.aligned;
@@ -3280,13 +3281,12 @@ static void struct_layout(CType *type, AttributeDef *ad) · 7 unmodified lines
32803281 int extra_bytes = 0;
32813282 int typealign, bit_size;
32823283 int size = type_size(&f->type, &typealign);
3283− int pcc = !tcc_state->ms_bitfields;
32843284 if (f->type.t & VT_BITFIELD) {
32853285 bit_size = (f->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
32863286 /* without ms-bitfields, allocate the
32873287 * minimum number of bytes necessary,
32883288 * adding single bytes as needed */
3289− if (!tcc_state->ms_bitfields) {
3289+ if (0 && !tcc_state->ms_bitfields) {
32903290 if (bit_pos == 0)
32913291 /* minimum bytes for new bitfield */
32923292 size = (bit_size + 7) / 8;
@@ -3311,55 +3311,59 @@ static void struct_layout(CType *type, AttributeDef *ad) · 18 unmodified lines
33113311 } else {
33123312 align = typealign;
33133313 }
3314− /*if (extra_bytes) c += extra_bytes;
3315− else*/ if (bit_size < 0) {
3316− int addbytes = pcc ? (bit_pos + 7) >> 3 : 0;
3314+ if (extra_bytes) c += extra_bytes;
3315+ else if (bit_size < 0) {
3316+ prevbt = VT_STRUCT;
33173317 if (type->ref->type.t == TOK_STRUCT) {
3318+ int addbytes = pcc ? (bit_pos + 7) >> 3 : 0;
33183319 c = (c + addbytes + align - 1) & -align;
33193320 offset = c;
33203321 if (size > 0)
33213322 c += size;
33223323 } else {
3324+ union_tail:
33233325 offset = 0;
3324− if (addbytes > c)
3325− c = addbytes;
33263326 if (size > c)
33273327 c = size;
33283328 }
33293329 if (align > maxalign)
33303330 maxalign = align;
33313331 bit_pos = 0;
3332− prevbt = VT_STRUCT;
3332+ } else if (type->ref->type.t != TOK_STRUCT) {
3333+ if (pcc)
3334+ size = (bit_size + 7) >> 3;
3335+ f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
3336+ | (0 << VT_STRUCT_SHIFT);
3337+ goto union_tail;
33333338 } else {
33343339 /* A bit-field. Layout is more complicated. There are two
33353340 options TCC implements: PCC compatible and MS compatible
33363341 (PCC compatible is what GCC uses for almost all targets). */
3337− if (!bit_pos) {
3338− if (type->ref->type.t == TOK_STRUCT) {
3339− /* Don't align c here. That's only to be done
3340− in certain cases. */
3341− offset = c;
3342− } else {
3343− offset = 0;
3344− }
3345− }
33463342 if (pcc) {
33473343 /* In PCC layout a non-packed bit-field is placed adjacent
33483344 to the preceding bit-fields, except if it would overflow
33493345 its container (depending on base type) or it's a zero-width
33503346 bit-field. Packed non-zero-width bit-fields always are
33513347 placed adjacent. */
3352− if ((typealign != 1 &&
3353− bit_pos + bit_size > size * 8) ||
3354− bit_size == 0) {
3348+ int ofs = (c * 8 + bit_pos) % (typealign * 8);
3349+ int ofs2 = ofs + bit_size + (typealign * 8) - 1;
3350+ /*if ((typealign != 1 &&
3351+ //bit_pos + bit_size > size * 8) ||
3352+ (((c + ((bit_pos + 7) >> 3) + typealign - 1) & -typealign)
3353+ != ((c + ((bit_pos + bit_size + 7) >> 3) + typealign - 1) & -typealign))) ||
3354+ bit_size == 0 ||
3355+ (bit_pos + bit_size > size * 8)
3356+ ) {*/
3357+ if (bit_size == 0 ||
3358+ (typealign != 1 && (ofs2 / (typealign * 8)) > ((size*8)/(typealign*8)))) {
33553359 c = (c + ((bit_pos + 7) >> 3) + typealign - 1) & -typealign;
3356− offset = c;
33573360 bit_pos = 0;
33583361 }
3362+ offset = c;
33593363 /* In PCC layout named bit-fields influence the alignment
33603364 of the containing struct using the base types alignment,
33613365 except for packed fields or zero-width fields. */
3362− if (bit_size > 0) {
3366+ if (!(f->v & SYM_FIRST_ANOM)) {
33633367 if (align > maxalign)
33643368 maxalign = align;
33653369 if (typealign > maxalign)
@@ -3444,7 +3448,8 @@ static void struct_layout(CType *type, AttributeDef *ad) · 78 unmodified lines
34443448 f->r = 0;
34453449 }
34463450 /* store size and alignment */
3447− type->ref->c = (c + ((bit_pos + 7) >> 3) + maxalign - 1) & -maxalign;
3451+ type->ref->c = (c + (pcc ? (bit_pos + 7) >> 3 : 0)
3452+ + maxalign - 1) & -maxalign;
34483453 type->ref->r = maxalign;
34493454 }
34503455

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