Skip to content

Commit 31c5dfb

Browse files
authored
Merge pull request #412 from boriel/bugfix/O4_bad_is_required_check
Fix opt check for dependency
2 parents 5605768 + a054202 commit 31c5dfb

6 files changed

Lines changed: 85 additions & 3 deletions

File tree

src/arch/zx48k/optimizer/basicblock.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from .common import LABELS, JUMP_LABELS
1313
from .errors import OptimizerInvalidBasicBlockError, OptimizerError
1414
from .cpustate import CPUState
15+
from .patterns import RE_ID_OR_NUMBER
16+
1517
from ..peephole import evaluator
1618

1719
from . import helpers
@@ -377,6 +379,8 @@ def is_used(self, regs, i, top=None):
377379
if helpers.idx_args(regs[0][1:-1]) else []
378380

379381
rr = set(r16 + ix)
382+
mem_vars = set([] if rr else RE_ID_OR_NUMBER.findall(regs[0]))
383+
380384
for mem in self[i:top]: # For memory accesses only mark as NOT uses if it's overwritten
381385
if mem.inst == 'ld' and mem.opers[0] == regs[0]:
382386
return False
@@ -387,6 +391,9 @@ def is_used(self, regs, i, top=None):
387391
if rr and any(_ in r16 for _ in mem.destroys): # (hl) :: inc hl / (ix + n) :: inc ix
388392
return True
389393

394+
if mem.opers and mem_vars.intersection(RE_ID_OR_NUMBER.findall(mem.opers[-1])):
395+
return True
396+
390397
return True
391398

392399
regs = src.api.utils.flatten_list([helpers.single_registers(x) for x in regs]) # make a copy

src/arch/zx48k/optimizer/patterns.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@
3232

3333
# matches a pragma line
3434
RE_PRAGMA = re.compile(r'^#[ \t]?pragma[ \t]opt[ \t]')
35+
36+
# matches an ID or a number
37+
RE_ID_OR_NUMBER = re.compile(r'[0-9]+|[a-zA-Z_][a-zA-Z_0-9]*')

src/arch/zx48k/peephole/evaluator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ class Evaluator(object):
137137
|| (or)
138138
+ (addition or concatenation for strings)
139139
"""
140-
UNARY = UNARY
141-
BINARY = BINARY
140+
UNARY = dict(UNARY)
141+
BINARY = dict(BINARY)
142142

143143
def __init__(self, expression, unary=None, binary=None):
144144
""" Initializes the object parsing the expression and preparing it for its (later)

tests/arch/zx48k/optimizer/test_basicblock.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
import unittest
44
from src.arch.zx48k.optimizer import basicblock
55
from src.arch.zx48k import optimizer
6+
from src.arch.zx48k.peephole import evaluator
67

78

89
class TestBasicBlock(unittest.TestCase):
910
""" Tests basic blocks implementation
1011
"""
11-
def setUp(self):
12+
def setUp(self) -> None:
1213
self.blk = basicblock.BasicBlock([])
1314

15+
def tearDown(self) -> None:
16+
evaluator.Evaluator.UNARY = dict(evaluator.UNARY)
17+
1418
def test_block_partition_jp(self):
1519
code = """
1620
nop
@@ -157,3 +161,11 @@ def test_basic_block_clean_ld_hl(self):
157161
self.blk.code = [x for x in code.split('\n') if x.strip()]
158162
self.assertEqual(1, len(self.blk))
159163
self.assertEqual(['ld hl, (30000)'], self.blk.code)
164+
165+
def test_mempos_requires(self):
166+
code = """
167+
ld hl, (_k - 1)
168+
ld (_k), a
169+
"""
170+
self.blk.code = [x for x in code.split('\n') if x.strip()]
171+
self.assertTrue(self.blk.is_used(['(_k)'], 0))

tests/functional/opt4_due_0.asm

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
jp __MAIN_PROGRAM__
14+
ZXBASIC_USER_DATA:
15+
; Defines USER DATA Length in bytes
16+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
17+
.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN
18+
.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA
19+
_k:
20+
DEFB 05h
21+
_n:
22+
DEFB 00
23+
ZXBASIC_USER_DATA_END:
24+
__MAIN_PROGRAM__:
25+
ld a, (_k)
26+
xor 31
27+
ld (_k), a
28+
ld a, (_n)
29+
add a, a
30+
add a, a
31+
add a, a
32+
add a, a
33+
add a, a
34+
ld hl, (_k - 1)
35+
or h
36+
ld (_k), a
37+
ld bc, 0
38+
__END_PROGRAM:
39+
di
40+
ld hl, (__CALL_BACK__)
41+
ld sp, hl
42+
exx
43+
pop hl
44+
pop iy
45+
pop ix
46+
exx
47+
ei
48+
ret
49+
__CALL_BACK__:
50+
DEFW 0
51+
END

tests/functional/opt4_due_0.bas

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
DIM k as UByte = 5
4+
DIM n as UByte
5+
6+
k = k bXOR %11111
7+
k = (n << 5) bOR k
8+
9+

0 commit comments

Comments
 (0)