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