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

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