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

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