papermario/tools/find_bogus_functions.py

120 lines
3.3 KiB
Python
Raw Normal View History

2020-04-30 02:23:34 +02:00
#!/usr/bin/python3
import os
import re
script_dir = os.path.dirname(os.path.realpath(__file__))
asm_dir = script_dir + "/../asm/"
js = set()
jals = set()
bs = set()
bals = set()
2020-05-01 05:54:28 +02:00
debug = False
renames = []
def dprint(string):
if debug:
print(string)
2020-04-30 02:23:34 +02:00
def scan_file(path):
with open(path) as f:
file_lines = f.readlines()
for line in file_lines:
line = line.strip()
if re.findall(r'\sj\s+.*', line):
target = line.split()[-1]
js.add(target)
if re.findall(r'\sjal\s+.*', line):
target = line.split()[-1]
jals.add(target)
2020-05-02 06:24:20 +02:00
if re.findall(r'\sb.*', line):
2020-04-30 02:23:34 +02:00
target = line.split()[-1]
bs.add(target)
if re.findall(r'\sbal\s+.*', line):
target = line.split()[-1]
bals.add(target)
2020-05-02 06:24:20 +02:00
def get_linked_flags(label):
return (label in js or label in bs), (label in jals or label in bals)
2020-04-30 02:23:34 +02:00
def process_file(path):
print("Processing " + path)
2020-05-02 06:24:20 +02:00
with open(path) as fi:
file_lines = fi.readlines()
2020-04-30 02:23:34 +02:00
2020-05-02 06:24:20 +02:00
skip_next = False
2020-05-01 05:54:28 +02:00
to_delete = []
2020-04-30 02:23:34 +02:00
for i, line in enumerate(file_lines):
2020-05-02 06:24:20 +02:00
if skip_next:
skip_next = False
continue
2020-04-30 02:23:34 +02:00
line = line.strip()
2020-05-02 06:24:20 +02:00
if line.endswith(":") and not line.startswith(".L"):
func_preamble = line.startswith("func_")
label = line[:-1]
linked_to, linked_al_to = get_linked_flags(label)
if func_preamble:
# func_12345689
# Delete redundant label below
2020-07-23 03:01:17 +02:00
# if file_lines[i + 1].strip() == ".L" + label[5:13] + ":":
# to_delete.append(i + 1)
# skip_next = True
2020-05-02 06:24:20 +02:00
if linked_al_to:
# Seems to be legit. Do nothing for now
continue
elif linked_to:
# j / b 'd to, rename as label
renames.append((label, ".L" + label[5:]))
else:
# Not referenced by anything. Delete
to_delete.append(i)
2020-05-01 05:54:28 +02:00
if len(to_delete) > 0:
2020-05-02 06:24:20 +02:00
print("\tDeleting " + str(len(to_delete)) + " lines...")
for i in reversed(to_delete):
del file_lines[i]
with open(path, "w") as fo:
fo.writelines(file_lines)
2020-04-30 02:23:34 +02:00
# Collect all branches and jumps
for root, dirs, files in os.walk(asm_dir):
for file in files:
if file.endswith(".s"):
scan_file(os.path.join(root, file))
print("Scanned all files")
# Process files
for root, dirs, files in os.walk(asm_dir):
for file in files:
2020-05-02 06:24:20 +02:00
if file.endswith(".s") and "boot" not in file:
2020-04-30 02:23:34 +02:00
process_file(os.path.join(root, file))
2020-05-01 05:54:28 +02:00
if len(renames) > 0:
print("Renaming " + str(len(renames)) + " labels...")
for root, dirs, files in os.walk(asm_dir):
for file in files:
2020-05-02 06:24:20 +02:00
if file.endswith(".s"):
2020-05-01 05:54:28 +02:00
file_path = os.path.join(root, file)
with open(file_path) as f:
orig_file_text = f.read()
file_text = orig_file_text
for rename in renames:
file_text = file_text.replace(rename[0], rename[1])
if file_text != orig_file_text:
with open(file_path, "w") as f:
f.write(file_text)