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