[194] | 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
|
---|
[526] | 5 | #
|
---|
[194] | 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:
|
---|
[489] | 10 | # a) if a label can be fallen into, place "; fall through to <label>" above the label
|
---|
[194] | 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 | #
|
---|
[376] | 14 | #
|
---|
[526] | 15 | # XTIDE Universal BIOS and Associated Tools
|
---|
| 16 | # Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
|
---|
[376] | 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.
|
---|
[526] | 22 | #
|
---|
[376] | 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
|
---|
[526] | 26 | # GNU General Public License for more details.
|
---|
[376] | 27 | # Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
---|
| 28 | #
|
---|
[194] | 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 | {
|
---|
[526] | 53 | if( /^([a-z0-9_]+\:)?\s+db\s+(.*)$/i ||
|
---|
| 54 | /^([a-z0-9_]+\:)?\s+dw\s+(.*)$/i ||
|
---|
[489] | 55 | /^([a-z0-9_]+\:)?\s+mov\s+(.*)$/i ||
|
---|
[526] | 56 | /^([a-z0-9_]+\:)?\s+call\s+(.*)$/i ||
|
---|
| 57 | /^([a-z0-9_]+\:)?\s+push\s+(.*)$/i ||
|
---|
[532] | 58 | /^([a-z0-9_]+\:)?\s+loop\s+(.*)$/i ||
|
---|
[489] | 59 | /^([a-z0-9_]+\:)?\s+j[a-z]?[a-z]?[a-z]?[a-z]?[a-z]?\s+(.*)$/i ||
|
---|
[194] | 60 | /^([a-z0-9_]+)?\s+equ\s+(.*)$/i )
|
---|
| 61 | {
|
---|
| 62 | $rem = $2;
|
---|
| 63 | @words = split( /([a-z0-9_]+)/i, $_ );
|
---|
| 64 | for( $t = 0; $t <= $#words; $t++ )
|
---|
| 65 | {
|
---|
| 66 | $jumptable{ $words[$t] } = 1;
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | if( !(/^g_sz/) && /^([a-z0-9_]+)\:/i )
|
---|
| 70 | {
|
---|
| 71 | push( @definition, $1 );
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | $results = 0;
|
---|
| 76 | for( $t = 0; $t <= $#definition; $t++ )
|
---|
| 77 | {
|
---|
| 78 | $d = $definition[$t];
|
---|
| 79 | if( !$ok{$d} && !$jumptable{$d} )
|
---|
| 80 | {
|
---|
| 81 | print $definition[$t]."\n";
|
---|
| 82 | $results++;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | print ">>>> Unused Count: ".$results."\n";
|
---|