Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request boriel-basic#635 from boriel/refact/remove_aliased_by
Browse files Browse the repository at this point in the history
refact: remove usage of aliases
  • Loading branch information
boriel committed Nov 20, 2022
2 parents 9d4f0a5 + 9cdf4ad commit 5ac5337
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 102 deletions.
5 changes: 1 addition & 4 deletions src/api/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,7 @@ def visit_var(entry):
return

VariableVisitor._parent_variable = entry
if entry.alias is not None:
result.add(VarDependency(parent=entry, dependency=entry.alias))
visit_var(entry.alias)
elif entry.addr is not None:
if entry.addr is not None:
visit_var(entry.addr)

visit_var(var_entry)
Expand Down
12 changes: 3 additions & 9 deletions src/api/symboltable/symboltable.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def entry_size(var_entry: Union[SymbolVAR, SymbolVARARRAY]):
"""For local variables and params, returns the real variable or
local array size in bytes
"""
if var_entry.scope == SCOPE.global_ or var_entry.is_aliased: # aliases or global variables = 0
if var_entry.scope == SCOPE.global_ or var_entry.addr is not None: # aliases or global variables = 0
return 0

if var_entry.class_ != CLASS.array:
Expand All @@ -243,14 +243,8 @@ def entry_size(var_entry: Union[SymbolVAR, SymbolVARARRAY]):

# Local variables offset
if entry.class_ == CLASS.var and entry.scope == SCOPE.local:
if entry.alias is not None: # alias of another variable?
if entry.offset is None:
entry.offset = entry.alias.offset
else:
entry.offset = entry.alias.offset - entry.offset
else:
offset += entry_size(entry)
entry.offset = offset
offset += entry_size(entry)
entry.offset = offset

if entry.class_ == CLASS.array and entry.scope == SCOPE.local:
entry.offset = entry_size(entry) + offset
Expand Down
3 changes: 2 additions & 1 deletion src/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ def get_final_value(symbol: symbols.SYMBOL) -> Any:
def timeout(seconds: Union[Callable[[], int], int] = 10, error_message=os.strerror(errno.ETIME)):
def decorator(func):
def _handle_timeout(signum, frame):
raise TimeoutError(error_message)
# raise TimeoutError(error_message)
pass

def wrapper(*args, **kwargs):
signal.signal(signal.SIGALRM, _handle_timeout)
Expand Down
19 changes: 3 additions & 16 deletions src/arch/z80/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def visit_RANDOMIZE(self, node):

def visit_LABEL(self, node):
self.ic_label(node.mangled)
for tmp in node.aliased_by:
self.ic_label(tmp.mangled)
# for tmp in node.aliased_by:
# self.ic_label(tmp.mangled)

def visit_VAR(self, node):
__DEBUG__(
Expand All @@ -114,9 +114,6 @@ def visit_VAR(self, node):
self.ic_pload(node.type_, node.t, p + str(node.offset))
elif scope == SCOPE.local:
offset = node.offset
if node.alias is not None and node.alias.class_ == CLASS.array:
offset -= 1 + 2 * node.alias.count # TODO this is actually NOT implemented

self.ic_pload(node.type_, node.t, p + str(-offset))

def visit_CONSTEXPR(self, node):
Expand Down Expand Up @@ -940,8 +937,6 @@ def emit_var_assign(self, var, t):
elif var.scope == SCOPE.parameter:
self.ic_pstore(var.type_, p + str(var.offset), t)
elif var.scope == SCOPE.local:
if var.alias is not None and var.alias.class_ == CLASS.array:
var.offset -= 1 + 2 * var.alias.count
self.ic_pstore(var.type_, p + str(-var.offset), t)

def emit_let_left_part(self, node, t=None):
Expand Down Expand Up @@ -1103,11 +1098,7 @@ def visit_VARDECL(self, node):
if entry.addr is not None:
addr = self.traverse_const(entry.addr) if isinstance(entry.addr, symbols.SYMBOL) else entry.addr
self.ic_deflabel(entry.mangled, addr)
for entry in entry.aliased_by:
self.ic_deflabel(entry.mangled, entry.addr)
elif entry.alias is None:
for alias in entry.aliased_by:
self.ic_label(alias.mangled)
else:
if entry.default_value is None:
self.ic_var(entry.mangled, entry.size)
else:
Expand Down Expand Up @@ -1155,10 +1146,6 @@ def visit_ARRAYDECL(self, node):
else:
arr_data = ["00"] * node.size

for alias in entry.aliased_by:
offset = 1 + 2 * TYPE.size(gl.PTR_TYPE) + alias.offset # TODO: Generalize for multi-arch
self.ic_deflabel(alias.mangled, "%s + %i" % (entry.mangled, offset))

self.ic_varx(node.mangled, gl.PTR_TYPE, [idx_table_label])

if entry.addr:
Expand Down
6 changes: 3 additions & 3 deletions src/ply/lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------

import copy
import inspect
import os
import re
import sys
import types
import copy
import os
import inspect

# This tuple contains acceptable string types
StringTypes = (str, bytes)
Expand Down
4 changes: 2 additions & 2 deletions src/ply/yacc.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
# own risk!
# ----------------------------------------------------------------------------

import inspect
import re
import types
import sys
import inspect
import types

# -----------------------------------------------------------------------------
# === User configurable parameters ===
Expand Down
1 change: 0 additions & 1 deletion src/symbols/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import src.api.check as check
import src.api.errmsg as errmsg

from src.symbols.constexpr import SymbolCONSTEXPR
from src.symbols.number import SymbolNUMBER
from src.symbols.string_ import SymbolSTRING
Expand Down
6 changes: 3 additions & 3 deletions src/symbols/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from typing import Optional

from src.api.constants import CLASS

from src.symbols.symbol_ import Symbol
from src.symbols.constexpr import SymbolCONSTEXPR
from src.symbols.type_ import SymbolTYPE, Type as TYPE
from src.symbols.symbol_ import Symbol
from src.symbols.type_ import SymbolTYPE
from src.symbols.type_ import Type as TYPE


def _get_val(other):
Expand Down
31 changes: 0 additions & 31 deletions src/symbols/var.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@
# the GNU General License
# ----------------------------------------------------------------------

from typing import List, Union

from src.api import global_
from src.api.constants import CLASS, SCOPE
from src.symbols.id_ import SymbolID
from src.symbols.label import SymbolLABEL
from src.symbols.symbol_ import Symbol

# ----------------------------------------------------------------------
# Identifier Symbol object
Expand All @@ -37,8 +33,6 @@ def __init__(self, varname: str, lineno: int, offset=None, type_=None, class_: C
self.offset = offset # If local variable or parameter, +/- offset from top of the stack
self.default_value = None # If defined, variable will be initialized with this value (Arrays = List of Bytes)
self.byref = False # By default, it's a global var
self.alias = None # If not None, this var is an alias of another
self.aliased_by: List[Symbol] = [] # Which variables are an alias of this one
self.callable = None # For functions, subs, arrays and strings this will be True

@property
Expand All @@ -50,31 +44,6 @@ def byref(self, value: bool):
assert isinstance(value, bool)
self.__byref = value

def add_alias(self, entry: SymbolID):
"""Adds id to the current list 'aliased_by'"""
assert isinstance(entry, SymbolID)
self.aliased_by.append(entry)

def make_alias(self, entry: Union[SymbolID, SymbolLABEL]):
"""Make this variable an alias of another one"""
assert isinstance(entry, (SymbolVAR, SymbolLABEL))
entry.add_alias(self)
self.alias = entry
self.scope = entry.scope # Local aliases can be "global" (static)
self.addr = entry.addr

if isinstance(entry, SymbolVAR):
self.byref = entry.byref
self.offset = entry.offset
else:
self.byref = False
self.offset = None

@property
def is_aliased(self):
"""Return if this symbol is aliased by another"""
return len(self.aliased_by) > 0

def __str__(self):
return self.name

Expand Down
17 changes: 1 addition & 16 deletions src/zxbc/zxbparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,22 +668,7 @@ def p_var_decl_at(p):

if p[5].token == "CONSTEXPR":
tmp = p[5].expr
if tmp.token == "UNARY" and tmp.operator == "ADDRESS": # Must be an ID
if tmp.operand.token in ("VAR", "LABEL"):
entry.make_alias(tmp.operand)
elif tmp.operand.token == "ARRAYACCESS":
if tmp.operand.offset is None:
error(p.lineno(4), "Address is not constant. Only constant subscripts are allowed")
return

entry.make_alias(tmp.operand)
entry.offset = tmp.operand.offset
else:
error(p.lineno(4), "Only addresses of identifiers are allowed")
return
else:
entry.addr = tmp

entry.addr = tmp
elif not is_static(p[5]):
src.api.errmsg.syntax_error_address_must_be_constant(p.lineno(4))
return
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/zx48k/atlabel.asm
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
_radians:
_a:
DEFB 82h
DEFB 49h
DEFB 0Fh
DEFB 0DAh
DEFB 0A2h
_radians EQU _a
.core.ZXBASIC_USER_DATA_END:
.core.__MAIN_PROGRAM__:
ld hl, 0
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/zx48k/atlabel1.asm
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
_radians EQU .LABEL._mylabel
.core.ZXBASIC_USER_DATA_END:
.core.__MAIN_PROGRAM__:
.LABEL._mylabel:
_radians:
ld a, 082h
ld de, 00000h
ld bc, 00000h
Expand Down Expand Up @@ -70,5 +70,5 @@ __STOREF: ; Stores the given FP number in A EDCB at address HL
ld (hl), b
ret
pop namespace
#line 24 "atlabel1.bas"
#line 23 "zx48k/atlabel1.bas"
END
2 changes: 1 addition & 1 deletion tests/functional/zx48k/dim_at_label0.asm
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
_x EQU .LABEL._somelabel
.core.ZXBASIC_USER_DATA_END:
.core.__MAIN_PROGRAM__:
.LABEL._somelabel:
_x:
ld hl, 0
ld b, h
ld c, l
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/zx48k/dim_at_label3.asm
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
_x EQU .LABEL._somelabel
.core.ZXBASIC_USER_DATA_END:
.core.__MAIN_PROGRAM__:
.LABEL._somelabel:
_x:
ld hl, 0
ld b, h
ld c, l
Expand Down
11 changes: 0 additions & 11 deletions tests/symbols/test_symbolVAR.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ def test_size(self):
self.v.type_ = Type.byte_
self.assertEqual(self.v.type_, Type.byte_)

def test_add_alias(self):
self.v.add_alias(self.v)

def test_add_alias_fail(self):
self.assertRaises(AssertionError, self.v.add_alias, "blah")

def test_set_value(self):
self.v.class_ = CLASS.const
self.v.value = 1234
Expand All @@ -36,11 +30,6 @@ def test_set_value_var(self):
self.v.class_ = CLASS.var
self.assertRaises(AssertionError, getattr, self.v, "value")

def test_is_aliased(self):
self.assertFalse(self.v.is_aliased)
self.v.add_alias(self.v)
self.assertTrue(self.v.is_aliased)

def test_t(self):
self.assertEqual(self.v.scope, SCOPE.global_) # Must be initialized as global_
self.assertEqual(self.v.t, self.v.mangled)
Expand Down

0 comments on commit 5ac5337

Please sign in to comment.