Skip to content

Commit a897d60

Browse files
authored
Merge pull request #393 from boriel/bugfix/bad_arrangement
Bugfix/bad arrangement
2 parents 37e2c64 + 919531e commit a897d60

8 files changed

Lines changed: 201 additions & 12 deletions

File tree

api/optimize.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ToVisit(object):
2121
""" Used just to signal an object to be
2222
traversed.
2323
"""
24+
2425
def __init__(self, obj):
2526
self.obj = obj
2627

@@ -155,7 +156,14 @@ def visit_FUNCTION(self, node):
155156
def visit_LET(self, node):
156157
if self.O_LEVEL > 1 and not node.children[0].accessed:
157158
warning_not_used(node.children[0].lineno, node.children[0].name)
158-
yield symbols.BLOCK(*list(self.filter_inorder(node.children[1], lambda x: isinstance(x, symbols.CALL))))
159+
block = symbols.BLOCK(*[
160+
symbols.CALL(x.entry, x.args, x.lineno) for x in self.filter_inorder(
161+
node.children[1],
162+
lambda x: isinstance(x, symbols.FUNCCALL),
163+
lambda x: not isinstance(x, symbols.FUNCTION)
164+
)
165+
])
166+
yield block
159167
else:
160168
yield (yield self.generic_visit(node))
161169

ast_/ast.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ def _visit(self, node):
5454
def generic_visit(node: Ast):
5555
raise RuntimeError("No {}() method defined".format('visit_' + node.token))
5656

57-
def filter_inorder(self, node, filter_func: Callable[[Any], bool]):
58-
""" Visit the tree inorder, but only those that return true for filter
57+
def filter_inorder(self,
58+
node,
59+
filter_func: Callable[[Any], bool],
60+
child_selector: Callable[[Ast], bool] = lambda x: True):
61+
""" Visit the tree inorder, but only those that return true for filter_func and visiting children which
62+
return true for child_selector.
5963
"""
6064
visited = set()
6165
stack = [node]
@@ -66,5 +70,5 @@ def filter_inorder(self, node, filter_func: Callable[[Any], bool]):
6670
visited.add(node)
6771
if filter_func(node):
6872
yield self.visit(node)
69-
elif isinstance(node, Ast):
73+
if isinstance(node, Ast) and child_selector(node):
7074
stack.extend(node.children[::-1])

symbols/call.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# the GNU General License
1010
# ----------------------------------------------------------------------
1111

12+
from typing import Iterable
13+
1214
import api.global_ as gl
1315
from api.check import check_call_arguments
1416
from api.constants import CLASS
@@ -33,7 +35,7 @@ class SymbolCALL(Symbol):
3335
lineno: source code line where this call was made
3436
"""
3537

36-
def __init__(self, entry: SymbolFUNCTION, arglist, lineno):
38+
def __init__(self, entry: SymbolFUNCTION, arglist: Iterable[SymbolARGUMENT], lineno: int):
3739
super(SymbolCALL, self).__init__()
3840
assert isinstance(lineno, int)
3941
assert all(isinstance(x, SymbolARGUMENT) for x in arglist)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
ZXBASIC_USER_DATA_END:
20+
__MAIN_PROGRAM__:
21+
ld hl, 0
22+
ld b, h
23+
ld c, l
24+
__END_PROGRAM:
25+
di
26+
ld hl, (__CALL_BACK__)
27+
ld sp, hl
28+
exx
29+
pop hl
30+
pop iy
31+
pop ix
32+
exx
33+
ei
34+
ret
35+
__CALL_BACK__:
36+
DEFW 0
37+
ld hl, 0
38+
ld b, h
39+
ld c, l
40+
jp __END_PROGRAM
41+
_MultiKeys:
42+
push ix
43+
ld ix, 0
44+
add ix, sp
45+
ld a, (ix+5)
46+
_MultiKeys__leave:
47+
ld sp, ix
48+
pop ix
49+
exx
50+
pop hl
51+
ex (sp), hl
52+
exx
53+
ret
54+
_mainRoom:
55+
xor a
56+
push af
57+
call _MultiKeys
58+
or a
59+
jp z, __LABEL1
60+
ld a, 1
61+
ld (0), a
62+
__LABEL1:
63+
ld a, 1
64+
push af
65+
call _MultiKeys
66+
or a
67+
jp z, __LABEL3
68+
xor a
69+
ld (1), a
70+
__LABEL3:
71+
ld a, 2
72+
push af
73+
call _MultiKeys
74+
or a
75+
jp z, __LABEL5
76+
xor a
77+
ld (2), a
78+
__LABEL5:
79+
_mainRoom__leave:
80+
ret
81+
END
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
function MultiKeys(x as Ubyte) as Ubyte
3+
return x
4+
end function
5+
6+
7+
sub fastcall mainRoom()
8+
IF MultiKeys(0) then
9+
poke 0,1
10+
end if
11+
12+
IF MultiKeys(1) then
13+
poke 1,0
14+
end if
15+
16+
if MultiKeys(2) then
17+
poke 2,0
18+
end if
19+
20+
end sub
21+
22+
end
23+
24+
DIM dummy as Uinteger
25+
dummy=@mainRoom

tests/functional/opt2_unused_var.asm

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,11 @@ _Check:
6363
ld a, 5
6464
push af
6565
call _TestPrint
66-
push bc
67-
push de
68-
push af
6966
ld a, 11
7067
push af
7168
ld a, 6
7269
push af
7370
call _TestPrint
74-
push bc
75-
push de
76-
push af
7771
_Check__leave:
7872
ld sp, ix
7973
pop ix
@@ -84,5 +78,5 @@ __PAUSE:
8478
ld b, h
8579
ld c, l
8680
jp 1F3Dh ; PAUSE_1
87-
#line 62 "opt2_unused_var.bas"
81+
#line 56 "opt2_unused_var.bas"
8882
END
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
_a:
20+
DEFB 00
21+
ZXBASIC_USER_DATA_END:
22+
__MAIN_PROGRAM__:
23+
xor a
24+
push af
25+
call _test
26+
ld (_a), a
27+
ld hl, 0
28+
ld b, h
29+
ld c, l
30+
__END_PROGRAM:
31+
di
32+
ld hl, (__CALL_BACK__)
33+
ld sp, hl
34+
exx
35+
pop hl
36+
exx
37+
pop iy
38+
pop ix
39+
ei
40+
ret
41+
__CALL_BACK__:
42+
DEFW 0
43+
_test:
44+
push ix
45+
ld ix, 0
46+
add ix, sp
47+
ld a, (ix+5)
48+
push af
49+
ld h, 10
50+
pop af
51+
cp h
52+
jp nc, __LABEL1
53+
ld a, (ix+5)
54+
jp _test__leave
55+
__LABEL1:
56+
ld a, (ix+5)
57+
_test__leave:
58+
ld sp, ix
59+
pop ix
60+
exx
61+
pop hl
62+
ex (sp), hl
63+
exx
64+
ret
65+
END
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DIM a as UByte
2+
FUNCTION test(a as UByte) as UByte
3+
IF a < 10 THEN
4+
RETURN a
5+
END IF
6+
RETURN a
7+
END FUNCTION
8+
9+
LET a = test(0)
10+

0 commit comments

Comments
 (0)