Brian Gaeke | 5b343a4 | 2003-07-03 21:15:02 +0000 | [diff] [blame^] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | sub parse_objdump_file { |
| 4 | my ($filename) = @_; |
| 5 | my @result; |
| 6 | open (INPUT, $filename) or die "$filename: $!\n"; |
| 7 | while (<INPUT>) { |
| 8 | if (/\s*([0-9a-f]*):\t(([0-9a-f]{2} )+) *\t(.*)$/) { |
| 9 | my ($addr, $bytes, $instr) = ($1, $2, $4); |
| 10 | $addr = "0x" . $addr; |
| 11 | $bytes =~ s/\s*(.*\S)\s*/$1/; # trim any remaining whitespace |
| 12 | $instr =~ s/\s*(.*\S)\s*/$1/; |
| 13 | push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr}); |
| 14 | } |
| 15 | } |
| 16 | close INPUT; |
| 17 | return @result; |
| 18 | } |
| 19 | |
| 20 | sub parse_gdb_file { |
| 21 | my ($filename) = @_; |
| 22 | my @result; |
| 23 | my $got_addr; |
| 24 | open (INPUT, $filename) or die "$filename: $!\n"; |
| 25 | while (<INPUT>) { |
| 26 | if (/^(0x[0-9a-f]*):\t([^\t]*)\t[^:]*:\t((0x[0-9a-f]{2}\s*)+)\s*$/) { |
| 27 | my ($addr, $bytes, $instr) = ($1, $3, $2); |
| 28 | $bytes =~ s/0x//g; |
| 29 | $bytes =~ s/\s+/ /g; # regularize whitespace |
| 30 | $bytes =~ s/\s*(.*\S)\s*/$1/; # trim any remaining whitespace |
| 31 | $instr =~ s/\s*(.*\S)\s*/$1/; |
| 32 | push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr}); |
| 33 | } elsif (/^(0x[0-9a-f]*):\t$/) { # deal with gdb's line breaker |
| 34 | $got_addr = $1; |
| 35 | } elsif ($got_addr && /^ ([^\t]*)\t[^:]*:\t((0x[0-9a-f]{2}\s*)+)\s*$/) { |
| 36 | my ($addr, $bytes, $instr) = ($got_addr, $2, $1); |
| 37 | $bytes =~ s/0x//g; |
| 38 | $bytes =~ s/\s+/ /g; # regularize whitespace |
| 39 | $bytes =~ s/\s*(.*\S)\s*/$1/; # trim any remaining whitespace |
| 40 | $instr =~ s/\s*(.*\S)\s*/$1/; |
| 41 | push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr}); |
| 42 | undef $got_addr; |
| 43 | } |
| 44 | } |
| 45 | close INPUT; |
| 46 | return @result; |
| 47 | } |
| 48 | |
| 49 | sub binary_diffs { |
| 50 | my ($objdump_file, $gdb_file) = @_; |
| 51 | my @file1 = parse_objdump_file ($objdump_file); |
| 52 | my @file2 = parse_gdb_file ($gdb_file); |
| 53 | my $lastrecord = ($#file1 >= $#file2) ? ($#file1) : ($#file2); |
| 54 | for (my $i = 0; $i <= $lastrecord; ++$i) { |
| 55 | my $d1 = $file1[$i]; |
| 56 | my $d2 = $file2[$i]; |
| 57 | if ($d1->{'bytes'} ne $d2->{'bytes'}) { |
| 58 | printf "0x%08x:\t%30s \t%s\n", 0+$d1->{'addr'}, $d1->{'bytes'}, $d1->{'instr'}; |
| 59 | printf "0x%08x:\t%30s \t%s\n\n", 0+$d2->{'addr'}, $d2->{'bytes'}, $d2->{'instr'}; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $objdump_file = $ARGV[0]; |
| 65 | $gdb_file = $ARGV[1]; |
| 66 | binary_diffs ($objdump_file, $gdb_file); |
| 67 | exit (0); |
| 68 | __END__ |
| 69 | =pod |
| 70 | |
| 71 | =head1 NAME |
| 72 | |
| 73 | codegen-diff |
| 74 | |
| 75 | =head1 SYNOPSIS |
| 76 | |
| 77 | codegen-diff I<OBJDUMP-OUTPUT-FILE> I<GDB-DISASSEMBLY-FILE> |
| 78 | |
| 79 | =head1 DESCRIPTION |
| 80 | |
| 81 | B<codegen-diff> is a program that tries to show you the differences |
| 82 | between the code that B<llc> generated and the code that B<lli> generated. |
| 83 | |
| 84 | The way you use it is as follows: first, you create I<OBJDUMP-OUTPUT-FILE> |
| 85 | by running B<objdump> on the B<llc> compiled and linked binary. You need to |
| 86 | trim down the result so it contains only the function of interest. |
| 87 | |
| 88 | Second, you create I<GDB-DISASSEMBLY-FILE> by running B<gdb>, with my patch |
| 89 | to print out hex bytes in the B<disassemble> command output, on |
| 90 | B<lli>. Set a breakpoint in C<Emitter::finishFunction()> and wait until |
| 91 | the function you want is compiled. Then use the B<disassemble> command |
| 92 | to print out the assembly dump of the function B<lli> just compiled. |
| 93 | (Use C<lli -debug> to find out where the function starts and ends in memory.) |
| 94 | It's easiest to save this output by using B<script>. |
| 95 | |
| 96 | Finally, you run B<codegen-diff>, as indicated in the Synopsis section of |
| 97 | this manpage. It will print out a two-line stanza for each mismatched |
| 98 | instruction, with the B<llc> version first, and the B<lli> version second. |
| 99 | |
| 100 | =head1 AUTHOR |
| 101 | |
| 102 | B<codegen-diff> was written by Brian Gaeke. |
| 103 | |
| 104 | =head1 SEE ALSO |
| 105 | |
| 106 | L<gdb(1)>, L<objdump(1)>, L<script(1)>. |
| 107 | |
| 108 | You will need my B<gdb> patch: |
| 109 | |
| 110 | http://llvm.cs.uiuc.edu/~gaeke/gdb-disassembly-print-bytes.patch |
| 111 | |
| 112 | =cut |