gcc Sparc alignment problems

Jim Wilson wilson@specifixinc.com
Mon Apr 19 23:20:00 GMT 2004

Phil Prentice wrote:
>  Does anyone know of a gcc compiler flag or option that would enable us to 
> compile and successfully run 'C' code that will access mis-aligned integers 
> or dioubles which runs on a SPARC workstation running Solaris.
>   The Sun compiler achieves this by means of the -misalign flag

The Sun -misalign flag was originally added for people migrating from 
Sun3 (m68k) systems to Sun4 (sparc) systems.  m68k systems only align 
data to 2 bytes, whereas sparc systems align data to 4 bytes.  So if you 
have existing data created on a m68k system, you might not be able to 
read it on a sparc system.  -misalign adds the assumption that all 
pointers point to unaligned data, and thus we use byte loads and shift 
to access all data.  This allows use of m68k created data on a sparc system.

The m68k to sparc switchover happened so long ago that people really 
shouldn't need the -misalign flag anymore.  Use of this option will 
seriously hurt performance.  You would be much better off if you use 
attribute packed and/or attribute aligned to make your data structures 
match your data.

I did once add an equivalent option to gcc at Cygnus a long time ago, 
but I never cleaned it up to the point where I felt confident that it 
would be accepted into the FSF gcc sources.  It wasn't hard to add it. 
You just add another flags to MEM to indicate that they are unaligned. 
You set this flag when expanding an INDIRECT_REF in expand_expr.  In 
emit_move_insn, if the source is a MEM with this flag set, then you call 
extract_bitfield.  If the dest is a MEM with this flag set, then you 
call store_bitfield.

As a bonus, one can use this to implement unaligned pointers.  Just add 
an attribute unaligned, and when we deference a pointer with this 
attribute set, we set the MEM unaligned flag.
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com

More information about the Gcc-help mailing list