autc04 / Retro68

a gcc-based cross-compiler for classic 68K and PPC Macintoshes
GNU General Public License v3.0
548 stars 54 forks source link

ONEWORDINLINE et al fail with -fpic and other compiler options #238

Closed elliotnunn closed 5 months ago

elliotnunn commented 6 months ago

Hello Wolfgang!

To save RAM in classicvirtio, I am trying to run my DRVR code from ROM. This requires A5-relative data references to the global variables in RAM.

The "-msep-data" option seems to generate the A5-relative code I need, but it breaks ONEWORDINLINE, leaving the linker searching for nonexistent library functions like BLOCKMOVEDATA.

I wrote a quick shell script to demonstrate the effect of various options on the codegen. Outputs lacking ".short 0xa9ff" are broken.

I can't figure out from m68k.cc etc how the "raw_inline" attribute interacts with codegen options. Any idea how we can get this to work? Many thanks!

COMPILING THIS TEST CODE:
void Debugger(void) = {0xa9ff};
void testfunc(void) {
    Debugger();
}

COMPILED WITH "m68k-apple-macos-gcc -c "
   0:   4e56 0000       linkw %fp,#0
   4:   a9ff            .short 0xa9ff
   6:   4e71            nop
   8:   4e5e            unlk %fp
   a:   4e75            rts
   c:   8874 6573 7466  orw %a4@(000000007466756e)@(0000000063000000),%d4
  12:   756e 6300 0000 

COMPILED WITH "m68k-apple-macos-gcc -c -mpcrel"
   0:   4e56 0000       linkw %fp,#0
   4:   a9ff            .short 0xa9ff
   6:   4e71            nop
   8:   4e5e            unlk %fp
   a:   4e75            rts
   c:   8874 6573 7466  orw %a4@(000000007466756e)@(0000000063000000),%d4
  12:   756e 6300 0000 

COMPILED WITH "m68k-apple-macos-gcc -c -fpic"
   0:   4e56 0000       linkw %fp,#0
   4:   2f0d            movel %a5,%sp@-
   6:   4bfa 0000       lea %pc@(8 <testfunc+0x8>),%a5
   a:   202d 0000       movel %a5@(0),%d0
   e:   2040            moveal %d0,%a0
  10:   4e90            jsr %a0@
  12:   4e71            nop
  14:   2a6e fffc       moveal %fp@(-4),%a5
  18:   4e5e            unlk %fp
  1a:   4e75            rts
  1c:   8874 6573 7466  orw %a4@(000000007466756e)@(0000000063000000),%d4
  22:   756e 6300 0000 

COMPILED WITH "m68k-apple-macos-gcc -c -fPIC"
   0:   4e56 0000       linkw %fp,#0
   4:   2f0d            movel %a5,%sp@-
   6:   4bfa 0000       lea %pc@(8 <testfunc+0x8>),%a5
   a:   202d 0000       movel %a5@(0),%d0
   e:   2040            moveal %d0,%a0
  10:   4e90            jsr %a0@
  12:   4e71            nop
  14:   2a6e fffc       moveal %fp@(-4),%a5
  18:   4e5e            unlk %fp
  1a:   4e75            rts
  1c:   8874 6573 7466  orw %a4@(000000007466756e)@(0000000063000000),%d4
  22:   756e 6300 0000 

COMPILED WITH "m68k-apple-macos-gcc -c -msep-data"
   0:   4e56 0000       linkw %fp,#0
   4:   202d 0000       movel %a5@(0),%d0
   8:   2040            moveal %d0,%a0
   a:   4e90            jsr %a0@
   c:   4e71            nop
   e:   4e5e            unlk %fp
  10:   4e75            rts
  12:   8874 6573 7466  orw %a4@(000000007466756e)@(0000000063000000),%d4
  18:   756e 6300 0000 

COMPILED WITH "m68k-apple-macos-gcc -c -mid-shared-library"
   0:   4e56 0000       linkw %fp,#0
   4:   2f0d            movel %a5,%sp@-
   6:   2a6d 0000       moveal %a5@(0),%a5
   a:   202d 0000       movel %a5@(0),%d0
   e:   2040            moveal %d0,%a0
  10:   4e90            jsr %a0@
  12:   4e71            nop
  14:   2a6e fffc       moveal %fp@(-4),%a5
  18:   4e5e            unlk %fp
  1a:   4e75            rts
  1c:   8874 6573 7466  orw %a4@(000000007466756e)@(0000000063000000),%d4
  22:   756e 6300 0000 
autc04 commented 5 months ago

Frustrating. This used to work a few gcc updates ago.

It looks like gcc now handles the PIC/sep_data stuff before it gets to the code I added for the inline opcodes stuff.

autc04 commented 5 months ago

The inline syntax gets converted to a 'raw_inline' attribute that triggers some special code when the call insn is emitted in m68k.c. There is one place in calls.cc where I check for the attribute and disable some transformation in that case. But apparently another transformation happens in-between for fPIC. We just need to figure out where.

elliotnunn commented 5 months ago

How can I do an incremental build of GCC using the Retro68 build system? Following the readme causes a fresh build every time, making this hard to track down!

And I am very curious: why were you using -msep-data?

autc04 commented 5 months ago

Using Retro68's build script, you can't, it's just a series of command invocations. But after the build, you can cd to gcc-build/gcc and run make there. This will just build the cc1 binary inside the build directory, which you can test directly in this case (cc1 is just the compiler itself without the driver that wraps it, it's basically equivalent o gcc -S -o -)

autc04 commented 5 months ago

I think I have now managed to fix this. Have fun!

elliotnunn commented 5 months ago

Can confirm. Thank you!!