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

Update some websites
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun May 19 13:14:32 2019 +0200 (2019-05-19)
parents 87b6697bb350
children 0e811092e7bb
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 @argstr\$/ ||
218 /^ call near ptr @argnum\$/) { print; hold=17; next; }
219 s=$0
220 # These optimisation may break ZF or CF
221 if (/^ sub sp,2$/) { print " push ax"; next }
222 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
223 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
224 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
225 sub(/mov/,"and",s); print s; next # slower
226 }
227 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
228 sub(/mov/,"or",s); print s; next # slower
229 }
230 if (/^ or .*,0$/ || /^ and .*,-1$/) next
231 if (/^ or [abcd]x,/) {
232 split($2,args,",")
233 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
234 print " or " substr(args[1],1,1) "l," args[2]; next
235 }
236 }
237 if (/^ and [abcd]x,/) {
238 split($2,args,",")
239 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
240 print " and " substr(args[1],1,1) "l," args[2]; next
241 }
242 }
243 if (/^ or e[abcd]x,/) {
244 split($2,args,",")
245 if (args[2] == "large") { args[2] = $3 }
246 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
247 print " or " substr(args[1],2,1) "l," args[2]; next
248 }
249 }
250 if (/^ and e[abcd]x,/) {
251 split($2,args,",")
252 if (args[2] == "large") { args[2] = $3 }
253 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
254 print " and " substr(args[1],2,1) "l," args[2]; next
255 }
256 }
257 if (/^ or e[abcds][ix],/) {
258 split($2,args,",")
259 if (args[2] == "large") { args[2] = $3 }
260 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
261 print " or " substr(args[1],2) "," args[2]; next
262 }
263 }
264 if (/^ and e[abcds][ix],/) {
265 split($2,args,",")
266 if (args[2] == "large") { args[2] = $3 }
267 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
268 print " and " substr(args[1],2) "," args[2]; next
269 }
270 }
271 if (/^ add word ptr/ || /^ sub word ptr/ ||
272 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
273 split($0,args,",")
274 if (isnum(args[2]) && (args[2] % 256 == 0)) {
275 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
276 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
277 print s "/256"; next
278 }
279 }
280 if (/^ add dword ptr/ || /^ sub dword ptr/) {
281 split($0,args,",")
282 if (args[2] == "large") { args[2] = $3 }
283 if (isnum(args[2])) {
284 if (args[2] % 16777216 == 0) {
285 sub(/dword/,"byte",s)
286 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
287 print s "/16777216"; next
288 }
289 if (args[2] % 65536 == 0) {
290 sub(/dword/,"word",s)
291 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
292 print s "/65536"; next
293 }
294 }
295 }
296 if (/^ mov e.x,/) {
297 split($2,args,",")
298 r=args[1]
299 if (args[2] == "large") { args[2] = $3 }
300 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
301 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
302 print " xor " r "," r
303 if (args[2] == 0) next
304 x=" mov " substr(r,2,1)
305 if (args[2] % 256 == 0) {
306 print x "h," args[2] "/256"
307 }
308 else { print x "l," args[2] }
309 next
310 }
311 }
312 }
313 if (afterjmp) print ";" $0
314 else print
315 if (/^ jmp /) afterjmp=1
316 }