Bad volatile -mstrict-align code on powerpc on 4.0.2?

Till Straumann strauman at slac.stanford.edu
Sun Nov 6 21:43:54 UTC 2005


Peter Dufault wrote:
> Can someone sanity check this and verify that I'm not doing anything  
> stupid?  This is gcc 4.0.2 compiled on solaris 10.  It is generating  
> wacky code with -mstrict-align unless I'm doing something dumb.  In  the 
> following script it is generating reasonable code without - 
> mstrict-align, when I turn on -mstrict-align it is amoungst other  
> things reading back from the volatile register before writing to it.   
> Is there another attribute I'm supposed to use?

It could be that the compiler doesn't realize that your structure is
properly aligned although you hardcode it's address as 0x9fff9800.
Since the struct is 'packed' and you ask for 'mstrict-align' the 
compiler does only byte-wise access (no clue why writing a 16-bit
word as two bytes results in lbz/li/stb instead of just li/stb...).

I have tried to hint

__attribute__((packed,aligned(2)))

which seems to generate proper code even with -mstrict-align

YMMV

T.


BTW - if you don't mind a comment:
IMO it is better coding practice to use in_be16/out_be16 (or
ld_be16/st_be16 if you are sure you don't need the eieio)
for memory-mapped device register access.
It helps to make your code more portable and readable,
clearly identifying I/O operations as such (plus, these
inlines would automatically have enforced the correct
operation :-) w/o surprises...).

> 
> Peter
> 
> 
> ------------------------------------------------------------------------
> 
> Script started on Fri Nov 04 09:41:09 2005
> +++CMD+++> cat foo.c
> typedef unsigned short uint16_t;
> 
> struct foo_reg {
> 	uint16_t leds;		/**< The 16 LEDs on the side of the board. */
> 	uint16_t id_eaprom;	/**< The ID EAPROM. */
> 	uint16_t control;	/**< The control register. */
> 	uint16_t pad0;
> } __attribute__((__packed__));
> 
> void foo(void);
> 
> void foo(void) {
> 	volatile struct foo_reg *fp = (volatile struct foo_reg *)0x9fff9800;
> 	fp->control = 0x00ff;
> }
> +++CMD+++> powerpc-rtems-gcc -O -c foo.c
> +++CMD+++> powerpc-rtems-objdump --disassemble --source foo.o
> 
> foo.o:     file format elf32-powerpc
> 
> Disassembly of section .text:
> 
> 00000000 <foo>:
>    0:	3d 20 9f ff 	lis     r9,-24577
>    4:	61 29 98 00 	ori     r9,r9,38912
>    8:	38 00 00 ff 	li      r0,255
>    c:	b0 09 00 04 	sth     r0,4(r9)
>   10:	4e 80 00 20 	blr
> +++CMD+++> powerpc-rtems-gcc -O -mstrict-align -c foo.c
> +++CMD+++> powerpc-rtems-objdump --disassemble --source foo.o
> 
> foo.o:     file format elf32-powerpc
> 
> Disassembly of section .text:
> 
> 00000000 <foo>:
>    0:	3d 20 9f ff 	lis     r9,-24577
>    4:	61 29 98 00 	ori     r9,r9,38912
>    8:	88 09 00 04 	lbz     r0,4(r9)
>    c:	38 00 00 00 	li      r0,0
>   10:	98 09 00 04 	stb     r0,4(r9)
>   14:	88 09 00 05 	lbz     r0,5(r9)
>   18:	38 00 ff ff 	li      r0,-1
>   1c:	98 09 00 05 	stb     r0,5(r9)
>   20:	4e 80 00 20 	blr
> +++CMD+++> 
> script done on Fri Nov 04 09:41:58 2005




More information about the users mailing list