297
|
1 /* Micro-C Switch Code generation Part */
|
607
|
2
|
|
3 /************************************************************************
|
|
4 ** Copyright (C) 2006 Shinji Kono
|
|
5 ** 連絡先: 琉球大学情報工学科 河野 真治
|
|
6 ** (E-Mail Address: kono@ie.u-ryukyu.ac.jp)
|
|
7 **
|
|
8 ** このソースのいかなる複写,改変,修正も許諾します。ただし、
|
|
9 ** その際には、誰が貢献したを示すこの部分を残すこと。
|
|
10 ** 再配布や雑誌の付録などの問い合わせも必要ありません。
|
|
11 ** 営利利用も上記に反しない範囲で許可します。
|
|
12 ** バイナリの配布の際にはversion messageを保存することを条件とします。
|
|
13 ** このプログラムについては特に何の保証もしない、悪しからず。
|
|
14 **
|
|
15 ** Everyone is permitted to do anything on this program
|
|
16 ** including copying, modifying, improving,
|
|
17 ** as long as you don't try to pretend that you wrote it.
|
|
18 ** i.e., the above copyright notice has to appear in all copies.
|
|
19 ** Binary distribution requires original version messages.
|
|
20 ** You don't have to ask before copying, redistribution or publishing.
|
|
21 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
|
|
22 ***********************************************************************/
|
297
|
23
|
327
|
24 #include <stdio.h>
|
297
|
25 #include "mc.h"
|
327
|
26 #include "mc-parse.h"
|
|
27 #include "mc-codegen.h"
|
297
|
28 #include "mc-code.h"
|
|
29
|
|
30 #if CASE_CODE
|
|
31
|
299
|
32 extern void genswitch(int cslist,int cslabel);
|
297
|
33
|
|
34 /*
|
|
35 value label pair
|
|
36 cslist = list3(value,next,label) label==0 means skip
|
|
37 continuous cslist
|
|
38 chunks: list4(cslist,next,delta,list3(count,min,max))
|
|
39 mergeable chunks count list
|
|
40 merge: list4(continuous number of chunk,next,delta,case_count);
|
|
41 index list
|
|
42 index: list4(label,next,max,min);
|
645
|
43
|
|
44
|
|
45
|
|
46 A list of value label pair is generated in do_case, in sorted list.
|
|
47
|
|
48 The list is separated in continous cslists which has a same
|
|
49 delta in make_chunk(int cslist).
|
|
50
|
|
51 merge_chunk(int chunks) tries to findout possible merge of continous
|
|
52 cslist. If merge_chunk_p() accepts reasonably densed switch talbe,
|
|
53 the cslists are merged.
|
|
54
|
|
55 switch_index() makes hierachical indexed branches, using switch_leaf().
|
|
56 switch_leaf(). switch_leaf() generates table jump if it is suitable.
|
|
57 Otherwise it generates multi-stage cascading jump. siwtch_make_index()
|
|
58 generates multi-stage index in a recursive way.
|
|
59
|
|
60
|
297
|
61 */
|
|
62
|
|
63 #define chunk_max(chunks) caddr(cadddr(chunks))
|
|
64 #define chunk_min(chunks) cadr(cadddr(chunks))
|
|
65 #define index_max(index) caddr(index)
|
|
66 #define index_min(index) cadddr(index)
|
298
|
67 #define index_label(index) car(index)
|
|
68
|
300
|
69 #define CASE_TABLE_COUNT 10
|
302
|
70 #define CASE_INDEX_COUNT CASE_TABLE_COUNT // have to be equal
|
298
|
71
|
361
|
72 static int djmp;
|
|
73
|
|
74 static void
|
|
75 check_djmp()
|
|
76 {
|
|
77 if (control && dlabel && djmp)
|
880
|
78 jmp(dlabel);
|
361
|
79 djmp = 0;
|
|
80 }
|
297
|
81
|
|
82 /*
|
326
|
83 group continuous case value
|
297
|
84 */
|
|
85
|
|
86 static int
|
|
87 make_chunk(int cslist)
|
|
88 {
|
303
|
89 register int delta,delta1,list,p,count;
|
297
|
90 // cslist is already sorted
|
|
91 delta = 0;
|
|
92 list = list4(cslist,0,1,list3(0,car(cslist) /* min */,0));
|
|
93 count=1;
|
|
94 for(p=0;cadr(cslist);cslist = cadr(cslist),count++) {
|
|
95 // compute new delta
|
880
|
96 delta1 = car(cadr(cslist))-car(cslist);
|
297
|
97 // empty case
|
880
|
98 if (!caddr(cslist)) count--;
|
|
99 if (delta==0) {
|
297
|
100 // 2nd element
|
880
|
101 caddr(list) = delta = delta1;
|
|
102 if (p) cadr(p)=0; // terminate previous chunk
|
|
103 } else if (delta1!=delta) {
|
|
104 // not contiguous, start new list
|
|
105 caddr(cadddr(list)) = car(cslist); // max
|
|
106 car(cadddr(list)) = count; count=0;
|
297
|
107 delta = 0;
|
880
|
108 list = list4(cadr(cslist),list,1,list3(0,0,0));
|
|
109 cadr(cadddr(list)) = car(cadr(cslist)); // min
|
297
|
110 // prepare terminate point for next turn
|
880
|
111 p=cslist;
|
|
112 }
|
297
|
113 }
|
|
114 if (p) cadr(p)=0; // terminate previous chunk
|
|
115 car(cadddr(list)) = count;
|
|
116 caddr(cadddr(list)) = car(cslist); // max
|
|
117 return list;
|
|
118 }
|
|
119
|
592
|
120 #define CASE_MERGE_RATE 50
|
297
|
121
|
303
|
122 static int gcd(int i0,int j0)
|
297
|
123 {
|
303
|
124 register int k,i=i0,j=j0;
|
297
|
125 if (i<j) { k=i; i=j; j=k;}
|
|
126 for(;;) {
|
880
|
127 if ((k=i%j)==0) return j;
|
|
128 i = j; j = k;
|
297
|
129 }
|
|
130 }
|
|
131
|
|
132 /*
|
|
133 check two chunks are mergeable or not.
|
|
134 */
|
|
135
|
|
136 static int
|
|
137 merge_chunk_p(int *delta,int max,int min,int *count,int delta1,int count1)
|
|
138 {
|
|
139 int range = max-min;
|
|
140 int g;
|
623
|
141 int c = 128*CASE_MERGE_RATE/100;
|
297
|
142 // compute possible merge delta
|
|
143 g = gcd(*delta,delta1);
|
|
144 g = gcd(g,range);
|
|
145 range /= g;
|
|
146 *count = count1 = count1+*count;
|
|
147 *delta = g;
|
311
|
148 #if 0
|
297
|
149 if (count1*128>(range*128*CASE_MERGE_RATE/100)) {
|
880
|
150 if (lsrc) printf("## min %d, max %d, count %d, delta %d, rate %g t=%d\n",
|
|
151 min,max,count1,g,
|
|
152 ((double)count1)*100.0/range,
|
|
153 count1*128>(range*128*CASE_MERGE_RATE/100)
|
|
154 );
|
297
|
155 }
|
|
156 #endif
|
|
157 // count/((max-min)/delta) > 0.8
|
623
|
158 return count1*128>(range*c);
|
297
|
159 }
|
|
160
|
311
|
161 static void
|
468
|
162 free_chunks(int chunks)
|
311
|
163 {
|
|
164 int p;
|
|
165 while(chunks) {
|
880
|
166 p = car(chunks);
|
|
167 chunks = cadr(chunks); // free_glist3 will destroy cadr
|
|
168 free_glist3(p);
|
311
|
169 }
|
|
170 }
|
297
|
171
|
|
172 static int
|
|
173 merge_chunk(int chunks)
|
|
174 {
|
303
|
175 register int list = chunks;
|
|
176 register int tail,last,max,min,i;
|
|
177 register int c_count,p,widest;
|
|
178 register int merge = 0;
|
|
179 int count,delta=1;
|
|
180
|
297
|
181 while(cadr(list)) {
|
880
|
182 p = cadddr(list);
|
|
183 c_count = count = car(p); max=caddr(p);
|
|
184 widest = 1; last = list;
|
|
185 delta = caddr(list);
|
297
|
186 // check possible merge against the end of the chunks
|
880
|
187 for(i=1,tail=cadr(list);cadr(tail); tail=cadr(tail),i++) {
|
|
188 p = cadddr(tail);
|
|
189 min = cadr(p);
|
|
190 if (merge_chunk_p(&delta,max,min,&count,caddr(tail),car(p))) {
|
297
|
191 // It is mergeable.
|
880
|
192 widest = i+1; last = tail; c_count=count;
|
|
193 }
|
|
194 }
|
|
195 merge = list4(widest,merge,delta,c_count);
|
297
|
196 // skip merged chunks
|
880
|
197 list = cadr(last);
|
297
|
198 }
|
|
199 // last one can remain.
|
|
200 if (list) merge = list4(1,merge,caddr(list),car(cadddr(list)));
|
|
201 return merge;
|
|
202 }
|
|
203
|
645
|
204 /*
|
|
205 Generates series of compare immideate. It is suitable for
|
|
206 small amount of jump and random value.
|
|
207 */
|
297
|
208 static int
|
|
209 cascade_compare(int count,int cslist)
|
|
210 {
|
|
211 int list;
|
|
212 for(;count-->0;cslist=cadr(cslist)) {
|
880
|
213 list = car(cslist);
|
609
|
214 #ifdef DEBUG_MERGE
|
880
|
215 if (lsrc) printf("## cascade cases delta=%d count=%d min=%d max=%d\n",
|
|
216 caddr(cslist),car(cadddr(cslist)),
|
|
217 cadr(cadddr(cslist)),caddr(cadddr(cslist))
|
|
218 );
|
609
|
219 #endif
|
880
|
220 for(;list; list=cadr(list)) {
|
|
221 if (caddr(list))
|
|
222 cmpdimm(car(list),csvalue1,caddr(list),0);
|
297
|
223 // value register label mode
|
880
|
224 }
|
297
|
225 }
|
302
|
226 // have to be fall thru
|
297
|
227 return cslist;
|
|
228 }
|
|
229
|
645
|
230 /*
|
|
231 Make table jump code, suitable for fixed delta and large size
|
|
232 table.
|
|
233 */
|
300
|
234 static int
|
|
235 table_jump(int count,int delta,int chunks)
|
|
236 {
|
|
237 int list,i;
|
569
|
238 int l,max=0,min;
|
382
|
239 if (!code_table_jump_p(delta))
|
880
|
240 return cascade_compare(count,chunks);
|
300
|
241
|
|
242 min=cadr(cadddr(chunks));
|
|
243 // find max
|
|
244 for(i=0,list=chunks;i<count;list=cadr(list),i++) {
|
880
|
245 max=caddr(cadddr(list));
|
300
|
246 }
|
|
247
|
652
|
248 if (lsrc) printf("## table count %d delta %d max %d min %d\n",
|
880
|
249 count,delta,max,min);
|
300
|
250
|
|
251 l = fwdlabel();
|
458
|
252 if (car(csvalue1)!=REGISTER) error(-1);
|
|
253 code_table_jump(l,cadr(csvalue1),delta,max,min,dlabel);
|
302
|
254 control=0;
|
300
|
255 code_table_open(l);
|
|
256 i = min;
|
|
257 for(;count-->0;chunks=cadr(chunks)) {
|
880
|
258 list = car(chunks);
|
|
259 for(;list; list=cadr(list)) {
|
|
260 if (caddr(list)) {
|
|
261 for(;i<car(list);i+=delta) {
|
|
262 code_table_value(dlabel,l);
|
|
263 }
|
|
264 code_table_value(caddr(list),l);
|
|
265 i+=delta;
|
|
266 }
|
|
267 }
|
300
|
268 }
|
|
269 code_table_close();
|
|
270 return chunks;
|
|
271 }
|
|
272
|
297
|
273
|
|
274 /*
|
|
275 generate index jmp
|
|
276 */
|
|
277
|
|
278 static void
|
|
279 switch_index_jmp(int index,int label)
|
|
280 {
|
298
|
281 int value = index_min(index);
|
297
|
282 cmpdimm(value,csvalue1,label,LT);
|
|
283 // value register label mode
|
|
284 }
|
|
285
|
|
286 /*
|
|
287 generate leaf index branch
|
|
288 */
|
|
289
|
|
290 static int
|
298
|
291 switch_make_index_leaf(int count,int index,int level)
|
297
|
292 {
|
298
|
293 control=1;
|
652
|
294 if (lsrc) printf("## index leaf count=%d level=%d\n",count,level);
|
297
|
295 for(;count-- !=0 && cadr(index);index=cadr(index)) {
|
880
|
296 switch_index_jmp( cadr(index), index_label(index) );
|
297
|
297 }
|
|
298 if (!cadr(index)) {
|
880
|
299 // don't check max boundary
|
|
300 jmp(index_label(index));
|
|
301 return 0;
|
297
|
302 }
|
298
|
303 if (count!= -1) error(-1);
|
361
|
304 djmp = 1;
|
297
|
305 return index;
|
|
306 }
|
|
307
|
|
308 /*
|
|
309 generate index switch branch (no table).
|
|
310 if necessary generate higher index.
|
|
311 */
|
|
312
|
|
313 static void
|
299
|
314 switch_make_index(int index0,int count,int cslabel,int gmax,int level)
|
297
|
315 {
|
|
316 int index=0;
|
|
317 int icount=0;
|
|
318 int l,min,max;
|
298
|
319 control=1;
|
299
|
320 if (count <= CASE_INDEX_COUNT) {
|
880
|
321 check_djmp();
|
|
322 fwddef(cslabel);
|
|
323 switch_make_index_leaf(-1,index0,level);
|
299
|
324 } else {
|
880
|
325 // too large index, make hihger index
|
|
326 while (index0) {
|
|
327 check_djmp();
|
|
328 l = backdef();
|
|
329 min = index_min(index0);
|
|
330 index0=switch_make_index_leaf(CASE_INDEX_COUNT,index0,level);
|
|
331 if (index0) {
|
|
332 max = index_min(index0)-1;
|
|
333 } else {
|
|
334 max = gmax;
|
|
335 }
|
|
336 index = list4(l,index,max,min);
|
|
337 count-=CASE_INDEX_COUNT;
|
|
338 icount++;
|
|
339 }
|
|
340 // process higher indexes in a recursive way
|
|
341 switch_make_index(reverse0(index),icount,cslabel,gmax,level+1);
|
297
|
342 }
|
|
343 }
|
|
344
|
|
345 /*
|
|
346 generate leaf switch branch or table jump
|
|
347 */
|
|
348
|
|
349 static int
|
300
|
350 switch_leaf(int count,int merge,int chunks)
|
297
|
351 {
|
298
|
352 control=1;
|
297
|
353 for(;count-- !=0 && merge;merge=cadr(merge)) {
|
609
|
354 #ifdef DEBUG_MERGE
|
880
|
355 if (lsrc) printf("## merge count %d delta %d c_count %d\n",
|
|
356 car(merge),caddr(merge),cadddr(merge));
|
609
|
357 #endif
|
880
|
358 if (cadddr(merge)>CASE_TABLE_COUNT) {
|
|
359 chunks = table_jump(car(merge),caddr(merge),chunks);
|
|
360 } else {
|
|
361 chunks = cascade_compare(car(merge),chunks);
|
|
362 }
|
297
|
363 }
|
302
|
364 // table_jump has no fall thru, but cascade_compare does.
|
361
|
365 if (control) djmp = 1;
|
300
|
366 return chunks;
|
297
|
367 }
|
|
368
|
|
369 /*
|
|
370 make index if necessary or
|
|
371 generate case branch or table jump
|
|
372 */
|
|
373
|
|
374 static void
|
299
|
375 switch_index(int merge,int chunks,int cslabel,int gmax)
|
297
|
376 {
|
|
377 int m,l,max,min;
|
|
378 int chnkcnt = 0;
|
|
379 int icount = 0;
|
|
380 int count = 0;
|
|
381 int index = 0;
|
|
382 for(m=merge;m;m=cadr(m)) {
|
880
|
383 if (!index && !cadr(m)) break; // no index for single chunk
|
297
|
384 chnkcnt++;
|
880
|
385 count += cadddr(m);
|
|
386 if (count > CASE_INDEX_COUNT || (index && !cadr(m))) {
|
|
387 // too large for single stage cascading or
|
|
388 // we already have indexies and this is the last one.
|
|
389 icount++;
|
|
390 min = car(car(chunks));
|
|
391 check_djmp();
|
|
392 l = backdef();
|
|
393 chunks= switch_leaf(chnkcnt,merge,chunks);
|
|
394 max = chunks?car(car(chunks)):gmax;
|
297
|
395 index = list4(l,index,max,min);
|
880
|
396 merge = cadr(m); count = 0; chnkcnt = 0;
|
|
397 }
|
297
|
398 }
|
|
399 if (index) {
|
880
|
400 // yes we have indexes
|
|
401 index = reverse0(index);
|
297
|
402 // check lower bound
|
880
|
403 // switch_index_jmp(index,dlabel?dlabel:blabel);
|
298
|
404 // nonsense because no control here.
|
880
|
405 switch_make_index(index,icount,cslabel,gmax,0);
|
297
|
406 } else {
|
880
|
407 fwddef(cslabel);
|
|
408 switch_leaf(-1,merge,chunks);
|
297
|
409 }
|
361
|
410 if (control&&dlabel) djmp = 1;
|
297
|
411 }
|
|
412
|
|
413 /* generate switch table, index, cascading branch */
|
|
414
|
|
415
|
|
416 void
|
299
|
417 genswitch(int cslist,int cslabel)
|
297
|
418 {
|
|
419 int chunks,merge,gmax;
|
312
|
420 #if 0
|
311
|
421 int i;
|
|
422 int j;
|
297
|
423 for(i=cslist;i;i=cadr(i)) {
|
880
|
424 if (lsrc) printf("## case %d L_%d\n",car(i),caddr(i));
|
297
|
425 }
|
|
426 #endif
|
311
|
427 control=1;
|
456
|
428 if (chk) return;
|
297
|
429 /* find stepwise chunks */
|
|
430 chunks=make_chunk(cslist);
|
311
|
431 #if 0
|
298
|
432 j = 0;
|
297
|
433 for(i=cslist;i;i=cadr(i)) {
|
298
|
434 j++;
|
297
|
435 }
|
|
436 #endif
|
|
437 /* possible merge of loose stepwise chunks */
|
|
438 merge = merge_chunk(chunks);
|
|
439 #if 0
|
|
440 // chunks: list3(widest,next,delta);
|
652
|
441 if (lsrc) printf("## chunks %d = sum ",j);
|
297
|
442 j = 0;
|
|
443 for(i=merge;i;i=cadr(i)) {
|
880
|
444 if (lsrc) printf(" %d/%d",car(i),caddr(i));
|
|
445 j+=car(i);
|
297
|
446 }
|
652
|
447 if (lsrc) printf(" sum = %d\n",j);
|
297
|
448 #endif
|
|
449 /* make index branch or table jump */
|
|
450 gmax = chunk_max(chunks);
|
|
451 chunks = reverse0(chunks);
|
299
|
452 if (!dlabel) dlabel = blabel;
|
361
|
453 djmp = 0;
|
299
|
454 switch_index(merge,chunks,cslabel,gmax);
|
311
|
455 free_chunks(chunks);
|
361
|
456 if (dlabel!=blabel)
|
880
|
457 check_djmp();
|
297
|
458 }
|
|
459
|
|
460 #endif
|
|
461
|
|
462
|
|
463
|
|
464 /* end */
|