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

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