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

linld: add iso support
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Jan 08 20:15:35 2021 +0000 (2021-01-08)
parents e07718b42e73
children 5c1ce90eb1d6
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")
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"
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 " cmp word ptr [si+39],17475"
312 print " stc"
313 $0=" jne returnC"
314 }
315 }
316 if (/cpytodirpage.x->dirpage/) isiso=16
317 if (isiso == 16) { # ISO9660.LST
318 if (/filesize2dirsize/) {
319 print " mov bx,word ptr [si+9]"
320 print " mov ax,word ptr [si+13]"
321 print "setdirofsnsz:"
322 print " mov word ptr [si+26],bx"
323 }
324 if (/ax,/ || /si\+13/ || /si\+26/) next
325 if (/si\+24/) isiso=1
326 }
327 if (/found:/) isiso=15
328 if (/short @1@394/) sub(/@1@394/,"@1@422")
329 if (isiso != 15 && /si,offset DGROUP:_isostate/) $0=";" $0
330 if (isiso == 15) { # ISO9660.LST
331 if (/xor/) {
332 print "returnNotC:"
333 print " cmc"
334 print "returnC:"
335 print "@1@422:"
336 print " sbb ax,ax"
337 print "return:"
338 next
339 }
340 if (/@1@422:/ || /bp$/ || /bp,sp/ || /sp,2/) next
341 if (/\[di\],47/) isiso=17
342 }
343 if (/short @1@142/) { isiso=14; next }
344 if (isiso == 14) { # ISO9660.LST
345 if (/ax,-1/) next
346 if (/jmp @1@422/) sub(/jmp/,"jb")
347 }
348 if (/p = x->buffer \+ 34/) isiso=13
349 if (isiso == 13) { # ISO9660.LST
350 if (/di,si/) $0=" xchg ax,bx"
351 if (/di,ax/) $0=" lea di,[si+bx+72]"
352 if (/di,72/) {
353 isiso=0
354 next
355 }
356 }
357 if (/register len/) isiso=12
358 if (isiso == 12) { # ISO9660.LST
359 if (/di\+2/) sub(/al/,"bl")
360 if (/cbw/) next
361 sub(/dx,ax/,"bh,0")
362 if (/bx,dx/) next
363 sub(/i,dx/,"i,bx")
364 sub(/bx\+di\],0/,"bx+di],bh")
365 if (/while/) isiso=0
366 }
367 if (/curpos >= SECT/) isiso=10
368 if (isiso == 10) { # ISO9660.LST
369 if (/cmp/) {
370 sub(/cmp /,"mov bx,")
371 sub(/i.*/,"i]")
372 print
373 $0=" cmp bh,2048/256"
374 }
375 if (/mov/) {
376 isiso=0
377 next
378 }
379 }
380 if (/<< SECTORBITS/) isiso=9
381 if (isiso == 9) { # ISO9660.LST
382 if (/dx,/) next
383 sub(/mov ax,/,"les ax,d")
384 if (/^ call/) {
385 print " extrn N_LXLSH@ES:near"
386 sub(/N_LXLSH@/,"N_LXLSH@ES")
387 isiso=0
388 }
389 }
390 if (/filesize =/) isiso=8
391 if (isiso == 8) { # ISO9660.LST
392 if (/ax,/) next
393 sub(/mov dx,/,"les dx,d")
394 sub(/,ax/,",es")
395 if (/filemod/) isiso=0
396 }
397 if (/entrysize =/) isiso=5
398 if (isiso == 5) { # ISO9660.LST
399 if (/ax,ax/) next
400 sub(/ax/,"cx")
401 sub(/je/,"jcxz")
402 if (/jcxz/) {
403 hold=0
404 print s
405 sub(/@1@114/,"@1@394")
406 print
407 print " mov word ptr [si+10],dx"
408 $0=" mov word ptr [si+8],ax"
409 }
410 if (/return/) isiso=0
411 }
412 if (/do s\+\+; while/) isiso=3
413 if (isiso == 3) { # ISO9660.LST
414 sub(/cmp byte ptr \[.i\]/,"sub al")
415 if (/inc /) { r=$2; print; next }
416 if (/al,0/) print " mov al,[" r "]"
417 if (/al,byte ptr/) next
418 if (/byte ptr \[.*\],0/) next
419 }
420 if (/ptr .isoreaddir/) isiso=1
421 if (isiso == 1) { # ISO9660.LST
422 if (/n = name;/) {
423 isiso=19
424 print " xchg ax,cx"
425 print " xchg cl,byte ptr [di] ; c"
426 }
427 if (/short @2@366/) sub(/jne/,"jnc")
428 if (/short @2@282/) sub(/je/,"jc")
429 if (/short @2@562/) sub(/@2@562/,"returnNotC")
430 if (/short @2@534/) { print; $0=" inc cx" }
431 if (/ax,word ptr \[si\+4\]/) $0=" xchg ax,bx ; " $0
432 if (/\[di\],al/) next
433 if (/@2@310/) next
434 if (/ax,-1/) next
435 if (/inc di/ || /@@0/) next
436 if (/@2@142$/) { print " inc di"; sub(/jmp/,"loop") }
437 }
438 if (/i\+36\]/) next
439 sub(/DGROUP:_isostate\[bx/,"[bx+si")
440 } # file == "iso9660.cpp"
441 if (wascall) {
442 if (rcall != "") {
443 if (/,ax$/) print " mov " rcall ",ax"
444 else print " xchg ax," rcall
445 wascall=0
446 }
447 else if (/^ mov .i,ax$/) {
448 split($2,y,",")
449 rcall=y[1]
450 next
451 }
452 else wascall=0
453 }
454 if (/^ call /) { wascall=1; rcall="" }
455 if (hold == 0) {
456 s=$0
457 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
458 r=$2; kept=0
459 hold=1; split($2,regs,","); next
460 }
461 if (/^ inc e?.[ixhl]/ || /^ dec e?.[ixhl]/) {
462 hold=2; r=$2; next
463 }
464 if (/^ mov [abcds][ix],/ && ! /,.s/) {
465 hold=3; split($2,regs,","); next
466 }
467 if (/^ movzx eax,ax$/) { hold=4; next }
468 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
469 split($0,regs,",")
470 if (isnum(regs[2]) && regs[2] != 0 &&
471 (regs[2] % 256) == 0) {
472 hold=5; next
473 }
474 }
475 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
476 if (/^ mov cl,4$/) { hold=7; next }
477 if (/^ cmp word ptr DGROUP:.*,0$/) {
478 hold=8; split($2,regs,","); next
479 }
480 if (/^ cbw/) { hold=11; kept=0; next }
481 if (/^ add [abcds][ix],2$/) {
482 split($2,regs,","); hold=12; next
483 }
484 if (/^ sub [abcds][ix],2$/) {
485 split($2,regs,","); hold=13; next
486 }
487 if (/^ push dx$/) {
488 hold=14; next;
489 }
490 }
491 else if (hold == 1) {
492 if (/^ ;/) { line[kept++]=$0; next }
493 hold=0; split($2,args,","); op=""
494 if ($1 == "add") op="+"
495 if ($1 == "sub") op="-"
496 if ($1 == "inc") { op="+"; args[2]="1"; }
497 if ($1 == "dec") { op="-"; args[2]="1"; }
498 if (op != "" && regs[1] == args[1]) {
499 if (isnum(args[2])) {
500 for (i = kept++; i > 0; i--) line[i] = line[i-1]
501 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
502 sub(/\+-/,"-",line[0])
503 hold=10; next
504 }
505 line[kept++]=$0
506 hold=1
507 next
508 }
509 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
510 print " xchg " r
511 }
512 else print s
513 for (i = 0; i < kept; i++) print line[i]; kept=0
514 }
515 else if (hold == 2) {
516 split($0,args,",")
517 if (/^ mov / && r == args[2]) { print s; s=$0; next }
518 split($2,args,",")
519 hold=0; print s
520 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
521 }
522 else if (hold == 3) {
523 hold=0
524 if (/^ call / && regs[2] == "ax") s=" xchg ax," regs[1]
525 if (/^ add [abcds][ix],/) {
526 split($2,regs2,",")
527 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
528 t=$0; sub(/mov/,$1,s); sub(/add/,"mov",t)
529 print t; print s; next
530 }
531 }
532 print s
533 }
534 else if (hold == 4) {
535 hold=0
536 if (/^ push eax$/) {
537 print " push 0"; print " push ax"; next
538 } else { print s }
539 }
540 else if (hold == 5) {
541 hold=0
542 if ($1 == "jae" || $1 == "jb") {
543 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
544 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
545 s = s "/256"
546 }
547 print s
548 }
549 else if (hold == 6) {
550 if (($1 == "and" || $1 == "add") && $2 ~ /^ax,/) {
551 line[kept++]=$0
552 next
553 }
554 p=$0
555 if (/^ movzx eax,ax$/) {
556 s=" mov eax,cs"; p=""
557 }
558 print s
559 for (i = 0; i < kept; i++) print line[i]; kept=0
560 if (p != "") print p
561 hold=0; next
562 }
563 else if (hold == 7) {
564 hold=0
565 if (/^ call near ptr N_LXURSH@$/) {
566 print " extrn N_LXURSH@4:near"
567 print " call near ptr N_LXURSH@4"
568 next
569 }
570 if (/^ call near ptr N_LXLSH@$/) {
571 print " extrn N_LXLSH@4:near"
572 print " call near ptr N_LXLSH@4"
573 next
574 }
575 print s
576 }
577 else if (hold == 8) {
578 if ($1 == "je" || $1 == "jne") { p=$0; hold=9; next }
579 hold=0
580 print s
581 }
582 else if (hold == 9) {
583 hold=0; split($2,args,",")
584 if (/^ mov ax,/ && args[2] == regs[1]) {
585 print; print " or ax,ax"; print p; next
586 }
587 print s; print p;
588 }
589 else if (hold == 10) {
590 split($2,args,","); op=""
591 if ($1 == "add") op="+"
592 if ($1 == "sub") op="-"
593 if ($1 == "inc") { op="+"; args[2]="1"; }
594 if ($1 == "dec") { op="-"; args[2]="1"; }
595 if (op != "" && isnum(args[2])) {
596 split(line[0],reg,",")
597 if (substr(reg[1],length(reg[1])-1,2) == args[1]) {
598 line[0] = substr(line[0],1,length(line[0])-1) op args[2] "]"
599 next
600 }
601 }
602 hold=0
603 if (/^ mov [sd]i,ax$/) {
604 split($2,args,",")
605 for (i = 0; i < kept; i++) {
606 sub(/ax/,args[1],line[i]); print line[i]
607 }
608 next
609 }
610 for (i = 0; i < kept; i++) print line[i]
611 }
612 else if (hold == 11) {
613 if (/^ inc ax$/ || /^ dec ax$/) {
614 line[kept++]=$0; next
615 }
616 split($2,args,",")
617 if (/^ mov cl,/) {
618 split($2,args,",")
619 if (args[2] >= 8) {
620 line[kept++]=$0; next
621 }
622 }
623 if (!/^ shl ax,/ || (args[2] != "cl" && args[2] < 8)) {
624 print " cbw "
625 }
626 for (i = 0; i < kept; i++) print line[i]
627 hold=kept=0
628 }
629 else if (hold == 12) {
630 hold=0
631 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
632 print " inc " regs[1]
633 print " inc " regs[1]
634 }
635 else print " add " regs[1] ",2"
636 }
637 else if (hold == 13) {
638 hold=0
639 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
640 print " dec " regs[1]
641 print " dec " regs[1]
642 }
643 else print " sub " regs[1] ",2"
644 }
645 else if (hold == 14) {
646 if (/^ push ax$/) { hold++; next; }
647 print " push dx";
648 hold=0;
649 }
650 else if (hold == 15) {
651 if (/^ pop eax$/) { hold++; next; }
652 print " push dx";
653 print " push ax";
654 hold=0;
655 }
656 else if (hold == 16) {
657 hold=0;
658 if (/^ shr eax,16$/) { print " xchg ax,dx"; next; }
659 print " push dx";
660 print " push ax";
661 print " pop eax";
662 }
663 else if (hold == 17) {
664 hold=0;
665 if (/^ cmp ax,-1$/) { print " inc ax"; next; }
666 }
667 if (/^ call near ptr @fileexist\$/ || # return boolean :
668 /^ call near ptr @isoreaddir\$/ || # 0=true, -1=false
669 /^ call near ptr @isoreset\$/ ||
670 /^ call near ptr @isoopen\$/ ||
671 /^ call near ptr @isoreadsector\$/ ||
672 /^ call near ptr @strhead\$/ ||
673 /^ call near ptr @strcmp\$/ ||
674 /^ call near ptr @argstr\$/ ||
675 /^ call near ptr @argnum\$/) { print; hold=17; next; }
676 s=$0
677 # These optimisation may break ZF or CF
678 if (/^ sub sp,2$/) { print " push ax"; next }
679 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
680 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
681 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
682 sub(/mov/,"and",s); print s; next # slower
683 }
684 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
685 sub(/mov/,"or",s); print s; next # slower
686 }
687 if (/^ or .*,0$/ || /^ and .*,-1$/) next
688 if (/^ or [abcd]x,/) {
689 split($2,args,",")
690 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
691 print " or " substr(args[1],1,1) "l," args[2]; next
692 }
693 }
694 if (/^ and [abcd]x,/) {
695 split($2,args,",")
696 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
697 print " and " substr(args[1],1,1) "l," args[2]; next
698 }
699 }
700 if (/^ or e[abcd]x,/) {
701 split($2,args,",")
702 if (args[2] == "large") { args[2] = $3 }
703 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
704 print " or " substr(args[1],2,1) "l," args[2]; next
705 }
706 }
707 if (/^ and e[abcd]x,/) {
708 split($2,args,",")
709 if (args[2] == "large") { args[2] = $3 }
710 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
711 print " and " substr(args[1],2,1) "l," args[2]; next
712 }
713 }
714 if (/^ or e[abcds][ix],/) {
715 split($2,args,",")
716 if (args[2] == "large") { args[2] = $3 }
717 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
718 print " or " substr(args[1],2) "," args[2]; next
719 }
720 }
721 if (/^ and e[abcds][ix],/) {
722 split($2,args,",")
723 if (args[2] == "large") { args[2] = $3 }
724 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
725 print " and " substr(args[1],2) "," args[2]; next
726 }
727 }
728 if (/^ add word ptr/ || /^ sub word ptr/ ||
729 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
730 split($0,args,",")
731 if (isnum(args[2]) && (args[2] % 256 == 0)) {
732 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
733 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
734 print s "/256"; next
735 }
736 }
737 if (/^ add dword ptr/ || /^ sub dword ptr/) {
738 split($0,args,",")
739 if (args[2] == "large") { args[2] = $3 }
740 if (isnum(args[2])) {
741 if (args[2] % 16777216 == 0) {
742 sub(/dword/,"byte",s)
743 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
744 print s "/16777216"; next
745 }
746 if (args[2] % 65536 == 0) {
747 sub(/dword/,"word",s)
748 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
749 print s "/65536"; next
750 }
751 }
752 }
753 if (/^ mov e.x,/) {
754 split($2,args,",")
755 r=args[1]
756 if (args[2] == "large") { args[2] = $3 }
757 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
758 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
759 print " xor " r "," r
760 if (args[2] == 0) next
761 x=" mov " substr(r,2,1)
762 if (args[2] % 256 == 0) {
763 print x "h," args[2] "/256"
764 }
765 else { print x "l," args[2] }
766 next
767 }
768 }
769 }
770 if (afterjmp) print ";" $0
771 else print
772 if (/^ jmp / || /^ call near ptr _boot_kernel/ ||
773 /^ call near ptr @die\$qpxzc/ ||
774 /^ call near ptr @exit\$qv/) afterjmp=1
775 }