297
|
1 /* Micro-C Switch Code generation Part */
|
|
2 /* $Id$ */
|
|
3
|
|
4 #define EXTERN extern
|
|
5 #include "mc.h"
|
|
6 #include "mc-code.h"
|
|
7 #include "mc-codegen.h"
|
|
8
|
|
9 #if CASE_CODE
|
|
10
|
|
11 extern void genswitch(int cslist,int cslabel,int dlabel);
|
|
12
|
|
13 /*
|
|
14 value label pair
|
|
15 cslist = list3(value,next,label) label==0 means skip
|
|
16 continuous cslist
|
|
17 chunks: list4(cslist,next,delta,list3(count,min,max))
|
|
18 mergeable chunks count list
|
|
19 merge: list4(continuous number of chunk,next,delta,case_count);
|
|
20 index list
|
|
21 index: list4(label,next,max,min);
|
|
22 */
|
|
23
|
|
24 #define chunk_max(chunks) caddr(cadddr(chunks))
|
|
25 #define chunk_min(chunks) cadr(cadddr(chunks))
|
|
26 #define index_max(index) caddr(index)
|
|
27 #define index_min(index) cadddr(index)
|
298
|
28 #define index_label(index) car(index)
|
|
29
|
|
30 #define CASE_TABLE_COUNT 3
|
|
31 #define CASE_INDEX_COUNT 3
|
|
32
|
297
|
33
|
|
34 /*
|
|
35 group continous case value
|
|
36 */
|
|
37
|
|
38 static int
|
|
39 make_chunk(int cslist)
|
|
40 {
|
|
41 int delta,delta1,list,p,count;
|
|
42 // cslist is already sorted
|
|
43 delta = 0;
|
|
44 list = list4(cslist,0,1,list3(0,car(cslist) /* min */,0));
|
|
45 count=1;
|
|
46 for(p=0;cadr(cslist);cslist = cadr(cslist),count++) {
|
|
47 // compute new delta
|
|
48 delta1 = car(cadr(cslist))-car(cslist);
|
|
49 // empty case
|
|
50 if (!caddr(cslist)) count--;
|
|
51 if (delta==0) {
|
|
52 // 2nd element
|
|
53 caddr(list) = delta = delta1;
|
|
54 if (p) cadr(p)=0; // terminate previous chunk
|
|
55 } else if (delta1!=delta) {
|
|
56 // not contiguous, start new list
|
|
57 caddr(cadddr(list)) = car(cslist); // max
|
|
58 car(cadddr(list)) = count; count=0;
|
|
59 delta = 0;
|
|
60 list = list4(cadr(cslist),list,1,list3(0,0,0));
|
|
61 cadr(cadddr(list)) = car(cadr(cslist)); // min
|
|
62 // prepare terminate point for next turn
|
|
63 p=cslist;
|
|
64 }
|
|
65 }
|
|
66 if (p) cadr(p)=0; // terminate previous chunk
|
|
67 car(cadddr(list)) = count;
|
|
68 caddr(cadddr(list)) = car(cslist); // max
|
|
69 return list;
|
|
70 }
|
|
71
|
|
72 #define CASE_MERGE_RATE 70
|
|
73
|
|
74 static int gcd(int i,int j)
|
|
75 {
|
|
76 int k;
|
|
77 if (i<j) { k=i; i=j; j=k;}
|
|
78 for(;;) {
|
|
79 if ((k=i%j)==0) return j;
|
|
80 i = j; j = k;
|
|
81 }
|
|
82 }
|
|
83
|
|
84 /*
|
|
85 check two chunks are mergeable or not.
|
|
86 */
|
|
87
|
|
88 static int
|
|
89 merge_chunk_p(int *delta,int max,int min,int *count,int delta1,int count1)
|
|
90 {
|
|
91 int range = max-min;
|
|
92 int g;
|
|
93 // compute possible merge delta
|
|
94 g = gcd(*delta,delta1);
|
|
95 g = gcd(g,range);
|
|
96 range /= g;
|
|
97 *count = count1 = count1+*count;
|
|
98 *delta = g;
|
|
99 #if 1
|
|
100 if (count1*128>(range*128*CASE_MERGE_RATE/100)) {
|
|
101 printf("# min %d, max %d, count %d, delta %d, rate %g t=%d\n",
|
|
102 min,max,count1,g,
|
|
103 ((double)count1)*100.0/range,
|
|
104 count1*128>(range*128*CASE_MERGE_RATE/100)
|
|
105 );
|
|
106 }
|
|
107 #endif
|
|
108 // count/((max-min)/delta) > 0.8
|
|
109 return count1*128>(range*128*CASE_MERGE_RATE/100);
|
|
110 }
|
|
111
|
|
112
|
|
113 static int
|
|
114 merge_chunk(int chunks)
|
|
115 {
|
|
116 int list = chunks;
|
|
117 int tail,last,max,min,i;
|
|
118 int count,c_count,p,widest,delta=1;
|
|
119 int merge = 0;
|
|
120 while(cadr(list)) {
|
|
121 p = cadddr(list);
|
|
122 c_count = count = car(p); max=caddr(p);
|
|
123 widest = 1; last = list;
|
|
124 delta = caddr(list);
|
|
125 // check possible merge against the end of the chunks
|
|
126 for(i=1,tail=cadr(list);cadr(tail); tail=cadr(tail),i++) {
|
|
127 p = cadddr(tail);
|
|
128 min = cadr(p);
|
|
129 if (merge_chunk_p(&delta,max,min,&count,caddr(tail),car(p))) {
|
|
130 // It is mergeable.
|
|
131 widest = i+1; last = tail; c_count=count;
|
|
132 }
|
|
133 }
|
|
134 merge = list4(widest,merge,delta,c_count);
|
|
135 // skip merged chunks
|
|
136 list = cadr(last);
|
|
137 }
|
|
138 // last one can remain.
|
|
139 if (list) merge = list4(1,merge,caddr(list),car(cadddr(list)));
|
|
140 return merge;
|
|
141 }
|
|
142
|
|
143 static int
|
|
144 table_jump(int count,int delta,int cslist)
|
|
145 {
|
|
146 int list;
|
|
147 for(;count-->0;cslist=cadr(cslist)) {
|
|
148 list = car(cslist);
|
|
149 printf("# table cases delta=%d count=%d min=%d max=%d\n",
|
|
150 caddr(cslist),car(cadddr(cslist)),
|
|
151 cadr(cadddr(cslist)),caddr(cadddr(cslist))
|
|
152 );
|
|
153 for(;list; list=cadr(list)) {
|
|
154 if (caddr(list))
|
|
155 cmpdimm(car(list),csvalue1,caddr(list),0);
|
|
156 }
|
|
157 }
|
|
158 return cslist;
|
|
159 }
|
|
160
|
|
161 static int
|
|
162 cascade_compare(int count,int cslist)
|
|
163 {
|
|
164 int list;
|
|
165 for(;count-->0;cslist=cadr(cslist)) {
|
|
166 list = car(cslist);
|
|
167 printf("# cascade cases delta=%d count=%d min=%d max=%d\n",
|
|
168 caddr(cslist),car(cadddr(cslist)),
|
|
169 cadr(cadddr(cslist)),caddr(cadddr(cslist))
|
|
170 );
|
|
171 for(;list; list=cadr(list)) {
|
|
172 if (caddr(list))
|
|
173 cmpdimm(car(list),csvalue1,caddr(list),0);
|
|
174 // value register label mode
|
|
175 }
|
|
176 }
|
|
177 return cslist;
|
|
178 }
|
|
179
|
|
180
|
|
181 /*
|
|
182 generate index jmp
|
|
183 */
|
|
184
|
|
185 static void
|
|
186 switch_index_jmp(int index,int label)
|
|
187 {
|
298
|
188 int value = index_min(index);
|
297
|
189 cmpdimm(value,csvalue1,label,LT);
|
|
190 // value register label mode
|
|
191 }
|
|
192
|
|
193 /*
|
|
194 generate leaf index branch
|
|
195 */
|
|
196
|
|
197 static int
|
298
|
198 switch_make_index_leaf(int count,int index,int level)
|
297
|
199 {
|
298
|
200 control=1;
|
|
201 printf("# index leaf count=%d level=%d\n",count,level);
|
297
|
202 for(;count-- !=0 && cadr(index);index=cadr(index)) {
|
298
|
203 switch_index_jmp( cadr(index), index_label(index) );
|
297
|
204 }
|
|
205 if (!cadr(index)) {
|
|
206 // don't check max boundary
|
298
|
207 jmp(index_label(index));
|
297
|
208 return 0;
|
|
209 }
|
298
|
210 if (count!= -1) error(-1);
|
|
211 if (control&&dlabel) jmp(dlabel);
|
297
|
212 return index;
|
|
213 }
|
|
214
|
|
215 /*
|
|
216 generate index switch branch (no table).
|
|
217 if necessary generate higher index.
|
|
218 */
|
|
219
|
|
220 static void
|
298
|
221 switch_make_index(int index0,int count,int cslabel,int level)
|
297
|
222 {
|
|
223 int index=0;
|
|
224 int icount=0;
|
|
225 int l,min,max;
|
298
|
226 control=1;
|
297
|
227 while (index0 && count > CASE_INDEX_COUNT) {
|
|
228 l = backdef();
|
|
229 min = index_min(index0);
|
298
|
230 index0=switch_make_index_leaf(CASE_INDEX_COUNT,index0,level);
|
|
231 max = index_min(index0)-1;
|
297
|
232 index = list4(l,index,max,min);
|
|
233 count-=CASE_INDEX_COUNT;
|
|
234 icount++;
|
|
235 }
|
|
236 if (index) {
|
298
|
237 switch_make_index(reverse0(index),icount,cslabel,level+1);
|
297
|
238 } else {
|
|
239 fwddef(cslabel);
|
298
|
240 switch_make_index_leaf(-1,index0,level);
|
297
|
241 }
|
298
|
242 if (control&&dlabel) jmp(dlabel);
|
297
|
243 }
|
|
244
|
|
245 /*
|
|
246 generate leaf switch branch or table jump
|
|
247 */
|
|
248
|
|
249 static int
|
|
250 switch_leaf(int count,int merge,int cslist)
|
|
251 {
|
298
|
252 control=1;
|
297
|
253 for(;count-- !=0 && merge;merge=cadr(merge)) {
|
|
254 printf("# merge count %d delta %d c_count %d\n",
|
|
255 car(merge),caddr(merge),cadddr(merge));
|
|
256 if (cadddr(merge)>CASE_TABLE_COUNT)
|
|
257 cslist = table_jump(car(merge),caddr(merge),cslist);
|
|
258 else
|
|
259 cslist = cascade_compare(car(merge),cslist);
|
|
260 }
|
298
|
261 if (control&&dlabel) jmp(dlabel);
|
297
|
262 return cslist;
|
|
263 }
|
|
264
|
|
265 /*
|
|
266 make index if necessary or
|
|
267 generate case branch or table jump
|
|
268 */
|
|
269
|
|
270 static void
|
|
271 switch_index(int merge,int chunks,int cslabel,int dlabel,int gmax)
|
|
272 {
|
|
273 int m,l,max,min;
|
|
274 int chnkcnt = 0;
|
|
275 int icount = 0;
|
|
276 int count = 0;
|
|
277 int index = 0;
|
|
278 for(m=merge;m;m=cadr(m)) {
|
298
|
279 if (!index && !cadr(m)) break; // no index for single chunk
|
297
|
280 chnkcnt++;
|
|
281 count += cadddr(m);
|
|
282 if (count > CASE_INDEX_COUNT || (index && !cadr(m))) {
|
|
283 icount++;
|
|
284 min = car(car(chunks));
|
|
285 l = backdef();
|
|
286 chunks= switch_leaf(chnkcnt,merge,chunks);
|
|
287 max = chunks?car(car(chunks)):gmax;
|
|
288 index = list4(l,index,max,min);
|
|
289 merge = cadr(m); count = 0; chnkcnt = 0;
|
|
290 }
|
|
291 }
|
|
292 if (index) {
|
|
293 index = reverse0(index);
|
|
294 // check lower bound
|
298
|
295 // switch_index_jmp(index,dlabel?dlabel:blabel);
|
|
296 // nonsense because no control here.
|
|
297 switch_make_index(index,icount,cslabel,0);
|
297
|
298 } else {
|
|
299 fwddef(cslabel);
|
|
300 switch_leaf(-1,merge,chunks);
|
|
301 }
|
298
|
302 if (control&&dlabel) jmp(dlabel);
|
297
|
303 }
|
|
304
|
|
305 /* generate switch table, index, cascading branch */
|
|
306
|
|
307
|
|
308 void
|
|
309 genswitch(int cslist,int cslabel,int dlabel)
|
|
310 {
|
|
311 int chunks,merge,gmax;
|
|
312 int i,j;
|
298
|
313 control=1;
|
297
|
314 #if 0
|
|
315 for(i=cslist;i;i=cadr(i)) {
|
|
316 printf("# case %d L_%d\n",car(i),caddr(i));
|
|
317 }
|
|
318 #endif
|
|
319 /* find stepwise chunks */
|
|
320 chunks=make_chunk(cslist);
|
|
321 #if 1
|
298
|
322 j = 0;
|
297
|
323 for(i=cslist;i;i=cadr(i)) {
|
298
|
324 j++;
|
297
|
325 }
|
|
326 #endif
|
|
327 /* possible merge of loose stepwise chunks */
|
|
328 merge = merge_chunk(chunks);
|
|
329 #if 0
|
|
330 // chunks: list3(widest,next,delta);
|
|
331 printf("# chunks %d = sum ",j);
|
|
332 j = 0;
|
|
333 for(i=merge;i;i=cadr(i)) {
|
|
334 printf(" %d/%d",car(i),caddr(i));
|
|
335 j+=car(i);
|
|
336 }
|
|
337 printf(" sum = %d\n",j);
|
|
338 #endif
|
|
339 /* make index branch or table jump */
|
|
340 gmax = chunk_max(chunks);
|
|
341 chunks = reverse0(chunks);
|
|
342 switch_index(merge,chunks,cslabel,dlabel,gmax);
|
|
343 }
|
|
344
|
|
345 #endif
|
|
346
|
|
347
|
|
348
|
|
349 /* end */
|