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