<< Back to previous view

[PCC-366] assignment to packed bitfield using wrong mask Created: 02/Jul/11  Updated: 30/Jul/11

Status: Resolved
Project: pcc
Component/s: C frontend
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major
Reporter: Iain Hibbert Assignee: Anders Magnusson
Resolution: Fixed Votes: 0
Environment: NetBSD/i386


 Description   
the following code

struct foo {
        unsigned char a:3;
        unsigned short b:14;
} __attribute__((__packed__));

void
foo(struct foo *f)
{
        f->b = 123;
}

when compiled, causes the assembler to complain

/tmp/ctm.10685a: Assembler messages:
/tmp/ctm.10685a:15: Warning: -131065 shortened to 7

this is because the bitfield assignment is trying to AND a value too large for the instruction

        .text
        .align 4
        .globl foo
        .type foo,@function
foo:
        pushl %ebp
        movl %esp,%ebp
        subl $4,%esp
.L11:
.L13:
        movl 8(%ebp),%eax
        movl %eax,-4(%ebp)
        movl -4(%ebp),%eax
        movw (%eax),%ax
        andw $-131065,%ax ## <-----
        orw $984,%ax
        movl -4(%ebp),%edx
        movw %ax,(%edx)
.L12:
        leave
        ret
        .size foo,.-foo
        .ident "PCC: pcc 1.1.0.DEVEL 20110701 for netbsd-i386"

the code is generated by trees.c after line 2420, but I'm not sure if its just because
'char' and 'short' types are not supported (should fail to compile, in that case?) or if
the wrong type is being used for the AND operation since andw is being emitted

in truth, I can't quite imagine how this structure is supposed to be laid out anyway
it seems wrong to use bit-fields and packed at the same time (this code this
example comes from is in usr.sbin/fwctl/fwdv.c on NetBSD, the structure being
defined in sys/dev/ieee1394/iec68113.h)

 Comments   
Comment by Anders Magnusson [ 02/Jul/11 06:13 PM ]
packed on bitfields avoids the space between fields of differently aligned bitfield types. I haven't check how packed bitfields are handled in the common code, it should be ensured that it works also on targets that cannot handle unaligned accesses.
Comment by Anders Magnusson [ 30/Jul/11 10:06 AM ]
All code for handling bitfields in a target independent way is written now and should work. The rest are bugs needed to be found.
Generated at Tue Sep 16 05:00:44 CEST 2014 using JIRA Enterprise Edition, Version: 3.13.1-#333.