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

tazboot: shrink (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Jun 15 22:12:18 2019 +0200 (2019-06-15)
parents 74a62a08433a
children 8b9f768b711e
line source
1 BEGIN { hold=0; is386=0; isload=0; isiso=0; istazboot=0; wascall=0 }
2 function isnum(n) { return match(n,/^[0-9+-]/) }
3 {
4 sub(/segment word public/,"segment byte public")
6 if (/^@.*:$/ || / endp$/) afterjmp=0
7 if (/dword ptr/) is386=1
8 if (/vid_mode = vid_mode/) isload=2
9 if (isload == 2) { # LOAD.LST
10 sub(/,0/,""); sub(/cmp /,"mov cx,")
11 sub(/je/,"jcxz")
12 if (/ax,word/) next
13 sub(/,ax/,",cx")
14 if (/version_string/) isload=0
15 }
16 if (/heap_top = _rm_buf/) isload=1
17 if (isload == 1) { # LOAD.LST
18 if (/mov al,byte ptr/ && is386) {
19 print " movzx eax,byte ptr [si]"
20 next
21 }
22 if (/ax,word ptr/) next
23 if (/^ call/) isload=0
24 }
25 if (/x->curdirsize == 0xFFFF/) isiso=4
26 if (isiso == 4) { # ISO9660.LST
27 sub(/DGROUP:_isostate\+14/,"[si+14]")
28 sub(/DGROUP:_isostate\+16/,"[si+16]")
29 if (/goto restarted/) isiso=0
30 }
31 if (/c = \*s; \*s = 0;/) isiso=3
32 if (isiso == 3) { # ISO9660.LST, TAZBOOT.LST
33 if (/al,byte ptr/) {
34 print " mov al,0"
35 sub(/mov/,"xchg")
36 }
37 if (/byte ptr \[.*\],0/) {
38 isiso=0
39 next
40 }
41 }
42 if (/endname = NULL/) isiso=2
43 if (isiso == 2) { # ISO9660.LST
44 if (/mov bx,cx/) next
45 gsub(/cx/,"bx")
46 sub(/DGROUP:_isostate\+31/,"[si+31]")
47 }
48 if (/const char \*n = name/) isiso=1
49 if (isiso == 1) { # ISO9660.LST
50 if ((/mov word ptr \[si\+32\],ax/ ) ||
51 (/mov ax,word ptr \[si\+28\]/) ||
52 (/bx,word ptr \[si\+32\]/) || (/ax,dx/)) next
53 if (/dx,/) sub(/dx/,"ax")
54 if ((/sub ax,word ptr \[si\+28\]/) ||
55 (/\[si\+12\]/) || (/ax,di/)) sub(/ax/,"bx")
56 if (/add word ptr \[si\+32\],ax/) $0=" add bx,word ptr [si+12]"
57 if (/al,/ || /,al/) sub(/al/,"cl")
58 if (/cmp byte ptr \[si\+30\],0/) $0=" or cl,cl"
59 if (/jne @@0$/) next
60 if (/jmp @3@58$/) $0=" je @3@58"
61 }
62 if (/isoopen\(s\+7\) != -1/) isotazboot=8
63 if (isotazboot == 8) { # TAZBOOT.LST
64 if (/ax,si/) next
65 sub(/ax,ax/,"si,si")
66 if (/magic/) isotazboot=0
67 }
68 if (/\+\+isknoppix/) isotazboot=7
69 if (isotazboot == 7) { # TAZBOOT.LST
70 if (/al,byte/) sub (/al,byte ptr DGROUP:/,"bx,offset ")
71 if (/inc/) sub (/al/,"word ptr [bx]")
72 if (/,al/) next
73 if (/isokernel/) isotazboot=0
74 }
75 if (/if \(c\) s\+\+;/) isotazboot=6
76 if (isotazboot == 6) { # TAZBOOT.LST
77 if (/cmp/) {
78 $0=" cmp al,0"
79 isotazboot=0
80 }
81 }
82 if (/initrd_state.info\[m->state\]/) isotazboot=5
83 if (isotazboot == 5) { # TAZBOOT.LST
84 if (/cx,ax/) $0=" xchg ax,bx"
85 if (/mov ax,word ptr \[si\]/) $0=" lodsw"
86 if (/ax,word ptr \[si\+28\]/) next
87 if (/bx,cx/) next
88 if (/endp/) isotazboot=0
89 }
90 if (/0x7FF0/) isotazboot=4
91 if (isotazboot == 4) { # TAZBOOT.LST
92 if (/ax,word ptr/) {
93 print " mov ax,32752"
94 sub(/mov/,"sub")
95 }
96 if (/bx,/ || /cx,/ || /dx,/) next
97 sub(/,bx/,",0")
98 sub(/,cx/,",ax")
99 if (/short/) isotazboot=0
100 }
101 if (/c = x->filename/) isotazboot=3
102 if (isotazboot == 3) { # TAZBOOT.LST
103 if (/ax,/) $0=" xchg ax,bx"
104 if (/\]$/) next
105 if (/@strcpy\$qpxzct1/) isotazboot=0
106 }
107 if (/memtop/) isotazboot=2
108 if (isotazboot == 2) { # TAZBOOT.LST
109 if (/DGROUP:_base_himem\+2,dx/) print " mov bx,offset _base_himem"
110 sub(/DGROUP:_base_himem,/,"[bx],")
111 sub(/DGROUP:_base_himem\+2,/,"[bx+2],")
112 sub(/DGROUP:_base_himem\+3,/,"[bx+3],")
113 if (/word ptr \[bx\+2\],0/) {
114 print s
115 hold=0
116 print " mov bx,word ptr [bx+2]"
117 $0=" or bx,bx"
118 }
119 if (/\[bp-4\],ax/) sub(/ax/,"bx")
120 if (/ax,word ptr \[bx\+2\]/ || /bx,ax/) next
121 if (/@strcmp\$qpxzct1/) isotazboot=0
122 }
123 if (/static void addinitrd/) isotazboot=100
124 if (isotazboot == 100) { # TAZBOOT.LST
125 if (/cx,ax/) {
126 print " xchg ax,si"
127 print " push di"
128 print " mov di,offset _isostate+4"
129 print " movsw"
130 print " movsw"
131 print " movsw"
132 print " movsw"
133 $0=" pop di"
134 }
135 if (/mov/ && !/si/ && !/cl/) next
136 if (/initrd.size \+=/) isotazboot=101
137 }
138 if (isotazboot > 100) { # TAZBOOT.LST
139 if (/m->next_chunk = next_chunk/) isotazboot=101
140 if (/load_initrd/) isotazboot=102
141 if (/push si/ && isotazboot == 102) next
142 if (/pop si/ && isotazboot == 102) next
143 sub(/\[si\]/,"[bx]")
144 sub(/push si$/,"push bx")
145 sub(/si,/,"bx,")
146 sub(/si\+/,"bx+")
147 if (/mov cx,ax/) $0=" xchg ax,bx"
148 if (/bx,cx/) next
149 sub(/cx/,"bx")
150 sub(/DGROUP:_imgs\+38$/,"[bx+38-32]")
151 sub(/DGROUP:_imgs\+40$/,"[bx+40-32]")
152 if (/static void bootiso/) isotazboot=0
153 }
154 if (wascall) {
155 if (rcall != "") {
156 if (/,ax$/) print " mov " rcall ",ax"
157 else print " xchg ax," rcall
158 wascall=0
159 }
160 else if (/^ mov .i,ax$/) {
161 split($2,y,",")
162 rcall=y[1]
163 next
164 }
165 else wascall=0
166 }
167 if (/^ call /) { wascall=1; rcall="" }
168 if (hold == 0) {
169 s=$0
170 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
171 r=$2; kept=0
172 hold=1; split($2,regs,","); next
173 }
174 if (/^ inc e?.[ixhl]/ || /^ dec e?.[ixhl]/) {
175 hold=2; r=$2; next
176 }
177 if (/^ mov [abcds][ix],/ && ! /,.s/) {
178 hold=3; split($2,regs,","); next
179 }
180 if (/^ movzx eax,ax$/) { hold=4; next }
181 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
182 split($0,regs,",")
183 if (isnum(regs[2]) && regs[2] != 0 &&
184 (regs[2] % 256) == 0) {
185 hold=5; next
186 }
187 }
188 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
189 if (/^ mov cl,4$/) { hold=7; next }
190 if (/^ cmp word ptr DGROUP:.*,0$/) {
191 hold=8; split($2,regs,","); next
192 }
193 if (/^ cbw/) { hold=11; kept=0; next }
194 if (/^ add [abcds][ix],2$/) {
195 split($2,regs,","); hold=12; next
196 }
197 if (/^ sub [abcds][ix],2$/) {
198 split($2,regs,","); hold=13; next
199 }
200 if (/^ push dx$/) {
201 hold=14; next;
202 }
203 }
204 else if (hold == 1) {
205 if (/^ ;/) { line[kept++]=$0; next }
206 hold=0; split($2,args,","); op=""
207 if ($1 == "add") op="+"
208 if ($1 == "sub") op="-"
209 if ($1 == "inc") { op="+"; args[2]="1"; }
210 if ($1 == "dec") { op="-"; args[2]="1"; }
211 if (op != "" && regs[1] == args[1]) {
212 if (isnum(args[2])) {
213 for (i = kept++; i > 0; i--) line[i] = line[i-1]
214 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
215 hold=10; next
216 }
217 line[kept++]=$0
218 hold=1
219 next
220 }
221 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
222 print " xchg " r
223 }
224 else print s
225 for (i = 0; i < kept; i++) print line[i]; kept=0
226 }
227 else if (hold == 2) {
228 split($0,args,",")
229 if (/^ mov / && r == args[2]) { print s; s=$0; next }
230 split($2,args,",")
231 hold=0; print s
232 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
233 }
234 else if (hold == 3) {
235 hold=0
236 if (/^ call / && regs[2] == "ax") s=" xchg ax," regs[1]
237 if (/^ add [abcds][ix],/) {
238 split($2,regs2,",")
239 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
240 t=$0; sub(/mov/,$1,s); sub(/add/,"mov",t)
241 print t; print s; next
242 }
243 }
244 print s
245 }
246 else if (hold == 4) {
247 hold=0
248 if (/^ push eax$/) {
249 print " push 0"; print " push ax"; next
250 } else { print s }
251 }
252 else if (hold == 5) {
253 hold=0
254 if ($1 == "jae" || $1 == "jb") {
255 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
256 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
257 s = s "/256"
258 }
259 print s
260 }
261 else if (hold == 6) {
262 if (($1 == "and" || $1 == "add") && $2 ~ /^ax,/) {
263 line[kept++]=$0
264 next
265 }
266 p=$0
267 if (/^ movzx eax,ax$/) {
268 s=" mov eax,cs"; p=""
269 }
270 print s
271 for (i = 0; i < kept; i++) print line[i]; kept=0
272 if (p != "") print p
273 hold=0; next
274 }
275 else if (hold == 7) {
276 hold=0
277 if (/^ call near ptr N_LXURSH@$/) {
278 print " extrn N_LXURSH@4:near"
279 print " call near ptr N_LXURSH@4"
280 next
281 }
282 if (/^ call near ptr N_LXLSH@$/) {
283 print " extrn N_LXLSH@4:near"
284 print " call near ptr N_LXLSH@4"
285 next
286 }
287 print s
288 }
289 else if (hold == 8) {
290 if ($1 == "je" || $1 == "jne") { p=$0; hold=9; next }
291 hold=0
292 print s
293 }
294 else if (hold == 9) {
295 hold=0; split($2,args,",")
296 if (/^ mov ax,/ && args[2] == regs[1]) {
297 print; print " or ax,ax"; print p; next
298 }
299 print s; print p;
300 }
301 else if (hold == 10) {
302 split($2,args,","); op=""
303 if ($1 == "add") op="+"
304 if ($1 == "sub") op="-"
305 if ($1 == "inc") { op="+"; args[2]="1"; }
306 if ($1 == "dec") { op="-"; args[2]="1"; }
307 if (op != "" && isnum(args[2])) {
308 split(line[0],reg,",")
309 if (substr(reg[1],length(reg[1])-1,2) == args[1]) {
310 line[0] = substr(line[0],1,length(line[0])-1) op args[2] "]"
311 next
312 }
313 }
314 hold=0
315 if (/^ mov [sd]i,ax$/) {
316 split($2,args,",")
317 for (i = 0; i < kept; i++) {
318 sub(/ax/,args[1],line[i]); print line[i]
319 }
320 next
321 }
322 for (i = 0; i < kept; i++) print line[i]
323 }
324 else if (hold == 11) {
325 if (/^ inc ax$/ || /^ dec ax$/) {
326 line[kept++]=$0; next
327 }
328 split($2,args,",")
329 if (/^ mov cl,/) {
330 split($2,args,",")
331 if (args[2] >= 8) {
332 line[kept++]=$0; next
333 }
334 }
335 if (!/^ shl ax,/ || (args[2] != "cl" && args[2] < 8)) {
336 print " cbw "
337 }
338 for (i = 0; i < kept; i++) print line[i]
339 hold=kept=0
340 }
341 else if (hold == 12) {
342 hold=0
343 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
344 print " inc " regs[1]
345 print " inc " regs[1]
346 }
347 else print " add " regs[1] ",2"
348 }
349 else if (hold == 13) {
350 hold=0
351 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
352 print " dec " regs[1]
353 print " dec " regs[1]
354 }
355 else print " sub " regs[1] ",2"
356 }
357 else if (hold == 14) {
358 if (/^ push ax$/) { hold++; next; }
359 print " push dx";
360 hold=0;
361 }
362 else if (hold == 15) {
363 if (/^ pop eax$/) { hold++; next; }
364 print " push dx";
365 print " push ax";
366 hold=0;
367 }
368 else if (hold == 16) {
369 hold=0;
370 if (/^ shr eax,16$/) { print " xchg ax,dx"; next; }
371 print " push dx";
372 print " push ax";
373 print " pop eax";
374 }
375 else if (hold == 17) {
376 hold=0;
377 if (/^ cmp ax,-1$/) { print " inc ax"; next; }
378 }
379 if (/^ call near ptr @fileexist\$/ || # return boolean :
380 /^ call near ptr @isoreaddir\$/ || # 0=true, -1=false
381 /^ call near ptr @isoreset\$/ ||
382 /^ call near ptr @isoopen\$/ ||
383 /^ call near ptr @isoreadsector\$/ ||
384 /^ call near ptr @strhead\$/ ||
385 /^ call near ptr @argstr\$/ ||
386 /^ call near ptr @argnum\$/) { print; hold=17; next; }
387 s=$0
388 # These optimisation may break ZF or CF
389 if (/^ sub sp,2$/) { print " push ax"; next }
390 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
391 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
392 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
393 sub(/mov/,"and",s); print s; next # slower
394 }
395 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
396 sub(/mov/,"or",s); print s; next # slower
397 }
398 if (/^ or .*,0$/ || /^ and .*,-1$/) next
399 if (/^ or [abcd]x,/) {
400 split($2,args,",")
401 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
402 print " or " substr(args[1],1,1) "l," args[2]; next
403 }
404 }
405 if (/^ and [abcd]x,/) {
406 split($2,args,",")
407 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
408 print " and " substr(args[1],1,1) "l," args[2]; next
409 }
410 }
411 if (/^ or e[abcd]x,/) {
412 split($2,args,",")
413 if (args[2] == "large") { args[2] = $3 }
414 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
415 print " or " substr(args[1],2,1) "l," args[2]; next
416 }
417 }
418 if (/^ and e[abcd]x,/) {
419 split($2,args,",")
420 if (args[2] == "large") { args[2] = $3 }
421 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
422 print " and " substr(args[1],2,1) "l," args[2]; next
423 }
424 }
425 if (/^ or e[abcds][ix],/) {
426 split($2,args,",")
427 if (args[2] == "large") { args[2] = $3 }
428 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
429 print " or " substr(args[1],2) "," args[2]; next
430 }
431 }
432 if (/^ and e[abcds][ix],/) {
433 split($2,args,",")
434 if (args[2] == "large") { args[2] = $3 }
435 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
436 print " and " substr(args[1],2) "," args[2]; next
437 }
438 }
439 if (/^ add word ptr/ || /^ sub word ptr/ ||
440 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
441 split($0,args,",")
442 if (isnum(args[2]) && (args[2] % 256 == 0)) {
443 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
444 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
445 print s "/256"; next
446 }
447 }
448 if (/^ add dword ptr/ || /^ sub dword ptr/) {
449 split($0,args,",")
450 if (args[2] == "large") { args[2] = $3 }
451 if (isnum(args[2])) {
452 if (args[2] % 16777216 == 0) {
453 sub(/dword/,"byte",s)
454 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
455 print s "/16777216"; next
456 }
457 if (args[2] % 65536 == 0) {
458 sub(/dword/,"word",s)
459 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
460 print s "/65536"; next
461 }
462 }
463 }
464 if (/^ mov e.x,/) {
465 split($2,args,",")
466 r=args[1]
467 if (args[2] == "large") { args[2] = $3 }
468 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
469 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
470 print " xor " r "," r
471 if (args[2] == 0) next
472 x=" mov " substr(r,2,1)
473 if (args[2] % 256 == 0) {
474 print x "h," args[2] "/256"
475 }
476 else { print x "l," args[2] }
477 next
478 }
479 }
480 }
481 if (afterjmp) print ";" $0
482 else print
483 if (/^ jmp /) afterjmp=1
484 }