3 # Copyright 2000, 2001 Free Software Foundation, Inc.
5 # This file is part of the GNU MP Library.
7 # The GNU MP Library is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published
9 # by the Free Software Foundation; either version 3 of the License, or (at
10 # your option) any later version.
12 # The GNU MP Library is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 # License for more details.
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with the GNU MP Library. If not, see http://www.gnu.org/licenses/.
21 # Usage: cross.pl [filename.o]...
23 # Produce an annotated disassembly of the given object files, indicating
24 # certain code alignment and addressing mode problems afflicting K6 chips.
25 # "ZZ" is used on all annotations, so this can be searched for.
27 # With no arguments, all .o files corresponding to .asm files are processed.
28 # This is good in the mpn object directory of a k6*-*-* build.
30 # Code alignments of 8 bytes or more are handled. When 32 is used, cache
31 # line boundaries will fall in at offsets 0x20,0x40,etc and problems are
32 # flagged at those locations. When 16 is used, the line boundaries can also
33 # fall at offsets 0x10,0x30,0x50,etc, depending where the file is loaded, so
34 # problems are identified there too. Likewise when 8 byte alignment is used
35 # problems are flagged additionally at 0x08,0x18,0x28,etc.
37 # Usually 32 byte alignment is used for k6 routines, but less is certainly
38 # possible if through good luck, or a little tweaking, cache line crossing
39 # problems can be avoided at the extra locations.
43 # Instructions without mod/rm bytes or which are already vector decoded are
44 # unaffected by cache line boundary crossing, but not all of these have yet
45 # been put in as exceptions. All that occur in practice in GMP are present
48 # There's no messages for using the vector decoded addressing mode (%esi),
49 # but that's easy to avoid when coding.
53 # Warn about jump targets that are poorly aligned (less than 2 instructions
54 # before a cache line boundary).
60 my ($addr,$b1,$b2,$b3, $prefix,$opcode,$modrm);
63 open (IN, "objdump -Srfh $file |")
64 || die "Cannot open pipe from objdump\n";
68 if (/^[ \t]*[0-9]+[ \t]+\.text[ \t]/ && /2\*\*([0-9]+)$/) {
71 print "ZZ cross.pl cannot handle alignment < 2**3\n";
76 if (/^[ \t]*([0-9a-f]*):[ \t]*([0-9a-f]+)[ \t]+([0-9a-f]+)[ \t]+([0-9a-f]+)/) {
77 ($addr,$b1,$b2,$b3) = ($1,$2,$3,$4);
79 } elsif (/^[ \t]*([0-9a-f]*):[ \t]*([0-9a-f]+)[ \t]+([0-9a-f]+)/) {
80 ($addr,$b1,$b2,$b3) = ($1,$2,$3,'');
82 } elsif (/^[ \t]*([0-9a-f]*):[ \t]*([0-9a-f]+)/) {
83 ($addr,$b1,$b2,$b3) = ($1,$2,'','');
99 # modrm of the form 00-xxx-100 with an 0F prefix is the problem case
100 # for K6 and pre-CXT K6-2
102 && $opcode !~ /^8/ # jcond disp32
103 && $modrm =~ /^[0-3][4c]/) {
104 print "ZZ ($file) >3 bytes to determine instruction length [K6]\n";
107 # with just an opcode, starting 1f mod 20h
108 if (($align==32 && $addr =~ /[13579bdf]f$/
109 || $align==16 && $addr =~ /f$/
110 || $align==8 && $addr =~ /[7f]$/)
112 && $opcode !~ /1[012345]/ # adc
113 && $opcode !~ /1[89abcd]/ # sbb
114 && $opcode !~ /^4/ # inc/dec reg
115 && $opcode !~ /^5/ # push/pop reg
116 && $opcode !~ /68/ # push $imm32
117 && $opcode !~ /^7/ # jcond disp8
118 && $opcode !~ /a[89]/ # test+imm
119 && $opcode !~ /a[a-f]/ # stos/lods/scas
120 && $opcode !~ /b8/ # movl $imm32,%eax
121 && $opcode !~ /d[0123]/ # rcl
122 && $opcode !~ /e[0123]/ # loop/loopz/loopnz/jcxz
123 && $opcode !~ /e8/ # call disp32
124 && $opcode !~ /e[9b]/ # jmp disp32/disp8
125 && $opcode !~ /f[89abcd]/ # clc,stc,cli,sti,cld,std
126 && !($opcode =~ /f[67]/ # grp 1
127 && $modrm =~ /^[2367abef]/) # mul, imul, div, idiv
129 print "ZZ ($file) opcode/modrm cross 32-byte boundary\n";
132 # with an 0F prefix, anything starting at 1f mod 20h
133 if (($align==32 && $addr =~ /[13579bdf][f]$/
134 || $align==16 && $addr =~ /f$/
135 || $align==8 && $addr =~ /[7f]$/)
137 && $opcode !~ /af/ # imul
138 && $opcode !~ /a[45]/ # shldl
139 && $opcode !~ /a[cd]/ # shrdl
141 print "ZZ ($file) prefix/opcode cross 32-byte boundary\n";
144 # with an 0F prefix, anything with mod/rm starting at 1e mod 20h
145 if (($align==32 && $addr =~ /[13579bdf][e]$/
146 || $align==16 && $addr =~ /[e]$/
147 || $align==8 && $addr =~ /[6e]$/)
149 && $opcode !~ /^8/ # jcond disp32
150 && $opcode !~ /af/ # imull reg,reg
151 && $opcode !~ /a[45]/ # shldl
152 && $opcode !~ /a[cd]/ # shrdl
154 print "ZZ ($file) prefix/opcode/modrm cross 32-byte boundary\n";
157 close IN || die "Error from objdump (or objdump not available)\n";
165 @files = glob "*.asm";
166 map {s/.asm/.o/} @files;