Menu

#435 mos6502 peephole optimizer bugfix

None
closed-accepted
None
5
2023-02-05
2022-06-16
No

While working on porting gbdk-2020 ( https://github.com/gbdk-2020/gbdk-2020/ ) to the NES/Famicom, I noticed a bug in the generated code, where garbage data in the X register appeared to be used instead of zero.
This would cause the collision detection for examples/cross-platform/pong to malfunction and not detect collisions between pad and ball.

The problem turned out to be a bug in the peephole optimizer definitions. More specifically this pattern which replaces an "ldx %2" and a later "txa" with an "lda %2".

replace restart {
          lda    %1
        ldx    %2
    clc
    adc    %3
    sta    %4
    txa
    adc     %5
    sta    %6
} by {
    lda    %1
    ; Peephole genpointer XA   - eliminated ldx
    clc
    adc    %3
    sta    %4
        lda    %2
    ; Peephole genpointer XA   - replaced txa with lda
    adc     %5
    sta    %6
}

Doing this reduction appears to leave the compiler in a state where it still believes X contains the original value, and yet-another txa (separated from this rule) would then have garbage instead.

Adding "if notUsed('x')" suffix to the rule fixed this bug, and appears to not break any other gbdk-2020 examples I've tried so far.
I have attached a patch that does this to this ticket.

1 Attachments

Related

Wiki: NGI0-Entrust-SDCC

Discussion

  • Philipp Klaus Krause

    • status: open --> closed-accepted
    • assigned_to: Philipp Klaus Krause
    • Group: -->
     
    • Michel Iwaniec

      Michel Iwaniec - 2023-01-29

      Hi Philipp,

      Thank you for finally merging this fix!

      Do you think you might be able to have a look at https://sourceforge.net/p/sdcc/patches/436/ as well?

      It's also been pending for very long now... and having 6502 have the same 24bit address format as other build targets would really simplify my gbdk-2020 work :)

      Kind Regards,
      Michel

      EDIT: Removed E-mail boilerplate.

       

      Last edit: Benedikt Freisen 2023-01-29
      • Philipp Klaus Krause

        No. I'm not really familiar with 6502 or the assembler/linker. I've worked on the peephole optimizer enough that I felt confident to apply this patch, despite it being for mos6502; but the mos6502 linker is something someone familiar with the assembler/linker or mos6502 should have a look at.

         
        • Michel Iwaniec

          Michel Iwaniec - 2023-01-31

          Ah, ok. That's fair enough.

          Do you happen to know who the best person to ask for 6502-related patch reviews might be?

          Kind Regards,
          Michel

          EDIT: Removed E-mail boilerplate.

           

          Last edit: Benedikt Freisen 2023-01-31
          • Philipp Klaus Krause

             
  • Philipp Klaus Krause

    Applied in [r13804]. Thanks.

     

    Related

    Commit: [r13804]


Log in to post a comment.

MongoDB Logo MongoDB