annotate mc-switch.c @ 389:68fb244e0e3c

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