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

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