diff --git a/papermario/tools/find_bogus_functions.py b/papermario/tools/find_bogus_functions.py index 6b46451dd3..d6a92e43d3 100644 --- a/papermario/tools/find_bogus_functions.py +++ b/papermario/tools/find_bogus_functions.py @@ -11,6 +11,15 @@ jals = set() bs = set() bals = set() +debug = False + +renames = [] + + +def dprint(string): + if debug: + print(string) + def scan_file(path): with open(path) as f: @@ -38,10 +47,11 @@ def process_file(path): file_lines = f.readlines() cur_fun = None + to_delete = [] for i, line in enumerate(file_lines): line = line.strip() if cur_fun is None: - if line.endswith(":"): + if line.endswith(":") and not line.startswith(".L"): cur_fun = line[:-1] continue else: @@ -50,16 +60,33 @@ def process_file(path): continue if line.endswith(":") and not line.startswith(".L"): linked_to = False + linked_al_to = False + func_preamble = line.startswith("func_") bogus_label = line[:-1] - print("Found dubious func: " + bogus_label) + # print("Found dubious func: " + bogus_label) if bogus_label in jals or bogus_label in bals: - linked_to = True - print("\t in jal/bal instruction - eek!") + linked_al_to = True + dprint("\t in jal/bal instruction - eek!") if bogus_label in js or bogus_label in bs: linked_to = True - print("\t in j/b instruction - change to label") + dprint("\t in j/b instruction - change to label") if not linked_to: - print(" not referenced anywhere") + dprint(" not referenced anywhere") + + if func_preamble: + # func_12345689 + if linked_al_to: + # Seems to be legit. Do nothing for now + dog = 5 + elif linked_to: + # j / b 'd to, rename as label + renames.append((bogus_label, ".L" + bogus_label[5:])) + else: + # Not referenced by anything. Delete + to_delete.append(i) + + if len(to_delete) > 0: + dog = 5 # Collect all branches and jumps @@ -67,13 +94,26 @@ for root, dirs, files in os.walk(asm_dir): for file in files: if file.endswith(".s"): scan_file(os.path.join(root, file)) - dog = 5 print("Scanned all files") # Process files for root, dirs, files in os.walk(asm_dir): for file in files: - if file.endswith(".s") and "giant" not in file: + if file.endswith(".s"): process_file(os.path.join(root, file)) - dog = 5 + +if len(renames) > 0: + print("Renaming " + str(len(renames)) + " labels...") + for root, dirs, files in os.walk(asm_dir): + for file in files: + if file.endswith(".s") and "giant" not in file: + 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)