wok view linld/stuff/src/pipehole.awk @ rev 21751
tazboot: shrink (again)
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sat Jun 15 18:53:05 2019 +0200 (2019-06-15) |
parents | c19368210c2b |
children | 30270fe0d1b3 |
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 (/ax,word ptr \[bx\+2\]/ || /\[bp-4\],ax/) sub(/ax/,"bx")
114 if (/bx,ax/) next
115 if (/@strcmp\$qpxzct1/) isotazboot=0
116 }
117 if (/static void addinitrd/) isotazboot=100
118 if (isotazboot == 100) { # TAZBOOT.LST
119 if (/cx,ax/) {
120 print " xchg ax,si"
121 print " push di"
122 print " mov di,offset _isostate+4"
123 print " movsw"
124 print " movsw"
125 print " movsw"
126 print " movsw"
127 $0=" pop di"
128 }
129 if (/mov/ && !/si/ && !/cl/) next
130 if (/initrd.size \+=/) isotazboot=101
131 }
132 if (isotazboot > 100) { # TAZBOOT.LST
133 if (/m->next_chunk = next_chunk/) isotazboot=101
134 if (/load_initrd/) isotazboot=102
135 if (/push si/ && isotazboot == 102) next
136 if (/pop si/ && isotazboot == 102) next
137 sub(/\[si\]/,"[bx]")
138 sub(/push si$/,"push bx")
139 sub(/si,/,"bx,")
140 sub(/si\+/,"bx+")
141 if (/mov cx,ax/) $0=" xchg ax,bx"
142 if (/bx,cx/) next
143 sub(/cx/,"bx")
144 sub(/DGROUP:_imgs\+38$/,"[bx+38-32]")
145 sub(/DGROUP:_imgs\+40$/,"[bx+40-32]")
146 if (/static void bootiso/) isotazboot=0
147 }
148 if (wascall) {
149 if (rcall != "") {
150 if (/,ax$/) print " mov " rcall ",ax"
151 else print " xchg ax," rcall
152 wascall=0
153 }
154 else if (/^ mov .i,ax$/) {
155 split($2,y,",")
156 rcall=y[1]
157 next
158 }
159 else wascall=0
160 }
161 if (/^ call /) { wascall=1; rcall="" }
162 if (hold == 0) {
163 s=$0
164 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
165 r=$2; kept=0
166 hold=1; split($2,regs,","); next
167 }
168 if (/^ inc e?.[ixhl]/ || /^ dec e?.[ixhl]/) {
169 hold=2; r=$2; next
170 }
171 if (/^ mov [abcds][ix],/ && ! /,.s/) {
172 hold=3; split($2,regs,","); next
173 }
174 if (/^ movzx eax,ax$/) { hold=4; next }
175 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
176 split($0,regs,",")
177 if (isnum(regs[2]) && regs[2] != 0 &&
178 (regs[2] % 256) == 0) {
179 hold=5; next
180 }
181 }
182 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
183 if (/^ mov cl,4$/) { hold=7; next }
184 if (/^ cmp word ptr DGROUP:.*,0$/) {
185 hold=8; split($2,regs,","); next
186 }
187 if (/^ cbw/) { hold=11; kept=0; next }
188 if (/^ add [abcds][ix],2$/) {
189 split($2,regs,","); hold=12; next
190 }
191 if (/^ sub [abcds][ix],2$/) {
192 split($2,regs,","); hold=13; next
193 }
194 if (/^ push dx$/) {
195 hold=14; next;
196 }
197 }
198 else if (hold == 1) {
199 if (/^ ;/) { line[kept++]=$0; next }
200 hold=0; split($2,args,","); op=""
201 if ($1 == "add") op="+"
202 if ($1 == "sub") op="-"
203 if ($1 == "inc") { op="+"; args[2]="1"; }
204 if ($1 == "dec") { op="-"; args[2]="1"; }
205 if (op != "" && regs[1] == args[1]) {
206 if (isnum(args[2])) {
207 for (i = kept++; i > 0; i--) line[i] = line[i-1]
208 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
209 hold=10; next
210 }
211 line[kept++]=$0
212 hold=1
213 next
214 }
215 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
216 print " xchg " r
217 }
218 else print s
219 for (i = 0; i < kept; i++) print line[i]; kept=0
220 }
221 else if (hold == 2) {
222 split($0,args,",")
223 if (/^ mov / && r == args[2]) { print s; s=$0; next }
224 split($2,args,",")
225 hold=0; print s
226 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
227 }
228 else if (hold == 3) {
229 hold=0
230 if (/^ call / && regs[2] == "ax") s=" xchg ax," regs[1]
231 if (/^ add [abcds][ix],/) {
232 split($2,regs2,",")
233 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
234 t=$0; sub(/mov/,$1,s); sub(/add/,"mov",t)
235 print t; print s; next
236 }
237 }
238 print s
239 }
240 else if (hold == 4) {
241 hold=0
242 if (/^ push eax$/) {
243 print " push 0"; print " push ax"; next
244 } else { print s }
245 }
246 else if (hold == 5) {
247 hold=0
248 if ($1 == "jae" || $1 == "jb") {
249 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
250 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
251 s = s "/256"
252 }
253 print s
254 }
255 else if (hold == 6) {
256 if (($1 == "and" || $1 == "add") && $2 ~ /^ax,/) {
257 line[kept++]=$0
258 next
259 }
260 p=$0
261 if (/^ movzx eax,ax$/) {
262 s=" mov eax,cs"; p=""
263 }
264 print s
265 for (i = 0; i < kept; i++) print line[i]; kept=0
266 if (p != "") print p
267 hold=0; next
268 }
269 else if (hold == 7) {
270 hold=0
271 if (/^ call near ptr N_LXURSH@$/) {
272 print " extrn N_LXURSH@4:near"
273 print " call near ptr N_LXURSH@4"
274 next
275 }
276 if (/^ call near ptr N_LXLSH@$/) {
277 print " extrn N_LXLSH@4:near"
278 print " call near ptr N_LXLSH@4"
279 next
280 }
281 print s
282 }
283 else if (hold == 8) {
284 if ($1 == "je" || $1 == "jne") { p=$0; hold=9; next }
285 hold=0
286 print s
287 }
288 else if (hold == 9) {
289 hold=0; split($2,args,",")
290 if (/^ mov ax,/ && args[2] == regs[1]) {
291 print; print " or ax,ax"; print p; next
292 }
293 print s; print p;
294 }
295 else if (hold == 10) {
296 split($2,args,","); op=""
297 if ($1 == "add") op="+"
298 if ($1 == "sub") op="-"
299 if ($1 == "inc") { op="+"; args[2]="1"; }
300 if ($1 == "dec") { op="-"; args[2]="1"; }
301 if (op != "" && isnum(args[2])) {
302 split(line[0],reg,",")
303 if (substr(reg[1],length(reg[1])-1,2) == args[1]) {
304 line[0] = substr(line[0],1,length(line[0])-1) op args[2] "]"
305 next
306 }
307 }
308 hold=0
309 if (/^ mov [sd]i,ax$/) {
310 split($2,args,",")
311 for (i = 0; i < kept; i++) {
312 sub(/ax/,args[1],line[i]); print line[i]
313 }
314 next
315 }
316 for (i = 0; i < kept; i++) print line[i]
317 }
318 else if (hold == 11) {
319 if (/^ inc ax$/ || /^ dec ax$/) {
320 line[kept++]=$0; next
321 }
322 split($2,args,",")
323 if (/^ mov cl,/) {
324 split($2,args,",")
325 if (args[2] >= 8) {
326 line[kept++]=$0; next
327 }
328 }
329 if (!/^ shl ax,/ || (args[2] != "cl" && args[2] < 8)) {
330 print " cbw "
331 }
332 for (i = 0; i < kept; i++) print line[i]
333 hold=kept=0
334 }
335 else if (hold == 12) {
336 hold=0
337 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
338 print " inc " regs[1]
339 print " inc " regs[1]
340 }
341 else print " add " regs[1] ",2"
342 }
343 else if (hold == 13) {
344 hold=0
345 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
346 print " dec " regs[1]
347 print " dec " regs[1]
348 }
349 else print " sub " regs[1] ",2"
350 }
351 else if (hold == 14) {
352 if (/^ push ax$/) { hold++; next; }
353 print " push dx";
354 hold=0;
355 }
356 else if (hold == 15) {
357 if (/^ pop eax$/) { hold++; next; }
358 print " push dx";
359 print " push ax";
360 hold=0;
361 }
362 else if (hold == 16) {
363 hold=0;
364 if (/^ shr eax,16$/) { print " xchg ax,dx"; next; }
365 print " push dx";
366 print " push ax";
367 print " pop eax";
368 }
369 else if (hold == 17) {
370 hold=0;
371 if (/^ cmp ax,-1$/) { print " inc ax"; next; }
372 }
373 if (/^ call near ptr @fileexist\$/ || # return boolean :
374 /^ call near ptr @isoreaddir\$/ || # 0=true, -1=false
375 /^ call near ptr @isoreset\$/ ||
376 /^ call near ptr @isoopen\$/ ||
377 /^ call near ptr @isoreadsector\$/ ||
378 /^ call near ptr @strhead\$/ ||
379 /^ call near ptr @argstr\$/ ||
380 /^ call near ptr @argnum\$/) { print; hold=17; next; }
381 s=$0
382 # These optimisation may break ZF or CF
383 if (/^ sub sp,2$/) { print " push ax"; next }
384 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
385 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
386 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
387 sub(/mov/,"and",s); print s; next # slower
388 }
389 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
390 sub(/mov/,"or",s); print s; next # slower
391 }
392 if (/^ or .*,0$/ || /^ and .*,-1$/) next
393 if (/^ or [abcd]x,/) {
394 split($2,args,",")
395 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
396 print " or " substr(args[1],1,1) "l," args[2]; next
397 }
398 }
399 if (/^ and [abcd]x,/) {
400 split($2,args,",")
401 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
402 print " and " substr(args[1],1,1) "l," args[2]; next
403 }
404 }
405 if (/^ or e[abcd]x,/) {
406 split($2,args,",")
407 if (args[2] == "large") { args[2] = $3 }
408 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
409 print " or " substr(args[1],2,1) "l," args[2]; next
410 }
411 }
412 if (/^ and e[abcd]x,/) {
413 split($2,args,",")
414 if (args[2] == "large") { args[2] = $3 }
415 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
416 print " and " substr(args[1],2,1) "l," args[2]; next
417 }
418 }
419 if (/^ or e[abcds][ix],/) {
420 split($2,args,",")
421 if (args[2] == "large") { args[2] = $3 }
422 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
423 print " or " substr(args[1],2) "," args[2]; next
424 }
425 }
426 if (/^ and e[abcds][ix],/) {
427 split($2,args,",")
428 if (args[2] == "large") { args[2] = $3 }
429 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
430 print " and " substr(args[1],2) "," args[2]; next
431 }
432 }
433 if (/^ add word ptr/ || /^ sub word ptr/ ||
434 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
435 split($0,args,",")
436 if (isnum(args[2]) && (args[2] % 256 == 0)) {
437 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
438 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
439 print s "/256"; next
440 }
441 }
442 if (/^ add dword ptr/ || /^ sub dword ptr/) {
443 split($0,args,",")
444 if (args[2] == "large") { args[2] = $3 }
445 if (isnum(args[2])) {
446 if (args[2] % 16777216 == 0) {
447 sub(/dword/,"byte",s)
448 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
449 print s "/16777216"; next
450 }
451 if (args[2] % 65536 == 0) {
452 sub(/dword/,"word",s)
453 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
454 print s "/65536"; next
455 }
456 }
457 }
458 if (/^ mov e.x,/) {
459 split($2,args,",")
460 r=args[1]
461 if (args[2] == "large") { args[2] = $3 }
462 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
463 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
464 print " xor " r "," r
465 if (args[2] == 0) next
466 x=" mov " substr(r,2,1)
467 if (args[2] % 256 == 0) {
468 print x "h," args[2] "/256"
469 }
470 else { print x "l," args[2] }
471 next
472 }
473 }
474 }
475 if (afterjmp) print ";" $0
476 else print
477 if (/^ jmp /) afterjmp=1
478 }