#570 SDCC Compiler Bug...

closed-rejected
Bernhard Held
5
2013-05-25
2003-08-13
Anonymous
No

... or optimizer bug.
My code

void TransmitPacket()
{
unsigned char n;
CE = 1;
Delay100us(1);

SpiReadWrite(shockbase[0x08]);
SpiReadWrite(shockbase[0x09]);
SpiReadWrite(shockbase[0x0A]);
SpiReadWrite(shockbase[0x0B]);

for(n=0;n<20;n++)
{
SpiReadWrite(n);
}
CE = 0;
Delay100us(100);
}
Does not compile/optimize the FOR loop correct!
However changing datatype for [n] to [char]
will get the code to work.
Both CHAR and UCHAR asmlisting for the loop is attached
below.

Compiler version [2.3.5] [Apr 21 2003] [MINGW32]

//best regards
//Jörgen Tapani
//jota@telia.com

///////////////////////////////////////////////////
//with char n;
;pcu.c:95: for(n=0;n<20;n++)
; genAssign
mov r2,#0x00
00101$:
; genCmpLt
; genCmp
clr c
mov a,r2
xrl a,#0x80
subb a,#0x94
; genIfxJump
; Peephole 108 removed ljmp by inverse jump logic
jnc 00104$
00109$:
;pcu.c:97: SpiReadWrite(n);
; genCall
mov dpl,r2
push ar2
lcall _SpiReadWrite
pop ar2
;pcu.c:95: for(n=0;n<20;n++)
///////////////////////////////////////////////////

///////////////////////////////////////////////////
//with unsigned char n;
;pcu.c:95: for(n=0;n<20;n++)
; genAssign
mov r2,#0x00
00101$:
; genCmpLt
; genCmp
cjne r2,#0x14,00109$
00109$:
; genIfxJump
; Peephole 108 removed ljmp by inverse jump logic
jnc 00104$
00110$:
;pcu.c:97: SpiReadWrite(n);
; genCall
mov dpl,r2
push ar2
lcall _SpiReadWrite
pop ar2
;pcu.c:95: for(n=0;n<20;n++)
///////////////////////////////////////////////////

Discussion

  • Bernhard Held
    Bernhard Held
    2003-08-14

    Logged In: YES
    user_id=203539

    The first time I've seen this code, I also though it's buggy. But it's
    perfectly correct: cjne sets the carry flag if r2 < 0x14, which can be
    tested by jnc.

     
  • Bernhard Held
    Bernhard Held
    2003-08-14

    • milestone: --> non_bugs
    • assigned_to: nobody --> bernhardheld
    • status: open --> closed-rejected
     
  • Logged In: NO

    Yes i'm sure it seems like it, but it doesn't!
    It might be something weird with 'r2' and the function
    '_SpiReadWrite'.

    Now changing CHAR to UCHAR makes the code to not work.
    That is a fact!
    Since I know that I can surley use CHAR.
    But it annoyes me!

    //best regards
    //Jrgen Tapani
    //jota@telia.com

    Here is the asmlisting for 'SpiReadWrite'
    ///////////////////////////////////////////////////
    _SpiReadWrite:
    ; genReceive
    mov r2,dpl
    ;pcu.c:51: EXIF &= ~0x20;
    ; genAnd
    anl _EXIF,#0xDF
    ;pcu.c:52: SPI_DATA = b;
    ; genAssign
    mov _SPI_DATA,r2
    ;pcu.c:53: while((EXIF & 0x20) == 0);
    00101$:
    ; genAnd
    mov a,#0x20
    anl a,_EXIF
    mov r2,a
    ; genCmpEq
    cjne r2,#0x00,00108$
    ; Peephole 132 changed ljmp to sjmp
    sjmp 00101$
    00108$:
    ;pcu.c:54: return SPI_DATA;
    ; genRet
    mov dpl,_SPI_DATA
    00104$:
    ret
    ///////////////////////////////////////////////////

     
  • Bernhard Held
    Bernhard Held
    2003-08-15

    Logged In: YES
    user_id=203539

    I had a 2nd look at the code, but I can't find anything special. I
    believe you that you've got a problem, but it's not in this piece of
    source. If you still think there's bug, you will have to exactly point at
    the wrong opcode. Until then the bug report keeps closed.