wok-current view linld/stuff/src/pipehole.awk @ rev 23989

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