a2d/res/stats.pl

73 lines
1.8 KiB
Perl
Raw Normal View History

#!/usr/bin/env perl
2018-02-27 03:59:33 +00:00
# stats.pl < source.s -- dump stats
# stats.pl unscoped < source.s -- list Lxxxx symbols not within 2 scopes
# stats.pl single < source.s -- list Lxxxx symbols with no references
use strict;
use warnings;
2018-02-27 03:59:33 +00:00
my $command = shift(@ARGV) // "";
my %defs;
my %refs;
my %unscoped;
2018-03-04 19:35:04 +00:00
my %raw;
my $depth = 0;
while (<STDIN>) {
2018-03-05 05:33:56 +00:00
s/;.*//;
++$depth if m/\.proc/ || m/\.scope/;
--$depth if m/\.endproc/ || m/\.endscope/;
2018-02-27 03:59:33 +00:00
2018-03-05 05:33:56 +00:00
next if m/\.assert|\.org|PAD_TO/;
2018-03-07 16:12:30 +00:00
s/:= \$[0-9A-F]+//; # trust assignments of absolutes
2018-03-05 05:33:56 +00:00
2018-03-03 04:53:10 +00:00
if (m/^(L[0-9A-F]{4})(?::|\s+:=)(.*)/) {
2018-02-27 03:59:33 +00:00
my $def = $1;
$_ = $2;
$defs{$def} = ($defs{$def} // 0) + 1;
$unscoped{$def} = 1 if $depth < 2;
}
2018-03-04 19:35:04 +00:00
foreach my $term (split /[ (),+\-*\/]/, $_) {
$term =~ s/\s+//g;
next unless $term;
if ($term =~ m/^L[0-9A-F]{4}$/) {
2018-03-05 05:33:56 +00:00
$refs{$term} = 1 + ($refs{$term} // 0);
2018-03-04 19:35:04 +00:00
} elsif ($term =~ m/^\$[0-9A-F]{4}$/) {
2018-03-05 05:33:56 +00:00
$raw{$term} = 1 + ($raw{$term} // 0);
}
}
}
2018-02-27 03:59:33 +00:00
my $unrefed = 0;
foreach my $def (keys %defs) {
++$unrefed unless defined $refs{$def};
}
2018-02-27 03:59:33 +00:00
my $defs = scalar(keys %defs);
my $unscoped = scalar(keys %unscoped);
2018-03-04 19:35:04 +00:00
my $raws = scalar(keys %raw);
2018-02-27 03:59:33 +00:00
my $scoped = $defs - $unscoped;
2018-02-27 03:59:33 +00:00
if ($command eq "unscoped") {
foreach my $def (sort keys %unscoped) {
print "$def\n";
}
} elsif ($command eq "unrefed") {
foreach my $def (sort keys %defs) {
print "$def\n" unless defined $refs{$def};
}
2018-03-04 19:35:04 +00:00
} elsif ($command eq "raw") {
foreach my $addr (sort keys %raw) {
print "$addr\n";
}
2018-02-27 03:59:33 +00:00
} elsif ($command eq "") {
2018-03-04 19:35:04 +00:00
printf("unscoped: %4d scoped: %4d raw: %4d unrefed: %4d\n",
2018-03-05 05:33:56 +00:00
$unscoped, $scoped, $raws, $unrefed);
2018-02-27 03:59:33 +00:00
} else {
die "Unknown command: $command\n";
}