Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions arch/zx48k/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import math
import re
from typing import List

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


def get_bytes(elements: List[str]) -> List[str]:
""" Returns a list a default set of bytes/words in hexadecimal
(starting with an hex number) or literals (starting with #).
Numeric values with more than 2 digits represents a WORD (2 bytes) value.
E.g. '01' => 01h, '001' => 1, 0 bytes (0001h)
Literal values starts with # (1 byte) or ## (2 bytes)
E.g. '#label + 1' => (label + 1) & 0xFF
'##(label + 1)' => (label + 1) & 0xFFFF
"""
output = []

for x in elements:
if x.startswith('##'): # 2-byte literal
output.append('({}) & 0xFF'.format(x[2:]))
output.append('(({}) >> 8) & 0xFF'.format(x[2:]))
continue

if x.startswith('#'): # 1-byte literal
output.append('({}) & 0xFF'.format(x[1:]))
continue

# must be an hex number
assert RE_HEXA.match(x), 'expected an hex number, got "%s"' % x
output.append('%02X' % int(x[-2:], 16))
if len(x) > 2:
output.append('%02X' % int(x[-4:-2:], 16))

return output


def get_bytes_size(elements: List[str]) -> int:
""" Defines a memory space with a default set of bytes/words in hexadecimal
(starting with an hex number) or literals (starting with #).
Numeric values with more than 2 digits represents a WORD (2 bytes) value.
E.g. '01' => 01h, '001' => 1, 0 bytes (0001h)
Literal values starts with # (1 byte) or ## (2 bytes)
E.g. '#label + 1' => (label + 1) & 0xFF
'##(label + 1)' => (label + 1) & 0xFFFF
"""
return len(get_bytes(elements))


# ------------------------------------------------------------------
# Typecast conversions
# ------------------------------------------------------------------
Expand Down Expand Up @@ -559,7 +602,6 @@ def _lvard(ins):
"""
output = []

l = eval(ins.quad[2]) # List of bytes to push
label = tmp_label()
offset = int(ins.quad[1])
tmp = list(ins.quad)
Expand All @@ -573,7 +615,7 @@ def _lvard(ins):
output.append('add hl, bc')
output.append('ex de, hl')
output.append('ld hl, %s' % label)
output.append('ld bc, %i' % len(l))
output.append('ld bc, %i' % get_bytes_size(eval(tmp[2])))
output.append('ldir')

return output
Expand Down
55 changes: 55 additions & 0 deletions tests/functional/const7.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
org 32768
__START_PROGRAM:
di
push ix
push iy
exx
push hl
exx
ld hl, 0
add hl, sp
ld (__CALL_BACK__), hl
ei
call _x
ld hl, 0
ld b, h
ld c, l
__END_PROGRAM:
di
ld hl, (__CALL_BACK__)
ld sp, hl
exx
pop hl
exx
pop iy
pop ix
ei
ret
__CALL_BACK__:
DEFW 0
_x:
push ix
ld ix, 0
add ix, sp
ld hl, 0
push hl
push ix
pop hl
ld bc, -2
add hl, bc
ex de, hl
ld hl, __LABEL0
ld bc, 2
ldir
_x__leave:
ld sp, ix
pop ix
ret
ZXBASIC_USER_DATA:
__LABEL0:
DEFW 12345
; Defines DATA END --> HEAP size is 0
ZXBASIC_USER_DATA_END:
; Defines USER DATA Length in bytes
ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA
END
11 changes: 11 additions & 0 deletions tests/functional/const7.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


CONST number as Uinteger = 12345

SUB x
DIM a as UInteger = number
END SUB

x