#1635 PIC14: bit operations with wrong value in W register

closed-fixed
5
2013-05-25
2010-04-12
Jim Paris
No

Attached code miscompiles on pic14.
sdcc -v:
SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.9.0 #5416 (Jan 10 2010) (UNIX)

compile with:
sdcc --std-c89 -mpic14 -p16f628a --opt-code-speed -c -o bug.o bug.c

In the .lst file, you can see four occurances like this one:

00192 ; .line 11; "bug.c" RB6 = (c & 8) ? 1 : 0;
001E 0000 0000 00193 BANKSEL r0x1001
0020 0C00 00194 RRF r0x1001,W
0021 1803 00195 BTFSC STATUS,0
0022 2800 00196 GOTO _00001_DS_
0023 0000 0000 00197 BANKSEL _PORTB_bits
0025 1300 00198 BCF _PORTB_bits,6
0026 00199 _00001_DS_
0026 1C03 00200 BTFSS STATUS,0
0027 2800 00201 GOTO _00002_DS_
0028 0000 0000 00202 BANKSEL _PORTB_bits
002A 1700 00203 BSF _PORTB_bits,6
002B 00204 _00002_DS_

but the "W" register (used in "RRF r0x1001,W") is a stale value, in this case 0x7B.

Discussion

  • Jim Paris
    Jim Paris
    2010-04-12

    Source code exhibiting the bug

     
    Attachments
  • Jim Paris
    Jim Paris
    2010-04-12

    Generated .lst file

     
    Attachments
  • Raphael Neider
    Raphael Neider
    2010-04-12

    I do not see the problem: "RRF r0x1001, W" does not READ W at all -- it overwrites W with "r0x1001 >> 1" and updates STATUS, extracting the least signicant bit of r0x1001 (prior to shifting).
    Could you give any more details? Observed vs. expected output?

     
  • Raphael Neider
    Raphael Neider
    2010-04-12

    OK, I see the problem now:

    RB6 = (c & 8) ? 1 : 0;

    is handled incorrectly (also for c&4, c&2, and c&1). Instead of implicitly doing

    RB6 = (0 != (c & 8)) ? 1 : 0;

    sdcc executes

    struct {
    uint8_t b0:1;
    } temp;
    temp.b0 = (c & 8); // THIS ALWAYS CLEARS temp.b0
    RB6 = ((uint8_t)temp.b0) ? 1: 0;

    by mistaking the logic bit test on bit 3 for an assignment to a bitfield of width 1, for which only bit 0 of the expression is relevant...

    As a workaround, you can use

    RB6 = (0 != (c & 8)) ? 1 : 0;

    I'll try to fix this.

    Best regards,
    Raphael

     
  • Jim Paris
    Jim Paris
    2010-04-13

    Sorry for the misdiagnosis, been dealing with too many assembly language architectures lately :)
    Your description sounds right and the workaround does the right thing. Thanks for looking into it.

     
  • Borut Ražem
    Borut Ražem
    2012-01-23

    • summary: bit operations with wrong value in W register --> PIC14: bit operations with wrong value in W register
     
  • Raphael Neider
    Raphael Neider
    2012-02-11

    Fixed for PIC14 and PIC16 in r7302.

     
  • Raphael Neider
    Raphael Neider
    2012-02-11

    • milestone: --> fixed
    • status: open --> closed-fixed
     
  • Raphael Neider
    Raphael Neider
    2012-02-12

    • assigned_to: nobody --> tecodev