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

Fix loram dos boot
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Aug 01 15:16:29 2019 +0200 (2019-08-01)
parents 68f9518c4cb0
children 1c40a6bea05c
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
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
247 if (/_vid_mode,ax/ && xlabel == "") goto2=1
248 if (/_initrd_name,si/ && xlabel == "") goto2=1
249 if (/_base_himem\+2,/ && xlabel == "@") goto2=1
250 if (/DGROUP:_skip_alloc/ && xlabel == "@") goto2=1
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 (/if\(\*s>=/) isotazboot=14
261 if (isotazboot == 14) { # LINLD.LST
262 if (/jmp/) {
263 $0=" db 0A9h ; test ax,xxxx instead of " $0
264 isotazboot=0
265 }
266 }
267 if (file == "tazboot.cpp" && /; s \+= 4/) isotazboot=13
268 if (isotazboot == 13) { # TAZBOOT.LST
269 if (/si,4/) $0=" lea bx,[si+4]"
270 if (/bx,si/) next
271 if (/DGROUP:_topmem/ || /set_iso/) isotazboot=0
272 }
273 if (file == "tazboot.cpp" && /case 0x652F:/) isotazboot=12
274 if (isotazboot == 12) { # TAZBOOT.LST
275 sub(/si,word/,"bx,word")
276 if (/short/) isotazboot=0
277 }
278 if (/return load_kernel/) isotazboot=11
279 if (isotazboot == 11) { # TAZBOOT.LST
280 sub(/call/,"jmp")
281 if (/ret/ || /pop/) next
282 if (/endp/) isotazboot=0
283 }
284 if (/cmdline=s\+=3/ || /magic \!= 0/ || /&root_dev =/) { isotazboot=10; j="" }
285 if (isotazboot == 10) { # TAZBOOT.LST && LINLD.LST
286 if (/je/ || /jne/) { j=$1; next }
287 if (/jmp/) {
288 if (j=="jne") sub(/jmp/,"je")
289 else if (j=="je") sub(/jmp/,"jne")
290 isotazboot=0
291 }
292 }
293 if (/static const unsigned long initrddesc = 18L/) isotazboot=9
294 if (isotazboot == 9) { # TAZBOOT.LST
295 if (/,0/) {
296 split($4,y,",")
297 print " mov bx,offset " y[1]
298 }
299 if (/DGROUP:.*\+6,46/) {
300 sub(/DGROUP:.*\+6,/,"[bx+6],")
301 isotazboot=0
302 }
303 if (/mov/) $0=" mov si,bx"
304 sub(/DGROUP:.*,/,"[bx],")
305 }
306 if (/isoopen\(s\+7\) != -1/) isotazboot=8
307 if (isotazboot == 8) { # TAZBOOT.LST
308 if (/ax,si/) next
309 sub(/ax,ax/,"si,si")
310 if (/magic/) isotazboot=0
311 }
312 if (/\+\+isknoppix/) isotazboot=7
313 if (isotazboot == 7) { # TAZBOOT.LST
314 if (/inc/ || /,al/) next
315 if (/al,byte/) sub (/mov al,/,"inc ")
316 if (/isokernel/) isotazboot=0
317 }
318 if (/if \(c\) s\+\+;/) isotazboot=6
319 if (isotazboot == 6) { # TAZBOOT.LST
320 if (/cmp/) {
321 $0=" cmp al,0"
322 isotazboot=0
323 }
324 }
325 if (/static void next_chunk/) isotazboot=5
326 if (isotazboot == 501) {
327 if (/ret/) {
328 print "@1@86:"
329 isotazboot=0
330 }
331 }
332 if (isotazboot == 5 || isotazboot == 500) { # TAZBOOT.LST
333 if (/cx,ax/) $0=" xchg ax,bx"
334 if (/ax,word ptr \[si\+28\]/ && isotazboot == 500) next
335 if (/bx,cx/) next
336 if (/push/ || /pop/ || /bp,sp/ || /si,/) next
337 sub(/\[si/,"[di")
338 if (/initrd_info/) isotazboot=500
339 if (/bx\+6\]/) next
340 if (/bx\+4\]/) sub(/mov dx,/,"les dx,d")
341 sub(/di\+24\],ax/,"di+24],es")
342 sub(/call/,"jmp")
343 if (/ret/ || /pop/ || /^@1@86:/) next
344 if (/_isostate\+14/) next
345 if (/_isostate\+12/) {
346 sub(/mov ax,/,"les ax,d")
347 print
348 print " mov dx,es"
349 next
350 }
351 if (/ax,-4/) isotazboot++
352 }
353 if (/0x7FF0/) isotazboot=4
354 if (isotazboot == 4) { # TAZBOOT.LST
355 if (/ax,word ptr/) {
356 print " mov ax,32752"
357 print " cwd"
358 sub(/mov/,"sub")
359 }
360 if (/bx,/ || /cx,/ || /dx,/) next
361 sub(/,0/,",dx")
362 sub(/,bx/,",dx")
363 sub(/,cx/,",ax")
364 if (/@addinitrd\$qv/) isotazboot=0
365 }
366 if (/c = x->filename/) isotazboot=3
367 if (isotazboot == 3) { # TAZBOOT.LST
368 if (/ax,/) $0=" xchg ax,bx"
369 if (/\]$/) next
370 if (/@strcpy\$qpxzct1/) isotazboot=0
371 }
372 if (/base_himem = memtop/) isotazboot=2
373 if (isotazboot == 2) { # TAZBOOT.LST
374 if (/@6@226/ || /mov ax,1/ || /@6@254/ || /xor ax,ax/) next
375 if (/word ptr \[bx\+2\],0/) {
376 print s; hold=0
377 print " mov bx,word ptr [bx+2]"
378 $0=" or bx,bx"
379 }
380 if (/\[bp-4\],ax/) sub(/ax/,"bx")
381 if (/ax,word ptr \[bx\+2\]/ || /bx,ax/) next
382 if (/_base_himem\+2,dx/) {
383 print " mov bx,offset DGROUP:_base_himem+2"
384 }
385 sub(/DGROUP:_base_himem,/,"[bx-2],")
386 sub(/DGROUP:_base_himem\+2,/,"[bx],")
387 sub(/DGROUP:_base_himem\+3,/,"[bx+1],")
388 if (/@strcmp\$qpxzct1/) isotazboot=0
389 }
390 if (/static void addinitrd/) isotazboot=100
391 if (isotazboot == 100) { # TAZBOOT.LST
392 if (/cx,ax/) {
393 print " mov si,offset _isostate+8"
394 print " push ds"
395 print " pop es"
396 print " xchg ax,di"
397 print " movsw"
398 print " movsw"
399 print " movsw"
400 print " movsw"
401 $0=" xchg ax,di"
402 }
403 if (/mov/ && !/si/ && !/cl/) next
404 if (/void load_initrds/) isotazboot=101
405 }
406 if (isotazboot == 101 || isotazboot == 102) { # TAZBOOT.LST
407 sub(/\[si/,"[di"); sub(/si,/,"di,"); sub(/si$/,"di")
408 sub(/DGROUP:_imgs\+38$/,"[di+38-32]")
409 sub(/DGROUP:_imgs\+40$/,"[di+40-32]")
410 if (/isofd/) isotazboot=102
411 if (/push/ && isotazboot == 102) next
412 if (/pop/ && isotazboot == 102) next
413 if (/load_initrd/) isotazboot=101
414 if (/isokernel/) isotazboot=103
415 }
416 if (isotazboot > 102) { # TAZBOOT.LST
417 if (/push/ || /pop/) next
418 sub(/\[si/,"[bx")
419 sub(/si,/,"bx,")
420 if (/static void bootiso/) isotazboot=0
421 }
422 if (wascall) {
423 if (rcall != "") {
424 if (/,ax$/) print " mov " rcall ",ax"
425 else print " xchg ax," rcall
426 wascall=0
427 }
428 else if (/^ mov .i,ax$/) {
429 split($2,y,",")
430 rcall=y[1]
431 next
432 }
433 else wascall=0
434 }
435 if (/^ call /) { wascall=1; rcall="" }
436 if (hold == 0) {
437 s=$0
438 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
439 r=$2; kept=0
440 hold=1; split($2,regs,","); next
441 }
442 if (/^ inc e?.[ixhl]/ || /^ dec e?.[ixhl]/) {
443 hold=2; r=$2; next
444 }
445 if (/^ mov [abcds][ix],/ && ! /,.s/) {
446 hold=3; split($2,regs,","); next
447 }
448 if (/^ movzx eax,ax$/) { hold=4; next }
449 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
450 split($0,regs,",")
451 if (isnum(regs[2]) && regs[2] != 0 &&
452 (regs[2] % 256) == 0) {
453 hold=5; next
454 }
455 }
456 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
457 if (/^ mov cl,4$/) { hold=7; next }
458 if (/^ cmp word ptr DGROUP:.*,0$/) {
459 hold=8; split($2,regs,","); next
460 }
461 if (/^ cbw/) { hold=11; kept=0; next }
462 if (/^ add [abcds][ix],2$/) {
463 split($2,regs,","); hold=12; next
464 }
465 if (/^ sub [abcds][ix],2$/) {
466 split($2,regs,","); hold=13; next
467 }
468 if (/^ push dx$/) {
469 hold=14; next;
470 }
471 }
472 else if (hold == 1) {
473 if (/^ ;/) { line[kept++]=$0; next }
474 hold=0; split($2,args,","); op=""
475 if ($1 == "add") op="+"
476 if ($1 == "sub") op="-"
477 if ($1 == "inc") { op="+"; args[2]="1"; }
478 if ($1 == "dec") { op="-"; args[2]="1"; }
479 if (op != "" && regs[1] == args[1]) {
480 if (isnum(args[2])) {
481 for (i = kept++; i > 0; i--) line[i] = line[i-1]
482 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
483 hold=10; next
484 }
485 line[kept++]=$0
486 hold=1
487 next
488 }
489 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
490 print " xchg " r
491 }
492 else print s
493 for (i = 0; i < kept; i++) print line[i]; kept=0
494 }
495 else if (hold == 2) {
496 split($0,args,",")
497 if (/^ mov / && r == args[2]) { print s; s=$0; next }
498 split($2,args,",")
499 hold=0; print s
500 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
501 }
502 else if (hold == 3) {
503 hold=0
504 if (/^ call / && regs[2] == "ax") s=" xchg ax," regs[1]
505 if (/^ add [abcds][ix],/) {
506 split($2,regs2,",")
507 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
508 t=$0; sub(/mov/,$1,s); sub(/add/,"mov",t)
509 print t; print s; next
510 }
511 }
512 print s
513 }
514 else if (hold == 4) {
515 hold=0
516 if (/^ push eax$/) {
517 print " push 0"; print " push ax"; next
518 } else { print s }
519 }
520 else if (hold == 5) {
521 hold=0
522 if ($1 == "jae" || $1 == "jb") {
523 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
524 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
525 s = s "/256"
526 }
527 print s
528 }
529 else if (hold == 6) {
530 if (($1 == "and" || $1 == "add") && $2 ~ /^ax,/) {
531 line[kept++]=$0
532 next
533 }
534 p=$0
535 if (/^ movzx eax,ax$/) {
536 s=" mov eax,cs"; p=""
537 }
538 print s
539 for (i = 0; i < kept; i++) print line[i]; kept=0
540 if (p != "") print p
541 hold=0; next
542 }
543 else if (hold == 7) {
544 hold=0
545 if (/^ call near ptr N_LXURSH@$/) {
546 print " extrn N_LXURSH@4:near"
547 print " call near ptr N_LXURSH@4"
548 next
549 }
550 if (/^ call near ptr N_LXLSH@$/) {
551 print " extrn N_LXLSH@4:near"
552 print " call near ptr N_LXLSH@4"
553 next
554 }
555 print s
556 }
557 else if (hold == 8) {
558 if ($1 == "je" || $1 == "jne") { p=$0; hold=9; next }
559 hold=0
560 print s
561 }
562 else if (hold == 9) {
563 hold=0; split($2,args,",")
564 if (/^ mov ax,/ && args[2] == regs[1]) {
565 print; print " or ax,ax"; print p; next
566 }
567 print s; print p;
568 }
569 else if (hold == 10) {
570 split($2,args,","); op=""
571 if ($1 == "add") op="+"
572 if ($1 == "sub") op="-"
573 if ($1 == "inc") { op="+"; args[2]="1"; }
574 if ($1 == "dec") { op="-"; args[2]="1"; }
575 if (op != "" && isnum(args[2])) {
576 split(line[0],reg,",")
577 if (substr(reg[1],length(reg[1])-1,2) == args[1]) {
578 line[0] = substr(line[0],1,length(line[0])-1) op args[2] "]"
579 next
580 }
581 }
582 hold=0
583 if (/^ mov [sd]i,ax$/) {
584 split($2,args,",")
585 for (i = 0; i < kept; i++) {
586 sub(/ax/,args[1],line[i]); print line[i]
587 }
588 next
589 }
590 for (i = 0; i < kept; i++) print line[i]
591 }
592 else if (hold == 11) {
593 if (/^ inc ax$/ || /^ dec ax$/) {
594 line[kept++]=$0; next
595 }
596 split($2,args,",")
597 if (/^ mov cl,/) {
598 split($2,args,",")
599 if (args[2] >= 8) {
600 line[kept++]=$0; next
601 }
602 }
603 if (!/^ shl ax,/ || (args[2] != "cl" && args[2] < 8)) {
604 print " cbw "
605 }
606 for (i = 0; i < kept; i++) print line[i]
607 hold=kept=0
608 }
609 else if (hold == 12) {
610 hold=0
611 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
612 print " inc " regs[1]
613 print " inc " regs[1]
614 }
615 else print " add " regs[1] ",2"
616 }
617 else if (hold == 13) {
618 hold=0
619 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
620 print " dec " regs[1]
621 print " dec " regs[1]
622 }
623 else print " sub " regs[1] ",2"
624 }
625 else if (hold == 14) {
626 if (/^ push ax$/) { hold++; next; }
627 print " push dx";
628 hold=0;
629 }
630 else if (hold == 15) {
631 if (/^ pop eax$/) { hold++; next; }
632 print " push dx";
633 print " push ax";
634 hold=0;
635 }
636 else if (hold == 16) {
637 hold=0;
638 if (/^ shr eax,16$/) { print " xchg ax,dx"; next; }
639 print " push dx";
640 print " push ax";
641 print " pop eax";
642 }
643 else if (hold == 17) {
644 hold=0;
645 if (/^ cmp ax,-1$/) { print " inc ax"; next; }
646 }
647 if (/^ call near ptr @fileexist\$/ || # return boolean :
648 /^ call near ptr @isoreaddir\$/ || # 0=true, -1=false
649 /^ call near ptr @isoreset\$/ ||
650 /^ call near ptr @isoopen\$/ ||
651 /^ call near ptr @isoreadsector\$/ ||
652 /^ call near ptr @strhead\$/ ||
653 /^ call near ptr @argstr\$/ ||
654 /^ call near ptr @argnum\$/) { print; hold=17; next; }
655 s=$0
656 # These optimisation may break ZF or CF
657 if (/^ sub sp,2$/) { print " push ax"; next }
658 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
659 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
660 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
661 sub(/mov/,"and",s); print s; next # slower
662 }
663 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
664 sub(/mov/,"or",s); print s; next # slower
665 }
666 if (/^ or .*,0$/ || /^ and .*,-1$/) next
667 if (/^ or [abcd]x,/) {
668 split($2,args,",")
669 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
670 print " or " substr(args[1],1,1) "l," args[2]; next
671 }
672 }
673 if (/^ and [abcd]x,/) {
674 split($2,args,",")
675 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
676 print " and " substr(args[1],1,1) "l," args[2]; next
677 }
678 }
679 if (/^ or e[abcd]x,/) {
680 split($2,args,",")
681 if (args[2] == "large") { args[2] = $3 }
682 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
683 print " or " substr(args[1],2,1) "l," args[2]; next
684 }
685 }
686 if (/^ and e[abcd]x,/) {
687 split($2,args,",")
688 if (args[2] == "large") { args[2] = $3 }
689 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
690 print " and " substr(args[1],2,1) "l," args[2]; next
691 }
692 }
693 if (/^ or e[abcds][ix],/) {
694 split($2,args,",")
695 if (args[2] == "large") { args[2] = $3 }
696 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
697 print " or " substr(args[1],2) "," args[2]; next
698 }
699 }
700 if (/^ and e[abcds][ix],/) {
701 split($2,args,",")
702 if (args[2] == "large") { args[2] = $3 }
703 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
704 print " and " substr(args[1],2) "," args[2]; next
705 }
706 }
707 if (/^ add word ptr/ || /^ sub word ptr/ ||
708 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
709 split($0,args,",")
710 if (isnum(args[2]) && (args[2] % 256 == 0)) {
711 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
712 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
713 print s "/256"; next
714 }
715 }
716 if (/^ add dword ptr/ || /^ sub dword ptr/) {
717 split($0,args,",")
718 if (args[2] == "large") { args[2] = $3 }
719 if (isnum(args[2])) {
720 if (args[2] % 16777216 == 0) {
721 sub(/dword/,"byte",s)
722 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
723 print s "/16777216"; next
724 }
725 if (args[2] % 65536 == 0) {
726 sub(/dword/,"word",s)
727 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
728 print s "/65536"; next
729 }
730 }
731 }
732 if (/^ mov e.x,/) {
733 split($2,args,",")
734 r=args[1]
735 if (args[2] == "large") { args[2] = $3 }
736 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
737 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
738 print " xor " r "," r
739 if (args[2] == 0) next
740 x=" mov " substr(r,2,1)
741 if (args[2] % 256 == 0) {
742 print x "h," args[2] "/256"
743 }
744 else { print x "l," args[2] }
745 next
746 }
747 }
748 }
749 if (afterjmp) print ";" $0
750 else print
751 if (/^ jmp / || /^ call near ptr _boot_kernel/ ||
752 /^ call near ptr @die$qpxzc/) afterjmp=1
753 }