1+ from collections import defaultdict
2+
13from src .api .config import OPTIONS
24from src .api .debug import __DEBUG__
35from src .api .utils import flatten_list
810from .common import JUMP_LABELS , LABELS , MEMORY
911from .helpers import ALL_REGS , END_PROGRAM_LABEL
1012from .labelinfo import LabelInfo
13+ from .memcell import MemCell
1114from .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
1622def 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