wok-6.x view linld/stuff/src/pipehole.awk @ rev 21569

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