Skip to content

Commit 9de3c54

Browse files
authored
Merge pull request #542 from boriel/bugfix/wrong_opt_in_O4
Bugfix/wrong opt in o4
2 parents eec47b6 + 88ceb4c commit 9de3c54

8 files changed

Lines changed: 99 additions & 11 deletions

File tree

src/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
from src.api import errors # noqa
1414
from src.api import errmsg # noqa
1515
from src.api import utils # noqa
16+
from src.api import symboltable # noqa

src/api/symboltable.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,6 @@ def access_call(self, id_: str, lineno: int, scope=None, type_=None):
533533
entry.callable = False
534534
return entry
535535

536-
# Mangled name (functions always has _name as mangled)
537-
# entry.mangled = '_%s' % entry.name
538-
# entry.callable = True # HINT: must be true already
539536
return entry
540537

541538
def access_label(self, id_: str, lineno: int, scope: Optional[Scope] = None):
@@ -691,7 +688,7 @@ def declare_label(self, id_: str, lineno: int) -> Optional[SymbolLABEL]:
691688
# Just the label, because it starts with '.' so it's a root-global label
692689
entry.mangled = f'{id_}'
693690
else:
694-
# TODO: This shouln't be needed (but still is). Need investigation
691+
# TODO: This shouldn't be needed (but still is). Need investigation
695692
entry.mangled = f'{global_.LABELS_NAMESPACE}.{symbols.LABEL.prefix}{entry.name}'
696693

697694
entry.is_line_number = isinstance(id1, int)
@@ -720,7 +717,7 @@ def declare_param(self, id_: str, lineno: int, type_=None, is_array=False) -> Op
720717
entry = self.declare(id_, lineno, symbols.PARAMDECL(id_, lineno, type_))
721718

722719
if entry is None:
723-
return
720+
return None
724721

725722
entry.declared = True
726723
if entry.type_.implicit:

src/arch/zx48k/optimizer/basicblock.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,18 +381,22 @@ def is_used(self, regs, i, top=None):
381381
top -= 1
382382

383383
if regs and regs[0][0] == '(' and regs[0][-1] == ')': # A memory address
384-
r16 = helpers.single_registers(regs[0][1:-1])\
384+
r16 = helpers.single_registers(regs[0][1:-1]) \
385385
if helpers.is_16bit_oper_register(regs[0][1:-1]) else []
386386
ix = helpers.single_registers(helpers.idx_args(regs[0][1:-1])[0]) \
387387
if helpers.idx_args(regs[0][1:-1]) else []
388388

389389
rr = set(r16 + ix)
390390
mem_vars = set([] if rr else RE_ID_OR_NUMBER.findall(regs[0]))
391391

392-
for mem in self[i:top]: # For memory accesses only mark as NOT uses if it's overwritten
392+
for mem in self[i:top]: # For memory accesses only mark as NOT used if it's overwritten
393393
if mem.inst == 'ld' and mem.opers[0] == regs[0]:
394394
return False
395395

396+
# And, Or, Xor uses both operands
397+
if mem.inst in {'and', 'or', 'xor'} and mem.opers[0] == regs[0]:
398+
return True
399+
396400
if mem.opers and mem.opers[-1] == regs[0]:
397401
return True
398402

src/arch/zx48k/peephole/opts/109_o4_ld_mem_op_ld_mem.opt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
;; Removes useless LD's
2-
;; Tries to guess the value of the 1st and 2nd operands.
3-
;; If they're the same, this LD is useless
1+
;; Removes useless operation
2+
;; Tries to guess if the result of the operation LD, OR, AND, XOR, ADC, ADD, SBC is later used
43

54
OLEVEL: 4
65
OFLAG: 109

src/zxbc/zxbparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ def p_var_decl_at(p):
687687
if entry is None:
688688
return
689689

690-
if p[5].token in 'CONST':
690+
if p[5].token == 'CONST':
691691
tmp = p[5].expr
692692
if tmp.token == 'UNARY' and tmp.operator == 'ADDRESS': # Must be an ID
693693
if tmp.operand.token in ('VAR', 'LABEL'):

tests/arch/zx48k/optimizer/test_basicblock.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,27 @@ def test_mempos_requires(self):
169169
"""
170170
self.blk.code = [x for x in code.split('\n') if x.strip()]
171171
self.assertTrue(self.blk.is_used(['(_k)'], 0))
172+
173+
def test_is_used_or_ix(self):
174+
code = """
175+
or (ix-1)
176+
ld (ix-1), a
177+
"""
178+
self.blk.code = [x for x in code.split('\n') if x.strip()]
179+
assert self.blk.is_used(['(ix-1)'], 0)
180+
181+
def test_is_used_and_ix(self):
182+
code = """
183+
and (ix-1)
184+
ld (ix-1), a
185+
"""
186+
self.blk.code = [x for x in code.split('\n') if x.strip()]
187+
assert self.blk.is_used(['(ix-1)'], 0)
188+
189+
def test_is_used_xor_ix(self):
190+
code = """
191+
xor (ix-1)
192+
ld (ix-1), a
193+
"""
194+
self.blk.code = [x for x in code.split('\n') if x.strip()]
195+
assert self.blk.is_used(['(ix-1)'], 0)

tests/functional/opt4_due0.asm

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
org 32768
2+
.core.__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 (.core.__CALL_BACK__), hl
12+
ei
13+
jp .core.__MAIN_PROGRAM__
14+
.core.__CALL_BACK__:
15+
DEFW 0
16+
.core.ZXBASIC_USER_DATA:
17+
; Defines USER DATA Length in bytes
18+
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
19+
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
20+
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
21+
.core.ZXBASIC_USER_DATA_END:
22+
.core.__MAIN_PROGRAM__:
23+
call _Demo_PutPlayer
24+
ld b, h
25+
ld c, l
26+
.core.__END_PROGRAM:
27+
di
28+
ld hl, (.core.__CALL_BACK__)
29+
ld sp, hl
30+
exx
31+
pop hl
32+
pop iy
33+
pop ix
34+
exx
35+
ei
36+
ret
37+
_Demo_PutPlayer:
38+
push ix
39+
ld ix, 0
40+
add ix, sp
41+
ld hl, 0
42+
push hl
43+
ld a, (ix-1)
44+
and 199
45+
ld (ix-1), a
46+
ld a, (ix-2)
47+
or (ix-1)
48+
ld (ix-1), a
49+
_Demo_PutPlayer__leave:
50+
ld sp, ix
51+
pop ix
52+
ret
53+
;; --- end of user code ---
54+
END

tests/functional/opt4_due0.bas

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sub Demo_PutPlayer()
2+
dim atrSprite, atrGame as ubyte
3+
4+
atrSprite=atrSprite bAND %11000111
5+
atrSprite=atrGame bOR atrSprite
6+
end sub
7+
8+
Demo_PutPlayer()
9+

0 commit comments

Comments
 (0)