Skip to content

Conversation

calc84maniac
Copy link

Inlined memmove was generating a comparison pseudo-instruction using the input destination pointer register directly, which could end up being an invalid register such as IY, generating an sbc hl, iy instruction.

I moved the comparison into the memmove pseudo-instruction itself to ensure it always uses sbc hl, de, and also optimized the LDDR path to use the subtraction result to add BC-1 to DE prior to adding it back into HL.

I also managed to ensure the BC, DE, HL registers are carried directly into the LDIR/LDDR paths, though I'm not 100% sure if what I did was valid (I allocated virtual registers to pass them into the blocks, then replaced all usages of those virtual registers with the physical registers). LLVM didn't yell at me though, unlike with some other approaches I tried.

Example code output before this fix:

        ld      hl, (iy + 5)
        ld      bc, 5
        call    __imulu
        push    hl
        pop     de
        ld      hl, _vars
        add     hl, de
        ld      (ix - 14), hl
        lea     de, iy
        or      a, a
        sbc     hl, iy
        jr      c, BB2_5
        ld      hl, (ix - 14)
        ld      iy, 5
        lea     bc, iy
        ldir
        jr      BB2_6
BB2_5:
        ld      hl, (ix - 14)
        ld      iy, 5
        lea     bc, iy
        add     hl, bc
        dec     hl
        ex      de, hl
        add     hl, bc
        dec     hl
        ex      de, hl
        lddr
BB2_6:
        ld      de, (ix - 14)
        ld      hl, (ix - 11)
        lea     bc, iy
        ldir

And after:

        ld      hl, (iy + 5)
        ld      bc, 5
        call    __imulu
        push    hl
        pop     de
        ld      hl, _vars
        add     hl, de
        lea     de, iy
        push    hl
        pop     iy
        or      a, a
        sbc     hl, de
        jr      c, BB2_5
        add     hl, de
        ldir
        jr      BB2_6
BB2_5:
        ex      de, hl
        add     hl, bc
        dec     hl
        ex      de, hl
        add     hl, de
        lddr
BB2_6:
        lea     de, iy
        ld      hl, (ix - 11)
        ld      bc, 5
        ldir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant