Mercurial > hg > CbC > old > device
view mc-switch.c @ 436:d92786033042 loprtc-div
loprtc
author | kono |
---|---|
date | Sun, 14 Nov 2004 15:41:05 +0900 |
parents | 832e1f6bba82 |
children | b8f95294eb77 |
line wrap: on
line source
/* Micro-C Switch Code generation Part */ /* $Id$ */ #include <stdio.h> #include "mc.h" #include "mc-parse.h" #include "mc-codegen.h" #include "mc-code.h" #if CASE_CODE extern void genswitch(int cslist,int cslabel); /* 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) #define index_label(index) car(index) #define CASE_TABLE_COUNT 10 #define CASE_INDEX_COUNT CASE_TABLE_COUNT // have to be equal static int djmp; static void check_djmp() { if (control && dlabel && djmp) jmp(dlabel); djmp = 0; } /* group continuous case value */ static int make_chunk(int cslist) { register 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 i0,int j0) { register int k,i=i0,j=j0; 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 0 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 void free_chunks(chunks) { int p; while(chunks) { p = car(chunks); chunks = cadr(chunks); // free_glist3 will destroy cadr free_glist3(p); } } static int merge_chunk(int chunks) { register int list = chunks; register int tail,last,max,min,i; register int c_count,p,widest; register int merge = 0; int count,delta=1; 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 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 } } // have to be fall thru return cslist; } static int table_jump(int count,int delta,int chunks) { int list,i; int l,max,min; if (!code_table_jump_p(delta)) return cascade_compare(count,chunks); min=cadr(cadddr(chunks)); // find max for(i=0,list=chunks;i<count;list=cadr(list),i++) { max=caddr(cadddr(list)); } printf("# table count %d delta %d max %d min %d\n", count,delta,max,min); l = fwdlabel(); code_table_jump(l,csvalue1,delta,max,min,dlabel); control=0; code_table_open(l); i = min; for(;count-->0;chunks=cadr(chunks)) { list = car(chunks); for(;list; list=cadr(list)) { if (caddr(list)) { for(;i<car(list);i+=delta) { code_table_value(dlabel,l); } code_table_value(caddr(list),l); i+=delta; } } } code_table_close(); return chunks; } /* generate index jmp */ static void switch_index_jmp(int index,int label) { int value = index_min(index); cmpdimm(value,csvalue1,label,LT); // value register label mode } /* generate leaf index branch */ static int switch_make_index_leaf(int count,int index,int level) { control=1; printf("# index leaf count=%d level=%d\n",count,level); for(;count-- !=0 && cadr(index);index=cadr(index)) { switch_index_jmp( cadr(index), index_label(index) ); } if (!cadr(index)) { // don't check max boundary jmp(index_label(index)); return 0; } if (count!= -1) error(-1); djmp = 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 gmax,int level) { int index=0; int icount=0; int l,min,max; control=1; if (count <= CASE_INDEX_COUNT) { check_djmp(); fwddef(cslabel); switch_make_index_leaf(-1,index0,level); } else { while (index0) { check_djmp(); l = backdef(); min = index_min(index0); index0=switch_make_index_leaf(CASE_INDEX_COUNT,index0,level); if (index0) { max = index_min(index0)-1; } else { max = gmax; } index = list4(l,index,max,min); count-=CASE_INDEX_COUNT; icount++; } switch_make_index(reverse0(index),icount,cslabel,gmax,level+1); } } /* generate leaf switch branch or table jump */ static int switch_leaf(int count,int merge,int chunks) { control=1; 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) { chunks = table_jump(car(merge),caddr(merge),chunks); } else { chunks = cascade_compare(car(merge),chunks); } } // table_jump has no fall thru, but cascade_compare does. if (control) djmp = 1; return chunks; } /* make index if necessary or generate case branch or table jump */ static void switch_index(int merge,int chunks,int cslabel,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)) { if (!index && !cadr(m)) break; // no index for single chunk chnkcnt++; count += cadddr(m); if (count > CASE_INDEX_COUNT || (index && !cadr(m))) { icount++; min = car(car(chunks)); check_djmp(); 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 (index) { index = reverse0(index); // check lower bound // switch_index_jmp(index,dlabel?dlabel:blabel); // nonsense because no control here. switch_make_index(index,icount,cslabel,gmax,0); } else { fwddef(cslabel); switch_leaf(-1,merge,chunks); } if (control&&dlabel) djmp = 1; } /* generate switch table, index, cascading branch */ void genswitch(int cslist,int cslabel) { int chunks,merge,gmax; #if 0 int i; int j; for(i=cslist;i;i=cadr(i)) { printf("# case %d L_%d\n",car(i),caddr(i)); } #endif control=1; /* find stepwise chunks */ chunks=make_chunk(cslist); #if 0 j = 0; for(i=cslist;i;i=cadr(i)) { j++; } #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); if (!dlabel) dlabel = blabel; djmp = 0; switch_index(merge,chunks,cslabel,gmax); free_chunks(chunks); if (dlabel!=blabel) check_djmp(); } #endif /* end */