Skip to content

Commit d84a204

Browse files
authored
Merge pull request #353 from boriel/bugfix/wrong_constant_initalization
Fix local variable initalization
2 parents 94c3f6f + 1a913c5 commit d84a204

3 files changed

Lines changed: 110 additions & 2 deletions

File tree

arch/zx48k/backend/__init__.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import math
66
import re
7+
from typing import List
78

89
from . import errors
910
from .errors import InvalidICError as InvalidIC
@@ -217,6 +218,48 @@ def is_int_type(stype):
217218
return stype[0] in ('u', 'i')
218219

219220

221+
def get_bytes(elements: List[str]) -> List[str]:
222+
""" Returns a list a default set of bytes/words in hexadecimal
223+
(starting with an hex number) or literals (starting with #).
224+
Numeric values with more than 2 digits represents a WORD (2 bytes) value.
225+
E.g. '01' => 01h, '001' => 1, 0 bytes (0001h)
226+
Literal values starts with # (1 byte) or ## (2 bytes)
227+
E.g. '#label + 1' => (label + 1) & 0xFF
228+
'##(label + 1)' => (label + 1) & 0xFFFF
229+
"""
230+
output = []
231+
232+
for x in elements:
233+
if x.startswith('##'): # 2-byte literal
234+
output.append('({}) & 0xFF'.format(x[2:]))
235+
output.append('(({}) >> 8) & 0xFF'.format(x[2:]))
236+
continue
237+
238+
if x.startswith('#'): # 1-byte literal
239+
output.append('({}) & 0xFF'.format(x[1:]))
240+
continue
241+
242+
# must be an hex number
243+
assert RE_HEXA.match(x), 'expected an hex number, got "%s"' % x
244+
output.append('%02X' % int(x[-2:], 16))
245+
if len(x) > 2:
246+
output.append('%02X' % int(x[-4:-2:], 16))
247+
248+
return output
249+
250+
251+
def get_bytes_size(elements: List[str]) -> int:
252+
""" Defines a memory space with a default set of bytes/words in hexadecimal
253+
(starting with an hex number) or literals (starting with #).
254+
Numeric values with more than 2 digits represents a WORD (2 bytes) value.
255+
E.g. '01' => 01h, '001' => 1, 0 bytes (0001h)
256+
Literal values starts with # (1 byte) or ## (2 bytes)
257+
E.g. '#label + 1' => (label + 1) & 0xFF
258+
'##(label + 1)' => (label + 1) & 0xFFFF
259+
"""
260+
return len(get_bytes(elements))
261+
262+
220263
# ------------------------------------------------------------------
221264
# Typecast conversions
222265
# ------------------------------------------------------------------
@@ -559,7 +602,6 @@ def _lvard(ins):
559602
"""
560603
output = []
561604

562-
l = eval(ins.quad[2]) # List of bytes to push
563605
label = tmp_label()
564606
offset = int(ins.quad[1])
565607
tmp = list(ins.quad)
@@ -573,7 +615,7 @@ def _lvard(ins):
573615
output.append('add hl, bc')
574616
output.append('ex de, hl')
575617
output.append('ld hl, %s' % label)
576-
output.append('ld bc, %i' % len(l))
618+
output.append('ld bc, %i' % get_bytes_size(eval(tmp[2])))
577619
output.append('ldir')
578620

579621
return output

tests/functional/const7.asm

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
call _x
14+
ld hl, 0
15+
ld b, h
16+
ld c, l
17+
__END_PROGRAM:
18+
di
19+
ld hl, (__CALL_BACK__)
20+
ld sp, hl
21+
exx
22+
pop hl
23+
exx
24+
pop iy
25+
pop ix
26+
ei
27+
ret
28+
__CALL_BACK__:
29+
DEFW 0
30+
_x:
31+
push ix
32+
ld ix, 0
33+
add ix, sp
34+
ld hl, 0
35+
push hl
36+
push ix
37+
pop hl
38+
ld bc, -2
39+
add hl, bc
40+
ex de, hl
41+
ld hl, __LABEL0
42+
ld bc, 2
43+
ldir
44+
_x__leave:
45+
ld sp, ix
46+
pop ix
47+
ret
48+
ZXBASIC_USER_DATA:
49+
__LABEL0:
50+
DEFW 12345
51+
; Defines DATA END --> HEAP size is 0
52+
ZXBASIC_USER_DATA_END:
53+
; Defines USER DATA Length in bytes
54+
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
55+
END

tests/functional/const7.bas

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
CONST number as Uinteger = 12345
4+
5+
SUB x
6+
DIM a as UInteger = number
7+
END SUB
8+
9+
x
10+
11+

0 commit comments

Comments
 (0)