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

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