Skip to content

Commit f2309da

Browse files
committed
Optimize substring assignation
1 parent 0e23e9b commit f2309da

19 files changed

Lines changed: 423 additions & 4944 deletions

api/check.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@ def is_block_accessed(block):
411411
return False
412412

413413

414+
def is_temporary_value(node) -> bool:
415+
""" Returns if the AST node value is a variable or a temporary copy in the heap.
416+
"""
417+
return node.token not in ('STRING', 'VAR') and node.t[0] not in ('_', '#')
418+
419+
414420
def common_type(a, b):
415421
""" Returns a type which is common for both a and b types.
416422
Returns None if no common types allowed.

arch/zx48k/translator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,13 @@ def visit_LETARRAY(self, node):
314314

315315
def visit_LETSUBSTR(self, node):
316316
yield node.children[3]
317-
self.ic_param(TYPE.string, node.children[3].t)
318-
self.ic_param(TYPE.ubyte, 1)
317+
318+
if check.is_temporary_value(node.children[3]):
319+
self.ic_param(TYPE.string, node.children[3].t)
320+
self.ic_param(TYPE.ubyte, 1)
321+
else:
322+
self.ic_param(gl.PTR_TYPE, node.children[3].t)
323+
self.ic_param(TYPE.ubyte, 0)
319324

320325
yield node.children[1]
321326
self.ic_param(gl.PTR_TYPE, node.children[1].t)
@@ -333,6 +338,7 @@ def visit_LETARRAYSUBSTR(self, node):
333338
yield expr
334339
self.ic_param(TYPE.string, expr.t)
335340

341+
# TODO: this produces a memory leak
336342
if expr.token != 'STRING' and (expr.token != 'VAR' or expr.mangled[0] != '_'):
337343
self.ic_param(TYPE.ubyte, 1) # If the argument is not a variable, it must be freed
338344
else:

0 commit comments

Comments
 (0)