wok-current view linld/stuff/src/pipehole.awk @ rev 21628
linld: more ram for zImage (again)
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Mon May 27 23:23:13 2019 +0200 (2019-05-27) |
parents | 66530154b12f |
children | 861efbf7a5de |
line source
1 BEGIN { hold=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 (hold == 0) {
8 s=$0
9 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
10 r=$2; kept=0
11 hold=1; split($2,regs,","); next
12 }
13 if (/^ inc e?.[ix]/ || /^ dec e?.[ix]/) {
14 hold=2; r=$2; next
15 }
16 if (/^ mov [abcds][ix],/ && ! /,.s/) {
17 hold=3; split($2,regs,","); next
18 }
19 if (/^ movzx eax,ax$/) { hold=4; next }
20 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
21 split($0,regs,",")
22 if (isnum(regs[2]) && regs[2] != 0 &&
23 (regs[2] % 256) == 0) {
24 hold=5; next
25 }
26 }
27 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
28 if (/^ mov cl,4$/) { hold=7; next }
29 if (/^ cmp word ptr DGROUP:.*,0$/) {
30 hold=8; split($2,regs,","); next
31 }
32 if (/^ cbw/) { hold=11; kept=0; next }
33 if (/^ add [abcds][ix],2$/) {
34 split($2,regs,","); hold=12; next
35 }
36 if (/^ sub [abcds][ix],2$/) {
37 split($2,regs,","); hold=13; next
38 }
39 if (/^ push dx$/) {
40 hold=14; next;
41 }
42 }
43 else if (hold == 1) {
44 if (/^ ;/) { line[kept++]=$0; next }
45 hold=0; split($2,args,","); op=""
46 if ($1 == "add") op="+"
47 if ($1 == "sub") op="-"
48 if ($1 == "inc") { op="+"; args[2]="1"; }
49 if ($1 == "dec") { op="-"; args[2]="1"; }
50 if (op != "" && regs[1] == args[1]) {
51 if (isnum(args[2])) {
52 for (i = kept++; i > 0; i--) line[i] = line[i-1]
53 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
54 hold=10; next
55 }
56 line[kept++]=$0
57 hold=1
58 next
59 }
60 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
61 print " xchg " r
62 }
63 else print s
64 for (i = 0; i < kept; i++) print line[i]; kept=0
65 }
66 else if (hold == 2) {
67 hold=0; split($2,args,","); print s
68 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
69 }
70 else if (hold == 3) {
71 hold=0
72 if (/^ call / && regs[2] == "ax") s=" xchg ax," regs[1]
73 if (/^ add [abcds][ix],/) {
74 split($2,regs2,",")
75 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
76 t=$0; sub(/mov/,$1,s); sub(/add/,"mov",t)
77 print t; print s; next
78 }
79 }
80 print s
81 }
82 else if (hold == 4) {
83 hold=0
84 if (/^ push eax$/) {
85 print " push 0"; print " push ax"; next
86 } else { print s }
87 }
88 else if (hold == 5) {
89 hold=0
90 if ($1 == "jae" || $1 == "jb") {
91 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
92 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
93 s = s "/256"
94 }
95 print s
96 }
97 else if (hold == 6) {
98 if (($1 == "and" || $1 == "add") && $2 ~ /^ax,/) {
99 line[kept++]=$0
100 next
101 }
102 p=$0
103 if (/^ movzx eax,ax$/) {
104 s=" mov eax,cs"; p=""
105 }
106 print s
107 for (i = 0; i < kept; i++) print line[i]; kept=0
108 if (p != "") print p
109 hold=0; next
110 }
111 else if (hold == 7) {
112 hold=0
113 if (/^ call near ptr N_LXURSH@$/) {
114 print " extrn N_LXURSH@4:near"
115 print " call near ptr N_LXURSH@4"
116 next
117 }
118 if (/^ call near ptr N_LXLSH@$/) {
119 print " extrn N_LXLSH@4:near"
120 print " call near ptr N_LXLSH@4"
121 next
122 }
123 print s
124 }
125 else if (hold == 8) {
126 if ($1 == "je" || $1 == "jne") { p=$0; hold=9; next }
127 hold=0
128 print s
129 }
130 else if (hold == 9) {
131 hold=0; split($2,args,",")
132 if (/^ mov ax,/ && args[2] == regs[1]) {
133 print; print " or ax,ax"; print p; next
134 }
135 print s; print p;
136 }
137 else if (hold == 10) {
138 split($2,args,","); op=""
139 if ($1 == "add") op="+"
140 if ($1 == "sub") op="-"
141 if ($1 == "inc") { op="+"; args[2]="1"; }
142 if ($1 == "dec") { op="-"; args[2]="1"; }
143 if (op != "" && isnum(args[2])) {
144 split(line[0],reg,",")
145 if (substr(reg[1],length(reg[1])-1,2) == args[1]) {
146 line[0] = substr(line[0],1,length(line[0])-1) op args[2] "]"
147 next
148 }
149 }
150 hold=0
151 if (/^ mov [sd]i,ax$/) {
152 split($2,args,",")
153 for (i = 0; i < kept; i++) {
154 sub(/ax/,args[1],line[i]); print line[i]
155 }
156 next
157 }
158 for (i = 0; i < kept; i++) print line[i]
159 }
160 else if (hold == 11) {
161 if (/^ inc ax$/ || /^ dec ax$/) {
162 line[kept++]=$0; next
163 }
164 split($2,args,",")
165 if (/^ mov cl,/) {
166 split($2,args,",")
167 if (args[2] >= 8) {
168 line[kept++]=$0; next
169 }
170 }
171 if (!/^ shl ax,/ || (args[2] != "cl" && args[2] < 8)) {
172 print " cbw "
173 }
174 for (i = 0; i < kept; i++) print line[i]
175 hold=kept=0
176 }
177 else if (hold == 12) {
178 hold=0
179 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
180 print " inc " regs[1]
181 print " inc " regs[1]
182 }
183 else print " add " regs[1] ",2"
184 }
185 else if (hold == 13) {
186 hold=0
187 if ($1 != "adc" && $1 != "sbb" && ! /^ jn?[abc]/) {
188 print " dec " regs[1]
189 print " dec " regs[1]
190 }
191 else print " sub " regs[1] ",2"
192 }
193 else if (hold == 14) {
194 if (/^ push ax$/) { hold++; next; }
195 print " push dx";
196 hold=0;
197 }
198 else if (hold == 15) {
199 if (/^ pop eax$/) { hold++; next; }
200 print " push dx";
201 print " push ax";
202 hold=0;
203 }
204 else if (hold == 16) {
205 hold=0;
206 if (/^ shr eax,16$/) { print " xchg ax,dx"; next; }
207 print " push dx";
208 print " push ax";
209 print " pop eax";
210 }
211 else if (hold == 17) {
212 hold=0;
213 if (/^ cmp ax,-1$/) { print " inc ax"; next; }
214 }
215 if (/^ call near ptr @fileexist\$/ || # return boolean :
216 /^ call near ptr @isoreaddir\$/ || # 0=true, -1=false
217 /^ call near ptr @isoreset\$/ ||
218 /^ call near ptr @isoopen\$/ ||
219 /^ call near ptr @isoreadsector\$/ ||
220 /^ call near ptr @strhead\$/ ||
221 /^ call near ptr @argstr\$/ ||
222 /^ call near ptr @argnum\$/) { print; hold=17; next; }
223 s=$0
224 # These optimisation may break ZF or CF
225 if (/^ sub sp,2$/) { print " push ax"; next }
226 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
227 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
228 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
229 sub(/mov/,"and",s); print s; next # slower
230 }
231 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
232 sub(/mov/,"or",s); print s; next # slower
233 }
234 if (/^ or .*,0$/ || /^ and .*,-1$/) next
235 if (/^ or [abcd]x,/) {
236 split($2,args,",")
237 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
238 print " or " substr(args[1],1,1) "l," args[2]; next
239 }
240 }
241 if (/^ and [abcd]x,/) {
242 split($2,args,",")
243 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
244 print " and " substr(args[1],1,1) "l," args[2]; next
245 }
246 }
247 if (/^ or e[abcd]x,/) {
248 split($2,args,",")
249 if (args[2] == "large") { args[2] = $3 }
250 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
251 print " or " substr(args[1],2,1) "l," args[2]; next
252 }
253 }
254 if (/^ and e[abcd]x,/) {
255 split($2,args,",")
256 if (args[2] == "large") { args[2] = $3 }
257 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
258 print " and " substr(args[1],2,1) "l," args[2]; next
259 }
260 }
261 if (/^ or e[abcds][ix],/) {
262 split($2,args,",")
263 if (args[2] == "large") { args[2] = $3 }
264 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
265 print " or " substr(args[1],2) "," args[2]; next
266 }
267 }
268 if (/^ and e[abcds][ix],/) {
269 split($2,args,",")
270 if (args[2] == "large") { args[2] = $3 }
271 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
272 print " and " substr(args[1],2) "," args[2]; next
273 }
274 }
275 if (/^ add word ptr/ || /^ sub word ptr/ ||
276 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
277 split($0,args,",")
278 if (isnum(args[2]) && (args[2] % 256 == 0)) {
279 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
280 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
281 print s "/256"; next
282 }
283 }
284 if (/^ add dword ptr/ || /^ sub dword ptr/) {
285 split($0,args,",")
286 if (args[2] == "large") { args[2] = $3 }
287 if (isnum(args[2])) {
288 if (args[2] % 16777216 == 0) {
289 sub(/dword/,"byte",s)
290 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
291 print s "/16777216"; next
292 }
293 if (args[2] % 65536 == 0) {
294 sub(/dword/,"word",s)
295 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
296 print s "/65536"; next
297 }
298 }
299 }
300 if (/^ mov e.x,/) {
301 split($2,args,",")
302 r=args[1]
303 if (args[2] == "large") { args[2] = $3 }
304 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
305 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
306 print " xor " r "," r
307 if (args[2] == 0) next
308 x=" mov " substr(r,2,1)
309 if (args[2] % 256 == 0) {
310 print x "h," args[2] "/256"
311 }
312 else { print x "l," args[2] }
313 next
314 }
315 }
316 }
317 if (afterjmp) print ";" $0
318 else print
319 if (/^ jmp /) afterjmp=1
320 }