2
0

codegen-diff 4.3 KB

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