pokeemerald/.github/calcrom/calcrom.pl

176 lines
5.5 KiB
Perl
Raw Normal View History

2018-06-16 18:39:54 +02:00
#!/usr/bin/perl
2022-11-27 23:33:09 +01:00
# Usage:
# calcrom.pl <mapfile> [--data]
#
# mapfile: path to .map file output by LD
# data: set to output % breakdown of data
2019-03-01 07:06:27 +01:00
use IPC::Cmd qw[ run ];
2021-10-14 15:52:33 +02:00
use Getopt::Long;
2019-03-01 07:06:27 +01:00
2021-10-14 15:52:33 +02:00
my $usage = "Usage: calcrom.pl file.map [--data]\n";
2021-10-14 16:15:08 +02:00
my $showData;
GetOptions("data" => \$showData) or die $usage;
(@ARGV == 1)
2021-10-14 15:52:33 +02:00
or die $usage;
2018-06-16 18:39:54 +02:00
open(my $file, $ARGV[0])
or die "ERROR: could not open file '$ARGV[0]'.\n";
my $src = 0;
my $asm = 0;
2019-08-06 19:00:03 +02:00
my $srcdata = 0;
my $data = 0;
2018-06-16 18:39:54 +02:00
while (my $line = <$file>)
{
if ($line =~ /^ \.(\w+)\s+0x[0-9a-f]+\s+(0x[0-9a-f]+) (\w+)\/.+\.o/)
{
my $section = $1;
my $size = hex($2);
my $dir = $3;
if ($section =~ /text/)
{
if ($dir eq 'src')
{
$src += $size;
}
elsif ($dir eq 'asm')
{
$asm += $size;
}
}
2019-08-06 19:00:03 +02:00
elsif ($section =~ /rodata/)
{
if ($dir eq 'src')
{
$srcdata += $size;
}
elsif ($dir eq 'data')
{
$data += $size;
}
}
2018-06-16 18:39:54 +02:00
}
}
2019-08-06 19:00:03 +02:00
(my $elffname = $ARGV[0]) =~ s/\.map/.elf/;
# Note that the grep filters out all branch labels. It also requires a minimum
# line length of 5, to filter out a ton of generated symbols (like AcCn). No
# settings to nm seem to remove these symbols. Finally, nm prints out a separate
# entry for whenever a name appears in a file, not just where it's defined. uniq
# removes all the duplicate entries.
#
#
# You'd expect this to take a while, because of uniq. It runs in under a second,
# though. Uniq is pretty fast!
2019-08-06 19:00:03 +02:00
my $base_cmd = "nm $elffname | awk '{print \$3}' | grep '^[^_].\\{4\\}' | uniq";
# This looks for Unknown_, Unknown_, or sub_, followed by an address. Note that
# it matches even if stuff precedes the unknown, like sUnknown/gUnknown.
2022-11-27 23:33:09 +01:00
my $undoc_regex = "'[Uu]nknown_[0-9a-fA-F]\\{5,7\\}\\|sub_[0-9a-fA-F]\\{5,7\\}'";
# This looks for every symbol with an address at the end of it. Some things are
# given a name based on their type / location, but still have an unknown purpose.
# For example, FooMap_EventScript_FFFFFFF.
2022-11-27 23:33:09 +01:00
# The above may be double counted here, and will need to be filtered out.
my $partial_doc_regex = "'_[0-28][0-9a-fA-F]\\{5,7\\}'";
my $count_cmd = "wc -l";
# It sucks that we have to run this three times, but I can't figure out how to get
# stdin working for subcommands in perl while still having a timeout. It's decently
# fast anyway.
2019-03-01 07:06:27 +01:00
my $total_syms_as_string;
(run (
command => "$base_cmd | $count_cmd",
2019-03-01 07:09:53 +01:00
buffer => \$total_syms_as_string,
timeout => 60
2019-03-01 07:06:27 +01:00
))
or die "ERROR: Error while getting all symbols: $?";
my $undocumented_as_string;
(run (
2022-11-27 23:33:09 +01:00
command => "$base_cmd | grep $undoc_regex | $count_cmd",
2019-03-01 07:09:53 +01:00
buffer => \$undocumented_as_string,
timeout => 60
2019-03-01 07:06:27 +01:00
))
or die "ERROR: Error while filtering for undocumented symbols: $?";
my $partial_documented_as_string;
(run (
2022-11-27 23:33:09 +01:00
command => "$base_cmd | grep $partial_doc_regex | grep -v $undoc_regex | $count_cmd",
buffer => \$partial_documented_as_string,
timeout => 60
))
or die "ERROR: Error while filtering for partial symbols: $?";
# Performing addition on a string converts it to a number. Any string that fails
# to convert to a number becomes 0. So if our converted number is 0, but our string
# is nonzero, then the conversion was an error.
2021-11-17 21:04:42 +01:00
$undocumented_as_string =~ s/^\s+|\s+$//g;
2019-03-01 07:06:27 +01:00
my $undocumented = $undocumented_as_string + 0;
2021-11-17 21:04:42 +01:00
(($undocumented != 0) or (($undocumented == 0) and ($undocumented_as_string eq "0")))
2019-03-01 07:06:27 +01:00
or die "ERROR: Cannot convert string to num: '$undocumented_as_string'";
2021-11-17 21:04:42 +01:00
$partial_documented_as_string =~ s/^\s+|\s+$//g;
my $partial_documented = $partial_documented_as_string + 0;
2021-11-17 21:04:42 +01:00
(($partial_documented != 0) or (($partial_documented == 0) and ($partial_documented_as_string eq "0")))
2022-11-27 23:33:09 +01:00
or die "ERROR: Cannot convert string to num: '$partial_documented_as_string'";
2021-11-17 21:04:42 +01:00
$total_syms_as_string =~ s/^\s+|\s+$//g;
2019-03-01 07:06:27 +01:00
my $total_syms = $total_syms_as_string + 0;
2021-11-17 21:04:42 +01:00
(($total_syms != 0) or (($total_syms == 0) and ($total_syms_as_string eq "0")))
2019-03-01 07:06:27 +01:00
or die "ERROR: Cannot convert string to num: '$total_syms_as_string'";
($total_syms != 0)
or die "ERROR: No symbols found.";
2018-06-16 18:39:54 +02:00
my $total = $src + $asm;
my $srcPct = sprintf("%.4f", 100 * $src / $total);
my $asmPct = sprintf("%.4f", 100 * $asm / $total);
2019-03-01 07:06:27 +01:00
my $documented = $total_syms - ($undocumented + $partial_documented);
2019-03-01 07:06:27 +01:00
my $docPct = sprintf("%.4f", 100 * $documented / $total_syms);
my $partialPct = sprintf("%.4f", 100 * $partial_documented / $total_syms);
2019-03-01 07:06:27 +01:00
my $undocPct = sprintf("%.4f", 100 * $undocumented / $total_syms);
2020-03-03 22:29:57 +01:00
if ($asm == 0)
{
print "Code decompilation is 100% complete\n"
}
else
{
print "$total total bytes of code\n";
print "$src bytes of code in src ($srcPct%)\n";
print "$asm bytes of code in asm ($asmPct%)\n";
}
2019-03-01 07:06:27 +01:00
print "\n";
2020-03-03 22:29:57 +01:00
if ($partial_documented == 0 && $undocumented == 0)
{
print "Documentation is 100% complete\n"
}
else
{
print "$total_syms total symbols\n";
print "$documented symbols documented ($docPct%)\n";
print "$partial_documented symbols partially documented ($partialPct%)\n";
print "$undocumented symbols undocumented ($undocPct%)\n";
}
2019-08-06 19:00:03 +02:00
2021-10-14 15:52:33 +02:00
if ($showData)
2020-03-03 22:29:57 +01:00
{
2021-10-14 15:52:33 +02:00
print "\n";
my $dataTotal = $srcdata + $data;
my $srcDataPct = sprintf("%.4f", 100 * $srcdata / $dataTotal);
my $dataPct = sprintf("%.4f", 100 * $data / $dataTotal);
2020-03-03 22:29:57 +01:00
print "$dataTotal total bytes of data\n";
print "$srcdata bytes of data in src ($srcDataPct%)\n";
print "$data bytes of data in data ($dataPct%)\n";
}