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

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