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