As far as I know, by putting the packed attribute on struct 'a' you are
removing the guarantee that the structure starts at an word aligned
address. The address of 'a' isn't known at compile time and so the
compiler must generate code that will work at all memory offsets (I
think?).

How did the compiler behave in the old version?

- Wayne

-----Original Message-----
From: Robert Kavaler [mailto:[email protected]] 
Sent: Friday, 30 October 2009 4:35 PM
To: [email protected]
Subject: [Mspgcc-users] Possible code generation bug for packed structs

A bug (or unfortunate feature) has been added to 3.2.3 version of the 
compiler related to packed structures. Older versions of the compiler 
(from 5 years ago) did not show this problem, but the latest CVS version

10/12/2009 does. The problem is that when the packed attribute is 
applied to a struct the compiler generates very optimal code for 
accesses to int aligned int variables.  Here is a simple test program:

struct __attribute__ ((packed)) a { int a; };
struct a aa;
void testa() { aa.a |= 0x8000; }

struct b { int b;};
struct b bb;
void testb() { bb.b |= 0x8000; }

compiled by:
/opt/mspgcc/bin/msp430-gcc -O4 -mmcu=msp430x149 -S mspbug.c

yields:

    .file    "mspbug.c"
    .arch msp430x149

/* Hardware multiplier registers: */
__MPY=0x130
__MPYS=0x132
__MAC=0x134
__MACS=0x136
__OP2=0x138
__RESLO=0x13a
__RESHI=0x13c
__SUMEXT=0x13e

    .text
    .p2align 1,0
.global    testa
    .type    testa,@function
/***********************
 * Function `testa'
 ***********************/
testa:
    /* prologue: frame size = 0 */
.L__FrameSize_testa=0x0
.L__FrameOffset_testa=0x0
    /* prologue end (size=0) */

    mov.b    &aa, r14
    mov    #aa+1, r12
    mov.b    @r12, r13
    swpb    r13
    bis    r14, r13
    bis    #llo(-32768), r13
    mov.b    r13, &aa
    swpb    r13
    mov.b    r13, @r12
    ret

    /* epilogue: not required */
    /* function testa size 15 (14) */
.Lfe1:
    .size    testa,.Lfe1-testa
/********* End of function ******/

    .p2align 1,0
.global    testb
    .type    testb,@function
/***********************
 * Function `testb'
 ***********************/
testb:
    /* prologue: frame size = 0 */
.L__FrameSize_testb=0x0
.L__FrameOffset_testb=0x0
    /* prologue end (size=0) */

    bis    #llo(-32768), &bb
    ret

    /* epilogue: not required */
    /* function testb size 4 (3) */
.Lfe2:
    .size    testb,.Lfe2-testb
/********* End of function ******/

    .comm aa,2,2
    .comm bb,2,2

/*********************************************************************
 * File mspbug.c: code size: 19 words (0x13)
 * incl. words in prologues: 0, epilogues: 2
 *********************************************************************/


Notice show the unpacked structure generates one instruction while the 
packed structure generates 9 instructions to do the same thing. Without 
the optimizer the 9 instructions grows to 16 instructions.


------------------------------------------------------------------------
------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and
stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Mspgcc-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to