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

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