freemint / m68k-atari-mint-gcc

Fork of GNU's gcc with support for the m68k-atari-mint target
https://github.com/freemint/m68k-atari-mint-gcc/wiki
Other
26 stars 7 forks source link

gcc 7.3.0 produces code that can cause address errors for -m68000 #9

Closed th-otto closed 6 years ago

th-otto commented 6 years ago

The current version sometimes produces accesses to odd address, that would cause an address-error on plain 68000. The culprit seems to be https://github.com/freemint/m68k-atari-mint-gcc/blob/45f50048599322ca2c4bd34420b992c113195e72/gcc/config/m68k/mint.h#L23-L26

Simple test case:

void test(int c, char *p)
{
        *p++ = 'A' + c;
        *p++ = ':';
        *p = '\0';
}

produces

_test:
        move.l 8(%sp),%a0
        move.b 7(%sp),%d0
        add.b #65,%d0
        move.b %d0,(%a0)
        move.w #14848,1(%a0)
        rts

Similar code can also been found in EmuTOS https://github.com/emutos/emutos/blob/ec5bc814419255a1b1ac6e43543ef30589639144/aes/gemshlib.c#L98

Removing those lines would fix that, but also turn off that optimization for 020+

The generated code would then look like

_test:
        move.l 8(%sp),%a0
        move.b 7(%sp),%d0
        add.b #65,%d0
        move.b %d0,(%a0)
        move.b #58,1(%a0)
        clr.b 2(%a0)
        rts

I'm not sure what is better on 020: writing a word to an odd address, or writing 2 bytes instead?

I also could not convince gcc to use (a0)+ instead.

vinriviere commented 6 years ago

What is "current version"? We have several branches.

th-otto commented 6 years ago

The gcc-7-mint branch, which was used by Miro to build the native compilers. 4.6.4 is not affected by this.

mikrosk commented 6 years ago

I'm not sure what is better on 020: writing a word to an odd address, or writing 2 bytes instead?

Surely one word on an odd address. Unfortunately http://brownbot.mooo.com seems to be broken right now (@ggnkua do you know about it?) so I can't test other compilers how they handle 68000 vs. 68020.

vinriviere commented 6 years ago

Test with: ...gcc -m68000 -O2 -fomit-frame-pointer -S a.c -o -

m68k-atari-mint-gcc 4.6.4 produces the right thing: exactly "The generated code would then look like" above, without move.w.

Standard m68k-elf-gcc 7.1.0 also produces the exact same code.

And in any case, if I replace -m68000 by any other CPU options, the result stays the same: always good.

ggnkua commented 6 years ago

I'm not sure what is better on 020: writing a word to an odd address, or writing 2 bytes instead?

Surely one word on an odd address. Unfortunately http://brownbot.mooo.com seems to be broken right now (@ggnkua do you know about it?) so I can't test other compilers how they handle 68000 vs. 68020.

Cannot reproduce here either. I get code snippet 2. If anyone wants to play around, http://brownbot.mooo.com/z/T6z_ao

mikrosk commented 6 years ago

@ggnkua sorry, I should have been more specific - I meant that I got only a blank screen (no output) for the compiled code. seems to be working now and yes, your ELF build seems to be all right as well.

mikrosk commented 6 years ago

I confirm @vinriviere findings - it seems that the right thing to do is to ignore this optimisation, i.e. always produce the 68000 version. Does disabling STRICT_ALIGNMENT have some other side effects?

mikrosk commented 6 years ago

Fixed by https://github.com/freemint/m68k-atari-mint-gcc/commit/4b7a290df1a088933a94510aa50a6e0edea90469, will prepare a new release.