[Scummvm-devel] Broken Sword 2 problem

yotam barnoy yotambarnoy at gmail.com
Wed Sep 1 14:20:34 CEST 2010


On Wed, Sep 1, 2010 at 3:06 PM, Willem Jan Palenstijn <wjp at usecode.org> wrote:
>
> Hm, I'm not convinced. The only access to that memory is through this
> Unaligned32 struct, as far as this code is concerned, so there shouldn't be any
> aliasing.

When the compiler optimizes for example header.h's getLogicLevel() and
setLogicLevel() in BS2, we have a byte *_addr ie. a byte array. But
when it's accessed via READ_LE_UINT32, a struct is aliased onto it.
The compiler assumes they can't be in the same place in memory and
optimizes getLogicLevel(), returning the same value without bothering
to read it again.

>
> But the __may_alias__ you mention does sound like it would likely be safer.
> (And it does work for the example I was trying.) Do you know in which version
> of gcc it was added? The oldest one if I have easily available is 4.1.2 and it
> works there.

It works here as well.
Not sure exactly how old it is, but I found a reference on google
using __may_alias__ that's for gcc 3.3, though it seems at that point
they hadn't turned on strict aliasing by default for -O2 and -O3.

Yotam


>
>> I'm currently experimenting with using the __may_alias__ gcc attribute
>> which one of the gcc guys suggested to me. In other words, using
>> __attribute__ ((__packed__, __may_alias__)).
>
>
> -Willem Jan
>




More information about the Scummvm-devel mailing list