bebbo / gcc

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

C++ discards register information from variables inside template code #228

Closed michalsc closed 3 months ago

michalsc commented 4 months ago

When compiling c++ code, the marcos/inlines from proto/ SDK directory are wrongly resolved causing catastrophic results, where library base is not loaded to A6 register or putting function arguments to wrong registers.

I have uploaded example code to your compiler explorer: http://franke.ms/cex/z/d1xP8G

Noticable snippets from the code above:

  1. Call to VPrintf (this is how the Printf is resolved) shall put string pointer to D1, pointer to ARG list into D2 and DOSBase into A6, but instead it loads ARG list into A0 and DOSBase into D0, then references never touched A6:

    lea (8,sp),a0
    move.l #.LC0,d1
    move.l _DOSBase,d0
    jsr a6@(-0x3ba:W)
  2. Call to AllocMem shall put byte size to D0, requirements into D1 and SysBase into A6, instead byte size is loaded into D1, SysBase into D0 and requirements into A1:

        moveq bebbo/amiga-gcc#40,d1
        move.l _SysBase,d0
        sub.l a1,a1
        jsr a6@(-0xc6:W)

Subsequent calls from deallocate() method lead to different but still wrong results.

Please note that using clib headers which do a standard C calls to external library result in corrent function invocations but add an additional overhead.

Affected is gcc from 6.5 series. I made additional test with gcc-13.1 from your amiga13 branch and it generates correct assembly for the calls (see attached file) stl.s-13.zip

bebbo commented 4 months ago

it seems that the register information for variables gets lost in c++.

Workaround: use extern "C" wrappers:

extern "C" {
    inline void _Printf(char const * f, ...) {
        VPrintf(f, 1 + &f);
    }
}
michalsc commented 4 months ago

Yes, that would be one option, another is to use clib and linked libraries with stubs. The question remains if it is somehow fixable for gcc-6.5 or if we are stuck with what we have now. Please note on gcc-13.1 from your amiga13 branch everything seems to work as expected.

bebbo commented 4 months ago

Yes, that would be one option, another is to use clib and linked libraries with stubs. The question remains if it is somehow fixable for gcc-6.5 or if we are stuck with what we have now. Please note on gcc-13.1 from your amiga13 branch everything seems to work as expected.

I noted that. Just buy me time...

bebbo commented 4 months ago

Seems to happen only in templates. Minimal: http://franke.ms/cex/z/77Wsah

bebbo commented 4 months ago

please test

michalsc commented 3 months ago

Sorry for late reply - I've just tested it with doubly nested case - template based allocator used within template class - everything is working as expected.

As always many thanks for great work!