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