annotate mc-switch.c @ 491:a379da780856

cond signed/unsigned
author kono
date Wed, 21 Dec 2005 11:33:53 +0900
parents 464e7480395c
children 1fcad06b264a
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)) {
468
464e7480395c *** empty log message ***
kono
parents: 458
diff changeset
111 printf("## min %d, max %d, count %d, delta %d, rate %g t=%d\n",
297
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
468
464e7480395c *** empty log message ***
kono
parents: 458
diff changeset
123 free_chunks(int chunks)
311
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);
468
464e7480395c *** empty log message ***
kono
parents: 458
diff changeset
171 printf("## cascade cases delta=%d count=%d min=%d max=%d\n",
297
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
468
464e7480395c *** empty log message ***
kono
parents: 458
diff changeset
199 printf("## table count %d delta %d max %d min %d\n",
300
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();
458
0bc9f3273aa7 *** empty log message ***
kono
parents: 456
diff changeset
203 if (car(csvalue1)!=REGISTER) error(-1);
0bc9f3273aa7 *** empty log message ***
kono
parents: 456
diff changeset
204 code_table_jump(l,cadr(csvalue1),delta,max,min,dlabel);
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
205 control=0;
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
206 code_table_open(l);
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
207 i = min;
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
208 for(;count-->0;chunks=cadr(chunks)) {
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
209 list = car(chunks);
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
210 for(;list; list=cadr(list)) {
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
211 if (caddr(list)) {
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
212 for(;i<car(list);i+=delta) {
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
213 code_table_value(dlabel,l);
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
214 }
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
215 code_table_value(caddr(list),l);
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
216 i+=delta;
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 }
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
220 code_table_close();
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
221 return chunks;
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
222 }
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
223
297
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 generate index jmp
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
227 */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
228
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
229 static void
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
230 switch_index_jmp(int index,int label)
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
231 {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
232 int value = index_min(index);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
233 cmpdimm(value,csvalue1,label,LT);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
234 // value register label mode
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 /*
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
238 generate leaf index branch
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
239 */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
240
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
241 static int
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
242 switch_make_index_leaf(int count,int index,int level)
297
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 control=1;
468
464e7480395c *** empty log message ***
kono
parents: 458
diff changeset
245 printf("## index leaf count=%d level=%d\n",count,level);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
246 for(;count-- !=0 && cadr(index);index=cadr(index)) {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
247 switch_index_jmp( cadr(index), index_label(index) );
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
248 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
249 if (!cadr(index)) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
250 // don't check max boundary
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
251 jmp(index_label(index));
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
252 return 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
253 }
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
254 if (count!= -1) error(-1);
361
93652cda75c7 minor speed up (macro/switch)
kono
parents: 327
diff changeset
255 djmp = 1;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
256 return index;
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 /*
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
260 generate index switch branch (no table).
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
261 if necessary generate higher index.
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
262 */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
263
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
264 static void
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
265 switch_make_index(int index0,int count,int cslabel,int gmax,int level)
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
266 {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
267 int index=0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
268 int icount=0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
269 int l,min,max;
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
270 control=1;
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
271 if (count <= CASE_INDEX_COUNT) {
361
93652cda75c7 minor speed up (macro/switch)
kono
parents: 327
diff changeset
272 check_djmp();
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
273 fwddef(cslabel);
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
274 switch_make_index_leaf(-1,index0,level);
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
275 } else {
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
276 while (index0) {
361
93652cda75c7 minor speed up (macro/switch)
kono
parents: 327
diff changeset
277 check_djmp();
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
278 l = backdef();
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
279 min = index_min(index0);
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
280 index0=switch_make_index_leaf(CASE_INDEX_COUNT,index0,level);
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
281 if (index0) {
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
282 max = index_min(index0)-1;
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
283 } else {
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
284 max = gmax;
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
285 }
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
286 index = list4(l,index,max,min);
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
287 count-=CASE_INDEX_COUNT;
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
288 icount++;
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
289 }
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
290 switch_make_index(reverse0(index),icount,cslabel,gmax,level+1);
297
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 /*
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
295 generate leaf switch branch or table jump
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
296 */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
298 static int
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
299 switch_leaf(int count,int merge,int chunks)
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
300 {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
301 control=1;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
302 for(;count-- !=0 && merge;merge=cadr(merge)) {
468
464e7480395c *** empty log message ***
kono
parents: 458
diff changeset
303 printf("## merge count %d delta %d c_count %d\n",
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
304 car(merge),caddr(merge),cadddr(merge));
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
305 if (cadddr(merge)>CASE_TABLE_COUNT) {
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
306 chunks = table_jump(car(merge),caddr(merge),chunks);
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
307 } else {
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
308 chunks = cascade_compare(car(merge),chunks);
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
309 }
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
310 }
302
bea7ab2fb218 switch table jump finished.
kono
parents: 300
diff changeset
311 // table_jump has no fall thru, but cascade_compare does.
361
93652cda75c7 minor speed up (macro/switch)
kono
parents: 327
diff changeset
312 if (control) djmp = 1;
300
86255dd7f148 table done. (?)
kono
parents: 299
diff changeset
313 return chunks;
297
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 /*
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
317 make index if necessary or
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
318 generate case branch or table jump
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
319 */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
320
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
321 static void
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
322 switch_index(int merge,int chunks,int cslabel,int gmax)
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
323 {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
324 int m,l,max,min;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
325 int chnkcnt = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
326 int icount = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
327 int count = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
328 int index = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
329 for(m=merge;m;m=cadr(m)) {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
330 if (!index && !cadr(m)) break; // no index for single chunk
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
331 chnkcnt++;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
332 count += cadddr(m);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
333 if (count > CASE_INDEX_COUNT || (index && !cadr(m))) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
334 icount++;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
335 min = car(car(chunks));
361
93652cda75c7 minor speed up (macro/switch)
kono
parents: 327
diff changeset
336 check_djmp();
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
337 l = backdef();
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
338 chunks= switch_leaf(chnkcnt,merge,chunks);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
339 max = chunks?car(car(chunks)):gmax;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
340 index = list4(l,index,max,min);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
341 merge = cadr(m); count = 0; chnkcnt = 0;
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 if (index) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
345 index = reverse0(index);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
346 // check lower bound
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
347 // switch_index_jmp(index,dlabel?dlabel:blabel);
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
348 // nonsense because no control here.
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
349 switch_make_index(index,icount,cslabel,gmax,0);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
350 } else {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
351 fwddef(cslabel);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
352 switch_leaf(-1,merge,chunks);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
353 }
361
93652cda75c7 minor speed up (macro/switch)
kono
parents: 327
diff changeset
354 if (control&&dlabel) djmp = 1;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
355 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
356
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
357 /* generate switch table, index, cascading branch */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
358
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
359
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
360 void
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
361 genswitch(int cslist,int cslabel)
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
362 {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
363 int chunks,merge,gmax;
312
a93e619cf772 typeof, MIPS stdarg
kono
parents: 311
diff changeset
364 #if 0
311
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
365 int i;
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
366 int j;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
367 for(i=cslist;i;i=cadr(i)) {
468
464e7480395c *** empty log message ***
kono
parents: 458
diff changeset
368 printf("## case %d L_%d\n",car(i),caddr(i));
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
369 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
370 #endif
311
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
371 control=1;
456
b8f95294eb77 inline continue... if test passed.
kono
parents: 382
diff changeset
372 if (chk) return;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
373 /* find stepwise chunks */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
374 chunks=make_chunk(cslist);
311
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
375 #if 0
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
376 j = 0;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
377 for(i=cslist;i;i=cadr(i)) {
298
4ccacae1d2e6 switch index level=1 odd situation.
kono
parents: 297
diff changeset
378 j++;
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
379 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
380 #endif
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
381 /* possible merge of loose stepwise chunks */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
382 merge = merge_chunk(chunks);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
383 #if 0
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
384 // chunks: list3(widest,next,delta);
468
464e7480395c *** empty log message ***
kono
parents: 458
diff changeset
385 printf("## chunks %d = sum ",j);
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
386 j = 0;
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
387 for(i=merge;i;i=cadr(i)) {
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
388 printf(" %d/%d",car(i),caddr(i));
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
389 j+=car(i);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
390 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
391 printf(" sum = %d\n",j);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
392 #endif
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
393 /* make index branch or table jump */
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
394 gmax = chunk_max(chunks);
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
395 chunks = reverse0(chunks);
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
396 if (!dlabel) dlabel = blabel;
361
93652cda75c7 minor speed up (macro/switch)
kono
parents: 327
diff changeset
397 djmp = 0;
299
3d260008c449 level 2 looks ok.
kono
parents: 298
diff changeset
398 switch_index(merge,chunks,cslabel,gmax);
311
38c9976863b7 swtich list fix. macro fix.
kono
parents: 303
diff changeset
399 free_chunks(chunks);
361
93652cda75c7 minor speed up (macro/switch)
kono
parents: 327
diff changeset
400 if (dlabel!=blabel)
93652cda75c7 minor speed up (macro/switch)
kono
parents: 327
diff changeset
401 check_djmp();
297
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
402 }
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
403
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
404 #endif
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
405
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
406
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
407
0f79c95df73a switch index no compile error
kono
parents:
diff changeset
408 /* end */