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

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