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

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