Menu

#3135 GBZ80 accessing variable on stack spoils result in HL

closed-fixed
None
GBZ80
5
2020-11-02
2020-10-17
Tony Pavlov
No

offset of (Actor)[hit].hit_1_ptr is calculated into HL. then SDCC fetches value of stack-allocated variable to make conditional jump, that spoils HL. then it adds address of actors array to HL, assuming it still contains offset:

;test9.c:45: TestFn2(&actors[hit].hit_1_ptr);
    ld  e, l
    ld  d, #0x00
    ld  l, e
    ld  h, d
    add hl, hl
    add hl, de
    add hl, hl
;test9.c:44: if (projectiles[i].col_group == 2) {
    ldhl    sp, #0
    ld  a, (hl)
    sub a, #0x02
    jr  NZ, 00104$
;test9.c:45: TestFn2(&actors[hit].hit_1_ptr);
    ld  de, #_actors
    add hl, de

that behaviour is very fragile. for example --max-allocs-per-node 50000 fixes that by eliminating stack-allocated projectiles[i].col_group.

version:

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.3 #11916 (MINGW64)

commandline:

sdcc -mgbz80 --fsigned-char --no-std-crt0 -I ..\..\gbdk\include -I ..\..\gbdk\include\asm -I src\include -c test9.c -o build\test9.rel
2 Attachments

Discussion

  • Philipp Klaus Krause

    I haven't been able to reproduce this on my Debian GNU/Linux testing system so far, but will keep looking into it further.

    P.S.: In [r11923], I added a test, which I hope will trigger this bug on some of the systems in the compile farm.

    P.P.S.: Could you add the test.asm when compiling with --fverbose-asm --no-peep --i-code-in-asm?

     

    Last edit: Philipp Klaus Krause 2020-10-26
    • Tony Pavlov

      Tony Pavlov - 2020-10-26

      how can i help you here? maybe provide some additional info?

      SDCC 4.0.4 #11922 (MINGW64)
      

      generates:

      ;   genMove_o
      ;test9.c:45: TestFn2(&actors[hit].hit_1_ptr);
      ;   genMult
      ;fetchPairLong
      ; common peephole 1 removed dead load from l into e.
      ; common peephole 1 removed dead load from #0x00 into d.
      ; common peephole 1 removed dead load from e into l.
      ; common peephole 1 removed dead load from d into h.
      ; common peephole 2a removed add hl,hl due to unused result.
      ; common peephole 2a removed add hl,de due to unused result.
      ; common peephole 2a removed add hl,hl due to unused result.
      ;   genMove_o
      ;   genMove_o
      ;test9.c:44: if (projectiles[i].col_group == 2) {
      ;   genCmpEq
      ;   AOP_STK for _TestFn_sloc0_1_0
          ldhl    sp, #0
          ld  a, (hl)
          sub a, #0x02
      ; common peephole 81 removed jp by using inverse jump logic
      ; common peephole 158 removed unused label 00135$.
      ; common peephole 163 changed absolute to relative conditional jump.
          jr  NZ, 00104$
      ; common peephole 82 removed jp by using inverse jump logic
      ; common peephole 158 removed unused label 00136$.
      ;test9.c:45: TestFn2(&actors[hit].hit_1_ptr);
      ;   genPlus
      ;fetchPairLong
      ;fetchLitPair
          ld  de, #_actors
          add hl, de
      

      with disabled PH rules:

      ;   genMove_o
      ;test9.c:45: TestFn2(&actors[hit].hit_1_ptr);
      ;   genMult
      ;fetchPairLong
          ld  e, l
          ld  d, #0x00
          ld  l, e
          ld  h, d
          add hl, hl
          add hl, de
          add hl, hl
      ;   genMove_o
      ;   genMove_o
      ;test9.c:44: if (projectiles[i].col_group == 2) {
      ;   genCmpEq
      ;   AOP_STK for _TestFn_sloc0_1_0
          ldhl    sp, #0
          ld  a, (hl)
          sub a, #0x02
          jp  NZ,00135$
          jp  00136$
      00135$:
          jp  00104$
      00136$:
      ;test9.c:45: TestFn2(&actors[hit].hit_1_ptr);
      ;   genPlus
      ;fetchPairLong
      ;fetchLitPair
          ld  de, #_actors
          add hl, de
      
       
  • Tony Pavlov

    Tony Pavlov - 2020-10-26
    sdcc -mgbz80 --fsigned-char --no-std-crt0 -I ..\..\gbdk\include -I ..\..\gbdk\include\asm -I src\include -c --fverbose-asm --no-peep --i-code-in-asm test9.c -o build\test9.rel
    

    what is the code here, generated by linux port, i wonder?

    full asm in attach below:

     

    Last edit: Tony Pavlov 2020-10-26
    • Philipp Klaus Krause

      This should be fixed in [r11924]. Please test.
      On my Debian GNU/Linux testing system, projectiles[i].col_group got allocated to register b.

       
      • Tony Pavlov

        Tony Pavlov - 2020-10-27

        just released r11925 works perfect! thank you, Philipp!

         
  • Philipp Klaus Krause

    • assigned_to: Philipp Klaus Krause
    • Category: other --> GBZ80
     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
     

Log in to post a comment.

Monday.com Logo