wok view linld/stuff/src/pipehole.awk @ rev 21735

vlgothic-fonts: use archive.org as web_site
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Jun 13 15:52:10 2019 +0200 (2019-06-13)
parents 861efbf7a5de
children f9e283da869a
line source
1 BEGIN { hold=0; is386=0; isload=0; isiso=0; wascall=0 }
2 function isnum(n) { return match(n,/^[0-9+-]/) }
3 {
4 sub(/segment word public/,"segment byte public")
6 if (/^@.*:$/ || / endp$/) afterjmp=0
7 if (/dword ptr/) is386=1
8 if (/heap_top = _rm_buf/) isload=1
9 if (isload) { # LOAD.LST
10 if (/mov al,byte ptr/ && is386) {
11 print " movzx eax,byte ptr [si]"
12 next
13 }
14 if (/ax,word ptr/) next
15 if (/^ call/) isload=0
16 }
17 if (/x->curdirsize == 0xFFFF/) isiso=4
18 if (isiso == 4) { # ISO9660.LST
19 if (/DGROUP:_isostate\+14,-1/) {
20 sub(/DGROUP:_isostate\+14/,"[si+14]")
21 isiso=0
22 }
23 }
24 if (/c = \*s;/) isiso=3
25 if (isiso == 3) { # ISO9660.LST
26 if (/al,byte ptr/) {
27 print " mov al,0"
28 sub(/mov/,"xchg")
29 }
30 if (/byte ptr \[di\],0/) {
31 isiso=0
32 next
33 }
34 }
35 if (/endname = NULL/) isiso=2
36 if (isiso == 2) { # ISO9660.LST
37 if (/mov bx,cx/) next
38 gsub(/cx/,"bx")
39 }
40 if (/const char \*n = name/) isiso=1
41 if (isiso == 1) { # ISO9660.LST
42 if ((/mov word ptr \[si\+32\],ax/ ) ||
43 (/mov ax,word ptr \[si\+28\]/) ||
44 (/bx,word ptr \[si\+32\]/) || (/ax,dx/)) next
45 if (/dx,/) sub(/dx/,"ax")
46 if ((/sub ax,word ptr \[si\+28\]/) ||
47 (/\[si\+12\]/) || (/ax,di/)) sub(/ax/,"bx")
48 if (/add word ptr \[si\+32\],ax/) $0=" add bx,word ptr [si+12]"
49 if (/al,/ || /,al/) sub(/al/,"cl")
50 if (/cmp byte ptr \[si\+30\],0/) $0=" or cl,cl"
51 if (/jne @@0$/) next
52 if (/jmp @3@58$/) $0=" je @3@58"
53 }
54 if (wascall) {
55 if (rcall != "") {
56 if (/,ax$/) print " mov " rcall ",ax"
57 else print " xchg ax," rcall
58 wascall=0
59 }
60 else if (/^ mov .i,ax$/) {
61 split($2,y,",")
62 rcall=y[1]
63 next
64 }
65 else wascall=0
66 }
67 if (/^ call /) { wascall=1; rcall="" }
68 if (hold == 0) {
69 s=$0
70 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
71 r=$2; kept=0
72 hold=1; split($2,regs,","); next
73 }
74 if (/^ inc e?.[ixhl]/ || /^ dec e?.[ixhl]/) {
75 hold=2; r=$2; next
76 }
77 if (/^ mov [abcds][ix],/ && ! /,.s/) {
78 hold=3; split($2,regs,","); next
79 }
80 if (/^ movzx eax,ax$/) { hold=4; next }
81 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
82 split($0,regs,",")
83 if (isnum(regs[2]) && regs[2] != 0 &&
84 (regs[2] % 256) == 0) {
85 hold=5; next
86 }
87 }
88 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
89 if (/^ mov cl,4$/) { hold=7; next }
90 if (/^ cmp word ptr DGROUP:.*,0$/) {
91 hold=8; split($2,regs,","); next
92 }
93 if (/^ cbw/) { hold=11; kept=0; next }
94 if (/^ add [abcds][ix],2$/) {
95 split($2,regs,","); hold=12; next
96 }
97 if (/^ sub [abcds][ix],2$/) {
98 split($2,regs,","); hold=13; next
99 }
100 if (/^ push dx$/) {
101 hold=14; next;
102 }
103 }
104 else if (hold == 1) {
105 if (/^ ;/) { line[kept++]=$0; next }
106 hold=0; split($2,args,","); op=""
107 if ($1 == "add") op="+"
108 if ($1 == "sub") op="-"
109 if ($1 == "inc") { op="+"; args[2]="1"; }
110 if ($1 == "dec") { op="-"; args[2]="1"; }
111 if (op != "" && regs[1] == args[1]) {
112 if (isnum(args[2])) {
113 for (i = kept++; i > 0; i--) line[i] = line[i-1]
114 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
115 hold=10; next
116 }
117 line[kept++]=$0
118 hold=1
119 next
120 }
121 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
122 print " xchg " r
123 }
124 else print s
125 for (i = 0; i < kept; i++) print line[i]; kept=0
126 }
127 else if (hold == 2) {
128 split($0,args,",")
129 if (/^ mov / && r == args[2]) { print s; s=$0; next }
130 split($2,args,",")
131 hold=0; print s
132 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
133 }
134 else if (hold == 3) {
135 hold=0
136 if (/^ call / && regs[2] == "ax") s=" xchg ax," regs[1]
137 if (/^ add [abcds][ix],/) {
138 split($2,regs2,",")
139 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
140 t=$0; sub(/mov/,$1,s); sub(/add/,"mov",t)
141 print t; print s; next
142 }
143 }
144 print s
145 }
146 else if (hold == 4) {
147 hold=0
148 if (/^ push eax$/) {
149 print " push 0"; print " push ax"; next
150 } else { print s }
151 }
152 else if (hold == 5) {
153 hold=0
154 if ($1 == "jae" || $1 == "jb") {
155 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
156 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
157 s = s "/256"
158 }
159 print s
160 }
161 else if (hold == 6) {
162 if (($1 == "and" || $1 == "add") && $2 ~ /^ax,/) {
163 line[kept++]=$0
164 next
165 }
166 p=$0
167 if (/^ movzx eax,ax$/) {
168 s=" mov eax,cs"; p=""
169 }
170 print s
171 for (i = 0; i < kept; i++) print line[i]; kept=0
172 if (p != "") print p
173 hold=0; next
174 }
175 else if (hold == 7) {
176 hold=0
177 if (/^ call near ptr N_LXURSH@$/) {
178 print " extrn N_LXURSH@4:near"
179 print " call near ptr N_LXURSH@4"
180 next
181 }
182 if (/^ call near ptr N_LXLSH@$/) {
183 print " extrn N_LXLSH@4:near"
184 print " call near ptr N_LXLSH@4"
185 next
186 }
187 print s
188 }
189 else if (hold == 8) {
190 if ($1 == "je" || $1 == "jne") { p=$0; hold=9; next }
191 hold=0
192 print s
193 }
194 else if (hold == 9) {
195 hold=0; split($2,args,",")
196 if (/^ mov ax,/ && args[2] == regs[1]) {
197 print; print " or ax,ax"; print p; next
198 }
199 print s; print p;
200 }
201 else if (hold == 10) {
202 split($2,args,","); op=""
203 if ($1 == "add") op="+"
204 if ($1 == "sub") op="-"
205 if ($1 == "inc") { op="+"; args[2]="1"; }
206 if ($1 == "dec") { op="-"; args[2]="1"; }
207 if (op != "" && isnum(args[2])) {
208 split(line[0],reg,",")
209 if (substr(reg[1],length(reg[1])-1,2) == args[1]) {
210 line[0] = substr(line[0],1,length(line[0])-1) op args[2] "]"
211 next
212 }
213 }
214 hold=0
215 if (/^ mov [sd]i,ax$/) {
216 split($2,args,",")
217 for (i = 0; i < kept; i++) {
218 sub(/ax/,args[1],line[i]); print line[i]
219 }
220 next
221 }
222 for (i = 0; i < kept; i++) print line[i]
223 }
224 else if (hold == 11) {
225 if (/^ inc ax$/ || /^ dec ax$/) {
226 line[kept++]=$0; next
227 }
228 split($2,args,",")
229 if (/^ mov cl,/) {
230 split($2,args,",")
231 if (args[2] >= 8) {
232 line[kept++]=$0; next
233 }
234 }
235 if (!/^ shl ax,/ || (args[2] != "cl" && args[2] < 8)) {
236 print " cbw "
237 }
238 for (i = 0; i < kept; i++) print line[i]
239 hold=kept=0
240 }
241 else if (hold == 12) {
242 hold=0
243 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
244 print " inc " regs[1]
245 print " inc " regs[1]
246 }
247 else print " add " regs[1] ",2"
248 }
249 else if (hold == 13) {
250 hold=0
251 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
252 print " dec " regs[1]
253 print " dec " regs[1]
254 }
255 else print " sub " regs[1] ",2"
256 }
257 else if (hold == 14) {
258 if (/^ push ax$/) { hold++; next; }
259 print " push dx";
260 hold=0;
261 }
262 else if (hold == 15) {
263 if (/^ pop eax$/) { hold++; next; }
264 print " push dx";
265 print " push ax";
266 hold=0;
267 }
268 else if (hold == 16) {
269 hold=0;
270 if (/^ shr eax,16$/) { print " xchg ax,dx"; next; }
271 print " push dx";
272 print " push ax";
273 print " pop eax";
274 }
275 else if (hold == 17) {
276 hold=0;
277 if (/^ cmp ax,-1$/) { print " inc ax"; next; }
278 }
279 if (/^ call near ptr @fileexist\$/ || # return boolean :
280 /^ call near ptr @isoreaddir\$/ || # 0=true, -1=false
281 /^ call near ptr @isoreset\$/ ||
282 /^ call near ptr @isoopen\$/ ||
283 /^ call near ptr @isoreadsector\$/ ||
284 /^ call near ptr @strhead\$/ ||
285 /^ call near ptr @argstr\$/ ||
286 /^ call near ptr @argnum\$/) { print; hold=17; next; }
287 s=$0
288 # These optimisation may break ZF or CF
289 if (/^ sub sp,2$/) { print " push ax"; next }
290 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
291 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
292 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
293 sub(/mov/,"and",s); print s; next # slower
294 }
295 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
296 sub(/mov/,"or",s); print s; next # slower
297 }
298 if (/^ or .*,0$/ || /^ and .*,-1$/) next
299 if (/^ or [abcd]x,/) {
300 split($2,args,",")
301 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
302 print " or " substr(args[1],1,1) "l," args[2]; next
303 }
304 }
305 if (/^ and [abcd]x,/) {
306 split($2,args,",")
307 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
308 print " and " substr(args[1],1,1) "l," args[2]; next
309 }
310 }
311 if (/^ or e[abcd]x,/) {
312 split($2,args,",")
313 if (args[2] == "large") { args[2] = $3 }
314 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
315 print " or " substr(args[1],2,1) "l," args[2]; next
316 }
317 }
318 if (/^ and e[abcd]x,/) {
319 split($2,args,",")
320 if (args[2] == "large") { args[2] = $3 }
321 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
322 print " and " substr(args[1],2,1) "l," args[2]; next
323 }
324 }
325 if (/^ or e[abcds][ix],/) {
326 split($2,args,",")
327 if (args[2] == "large") { args[2] = $3 }
328 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
329 print " or " substr(args[1],2) "," args[2]; next
330 }
331 }
332 if (/^ and e[abcds][ix],/) {
333 split($2,args,",")
334 if (args[2] == "large") { args[2] = $3 }
335 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
336 print " and " substr(args[1],2) "," args[2]; next
337 }
338 }
339 if (/^ add word ptr/ || /^ sub word ptr/ ||
340 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
341 split($0,args,",")
342 if (isnum(args[2]) && (args[2] % 256 == 0)) {
343 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
344 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
345 print s "/256"; next
346 }
347 }
348 if (/^ add dword ptr/ || /^ sub dword ptr/) {
349 split($0,args,",")
350 if (args[2] == "large") { args[2] = $3 }
351 if (isnum(args[2])) {
352 if (args[2] % 16777216 == 0) {
353 sub(/dword/,"byte",s)
354 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
355 print s "/16777216"; next
356 }
357 if (args[2] % 65536 == 0) {
358 sub(/dword/,"word",s)
359 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
360 print s "/65536"; next
361 }
362 }
363 }
364 if (/^ mov e.x,/) {
365 split($2,args,",")
366 r=args[1]
367 if (args[2] == "large") { args[2] = $3 }
368 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
369 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
370 print " xor " r "," r
371 if (args[2] == 0) next
372 x=" mov " substr(r,2,1)
373 if (args[2] % 256 == 0) {
374 print x "h," args[2] "/256"
375 }
376 else { print x "l," args[2] }
377 next
378 }
379 }
380 }
381 if (afterjmp) print ";" $0
382 else print
383 if (/^ jmp /) afterjmp=1
384 }