wok-current view linld/stuff/src/pipehole.awk @ rev 24019

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