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

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