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

linld: pascal convention calls
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Nov 09 16:37:55 2018 +0100 (2018-11-09)
parents d7919052f62f
children af959ebf1305
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 if (/^ call near ptr N_LXLSH@$/) {
63 print " extrn N_LXLSH@4:near"
64 print " call near ptr N_LXLSH@4"
65 next
66 }
67 print s
68 }
69 s=$0
70 # These optimisation may break ZF or CF
71 if (/^ sub sp,2$/) { print " push ax"; next }
72 if (/^ sub sp,4$/) { print " push ax"; print " push ax"; next }
73 if (/^ add sp,4$/) { print " pop cx"; print " pop cx"; next }
74 if (/^ mov d*word ptr .*,0$/ || /^ mov dword ptr .*,large 0$/) {
75 sub(/mov/,"and",s); print s; next # slower
76 }
77 if (/^ mov d*word ptr .*,-1$/ || /^ mov dword ptr .*,large -1$/) {
78 sub(/mov/,"or",s); print s; next # slower
79 }
80 if (/^ or .*,0$/ || /^ and .*,-1$/) next
81 if (/^ or [abcd]x,/) {
82 split($2,args,",")
83 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
84 print " or " substr(args[1],1,1) "l," args[2]; next
85 }
86 }
87 if (/^ and [abcd]x,/) {
88 split($2,args,",")
89 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
90 print " and " substr(args[1],1,1) "l," args[2]; next
91 }
92 }
93 if (/^ or e[abcd]x,/) {
94 split($2,args,",")
95 if (args[2] == "large") { args[2] = $3 }
96 if (isnum(args[2]) && args[2] >= 0 && args[2] < 256) {
97 print " or " substr(args[1],2,1) "l," args[2]; next
98 }
99 }
100 if (/^ and e[abcd]x,/) {
101 split($2,args,",")
102 if (args[2] == "large") { args[2] = $3 }
103 if (isnum(args[2]) && args[2] >= -256 && args[2] < 0) {
104 print " and " substr(args[1],2,1) "l," args[2]; next
105 }
106 }
107 if (/^ or e[abcds][ix],/) {
108 split($2,args,",")
109 if (args[2] == "large") { args[2] = $3 }
110 if (isnum(args[2]) && args[2] >= 0 && args[2] < 65536) {
111 print " or " substr(args[1],2) "," args[2]; next
112 }
113 }
114 if (/^ and e[abcds][ix],/) {
115 split($2,args,",")
116 if (args[2] == "large") { args[2] = $3 }
117 if (isnum(args[2]) && args[2] >= -65536 && args[2] < 0) {
118 print " and " substr(args[1],2) "," args[2]; next
119 }
120 }
121 if (/^ add word ptr/ || /^ sub word ptr/) {
122 split($0,args,",")
123 if (isnum(args[2]) && (args[2] % 256 == 0)) {
124 sub(/word/,"byte",s); sub(/\]/,"+1]",s)
125 print s "/256"; next
126 }
127 }
128 if (/^ add dword ptr/ || /^ sub dword ptr/) {
129 split($0,args,",")
130 if (args[2] == "large") { split(args[2],args," ") }
131 if (isnum(args[2])) {
132 if (args[2] % 16777216 == 0) {
133 sub(/dword/,"byte",s); sub(/\]/,"+3]",s)
134 print s "/16777216"; next
135 }
136 if (args[2] % 65536 == 0) {
137 sub(/dword/,"word",s); sub(/\]/,"+2]",s)
138 print s "/65536"; next
139 }
140 }
141 }
142 if (/^ mov e.x,/) {
143 split($2,args,",")
144 r=args[1]
145 if (args[2] == "large") { args[2] = $3 }
146 if (isnum(args[2]) && args[2] % 65536 == args[2]) {
147 if (args[2] % 256 == args[2] || args[2] % 256 == 0) {
148 print " xor " r "," r
149 if (args[2] == 0) next
150 x=" mov " substr(r,2,1)
151 if (args[2] % 256 == 0) {
152 print x "h," args[2] "/256"
153 }
154 else { print x "l," args[2] }
155 next
156 }
157 }
158 }
159 print
160 }