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

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