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

Up firefox-official (67.0.3), tinyproxy (1.10.0)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Jun 19 17:26:14 2019 +0200 (2019-06-19)
parents 30270fe0d1b3
children 2373992ff751
line source
1 BEGIN { hold=0; is386=0; isload=0; isiso=0; istazboot=0; wascall=0; label="none"; xlabel="" }
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 (/void load_initrd\(\)/) isload=3
9 if (isload == 3) { # LOAD.LST
10 if(/push di/ || /pop di/) next
11 sub(/\[di/,"[bx")
12 sub(/\di,/,"bx,")
13 }
14 if (/vid_mode = vid_mode/) isload=2
15 if (isload == 2) { # LOAD.LST
16 sub(/,0/,""); sub(/cmp /,"mov cx,")
17 sub(/je/,"jcxz")
18 if (/ax,word/) next
19 sub(/,ax/,",cx")
20 if (/version_string/) isload=0
21 }
22 if (/heap_top = _rm_buf/) isload=1
23 if (isload == 1) { # LOAD.LST
24 if (/mov al,byte ptr/ && is386) {
25 print " movzx eax,byte ptr [si]"
26 next
27 }
28 if (/ax,word ptr/) next
29 if (/^ call/) isload=0
30 }
31 if (/CD001/) isiso=7
32 if (isiso == 7) { # ISO9660.LST
33 sub(/mov ax,-1/,"dec ax")
34 if (/jmp/) isiso=0
35 }
36 if (/int len =/) isiso=6
37 if (isiso == 6) { # ISO9660.LST
38 if (/dx,ax/) next
39 sub(/ax/,"dx")
40 sub(/cx,di/,"bx,di")
41 sub(/cx,dx/,"bx,ax")
42 sub(/di,dx/,"di,ax")
43 if (/while/) isiso=2
44 }
45 if (/entrysize =/) isiso=5
46 if (isiso == 5) { # ISO9660.LST
47 if (/ax,ax/) next
48 sub(/ax/,"cx")
49 sub(/je/,"jcxz")
50 if (/return/) isiso=0
51 }
52 if (/x->curdirsize == 0xFFFF/) isiso=4
53 if (isiso == 4) { # ISO9660.LST
54 sub(/DGROUP:_isostate\+18/,"[si+18]")
55 sub(/DGROUP:_isostate\+20/,"[si+20]")
56 if (/goto restarted/) isiso=0
57 }
58 if (/do s\+\+; while/) isiso=3
59 if (/for \(p = s; \*s && \*s \!=/) isiso=3
60 if (isiso == 3) { # ISO9660.LST, TAZBOOT.LST
61 sub(/cmp byte ptr \[.i\]/,"sub al")
62 if (/mov byte ptr \[bp-5\],al/) $0=" push ax"
63 if (/mov al,byte ptr \[bp-5\]/) $0=" pop ax"
64 if (/inc /) { r=$2; print; next }
65 if (/al,0/) print " mov al,[" r "]"
66 if (/al,byte ptr/) sub(/mov/,"xchg")
67 if (/byte ptr \[.*\],0/) next
68 if (/jmp/) print " mov bx,si"
69 if (/word ptr \[bp-4\]/) next
70 if (/\) s\+\+;/ || /\],-1/) isiso=0
71 }
72 if (/endname = NULL/) isiso=2
73 if (isiso == 2) { # ISO9660.LST
74 if (/mov bx,cx/) next
75 gsub(/cx/,"bx")
76 sub(/DGROUP:_isostate\+35/,"[si+35]")
77 }
78 if (/const char \*n = name/) isiso=1
79 if (isiso == 1) { # ISO9660.LST
80 if ((/mov word ptr \[si\+32\],ax/ ) ||
81 (/mov ax,word ptr \[si\+2\]/) ||
82 (/bx,word ptr \[si\+32\]/) || (/ax,dx/)) next
83 if (/dx,/) sub(/dx/,"ax")
84 if ((/sub ax,word ptr \[si\+2\]/) ||
85 (/\[si\+16\]/) || (/ax,di/)) sub(/ax/,"bx")
86 if (/add word ptr \[si\+32\],ax/) $0=" add bx,word ptr [si+16]"
87 if (/al,/ || /,al/) sub(/al/,"cl")
88 if (/cmp byte ptr \[si\+34\],0/) $0=" or cl,cl"
89 if (/jne @@0$/) next
90 if (/jmp @3@58$/) $0=" je @3@58"
91 sub(/mov ax,-1/,"dec ax")
92 }
93 if (/endp/) { xlabel = ""; goto2=0 }
94 if (/isoopen\(s\+7\)/ && xlabel == "") goto2=1
95 if (/_vid_mode,ax/ && xlabel == "") goto2=1
96 if (/_base_himem\+2,/ && xlabel == "@") goto2=1
97 if (/puts\(cmdline\)/ && xlabel == "@@") goto2=1
98 if (goto2 == 1 && /jmp/) { # TAZBOOT.LST && LINLD.LST
99 print $NF xlabel "@:"
100 label=$NF
101 }
102 if (goto2 > 0 && label == $NF) {
103 $0=$0 xlabel
104 if (goto2++ == 1) xlabel=xlabel "@"
105 }
106 if (/cmdline=s\+=3/ || /magic \!= 0/ || /&root_dev =/) { isotazboot=10; j="" }
107 if (isotazboot == 10) { # TAZBOOT.LST && LINLD.LST
108 if (/je/ || /jne/) { j=$1; next }
109 if (/jmp/) {
110 if (j=="jne") sub(/jmp/,"je")
111 else if (j=="je") sub(/jmp/,"jne")
112 isotazboot=0
113 }
114 }
115 if (/static const unsigned long initrddesc = 18L/) isotazboot=9
116 if (isotazboot == 9) { # TAZBOOT.LST
117 if (/,0/) {
118 split($4,y,",")
119 print " mov bx,offset " y[1]
120 }
121 if (/DGROUP:.*\+6,46/) {
122 sub(/DGROUP:.*\+6,/,"[bx+6],")
123 isotazboot=0
124 }
125 if (/mov/) $0=" mov si,bx"
126 sub(/DGROUP:.*,/,"[bx],")
127 }
128 if (/isoopen\(s\+7\) != -1/) isotazboot=8
129 if (isotazboot == 8) { # TAZBOOT.LST
130 if (/ax,si/) next
131 sub(/ax,ax/,"si,si")
132 if (/magic/) isotazboot=0
133 }
134 if (/\+\+isknoppix/) isotazboot=7
135 if (isotazboot == 7) { # TAZBOOT.LST
136 if (/al,byte/) sub (/al,byte ptr DGROUP:/,"bx,offset ")
137 if (/inc/) sub (/al/,"word ptr [bx]")
138 if (/,al/) next
139 if (/isokernel/) isotazboot=0
140 }
141 if (/if \(c\) s\+\+;/) isotazboot=6
142 if (isotazboot == 6) { # TAZBOOT.LST
143 if (/cmp/) {
144 $0=" cmp al,0"
145 isotazboot=0
146 }
147 }
148 if (/static void next_chunk/) isotazboot=5
149 if (isotazboot == 5 || isotazboot == 500) { # TAZBOOT.LST
150 if (/cx,ax/) $0=" xchg ax,bx"
151 if (/ax,word ptr \[si\+28\]/ && isotazboot == 500) next
152 if (/bx,cx/) next
153 if (/push/ || /pop/ || /bp,sp/ || /si,/) next
154 sub(/\[si/,"[di")
155 if (/initrd_info/) isotazboot=500
156 if (/endp/) isotazboot=0
157 }
158 if (/0x7FF0/) isotazboot=4
159 if (isotazboot == 4) { # TAZBOOT.LST
160 if (/ax,word ptr/) {
161 print " mov ax,32752"
162 print " cwd"
163 sub(/mov/,"sub")
164 }
165 if (/bx,/ || /cx,/ || /dx,/) next
166 sub(/,0/,",dx")
167 sub(/,bx/,",dx")
168 sub(/,cx/,",ax")
169 if (/@addinitrd\$qv/) isotazboot=0
170 }
171 if (/c = x->filename/) isotazboot=3
172 if (isotazboot == 3) { # TAZBOOT.LST
173 if (/ax,/) $0=" xchg ax,bx"
174 if (/\]$/) next
175 if (/@strcpy\$qpxzct1/) isotazboot=0
176 }
177 if (/base_himem = memtop/) isotazboot=2
178 if (isotazboot == 2) { # TAZBOOT.LST
179 if (/@6@226/ || /mov ax,1/ || /@6@254/ || /xor ax,ax/) next
180 if (/word ptr \[bx\+2\],0/) {
181 print s; hold=0
182 print " mov bx,word ptr [bx+2]"
183 $0=" or bx,bx"
184 }
185 if (/\[bp-4\],ax/) sub(/ax/,"bx")
186 if (/ax,word ptr \[bx\+2\]/ || /bx,ax/) next
187 if (/@strcmp\$qpxzct1/) isotazboot=0
188 }
189 if (/static void addinitrd/) isotazboot=100
190 if (isotazboot == 100) { # TAZBOOT.LST
191 if (/cx,ax/) {
192 print " mov si,offset _isostate+8"
193 print " xchg ax,di"
194 print " movsw"
195 print " movsw"
196 print " movsw"
197 print " movsw"
198 $0=" xchg ax,di"
199 }
200 if (/mov/ && !/si/ && !/cl/) next
201 if (/void load_initrds/) isotazboot=101
202 }
203 if (isotazboot == 101 || isotazboot == 102) { # TAZBOOT.LST
204 sub(/\[si/,"[di"); sub(/si,/,"di,"); sub(/si$/,"di")
205 sub(/DGROUP:_imgs\+38$/,"[di+38-32]")
206 sub(/DGROUP:_imgs\+40$/,"[di+40-32]")
207 if (/isofd/) isotazboot=102
208 if (/push/ && isotazboot == 102) next
209 if (/pop/ && isotazboot == 102) next
210 if (/load_initrd/) isotazboot=101
211 if (/isokernel/) isotazboot=103
212 }
213 if (isotazboot > 102) { # TAZBOOT.LST
214 if (/push/ || /pop/) next
215 sub(/\[si/,"[bx")
216 sub(/si,/,"bx,")
217 if (/static void bootiso/) isotazboot=0
218 }
219 if (wascall) {
220 if (rcall != "") {
221 if (/,ax$/) print " mov " rcall ",ax"
222 else print " xchg ax," rcall
223 wascall=0
224 }
225 else if (/^ mov .i,ax$/) {
226 split($2,y,",")
227 rcall=y[1]
228 next
229 }
230 else wascall=0
231 }
232 if (/^ call /) { wascall=1; rcall="" }
233 if (hold == 0) {
234 s=$0
235 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
236 r=$2; kept=0
237 hold=1; split($2,regs,","); next
238 }
239 if (/^ inc e?.[ixhl]/ || /^ dec e?.[ixhl]/) {
240 hold=2; r=$2; next
241 }
242 if (/^ mov [abcds][ix],/ && ! /,.s/) {
243 hold=3; split($2,regs,","); next
244 }
245 if (/^ movzx eax,ax$/) { hold=4; next }
246 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
247 split($0,regs,",")
248 if (isnum(regs[2]) && regs[2] != 0 &&
249 (regs[2] % 256) == 0) {
250 hold=5; next
251 }
252 }
253 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
254 if (/^ mov cl,4$/) { hold=7; next }
255 if (/^ cmp word ptr DGROUP:.*,0$/) {
256 hold=8; split($2,regs,","); next
257 }
258 if (/^ cbw/) { hold=11; kept=0; next }
259 if (/^ add [abcds][ix],2$/) {
260 split($2,regs,","); hold=12; next
261 }
262 if (/^ sub [abcds][ix],2$/) {
263 split($2,regs,","); hold=13; next
264 }
265 if (/^ push dx$/) {
266 hold=14; next;
267 }
268 }
269 else if (hold == 1) {
270 if (/^ ;/) { line[kept++]=$0; next }
271 hold=0; split($2,args,","); op=""
272 if ($1 == "add") op="+"
273 if ($1 == "sub") op="-"
274 if ($1 == "inc") { op="+"; args[2]="1"; }
275 if ($1 == "dec") { op="-"; args[2]="1"; }
276 if (op != "" && regs[1] == args[1]) {
277 if (isnum(args[2])) {
278 for (i = kept++; i > 0; i--) line[i] = line[i-1]
279 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
280 hold=10; next
281 }
282 line[kept++]=$0
283 hold=1
284 next
285 }
286 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
287 print " xchg " r
288 }
289 else print s
290 for (i = 0; i < kept; i++) print line[i]; kept=0
291 }
292 else if (hold == 2) {
293 split($0,args,",")
294 if (/^ mov / && r == args[2]) { print s; s=$0; next }
295 split($2,args,",")
296 hold=0; print s
297 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
298 }
299 else if (hold == 3) {
300 hold=0
301 if (/^ call / && regs[2] == "ax") s=" xchg ax," regs[1]
302 if (/^ add [abcds][ix],/) {
303 split($2,regs2,",")
304 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
305 t=$0; sub(/mov/,$1,s); sub(/add/,"mov",t)
306 print t; print s; next
307 }
308 }
309 print s
310 }
311 else if (hold == 4) {
312 hold=0
313 if (/^ push eax$/) {
314 print " push 0"; print " push ax"; next
315 } else { print s }
316 }
317 else if (hold == 5) {
318 hold=0
319 if ($1 == "jae" || $1 == "jb") {
320 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
321 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
322 s = s "/256"
323 }
324 print s
325 }
326 else if (hold == 6) {
327 if (($1 == "and" || $1 == "add") && $2 ~ /^ax,/) {
328 line[kept++]=$0
329 next
330 }
331 p=$0
332 if (/^ movzx eax,ax$/) {
333 s=" mov eax,cs"; p=""
334 }
335 print s
336 for (i = 0; i < kept; i++) print line[i]; kept=0
337 if (p != "") print p
338 hold=0; next
339 }
340 else if (hold == 7) {
341 hold=0
342 if (/^ call near ptr N_LXURSH@$/) {
343 print " extrn N_LXURSH@4:near"
344 print " call near ptr N_LXURSH@4"
345 next
346 }
347 if (/^ call near ptr N_LXLSH@$/) {
348 print " extrn N_LXLSH@4:near"
349 print " call near ptr N_LXLSH@4"
350 next
351 }
352 print s
353 }
354 else if (hold == 8) {
355 if ($1 == "je" || $1 == "jne") { p=$0; hold=9; next }
356 hold=0
357 print s
358 }
359 else if (hold == 9) {
360 hold=0; split($2,args,",")
361 if (/^ mov ax,/ && args[2] == regs[1]) {
362 print; print " or ax,ax"; print p; next
363 }
364 print s; print p;
365 }
366 else if (hold == 10) {
367 split($2,args,","); op=""
368 if ($1 == "add") op="+"
369 if ($1 == "sub") op="-"
370 if ($1 == "inc") { op="+"; args[2]="1"; }
371 if ($1 == "dec") { op="-"; args[2]="1"; }
372 if (op != "" && isnum(args[2])) {
373 split(line[0],reg,",")
374 if (substr(reg[1],length(reg[1])-1,2) == args[1]) {
375 line[0] = substr(line[0],1,length(line[0])-1) op args[2] "]"
376 next
377 }
378 }
379 hold=0
380 if (/^ mov [sd]i,ax$/) {
381 split($2,args,",")
382 for (i = 0; i < kept; i++) {
383 sub(/ax/,args[1],line[i]); print line[i]
384 }
385 next
386 }
387 for (i = 0; i < kept; i++) print line[i]
388 }
389 else if (hold == 11) {
390 if (/^ inc ax$/ || /^ dec ax$/) {
391 line[kept++]=$0; next
392 }
393 split($2,args,",")
394 if (/^ mov cl,/) {
395 split($2,args,",")
396 if (args[2] >= 8) {
397 line[kept++]=$0; next
398 }
399 }
400 if (!/^ shl ax,/ || (args[2] != "cl" && args[2] < 8)) {
401 print " cbw "
402 }
403 for (i = 0; i < kept; i++) print line[i]
404 hold=kept=0
405 }
406 else if (hold == 12) {
407 hold=0
408 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
409 print " inc " regs[1]
410 print " inc " regs[1]
411 }
412 else print " add " regs[1] ",2"
413 }
414 else if (hold == 13) {
415 hold=0
416 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
417 print " dec " regs[1]
418 print " dec " regs[1]
419 }
420 else print " sub " regs[1] ",2"
421 }
422 else if (hold == 14) {
423 if (/^ push ax$/) { hold++; next; }
424 print " push dx";
425 hold=0;
426 }
427 else if (hold == 15) {
428 if (/^ pop eax$/) { hold++; next; }
429 print " push dx";
430 print " push ax";
431 hold=0;
432 }
433 else if (hold == 16) {
434 hold=0;
435 if (/^ shr eax,16$/) { print " xchg ax,dx"; next; }
436 print " push dx";
437 print " push ax";
438 print " pop eax";
439 }
440 else if (hold == 17) {
441 hold=0;
442 if (/^ cmp ax,-1$/) { print " inc ax"; next; }
443 }
444 if (/^ call near ptr @fileexist\$/ || # return boolean :
445 /^ call near ptr @isoreaddir\$/ || # 0=true, -1=false
446 /^ call near ptr @isoreset\$/ ||
447 /^ call near ptr @isoopen\$/ ||
448 /^ call near ptr @isoreadsector\$/ ||
449 /^ call near ptr @strhead\$/ ||
450 /^ call near ptr @argstr\$/ ||
451 /^ call near ptr @argnum\$/) { print; hold=17; next; }
452 s=$0
453 # These optimisation may break ZF or CF
454 if (/^ sub sp,2$/) { print " push ax"; next }
455 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
456 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
457 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
458 sub(/mov/,"and",s); print s; next # slower
459 }
460 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
461 sub(/mov/,"or",s); print s; next # slower
462 }
463 if (/^ or .*,0$/ || /^ and .*,-1$/) next
464 if (/^ or [abcd]x,/) {
465 split($2,args,",")
466 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
467 print " or " substr(args[1],1,1) "l," args[2]; next
468 }
469 }
470 if (/^ and [abcd]x,/) {
471 split($2,args,",")
472 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
473 print " and " substr(args[1],1,1) "l," args[2]; next
474 }
475 }
476 if (/^ or e[abcd]x,/) {
477 split($2,args,",")
478 if (args[2] == "large") { args[2] = $3 }
479 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
480 print " or " substr(args[1],2,1) "l," args[2]; next
481 }
482 }
483 if (/^ and e[abcd]x,/) {
484 split($2,args,",")
485 if (args[2] == "large") { args[2] = $3 }
486 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
487 print " and " substr(args[1],2,1) "l," args[2]; next
488 }
489 }
490 if (/^ or e[abcds][ix],/) {
491 split($2,args,",")
492 if (args[2] == "large") { args[2] = $3 }
493 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
494 print " or " substr(args[1],2) "," args[2]; next
495 }
496 }
497 if (/^ and e[abcds][ix],/) {
498 split($2,args,",")
499 if (args[2] == "large") { args[2] = $3 }
500 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
501 print " and " substr(args[1],2) "," args[2]; next
502 }
503 }
504 if (/^ add word ptr/ || /^ sub word ptr/ ||
505 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
506 split($0,args,",")
507 if (isnum(args[2]) && (args[2] % 256 == 0)) {
508 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
509 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
510 print s "/256"; next
511 }
512 }
513 if (/^ add dword ptr/ || /^ sub dword ptr/) {
514 split($0,args,",")
515 if (args[2] == "large") { args[2] = $3 }
516 if (isnum(args[2])) {
517 if (args[2] % 16777216 == 0) {
518 sub(/dword/,"byte",s)
519 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
520 print s "/16777216"; next
521 }
522 if (args[2] % 65536 == 0) {
523 sub(/dword/,"word",s)
524 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
525 print s "/65536"; next
526 }
527 }
528 }
529 if (/^ mov e.x,/) {
530 split($2,args,",")
531 r=args[1]
532 if (args[2] == "large") { args[2] = $3 }
533 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
534 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
535 print " xor " r "," r
536 if (args[2] == 0) next
537 x=" mov " substr(r,2,1)
538 if (args[2] % 256 == 0) {
539 print x "h," args[2] "/256"
540 }
541 else { print x "l," args[2] }
542 next
543 }
544 }
545 }
546 if (afterjmp) print ";" $0
547 else print
548 if (/^ jmp / || /^ call near ptr _boot_kernel/ ||
549 /^ call near ptr @die$qpxzc/) afterjmp=1
550 }