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

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