Skip to content

Commit 6d195e1

Browse files
authored
Merge pull request #514 from boriel/bugfix/missing_label_when_accessing_global_array
Bugfix/missing label when accessing global array
2 parents afb4c16 + 85ce194 commit 6d195e1

7 files changed

Lines changed: 159 additions & 3 deletions

File tree

src/api/optimize.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,17 @@ def visit_BLOCK(self, node):
159159
class FunctionGraphVisitor(UniqueVisitor):
160160
""" Mark FUNCALLS
161161
"""
162+
def _get_calls_from_children(self, node):
163+
return [
164+
symbol
165+
for symbol in self.filter_inorder(node, lambda x: isinstance(x, (symbols.FUNCCALL, symbols.CALL)))
166+
if not isinstance(symbol, symbols.ARRAYACCESS)
167+
]
168+
162169
def _set_children_as_accessed(self, node: symbols.SYMBOL):
163170
parent = node.get_parent(symbols.FUNCDECL)
164171
if parent is None: # Global scope?
165-
for symbol in self.filter_inorder(node, lambda x: isinstance(x, (symbols.FUNCCALL, symbols.CALL))):
172+
for symbol in self._get_calls_from_children(node):
166173
symbol.entry.accessed = True
167174

168175
def visit_FUNCCALL(self, node: symbols.SYMBOL):
@@ -175,7 +182,7 @@ def visit_CALL(self, node: symbols.SYMBOL):
175182

176183
def visit_FUNCDECL(self, node: symbols.SYMBOL):
177184
if node.entry.accessed:
178-
for symbol in self.filter_inorder(node, lambda x: isinstance(x, (symbols.FUNCCALL, symbols.CALL))):
185+
for symbol in self._get_calls_from_children(node):
179186
symbol.entry.accessed = True
180187

181188
yield node
@@ -277,6 +284,21 @@ def visit_LET(self, node):
277284
else:
278285
yield (yield self.generic_visit(node))
279286

287+
def visit_LETARRAY(self, node):
288+
lvalue = node.args[0].entry
289+
if self.O_LEVEL > 1 and not lvalue.accessed:
290+
warning_not_used(lvalue.lineno, lvalue.name, fname=lvalue.filename)
291+
block = symbols.BLOCK(*[
292+
symbols.CALL(x.entry, x.args, x.lineno, lvalue.filename) for x in self.filter_inorder(
293+
node.children[1],
294+
lambda x: isinstance(x, symbols.FUNCCALL),
295+
lambda x: not isinstance(x, symbols.FUNCTION)
296+
)
297+
])
298+
yield block
299+
else:
300+
yield (yield self.generic_visit(node))
301+
280302
def visit_LETSUBSTR(self, node):
281303
if self.O_LEVEL > 1 and not node.children[0].accessed:
282304
errmsg.warning_not_used(node.children[0].lineno, node.children[0].name)

src/arch/zx48k/backend/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
# Whether to use the FunctionExit scheme
127127
FLAG_use_function_exit = False
128128

129-
# Whether an 'end' has already been emmitted
129+
# Whether an 'end' has already been emitted
130130
FLAG_end_emitted = False
131131

132132
# Default code ORG
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
__CALL_BACK__:
15+
DEFW 0
16+
ZXBASIC_USER_DATA:
17+
; Defines USER DATA Length in bytes
18+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
19+
.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN
20+
.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA
21+
ZXBASIC_USER_DATA_END:
22+
__MAIN_PROGRAM__:
23+
call _Init
24+
ld hl, 0
25+
ld b, h
26+
ld c, l
27+
__END_PROGRAM:
28+
di
29+
ld hl, (__CALL_BACK__)
30+
ld sp, hl
31+
exx
32+
pop hl
33+
pop iy
34+
pop ix
35+
exx
36+
ei
37+
ret
38+
_Init:
39+
push ix
40+
ld ix, 0
41+
add ix, sp
42+
ld hl, 0
43+
push hl
44+
inc sp
45+
_Init__leave:
46+
ld sp, ix
47+
pop ix
48+
ret
49+
;; --- end of user code ---
50+
END
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
DIM myArray(1 to 6) as ubyte
2+
3+
SUB Init()
4+
DIM i as ubyte
5+
myArray(i) = 0
6+
END SUB
7+
8+
Init()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
__CALL_BACK__:
15+
DEFW 0
16+
ZXBASIC_USER_DATA:
17+
; Defines USER DATA Length in bytes
18+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
19+
.__LABEL__.ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_LEN
20+
.__LABEL__.ZXBASIC_USER_DATA EQU ZXBASIC_USER_DATA
21+
_myArray2:
22+
DEFW __LABEL0
23+
_myArray2.__DATA__.__PTR__:
24+
DEFW _myArray2.__DATA__
25+
_myArray2.__DATA__:
26+
DEFB 00h
27+
DEFB 00h
28+
DEFB 00h
29+
DEFB 00h
30+
DEFB 00h
31+
DEFB 00h
32+
__LABEL0:
33+
DEFW 0000h
34+
DEFB 01h
35+
ZXBASIC_USER_DATA_END:
36+
__MAIN_PROGRAM__:
37+
call _Init
38+
ld hl, 0
39+
ld b, h
40+
ld c, l
41+
__END_PROGRAM:
42+
di
43+
ld hl, (__CALL_BACK__)
44+
ld sp, hl
45+
exx
46+
pop hl
47+
pop iy
48+
pop ix
49+
exx
50+
ei
51+
ret
52+
_Init:
53+
push ix
54+
ld ix, 0
55+
add ix, sp
56+
ld hl, 0
57+
push hl
58+
inc sp
59+
_Init__leave:
60+
ld sp, ix
61+
pop ix
62+
ret
63+
;; --- end of user code ---
64+
END
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DIM myArray(1 to 6) as ubyte
2+
DIM myArray2(1 to 6) as Ubyte
3+
4+
5+
SUB Init()
6+
DIM i as ubyte
7+
myArray(i) = 2 * myArray2(i)
8+
END SUB
9+
10+
Init()

tests/functional/test_errmsg.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ dim_at_init_err.bas:4: error: Syntax Error. Unexpected token 'AT' <AT>
200200
bad_pragma.bas:2: warning: Ignoring unknown pragma 'BAD_PRAGMA'
201201
bad_pragma.bas:4: warning: Ignoring unknown pragma 'BAD_PRAGMA'
202202
bad_pragma.bas:6: warning: Ignoring unknown pragma 'BAD_PRAGMA'
203+
>>> process_file('opt2_global_array2.bas')
204+
opt2_global_array2.bas:1: warning: Variable 'myArray' is never used
203205

204206
# Test warning silencing
205207
>>> process_file('mcleod3.bas', ['-S', '-q', '-O --expect-warnings=2'])

0 commit comments

Comments
 (0)