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.