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

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