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)) {
|
468
|
111 printf("## min %d, max %d, count %d, delta %d, rate %g t=%d\n",
|
297
|
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
|
468
|
123 free_chunks(int chunks)
|
311
|
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);
|
468
|
171 printf("## cascade cases delta=%d count=%d min=%d max=%d\n",
|
297
|
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
|
468
|
199 printf("## table count %d delta %d max %d min %d\n",
|
300
|
200 count,delta,max,min);
|
|
201
|
|
202 l = fwdlabel();
|
458
|
203 if (car(csvalue1)!=REGISTER) error(-1);
|
|
204 code_table_jump(l,cadr(csvalue1),delta,max,min,dlabel);
|
302
|
205 control=0;
|
300
|
206 code_table_open(l);
|
|
207 i = min;
|
|
208 for(;count-->0;chunks=cadr(chunks)) {
|
|
209 list = car(chunks);
|
|
210 for(;list; list=cadr(list)) {
|
|
211 if (caddr(list)) {
|
|
212 for(;i<car(list);i+=delta) {
|
|
213 code_table_value(dlabel,l);
|
|
214 }
|
|
215 code_table_value(caddr(list),l);
|
|
216 i+=delta;
|
|
217 }
|
|
218 }
|
|
219 }
|
|
220 code_table_close();
|
|
221 return chunks;
|
|
222 }
|
|
223
|
297
|
224
|
|
225 /*
|
|
226 generate index jmp
|
|
227 */
|
|
228
|
|
229 static void
|
|
230 switch_index_jmp(int index,int label)
|
|
231 {
|
298
|
232 int value = index_min(index);
|
297
|
233 cmpdimm(value,csvalue1,label,LT);
|
|
234 // value register label mode
|
|
235 }
|
|
236
|
|
237 /*
|
|
238 generate leaf index branch
|
|
239 */
|
|
240
|
|
241 static int
|
298
|
242 switch_make_index_leaf(int count,int index,int level)
|
297
|
243 {
|
298
|
244 control=1;
|
468
|
245 printf("## index leaf count=%d level=%d\n",count,level);
|
297
|
246 for(;count-- !=0 && cadr(index);index=cadr(index)) {
|
298
|
247 switch_index_jmp( cadr(index), index_label(index) );
|
297
|
248 }
|
|
249 if (!cadr(index)) {
|
|
250 // don't check max boundary
|
298
|
251 jmp(index_label(index));
|
297
|
252 return 0;
|
|
253 }
|
298
|
254 if (count!= -1) error(-1);
|
361
|
255 djmp = 1;
|
297
|
256 return index;
|
|
257 }
|
|
258
|
|
259 /*
|
|
260 generate index switch branch (no table).
|
|
261 if necessary generate higher index.
|
|
262 */
|
|
263
|
|
264 static void
|
299
|
265 switch_make_index(int index0,int count,int cslabel,int gmax,int level)
|
297
|
266 {
|
|
267 int index=0;
|
|
268 int icount=0;
|
|
269 int l,min,max;
|
298
|
270 control=1;
|
299
|
271 if (count <= CASE_INDEX_COUNT) {
|
361
|
272 check_djmp();
|
297
|
273 fwddef(cslabel);
|
298
|
274 switch_make_index_leaf(-1,index0,level);
|
299
|
275 } else {
|
|
276 while (index0) {
|
361
|
277 check_djmp();
|
299
|
278 l = backdef();
|
|
279 min = index_min(index0);
|
|
280 index0=switch_make_index_leaf(CASE_INDEX_COUNT,index0,level);
|
|
281 if (index0) {
|
|
282 max = index_min(index0)-1;
|
|
283 } else {
|
|
284 max = gmax;
|
|
285 }
|
|
286 index = list4(l,index,max,min);
|
|
287 count-=CASE_INDEX_COUNT;
|
|
288 icount++;
|
|
289 }
|
|
290 switch_make_index(reverse0(index),icount,cslabel,gmax,level+1);
|
297
|
291 }
|
|
292 }
|
|
293
|
|
294 /*
|
|
295 generate leaf switch branch or table jump
|
|
296 */
|
|
297
|
|
298 static int
|
300
|
299 switch_leaf(int count,int merge,int chunks)
|
297
|
300 {
|
298
|
301 control=1;
|
297
|
302 for(;count-- !=0 && merge;merge=cadr(merge)) {
|
468
|
303 printf("## merge count %d delta %d c_count %d\n",
|
297
|
304 car(merge),caddr(merge),cadddr(merge));
|
302
|
305 if (cadddr(merge)>CASE_TABLE_COUNT) {
|
300
|
306 chunks = table_jump(car(merge),caddr(merge),chunks);
|
302
|
307 } else {
|
300
|
308 chunks = cascade_compare(car(merge),chunks);
|
302
|
309 }
|
297
|
310 }
|
302
|
311 // table_jump has no fall thru, but cascade_compare does.
|
361
|
312 if (control) djmp = 1;
|
300
|
313 return chunks;
|
297
|
314 }
|
|
315
|
|
316 /*
|
|
317 make index if necessary or
|
|
318 generate case branch or table jump
|
|
319 */
|
|
320
|
|
321 static void
|
299
|
322 switch_index(int merge,int chunks,int cslabel,int gmax)
|
297
|
323 {
|
|
324 int m,l,max,min;
|
|
325 int chnkcnt = 0;
|
|
326 int icount = 0;
|
|
327 int count = 0;
|
|
328 int index = 0;
|
|
329 for(m=merge;m;m=cadr(m)) {
|
298
|
330 if (!index && !cadr(m)) break; // no index for single chunk
|
297
|
331 chnkcnt++;
|
|
332 count += cadddr(m);
|
|
333 if (count > CASE_INDEX_COUNT || (index && !cadr(m))) {
|
|
334 icount++;
|
|
335 min = car(car(chunks));
|
361
|
336 check_djmp();
|
297
|
337 l = backdef();
|
|
338 chunks= switch_leaf(chnkcnt,merge,chunks);
|
|
339 max = chunks?car(car(chunks)):gmax;
|
|
340 index = list4(l,index,max,min);
|
|
341 merge = cadr(m); count = 0; chnkcnt = 0;
|
|
342 }
|
|
343 }
|
|
344 if (index) {
|
|
345 index = reverse0(index);
|
|
346 // check lower bound
|
298
|
347 // switch_index_jmp(index,dlabel?dlabel:blabel);
|
|
348 // nonsense because no control here.
|
299
|
349 switch_make_index(index,icount,cslabel,gmax,0);
|
297
|
350 } else {
|
|
351 fwddef(cslabel);
|
|
352 switch_leaf(-1,merge,chunks);
|
|
353 }
|
361
|
354 if (control&&dlabel) djmp = 1;
|
297
|
355 }
|
|
356
|
|
357 /* generate switch table, index, cascading branch */
|
|
358
|
|
359
|
|
360 void
|
299
|
361 genswitch(int cslist,int cslabel)
|
297
|
362 {
|
|
363 int chunks,merge,gmax;
|
312
|
364 #if 0
|
311
|
365 int i;
|
|
366 int j;
|
297
|
367 for(i=cslist;i;i=cadr(i)) {
|
468
|
368 printf("## case %d L_%d\n",car(i),caddr(i));
|
297
|
369 }
|
|
370 #endif
|
311
|
371 control=1;
|
456
|
372 if (chk) return;
|
297
|
373 /* find stepwise chunks */
|
|
374 chunks=make_chunk(cslist);
|
311
|
375 #if 0
|
298
|
376 j = 0;
|
297
|
377 for(i=cslist;i;i=cadr(i)) {
|
298
|
378 j++;
|
297
|
379 }
|
|
380 #endif
|
|
381 /* possible merge of loose stepwise chunks */
|
|
382 merge = merge_chunk(chunks);
|
|
383 #if 0
|
|
384 // chunks: list3(widest,next,delta);
|
468
|
385 printf("## chunks %d = sum ",j);
|
297
|
386 j = 0;
|
|
387 for(i=merge;i;i=cadr(i)) {
|
|
388 printf(" %d/%d",car(i),caddr(i));
|
|
389 j+=car(i);
|
|
390 }
|
|
391 printf(" sum = %d\n",j);
|
|
392 #endif
|
|
393 /* make index branch or table jump */
|
|
394 gmax = chunk_max(chunks);
|
|
395 chunks = reverse0(chunks);
|
299
|
396 if (!dlabel) dlabel = blabel;
|
361
|
397 djmp = 0;
|
299
|
398 switch_index(merge,chunks,cslabel,gmax);
|
311
|
399 free_chunks(chunks);
|
361
|
400 if (dlabel!=blabel)
|
|
401 check_djmp();
|
297
|
402 }
|
|
403
|
|
404 #endif
|
|
405
|
|
406
|
|
407
|
|
408 /* end */
|