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

Up sshguard (2.2.0)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Nov 05 11:52:27 2018 +0100 (2018-11-05)
parents f5088c165f51
children f0d71e920c5a
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 r=$2
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 (/^ mov cl,4$/) { hold=8; next }
18 }
19 else if (hold == 1) {
20 if (/^ ;/) { print; next }
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 if (/^ pop [ds]i/ && regs[2] ~ /^[ds]i$/) {
29 print " xchg " r
30 }
31 else print "\tmov\t" regs[1] "," regs[2]
32 }
33 else if (hold == 2) {
34 hold=0; split($2,args,","); print s
35 if ($1 == "or" && r == args[1] && r == args[2]) next # don't clear C ...
36 }
37 else if (hold == 3) {
38 hold=0
39 if (/^ add [abcds][ix],/ || /^ sub [abcds][ix],/) {
40 split($2,regs2,",")
41 if (regs[1] == regs2[1] && (regs2[2] == "offset" || isnum(regs2[2]))) {
42 t=$0; sub(/mov/,$1,s)
43 if ($1 == "add") sub(/add/,"mov",t); else sub(/sub/,"mov",t)
44 print t; print s; next
45 }
46 }
47 print s
48 }
49 else if (hold == 4) {
50 hold=0
51 if (/^ push eax$/) {
52 print " push 0"; print " push ax"; next
53 } else { print s }
54 }
55 else if (hold == 8) {
56 hold=0
57 if (/^ call near ptr N_LXURSH@$/) {
58 print " extrn N_LXURSH@4:near"
59 print " call near ptr N_LXURSH@4"
60 next
61 }
62 print s
63 }
64 s=$0
65 # These optimisation may break ZF or CF
66 if (/^ sub sp,2$/) { print " push ax"; next }
67 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
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 }