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

linld: fix memcpy32 (16 low mem case)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Oct 23 08:25:49 2018 +0200 (2018-10-23)
parents c2946aeb8b57
children d7919052f62f
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 .x,bx$/ || /^ mov .x,.i$/) {
7 hold=1; split($2,regs,","); next
8 }
9 if (/^ inc e?.[ix]/ || /^ dec e?.[ix]/) {
10 hold=2; r=$2; next
11 }
12 if (/^ mov [abcds][ix],/ && ! /,.s/) {
13 hold=3; split($2,regs,","); next
14 }
15 if (/^ movzx eax,ax$/) { hold=4; next }
16 if (0) {
17 if (/^ cmp dx,-1$/) { hold=10; next }
18 }
19 }
20 else if (hold == 1) {
21 hold=0; split($2,args,","); op=""
22 if ($1 == "add") op="+"
23 if ($1 == "sub") op="-"
24 if (op != "" && regs[1] == args[1] && isnum(args[2])) {
25 print "\tlea\t" regs[1] ",[" regs[2] op args[2] "]"
26 next
27 }
28 print "\tmov\t" regs[1] "," regs[2]
29 }
30 else if (hold == 2) {
31 hold=0; split($2,args,","); print s
32 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
33 }
34 else if (hold == 3) {
35 hold=0
36 if (/^ add [abcds][ix],/ || /^ sub [abcds][ix],/) {
37 split($2,regs2,",")
38 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
39 t=$0; sub(/mov/,$1,s)
40 if ($1 == "add") sub(/add/,"mov",t); else sub(/sub/,"mov",t)
41 print t; print s; next
42 }
43 }
44 print s
45 }
46 else if (hold == 4) {
47 hold=0
48 if (/^ push eax$/) {
49 print " push 0"; print " push ax"; next
50 } else { print s }
51 }
52 else if (hold == 10) {
53 if ($1 == "je" || $1 == "jne") { s2=$0; cmp=$1; hold++; next }
54 hold=0; print s
55 }
56 else if (hold == 11) {
57 if (/^ cmp ax,-1$/) { s3=$0; hold++; next }
58 hold=0; print s; print s2
59 }
60 else if (hold == 12) {
61 if (($1 == "je" || $1 == "jne") && $1 != cmp) {
62 print " and ax,dx"; print " inc ax"
63 } else { print s; print s2; print s3 }
64 hold=0
65 }
66 s=$0
67 # These optimisation may break ZF or CF
68 if (/^ sub sp,2$/) { print " push ax"; next }
69 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
70 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
71 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
72 sub(/mov/,"and",s); print s; next # slower
73 }
74 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
75 sub(/mov/,"or",s); print s; next # slower
76 }
77 if (/^ or .*,0$/ || /^ and .*,-1$/) next
78 if (/^ or [abcd]x,/) {
79 split($2,args,",")
80 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
81 print " or " substr(args[1],1,1) "l," args[2]; next
82 }
83 }
84 if (/^ and [abcd]x,/) {
85 split($2,args,",")
86 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
87 print " and " substr(args[1],1,1) "l," args[2]; next
88 }
89 }
90 if (/^ or e[abcd]x,/) {
91 split($2,args,",")
92 if (args[2] == "large") { args[2] = $3 }
93 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
94 print " or " substr(args[1],2,1) "l," args[2]; next
95 }
96 }
97 if (/^ and e[abcd]x,/) {
98 split($2,args,",")
99 if (args[2] == "large") { args[2] = $3 }
100 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
101 print " and " substr(args[1],2,1) "l," args[2]; next
102 }
103 }
104 if (/^ or e[abcds][ix],/) {
105 split($2,args,",")
106 if (args[2] == "large") { args[2] = $3 }
107 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
108 print " or " substr(args[1],2) "," args[2]; next
109 }
110 }
111 if (/^ and e[abcds][ix],/) {
112 split($2,args,",")
113 if (args[2] == "large") { args[2] = $3 }
114 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
115 print " and " substr(args[1],2) "," args[2]; next
116 }
117 }
118 if (/^ add word ptr/ || /^ sub word ptr/) {
119 split($0,args,",")
120 if (isnum(args[2]) && (args[2] % 256 == 0)) {
121 sub(/word/,"byte",s); sub(/\]/,"+1]",s)
122 print s "/256"; next
123 }
124 }
125 if (/^ add dword ptr/ || /^ sub dword ptr/) {
126 split($0,args,",")
127 if (args[2] == "large") { split(args[2],args," ") }
128 if (isnum(args[2])) {
129 if (args[2] % 16777216 == 0) {
130 sub(/dword/,"byte",s); sub(/\]/,"+3]",s)
131 print s "/16777216"; next
132 }
133 if (args[2] % 65536 == 0) {
134 sub(/dword/,"word",s); sub(/\]/,"+2]",s)
135 print s "/65536"; next
136 }
137 }
138 }
139 if (/^ mov e.x,/) {
140 split($2,args,",")
141 r=args[1]
142 if (args[2] == "large") { args[2] = $3 }
143 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
144 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
145 print " xor " r "," r
146 if (args[2] == 0) next
147 x=" mov " substr(r,2,1)
148 if (args[2] % 256 == 0) {
149 print x "h," args[2] "/256"
150 }
151 else { print x "l," args[2] }
152 next
153 }
154 }
155 }
156 print
157 }