annotate mc-switch.c @ 342:3e0b1183983d

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