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

Up firefox-official (64.0), again
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Jan 03 12:04:23 2019 +0100 (2019-01-03)
parents 014e68d6a0ef
children ab907169f156
line source
1 BEGIN { hold=0 }
2 function isnum(n) { return match(n,/^[0-9+-]/) }
3 {
4 if (hold == 0) {
5 s=$0
6 if (/^ mov .[ix],bx$/ || /^ mov .[ix],.i$/) {
7 r=$2; kept=0
8 hold=1; split($2,regs,","); next
9 }
10 if (/^ inc e?.[ix]/ || /^ dec e?.[ix]/) {
11 hold=2; r=$2; next
12 }
13 if (/^ mov [abcds][ix],/ && ! /,.s/) {
14 hold=3; split($2,regs,","); next
15 }
16 if (/^ movzx eax,ax$/) { hold=4; next }
17 if (/^ cmp word ptr/ || /^ cmp [bcd]x,/) {
18 split($0,regs,",")
19 if (isnum(regs[2]) && regs[2] != 0 &&
20 (regs[2] % 256) == 0) {
21 hold=5; next
22 }
23 }
24 if (/^ mov ax,cs$/) { hold=6; kept=0; next }
25 if (/^ mov cl,4$/) { hold=7; next }
26 if (/^ cmp word ptr DGROUP:.*,0$/) {
27 hold=8; split($2,regs,","); next
28 }
29 }
30 else if (hold == 1) {
31 if (/^ ;/) { line[kept++]=$0; next }
32 hold=0; split($2,args,","); op=""
33 if ($1 == "add") op="+"
34 if ($1 == "sub") op="-"
35 if (op != "" && regs[1] == args[1]) {
36 if (isnum(args[2])) {
37 for (i = kept++; i > 0; i--) line[i] = line[i-1]
38 line[0] = "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
39 hold=10; next
40 }
41 line[kept++]=$0
42 hold=1
43 next
44 }
45 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
46 print " xchg " r
47 }
48 else print s
49 for (i = 0; i < kept; i++) print line[i]; kept=0
50 }
51 else if (hold == 2) {
52 hold=0; split($2,args,","); print s
53 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
54 }
55 else if (hold == 3) {
56 hold=0
57 if (/^ add [abcds][ix],/) {
58 split($2,regs2,",")
59 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
60 t=$0; sub(/mov/,$1,s); sub(/add/,"mov",t)
61 print t; print s; next
62 }
63 }
64 print s
65 }
66 else if (hold == 4) {
67 hold=0
68 if (/^ push eax$/) {
69 print " push 0"; print " push ax"; next
70 } else { print s }
71 }
72 else if (hold == 5) {
73 hold=0
74 if ($1 == "jae" || $1 == "jb") {
75 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
76 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
77 s = s "/256"
78 }
79 print s
80 }
81 else if (hold == 6) {
82 if (($1 == "and" || $1 == "add") && $2 ~ /^ax,/) {
83 line[kept++]=$0
84 next
85 }
86 p=$0
87 if (/^ movzx eax,ax$/) {
88 s=" mov eax,cs"; p=""
89 }
90 print s
91 for (i = 0; i < kept; i++) print line[i]; kept=0
92 if (p != "") print p
93 hold=0; next
94 }
95 else if (hold == 7) {
96 hold=0
97 if (/^ call near ptr N_LXURSH@$/) {
98 print " extrn N_LXURSH@4:near"
99 print " call near ptr N_LXURSH@4"
100 next
101 }
102 if (/^ call near ptr N_LXLSH@$/) {
103 print " extrn N_LXLSH@4:near"
104 print " call near ptr N_LXLSH@4"
105 next
106 }
107 print s
108 }
109 else if (hold == 8) {
110 if ($1 == "je" || $1 == "jne") { p=$0; hold=9; next }
111 hold=0
112 print s
113 }
114 else if (hold == 9) {
115 hold=0; split($2,args,",")
116 if (/^ mov ax,/ && args[2] == regs[1]) {
117 print; print " or ax,ax"; print p; next
118 }
119 print s; print p;
120 }
121 else if (hold == 10) {
122 hold=0
123 if (/^ mov [sd]i,ax$/) {
124 split($2,args,",")
125 for (i = 0; i < kept; i++) {
126 sub(/ax/,args[1],line[i]); print line[i]
127 }
128 next
129 }
130 for (i = 0; i < kept; i++) print line[i]
131 }
132 s=$0
133 # These optimisation may break ZF or CF
134 if (/^ sub sp,2$/) { print " push ax"; next }
135 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
136 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
137 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
138 sub(/mov/,"and",s); print s; next # slower
139 }
140 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
141 sub(/mov/,"or",s); print s; next # slower
142 }
143 if (/^ or .*,0$/ || /^ and .*,-1$/) next
144 if (/^ or [abcd]x,/) {
145 split($2,args,",")
146 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
147 print " or " substr(args[1],1,1) "l," args[2]; next
148 }
149 }
150 if (/^ and [abcd]x,/) {
151 split($2,args,",")
152 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
153 print " and " substr(args[1],1,1) "l," args[2]; next
154 }
155 }
156 if (/^ or e[abcd]x,/) {
157 split($2,args,",")
158 if (args[2] == "large") { args[2] = $3 }
159 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
160 print " or " substr(args[1],2,1) "l," args[2]; next
161 }
162 }
163 if (/^ and e[abcd]x,/) {
164 split($2,args,",")
165 if (args[2] == "large") { args[2] = $3 }
166 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
167 print " and " substr(args[1],2,1) "l," args[2]; next
168 }
169 }
170 if (/^ or e[abcds][ix],/) {
171 split($2,args,",")
172 if (args[2] == "large") { args[2] = $3 }
173 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
174 print " or " substr(args[1],2) "," args[2]; next
175 }
176 }
177 if (/^ and e[abcds][ix],/) {
178 split($2,args,",")
179 if (args[2] == "large") { args[2] = $3 }
180 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
181 print " and " substr(args[1],2) "," args[2]; next
182 }
183 }
184 if (/^ add word ptr/ || /^ sub word ptr/ ||
185 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
186 split($0,args,",")
187 if (isnum(args[2]) && (args[2] % 256 == 0)) {
188 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
189 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
190 print s "/256"; next
191 }
192 }
193 if (/^ add dword ptr/ || /^ sub dword ptr/) {
194 split($0,args,",")
195 if (args[2] == "large") { args[2] = $3 }
196 if (isnum(args[2])) {
197 if (args[2] % 16777216 == 0) {
198 sub(/dword/,"byte",s)
199 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
200 print s "/16777216"; next
201 }
202 if (args[2] % 65536 == 0) {
203 sub(/dword/,"word",s)
204 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
205 print s "/65536"; next
206 }
207 }
208 }
209 if (/^ mov e.x,/) {
210 split($2,args,",")
211 r=args[1]
212 if (args[2] == "large") { args[2] = $3 }
213 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
214 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
215 print " xor " r "," r
216 if (args[2] == 0) next
217 x=" mov " substr(r,2,1)
218 if (args[2] % 256 == 0) {
219 print x "h," args[2] "/256"
220 }
221 else { print x "l," args[2] }
222 next
223 }
224 }
225 }
226 print
227 }