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

Up glza (0.11.4)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat May 21 21:38:29 2022 +0000 (2022-05-21)
parents c11594098e34
children
line source
1 BEGIN { hold=0; is386=0; isload=0; isiso=0; istazboot=0; wascall=0; ishimem=0; label="none"; xlabel=""; file="" }
2 function isnum(n) { return match(n,/^[0-9+-]/) }
3 {
4 sub(/segment word public/,"segment byte public")
6 if (/^ ; $/) next
7 if (/^@.*:$/ || / endp$/) afterjmp=0
8 if (/^ \.386p$/) is386=1
9 if (file == "" && /debug S/) { file=$3; gsub(/\"/,"",file) }
10 if (/debug S/) {
11 print " %PAGESIZE 255"
12 print " include common.inc"
13 }
14 if (file == "linld.cpp") {
15 if (/\[si/ || /si,/ || /,si/) sub(/si/,"di")
16 else if (/\[di/ || /di,/ || /,di/) sub(/di/,"si")
17 if (/add di,2/) $0=" scasw ; " $0
18 if (/bx,di/ || /\[bp-2\]/) next
19 sub(/\[bx\],0/,"[di],0")
20 if (/ptr @exit\$qv/) {
21 if (gotexit) next
22 print "call_exit:"
23 gotexit=1
24 }
25 if (islinld==1) {
26 if (/,word.*/) islinld=0
27 print "; " $0
28 next
29 }
30 if (/^_main proc/) islinld=1
31 if (/image\|initrd/) islinld=3
32 if (islinld==3 && /bx,word ptr/) { print "; " $0; next }
33 if (/fileexist\$qpxzc/) islinld=4
34 if (islinld==4) {
35 if (/DGROUP:_heap_top/) next
36 if (/ax,-1/) {
37 print " inc ax"
38 print " mov ax,word ptr [di]"
39 next
40 }
41 if (/ax,word ptr \[/) next
42 }
43 if (/buf_cmdline\+1/) {
44 islinld=5
45 print " mov bx,offset DGROUP:_buf_cmdline+1"
46 sub(/offset DGROUP:_buf_cmdline\+1/,"bx")
47 }
48 if (islinld==5) {
49 if (/bx,offset DGROUP:_buf_cmdline/) $0=" dec bx"
50 if (/ax,word ptr/) next
51 if (/call/) islinld=0
52 }
53 if (/bx,word ptr DGROUP:_cmdstr\+6/) next
54 if (/_cmdstr\+6,0/) {
55 print " mov bx,word ptr [si+6]"
56 $0=" or bx,bx"
57 }
58 if (/heap_top \+1;/) islinld=6
59 if (islinld==6) {
60 sub(/ax/,"bx")
61 if (/strcatb/) {
62 print " dec bx"
63 islinld=4
64 }
65 }
66 if (/minram>>16/) islinld=7
67 if (islinld==7) {
68 if (/endif/) islinld=0
69 if (/mov/) sub(/ax/,"cx");
70 if (/;/ || /_memtop/) print
71 if (/cmdnum/) {
72 print
73 print " ja call_exit"
74 }
75 next
76 }
77 if (/mincpu > cputype/) islinld=8
78 if (islinld==8) {
79 if (/endif/) islinld=0
80 if (/jbe/) $0=" ja call_exit"
81 }
82 } # file == "linld.cpp"
83 if (file == "himem.cpp") {
84 if (/sp,bp/ || /pop bp/ || /enter/ || /leave/) next
85 if (/void load_image/) ishimem=1
86 if (ishimem == 1 && is386 == 0) {
87 if (/si\+8\]$/ || /si\+4\]$/ || /si\+16\]$/) next
88 if (/si\+6\]$/ || /si\+2\]$/ || /si\+14\]$/) sub(/mov dx,/,"les dx,d")
89 if (/si\+12\],ax/ || /si\+16\],ax/ || /di\+2\],ax/ || /DGROUP:_himem_buf\+2,ax/) sub(/,ax/,",es")
90 if (/dx,dword ptr \[si\+14\]/ || /DGROUP:_himem_buf,dx/) sub(/dx/,"ax")
91 }
92 if (ishimem == 1) {
93 if (/do \{/) ishimem=2
94 if (/bx,si/ || /di,ax/ || /push bp/ || /bp,sp/ || /push di/ || /push si/) next
95 if (/sp,2/ || /bp\+4/) next
96 }
97 if (ishimem == 2) {
98 if (/movzx/) print " cwde"
99 if (/bp-2/ || /di,ax/ || /bx,di/ || /bx,si/) next
100 if (/storepage.bufv/) {
101 print " inc ax"
102 print " push ax"
103 }
104 if (/buf \+= size;/) {
105 print " pop ax"
106 }
107 if (/i\+12/) ishimem=20
108 }
109 if (ishimem == 20) {
110 if (/loadfail/) next
111 if (/je/) {
112 print " extrn jmploadfailure"
113 $0=" jne short jmploadfailure"
114 }
115 if (/endp/) ishimem=0
116 }
117 if (/@memcpy_image\$qp11image_himem/) next
118 if (/far last_ditch/) ishimem=3
119 if (ishimem == 3) {
120 sub(/DGROUP:_imgs\+4,/,"[si+4],")
121 if (/bx,di/ || /di,ax/) next
122 }
123 } # file == "himem.cpp"
124 if (file == "load.cpp") {
125 if (isload != 3 && isload != 6) { # LOAD.LST
126 if (/,si/ || /si,/ || /\[si/) sub(/si/,"di")
127 else if (/,di/ || /di,/ || /\[di/) sub(/di/,"si")
128 }
129 if (/moverm\(/) isload=16
130 if (isload != 16 && /bx,si/) next
131 if (/@moverm/) isload=0
132 if (/readrm\(m, 0x200/) isload=15
133 if (isload == 15) { # LOAD.LST
134 if (/bx,si/) next
135 if (/call/) isload=0
136 }
137 if (/load_image\(/) {
138 if (isload == 3) isload=13
139 else isload=14
140 }
141 if (isload == 14) { # LOAD.LST
142 if (/call/) $0=" jmp short load_imagez"
143 if (/ret/) isload=0
144 if (/pop/ || /ret/ || /push/) next
145 }
146 if (isload == 13) { # LOAD.LST
147 if (/pop/) isload=3
148 if (/push/ || /call/ || /pop/) next
149 }
150 if (/i\+21\],513$/) isload=11
151 if (isload == 12) { # LOAD.LST
152 if (/cmp/) next
153 if (/jb/) isload=0
154 sub(/jb/,"jcxz")
155 }
156 if (isload == 11) { # LOAD.LST
157 if (/cmp/) {
158 print " mov cx,513"
159 sub(/cmp /,"sub cx,")
160 sub(/,513/,"")
161 }
162 if (/jb/) isload=12
163 sub(/jb/,"ja")
164 }
165 sub(/_imgs\+65534/,"_imgs-2")
166 if (/setup_sects == 0/) isload=9
167 if (isload == 9) { # LOAD.LST
168 sub(/,0/,",al ; worst case 2k boundary (iso)")
169 if (/jne/) isload=0
170 }
171 if (/cmd_line_ptr =/ && is386 == 0) isload=7
172 if (isload == 7) { # LOAD.LST
173 if (/add/ || /xor/ || /extrn/ || /N_LXLSH@/ || /cl,4/ || /,ax/) next
174 if (/enable A20 if needed/) { print nextinst; isload=0 }
175 if (/i-463/) $0=" mov bx,-463"
176 if (/i-465/) {
177 sub(/465/,"2"); sub(/\[/,"[bx+")
178 nextinst=$0; sub(/-2\],-23745/,"],8000h",nextinst)
179 }
180 if (/,dx/) {
181 print " mov cl,12"
182 print " shr ax,cl"
183 print " mov bx,55"
184 sub(/dx/,"ax")
185 }
186 }
187 if (/pm_low == 0/) {
188 print " push di"
189 print " push si"
190 isload=6
191 }
192 if (isload == 6) { # LOAD.LST
193 if (/si\+2/) {
194 print " cmpsw"
195 next
196 }
197 if (/les/) sub(/bx,/,"di,")
198 if (/bx\+4/ || /es:/ || /call/ || /pop/ || /ret/) next
199 if (/si\+6/) {
200 print " movsw"
201 print " movsw"
202 print " movsw"
203 print " movsw"
204 print " pop si"
205 print "load_imagez:"
206 next
207 }
208 }
209 if (isload == 5) { # LOAD.LST
210 sub(/ax,/,"bx,")
211 if (/@puts\$qpxzc/) isload=0
212 if (/mov bx,ax/) next
213 sub(/,word ptr \[di\+29\]/,",cx")
214 }
215 if (/_cmdnum\+14/ && is386 == 0) isload=4
216 if (isload == 4) { # LOAD.LST
217 if (/_cmdnum\+14/) next
218 if (/_cmdnum\+12$/) {
219 $0=" les dx,dword ptr [bx+12]"
220 }
221 sub(/,ax/,",es")
222 if (/add ax,word ptr/) $0=" add ax,cx"
223 if (/i\+29\],0/) {
224 sub(/,0$/,"")
225 sub(/cmp /,"mov cx,")
226 }
227 sub(/je/,"jcxz")
228 if (/@strcpy/) isload=0
229 if (/\+0x200/) isload=5
230 }
231 if (/void load_initrd\(\)/) { isload=3; isload2=0 }
232 if (isload == 3) { # LOAD.LST
233 if (/bx,offset DGROUP:_imgs\+28/ || /push si/) next
234 if (/si,offset DGROUP:_imgs\+28/) print " push si"
235 if (/cmdstr\+4,0/) {
236 isload2++
237 print " mov ax,word ptr DGROUP:_cmdstr+4"
238 $0=" or ax,ax"
239 }
240 if (isload2 && /DGROUP:_cmdstr\+4/) $0=";" $0
241 if (/je short @2@.*/) sub(/@2@.*/,"load_initrd_ret")
242 if (/mov ax,word ptr \[si\]/) $0=" lodsw"
243 if( /jmp/) {
244 print "load_initrd_ret:"
245 print " ret"
246 next
247 }
248 if (/@loadfailure/) {
249 print " global jmploadfailure:near"
250 print "jmploadfailure:"
251 }
252 sub(/\[di/,"[bx")
253 sub(/di,/,"bx,")
254 }
255 if (/mode = vid_mode/) { isload=2; print " mov bx,offset _cmdnum" }
256 if (isload == 2) { # LOAD.LST
257 if (/DGROUP:_cmdnum/) { sub(/DGROUP:_cmdnum/,"[bx"); $0=$0 "]"}
258 sub(/,0/,""); sub(/cmp /,"mov cx,")
259 sub(/je/,"jcxz")
260 if (/ax,word/) next
261 sub(/,ax/,",cx")
262 if (/starting linux 1\.3\.73/) isload=0
263 }
264 if (/_rm_size=0x200/ || /heap_top = _rm_buf/) isload=1
265 if (isload == 1) { # LOAD.LST
266 if (/ptr .die\$qpxzc/) $0="@die@:\n" $0
267 if (/mov al,byte ptr/ && is386) {
268 sub(/mov al/,"movzx eax")
269 }
270 if (is386 == 0) {
271 if (/m->size -= _rm_size/) print " cwd ; do not trust rewind result (iso case)"
272 sub(/,0$/,",dx")
273 }
274 if (/ax,word ptr/) next
275 if (/^ call/) isload=0
276 }
277 } # file == "load.cpp"
278 if (file == "iso9660.cpp") {
279 if (swapsidi == 1) {
280 if (/\[si/) sub(/\[si/,"[di")
281 else (/\[di/) sub(/\[di/,"[si")
282 if (/\+si/) sub(/\+si/,"+di")
283 else (/\+di/) sub(/\+di/,"+si")
284 if (/,si/) sub(/,si/,",di")
285 else if (/,di/) sub(/,di/,",si")
286 if (/si,/) sub(/si,/,"di,")
287 else if (/di,/) sub(/di,/,"si,")
288 }
289 if (/di,offset DGROUP:_buf2k/) { si="si"; di="di" }
290 if (/si,offset DGROUP:_buf2k/) { si="di"; di="si" }
291 if (/leave/ || /enter/ || /bp/ || /sub sp,/) next
292 if (/ptr \[.i\+8\],dx/) next
293 if (/ptr \[.i\+6\],ax/) next
294 if (/ptr \[.i\+6\],eax/) next
295 if (/x,word ptr \[.i\+32\]/) next
296 if (/add word ptr \[.i\],ax/) sub(/ax/,"cx")
297 if (/ax,word ptr \[si\+18\]/) sub(/mov ax,/,"les ax,d")
298 if (/ax,word ptr \[si\+20\]/) next
299 if (/word ptr \[si\+25\],ax/) sub(/ax/,"es")
300 sub(/di,word ptr DGROUP:_isostate\+2/,"di,word ptr [si+2]")
301 if (/-257/) isiso=22
302 if (isiso == 22) {
303 sub(/-257/,"-257 ; clear C")
304 if (/je/) {
305 print " cbw"
306 print " xchg ax,bx"
307 print " xchg ax,dx ; .. or ."
308 $0=" je returnC"
309 }
310 }
311 if (/p = buf2k \+ 32 \+ x->curpos/) isiso=21
312 if (isiso == 21) { # ISO9660.LST
313 if (/ax,/) next
314 if (/.i,ax/) sub(/mov/,"xchg")
315 if (/# else/) isiso=0
316 }
317 if (/cx,.i/) {
318 if (isiso == 0) {
319 print " ifndef CLEAN_ISO9660"
320 print " inc " di
321 print " mov cx," di
322 }
323 isiso=20
324 }
325 if (isiso == 20) { # ISO9660.LST
326 if (/bx,dx/) {
327 isiso=12
328 next
329 }
330 if (/add .i,ax/ || /cbw/) next
331 if (/cmp/) sub(/\[/,"[bx+")
332 if (/inc cx/) next
333 if (/sub/) sub(/.i/,"bx")
334 sub(/inc .i/,"inc bx")
335 if (/al,byte ptr/) {
336 next
337 }
338 sub(/i-1\],12603/,"i],12603")
339 if (/bx\+.i\],46/) {
340 print " inc " di
341 print " mov cx," di
342 }
343 if (/i],0/) {
344 print " ifndef CLEAN_ISO9660"
345 print " inc " di
346 print " mov cx," di
347 print " endif"
348 print "seteos:"
349 sub(/,0/,",bh ; clear C")
350 sub(/mov byte ptr \[/,"and byte ptr [bx+")
351 }
352 if (/cx,.i/) next
353 if (/\}/) {
354 print " xchg ax,cx"
355 }
356 if (/cx,5/) $0=" lea dx,[" di "+5]"
357 if (/filename = s;/) {
358 isiso=0
359 }
360 }
361 if (isiso == 19) { # ISO9660.LST
362 if (/short @2@310/) $0=" jc restoreC"
363 if (/si\+32/ || /ax,dx/ || /ax,di/ || /cmp ax,-1/ || /sub ax,/) next
364 if (/ax,word ptr \[si\+28\]/) next
365 if (/bx,offset/) sub(/bx/,"ax")
366 sub(/dx,/,"bx,")
367 if (/short @2@282/) sub(/je/,"jnc")
368 if (/\[si\+30\],0/) $0=" ;or cl,cl"
369 if (/short @2@478/) sub(/je/,"jcxz")
370 if (/@strcmp\$qpxzct1/) {
371 print
372 print "restoreC:"
373 $0=" mov byte ptr [di],dl ; c"
374 isiso=1
375 }
376 }
377 if (isiso == 18) { # ISO9660.LST
378 if (/endif/) isiso=1
379 }
380 if (isiso == 17) { # ISO9660.LST
381 if (/si\+18/) {
382 print "next:"
383 print " mov word ptr [si+20],bx"
384 print
385 isiso=0
386 }
387 if (/_buf2k\+167/) {
388 print
389 $0=""
390 }
391 else sub(/ax/,"bx")
392 if (/,bx/) next
393 if (/isoreadrootsector/) {
394 print
395 print " mov ax,word ptr [_buf2k+1]"
396 print " xor ax,17475 ; clear C, CD"
397 print "jne_returnNotC:"
398 print " cmc"
399 $0=" jne returnC"
400 }
401 }
402 if (/if \(c\)/) isiso=16
403 if (isiso == 16) { # ISO9660.LST
404 if (/cmp/) {
405 print " mov bx,word ptr [si+7]"
406 print " mov ax,word ptr [si+11]"
407 print " dec dx"
408 print " jns next"
409 }
410 if (/isolseek/) isiso=1
411 else if (! /;/) next
412 }
413 if (/found:/) isiso=15
414 if (isiso != 15 && /si,offset DGROUP:_isostate/) $0=";" $0
415 if (isiso == 15) { # ISO9660.LST
416 if (/xor/) {
417 print "returnC:"
418 print " sbb dx,dx"
419 print "return:"
420 next
421 }
422 if (/i\+28\],cx/) {
423 print " ifndef BASIC_ISO9660"
424 print " xchg ax,dx"
425 print " endif"
426 next
427 }
428 if (/@1@422:/) next
429 if (/\[di\],47/) isiso=17
430 }
431 if (isiso == 14) { # ISO9660.LST
432 if (/ax,/ || /jge/) next
433 sub(/mov/,"sub")
434 sub(/,ax/,",8")
435 if (/jmp/) {
436 print " ;JUMPS"
437 print " jb returnCXZC"
438 $0=" ;NOJUMPS"
439 isiso=0
440 }
441 }
442 if (/p = buf2k \+ 34/) isiso=13
443 if (isiso == 13) { # ISO9660.LST
444 #if (/cbw/) $0=" ;cbw"
445 if (/i,.i/) $0=" xchg ax,bx"
446 if (/i,ax/) $0=" xchg ax," di
447 if (/i,70/ || /ptr \[.i\+32\]/) next
448 if (/register len/) {
449 isiso=12
450 }
451 }
452 if (isiso == 12) { # ISO9660.LST
453 if (/.i\+2/) sub(/al/,"bl")
454 if (/cbw/) next
455 if (/dx,ax/) next
456 if (/bx,dx/) next
457 sub(/i,dx/,"i,bx")
458 sub(/cx,/,"dx,")
459 if (/bx\+.i\],0/) {
460 sub(/,0/,",bh")
461 }
462 if (/jmp/) {
463 print
464 $0=" endif"
465 }
466 if (/jne/) {
467 print " ifdef BASIC_ISO9660"
468 print " lea cx,[" di "+5]"
469 print " je seteos"
470 print " else"
471 }
472 if (/while/) isiso=120
473 }
474 if (isiso == 120) { # ISO9660.LST
475 sub(/ax/,"cx")
476 if (/cmp cx,/) $0=" cmp " di ",cx"
477 sub(/jae/,"jb")
478 if (/endif/) isiso=0
479 }
480 if (/curpos >= SECT/) isiso=10
481 if (isiso == 10) { # ISO9660.LST
482 if (/,2048/) {
483 print " xor cx,cx"
484 sub(/cmp /,"mov bx,")
485 sub(/i.*/,"i]")
486 print
487 $0=" cmp bh,2048/256"
488 }
489 if (/DGROUP:_buf2k\[bx\],0/) $0=" cmp word ptr [bx+" di "],cx"
490 sub(/,0/,",cx")
491 if (/\[.i\],cx/) isiso=14
492 else if (/mov/) next
493 }
494 if (/<< SECTORBITS/) isiso=9
495 if (isiso == 9) { # ISO9660.LST
496 if (/dx,/) next
497 sub(/mov ax,/,"les ax,d")
498 if (/^ call/) {
499 print " extrn N_LXLSH@ES:near"
500 sub(/N_LXLSH@/,"N_LXLSH@ES")
501 isiso=0
502 }
503 }
504 if (/filesize =/) isiso=8
505 if (isiso == 8) { # ISO9660.LST
506 if (/ax,/ || /cbw/) next
507 sub(/mov dx,/,"les dx,d")
508 sub(/\],ax/,"],es")
509 sub(/cx,/,"dx,")
510 if (/DGROUP:s@\+/) {
511 print
512 next
513 }
514 if (/i\+33]/) {
515 print " ifdef ROCKRIDGE"
516 print " ifdef BASIC_ISO9660"
517 print " add " di ",32"
518 print " mov ax,[" di "]"
519 print " else"
520 print " mov ax,[" di "+32]"
521 print " endif"
522 print " mov bl,ah"
523 print " mov bh,0"
524 print " else"
525 print " mov al,[" di "+33]"
526 print " cbw"
527 $0=" endif"
528 }
529 if (/sub/) {
530 print " ifdef ROCKRIDGE"
531 print " sub dx,bx"
532 print " else"
533 print
534 $0=" endif"
535 isiso=0
536 }
537 }
538 if (/entrysize =/) isiso=5
539 if (isiso == 5) { # ISO9660.LST
540 if (/ax,ax/) next
541 if (/word ptr \[.i\+3.\],ax/) next
542 sub(/ax/,"cx")
543 sub(/je/,"jcxz")
544 if (/jcxz/) {
545 hold=0
546 print s
547 print " stc"
548 print "returnCXZC:"
549 sub(/@1@1../,"returnC")
550 print
551 if (is386) $0=" mov dword ptr [" si "+6],eax"
552 else {
553 print " mov word ptr [" si "+8],dx"
554 $0=" mov word ptr [" si "+6],ax"
555 }
556 }
557 if (/return/) isiso=0
558 }
559 if (/do s\+\+; while/) isiso=3
560 if (isiso == 3) { # ISO9660.LST
561 if (/do \{/) print "while1:"
562 sub(/cmp byte ptr \[.i\]/,"sub al")
563 if (/inc /) { r=$2; print; next }
564 if (/al,0/) print " mov al,[" r "]"
565 if (/word ptr \[si\],-1/) $0=" not word ptr [si] ; zero'd in BSS"
566 sub(/al,32/,"ax,32")
567 if (/byte ptr \[si\+30\],al/) $0=" xchg ax,cx"
568 if (/al,byte ptr \[/) next
569 if (/byte ptr \[.*\],0/) next
570 if (/byte ptr \[si\+31\],al/) next
571 }
572 if (/ptr .isoreaddir/) {
573 print " ifdef ISOHOOK"
574 print " push cx"
575 print
576 print " pop cx"
577 print " else"
578 print
579 $0=" endif"
580 isiso=1
581 }
582 if (isiso == 1) { # ISO9660.LST
583 if (/xor/) {
584 $0=" xor cl,32"
585 isiso=18
586 }
587 if (/n = name;/) {
588 isiso=19
589 print " xchg dl,byte ptr [di] ; c"
590 }
591 sub(/jne/,"jnc")
592 if (/short @2@450/) $0=" jc returnC"
593 if (/je/) $0=" jc while1"
594 if (/short @2@562/) sub(/@2@562/,"jne_returnNotC")
595 if (/jmp/) $0=" jmp jne_returnNotC"
596 if (/ax,word ptr \[si\+4\]/) $0=" xchg ax,bx ; " $0
597 if (/\[di\],al/ || /al,byte ptr/) next
598 if (/@2@338/) next
599 if (/ax,-1/) next
600 if (/inc di/ || /@@0/) next
601 if (/@2@142$/) print " inc di"
602 }
603 if (/i\+34\]/) next
604 if (/di,offset DGROUP:_isostate/) {
605 sub(/mov/,";mov")
606 sub(/di,/,"si,")
607 swapsidi=1
608 }
609 if (/^ ret/) swapsidi=0
610 } # file == "iso9660.cpp"
611 if (wascall) {
612 if (rcall != "") {
613 if (/,ax$/) print " mov " rcall ",ax"
614 else print " xchg ax," rcall
615 wascall=0
616 }
617 else if (/^ mov .i,ax$/) {
618 split($2,y,",")
619 rcall=y[1]
620 next
621 }
622 else wascall=0
623 }
624 if (/^ call /) { wascall=1; rcall="" }
625 if (hold == 0) {
626 s=$0
627 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
628 r=$2; kept=0
629 hold=1; split($2,regs,","); next
630 }
631 if (/^ inc e?.[ixhl]/ || /^ dec e?.[ixhl]/) {
632 hold=2; r=$2; next
633 }
634 if (/^ mov [abcds][ix],/ && ! /,.s/) {
635 hold=3; split($2,regs,","); next
636 }
637 if (/^ movzx eax,ax$/) { hold=4; next }
638 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
639 split($0,regs,",")
640 if (isnum(regs[2]) && regs[2] != 0 &&
641 (regs[2] % 256) == 0) {
642 hold=5; next
643 }
644 }
645 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
646 if (/^ mov cl,4$/) { hold=7; next }
647 if (/^ cmp word ptr DGROUP:.*,0$/) {
648 hold=8; split($2,regs,","); next
649 }
650 if (/^ cbw/) { hold=11; kept=0; next }
651 if (/^ add [abcds][ix],2$/) {
652 split($2,regs,","); hold=12; next
653 }
654 if (/^ sub [abcds][ix],2$/) {
655 split($2,regs,","); hold=13; next
656 }
657 if (/^ push dx$/) {
658 hold=14; next;
659 }
660 }
661 else if (hold == 1) {
662 if (/^ ;/) { line[kept++]=$0; next }
663 hold=0; split($2,args,","); op=""
664 if ($1 == "add") op="+"
665 if ($1 == "sub") op="-"
666 if ($1 == "inc") { op="+"; args[2]="1"; }
667 if ($1 == "dec") { op="-"; args[2]="1"; }
668 if (op != "" && regs[1] == args[1]) {
669 if (isnum(args[2])) {
670 for (i = kept++; i > 0; i--) line[i] = line[i-1]
671 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
672 sub(/\+-/,"-",line[0])
673 hold=10; next
674 }
675 line[kept++]=$0
676 hold=1
677 next
678 }
679 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
680 print " xchg " r
681 }
682 else print s
683 for (i = 0; i < kept; i++) print line[i]; kept=0
684 }
685 else if (hold == 2) {
686 split($0,args,",")
687 if (/^ mov / && r == args[2]) { print s; s=$0; next }
688 split($2,args,",")
689 hold=0; print s
690 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
691 }
692 else if (hold == 3) {
693 hold=0
694 if (/^ call / && regs[2] == "ax") s=" xchg ax," regs[1]
695 if (/^ add [abcds][ix],/) {
696 split($2,regs2,",")
697 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
698 t=$0; sub(/mov/,$1,s); sub(/add/,"mov",t)
699 print t; print s; next
700 }
701 }
702 print s
703 }
704 else if (hold == 4) {
705 hold=0
706 if (/^ push eax$/) {
707 print " push 0"; print " push ax"; next
708 } else { print s }
709 }
710 else if (hold == 5) {
711 hold=0
712 if ($1 == "jae" || $1 == "jb") {
713 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
714 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
715 s = s "/256"
716 }
717 print s
718 }
719 else if (hold == 6) {
720 if (($1 == "and" || $1 == "add") && $2 ~ /^ax,/) {
721 line[kept++]=$0
722 next
723 }
724 p=$0
725 if (/^ movzx eax,ax$/) {
726 s=" mov eax,cs"; p=""
727 }
728 print s
729 for (i = 0; i < kept; i++) print line[i]; kept=0
730 if (p != "") print p
731 hold=0; next
732 }
733 else if (hold == 7) {
734 hold=0
735 if (/^ call near ptr N_LXURSH@$/) {
736 print " extrn N_LXURSH@4:near"
737 print " call near ptr N_LXURSH@4"
738 next
739 }
740 if (/^ call near ptr N_LXLSH@$/) {
741 print " extrn N_LXLSH@4:near"
742 print " call near ptr N_LXLSH@4"
743 next
744 }
745 print s
746 }
747 else if (hold == 8) {
748 if ($1 == "je" || $1 == "jne") { p=$0; hold=9; next }
749 hold=0
750 print s
751 }
752 else if (hold == 9) {
753 hold=0; split($2,args,",")
754 if (/^ mov ax,/ && args[2] == regs[1]) {
755 print; print " or ax,ax"; print p; next
756 }
757 print s; print p;
758 }
759 else if (hold == 10) {
760 split($2,args,","); op=""
761 if ($1 == "add") op="+"
762 if ($1 == "sub") op="-"
763 if ($1 == "inc") { op="+"; args[2]="1"; }
764 if ($1 == "dec") { op="-"; args[2]="1"; }
765 if (op != "" && isnum(args[2])) {
766 split(line[0],reg,",")
767 if (substr(reg[1],length(reg[1])-1,2) == args[1]) {
768 line[0] = substr(line[0],1,length(line[0])-1) op args[2] "]"
769 next
770 }
771 }
772 hold=0
773 if (/^ mov [sd]i,ax$/) {
774 split($2,args,",")
775 for (i = 0; i < kept; i++) {
776 sub(/ax/,args[1],line[i]); print line[i]
777 }
778 next
779 }
780 for (i = 0; i < kept; i++) print line[i]
781 }
782 else if (hold == 11) {
783 if (/^ inc ax$/ || /^ dec ax$/) {
784 line[kept++]=$0; next
785 }
786 split($2,args,",")
787 if (/^ mov cl,/) {
788 split($2,args,",")
789 if (args[2] >= 8) {
790 line[kept++]=$0; next
791 }
792 }
793 if (!/^ shl ax,/ || (args[2] != "cl" && args[2] < 8)) {
794 print " cbw "
795 }
796 for (i = 0; i < kept; i++) print line[i]
797 hold=kept=0
798 }
799 else if (hold == 12) {
800 hold=0
801 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
802 print " inc " regs[1]
803 print " inc " regs[1]
804 }
805 else print " add " regs[1] ",2"
806 }
807 else if (hold == 13) {
808 hold=0
809 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
810 print " dec " regs[1]
811 print " dec " regs[1]
812 }
813 else print " sub " regs[1] ",2"
814 }
815 else if (hold == 14) {
816 if (/^ push ax$/) { hold++; next; }
817 print " push dx";
818 hold=0;
819 }
820 else if (hold == 15) {
821 if (/^ pop eax$/) { hold++; next; }
822 print " push dx";
823 print " push ax";
824 hold=0;
825 }
826 else if (hold == 16) {
827 hold=0;
828 if (/^ shr eax,16$/) { print " xchg ax,dx"; next; }
829 print " push dx";
830 print " push ax";
831 print " pop eax";
832 }
833 else if (hold == 17) {
834 hold=0;
835 if (/^ cmp ax,-1$/) { print " inc ax"; next; }
836 }
837 if (/^ call near ptr @fileexist\$/ || # return boolean :
838 /^ call near ptr @isoreaddir\$/ || # 0=true, -1=false
839 /^ call near ptr @isoreset\$/ ||
840 /^ call near ptr @isoopen\$/ ||
841 /^ call near ptr @isoreadsector\$/ ||
842 /^ call near ptr @strhead\$/ ||
843 /^ call near ptr @strcmp\$/ ||
844 /^ call near ptr @argstr\$/ ||
845 /^ call near ptr @argnum\$/) { print; hold=17; next; }
846 s=$0
847 # These optimisation may break ZF or CF
848 if (/^ sub sp,2$/) { print " push ax"; next }
849 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
850 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
851 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
852 sub(/mov/,"and",s); print s; next # slower
853 }
854 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
855 sub(/mov/,"or",s); print s; next # slower
856 }
857 if (/^ or .*,0$/ || /^ and .*,-1$/) next
858 if (/^ or [abcd]x,/) {
859 split($2,args,",")
860 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
861 print " or " substr(args[1],1,1) "l," args[2]; next
862 }
863 }
864 if (/^ and [abcd]x,/) {
865 split($2,args,",")
866 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
867 print " and " substr(args[1],1,1) "l," args[2]; next
868 }
869 }
870 if (/^ or e[abcd]x,/) {
871 split($2,args,",")
872 if (args[2] == "large") { args[2] = $3 }
873 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
874 print " or " substr(args[1],2,1) "l," args[2]; next
875 }
876 }
877 if (/^ and e[abcd]x,/) {
878 split($2,args,",")
879 if (args[2] == "large") { args[2] = $3 }
880 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
881 print " and " substr(args[1],2,1) "l," args[2]; next
882 }
883 }
884 if (/^ or e[abcds][ix],/) {
885 split($2,args,",")
886 if (args[2] == "large") { args[2] = $3 }
887 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
888 print " or " substr(args[1],2) "," args[2]; next
889 }
890 }
891 if (/^ and e[abcds][ix],/) {
892 split($2,args,",")
893 if (args[2] == "large") { args[2] = $3 }
894 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
895 print " and " substr(args[1],2) "," args[2]; next
896 }
897 }
898 if (/^ add word ptr/ || /^ sub word ptr/ ||
899 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
900 split($0,args,",")
901 if (isnum(args[2]) && (args[2] % 256 == 0)) {
902 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
903 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
904 print s "/256"; next
905 }
906 }
907 if (/^ add dword ptr/ || /^ sub dword ptr/) {
908 split($0,args,",")
909 if (args[2] == "large") { args[2] = $3 }
910 if (isnum(args[2])) {
911 if (args[2] % 16777216 == 0) {
912 sub(/dword/,"byte",s)
913 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
914 print s "/16777216"; next
915 }
916 if (args[2] % 65536 == 0) {
917 sub(/dword/,"word",s)
918 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
919 print s "/65536"; next
920 }
921 }
922 }
923 if (/^ mov e.x,/) {
924 split($2,args,",")
925 r=args[1]
926 if (args[2] == "large") { args[2] = $3 }
927 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
928 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
929 print " xor " r "," r
930 if (args[2] == 0) next
931 x=" mov " substr(r,2,1)
932 if (args[2] % 256 == 0) {
933 print x "h," args[2] "/256"
934 }
935 else { print x "l," args[2] }
936 next
937 }
938 }
939 }
940 if (afterjmp) print ";" $0
941 else print
942 if (/^ jmp / || /boot_kernel\(\);/ ||
943 /^ call near ptr @die\$qpxzc/ ||
944 /^ call near ptr @exit\$qv/) afterjmp=1
945 }