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

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