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