Skip to content

Commit e85caaa

Browse files
committed
Add optimizer template 103 and FLAGVAL
Remove redundant XOR A instructions when the value of A register is already 0. Also adds new optimizer primitive FLAGVAL
1 parent bc9d55b commit e85caaa

5 files changed

Lines changed: 81 additions & 1 deletion

File tree

arch/zx48k/optimizer/basicblock.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,10 @@ def optimize(self, patterns_list):
555555
code = self.code
556556
old_unary = dict(evaluator.Evaluator.UNARY)
557557
evaluator.Evaluator.UNARY['GVAL'] = lambda x: self.cpu.get(x)
558+
evaluator.Evaluator.UNARY['FLAGVAL'] = lambda x: {
559+
'c': str(self.cpu.C) if self.cpu.C is not None else helpers.new_tmp_val(),
560+
'z': str(self.cpu.Z) if self.cpu.Z is not None else helpers.new_tmp_val()
561+
}.get(x.lower(), helpers.new_tmp_val())
558562

559563
if api.config.OPTIONS.optimization.value > 3:
560564
regs, mems = self.guesses_initial_state_from_origin_blocks()

arch/zx48k/peephole/evaluator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
'GVAL': lambda x: helpers.new_tmp_val(), # To be updated in the O3 optimizer
5555
'IS_REQUIRED': lambda x: True, # by default always required
5656
'CTEST': lambda x: memcell.MemCell(x, 1).condition_flag, # condition test, if any. E.g. retz returns 'z'
57-
'NEEDS': lambda x: memcell.MemCell(x[0], 1).needs(x[1])
57+
'NEEDS': lambda x: memcell.MemCell(x[0], 1).needs(x[1]),
58+
'FLAGVAL': lambda x: helpers.new_tmp_val()
5859
}
5960

6061
# Binary operators
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
;; Removes useless XOR a
2+
3+
OLEVEL: 3
4+
OFLAG: 103
5+
6+
REPLACE {{
7+
xor a
8+
}}
9+
10+
IF {{
11+
(GVAL(a) == 0) && ((FLAGVAL(c) == 0 && FLAGVAL(z) == 1) || !IS_REQUIRED(f))
12+
}}
13+
14+
WITH {{
15+
}}

tests/functional/opt3_xorxor.asm

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
__LABEL0:
14+
__LABEL__looproom:
15+
xor a
16+
ld (_push), a
17+
ld (_suck), a
18+
ld a, 4
19+
ld (_paso), a
20+
jp __LABEL0
21+
__END_PROGRAM:
22+
di
23+
ld hl, (__CALL_BACK__)
24+
ld sp, hl
25+
exx
26+
pop hl
27+
pop iy
28+
pop ix
29+
exx
30+
ei
31+
ret
32+
__CALL_BACK__:
33+
DEFW 0
34+
35+
ZXBASIC_USER_DATA:
36+
_push:
37+
DEFB 00
38+
_suck:
39+
DEFB 00
40+
_paso:
41+
DEFB 00
42+
; Defines DATA END --> HEAP size is 0
43+
ZXBASIC_USER_DATA_END EQU ZXBASIC_MEM_HEAP
44+
; Defines USER DATA Length in bytes
45+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
46+
END

tests/functional/opt3_xorxor.bas

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
do
2+
3+
looproom:
4+
5+
push=0
6+
suck=0
7+
paso=4
8+
9+
loop
10+
11+
push=suck
12+
suck=paso
13+
push=push
14+

0 commit comments

Comments
 (0)