annotate mc-switch.c @ 298:4ccacae1d2e6

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