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

linld: typos (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Dec 01 12:25:32 2018 +0100 (2018-12-01)
parents 857bcb9a456d
children 828bdd9ad908
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
22 next
23 }
24 }
25 if (/^ mov cl,4$/) { hold=8; next }
26 }
27 else if (hold == 1) {
28 if (/^ ;/) { line[kept++]=$0; next }
29 hold=0; split($2,args,","); op=""
30 if ($1 == "add") op="+"
31 if ($1 == "sub") op="-"
32 if (op != "" && regs[1] == args[1]) {
33 if (isnum(args[2])) {
34 print "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
35 for (i = 0; i < kept; i++) print line[i]; kept=0
36 next
37 }
38 line[kept++]=$0
39 hold=1
40 next
41 }
42 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
43 print " xchg " r
44 }
45 else print s
46 for (i = 0; i < kept; i++) print line[i]; kept=0
47 }
48 else if (hold == 2) {
49 hold=0; split($2,args,","); print s
50 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
51 }
52 else if (hold == 3) {
53 hold=0
54 if (/^ add [abcds][ix],/) {
55 split($2,regs2,",")
56 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
57 t=$0; sub(/mov/,$1,s)
58 if ($1 == "add") sub(/add/,"mov",t); else sub(/sub/,"mov",t)
59 print t; print s; next
60 }
61 }
62 print s
63 }
64 else if (hold == 4) {
65 hold=0
66 if (/^ push eax$/) {
67 print " push 0"; print " push ax"; next
68 } else { print s }
69 }
70 else if (hold == 5) {
71 hold=0
72 if ($1 == "jae" || $1 == "jb") {
73 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
74 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
75 s = s "/256"
76 }
77 print s
78 }
79 else if (hold == 8) {
80 hold=0
81 if (/^ call near ptr N_LXURSH@$/) {
82 print " extrn N_LXURSH@4:near"
83 print " call near ptr N_LXURSH@4"
84 next
85 }
86 if (/^ call near ptr N_LXLSH@$/) {
87 print " extrn N_LXLSH@4:near"
88 print " call near ptr N_LXLSH@4"
89 next
90 }
91 print s
92 }
93 s=$0
94 # These optimisation may break ZF or CF
95 if (/^ sub sp,2$/) { print " push ax"; next }
96 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
97 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
98 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
99 sub(/mov/,"and",s); print s; next # slower
100 }
101 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
102 sub(/mov/,"or",s); print s; next # slower
103 }
104 if (/^ or .*,0$/ || /^ and .*,-1$/) next
105 if (/^ or [abcd]x,/) {
106 split($2,args,",")
107 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
108 print " or " substr(args[1],1,1) "l," args[2]; next
109 }
110 }
111 if (/^ and [abcd]x,/) {
112 split($2,args,",")
113 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
114 print " and " substr(args[1],1,1) "l," args[2]; next
115 }
116 }
117 if (/^ or e[abcd]x,/) {
118 split($2,args,",")
119 if (args[2] == "large") { args[2] = $3 }
120 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
121 print " or " substr(args[1],2,1) "l," args[2]; next
122 }
123 }
124 if (/^ and e[abcd]x,/) {
125 split($2,args,",")
126 if (args[2] == "large") { args[2] = $3 }
127 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
128 print " and " substr(args[1],2,1) "l," args[2]; next
129 }
130 }
131 if (/^ or e[abcds][ix],/) {
132 split($2,args,",")
133 if (args[2] == "large") { args[2] = $3 }
134 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
135 print " or " substr(args[1],2) "," args[2]; next
136 }
137 }
138 if (/^ and e[abcds][ix],/) {
139 split($2,args,",")
140 if (args[2] == "large") { args[2] = $3 }
141 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
142 print " and " substr(args[1],2) "," args[2]; next
143 }
144 }
145 if (/^ add word ptr/ || /^ sub word ptr/ ||
146 /^ add [bcd]x,/ || /^ sub [bcd]x,/) {
147 split($0,args,",")
148 if (isnum(args[2]) && (args[2] % 256 == 0)) {
149 sub(/word ptr/,"byte ptr",s); sub(/x,/,"h,",s) ||
150 sub(/\],/,"+1],",s) || sub(/,/,"+1,",s)
151 print s "/256"; next
152 }
153 }
154 if (/^ add dword ptr/ || /^ sub dword ptr/ ||
155 /^ add e[abcd]x,/ || /^ sub e[abcd]x,/) {
156 split($0,args,",")
157 if (args[2] == "large") { args[2] = $3 }
158 if (isnum(args[2])) {
159 if (/dword ptr/ && args[2] % 16777216 == 0) {
160 sub(/dword/,"byte",s);
161 sub(/\],/,"+3],",s) || sub(/,/,"+3,",s)
162 print s "/16777216"; next
163 }
164 if (args[2] % 65536 == 0) {
165 sub(/dword/,"word",s); sub(/ e/,"",s)
166 sub(/\],/,"+2],",s) || sub(/,/,"+2,",s)
167 print s "/65536"; next
168 }
169 }
170 }
171 if (/^ mov e.x,/) {
172 split($2,args,",")
173 r=args[1]
174 if (args[2] == "large") { args[2] = $3 }
175 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
176 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
177 print " xor " r "," r
178 if (args[2] == 0) next
179 x=" mov " substr(r,2,1)
180 if (args[2] % 256 == 0) {
181 print x "h," args[2] "/256"
182 }
183 else { print x "l," args[2] }
184 next
185 }
186 }
187 }
188 print
189 }