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