Skip to content

Commit c67c2b2

Browse files
author
Jose Rodriguez
committed
typing: improves typing
Also uses list.append() instead of += [value] which is slower.
1 parent 0674535 commit c67c2b2

3 files changed

Lines changed: 30 additions & 25 deletions

File tree

src/arch/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
#!/usr/bin/env python3
21
# -*- coding: utf-8 -*-
3-
# vim:ts=4:et:sw=4:
2+
3+
# ----------------------------------------------------------------------
4+
# Copyleft (K), Jose M. Rodriguez-Rosa (a.k.a. Boriel)
5+
#
6+
# This program is Free Software and is released under the terms of
7+
# the GNUv3 General License
8+
# ----------------------------------------------------------------------
49

510
import importlib
11+
from types import ModuleType
612

713
__all__ = (
814
"zx48k",
915
"zxnext",
1016
)
1117

1218
AVAILABLE_ARCHITECTURES = __all__
13-
target = None
19+
target: ModuleType
1420

1521

1622
def set_target_arch(target_arch: str):

src/arch/z80/optimizer/common.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,4 @@
1717
JUMP_LABELS: Final[set[str]] = set()
1818
MEMORY: Final[list[MemCell]] = [] # Instructions emitted by the backend
1919

20-
# PROC labels name space counter
21-
PROC_COUNTER = 0
22-
2320
BLOCKS: Final[list[BasicBlock]] = [] # Memory blocks

src/arch/z80/optimizer/main.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections import defaultdict
2+
13
from src.api.config import OPTIONS
24
from src.api.debug import __DEBUG__
35
from src.api.utils import flatten_list
@@ -8,10 +10,14 @@
810
from .common import JUMP_LABELS, LABELS, MEMORY
911
from .helpers import ALL_REGS, END_PROGRAM_LABEL
1012
from .labelinfo import LabelInfo
13+
from .memcell import MemCell
1114
from .patterns import RE_LABEL, RE_PRAGMA
1215

1316
__all__ = "init", "optimize"
1417

18+
# PROC labels name space counter
19+
PROC_COUNTER: int = 0
20+
1521

1622
def init():
1723
LABELS.clear()
@@ -68,19 +74,19 @@ def cleanup_local_labels(block: BasicBlock) -> None:
6874
"""
6975
global PROC_COUNTER
7076

71-
stack = [[]]
72-
hashes = [{}]
73-
stackprc = [PROC_COUNTER]
74-
used = [{}] # List of hashes of unresolved labels per scope
77+
stack: list[list[str]] = [[]]
78+
hashes: list[dict[str, str]] = [{}]
79+
stackprc: list[int] = [PROC_COUNTER]
80+
used: list[dict[str, list[MemCell]]] = [defaultdict(list)] # List of hashes of unresolved labels per scope
7581

7682
MEMORY[:] = block.mem[:]
7783

7884
for cell in MEMORY:
7985
if cell.inst.upper() == "PROC":
80-
stack += [[]]
81-
hashes += [{}]
82-
stackprc += [PROC_COUNTER]
83-
used += [{}]
86+
stack.append([])
87+
hashes.append({})
88+
stackprc.append(PROC_COUNTER)
89+
used.append(defaultdict(list))
8490
PROC_COUNTER += 1
8591
continue
8692

@@ -99,26 +105,25 @@ def cleanup_local_labels(block: BasicBlock) -> None:
99105
continue
100106

101107
tmp = cell.asm.asm
102-
if tmp.upper()[:5] == "LOCAL":
108+
if tmp.upper().startswith("LOCAL"):
103109
tmp = tmp[5:].split(",")
104110
for lbl in tmp:
105111
lbl = lbl.strip()
106112
if lbl in stack[-1]:
107113
continue
108-
stack[-1] += [lbl]
109-
hashes[-1][lbl] = "PROC%i." % stackprc[-1] + lbl
110-
if used[-1].get(lbl, None) is None:
111-
used[-1][lbl] = []
112114

113-
cell.asm = ";" + cell.asm # Remove it
115+
stack[-1].append(lbl)
116+
hashes[-1][lbl] = f"PROC{stackprc[-1]}.{lbl}"
117+
118+
cell.asm = f";{str(cell.asm)}" # Remove it
114119
continue
115120

116121
if cell.is_label:
117122
label = cell.inst
118123
for i in range(len(stack) - 1, -1, -1):
119124
if label in stack[i]:
120125
label = hashes[i][label]
121-
cell.asm = label + ":"
126+
cell.asm = f"{label}:"
122127
break
123128
continue
124129

@@ -132,10 +137,7 @@ def cleanup_local_labels(block: BasicBlock) -> None:
132137
break
133138

134139
if not labelUsed:
135-
if used[-1].get(label, None) is None:
136-
used[-1][label] = []
137-
138-
used[-1][label] += [cell]
140+
used[-1][label].append(cell)
139141

140142
for i in range(len(MEMORY) - 1, -1, -1):
141143
if MEMORY[i].asm.asm[0] == ";":

0 commit comments

Comments
 (0)