Skip to content

Commit bc9d55b

Browse files
committed
Jump optimization
Enhances jump / jr / call optimization whenever possible. JP A ... A: JP B translates JP A to JP B
1 parent 15bfcb0 commit bc9d55b

4 files changed

Lines changed: 136 additions & 4 deletions

File tree

arch/zx48k/optimizer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def optimize(initial_memory):
217217
continue
218218

219219
# The instruction that starts this block must be one of jr / jp
220-
first = block.get_first_non_label_instruction()
220+
first = block.get_next_exec_instruction()
221221
if first is None or first.inst not in ('jp', 'jr'):
222222
continue
223223

arch/zx48k/optimizer/basicblock.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,29 @@ def get_first_non_label_instruction(self):
495495
""" Returns the memcell of the given block, which is
496496
not a LABEL.
497497
"""
498-
for i in range(len(self)):
499-
if not self.mem[i].is_label:
500-
return self.mem[i]
498+
for mem in self:
499+
if not mem.is_label:
500+
return mem
501501

502502
return None
503503

504+
def get_next_exec_instruction(self):
505+
""" Return the first non label instruction to be executed, either
506+
in this block or in the following one. If there are more than one, return None.
507+
Also returns None if there is no instruction to be executed.
508+
"""
509+
result = self.get_first_non_label_instruction()
510+
blk = self
511+
512+
while result is None:
513+
if len(blk.goes_to) != 1:
514+
return None
515+
516+
blk = blk.goes_to[0]
517+
result = blk.get_first_non_label_instruction()
518+
519+
return result
520+
504521
def guesses_initial_state_from_origin_blocks(self):
505522
""" Returns two dictionaries (regs, memory) that contains the common values
506523
of the cpustates of all comes_from blocks

tests/functional/opt3_tolosob.asm

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
org 32768
2+
__START_PROGRAM:
3+
di
4+
push ix
5+
push iy
6+
exx
7+
push hl
8+
exx
9+
ld hl, 0
10+
add hl, sp
11+
ld (__CALL_BACK__), hl
12+
ei
13+
__LABEL__inicio:
14+
ld de, 0
15+
ld hl, (_toloTimer)
16+
or a
17+
sbc hl, de
18+
jp nz, __LABEL__inicio
19+
ld a, 1
20+
ld (_sobando), a
21+
sub 2
22+
jp nz, __LABEL3
23+
ld hl, (_toloTimer)
24+
inc hl
25+
ld a, (hl)
26+
sub 12
27+
jp nz, __LABEL5
28+
ld a, 3
29+
ld (_sobando), a
30+
jp __LABEL__inicio
31+
__LABEL5:
32+
__LABEL3:
33+
ld a, (_sobando)
34+
or a
35+
jp nz, __LABEL__inicio
36+
ld de, 10
37+
ld hl, (_toloTimer)
38+
or a
39+
sbc hl, de
40+
jp nz, __LABEL__inicio
41+
ld hl, (_toloStatus)
42+
ld a, (hl)
43+
and 2
44+
jp nz, __LABEL__inicio
45+
ld a, 1
46+
ld (_sobando), a
47+
__LABEL__pontolosobando:
48+
__LABEL11:
49+
__LABEL9:
50+
__LABEL7:
51+
__LABEL1:
52+
jp __LABEL__inicio
53+
__END_PROGRAM:
54+
di
55+
ld hl, (__CALL_BACK__)
56+
ld sp, hl
57+
exx
58+
pop hl
59+
pop iy
60+
pop ix
61+
exx
62+
ei
63+
ret
64+
__CALL_BACK__:
65+
DEFW 0
66+
#line 1 "eq16.asm"
67+
68+
__EQ16: ; Test if 16bit values HL == DE
69+
; Returns result in A: 0 = False, FF = True
70+
xor a ; Reset carry flag
71+
sbc hl, de
72+
ret nz
73+
inc a
74+
ret
75+
76+
#line 55 "opt3_tolosob.bas"
77+
78+
ZXBASIC_USER_DATA:
79+
_toloTimer:
80+
DEFB 00, 00
81+
_toloStatus:
82+
DEFB 00, 00
83+
_sobando:
84+
DEFB 00
85+
; Defines DATA END --> HEAP size is 0
86+
ZXBASIC_USER_DATA_END EQU ZXBASIC_MEM_HEAP
87+
; Defines USER DATA Length in bytes
88+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
89+
END

tests/functional/opt3_tolosob.bas

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
DIM toloTimer as UInteger
2+
DIM toloStatus as UInteger
3+
DIM toloLongSec, toloNSec, toloDelayAnim as UInteger
4+
DIM sobando as UByte
5+
6+
inicio:
7+
if toloTimer=0 then
8+
sobando = 1
9+
if sobando=2 then
10+
if peek (toloTimer+1)=12 then
11+
sobando=3
12+
goto pontolosobando
13+
end if
14+
end if
15+
16+
if sobando=0 then
17+
if toloTimer=10 then
18+
if peek(toloStatus) band %00000010=0 then
19+
sobando=1
20+
pontolosobando:
21+
end if
22+
end if
23+
end if
24+
end if
25+
goto inicio
26+

0 commit comments

Comments
 (0)