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

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