Issue Details (XML | Word | Printable)

Key: PCC-366
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Anders Magnusson
Reporter: Iain Hibbert
Votes: 0
Watchers: 0

If you were logged in you would be able to see more operations.

assignment to packed bitfield using wrong mask

Created: 02/Jul/11 12:22 PM   Updated: 30/Jul/11 10:06 AM
Component/s: C frontend
Affects Version/s: None
Fix Version/s: None


 Description  « Hide
the following code

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

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

        .align 4
        .globl foo
        .type foo,@function
        pushl %ebp
        movl %esp,%ebp
        subl $4,%esp
        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)
        .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)

 All   Comments   Change History      Sort Order: Descending order - Click to sort in ascending order
Anders Magnusson added a comment - 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.

Anders Magnusson added a comment - 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.