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