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

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