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
|
299
|
11 extern void genswitch(int cslist,int cslabel);
|
297
|
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
|
300
|
30 #define CASE_TABLE_COUNT 10
|
302
|
31 #define CASE_INDEX_COUNT CASE_TABLE_COUNT // have to be equal
|
298
|
32
|
297
|
33
|
|
34 /*
|
|
35 group continous case value
|
|
36 */
|
|
37
|
|
38 static int
|
|
39 make_chunk(int cslist)
|
|
40 {
|
303
|
41 register int delta,delta1,list,p,count;
|
297
|
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
|
303
|
74 static int gcd(int i0,int j0)
|
297
|
75 {
|
303
|
76 register int k,i=i0,j=j0;
|
297
|
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;
|
311
|
99 #if 0
|
297
|
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
|
311
|
112 static void
|
|
113 free_chunks(chunks)
|
|
114 {
|
|
115 int p;
|
|
116 while(chunks) {
|
|
117 p = car(chunks);
|
|
118 chunks = cadr(chunks); // free_glist3 will destroy cadr
|
|
119 free_glist3(p);
|
|
120 }
|
|
121 }
|
297
|
122
|
|
123 static int
|
|
124 merge_chunk(int chunks)
|
|
125 {
|
303
|
126 register int list = chunks;
|
|
127 register int tail,last,max,min,i;
|
|
128 register int c_count,p,widest;
|
|
129 register int merge = 0;
|
|
130 int count,delta=1;
|
|
131
|
297
|
132 while(cadr(list)) {
|
|
133 p = cadddr(list);
|
|
134 c_count = count = car(p); max=caddr(p);
|
|
135 widest = 1; last = list;
|
|
136 delta = caddr(list);
|
|
137 // check possible merge against the end of the chunks
|
|
138 for(i=1,tail=cadr(list);cadr(tail); tail=cadr(tail),i++) {
|
|
139 p = cadddr(tail);
|
|
140 min = cadr(p);
|
|
141 if (merge_chunk_p(&delta,max,min,&count,caddr(tail),car(p))) {
|
|
142 // It is mergeable.
|
|
143 widest = i+1; last = tail; c_count=count;
|
|
144 }
|
|
145 }
|
|
146 merge = list4(widest,merge,delta,c_count);
|
|
147 // skip merged chunks
|
|
148 list = cadr(last);
|
|
149 }
|
|
150 // last one can remain.
|
|
151 if (list) merge = list4(1,merge,caddr(list),car(cadddr(list)));
|
|
152 return merge;
|
|
153 }
|
|
154
|
|
155 static int
|
|
156 cascade_compare(int count,int cslist)
|
|
157 {
|
|
158 int list;
|
|
159 for(;count-->0;cslist=cadr(cslist)) {
|
|
160 list = car(cslist);
|
|
161 printf("# cascade cases delta=%d count=%d min=%d max=%d\n",
|
|
162 caddr(cslist),car(cadddr(cslist)),
|
|
163 cadr(cadddr(cslist)),caddr(cadddr(cslist))
|
|
164 );
|
|
165 for(;list; list=cadr(list)) {
|
|
166 if (caddr(list))
|
|
167 cmpdimm(car(list),csvalue1,caddr(list),0);
|
|
168 // value register label mode
|
|
169 }
|
|
170 }
|
302
|
171 // have to be fall thru
|
297
|
172 return cslist;
|
|
173 }
|
|
174
|
300
|
175 static int
|
|
176 table_jump(int count,int delta,int chunks)
|
|
177 {
|
|
178 int list,i;
|
|
179 int l,max,min;
|
|
180 if (!code_table_jump_p())
|
|
181 return cascade_compare(count,chunks);
|
|
182
|
|
183 min=cadr(cadddr(chunks));
|
|
184 // find max
|
|
185 for(i=0,list=chunks;i<count;list=cadr(list),i++) {
|
|
186 max=caddr(cadddr(list));
|
|
187 }
|
|
188
|
|
189 printf("# table count %d delta %d max %d min %d\n",
|
|
190 count,delta,max,min);
|
|
191
|
|
192 l = fwdlabel();
|
302
|
193 code_table_jump(l,csvalue1,delta,max,min,dlabel);
|
|
194 control=0;
|
300
|
195 code_table_open(l);
|
|
196 i = min;
|
|
197 for(;count-->0;chunks=cadr(chunks)) {
|
|
198 list = car(chunks);
|
|
199 for(;list; list=cadr(list)) {
|
|
200 if (caddr(list)) {
|
|
201 for(;i<car(list);i+=delta) {
|
|
202 code_table_value(dlabel,l);
|
|
203 }
|
|
204 code_table_value(caddr(list),l);
|
|
205 i+=delta;
|
|
206 }
|
|
207 }
|
|
208 }
|
|
209 code_table_close();
|
|
210 return chunks;
|
|
211 }
|
|
212
|
297
|
213
|
|
214 /*
|
|
215 generate index jmp
|
|
216 */
|
|
217
|
|
218 static void
|
|
219 switch_index_jmp(int index,int label)
|
|
220 {
|
298
|
221 int value = index_min(index);
|
297
|
222 cmpdimm(value,csvalue1,label,LT);
|
|
223 // value register label mode
|
|
224 }
|
|
225
|
|
226 /*
|
|
227 generate leaf index branch
|
|
228 */
|
|
229
|
|
230 static int
|
298
|
231 switch_make_index_leaf(int count,int index,int level)
|
297
|
232 {
|
298
|
233 control=1;
|
|
234 printf("# index leaf count=%d level=%d\n",count,level);
|
297
|
235 for(;count-- !=0 && cadr(index);index=cadr(index)) {
|
298
|
236 switch_index_jmp( cadr(index), index_label(index) );
|
297
|
237 }
|
|
238 if (!cadr(index)) {
|
|
239 // don't check max boundary
|
298
|
240 jmp(index_label(index));
|
297
|
241 return 0;
|
|
242 }
|
298
|
243 if (count!= -1) error(-1);
|
302
|
244 jmp(dlabel);
|
297
|
245 return index;
|
|
246 }
|
|
247
|
|
248 /*
|
|
249 generate index switch branch (no table).
|
|
250 if necessary generate higher index.
|
|
251 */
|
|
252
|
|
253 static void
|
299
|
254 switch_make_index(int index0,int count,int cslabel,int gmax,int level)
|
297
|
255 {
|
|
256 int index=0;
|
|
257 int icount=0;
|
|
258 int l,min,max;
|
298
|
259 control=1;
|
299
|
260 if (count <= CASE_INDEX_COUNT) {
|
297
|
261 fwddef(cslabel);
|
298
|
262 switch_make_index_leaf(-1,index0,level);
|
299
|
263 } else {
|
|
264 while (index0) {
|
|
265 l = backdef();
|
|
266 min = index_min(index0);
|
|
267 index0=switch_make_index_leaf(CASE_INDEX_COUNT,index0,level);
|
|
268 if (index0) {
|
|
269 max = index_min(index0)-1;
|
|
270 } else {
|
|
271 max = gmax;
|
|
272 }
|
|
273 index = list4(l,index,max,min);
|
|
274 count-=CASE_INDEX_COUNT;
|
|
275 icount++;
|
|
276 }
|
|
277 switch_make_index(reverse0(index),icount,cslabel,gmax,level+1);
|
297
|
278 }
|
|
279 }
|
|
280
|
|
281 /*
|
|
282 generate leaf switch branch or table jump
|
|
283 */
|
|
284
|
|
285 static int
|
300
|
286 switch_leaf(int count,int merge,int chunks)
|
297
|
287 {
|
298
|
288 control=1;
|
297
|
289 for(;count-- !=0 && merge;merge=cadr(merge)) {
|
|
290 printf("# merge count %d delta %d c_count %d\n",
|
|
291 car(merge),caddr(merge),cadddr(merge));
|
302
|
292 if (cadddr(merge)>CASE_TABLE_COUNT) {
|
300
|
293 chunks = table_jump(car(merge),caddr(merge),chunks);
|
302
|
294 } else {
|
300
|
295 chunks = cascade_compare(car(merge),chunks);
|
302
|
296 }
|
297
|
297 }
|
302
|
298 // table_jump has no fall thru, but cascade_compare does.
|
|
299 if (control) jmp(dlabel);
|
300
|
300 return chunks;
|
297
|
301 }
|
|
302
|
|
303 /*
|
|
304 make index if necessary or
|
|
305 generate case branch or table jump
|
|
306 */
|
|
307
|
|
308 static void
|
299
|
309 switch_index(int merge,int chunks,int cslabel,int gmax)
|
297
|
310 {
|
|
311 int m,l,max,min;
|
|
312 int chnkcnt = 0;
|
|
313 int icount = 0;
|
|
314 int count = 0;
|
|
315 int index = 0;
|
|
316 for(m=merge;m;m=cadr(m)) {
|
298
|
317 if (!index && !cadr(m)) break; // no index for single chunk
|
297
|
318 chnkcnt++;
|
|
319 count += cadddr(m);
|
|
320 if (count > CASE_INDEX_COUNT || (index && !cadr(m))) {
|
|
321 icount++;
|
|
322 min = car(car(chunks));
|
|
323 l = backdef();
|
|
324 chunks= switch_leaf(chnkcnt,merge,chunks);
|
|
325 max = chunks?car(car(chunks)):gmax;
|
|
326 index = list4(l,index,max,min);
|
|
327 merge = cadr(m); count = 0; chnkcnt = 0;
|
|
328 }
|
|
329 }
|
|
330 if (index) {
|
|
331 index = reverse0(index);
|
|
332 // check lower bound
|
298
|
333 // switch_index_jmp(index,dlabel?dlabel:blabel);
|
|
334 // nonsense because no control here.
|
299
|
335 switch_make_index(index,icount,cslabel,gmax,0);
|
297
|
336 } else {
|
|
337 fwddef(cslabel);
|
|
338 switch_leaf(-1,merge,chunks);
|
|
339 }
|
298
|
340 if (control&&dlabel) jmp(dlabel);
|
297
|
341 }
|
|
342
|
|
343 /* generate switch table, index, cascading branch */
|
|
344
|
|
345
|
|
346 void
|
299
|
347 genswitch(int cslist,int cslabel)
|
297
|
348 {
|
|
349 int chunks,merge,gmax;
|
312
|
350 #if 0
|
311
|
351 int i;
|
|
352 int j;
|
297
|
353 for(i=cslist;i;i=cadr(i)) {
|
|
354 printf("# case %d L_%d\n",car(i),caddr(i));
|
|
355 }
|
|
356 #endif
|
311
|
357 control=1;
|
297
|
358 /* find stepwise chunks */
|
|
359 chunks=make_chunk(cslist);
|
311
|
360 #if 0
|
298
|
361 j = 0;
|
297
|
362 for(i=cslist;i;i=cadr(i)) {
|
298
|
363 j++;
|
297
|
364 }
|
|
365 #endif
|
|
366 /* possible merge of loose stepwise chunks */
|
|
367 merge = merge_chunk(chunks);
|
|
368 #if 0
|
|
369 // chunks: list3(widest,next,delta);
|
|
370 printf("# chunks %d = sum ",j);
|
|
371 j = 0;
|
|
372 for(i=merge;i;i=cadr(i)) {
|
|
373 printf(" %d/%d",car(i),caddr(i));
|
|
374 j+=car(i);
|
|
375 }
|
|
376 printf(" sum = %d\n",j);
|
|
377 #endif
|
|
378 /* make index branch or table jump */
|
|
379 gmax = chunk_max(chunks);
|
|
380 chunks = reverse0(chunks);
|
299
|
381 if (!dlabel) dlabel = blabel;
|
|
382 switch_index(merge,chunks,cslabel,gmax);
|
311
|
383 free_chunks(chunks);
|
297
|
384 }
|
|
385
|
|
386 #endif
|
|
387
|
|
388
|
|
389
|
|
390 /* end */
|