Mercurial > hg > CbC > old > device
changeset 297:0f79c95df73a
switch index no compile error
author | kono |
---|---|
date | Sun, 06 Jun 2004 20:19:36 +0900 |
parents | 284aa4eaab0d |
children | 4ccacae1d2e6 |
files | Changes Makefile mc-code-powerpc.c mc-codegen.c mc-codegen.h mc-parse.c mc-switch.c mc-switch.h |
diffstat | 8 files changed, 447 insertions(+), 214 deletions(-) [+] |
line wrap: on
line diff
--- a/Changes Sun Jun 06 02:40:32 2004 +0900 +++ b/Changes Sun Jun 06 20:19:36 2004 +0900 @@ -4586,6 +4586,42 @@ ただ、一旦全部リストにしないと格納できないか。混在した場合は どうなるんだろう? 文句言えばいいの? +やっぱり、難しいんじゃないかなぁ。これって、その場でemit_data/ +decl_data できないよね。local の場合はassign の列にコンパイ +ルすることはできるんだけど。global は、assign_data で、なん +かのキューに入れるか。mode を増やすわけね。 + +decl_data の先頭でgetsymするのはまずいわけね。 + while(t1) { + offset = decl_data(car(t1),n,offset); /* alignment? */ + .... + } +の代わりに、 + + getsym(); + if (SYM==DOT) { + smode=mode; + sstlist = stlist; + stlist = 0; + if (mode!=LOCAL) + mode = SNAMEMODE; + while(sym!=RC) { + type = search_struct_type(..&offset1); + decl_data(car(t1),n,offset1); + } + if (mode== SNAMEMODE) + emit_sname_assign(stlist); // global? + offset += sizeof(struct); + stlist = sstlist; + mode=smode; + } else { + while(t1) + offset = decl_data(car(t1),n,offset); /* alignment? */ + .... + } +みたいな感じ? nest した場合は、emit_sname_assign でも decl_data +を呼んで、再度キューに入れるしかないか。 + Mon May 31 19:08:57 JST 2004 register_assop は、いいんだけど、register_assop_const の @@ -4685,7 +4721,7 @@ まぁ、やっぱり、 for i (0..n) { - c_i を可能な限り拡大する + c_i を c_n に向かって可能な限り拡大する できなかったら、それを生成 } @@ -4703,3 +4739,55 @@ 結果が良くないみたい。人間は、case 1: case 2: ... というように 書くからね。そんなに難しくないはずだが、直すのが面倒。 +Sun Jun 6 02:41:27 JST 2004 + +次はバイナリサーチで、テーブルを生成するコードを書けば良いだ +けだが。2分法だときれいなブランチパターンにならない。8分法ぐ +らいで書くのがいいんじゃないかな。 + + case_group_1: + if (c==case_2) goto case_2 ( or nested case_group ) + if (c==case_3) goto case_3 + .... + if (c==case_8) goto case_8 + goto default + case_group_2: + if (c==case_10) goto case_10 + ... + goto default + cslabel: + if (c==case_1) goto case_1 (*) + else (c<case_1) goto default + if (c==case_9) goto case_9 (*) + else (c<case_9) goto case_group_1 (or table lookup ) + if (c==case_17) goto case_17 (*) + else (c<case_17) goto case_group_2 + ... + else goto default; + +かな... (*) は、オプション。ない方が良いCPUもあるだろう。MIPSとか。 + +フラットの方が良い場合もあるはず。(実測する?) + +Sun Jun 6 09:39:13 JST 2004 + +で、nested branch は、どう実装すれば良いの? recursive に +書くんだよね? bottom up に書きたいところだけど。 + + #define CASE_INDEX_COUNT 8 + switch_index(int chunk,int *cslist) { + if (case count > CASE_INDEX_COUNT) { + chunk1 = gather_chunk() + chunk1 = switch_index(chunk,&cslist); + + while(chunks) { + chunk = switch_index(chunk,&cslist); + +ぐらい? (chunk でやるの?!) ... ん... だったら、merge_chunk +でできるんじゃないの? できるだろうけど、ちょっと複雑すぎるか。 + +table があるなら、必ず index は必要。 + +index がtableになることって... 偶然にはあるかも知れないし、 +無理にすればできないことはないだろうけど... +
--- a/Makefile Sun Jun 06 02:40:32 2004 +0900 +++ b/Makefile Sun Jun 06 20:19:36 2004 +0900 @@ -10,7 +10,7 @@ PRINTF= # printf.c CONVERTER=conv/c.o conv/null.o # conv/c2cbc.o conv/cbc2c.o -COMPLIB = mc-parse.o mc-codegen.o mc-tree.o +COMPLIB = mc-parse.o mc-codegen.o mc-switch.o mc-tree.o # CODE=mc-code-ia32.c CODE=mc-code-$(ARCH).c @@ -21,7 +21,8 @@ TAGS: ctags mc-code-powerpc.c mc-code.h mc-codegen.c mc-codegen.h \ - mc-parse.c mc-tree.c mc.h conv/c.c conv/c.h \ + mc-parse.c mc-tree.c mc-switch.c mc-switch.h \ + mc.h conv/c.c conv/c.h \ conv/conv.h conv/convdef.h conv/null.c mc-powerpc : mc-code-powerpc.o $(COMPLIB) $(CONVERTER) @@ -130,7 +131,7 @@ depend : makedepend mc-code-ia32.c mc-code.h mc-codegen.c mc-codegen.h \ - mc-parse.c mc-tree.c mc.h \ + mc-parse.c mc-tree.c mc-switch.c mc-switch.h mc.h \ conv/c.c conv/c.h conv/c2cbc.c conv/c2cbc.h conv/cbc2c.c \ conv/cbc2c.h conv/conv.h conv/convdef.h conv/null.c conv/null.h \ mc-code-powerpc.c \
--- a/mc-code-powerpc.c Sun Jun 06 02:40:32 2004 +0900 +++ b/mc-code-powerpc.c Sun Jun 06 20:19:36 2004 +0900 @@ -2633,7 +2633,11 @@ jcond(int l, char cond) { if (chk) return; - printf("\tb%s cr%d,L_%d\n",cond?"ne":"eq",cmpflag,l); + if (cond==LT) { + printf("\tb%s cr%d,L_%d\n",code_ge(0),cmpflag,l); + } else if (cond==1||cond==0) { + printf("\tb%s cr%d,L_%d\n",cond?"ne":"eq",cmpflag,l); + } else error(-1); } void
--- a/mc-codegen.c Sun Jun 06 02:40:32 2004 +0900 +++ b/mc-codegen.c Sun Jun 06 20:19:36 2004 +0900 @@ -1770,209 +1770,4 @@ return 0; } -#if CASE_CODE - -/* - cslist = list3(value,next,label) label==0 means skip - chunk = list4(cslist,next,delta,list3(count,min,max)) - */ - -/* - group continous case value - */ - -static int -make_chunk(int cslist) -{ - int delta,delta1,list,p,count; - // cslist is already sorted - delta = 0; - list = list4(cslist,0,1,list3(0,car(cslist) /* min */,0)); - count=1; - for(p=0;cadr(cslist);cslist = cadr(cslist),count++) { - // compute new delta - delta1 = car(cadr(cslist))-car(cslist); - // empty case - if (!caddr(cslist)) count--; - if (delta==0) { - // 2nd element - caddr(list) = delta = delta1; - if (p) cadr(p)=0; // terminate previous chunk - } else if (delta1!=delta) { - // not contiguous, start new list - caddr(cadddr(list)) = car(cslist); // max - car(cadddr(list)) = count; count=0; - delta = 0; - list = list4(cadr(cslist),list,1,list3(0,0,0)); - cadr(cadddr(list)) = car(cadr(cslist)); // min - // prepare terminate point for next turn - p=cslist; - } - } - if (p) cadr(p)=0; // terminate previous chunk - car(cadddr(list)) = count; - caddr(cadddr(list)) = car(cslist); // max - return list; -} - -#define CASE_MERGE_RATE 70 - -static int gcd(int i,int j) -{ - int k; - if (i<j) { k=i; i=j; j=k;} - for(;;) { - if ((k=i%j)==0) return j; - i = j; j = k; - } -} - -/* - check two chunks are mergeable or not. - */ - -int -merge_chunk_p(int *delta,int max,int min,int *count,int delta1,int count1) -{ - int range = max-min; - int g; - // compute possible merge delta - g = gcd(*delta,delta1); - g = gcd(g,range); - range /= g; - *count = count1 = count1+*count; - *delta = g; -#if 1 - if (count1*128>(range*128*CASE_MERGE_RATE/100)) { - printf("# min %d, max %d, count %d, delta %d, rate %g t=%d\n", - min,max,count1,g, - ((double)count1)*100.0/range, - count1*128>(range*128*CASE_MERGE_RATE/100) - ); - } -#endif - // count/((max-min)/delta) > 0.8 - return count1*128>(range*128*CASE_MERGE_RATE/100); -} - -/* - merge possible chunk - input: list4(cslist,next,delta,list3(count,min,max)) - output: list3(continuous number of chunk,next,delta,case_count); - */ - -static int -merge_chunk(int cslist) -{ - int list = cslist; - int tail,last,max,min,i; - int count,c_count,p,widest,delta=1; - int chunks = 0; - while(cadr(list)) { - p = cadddr(list); - c_count = count = car(p); max=caddr(p); - widest = 1; last = list; - delta = caddr(list); - // check possible merge against the end of the chunks - for(i=1,tail=cadr(list);cadr(tail); tail=cadr(tail),i++) { - p = cadddr(tail); - min = cadr(p); - if (merge_chunk_p(&delta,max,min,&count,caddr(tail),car(p))) { - // It is mergeable. - widest = i+1; last = tail; c_count=count; - } - } - chunks = list4(widest,chunks,delta,c_count); - // skip merged chunks - list = cadr(last); - } - // last one can remain. - if (list) chunks = list4(1,chunks,caddr(list),car(cadddr(list))); - return chunks; -} - -static int -table_jump(int count,int delta,int cslist) -{ - int list; - for(;count-->0;cslist=cadr(cslist)) { - list = car(cslist); - printf("# table cases delta=%d count=%d min=%d max=%d\n", - caddr(cslist),car(cadddr(cslist)), - cadr(cadddr(cslist)),caddr(cadddr(cslist)) - ); - for(;list; list=cadr(list)) { - if (caddr(list)) - cmpdimm(car(list),csvalue1,caddr(list),0); - } - } - return cslist; -} - -static int -cascade_compare(int count,int cslist) -{ - int list; - for(;count-->0;cslist=cadr(cslist)) { - list = car(cslist); - printf("# cascade cases delta=%d count=%d min=%d max=%d\n", - caddr(cslist),car(cadddr(cslist)), - cadr(cadddr(cslist)),caddr(cadddr(cslist)) - ); - for(;list; list=cadr(list)) { - if (caddr(list)) - cmpdimm(car(list),csvalue1,caddr(list),0); - } - } - return cslist; -} - -#define CASE_TABLE_COUNT 10 - -void -switch_table(int cslist,int cslabel,int dlabel) -{ - int chunks; - int i,j; -#if 0 - for(i=cslist;i;i=cadr(i)) { - printf("# case %d L_%d\n",car(i),caddr(i)); - } -#endif - cslist=make_chunk(cslist); -#if 1 - chunks = 0; - for(i=cslist;i;i=cadr(i)) { - chunks++; - } - j = chunks; -#endif - chunks = merge_chunk(cslist); -#if 0 - // chunks: list3(widest,next,delta); - printf("# chunks %d = sum ",j); - j = 0; - for(i=chunks;i;i=cadr(i)) { - printf(" %d/%d",car(i),caddr(i)); - j+=car(i); - } - printf(" sum = %d\n",j); -#endif - fwddef(cslabel); - cslist = reverse0(cslist); - for(;chunks;chunks=cadr(chunks)) { - printf("# chunk count %d delta %d c_count %d\n", - car(chunks),caddr(chunks),cadddr(chunks)); - if (cadddr(chunks)>CASE_TABLE_COUNT) - cslist = table_jump(car(chunks),caddr(chunks),cslist); - else - cslist = cascade_compare(car(chunks),cslist); - } - if (dlabel) jmp(dlabel); -} - -#endif - - - /* end */
--- a/mc-codegen.h Sun Jun 06 02:40:32 2004 +0900 +++ b/mc-codegen.h Sun Jun 06 20:19:36 2004 +0900 @@ -20,9 +20,6 @@ extern int backdef(void); extern int free_register_count(int); extern int fwdlabel(void); -#if CASE_CODE -extern void switch_table(int cslist,int cslabel,int dlabel); -#endif extern void b_expr(int e1, char cond, int l1,int err); extern void bexpr(int e1, char cond, int l1); extern void emit_init(void);
--- a/mc-parse.c Sun Jun 06 02:40:32 2004 +0900 +++ b/mc-parse.c Sun Jun 06 20:19:36 2004 +0900 @@ -3,6 +3,7 @@ #define EXTERN /**/ #include "mc.h" #include "mc-codegen.h" +#include "mc-switch.h" static NMTBL *decl0(void),*decl1(void),*lsearch(char *name,int sc); static NMTBL *gsearch(int sc); @@ -1895,7 +1896,7 @@ checkret(); #if CASE_CODE if (control) jmp(blabel); - switch_table(cslist,cslabel,dlabel); + genswitch(cslist,cslabel,dlabel); #else if(dlabel) def_label(cslabel,dlabel); else fwddef(cslabel);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mc-switch.c Sun Jun 06 20:19:36 2004 +0900 @@ -0,0 +1,344 @@ +/* Micro-C Switch Code generation Part */ +/* $Id$ */ + +#define EXTERN extern +#include "mc.h" +#include "mc-code.h" +#include "mc-codegen.h" + +#if CASE_CODE + +extern void genswitch(int cslist,int cslabel,int dlabel); + +/* + value label pair + cslist = list3(value,next,label) label==0 means skip + continuous cslist + chunks: list4(cslist,next,delta,list3(count,min,max)) + mergeable chunks count list + merge: list4(continuous number of chunk,next,delta,case_count); + index list + index: list4(label,next,max,min); + */ + +#define chunk_max(chunks) caddr(cadddr(chunks)) +#define chunk_min(chunks) cadr(cadddr(chunks)) +#define index_max(index) caddr(index) +#define index_min(index) cadddr(index) + +/* + group continous case value + */ + +static int +make_chunk(int cslist) +{ + int delta,delta1,list,p,count; + // cslist is already sorted + delta = 0; + list = list4(cslist,0,1,list3(0,car(cslist) /* min */,0)); + count=1; + for(p=0;cadr(cslist);cslist = cadr(cslist),count++) { + // compute new delta + delta1 = car(cadr(cslist))-car(cslist); + // empty case + if (!caddr(cslist)) count--; + if (delta==0) { + // 2nd element + caddr(list) = delta = delta1; + if (p) cadr(p)=0; // terminate previous chunk + } else if (delta1!=delta) { + // not contiguous, start new list + caddr(cadddr(list)) = car(cslist); // max + car(cadddr(list)) = count; count=0; + delta = 0; + list = list4(cadr(cslist),list,1,list3(0,0,0)); + cadr(cadddr(list)) = car(cadr(cslist)); // min + // prepare terminate point for next turn + p=cslist; + } + } + if (p) cadr(p)=0; // terminate previous chunk + car(cadddr(list)) = count; + caddr(cadddr(list)) = car(cslist); // max + return list; +} + +#define CASE_MERGE_RATE 70 + +static int gcd(int i,int j) +{ + int k; + if (i<j) { k=i; i=j; j=k;} + for(;;) { + if ((k=i%j)==0) return j; + i = j; j = k; + } +} + +/* + check two chunks are mergeable or not. + */ + +static int +merge_chunk_p(int *delta,int max,int min,int *count,int delta1,int count1) +{ + int range = max-min; + int g; + // compute possible merge delta + g = gcd(*delta,delta1); + g = gcd(g,range); + range /= g; + *count = count1 = count1+*count; + *delta = g; +#if 1 + if (count1*128>(range*128*CASE_MERGE_RATE/100)) { + printf("# min %d, max %d, count %d, delta %d, rate %g t=%d\n", + min,max,count1,g, + ((double)count1)*100.0/range, + count1*128>(range*128*CASE_MERGE_RATE/100) + ); + } +#endif + // count/((max-min)/delta) > 0.8 + return count1*128>(range*128*CASE_MERGE_RATE/100); +} + + +static int +merge_chunk(int chunks) +{ + int list = chunks; + int tail,last,max,min,i; + int count,c_count,p,widest,delta=1; + int merge = 0; + while(cadr(list)) { + p = cadddr(list); + c_count = count = car(p); max=caddr(p); + widest = 1; last = list; + delta = caddr(list); + // check possible merge against the end of the chunks + for(i=1,tail=cadr(list);cadr(tail); tail=cadr(tail),i++) { + p = cadddr(tail); + min = cadr(p); + if (merge_chunk_p(&delta,max,min,&count,caddr(tail),car(p))) { + // It is mergeable. + widest = i+1; last = tail; c_count=count; + } + } + merge = list4(widest,merge,delta,c_count); + // skip merged chunks + list = cadr(last); + } + // last one can remain. + if (list) merge = list4(1,merge,caddr(list),car(cadddr(list))); + return merge; +} + +static int +table_jump(int count,int delta,int cslist) +{ + int list; + for(;count-->0;cslist=cadr(cslist)) { + list = car(cslist); + printf("# table cases delta=%d count=%d min=%d max=%d\n", + caddr(cslist),car(cadddr(cslist)), + cadr(cadddr(cslist)),caddr(cadddr(cslist)) + ); + for(;list; list=cadr(list)) { + if (caddr(list)) + cmpdimm(car(list),csvalue1,caddr(list),0); + } + } + return cslist; +} + +static int +cascade_compare(int count,int cslist) +{ + int list; + for(;count-->0;cslist=cadr(cslist)) { + list = car(cslist); + printf("# cascade cases delta=%d count=%d min=%d max=%d\n", + caddr(cslist),car(cadddr(cslist)), + cadr(cadddr(cslist)),caddr(cadddr(cslist)) + ); + for(;list; list=cadr(list)) { + if (caddr(list)) + cmpdimm(car(list),csvalue1,caddr(list),0); + // value register label mode + } + } + return cslist; +} + +#define CASE_TABLE_COUNT 10 +#define CASE_INDEX_COUNT 10 + + +/* + generate index jmp + */ + +static void +switch_index_jmp(int index,int label) +{ + int value = car(car(caddr(caddr(index)))); + cmpdimm(value,csvalue1,label,LT); + // value register label mode +} + +/* + generate leaf index branch + */ + +static int +switch_make_index_leaf(int count,int index) +{ + for(;count-- !=0 && cadr(index);index=cadr(index)) { + switch_index_jmp( + cadr(index), /* next max */ + car(caddr(index)) /*label*/ ); + } + if (!cadr(index)) { + // don't check max boundary + jmp(car(caddr(index))); + return 0; + } + if (count!=0) error(-1); + return index; +} + +/* + generate index switch branch (no table). + if necessary generate higher index. + */ + +static void +switch_make_index(int index0,int count,int cslabel) +{ + int index=0; + int icount=0; + int l,min,max; + while (index0 && count > CASE_INDEX_COUNT) { + l = backdef(); + min = index_min(index0); + index0=switch_make_index_leaf(CASE_INDEX_COUNT,index0); + max = index_min(index0); + index = list4(l,index,max,min); + count-=CASE_INDEX_COUNT; + icount++; + } + if (index) { + switch_make_index(reverse0(index),icount,cslabel); + if (dlabel) jmp(dlabel); + } else { + fwddef(cslabel); + switch_make_index_leaf(-1,index0); + if (dlabel) jmp(dlabel); + } +} + +/* + generate leaf switch branch or table jump + */ + +static int +switch_leaf(int count,int merge,int cslist) +{ + for(;count-- !=0 && merge;merge=cadr(merge)) { + printf("# merge count %d delta %d c_count %d\n", + car(merge),caddr(merge),cadddr(merge)); + if (cadddr(merge)>CASE_TABLE_COUNT) + cslist = table_jump(car(merge),caddr(merge),cslist); + else + cslist = cascade_compare(car(merge),cslist); + } + return cslist; +} + +/* + make index if necessary or + generate case branch or table jump + */ + +static void +switch_index(int merge,int chunks,int cslabel,int dlabel,int gmax) +{ + int m,l,max,min; + int chnkcnt = 0; + int icount = 0; + int count = 0; + int index = 0; + for(m=merge;m;m=cadr(m)) { + chnkcnt++; + count += cadddr(m); + if (count > CASE_INDEX_COUNT || (index && !cadr(m))) { + icount++; + min = car(car(chunks)); + l = backdef(); + chunks= switch_leaf(chnkcnt,merge,chunks); + max = chunks?car(car(chunks)):gmax; + index = list4(l,index,max,min); + merge = cadr(m); count = 0; chnkcnt = 0; + if (dlabel) jmp(dlabel); + } + } + if (index) { + index = reverse0(index); + // check lower bound + switch_index_jmp(index,dlabel?dlabel:blabel); + switch_make_index(index,icount,cslabel); + if (dlabel) jmp(dlabel); + } else { + fwddef(cslabel); + switch_leaf(-1,merge,chunks); + if (dlabel) jmp(dlabel); + } +} + +/* generate switch table, index, cascading branch */ + + +void +genswitch(int cslist,int cslabel,int dlabel) +{ + int chunks,merge,gmax; + int i,j; +#if 0 + for(i=cslist;i;i=cadr(i)) { + printf("# case %d L_%d\n",car(i),caddr(i)); + } +#endif + /* find stepwise chunks */ + chunks=make_chunk(cslist); +#if 1 + chunks = 0; + for(i=cslist;i;i=cadr(i)) { + chunks++; + } + j = chunks; +#endif + /* possible merge of loose stepwise chunks */ + merge = merge_chunk(chunks); +#if 0 + // chunks: list3(widest,next,delta); + printf("# chunks %d = sum ",j); + j = 0; + for(i=merge;i;i=cadr(i)) { + printf(" %d/%d",car(i),caddr(i)); + j+=car(i); + } + printf(" sum = %d\n",j); +#endif + /* make index branch or table jump */ + gmax = chunk_max(chunks); + chunks = reverse0(chunks); + switch_index(merge,chunks,cslabel,dlabel,gmax); +} + +#endif + + + +/* end */