a2d/res/stats.pl

81 lines
2.1 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
2018-04-11 00:44:31 +00:00
# stats.pl scoped < source.s -- list Lxxxx symbols within 2 scopes
# stats.pl raw < source.s -- list $xxxx usage
# stats.pl unrefed < source.s -- list Lxxxx symbols with no references
2018-02-27 03:59:33 +00:00
use strict;
use warnings;
2018-02-27 03:59:33 +00:00
my $command = shift(@ARGV) // "";
my %defs;
my %refs;
2018-04-11 00:44:31 +00:00
my %scoped;
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-31 03:15:53 +00:00
s/\b[^L]\w+ := \$[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-04-11 00:44:31 +00:00
$scoped{$def} = 1 if $depth >= 2;
2018-02-27 03:59:33 +00:00
}
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-04-11 00:44:31 +00:00
my $scoped = scalar(keys %scoped);
2018-02-27 03:59:33 +00:00
if ($command eq "unscoped") {
foreach my $def (sort keys %unscoped) {
print "$def\n";
}
2018-04-11 00:44:31 +00:00
} elsif ($command eq "scoped") {
foreach my $def (sort keys %scoped) {
print "$def\n";
}
2018-02-27 03:59:33 +00:00
} 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";
}