Skip to content

Commit b83c220

Browse files
committed
Optimize loop stack handling, 22 less bytes.
1 parent 4729810 commit b83c220

1 file changed

Lines changed: 35 additions & 51 deletions

File tree

src/actions.asm

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -645,17 +645,19 @@ ret: rts
645645
.proc push_codep
646646
; Saves current code position in loop stack
647647
ldy loop_sp
648-
sta loop_stk, y
649-
pha
650-
jsr get_codep
651-
sta loop_stk + 1, y
652-
txa
653-
sta loop_stk + 2, y
648+
::push_codep_y:
654649
iny
655650
iny
656651
iny
657652
bmi loop_error
658653
sty loop_sp
654+
655+
sta loop_stk - 3, y
656+
pha
657+
jsr get_codep
658+
sta loop_stk - 2, y
659+
txa
660+
sta loop_stk - 1, y
659661
pla
660662
asl ; Check BIT 6
661663
bmi xit
@@ -674,25 +676,22 @@ xit: clc
674676
.proc pop_codep
675677
; Reads code position from loop stack
676678
ldy loop_sp
677-
dey
678-
dey
679-
dey
680-
sty loop_sp
681-
bmi loop_error
679+
beq loop_error
682680
; Check if loop type is correct
683-
retry: cmp loop_stk, y
684-
beq ok
681+
retry: cmp loop_stk-3, y
682+
beq get
685683
; If loop type is "ELSE", accept also "IF"
686684
cmp #LT_ELSE
687685
bne loop_error
688686
lda #LT_IF
689687
bne retry
690-
ok: ; Get saved position
691-
iny
692-
iny
693-
ldx loop_stk, y
688+
get: ; Get saved position
689+
dey
694690
dey
695-
lda loop_stk, y
691+
dey
692+
sty loop_sp
693+
ldx loop_stk+2, y
694+
lda loop_stk+1, y
696695
rtsclc: clc
697696
rts ; C is cleared on exit!
698697
.endproc
@@ -705,20 +704,12 @@ rtsclc: clc
705704
.proc check_loop_exit
706705
; Checks if there is an "EXIT" in the stack, and adjust target pointer
707706
ldy loop_sp
708-
dey
709-
dey
710-
dey
711-
bmi pop_codep::rtsclc
712-
lda loop_stk, y
707+
beq pop_codep::rtsclc
708+
lda loop_stk-3, y
713709
.assert LT_EXIT = 0, error, "LT_EXIT must be 0"
714710
bne pop_codep::rtsclc
715711
; Yes, pop and patch
716-
sty loop_sp
717-
iny
718-
iny
719-
ldx loop_stk, y
720-
dey
721-
lda loop_stk, y
712+
jsr pop_codep::get
722713
jsr patch_codep
723714
; And check for more possible EXIT's
724715
jmp check_loop_exit
@@ -728,41 +719,37 @@ rtsclc: clc
728719
; Search the loop stack for a loop (not "I"f nor "E"lse) and inserts a
729720
; patching code before
730721
ldy loop_sp
722+
tya
731723
retry: dey
732724
dey
733725
dey
734726
bmi loop_error
735-
lda loop_stk, y
727+
ldx loop_stk, y
736728
bmi retry ; FOR(2)/WHILE(2)/IF/ELSE/ELIF are > 127
737-
cmp #LT_PROC_DATA+1 ; PROC(1)/DATA
729+
cpx #LT_PROC_DATA+1 ; PROC(1)/DATA
738730
bcc loop_error
739731
ok:
740732
; Store slot
741733
sty loop_exit_comp
742734
; Check if enough stack
743-
ldx loop_sp
744-
inx
745-
inx
746-
inx
735+
adc #2
747736
bmi loop_error
748-
749737
; Keep new loop_sp in stack
750-
txa
751738
pha
739+
tay
752740

753741
; Move all stack 3 positions up
754742
move:
755-
dex
756-
lda loop_stk-3, x
757-
sta loop_stk, x
758-
cpx loop_exit_comp
743+
dey
744+
lda loop_stk-3, y
745+
sta loop_stk, y
746+
cpy loop_exit_comp
759747
bne move
760748

761749
; Store our new stack entry
762-
; X is the new slot
763-
stx loop_sp
750+
; Y is the new slot
764751
lda #LT_EXIT
765-
jsr push_codep
752+
jsr push_codep_y
766753

767754
; Restore new loop_sp
768755
pla
@@ -822,12 +809,9 @@ check_elif:
822809
jsr pop_patch_codep
823810
; Check and remove all ELIF targets
824811
ldy loop_sp
825-
dey
826-
dey
827-
dey
828-
bmi no_elif
812+
beq no_elif
829813
lda #LT_ELIF
830-
cmp loop_stk, y
814+
cmp loop_stk-3, y
831815
beq check_elif
832816
no_elif:
833817
clc
@@ -864,12 +848,12 @@ no_elif:
864848
; Pop the old position to patch (from IF)
865849
lda #LT_IF
866850
jsr pop_codep
867-
sta tmp1
851+
pha
868852
stx tmp1+1
869853
; Emit a jump to a new position (loop type ELIF/ELSE from code)
870854
jsr E_PUSH_LT
871855
; Parch current position + 2 (over jump)
872-
lda tmp1
856+
pla
873857
ldx tmp1+1
874858
bne patch_codep
875859
.endproc

0 commit comments

Comments
 (0)