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

syslinux/isoboot.s: accept user args (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Jun 19 17:01:11 2020 +0000 (2020-06-19)
parents c04335df099f
children 26195667e7c9
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 (/while \(\*\+\+s/) isiso=11
207 if (isiso == 11) { # ISO9660.LST
208 if (/cmp/ || /filename2open/) isiso=0
209 if (/cmp/) next
210 if (/mov/) {
211 sub(/mov bx,/,"cmp byte ptr [")
212 sub(/i$/,"i],0")
213 }
214 }
215 if (/curpos >= SECT/) isiso=10
216 if (isiso == 10) { # ISO9660.LST
217 if (/cmp/) {
218 sub(/cmp /,"mov bx,")
219 sub(/i.*/,"i]")
220 print
221 $0=" cmp bh,2048/256"
222 }
223 if (/mov/) {
224 isiso=0
225 next
226 }
227 }
228 if (/<< SECTORBITS/) isiso=9
229 if (isiso == 9) { # ISO9660.LST
230 if (/dx,/) next
231 sub(/mov ax,/,"les ax,d")
232 if (/^ call/) {
233 print " extrn N_LXLSH@ES:near"
234 sub(/N_LXLSH@/,"N_LXLSH@ES")
235 isiso=0
236 }
237 }
238 if (/filesize =/) isiso=8
239 if (isiso == 8) { # ISO9660.LST
240 if (/ax,/) next
241 sub(/mov dx,/,"les dx,d")
242 sub(/,ax/,",es")
243 if (/filemod/) isiso=0
244 }
245 if (/CD001/) isiso=7
246 if (isiso == 7) { # ISO9660.LST
247 sub(/mov ax,-1/,"dec ax")
248 if (/jmp/) isiso=0
249 }
250 if (/int len =/) isiso=6
251 if (isiso == 6) { # ISO9660.LST
252 if (/dx,ax/) next
253 sub(/ax/,"dx")
254 sub(/cx,di/,"bx,di")
255 sub(/cx,dx/,"bx,ax")
256 sub(/di,dx/,"di,ax")
257 if (/while/) isiso=2
258 }
259 if (/entrysize =/) isiso=5
260 if (isiso == 5) { # ISO9660.LST
261 if (/ax,ax/) next
262 sub(/ax/,"cx")
263 sub(/je/,"jcxz")
264 if (/return/) isiso=0
265 }
266 if (/x->curdirsize == 0xFFFF/) isiso=4
267 if (isiso == 4) { # ISO9660.LST
268 sub(/DGROUP:_isostate\+18/,"[si+18]")
269 sub(/DGROUP:_isostate\+20/,"[si+20]")
270 if (/goto restarted/) isiso=0
271 }
272 } # file == "iso9660.cpp"
273 if (file == "iso9660.cpp" || file == "tazboot.cpp") {
274 if (/do s\+\+; while/) isiso=3
275 if (/for \(p = s; \*s && \*s \!=/) isiso=3 # tazboot/main
276 if (isiso == 3) { # ISO9660.LST, TAZBOOT.LST
277 sub(/cmp byte ptr \[.i\]/,"sub al")
278 if (/mov byte ptr \[bp-5\],al/) $0=" push ax"
279 if (/mov al,byte ptr \[bp-5\]/) $0=" pop ax"
280 if (/inc /) { r=$2; print; next }
281 if (/al,0/) print " mov al,[" r "]"
282 if (/al,byte ptr/) sub(/mov/,"xchg")
283 if (/byte ptr \[.*\],0/) next
284 if (/jmp/) {
285 print " mov bx,si"
286 $0=" db 0A8h ; test al,xx instead of " $0
287 }
288 if (/word ptr \[bp-4\]/) next
289 if (/\) s\+\+;/ || /\],-1/) isiso=0
290 }
291 } # file == "iso9660.cpp" || file == "tazboot.cpp"
292 if (file == "iso9660.cpp") {
293 if (/endname = NULL/) isiso=2
294 if (isiso == 2) { # ISO9660.LST
295 if (/mov bx,cx/) next
296 gsub(/cx/,"bx")
297 sub(/DGROUP:_isostate\+35/,"[si+35]")
298 }
299 if (/const char \*n = name/) isiso=1
300 if (isiso == 1) { # ISO9660.LST
301 if ((/mov word ptr \[si\+32\],ax/ ) ||
302 (/mov ax,word ptr \[si\+2\]/) ||
303 (/bx,word ptr \[si\+32\]/) || (/ax,dx/)) next
304 if (/dx,/) sub(/dx/,"ax")
305 if ((/sub ax,word ptr \[si\+2\]/) ||
306 (/\[si\+16\]/) || (/ax,di/)) sub(/ax/,"bx")
307 if (/add word ptr \[si\+32\],ax/) $0=" add bx,word ptr [si+16]"
308 if (/al,/ || /,al/) sub(/al/,"cl")
309 if (/cmp byte ptr \[si\+34\],0/) $0=" or cl,cl"
310 if (/jne @@0$/) next
311 if (/jmp @3@58$/) $0=" je @3@58"
312 sub(/mov ax,-1/,"dec ax")
313 }
314 } # file == "iso9660.cpp"
315 if (/endp/) { xlabel = ""; goto2=0 }
316 if (/isoopen\(s\+7\)/ && xlabel == "") goto2=1 # tazboot/bootiso
317 if (/_vid_mode,ax/ && xlabel == "") goto2=1 # tazboot/main
318 if (/_initrd_name,si/ && xlabel == "") goto2=1 # tazboot/main
319 if (/_base_himem\+2,/ && xlabel == "@") goto2=1 # tazboot/bootiso tazboot/main
320 if (/DGROUP:_skip_alloc/ && xlabel == "@") goto2=1 # tazboot/bootiso tazboot/main
321 if (/puts\(cmdline\)/ && xlabel == "@@") goto2=1
322 if (goto2 == 1 && /jmp/) { # TAZBOOT.LST && LINLD.LST
323 print $NF xlabel "@:"
324 label=$NF
325 }
326 if (goto2 > 0 && label == $NF) {
327 $0=$0 xlabel
328 if (goto2++ == 1) xlabel=xlabel "@"
329 }
330 if (file == "tazboot.cpp" && /close\(x/) isotazboot=16
331 if (isotazboot == 160) { # TAZBOOT.LST
332 $0="; " $0
333 if (/ret/) isotazboot=0
334 }
335 if (isotazboot == 16) { # TAZBOOT.LST
336 if (/@.@/) {
337 isotazboot=160
338 next
339 }
340 }
341 if (file == "tazboot.cpp" && /jne @@2/) isotazboot=15
342 if (isotazboot == 15) { # TAZBOOT.LST
343 if (/@.@/) {
344 print " pop di"
345 print " pop si"
346 print " mov sp,bp"
347 print " pop bp"
348 print " ret"
349 next
350 }
351 if (/skip_alloc/) isotazboot=0
352 }
353 if (/if\(\*s>=/) isotazboot=14
354 if (isotazboot == 14) { # LINLD.LST
355 if (/jmp/) {
356 $0=" db 0A9h ; test ax,xxxx instead of " $0
357 isotazboot=0
358 }
359 }
360 if (file == "tazboot.cpp" && /; s \+= 4/) isotazboot=13 # tazboot/main
361 if (isotazboot == 13) { # TAZBOOT.LST
362 if (/si,4/) $0=" lea bx,[si+4]"
363 if (/bx,si/) next
364 if (/DGROUP:_topmem/ || /set_iso/) isotazboot=0
365 }
366 if (file == "tazboot.cpp" && /case 0x652F:/) isotazboot=12 # tazboot/main
367 if (isotazboot == 12) { # TAZBOOT.LST
368 sub(/si,word/,"bx,word")
369 if (/short/) isotazboot=0
370 }
371 if (/return load_kernel/) isotazboot=11 # tazboot/isokernel
372 if (isotazboot == 11) { # TAZBOOT.LST
373 sub(/call/,"jmp")
374 if (/ret/ || /pop/) next
375 if (/endp/) isotazboot=0
376 }
377 if (/cmdline=s\+=3/ || /magic \!= 0/ || /&root_dev =/) { isotazboot=10; j="" } # ,tazboot/bootiso,tazboot/main
378 if (isotazboot == 10) { # TAZBOOT.LST && LINLD.LST
379 if (/je/ || /jne/) { j=$1; next }
380 if (/jmp/) {
381 if (j=="jne") sub(/jmp/,"je")
382 else if (j=="je") sub(/jmp/,"jne")
383 isotazboot=0
384 }
385 }
386 if (/static const unsigned long initrddesc = 18L/) isotazboot=9 # tazboot/bootiso
387 if (isotazboot == 9) { # TAZBOOT.LST
388 if (/,0/) {
389 split($4,y,",")
390 print " mov bx,offset " y[1]
391 sub(/DGROUP:.*,/,"[bx],")
392 }
393 if (/mov/ && $3 == y[1]) next
394 if (/je/) next
395 if (/jmp/) sub(/jmp/,"jne")
396 sub(/ax,offset/,"bx,offset")
397 if (/bx,ax/) { isotazboot=0; next }
398 }
399 if (/isoopen\(s\+7\) != -1/) isotazboot=8 # tazboot/bootiso
400 if (isotazboot == 8) { # TAZBOOT.LST
401 sub(/\[bx/,"[si")
402 if (/bx,si/) next
403 if (/magic/) isotazboot=0
404 }
405 if (/isoopen\(\"bzImage\"\)/) isotazboot=7 # tazboot/bootiso
406 if (isotazboot == 7) { # TAZBOOT.LST
407 if (/inc/ || /,al/) next
408 if (/al,byte/) sub (/mov al,/,"inc ")
409 if (/isokernel/) isotazboot=0
410 }
411 if (/if \(c\) s\+\+;/) isotazboot=6 # tazboot/main
412 if (isotazboot == 6) { # TAZBOOT.LST
413 if (/cmp/) {
414 $0=" cmp al,0"
415 isotazboot=0
416 }
417 }
418 if (/static void next_chunk/) isotazboot=5 # tazboot/next_chunk
419 if (isotazboot == 501) {
420 if (/ret/) {
421 print "@1@86:"
422 isotazboot=0
423 }
424 }
425 if (isotazboot == 5 || isotazboot == 500) { # TAZBOOT.LST
426 if (/cx,ax/) $0=" xchg ax,bx"
427 if (/ax,word ptr \[si\+28\]/ && isotazboot == 500) next
428 if (/bx,cx/) next
429 if (/push/ || /pop/ || /bp,sp/ || /si,/) next
430 sub(/\[si/,"[di")
431 if (/initrd_info/) isotazboot=500
432 if (/bx\+6\]/) next
433 if (/bx\+4\]/) sub(/mov dx,/,"les dx,d")
434 sub(/di\+24\],ax/,"di+24],es")
435 sub(/call/,"jmp")
436 if (/ret/ || /pop/ || /^@1@86:/) next
437 if (/_isostate\+14/) next
438 if (/_isostate\+12/) {
439 sub(/mov ax,/,"les ax,d")
440 print
441 print " mov dx,es"
442 next
443 }
444 if (/ax,-4/) isotazboot++
445 }
446 if (/0x7FF0/) isotazboot=4 # tazboot/bootiso
447 if (isotazboot == 4) { # TAZBOOT.LST
448 if (/ax,word ptr/) {
449 print " mov ax,32752"
450 print " cwd"
451 sub(/mov/,"sub")
452 }
453 if (/bx,/ || /cx,/ || /dx,/) next
454 sub(/,0/,",dx")
455 sub(/,bx/,",dx")
456 sub(/,cx/,",ax")
457 if (/@addinitrd\$qv/) isotazboot=0
458 }
459 if (/c = x->filename/) isotazboot=3 # tazboot/bootiso
460 if (isotazboot == 3) { # TAZBOOT.LST
461 if (/ax,/) $0=" xchg ax,bx"
462 if (/\]$/) next
463 if (/@strcpy\$qpxzct1/) isotazboot=0
464 }
465 if (/base_himem = memtop/) isotazboot=2 # tazboot/bootiso
466 if (isotazboot == 2) { # TAZBOOT.LST
467 if (/word ptr \[si\+2\],0/) {
468 print s; hold=0
469 print " mov bx,word ptr [si+2]"
470 $0=" or bx,bx"
471 }
472 if (/\[bp-4\],ax/) sub(/ax/,"bx")
473 if (/ax,word ptr \[si\+2\]/ || /bx,ax/) next
474 if (/_base_himem\+2,dx/) {
475 print " mov bx,offset DGROUP:_base_himem+2"
476 }
477 sub(/DGROUP:_base_himem,/,"[bx-2],")
478 sub(/DGROUP:_base_himem\+2,/,"[bx],")
479 sub(/DGROUP:_base_himem\+3,/,"[bx+1],")
480 if (/@strcmp\$qpxzct1/) isotazboot=0
481 }
482 if (/static void addinitrd/) isotazboot=100 # tazboot/addinitrd
483 if (isotazboot == 100) { # TAZBOOT.LST
484 if (/cx,ax/) {
485 print " mov si,offset _isostate+8"
486 print " push ds"
487 print " pop es"
488 print " xchg ax,di"
489 print " movsw"
490 print " movsw"
491 print " movsw"
492 print " movsw"
493 $0=" xchg ax,di"
494 }
495 if (/mov/ && !/si/ && !/cl/) next
496 if (/void load_initrds/) isotazboot=101
497 }
498 if (isotazboot == 101 || isotazboot == 102) { # TAZBOOT.LST
499 sub(/\[si/,"[di"); sub(/si,/,"di,"); sub(/si$/,"di")
500 sub(/DGROUP:_imgs\+38$/,"[di+38-32]")
501 sub(/DGROUP:_imgs\+40$/,"[di+40-32]")
502 if (/isofd/) isotazboot=102
503 if (/push/ && isotazboot == 102) next
504 if (/pop/ && isotazboot == 102) next
505 if (/load_initrd/) isotazboot=101
506 if (/isokernel/) isotazboot=103
507 }
508 if (isotazboot > 102) { # TAZBOOT.LST
509 if (/push/ || /pop/) next
510 sub(/\[si/,"[bx")
511 sub(/si,/,"bx,")
512 if (/static void bootiso/) isotazboot=0
513 }
514 if (wascall) {
515 if (rcall != "") {
516 if (/,ax$/) print " mov " rcall ",ax"
517 else print " xchg ax," rcall
518 wascall=0
519 }
520 else if (/^ mov .i,ax$/) {
521 split($2,y,",")
522 rcall=y[1]
523 next
524 }
525 else wascall=0
526 }
527 if (/^ call /) { wascall=1; rcall="" }
528 if (hold == 0) {
529 s=$0
530 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
531 r=$2; kept=0
532 hold=1; split($2,regs,","); next
533 }
534 if (/^ inc e?.[ixhl]/ || /^ dec e?.[ixhl]/) {
535 hold=2; r=$2; next
536 }
537 if (/^ mov [abcds][ix],/ && ! /,.s/) {
538 hold=3; split($2,regs,","); next
539 }
540 if (/^ movzx eax,ax$/) { hold=4; next }
541 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
542 split($0,regs,",")
543 if (isnum(regs[2]) && regs[2] != 0 &&
544 (regs[2] % 256) == 0) {
545 hold=5; next
546 }
547 }
548 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
549 if (/^ mov cl,4$/) { hold=7; next }
550 if (/^ cmp word ptr DGROUP:.*,0$/) {
551 hold=8; split($2,regs,","); next
552 }
553 if (/^ cbw/) { hold=11; kept=0; next }
554 if (/^ add [abcds][ix],2$/) {
555 split($2,regs,","); hold=12; next
556 }
557 if (/^ sub [abcds][ix],2$/) {
558 split($2,regs,","); hold=13; next
559 }
560 if (/^ push dx$/) {
561 hold=14; next;
562 }
563 }
564 else if (hold == 1) {
565 if (/^ ;/) { line[kept++]=$0; next }
566 hold=0; split($2,args,","); op=""
567 if ($1 == "add") op="+"
568 if ($1 == "sub") op="-"
569 if ($1 == "inc") { op="+"; args[2]="1"; }
570 if ($1 == "dec") { op="-"; args[2]="1"; }
571 if (op != "" && regs[1] == args[1]) {
572 if (isnum(args[2])) {
573 for (i = kept++; i > 0; i--) line[i] = line[i-1]
574 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
575 sub(/\+-/,"-",line[0])
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 }