source: xtideuniversalbios/trunk/Tools/unused.pl @ 194

Last change on this file since 194 was 194, checked in by gregli@…, 12 years ago

ifdef'd out more unused code. Also added a tool for looking through the listing and the output of the precompiler to aid in finding dead code. Some changes in the files are to add annotations for the tool to avoid false positives.

File size: 1.8 KB
Line 
1#
2# Looks for unused entry points, to aid in discovering dead code that can be removed
3#
4# Usage: unused.pl listing unused.asm
5#       
6# where: listing is the normal listing from assembly
7#        unused.asm is assembled with the -E nasm flag
8#
9# Annotations can be placed in the source to eliminate false positives:
10#   a) if a label can be fallen into, place "; fall through to label" above the label
11#   b) "; unused entrypoint ok" can be placed on the same line with the label
12#   c) "; jump table entrypoint" can be placed on the same line with the label
13#
14
15print "::".$ARGV[0]."::".$ARGV[1]."::\n";
16
17open( LST, "<", $ARGV[0] ) || die "cannot open listing: ".$ARGV[0];
18open( UNUSED, "<", $ARGV[1] ) || die "cannot open unused.asm: ".$ARGV[1];
19
20while(<LST>)
21{
22    if( /fall\s+(-?through\s+)?(to\s+)?([a-z0-9_]+)/i )
23    {
24        $ok{ $3 } = 1;
25    }
26    if( /unused\s+entrypoint\s+ok/i && /^\s*\d+\s+\<\d\>\s([a-z0-9_]+)\:/i )
27    {
28        $ok{ $1 } = 1;
29    }
30    if( /jump\s*table\s+entrypoint/i && /^\s*\d+\s+\<\d\>\s([a-z0-9_]+)\:/i )
31    {
32        $ok{ $1 } = 1;
33    }
34}
35
36while(<UNUSED>)
37{
38    if( /^([a-z0-9_]+\:)?\s+db\s+(.*)$/i || /^([a-z0-9_]+\:)?\s+dw\s+(.*)$/i || /^([a-z0-9_]+\:)?\s+mov\s+(.*)$/i ||
39        /^([a-z0-9_]+\:)?\s+call\s+(.*)$/i || /^([a-z0-9_]+\:)?\s+j[a-z]?[a-z]?[a-z]?[a-z]?[a-z]?\s+(.*)$/i ||
40        /^([a-z0-9_]+)?\s+equ\s+(.*)$/i )
41    {
42        $rem = $2;
43        @words = split( /([a-z0-9_]+)/i, $_ );
44        for( $t = 0; $t <= $#words; $t++ )
45        {
46            $jumptable{ $words[$t] } = 1;
47        }
48    }
49    if( !(/^g_sz/) && /^([a-z0-9_]+)\:/i )
50    {
51        push( @definition, $1 );
52    }
53}
54
55$results = 0;
56for( $t = 0; $t <= $#definition; $t++ )
57{
58    $d = $definition[$t];
59    if( !$ok{$d} && !$jumptable{$d} )
60    {
61        print $definition[$t]."\n";
62        $results++;
63    }
64}
65
66print ">>>> Unused Count: ".$results."\n";
Note: See TracBrowser for help on using the repository browser.