wok-6.x view linld/stuff/src/pipehole.awk @ rev 24564

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