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

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