annotate mc-switch.c @ 313:f73b93de216a alloca

alloca done for ia32, powerpc, mips
author kono
date Tue, 15 Jun 2004 00:11:26 +0900
parents a93e619cf772
children e5d40f8c4cce
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
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
11 extern void genswitch(int cslist,int cslabel);
297
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
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
30 #define CASE_TABLE_COUNT 10
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
31 #define CASE_INDEX_COUNT CASE_TABLE_COUNT // have to be equal
298
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 {
303
88e20739e64c register variable initialization fix
kono
parents: 302
diff changeset
41 register int delta,delta1,list,p,count;
297
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
303
88e20739e64c register variable initialization fix
kono
parents: 302
diff changeset
74 static int gcd(int i0,int j0)
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
75 {
303
88e20739e64c register variable initialization fix
kono
parents: 302
diff changeset
76 register int k,i=i0,j=j0;
297
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;
311
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
99 #if 0
297
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
311
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
112 static void
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
113 free_chunks(chunks)
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
114 {
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
115 int p;
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
116 while(chunks) {
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
117 p = car(chunks);
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
118 chunks = cadr(chunks); // free_glist3 will destroy cadr
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
119 free_glist3(p);
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
120 }
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
121 }
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
122
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
123 static int
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
124 merge_chunk(int chunks)
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
125 {
303
88e20739e64c register variable initialization fix
kono
parents: 302
diff changeset
126 register int list = chunks;
88e20739e64c register variable initialization fix
kono
parents: 302
diff changeset
127 register int tail,last,max,min,i;
88e20739e64c register variable initialization fix
kono
parents: 302
diff changeset
128 register int c_count,p,widest;
88e20739e64c register variable initialization fix
kono
parents: 302
diff changeset
129 register int merge = 0;
88e20739e64c register variable initialization fix
kono
parents: 302
diff changeset
130 int count,delta=1;
88e20739e64c register variable initialization fix
kono
parents: 302
diff changeset
131
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
132 while(cadr(list)) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
133 p = cadddr(list);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
134 c_count = count = car(p); max=caddr(p);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
135 widest = 1; last = list;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
136 delta = caddr(list);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
137 // check possible merge against the end of the chunks
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
138 for(i=1,tail=cadr(list);cadr(tail); tail=cadr(tail),i++) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
139 p = cadddr(tail);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
140 min = cadr(p);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
141 if (merge_chunk_p(&delta,max,min,&count,caddr(tail),car(p))) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
142 // It is mergeable.
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
143 widest = i+1; last = tail; c_count=count;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
144 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
145 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
146 merge = list4(widest,merge,delta,c_count);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
147 // skip merged chunks
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
148 list = cadr(last);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
149 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
150 // last one can remain.
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
151 if (list) merge = list4(1,merge,caddr(list),car(cadddr(list)));
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
152 return merge;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
153 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
154
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
155 static int
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
156 cascade_compare(int count,int cslist)
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
157 {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
158 int list;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
159 for(;count-->0;cslist=cadr(cslist)) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
160 list = car(cslist);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
161 printf("# cascade cases delta=%d count=%d min=%d max=%d\n",
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
162 caddr(cslist),car(cadddr(cslist)),
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
163 cadr(cadddr(cslist)),caddr(cadddr(cslist))
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
164 );
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
165 for(;list; list=cadr(list)) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
166 if (caddr(list))
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
167 cmpdimm(car(list),csvalue1,caddr(list),0);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
168 // value register label mode
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
169 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
170 }
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
171 // have to be fall thru
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
172 return cslist;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
173 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
174
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
175 static int
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
176 table_jump(int count,int delta,int chunks)
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
177 {
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
178 int list,i;
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
179 int l,max,min;
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
180 if (!code_table_jump_p())
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
181 return cascade_compare(count,chunks);
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
182
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
183 min=cadr(cadddr(chunks));
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
184 // find max
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
185 for(i=0,list=chunks;i<count;list=cadr(list),i++) {
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
186 max=caddr(cadddr(list));
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
187 }
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
188
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
189 printf("# table count %d delta %d max %d min %d\n",
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
190 count,delta,max,min);
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
191
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
192 l = fwdlabel();
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
193 code_table_jump(l,csvalue1,delta,max,min,dlabel);
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
194 control=0;
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
195 code_table_open(l);
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
196 i = min;
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
197 for(;count-->0;chunks=cadr(chunks)) {
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
198 list = car(chunks);
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
199 for(;list; list=cadr(list)) {
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
200 if (caddr(list)) {
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
201 for(;i<car(list);i+=delta) {
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
202 code_table_value(dlabel,l);
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
203 }
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
204 code_table_value(caddr(list),l);
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
205 i+=delta;
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
206 }
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 code_table_close();
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
210 return chunks;
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
211 }
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
212
297
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 generate index jmp
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
216 */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
217
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
218 static void
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
219 switch_index_jmp(int index,int label)
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
220 {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
221 int value = index_min(index);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
222 cmpdimm(value,csvalue1,label,LT);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
223 // value register label mode
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
224 }
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 generate leaf index branch
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
228 */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
229
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
230 static int
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
231 switch_make_index_leaf(int count,int index,int level)
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
232 {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
233 control=1;
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
234 printf("# index leaf count=%d level=%d\n",count,level);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
235 for(;count-- !=0 && cadr(index);index=cadr(index)) {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
236 switch_index_jmp( cadr(index), index_label(index) );
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
237 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
238 if (!cadr(index)) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
239 // don't check max boundary
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
240 jmp(index_label(index));
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
241 return 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
242 }
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
243 if (count!= -1) error(-1);
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
244 jmp(dlabel);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
245 return index;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
246 }
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 generate index switch branch (no table).
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
250 if necessary generate higher index.
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
251 */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
252
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
253 static void
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
254 switch_make_index(int index0,int count,int cslabel,int gmax,int level)
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
255 {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
256 int index=0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
257 int icount=0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
258 int l,min,max;
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
259 control=1;
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
260 if (count <= CASE_INDEX_COUNT) {
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
261 fwddef(cslabel);
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
262 switch_make_index_leaf(-1,index0,level);
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
263 } else {
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
264 while (index0) {
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
265 l = backdef();
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
266 min = index_min(index0);
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
267 index0=switch_make_index_leaf(CASE_INDEX_COUNT,index0,level);
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
268 if (index0) {
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
269 max = index_min(index0)-1;
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
270 } else {
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
271 max = gmax;
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
272 }
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
273 index = list4(l,index,max,min);
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
274 count-=CASE_INDEX_COUNT;
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
275 icount++;
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
276 }
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
277 switch_make_index(reverse0(index),icount,cslabel,gmax,level+1);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
278 }
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 generate leaf switch branch or table jump
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
283 */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
284
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
285 static int
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
286 switch_leaf(int count,int merge,int chunks)
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
287 {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
288 control=1;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
289 for(;count-- !=0 && merge;merge=cadr(merge)) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
290 printf("# merge count %d delta %d c_count %d\n",
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
291 car(merge),caddr(merge),cadddr(merge));
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
292 if (cadddr(merge)>CASE_TABLE_COUNT) {
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
293 chunks = table_jump(car(merge),caddr(merge),chunks);
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
294 } else {
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
295 chunks = cascade_compare(car(merge),chunks);
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
296 }
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
297 }
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
298 // table_jump has no fall thru, but cascade_compare does.
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
299 if (control) jmp(dlabel);
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
300 return chunks;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
301 }
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 make index if necessary or
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
305 generate case branch or table jump
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 static void
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
309 switch_index(int merge,int chunks,int cslabel,int gmax)
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
310 {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
311 int m,l,max,min;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
312 int chnkcnt = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
313 int icount = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
314 int count = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
315 int index = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
316 for(m=merge;m;m=cadr(m)) {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
317 if (!index && !cadr(m)) break; // no index for single chunk
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
318 chnkcnt++;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
319 count += cadddr(m);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
320 if (count > CASE_INDEX_COUNT || (index && !cadr(m))) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
321 icount++;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
322 min = car(car(chunks));
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
323 l = backdef();
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
324 chunks= switch_leaf(chnkcnt,merge,chunks);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
325 max = chunks?car(car(chunks)):gmax;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
326 index = list4(l,index,max,min);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
327 merge = cadr(m); count = 0; chnkcnt = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
328 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
329 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
330 if (index) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
331 index = reverse0(index);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
332 // check lower bound
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
333 // switch_index_jmp(index,dlabel?dlabel:blabel);
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
334 // nonsense because no control here.
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
335 switch_make_index(index,icount,cslabel,gmax,0);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
336 } else {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
337 fwddef(cslabel);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
338 switch_leaf(-1,merge,chunks);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
339 }
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
340 if (control&&dlabel) jmp(dlabel);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
341 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
342
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
343 /* generate switch table, index, cascading branch */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
344
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
345
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
346 void
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
347 genswitch(int cslist,int cslabel)
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
348 {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
349 int chunks,merge,gmax;
312
a93e619cf772 typeof, MIPS stdarg
kono
parents: 311
diff changeset
350 #if 0
311
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
351 int i;
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
352 int j;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
353 for(i=cslist;i;i=cadr(i)) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
354 printf("# case %d L_%d\n",car(i),caddr(i));
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
355 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
356 #endif
311
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
357 control=1;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
358 /* find stepwise chunks */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
359 chunks=make_chunk(cslist);
311
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
360 #if 0
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
361 j = 0;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
362 for(i=cslist;i;i=cadr(i)) {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
363 j++;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
364 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
365 #endif
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
366 /* possible merge of loose stepwise chunks */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
367 merge = merge_chunk(chunks);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
368 #if 0
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
369 // chunks: list3(widest,next,delta);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
370 printf("# chunks %d = sum ",j);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
371 j = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
372 for(i=merge;i;i=cadr(i)) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
373 printf(" %d/%d",car(i),caddr(i));
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
374 j+=car(i);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
375 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
376 printf(" sum = %d\n",j);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
377 #endif
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
378 /* make index branch or table jump */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
379 gmax = chunk_max(chunks);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
380 chunks = reverse0(chunks);
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
381 if (!dlabel) dlabel = blabel;
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
382 switch_index(merge,chunks,cslabel,gmax);
311
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
383 free_chunks(chunks);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
384 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
385
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
386 #endif
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
387
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 /* end */