Skip to content

Commit 80e70fe

Browse files
committed
Refactorize remove-labels
1 parent 7394337 commit 80e70fe

1 file changed

Lines changed: 23 additions & 23 deletions

File tree

src/arch/zx48k/backend/__init__.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,6 +2309,28 @@ def emit_end():
23092309
return output
23102310

23112311

2312+
def remove_unused_labels(output: List[str]):
2313+
labels_used: Set[str] = set()
2314+
labels_to_delete: Dict[str, int] = {}
2315+
2316+
for i, ins in enumerate(output):
2317+
try_label = ins[:-1]
2318+
if try_label in TMP_LABELS:
2319+
if try_label in labels_used:
2320+
labels_to_delete.pop(try_label, None)
2321+
else:
2322+
labels_to_delete[try_label] = i
2323+
continue
2324+
2325+
for op in Asm.opers(ins):
2326+
if op in TMP_LABELS:
2327+
labels_used.add(op)
2328+
labels_to_delete.pop(op, None)
2329+
2330+
for i in sorted(labels_to_delete.values(), reverse=True):
2331+
output.pop(i)
2332+
2333+
23122334
def emit(mem, optimize=True):
23132335
""" Begin converting each quad instruction to asm
23142336
by iterating over the "mem" array, and called its
@@ -2344,29 +2366,7 @@ def output_join(output: List[str], new_chunk: List[str], optimize: bool = True):
23442366
output_join(output, convertToBool(), optimize=optimize)
23452367

23462368
if optimize and OPTIONS.optimization > 1:
2347-
# Remove unused labels
2348-
2349-
label_used = {}
2350-
label_to_delete = {}
2351-
2352-
for i, ins in enumerate(output):
2353-
try_label = ins[:-1]
2354-
if try_label in TMP_LABELS:
2355-
if label_used.get(try_label):
2356-
label_to_delete.pop(try_label, None)
2357-
continue
2358-
2359-
label_to_delete[try_label] = i
2360-
continue
2361-
2362-
for op in Asm.opers(ins):
2363-
if op in TMP_LABELS:
2364-
label_used[op] = True
2365-
label_to_delete.pop(op, None)
2366-
2367-
for i in sorted(label_to_delete.values(), reverse=True):
2368-
output.pop(i)
2369-
2369+
remove_unused_labels(output)
23702370
tmp = output
23712371
output = []
23722372
output_join(output, tmp)

0 commit comments

Comments
 (0)