Post Thu Dec 17, 2015 3:14 pm

Yet another optimized bug

Sample program test.bas:

  Code:
GOTO 10

sub FASTCALL proc1()
    asm
start:
        ld hl,(stuff)
        ld (23672),hl
        ret
stuff:
        defw 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        defw 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        defw 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        defw 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    end asm
end sub

sub FASTCALL proc2(a AS UBYTE)
    asm
        cp      10
        jr      nz, skip
        dec     a
skip:
        jp      start
    end asm
end sub

10 proc1()
   proc2(0)


Compiling it using -O2 works correctly and produces the following code for routine proc2:

  Code:
_proc2:
#line 17
        cp      10
        jr      nz, skip
        dec     a
skip:
        jp      start
#line 22
_proc2__leave:
        ret


Compiling it using -O3 produces error "Relative jump out of range" and produces the following code for routine proc2:

  Code:
_proc2:
#line 17
        cp      10
        jr      nz, start
        dec     a
skip:
        jp      start
#line 22
_proc2__leave:
        ret


Again, the optimizer is supposed to try to optimize its own generated code only, not someone else's assembly code!