Menu

#3177 [ucsim] equivalent opcodes have different results

closed-invalid
nobody
Simulator (20)
Simulator
5
2022-11-22
2021-02-08
No

With this patch I get with make -C support/regression test-ucgbz80:

Summary for 'ucgbz80': 1 abnormal stops ( ), 2101 failures, 16829 tests, 2384 test cases, 3921650 bytes, 990177747 ticks 

But

    ld  a, #0
    sbc a, (hl)

and

    sbc a, a
    sub a, (hl)

are equivalent code:
http://z80-heaven.wikidot.com/optimization#toc18

Most curious fail is bug-1918.c, the only change was:

- sbc a, a
- sub a, (hl)
+ ld  a, #0x00
+ sbc a, (hl)
  ld  a, #0x00
$ bin/sdcc -v
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/gbz80/tlcs90/ez80_z80/z80n/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15 4.0.7 #12058 (Linux)
published under GNU General Public License (GPL)
1 Attachments

Discussion

  • Daniel Drotos

    Daniel Drotos - 2021-02-10

    replace {
    ld a, #%1
    sbc a, (hl)
    } by {
    ; common peephole 122b optimized carry substraction by 0.
    sbc a, a
    sub a, (hl)
    }

    Both code results same value in A but they are producing different C flag. C flag of LD version does not depend on the C value before the code. But SBC version produces C which depends on starting C value.

    Daniel

     
    • Sebastian Riedel

      Yes thanks, it's not a bug then and I have to limit the rules with notUsed('f')

       
  • Sebastian Riedel

    • status: open --> closed-invalid
     
  • Maarten Brock

    Maarten Brock - 2022-11-22

    I bumped into this old and closed item, but I don't understand how both codes are different. Sure the incoming carry isn't used in LD, but it also doesn't modify the carry either, right?

    ld a,#0x00  ; carry not touched, a:=0
    sbc a,(hl)  ; use incoming carry, set outgoing carry
    

    Should in my mind be equal to:

    sbc a,a     ; use incoming carry, a:=0 or a:=-1
    sub a,(hl)  ; incoming carry ignored, set outgoing carry
    

    So if this fails regression tests then I suspect a problem in the simulator.

     
    • Sebastian Riedel

      no carry:

      a=0-(hl)-0
      
      a=0-(hl)
      

      If there is no carry, it shouldn’t matter.

      carry:

      a=0-(hl)-1
      
      a=-1-(hl)
      

      This makes a difference

      0-0-1 has a carry, but -1-0 doesn’t.

       
      • Maarten Brock

        Maarten Brock - 2022-11-22

        Ah, right. Sorry for the noise.

         

Log in to post a comment.