jacobly0 / llvm-project

This fork of the canonical git mirror of the LLVM subversion repository adds (e)Z80 targets. Please refer to the wiki for important build instructions.
https://github.com/jacobly0/llvm-project/wiki
120 stars 15 forks source link

16 bit load generated in z80 mode #22

Closed hansake closed 2 years ago

hansake commented 3 years ago

When compiling a very simple test program for z80 an incorrect 16 bit indexed load is generated by the compiler. This was correct in earlier version.

The test program:

/* Very very simple test program: testz80.c
 */
int main() {
    int a , b, c;

    b = 1;
    c = 2;
    a = b + c;
    return (a);
}

Compiled with:

$ ez80-clang --target=z80 -S testz80.c

gave in a previous release this output:

        section .text,"ax",@progbits
    section .text,"ax",@progbits
    public  _main
_main:
    push    ix
    ld  ix, 0
    add ix, sp
    ld  hl, -8
    add hl, sp
    ld  sp, hl
    ld  hl, 0
    ld  de, 1
    ld  bc, 2
    ld  (ix + -2), l
    ld  (ix + -1), h
    ld  (ix + -6), e
    ld  (ix + -5), d
    ld  (ix + -8), c
    ld  (ix + -7), b
    ld  l, (ix + -6)
    ld  h, (ix + -5)
    ld  e, (ix + -8)
    ld  d, (ix + -7)
    add hl, de
    ld  (ix + -4), l
    ld  (ix + -3), h
    ld  l, (ix + -4)
    ld  h, (ix + -3)
    ld  iy, 8
    add iy, sp
    ld  sp, iy
    pop ix
    ret
    section .text,"ax",@progbits

    ident   "clang version 13.0.0 (https://github.com/jacobly0/llvm-project.git d270916c9cf31e630cc9ef215671a0d1c56280f8)"
    extern  __Unwind_SjLj_Register
    extern  __Unwind_SjLj_Unregister

and in the latest release I cloned and compiled, the following assembler output is generated.

    section .text,"ax",@progbits
    section .text,"ax",@progbits
    public  _main
_main:
    push    ix
    ld  ix, 0
    add ix, sp
    ld  hl, -8
    add hl, sp
    ld  sp, hl
    ld  hl, 0
    ld  de, 1
    ld  bc, 2
    ld  (ix - 2), l
    ld  (ix - 1), h
    ld  (ix - 6), e
    ld  (ix - 5), d
    ld  (ix - 8), c
    ld  (ix - 7), b
    ld  l, (ix - 6)
    ld  h, (ix - 5)
    ld  e, (ix - 8)
    ld  d, (ix - 7)
    add hl, de
    ld  (ix - 4), l
    ld  (ix - 3), h
    ld  hl, (ix - 4) <--- incorrect instruction
    ld  iy, 8
    add iy, sp
    ld  sp, iy
    pop ix
    ret
    section .text,"ax",@progbits

    ident   "clang version 13.0.0 (https://github.com/jacobly0/llvm-project.git a25003f240988428e2fad6f77f25d9b600c59b5e)"
    extern  __Unwind_SjLj_Register
    extern  __Unwind_SjLj_Unregister

The instruction that upsets the assembler (z80-elf-as) is:

    ld  hl, (ix - 4)

which is correctly generated in an earlier release as:

    ld  l, (ix + -4)
    ld  h, (ix + -3)

Unfortunately Z80 and Z180 can't make a 16 bit indexed load.

Another thing that z80-elf-as don't understand is:

    public  _main

but that was fixed with a sed script used by the Makefile:

...Makefile
# Define a pattern rule that compiles every .c file into a .o file
%.o : %.c
    ez80-clang --target=z180 -S -o $<.is $<
    sed -f asmscript.sed $<.is >  $<.s
    z80-elf-as -march=z180 -o $@ $<.s
    rm $<.is $<.s
...asmscript.sed
s/\tpublic/\tglobal/
jacobly0 commented 2 years ago

Sorry, I was in the middle of moving and forgot to fix this.

hansake commented 2 years ago

Works good now. Thanks.