2023-05-04 11:03:02 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-09-27 20:13:50 +02:00
|
|
|
|
2021-11-12 13:23:43 +01:00
|
|
|
import json
|
2020-09-27 03:02:29 +02:00
|
|
|
import glob
|
|
|
|
import os
|
2022-01-02 13:10:49 +01:00
|
|
|
import argparse
|
|
|
|
from enum import IntEnum
|
2021-06-21 06:30:57 +02:00
|
|
|
|
2022-01-02 13:10:49 +01:00
|
|
|
|
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
asm_dir = script_dir + "/../ver/current/asm/nonmatchings"
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
modes = ["min", "max", "avg", "total", "size"]
|
2020-09-27 03:02:29 +02:00
|
|
|
|
|
|
|
sizes = {}
|
|
|
|
|
2021-06-21 06:30:57 +02:00
|
|
|
funcs = {}
|
|
|
|
|
2022-01-02 13:10:49 +01:00
|
|
|
|
|
|
|
# Calculate the number of instructions in a .s file
|
2020-09-27 03:02:29 +02:00
|
|
|
def calc_insns(f_path):
|
|
|
|
ret = 0
|
|
|
|
with open(f_path) as f:
|
|
|
|
f_lines = f.readlines()
|
|
|
|
for line in f_lines:
|
|
|
|
if line.startswith("/* "):
|
|
|
|
ret += 1
|
2021-06-21 06:30:57 +02:00
|
|
|
funcs[f_path.split("/")[-1][:-2]] = ret
|
2020-09-27 03:02:29 +02:00
|
|
|
return ret
|
|
|
|
|
2022-01-02 13:10:49 +01:00
|
|
|
|
|
|
|
# Calculate different data points for each .c files and store them as a Tuple
|
2020-09-27 03:02:29 +02:00
|
|
|
def do_dir(root, dir):
|
|
|
|
max = 0
|
2022-01-02 13:10:49 +01:00
|
|
|
min = 0
|
2020-09-27 03:02:29 +02:00
|
|
|
total = 0
|
|
|
|
|
|
|
|
files = glob.glob(os.path.join(root, dir) + "/*.s")
|
|
|
|
|
|
|
|
for f in files:
|
|
|
|
amt = calc_insns(f)
|
|
|
|
if amt > max:
|
|
|
|
max = amt
|
2022-01-02 13:10:49 +01:00
|
|
|
if min == 0 or amt < min:
|
2020-09-27 03:02:29 +02:00
|
|
|
min = amt
|
|
|
|
total += amt
|
2020-11-23 19:48:34 +01:00
|
|
|
|
2020-09-27 03:02:29 +02:00
|
|
|
avg = 0 if len(files) == 0 else total / len(files)
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
sizes[root + "/" + dir] = (min, max, total, avg, len(files))
|
2020-09-27 03:02:29 +02:00
|
|
|
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="A tool to receive information about the number of non-matching .s files "
|
|
|
|
+ "per .c file, or the size of .s files, measured by their number of instructions. "
|
|
|
|
+ "Option -p is used by default if no option is specified."
|
|
|
|
)
|
2022-01-02 13:10:49 +01:00
|
|
|
group = parser.add_mutually_exclusive_group()
|
2023-07-29 19:03:17 +02:00
|
|
|
group.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--files",
|
|
|
|
help="Default. Print the number of non-matching .s files per .c file, ordered by size.",
|
|
|
|
action="store_true",
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"-a",
|
|
|
|
"--alphabetical",
|
|
|
|
help="Print the size of .s files, ordered by name.",
|
|
|
|
action="store_true",
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"-s",
|
|
|
|
"--size",
|
|
|
|
help="Print the size of .s files, ordered by size.",
|
|
|
|
action="store_true",
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-l",
|
|
|
|
"--limit",
|
|
|
|
help="Only print the .c --files that are greater than or equal to the value.",
|
|
|
|
type=int,
|
|
|
|
default=0,
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-m",
|
|
|
|
"--mode",
|
|
|
|
help="Switches between output modes for --files. Allowed values are: {min, max, avg, total, size}.",
|
|
|
|
choices=modes,
|
|
|
|
default="size",
|
|
|
|
metavar="",
|
|
|
|
required=False,
|
|
|
|
)
|
2020-09-27 03:02:29 +02:00
|
|
|
|
2022-01-02 13:10:49 +01:00
|
|
|
args = parser.parse_args()
|
2020-09-27 03:02:29 +02:00
|
|
|
|
2021-06-21 06:30:57 +02:00
|
|
|
|
2022-01-02 13:10:49 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
for root, dirs, files in os.walk(asm_dir):
|
|
|
|
for asm_dir in dirs:
|
|
|
|
do_dir(root, asm_dir)
|
2023-04-29 15:28:06 +02:00
|
|
|
|
2022-01-02 13:10:49 +01:00
|
|
|
if args.alphabetical:
|
|
|
|
print(json.dumps(dict(sorted(funcs.items(), key=lambda f: f[0])), indent=4))
|
|
|
|
elif args.size:
|
|
|
|
print(json.dumps(dict(sorted(funcs.items(), key=lambda f: f[1])), indent=4))
|
|
|
|
else:
|
|
|
|
for thing in sorted(sizes.keys(), key=lambda x: sizes[x][modes.index(args.mode)]):
|
|
|
|
val = sizes[thing][modes.index(args.mode)]
|
|
|
|
if val > args.limit:
|
|
|
|
print(thing.split("nonmatchings/")[1].ljust(50) + str(val))
|