Skip to content

Commit 406f93f

Browse files
authored
Merge pull request #305 from boriel/bugfix/addr_of_local_array_element
Bugfix/addr of local array element
2 parents d0674b0 + f2f8a98 commit 406f93f

File tree

14 files changed

+536
-60
lines changed

14 files changed

+536
-60
lines changed

api/optimize.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import api.errmsg
77
from api.errmsg import warning
88
import api.check as chk
9-
from api.constants import TYPE
9+
from api.constants import TYPE, SCOPE
1010
import api.global_ as gl
1111
import symbols
1212
import types
@@ -85,14 +85,14 @@ def visit_ADDRESS(self, node):
8585
if node.operand.token != 'ARRAYACCESS':
8686
if not chk.is_dynamic(node.operand):
8787
node = symbols.CONST(node, node.lineno)
88-
elif node.operand.offset is not None: # A constant access. Calculate offset
89-
node = symbols.BINARY.make_node('PLUS',
90-
symbols.UNARY('ADDRESS', node.operand.entry, node.lineno,
91-
type_=self.TYPE(gl.PTR_TYPE)),
92-
symbols.NUMBER(node.operand.offset, lineno=node.operand.lineno,
93-
type_=self.TYPE(gl.PTR_TYPE)),
94-
lineno=node.lineno, func=lambda x, y: x + y
95-
)
88+
elif node.operand.offset is not None: # A constant access
89+
if node.operand.scope == SCOPE.global_: # Calculate offset if global variable
90+
node = symbols.BINARY.make_node(
91+
'PLUS',
92+
symbols.UNARY('ADDRESS', node.operand.entry, node.lineno, type_=self.TYPE(gl.PTR_TYPE)),
93+
symbols.NUMBER(node.operand.offset, lineno=node.operand.lineno, type_=self.TYPE(gl.PTR_TYPE)),
94+
lineno=node.lineno, func=lambda x, y: x + y
95+
)
9696
yield node
9797

9898
def visit_BINARY(self, node):

arch/zx48k/backend/__parray.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# This module contains local array (both parameters and
1010
# comparation intermediate-code traductions
1111
# --------------------------------------------------------------
12-
12+
from api import fp
1313
from .__common import REQUIRES
1414
from .__float import _fpush
1515
from .__f16 import f16
@@ -48,19 +48,19 @@ def _paddr(offset):
4848

4949

5050
def _paaddr(ins):
51-
''' Loads address of an array element into the stack
52-
'''
51+
""" Loads address of an array element into the stack
52+
"""
5353
output = _paddr(ins.quad[2])
5454
output.append('push hl')
5555

5656
return output
5757

5858

5959
def _paload8(ins):
60-
''' Loads an 8 bit value from a memory address
60+
""" Loads an 8 bit value from a memory address
6161
If 2nd arg. start with '*', it is always treated as
6262
an indirect value.
63-
'''
63+
"""
6464
output = _paddr(ins.quad[2])
6565
output.append('ld a, (hl)')
6666
output.append('push af')
@@ -69,10 +69,10 @@ def _paload8(ins):
6969

7070

7171
def _paload16(ins):
72-
''' Loads a 16 bit value from a memory address
72+
""" Loads a 16 bit value from a memory address
7373
If 2nd arg. start with '*', it is always treated as
7474
an indirect value.
75-
'''
75+
"""
7676
output = _paddr(ins.quad[2])
7777

7878
output.append('ld e, (hl)')
@@ -85,10 +85,10 @@ def _paload16(ins):
8585

8686

8787
def _paload32(ins):
88-
''' Load a 32 bit value from a memory address
88+
""" Loads a 32 bit value from a memory address
8989
If 2nd arg. start with '*', it is always treated as
9090
an indirect value.
91-
'''
91+
"""
9292
output = _paddr(ins.quad[2])
9393

9494
output.append('call __ILOAD32')
@@ -101,10 +101,10 @@ def _paload32(ins):
101101

102102

103103
def _paloadf(ins):
104-
''' Loads a floating point value from a memory address.
104+
""" Loads a floating point value from a memory address.
105105
If 2nd arg. start with '*', it is always treated as
106106
an indirect value.
107-
'''
107+
"""
108108
output = _paddr(ins.quad[2])
109109
output.append('call __ILOADF')
110110
output.extend(_fpush())
@@ -115,8 +115,8 @@ def _paloadf(ins):
115115

116116

117117
def _paloadstr(ins):
118-
''' Loads a string value from a memory address.
119-
'''
118+
""" Loads a string value from a memory address.
119+
"""
120120
output = _paddr(ins.quad[2])
121121

122122
output.append('call __ILOADSTR')
@@ -127,11 +127,11 @@ def _paloadstr(ins):
127127

128128

129129
def _pastore8(ins):
130-
''' Stores 2º operand content into address of 1st operand.
130+
""" Stores 2º operand content into address of 1st operand.
131131
1st operand is an array element. Dimensions are pushed into the
132132
stack.
133133
Use '*' for indirect store on 1st operand (A pointer to an array)
134-
'''
134+
"""
135135
output = _paddr(ins.quad[1])
136136

137137
value = ins.quad[2]
@@ -157,10 +157,10 @@ def _pastore8(ins):
157157

158158

159159
def _pastore16(ins):
160-
''' Stores 2º operand content into address of 1st operand.
160+
""" Stores 2º operand content into address of 1st operand.
161161
store16 a, x => *(&a) = x
162162
Use '*' for indirect store on 1st operand.
163-
'''
163+
"""
164164
output = _paddr(ins.quad[1])
165165

166166
value = ins.quad[2]
@@ -188,9 +188,9 @@ def _pastore16(ins):
188188

189189

190190
def _pastore32(ins):
191-
''' Stores 2º operand content into address of 1st operand.
191+
""" Stores 2º operand content into address of 1st operand.
192192
store16 a, x => *(&a) = x
193-
'''
193+
"""
194194
output = _paddr(ins.quad[1])
195195

196196
value = ins.quad[2]
@@ -224,9 +224,9 @@ def _pastore32(ins):
224224

225225

226226
def _pastoref16(ins):
227-
''' Stores 2º operand content into address of 1st operand.
227+
""" Stores 2º operand content into address of 1st operand.
228228
storef16 a, x => *(&a) = x
229-
'''
229+
"""
230230
output = _paddr(ins.quad[1])
231231

232232
value = ins.quad[2]
@@ -261,8 +261,8 @@ def _pastoref16(ins):
261261

262262

263263
def _pastoref(ins):
264-
''' Stores a floating point value into a memory address.
265-
'''
264+
""" Stores a floating point value into a memory address.
265+
"""
266266
output = _paddr(ins.quad[1])
267267

268268
value = ins.quad[2]
@@ -274,7 +274,7 @@ def _pastoref(ins):
274274

275275
try:
276276
if indirect:
277-
value = int(value) & 0xFFFF # Inmediate?
277+
value = int(value) & 0xFFFF # Immediate?
278278
output.append('push hl')
279279
output.append('ld hl, %i' % value)
280280
output.append('call __ILOADF')
@@ -284,8 +284,8 @@ def _pastoref(ins):
284284
output.append('pop hl') # Recovers pointer
285285
REQUIRES.add('iloadf.asm')
286286
else:
287-
value = float(value) # Inmediate?
288-
C, DE, HL = fp.immediate_float(value) # noqa TODO: it will fail
287+
value = float(value) # Immediate?
288+
C, DE, HL = fp.immediate_float(value)
289289
output.append('ld a, %s' % C)
290290
output.append('ld de, %s' % DE)
291291
output.append('ld bc, %s' % HL)
@@ -303,11 +303,11 @@ def _pastoref(ins):
303303

304304

305305
def _pastorestr(ins):
306-
''' Stores a string value into a memory address.
306+
""" Stores a string value into a memory address.
307307
It copies content of 2nd operand (string), into 1st, reallocating
308308
dynamic memory for the 1st str. These instruction DOES ALLOW
309-
inmediate strings for the 2nd parameter, starting with '#'.
310-
'''
309+
immediate strings for the 2nd parameter, starting with '#'.
310+
"""
311311
output = _paddr(ins.quad[1])
312312
temporal = False
313313
value = ins.quad[2]

arch/zx48k/backend/__pload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# (a.k.a. Boriel, http://www.boriel.com)
88
#
99
# This module contains parameter load
10-
# intermediate-code traductions
10+
# intermediate-code translations
1111
# --------------------------------------------------------------
1212

1313

@@ -51,7 +51,7 @@ def _paddr(ins):
5151

5252
def _pload(offset, size):
5353
""" Generic parameter loading.
54-
Emmits output code for setting IX at the right location.
54+
Emits output code for setting IX at the right location.
5555
size = Number of bytes to load:
5656
1 => 8 bit value
5757
2 => 16 bit value / string

arch/zx48k/translator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,16 +1145,16 @@ def visit_BNOT(self, node):
11451145
self.ic_bnot(node.operand.type_, node.t, node.operand.t)
11461146

11471147
def visit_ADDRESS(self, node):
1148-
scope = node.children[0].scope
1149-
if node.children[0].token == 'ARRAYACCESS':
1150-
yield node.children[0]
1148+
scope = node.operand.scope
1149+
if node.operand.token == 'ARRAYACCESS':
1150+
yield node.operand
11511151
# Address of an array element.
11521152
if scope == SCOPE.global_:
1153-
self.ic_aaddr(node.t, node.children[0].entry.mangled)
1153+
self.ic_aaddr(node.t, node.operand.entry.mangled)
11541154
elif scope == SCOPE.parameter:
1155-
self.ic_paaddr(node.t, node.children[0].entry.offset)
1155+
self.ic_paaddr(node.t, '*{}'.format(node.operand.entry.offset))
11561156
elif scope == SCOPE.local:
1157-
self.ic_paaddr(node.t, -node.children[0].entry.offset)
1157+
self.ic_paaddr(node.t, -node.operand.entry.offset)
11581158
else: # It's a scalar variable
11591159
if scope == SCOPE.global_:
11601160
self.ic_load(node.type_, node.t, '#' + node.operand.t)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
ld hl, _aglobal.__DATA__
14+
ld de, 4
15+
add hl, de
16+
push hl
17+
ld a, 99
18+
pop hl
19+
ld (hl), a
20+
ld hl, 0
21+
ld b, h
22+
ld c, l
23+
__END_PROGRAM:
24+
di
25+
ld hl, (__CALL_BACK__)
26+
ld sp, hl
27+
exx
28+
pop hl
29+
exx
30+
pop iy
31+
pop ix
32+
ei
33+
ret
34+
__CALL_BACK__:
35+
DEFW 0
36+
ZXBASIC_USER_DATA:
37+
_aglobal:
38+
DEFW __LABEL0
39+
_aglobal.__DATA__.__PTR__:
40+
DEFW _aglobal.__DATA__
41+
_aglobal.__DATA__:
42+
DEFB 00h
43+
DEFB 01h
44+
DEFB 02h
45+
DEFB 03h
46+
DEFB 04h
47+
DEFB 05h
48+
DEFB 06h
49+
DEFB 07h
50+
DEFB 08h
51+
__LABEL0:
52+
DEFW 0001h
53+
DEFW 0003h
54+
DEFB 01h
55+
; Defines DATA END --> HEAP size is 0
56+
ZXBASIC_USER_DATA_END:
57+
; Defines USER DATA Length in bytes
58+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
59+
END
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
REM test @arr(x, y) for GLOBAL scope
2+
DIM aglobal(2, 2) as UByte => {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}
3+
POKE @aglobal(1, 1), 99
4+

0 commit comments

Comments
 (0)