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

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