Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#1755 Z80 code generation bug

closed-works-for-me
z80 port (188)
5
2011-02-19
2011-02-12
Oleg N. Cher
No

Dear SDCC developers,

I found a SDCC Z80 code generation bug.

> 1.Attach the code you are compiling with SDCC

int main (void) {
unsigned char i,mr2,sp;
mr2 = 1; sp = 0; // (mr2-1)*sp = 0, i.e. for(i=1;i<=0;i++) ...
/* and because i=1, and (1<=0) is FALSE, this loop must NOT BE RUNNED */
for(i=1;i<=((mr2-1)*sp);i++) { // ... but this code IS RUNNED!!! :(
}
// Here is unreachable code, because loop is eternal.
}

> 2.Specify the exact command you use to run SDCC, or attach your Makefile.

sdcc\sdcc -mz80 --code-loc 26000 --data-loc 0xF000 --no-std-crt0 --opt-code-size --funsigned-char --disable-warning 59 -L z80 testbug.c

> 3.Specify the SDCC version, your platform and operating system

C:\Soft\Dev-Zx\SDCC\bug\sdcc>sdcc -v
SDCC : mcs51/gbz80/z80/ds390/pic16/pic14/TININative/ds400/hc08 3.0.1 #6183 (Jan 26 2011) (MINGW32)

> 4.Provide an exact copy of any error message or incorrect output

The problem is eternal loop with incorrect code generation. More earlier
versions of SDCC generate the correct code. I tried old version:

C:\Soft\Dev-Zx\SDCC\bug\sdcc>sdcc -v
SDCC : mcs51/gbz80/z80/ds390/pic16/pic14/TININative/ds400/hc08 2.9.7 #5839 (May 30 2010) (MINGW32)

and code was absolutely correct. Difference is:

SDCC VERSION 2.9.7 (CORRECT) SDCC VERSION 3.0.1 (INCORRECT)
;---------------------------------------------------------------
;testbug.c:5: for(i=1;i<=((mr2-1)*sp);i++) { // ... but this code IS RUNNED!!! :(
ld c,#0x01 ld c,#0x01
00101$: 00101$:
ld l,c ld l,c
ld a,#0x00 ld a,#0x00
ld h,a ld h,a
sub a,l sub a, l
ld a,#0x00 ld a,#0x00
sbc a,h sbc a, h
jp PO, 00110$
xor a, #0x80
00110$:
ret M ret M
inc c inc c
jr 00101$ jr 00101$
;---------------------------------------------------------------

Thank you.

P.S. This bug is present in release sdcc-3.0.0-setup.exe

Discussion

    • assigned_to: nobody --> spth
    • status: open --> closed-works-for-me
     
  • I cannot reproduce this bug. In particular, the following code always returns 0 for me:

    int broken (void) {
    unsigned char i,mr2,sp;
    mr2 = 1; sp = 0; // (mr2-1)*sp = 0, i.e. for(i=1;i<=0;i++) ...
    /* and because i=1, and (1<=0) is FALSE, this loop must NOT BE RUNNED */
    for(i=1;i<=((mr2-1)*sp);i++) {return 1; // ... but this code IS RUNNED!!! :(
    }
    return 0;// Here is unreachable code, because loop is eternal.
    }

    Philipp

    P.S.: I used sdcc 3.0.1 #6227 for testing, but

     
  • Oleg N. Cher
    Oleg N. Cher
    2011-02-20

    Dear Philipp,

    I tried to build the code proposed by you, and I see that the bug is lost, is not reproduced. Please try to compile my source code without any changes. I've attached all files for reproducing this bug - http://stef.anarxi.st/files/sdcc-bug.zip . Run testbug.bat and you'll have result *.trd file. This is an image of ZX Spectrum OS TR-DOS what can be launched in any Russian ZX Spectrum emulator with supporting TRD format. I would recommend EmuZWin with integrated dissasembler and step-by-step debbuger, you can download it here - http://kolmck.net/apps/EmuZWin.zip . Open resulting trd image in emulator, select TR-DOS <Enter>, press R (Run) <Enter> and you'll have launched code with eternal loop. Use Tools->Debug Window and you'll can see segment of eternal code and you'll can to run it step-by-step by pressing F8. Btw, you already have your lovely step-by-step Z80 debugger/simulator. Try to run under it binary file testbug.bin (with correct start address, of course). P.S. I used and have included to this zip archive very fresh today SDCC : mcs51/gbz80/z80/ds390/pic16/pic14/TININative/ds400/hc08 3.0.1 #6227 (Feb 19 2011) (MINGW32) - the bug is present here.

    With respect, Oleg