EtchedPixels / Fuzix-Compiler-Kit

Fuzix C Compiler Project
Other
45 stars 11 forks source link

z80 and 6809: issue subtracting char pointers #64

Closed DoctorWkt closed 4 months ago

DoctorWkt commented 4 months ago

Alan, I'm compiling levee with the z80 backend. In display.c:mvcur() there is this code which I've decorated:

int ww;                       /* Global variable which I added */

PROC
mvcur(int y, int x)
{
    static char gt[30];
    ...
#if ANSI
    {   register char *p = gt;
         /* Code that moves p up */

        ww= p - gt;      /* I added this and the printf below */
printf("p is %x, gt is %x sub is %x ww %x\n", p, gt, (p-gt), ww); exit(1);
        WRITE_TEXT(1, gt, (p-gt));

because I'm seeing this output from the printf():

p is 9eea, gt is 9ee4 sub is 9ee4 ww 9ee4

It looks like the (p-gt) isn't producing the right Z80 assembly code.

P.S. And if I change things to:

unsigned int ww, pp, gg;    /* Globals */
...
        pp= (unsigned int)p;
        gg= (unsigned int)gt;
        ww= pp - gg;
        WRITE_TEXT(1, gt, ww);

then things work fine.

DoctorWkt commented 4 months ago

This test program exhibits the behaviour but it goes away when I remove the static keyword.

#include <stdio.h>

int main() {
  static char gt[30];
  char *p;
  p= gt;

  *p++ = 'W';
  *p++ = 'K';
  *p++ = 'T';

  printf("p-gt is %d %x\n", (p-gt), (p-gt));
  return(0);
}
DoctorWkt commented 4 months ago

And ... the same behaviour (static: bad, non-static: good) is exhibited by the 6809 backend as well as the Z80 backend. Hmm. I've made it test036 in Fuzemsys.

EtchedPixels commented 4 months ago

For some reason it generated printf("./..", gt, gt);

Not quite sure where the subtract went but I see the wrong code

EtchedPixels commented 4 months ago

Ok swapping the types to int works (but found another bug in the Z80 codegen which I've now fixed).

It only occurs if

  1. The pointer is byte sized
  2. The static object is declared within the function

It's present on all targets and the backend doesn't know the difference between the two declarations, so this is a bug in the compiler proper not the backend I think.

EtchedPixels commented 4 months ago
    T_ARGCOMMA v0 t9 f0 
        T_ARGCOMMA v0 t10 f0 
            T_LABEL v0 t10 f0   gt
            T_LABEL v0 t10 f0   gt
        T_LABEL v0 t9 f0 
    T_NAME v0 t8000 f0  printf
EtchedPixels commented 4 months ago

Closed by 69e39601f8cded05099d25921b394f969b0ad4e4