15b343a4bSBrian Gaeke#!/usr/bin/perl
25b343a4bSBrian Gaeke
3*d5676e86SBrian Gaekeuse Getopt::Std;
4*d5676e86SBrian Gaeke
55b343a4bSBrian Gaekesub parse_objdump_file {
65b343a4bSBrian Gaeke  my ($filename) = @_;
75b343a4bSBrian Gaeke  my @result;
85b343a4bSBrian Gaeke  open (INPUT, $filename) or die "$filename: $!\n";
95b343a4bSBrian Gaeke  while (<INPUT>) {
105b343a4bSBrian Gaeke    if (/\s*([0-9a-f]*):\t(([0-9a-f]{2} )+) *\t(.*)$/) {
115b343a4bSBrian Gaeke      my ($addr, $bytes, $instr) = ($1, $2, $4);
125b343a4bSBrian Gaeke      $addr = "0x" . $addr;
135b343a4bSBrian Gaeke      $bytes =~ s/\s*(.*\S)\s*/$1/; # trim any remaining whitespace
145b343a4bSBrian Gaeke      $instr =~ s/\s*(.*\S)\s*/$1/;
155b343a4bSBrian Gaeke      push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr});
165b343a4bSBrian Gaeke    }
175b343a4bSBrian Gaeke  }
185b343a4bSBrian Gaeke  close INPUT;
195b343a4bSBrian Gaeke  return @result;
205b343a4bSBrian Gaeke}
215b343a4bSBrian Gaeke
225b343a4bSBrian Gaekesub parse_gdb_file {
235b343a4bSBrian Gaeke  my ($filename) = @_;
245b343a4bSBrian Gaeke  my @result;
255b343a4bSBrian Gaeke  my $got_addr;
265b343a4bSBrian Gaeke  open (INPUT, $filename) or die "$filename: $!\n";
275b343a4bSBrian Gaeke  while (<INPUT>) {
285b343a4bSBrian Gaeke    if (/^(0x[0-9a-f]*):\t([^\t]*)\t[^:]*:\t((0x[0-9a-f]{2}\s*)+)\s*$/) {
295b343a4bSBrian Gaeke      my ($addr, $bytes, $instr) = ($1, $3, $2);
305b343a4bSBrian Gaeke      $bytes =~ s/0x//g;
315b343a4bSBrian Gaeke      $bytes =~ s/\s+/ /g;           # regularize whitespace
325b343a4bSBrian Gaeke      $bytes =~ s/\s*(.*\S)\s*/$1/;  # trim any remaining whitespace
335b343a4bSBrian Gaeke      $instr =~ s/\s*(.*\S)\s*/$1/;
345b343a4bSBrian Gaeke      push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr});
355b343a4bSBrian Gaeke    } elsif (/^(0x[0-9a-f]*):\t$/) { # deal with gdb's line breaker
365b343a4bSBrian Gaeke      $got_addr = $1;
375b343a4bSBrian Gaeke    } elsif ($got_addr && /^    ([^\t]*)\t[^:]*:\t((0x[0-9a-f]{2}\s*)+)\s*$/) {
385b343a4bSBrian Gaeke      my ($addr, $bytes, $instr) = ($got_addr, $2, $1);
395b343a4bSBrian Gaeke      $bytes =~ s/0x//g;
405b343a4bSBrian Gaeke      $bytes =~ s/\s+/ /g;           # regularize whitespace
415b343a4bSBrian Gaeke      $bytes =~ s/\s*(.*\S)\s*/$1/;  # trim any remaining whitespace
425b343a4bSBrian Gaeke      $instr =~ s/\s*(.*\S)\s*/$1/;
435b343a4bSBrian Gaeke      push (@result, {'addr' => $addr, 'bytes' => $bytes, 'instr' => $instr});
445b343a4bSBrian Gaeke      undef $got_addr;
455b343a4bSBrian Gaeke    }
465b343a4bSBrian Gaeke  }
475b343a4bSBrian Gaeke  close INPUT;
485b343a4bSBrian Gaeke  return @result;
495b343a4bSBrian Gaeke}
505b343a4bSBrian Gaeke
515b343a4bSBrian Gaekesub binary_diffs {
525b343a4bSBrian Gaeke  my ($objdump_file, $gdb_file) = @_;
535b343a4bSBrian Gaeke  my @file1 = parse_objdump_file ($objdump_file);
545b343a4bSBrian Gaeke  my @file2 = parse_gdb_file ($gdb_file);
555b343a4bSBrian Gaeke  my $lastrecord = ($#file1 >= $#file2) ? ($#file1) : ($#file2);
565b343a4bSBrian Gaeke  for (my $i = 0; $i <= $lastrecord; ++$i) {
575b343a4bSBrian Gaeke    my $d1 = $file1[$i];
585b343a4bSBrian Gaeke    my $d2 = $file2[$i];
595b343a4bSBrian Gaeke    if ($d1->{'bytes'} ne $d2->{'bytes'}) {
60*d5676e86SBrian Gaeke      next if (($d1->{'instr'} eq $d2->{'instr'}) && $opt_d);
615b343a4bSBrian Gaeke      printf "0x%08x:\t%30s \t%s\n", 0+$d1->{'addr'}, $d1->{'bytes'}, $d1->{'instr'};
625b343a4bSBrian Gaeke      printf "0x%08x:\t%30s \t%s\n\n", 0+$d2->{'addr'}, $d2->{'bytes'}, $d2->{'instr'};
635b343a4bSBrian Gaeke    }
645b343a4bSBrian Gaeke  }
655b343a4bSBrian Gaeke}
665b343a4bSBrian Gaeke
67*d5676e86SBrian Gaeke&getopts('d');
685b343a4bSBrian Gaeke$objdump_file = $ARGV[0];
695b343a4bSBrian Gaeke$gdb_file = $ARGV[1];
705b343a4bSBrian Gaekebinary_diffs ($objdump_file, $gdb_file);
715b343a4bSBrian Gaekeexit (0);
725b343a4bSBrian Gaeke__END__
735b343a4bSBrian Gaeke=pod
745b343a4bSBrian Gaeke
755b343a4bSBrian Gaeke=head1 NAME
765b343a4bSBrian Gaeke
775b343a4bSBrian Gaekecodegen-diff
785b343a4bSBrian Gaeke
795b343a4bSBrian Gaeke=head1 SYNOPSIS
805b343a4bSBrian Gaeke
81*d5676e86SBrian Gaekecodegen-diff [-d] I<OBJDUMP-OUTPUT-FILE> I<GDB-DISASSEMBLY-FILE>
825b343a4bSBrian Gaeke
835b343a4bSBrian Gaeke=head1 DESCRIPTION
845b343a4bSBrian Gaeke
855b343a4bSBrian GaekeB<codegen-diff> is a program that tries to show you the differences
865b343a4bSBrian Gaekebetween the code that B<llc> generated and the code that B<lli> generated.
875b343a4bSBrian Gaeke
885b343a4bSBrian GaekeThe way you use it is as follows: first, you create I<OBJDUMP-OUTPUT-FILE>
895b343a4bSBrian Gaekeby running B<objdump> on the B<llc> compiled and linked binary. You need to
905b343a4bSBrian Gaeketrim down the result so it contains only the function of interest.
915b343a4bSBrian Gaeke
925b343a4bSBrian GaekeSecond, you create I<GDB-DISASSEMBLY-FILE> by running B<gdb>, with my patch
935b343a4bSBrian Gaeketo print out hex bytes in the B<disassemble> command output, on
945b343a4bSBrian GaekeB<lli>.  Set a breakpoint in C<Emitter::finishFunction()> and wait until
955b343a4bSBrian Gaekethe function you want is compiled.  Then use the B<disassemble> command
965b343a4bSBrian Gaeketo print out the assembly dump of the function B<lli> just compiled.
975b343a4bSBrian Gaeke(Use C<lli -debug> to find out where the function starts and ends in memory.)
985b343a4bSBrian GaekeIt's easiest to save this output by using B<script>.
995b343a4bSBrian Gaeke
1005b343a4bSBrian GaekeFinally, you run B<codegen-diff>, as indicated in the Synopsis section of
1015b343a4bSBrian Gaekethis manpage. It will print out a two-line stanza for each mismatched
1025b343a4bSBrian Gaekeinstruction, with the  B<llc> version first, and the  B<lli> version second.
1035b343a4bSBrian Gaeke
104*d5676e86SBrian Gaeke=head1 OPTIONS
105*d5676e86SBrian Gaeke
106*d5676e86SBrian Gaeke=over 4
107*d5676e86SBrian Gaeke
108*d5676e86SBrian Gaeke=item -d
109*d5676e86SBrian Gaeke
110*d5676e86SBrian GaekeDon't show instructions where the bytes are different but they
111*d5676e86SBrian Gaekedisassemble to the same thing. This puts a lot of trust in the
112*d5676e86SBrian Gaekedisassembler, but it might help you highlight the more egregious cases
113*d5676e86SBrian Gaekeof misassembly.
114*d5676e86SBrian Gaeke
115*d5676e86SBrian Gaeke=back
116*d5676e86SBrian Gaeke
1175b343a4bSBrian Gaeke=head1 AUTHOR
1185b343a4bSBrian Gaeke
1195b343a4bSBrian GaekeB<codegen-diff> was written by Brian Gaeke.
1205b343a4bSBrian Gaeke
1215b343a4bSBrian Gaeke=head1 SEE ALSO
1225b343a4bSBrian Gaeke
1235b343a4bSBrian GaekeL<gdb(1)>, L<objdump(1)>, L<script(1)>.
1245b343a4bSBrian Gaeke
1255b343a4bSBrian GaekeYou will need my B<gdb> patch:
1265b343a4bSBrian Gaeke
1275b343a4bSBrian Gaeke  http://llvm.cs.uiuc.edu/~gaeke/gdb-disassembly-print-bytes.patch
1285b343a4bSBrian Gaeke
1295b343a4bSBrian Gaeke=cut
130