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 | # |
---|
15 | # XTIDE Universal BIOS and Associated Tools |
---|
16 | # Copyright (C) 2009-2010 by Tomi Tilli, 2011-2012 by XTIDE Universal BIOS Team. |
---|
17 | # |
---|
18 | # This program is free software; you can redistribute it and/or modify |
---|
19 | # it under the terms of the GNU General Public License as published by |
---|
20 | # the Free Software Foundation; either version 2 of the License, or |
---|
21 | # (at your option) any later version. |
---|
22 | # |
---|
23 | # This program is distributed in the hope that it will be useful, |
---|
24 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
25 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
26 | # GNU General Public License for more details. |
---|
27 | # Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
28 | # |
---|
29 | |
---|
30 | print "::".$ARGV[0]."::".$ARGV[1]."::\n"; |
---|
31 | |
---|
32 | open( LST, "<", $ARGV[0] ) || die "cannot open listing: ".$ARGV[0]; |
---|
33 | open( UNUSED, "<", $ARGV[1] ) || die "cannot open unused.asm: ".$ARGV[1]; |
---|
34 | |
---|
35 | while(<LST>) |
---|
36 | { |
---|
37 | if( /fall\s+(-?through\s+)?(to\s+)?([a-z0-9_]+)/i ) |
---|
38 | { |
---|
39 | $ok{ $3 } = 1; |
---|
40 | } |
---|
41 | if( /unused\s+entrypoint\s+ok/i && /^\s*\d+\s+\<\d\>\s([a-z0-9_]+)\:/i ) |
---|
42 | { |
---|
43 | $ok{ $1 } = 1; |
---|
44 | } |
---|
45 | if( /jump\s*table\s+entrypoint/i && /^\s*\d+\s+\<\d\>\s([a-z0-9_]+)\:/i ) |
---|
46 | { |
---|
47 | $ok{ $1 } = 1; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | while(<UNUSED>) |
---|
52 | { |
---|
53 | if( /^([a-z0-9_]+\:)?\s+db\s+(.*)$/i || /^([a-z0-9_]+\:)?\s+dw\s+(.*)$/i || /^([a-z0-9_]+\:)?\s+mov\s+(.*)$/i || |
---|
54 | /^([a-z0-9_]+\:)?\s+call\s+(.*)$/i || /^([a-z0-9_]+\:)?\s+j[a-z]?[a-z]?[a-z]?[a-z]?[a-z]?\s+(.*)$/i || |
---|
55 | /^([a-z0-9_]+)?\s+equ\s+(.*)$/i ) |
---|
56 | { |
---|
57 | $rem = $2; |
---|
58 | @words = split( /([a-z0-9_]+)/i, $_ ); |
---|
59 | for( $t = 0; $t <= $#words; $t++ ) |
---|
60 | { |
---|
61 | $jumptable{ $words[$t] } = 1; |
---|
62 | } |
---|
63 | } |
---|
64 | if( !(/^g_sz/) && /^([a-z0-9_]+)\:/i ) |
---|
65 | { |
---|
66 | push( @definition, $1 ); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | $results = 0; |
---|
71 | for( $t = 0; $t <= $#definition; $t++ ) |
---|
72 | { |
---|
73 | $d = $definition[$t]; |
---|
74 | if( !$ok{$d} && !$jumptable{$d} ) |
---|
75 | { |
---|
76 | print $definition[$t]."\n"; |
---|
77 | $results++; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | print ">>>> Unused Count: ".$results."\n"; |
---|