Menu

#3283 Miscompilation (reassigning iy whilst it's holding a value)

closed-fixed
None
Z80
5
2021-09-22
2021-09-21
dom
No

Version:

SDCC : z80/z180/r2k/r2ka/r3ka/gbz80/ez80_z80/z80n 4.1.11 #12685 (Mac OS X x86_64)

Also in 12555 and 12419

Configure line:

./configure                 --disable-ds390-port --disable-ds400-port                 --disable-hc08-port --disable-s08-port --disable-mcs51-port                 --disable-pic-port --disable-pic14-port --disable-pic16-port                 --disable-tlcs90-port --disable-xa51-port --disable-stm8-port                 --disable-pdk13-port --disable-pdk14-port                 --disable-pdk15-port --disable-pdk16-port                 --disable-ucsim --disable-device-lib --disable-packihx

Compilation line:

sdcc -mz80 -S test.c

Test file (apologies for not trimming this down):

typedef unsigned char boolean;
#define true 1
#define false 0

typedef unsigned char obj_code;


typedef struct object_d {
    obj_code code;
    char *desc;
    unsigned int position;      // Always int, as carried =1500, worn=1600
    unsigned char attributes;
} object;


object *odummy;
unsigned char current_position;

char *message1006;
char *message1005;

extern object *search_object_p(unsigned int o);
extern void show_message(const char *m);

#define ISNOTMOVABLE  1
#define CARRIED 1500


boolean get(unsigned int o)
{
    odummy=search_object_p(o);
    if(odummy->position!=current_position) {
        show_message(message1006);
    } else if((odummy->attributes&ISNOTMOVABLE)==0) {
        show_message(message1005);
    } else {
        odummy->position=CARRIED;
        return false;
    }
    return true;
}

Generated code snippet:

        ld      (_odummy), hl
;blah.c:33: if(odummy->position!=current_position) {
        ld      iy, (_odummy)
        push    iy
        pop     bc
        inc     bc
        inc     bc
        inc     bc
        ld      l, c
        ld      h, b
        ld      e, (hl)
        inc     hl
        ld      d, (hl)
        ld      iy, #_current_position   <<<<<
        ld      l, 0 (iy)
;       spillPairReg hl
;       spillPairReg hl
        ld      h, #0x00
;       spillPairReg hl
;       spillPairReg hl
        cp      a, a
        sbc     hl, de
        jr      Z, 00105$
;blah.c:34: show_message(message1006);
        ld      hl, (_message1006)
        push    hl
        call    _show_message
        pop     af
        jr      00106$
00105$:
;blah.c:35: } else if((odummy->attributes&ISNOTMOVABLE)==0) {
        push    iy     <<<<
        pop     de
        ld      hl, #5
        add     hl, de
        ld      a, (hl)
        rrca
        jr      C, 00102$

Problematic lines marked with <<<. At line 35 it's assumed that iy should hold (odummy) however, it has been loaded up with the contents of current_position.

Discussion

  • Philipp Klaus Krause

    Thanks. I can reproduce the issue using current SDCC 4.1.10 #12683 on my Debian GNU/Linux testing system.

     
  • Philipp Klaus Krause

    • assigned_to: Philipp Klaus Krause
     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
     
  • Philipp Klaus Krause

    Fixed in [r12687].

     

Log in to post a comment.