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

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