bebbo / gcc

Bebbo's gcc-6-branch for m68k-amigaos
GNU General Public License v2.0
33 stars 11 forks source link

Any optimizations cause miscompiles and crashes #207

Closed Midar closed 12 months ago

Midar commented 1 year ago

Building with everything above -O0 seems to cause miscompiles leading to crashes. I don't have any specific example, since it seems so many things are miscompiled. From what I've seen when compiling ObjFW with -O1, -O2 and -Os:

-O1: Mostly works, until some loop gets miscompiled. -O2: Immediately crashes -Os: Immediately has an assertion fail

I'm not sure where to start at all, given how much is being miscompiled. But being able to use something better than -O0 would benefit such old hardware a lot.

bebbo commented 1 year ago

this reproduced the bug: http://franke.ms/cex/z/85Ed9M

the lines 43-45 read

        move.l (a2),(a0)
        move.l (a2),a0
        move.l a2,(4,a0)

with the fix it's

        move.l (a2),a0
        move.l a0,(a0)
        move.l (a2),a0
        move.l a2,(4,a0)
bebbo commented 1 year ago

http://franke.ms/cex/z/85Ed9M now shows the correct code

Midar commented 1 year ago

With the recent commits, it seems to get a lot farther now.

GCC also gets confused about initialization when using -Os.

Here's (simplified) code:

+ (id)handler
{
        id handler;

        [globalMutex lock];
        @try {
                handler = somethingReturningTheHandler();                  
        } @finally {   
                [globalMutex unlock];
        }                                                                  

        if (handler == nil)
                @throw [SomeException exception];

        return handler;
}

GCC complains with -Os that on return handler, handler could be uninitialized. However, if handler isn't initialized, this code is never reached as it means an exception was thrown and the control flow stopped.

bebbo commented 1 year ago

GCC complains with -Os that on return handler, handler could be uninitialized. However, if handler isn't initialized, this code is never reached as it means an exception was thrown and the control flow stopped.

I can't help there

bebbo commented 1 year ago

and using double for seconds is also not a good choice on the Amiga. Use long long instead.

Midar commented 1 year ago

long long doesn't have more than second precision. double allows the precision to be scaled with how far away the date is, which is very desirable: Nobody cares about nanosecond granularity for a date in the year 10524.