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

Add py3k7
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Oct 10 15:54:03 2018 +0200 (2018-10-10)
parents 77cbb661144f
children f5088c165f51
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 (/^ add sp,4/) { print " pop cx"; print " pop cx"; next }
69 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
70 sub(/mov/,"and",s); print s; next # slower
71 }
72 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
73 sub(/mov/,"or",s); print s; next # slower
74 }
75 if (/^ or .*,0$/ || /^ and .*,-1$/) next
76 if (/^ or [abcd]x,/) {
77 split($2,args,",")
78 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
79 print " or " substr(args[1],1,1) "l," args[2]; next
80 }
81 }
82 if (/^ and [abcd]x,/) {
83 split($2,args,",")
84 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
85 print " and " substr(args[1],1,1) "l," args[2]; next
86 }
87 }
88 if (/^ or e[abcd]x,/) {
89 split($2,args,",")
90 if (args[2] == "large") { args[2] = $3 }
91 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
92 print " or " substr(args[1],2,1) "l," args[2]; next
93 }
94 }
95 if (/^ and e[abcd]x,/) {
96 split($2,args,",")
97 if (args[2] == "large") { args[2] = $3 }
98 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
99 print " and " substr(args[1],2,1) "l," args[2]; next
100 }
101 }
102 if (/^ or e[abcds][ix],/) {
103 split($2,args,",")
104 if (args[2] == "large") { args[2] = $3 }
105 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
106 print " or " substr(args[1],2) "," args[2]; next
107 }
108 }
109 if (/^ and e[abcds][ix],/) {
110 split($2,args,",")
111 if (args[2] == "large") { args[2] = $3 }
112 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
113 print " and " substr(args[1],2) "," args[2]; next
114 }
115 }
116 if (/^ add word ptr/ || /^ sub word ptr/) {
117 split($0,args,",")
118 if (isnum(args[2]) && (args[2] % 256 == 0)) {
119 sub(/word/,"byte",s); sub(/\]/,"+1]",s)
120 print s "/256"; next
121 }
122 }
123 if (/^ add dword ptr/ || /^ sub dword ptr/) {
124 split($0,args,",")
125 if (args[2] == "large") { split(args[2],args," ") }
126 if (isnum(args[2])) {
127 if (args[2] % 16777216 == 0) {
128 sub(/dword/,"byte",s); sub(/\]/,"+3]",s)
129 print s "/16777216"; next
130 }
131 if (args[2] % 65536 == 0) {
132 sub(/dword/,"word",s); sub(/\]/,"+2]",s)
133 print s "/65536"; next
134 }
135 }
136 }
137 if (/^ mov e.x,/) {
138 split($2,args,",")
139 r=args[1]
140 if (args[2] == "large") { args[2] = $3 }
141 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
142 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
143 print " xor " r "," r
144 if (args[2] == 0) next
145 x=" mov " substr(r,2,1)
146 if (args[2] % 256 == 0) {
147 print x "h," args[2] "/256"
148 }
149 else { print x "l," args[2] }
150 next
151 }
152 }
153 }
154 print
155 }