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

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