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

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