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

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