Skip to content

Commit 78bb500

Browse files
authored
Merge pull request #443 from boriel/bugfix/byref_str
Bugfix/byref str
2 parents 62c4d04 + bfee37e commit 78bb500

10 files changed

Lines changed: 2304 additions & 7 deletions

src/api/check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def is_block_accessed(block):
420420
def is_temporary_value(node) -> bool:
421421
""" Returns if the AST node value is a variable or a temporary copy in the heap.
422422
"""
423-
return node.token not in ('STRING', 'VAR') and node.t[0] not in ('_', '#')
423+
return node.token not in ('PARAMDECL', 'STRING', 'VAR') and node.t[0] not in ('_', '#')
424424

425425

426426
def common_type(a, b):

src/arch/zx48k/backend/__pload.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def _paddr(ins):
5151

5252
def _pload(offset, size):
5353
""" Generic parameter loading.
54-
Emits output code for setting IX at the right location.
54+
Emits output code for loading at (IX + offset).
5555
size = Number of bytes to load:
56-
1 => 8 bit value
57-
2 => 16 bit value / string
58-
4 => 32 bit value / f16 value
59-
5 => 40 bit value
56+
1 => 8 bit value # A register
57+
2 => 16 bit value / string # HL register
58+
4 => 32 bit value / f16 value # DE (HI), HL (LO) register
59+
5 => 40 bit value / float value # A (exp) BC (HI), DE (LO) mantissa (as ZX Spectrum ROM)
6060
"""
6161
output = []
6262

src/arch/zx48k/translator.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ def visit_LETARRAY(self, node):
323323
raise InternalError("Invalid scope {} for variable '{}'".format(scope, arr.entry.name))
324324

325325
def visit_LETSUBSTR(self, node):
326+
""" LET X$(a TO b) = Y$
327+
"""
328+
# load Y$
326329
yield node.children[3]
327330

328331
if check.is_temporary_value(node.children[3]):
@@ -332,11 +335,30 @@ def visit_LETSUBSTR(self, node):
332335
self.ic_param(gl.PTR_TYPE, node.children[3].t)
333336
self.ic_param(TYPE.ubyte, 0)
334337

338+
# Load a
335339
yield node.children[1]
336340
self.ic_param(gl.PTR_TYPE, node.children[1].t)
341+
# Load b
337342
yield node.children[2]
338343
self.ic_param(gl.PTR_TYPE, node.children[2].t)
339-
self.ic_fparam(gl.PTR_TYPE, node.children[0].t)
344+
# Load x$
345+
str_var = node.children[0]
346+
scope = str_var.scope
347+
348+
if scope == SCOPE.global_:
349+
self.ic_fparam(gl.PTR_TYPE, str_var.t)
350+
elif scope == SCOPE.local:
351+
self.ic_pload(gl.PTR_TYPE, str_var.t, -str_var.offset)
352+
self.ic_fparam(gl.PTR_TYPE, f"{str_var.t}")
353+
elif scope == SCOPE.parameter:
354+
self.ic_pload(gl.PTR_TYPE, str_var.t, str_var.offset)
355+
if str_var.byref:
356+
self.ic_fparam(gl.PTR_TYPE, f"*{str_var.t}")
357+
else:
358+
self.ic_fparam(gl.PTR_TYPE, f"{str_var.t}")
359+
else:
360+
raise InternalError("Invalid scope {} for variable '{}'".format(scope, node.name))
361+
340362
self.ic_call('__LETSUBSTR', 0)
341363
backend.REQUIRES.add('letsubstr.asm')
342364

tests/api/test_check.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import unittest
5+
6+
from src.api import check
7+
from src import symbols
8+
9+
10+
class TestCheck(unittest.TestCase):
11+
""" Tests api.check
12+
"""
13+
def test_is_temporary_value_const_string(self):
14+
node = symbols.STRING("Hello world", 1)
15+
self.assertFalse(check.is_temporary_value(node))
16+
17+
def test_is_temporary_value_var(self):
18+
node = symbols.VAR("a", 1)
19+
self.assertFalse(check.is_temporary_value(node))
20+
21+
def test_is_temporary_value_param(self):
22+
node = symbols.PARAMDECL("a", 1)
23+
self.assertFalse(check.is_temporary_value(node))
24+
25+
def test_is_temporary_value_expr(self):
26+
child = symbols.VAR("a", 1)
27+
node = symbols.BINARY('PLUS', child, child, 1)
28+
self.assertTrue(check.is_temporary_value(node))

0 commit comments

Comments
 (0)