# HG changeset patch # User kono # Date 1192192845 -32400 # Node ID 6785c63f0558c436d268b2592cf9fd2b567036d3 # Parent 25115b50d033886eef36befe128d4a4f228734f8 decl_data static incomplete diff -r 25115b50d033 -r 6785c63f0558 .gdbinit --- a/.gdbinit Wed Oct 10 17:58:15 2007 +0900 +++ b/.gdbinit Fri Oct 12 21:40:45 2007 +0900 @@ -15,9 +15,9 @@ b errmsg # r -s test/tmp7.c # r -s test/tmp6.c -r -s test/call.c +# r -s test/call.c # r -s test/code-gen-all.c # r -s mc-code-powerpc.c -# r -s test/strinit.c +r -s test/strinit.c # r -s -DINLINE=inline test/strinit.c # r -s test/fact-a.c diff -r 25115b50d033 -r 6785c63f0558 mc-codegen.c --- a/mc-codegen.c Wed Oct 10 17:58:15 2007 +0900 +++ b/mc-codegen.c Fri Oct 12 21:40:45 2007 +0900 @@ -61,7 +61,7 @@ static void remove0(int *parent,int e) ; static void sassign(int e1); static int gen_decl_data(int e); -static int gen_decl_data0(NMTBL *nptr0,int target_type,int init,int offset); +static int gen_decl_data0(int v,int target_type,int init,int offset); #if FLOAT_CODE @@ -1795,16 +1795,9 @@ if (car(e4)==DECL_DATA && (car(e2)==GVAR || car(e2)==LVAR)) { // we can do optimize any other type of destination ... // but gen_decl_data0 requires nptr (of course wrong decision) - NMTBL nptr1; int t = caddr(e4); int sinit_vars = init_vars; init_vars = 0; - nptr1.sc = car(e2); - nptr1.dsp = cadr(e2); - nptr1.ty = t; - nptr1.nm = 0; - nptr1.next = 0; - nptr1.attr = 0; - gen_decl_data0(&nptr1,t,e4,0); + gen_decl_data0(e2,t,e4,0); emit_init_vars(); init_vars = sinit_vars; return; @@ -3595,12 +3588,13 @@ extern int -gen_delayed_decl_data(NMTBL *n,int offset) +gen_delayed_decl_data(int v,int offset) { int offset0=0; int e; int t,sz,offset1=0; int init = decl_str_init; + NMTBL *n = (NMTBL*)caddr(v); decl_str_init = 0; sz = size(n->ty); @@ -3615,22 +3609,22 @@ t=car(cadddr(init)); if (offset!=offset0) { // make space - assign_data(list2(CONST,offset-offset0),EMPTY,n,offset0); + assign_data(list2(CONST,offset-offset0),EMPTY,v,offset0); } type=cadr(cadddr(init)); - offset0 = gen_decl_data0(n,t,e,offset); + offset0 = gen_decl_data0(v,t,e,offset); init = cadr(init); } offset = offset0; if ((sz=(offset1+sz-offset))>0) - assign_data(list2(CONST,sz),EMPTY,n,offset0); + assign_data(list2(CONST,sz),EMPTY,v,offset0); decl_str_init = 0; local_nptr = 0; return offset; } static int -gen_decl_data_array(NMTBL *nptr0,int init,int target_type,int offset) +gen_decl_data_array(int v,int init,int target_type,int offset) { int type0 = cadr(target_type); /* array item type */ int e; @@ -3641,13 +3635,13 @@ error(-1); } e = pexpr(caddr(init)); - offset = gen_decl_data0(nptr0,type0,e,offset); + offset = gen_decl_data0(v,type0,e,offset); } return offset; } static int -gen_decl_data_field(NMTBL *nptr0,int init,int target_type,int offset) +gen_decl_data_field(int v,int init,int target_type,int offset) { int type0 = target_type; /* list of fields */ int e,t,type1,foffset; @@ -3670,7 +3664,7 @@ } static int -gen_decl_data_list(NMTBL *nptr0,int init,int target_type,int offset) +gen_decl_data_list(int v,int init,int target_type,int offset) { int type0 = caddr(target_type); /* list of fields */ int e; @@ -3681,7 +3675,7 @@ error(-1); } e = caddr(init); - offset = gen_decl_data0(nptr0,car(type0),e,offset); + offset = gen_decl_data0(v,car(type0),e,offset); type0 = cadr(type0); } return offset; @@ -3689,30 +3683,30 @@ static int -gen_decl_data0(NMTBL *nptr0,int target_type,int init,int offset) +gen_decl_data0(int v,int target_type,int init,int offset) { int e,t; if (car(init)==DECL_DATA) { switch( car(e=cadr(init))) { case DECL_DATA_LIST: - offset = gen_decl_data_list(nptr0,e,target_type,offset); + offset = gen_decl_data_list(v,e,target_type,offset); break; case DECL_DATA_FIELD: - offset = gen_decl_data_field(nptr0,e,target_type,offset); + offset = gen_decl_data_field(v,e,target_type,offset); break; case DECL_DATA_ARRAY: - offset = gen_decl_data_array(nptr0,e,target_type,offset); + offset = gen_decl_data_array(v,e,target_type,offset); break; default: t = caddr(init); // type of source - e = rvalue_t(e,t); - offset=assign_data(e,t,nptr0,offset); + // e = rvalue_t(e,t); + offset=assign_data(e,t,v,offset); } } else { error(-1); } if (decl_str_init) { - offset = gen_delayed_decl_data(nptr0,offset); + offset = gen_delayed_decl_data(v,offset); } return offset; } @@ -3738,7 +3732,7 @@ e1 = list3(RSTRUCT,list3( nptr0->sc,nptr0->dsp,(int)nptr0),sz); - gen_decl_data0(nptr0,t,e,offset); + gen_decl_data0(list3(nptr0->sc,nptr0->dsp,(int)nptr0),t,e,offset); if (init_vars) emit_init_vars(); g_expr0(e1); @@ -3751,9 +3745,11 @@ // extern int -assign_data(int e, int t, NMTBL *n,int offset) +assign_data(int e, int t, int v,int offset) { int ass,sz,bfd; + int v0 = car(v); + NMTBL *n = (NMTBL*)caddr(v); if (inmode) error(-1); #if STRUCT_ALIGN @@ -3796,13 +3792,8 @@ But some one may take it's address. We have to generate assign. */ ass = assign_expr0( - inmode?( - offset? - list3(ADD,list3(n->sc,n->dsp,(int)n),list2(CONST,offset)): - list3(n->sc,n->dsp,(int)n)): - (n->sc==REGISTER||n->sc==DREGISTER||n->sc==FREGISTER||n->sc==LREGISTER)? - list3(n->sc,n->dsp,(int)n): - list3(n->sc,n->dsp+offset,(int)n), + (v0==REGISTER||v0==DREGISTER||v0==FREGISTER||v0==LREGISTER)? + v: (offset? list3(v0,cadr(v)+offset,caddr(v)) : v), e,t,type); init_vars = list2(ass,init_vars); break; @@ -3832,12 +3823,14 @@ } extern void -flush_delayed_decl_data(NMTBL *n) +flush_delayed_decl_data(int v) { int offset; int offset0=0; int e; int t,sz,offset1=0; + NMTBL *n = (NMTBL*)caddr(v); + sz = size(n->ty); /* decl_str_init @@ -3850,28 +3843,28 @@ t=car(cadddr(decl_str_init)); if (offset!=offset0) { // make space - assign_data(list2(CONST,offset-offset0),EMPTY,n,offset0); + assign_data(list2(CONST,offset-offset0),EMPTY,v,offset0); } type=cadr(cadddr(decl_str_init)); // if (lsrc)printf("## %d flush c0(e)=%d type=%d t=%d offset=%d\n",lineno,car(e),type,t,offset); - offset0 = assign_data(e,t,n,offset); + offset0 = assign_data(e,t,v,offset); decl_str_init = cadr(decl_str_init); } offset = offset0; if ((sz=(offset1+sz-offset))>0) - assign_data(list2(CONST,sz),EMPTY,n,offset0); + assign_data(list2(CONST,sz),EMPTY,v,offset0); decl_str_init = 0; local_nptr = 0; } extern void -data_closing(NMTBL *n) +data_closing(int e) { if (!chk) { int smode = mode; mode = STADECL; - if (decl_str_init) flush_delayed_decl_data(n); + if (decl_str_init) flush_delayed_decl_data(e); mode = smode; - emit_data_closing(n); + emit_data_closing((NMTBL*)caddr(e)); } } diff -r 25115b50d033 -r 6785c63f0558 mc-codegen.h --- a/mc-codegen.h Wed Oct 10 17:58:15 2007 +0900 +++ b/mc-codegen.h Fri Oct 12 21:40:45 2007 +0900 @@ -44,7 +44,7 @@ extern NMTBL * def(NMTBL *n,int ctmode); extern int arg_reorder(int arg,int new_arg); -extern int assign_data(int e, int t, NMTBL *n,int offset); +extern int assign_data(int e, int t, int v,int offset); extern int assign_expr(int e1,int e2,int t); extern int assign_expr0(int e1,int e2,int t,int type0); extern int assop(int e1,int e2,int op,int t,int no_float); @@ -73,7 +73,7 @@ extern void codegen_decl_init(); /* called before each declaration */ extern void codegen_init(); /* called only once */ extern void codegen_reinit(); /* called for each file */ -extern void data_closing(NMTBL *n); +extern void data_closing(int v); extern void df_label(int cslabel, int dlabel); extern void emit_init_vars(void); extern void fcheck(NMTBL *n); @@ -96,7 +96,7 @@ extern void ret(void); extern void gen_ret(void); extern void gen_label_call(int l); -extern void flush_delayed_decl_data(NMTBL *n); +extern void flush_delayed_decl_data(int v); /* used by mc-inline */ diff -r 25115b50d033 -r 6785c63f0558 mc-inline.c --- a/mc-inline.c Wed Oct 10 17:58:15 2007 +0900 +++ b/mc-inline.c Fri Oct 12 21:40:45 2007 +0900 @@ -833,11 +833,16 @@ int e; for(; init; init = cadr(init)) { + int e1 = init; + if (car(e1)==DECL_DATA) { + // casted initilizer ? + continue; + } // ordered data - if (car(init)!=DECL_DATA_LIST) { + if (car(e1)!=DECL_DATA_LIST) { error(-1); } - e = pexpr(caddr(init)); + e = pexpr(caddr(e1)); offset = pdecl_data(var,car(type0),e,offset); type0 = cadr(type0); } @@ -852,6 +857,11 @@ decl_str_init = 0; target_type = type_value(target_type); + if (init==0) { + // empty declaration + // it can happen like a = (struct hoge){}; + return offset; + } e = cadr(init); if (car(init)==DECL_DATA) { switch(car(e)) { @@ -1422,7 +1432,11 @@ caddr(e1)); #endif case CAST: - if (car(e2)==DECL_DATA) { + if (e2==0) { + // casted empty structure (struct hoge){} + // I think we can skip it. It means nothing in declaration + return 0; + } else if (car(e2)==DECL_DATA) { // casted initialized structure (struct hoge){...} return list3(DECL_DATA,pexpr(cadr(e2)),caddr(e2)); } diff -r 25115b50d033 -r 6785c63f0558 mc-parse.c --- a/mc-parse.c Wed Oct 10 17:58:15 2007 +0900 +++ b/mc-parse.c Fri Oct 12 21:40:45 2007 +0900 @@ -46,7 +46,7 @@ extern double strtod(const char *nptr, char **endptr); #endif -static const int parse_mode = 1; // generate parse tree for all code +static int parse_mode = 1; // generate parse tree for all code static int HEAP_REPORT = 0; static int lfree_type_limit; // debugging purpose @@ -179,8 +179,8 @@ static int escape(void); static void statement(int); static int typename(void); -static int decl_data_field(int type,NMTBL *n,int offset); -static int decl_data(int t, NMTBL *n,int offset,int skip); +static int decl_data_field(int type,int v,int offset); +static int decl_data(int t, int v,int offset,int skip); static int typeid(int s); extern NMTBL * get_name_from_chptr(); static NMTBL * hash_search(char *name,struct cheap *scheap,int len,unsigned int hash,int mode); @@ -256,7 +256,11 @@ case 'v': HEAP_REPORT = 1; break; + case 'p': + parse_mode = !parse_mode; + break; case 'D': + // define macro option. processed by macro break; case 'C': if (av[ac2+1]) set_converter(av[ac2]+2); @@ -929,11 +933,13 @@ } else error(DCERR); } else { int init = 0; + int v; conv->return_type_(type,n,sd); n = def(n,ctmode); + v = list3(n->sc,n->dsp,(int)n); if (sym==ASS && n!=&null_nptr) { conv->op_(sym); - init = decl_data(type,n,0,0); data_closing(n); + init = decl_data(type,v,0,0); data_closing(v); } if (inmode && (mode==LDECL||mode==LLDECL)) { // NMTBL *n1 = get_nptr(); @@ -954,9 +960,10 @@ } conv->return_type_(type,n,1); def(n,ctmode); + v = list3(n->sc,n->dsp,(int)n); if (sym==ASS && n!=&null_nptr) { conv->op_(sym); - init = decl_data(type,n,0,0);data_closing(n); + init = decl_data(type,v,0,0);data_closing(v); } if (inmode && mode==LDECL) { parse = list5(ST_DECL,parse,(int)n, @@ -1512,7 +1519,7 @@ #define LOCAL_STRUCT_INIT_STATIC 1 static int -decl_data_1(int type,NMTBL *n,int offset) +decl_data_1(int type,int v,int offset) { int t; @@ -1520,12 +1527,12 @@ getsym(0); if (sym==LPAR) { - offset = decl_data_1(type,n,offset); + offset = decl_data_1(type,v,offset); checksym(RPAR); } else if (typeid(sym)) { t = typename(); checksym(RPAR); - offset = decl_data(t,n,offset,1); + offset = decl_data(t,v,offset,1); if (inmode) offset = list4(CAST,offset,t,type); } else { @@ -1535,7 +1542,7 @@ } static int -decl_data_field(int type,NMTBL *n,int offset) +decl_data_field(int type,int v,int offset) { int t1,t2,period=0; int foffset; @@ -1557,7 +1564,7 @@ // .__tcp_lhash_lock = (rwlock_t) { }, // We cannot distinguish this case and cascading comma here. // Do it more upper syntactical node; - if ((offset = decl_data_1(type,n,offset))) { + if ((offset = decl_data_1(type,v,offset))) { return offset; } checksym(RPAR); @@ -1576,10 +1583,10 @@ getsym(0); if (sym==ASS) { if (inmode) { - int offset1 = decl_data(t2,n,0,0); + int offset1 = decl_data(t2,v,0,0); offset = list4(DECL_DATA_FIELD,offset,offset1,(int)nptr1); } else { - decl_data(t2,n,offset+foffset,0); + decl_data(t2,v,offset+foffset,0); } } else error(INERR); @@ -1593,10 +1600,10 @@ if(!t1) break; // empty field case (it can happen...) // next decl_data must skip getsym if (inmode) { - int offset1 = decl_data(car(t1),n,0,1); /* alignment? */ + int offset1 = decl_data(car(t1),v,0,1); /* alignment? */ offset = list4(DECL_DATA_LIST,offset,offset1,car(t1)); } else { - offset = decl_data(car(t1),n,offset,1); /* alignment? */ + offset = decl_data(car(t1),v,offset,1); /* alignment? */ } t1 = cadr(t1); } @@ -1614,9 +1621,9 @@ // should be in mc-codegen.c static void -local_struct_static(NMTBL *n) +local_struct_static(int v) { - NMTBL *nptr0; + NMTBL *nptr0,*n = (NMTBL*)caddr(v); int sz = size(type),offset=0; int smode = mode; // uninitialized part should be 0. @@ -1632,10 +1639,11 @@ nptr0=new_static_name("__lstruct",'_'); def(nptr0,0); mode=smode; + v = list3(GVAR,0,(int)nptr0); //nptr0->next = local_static_list; local_static_list = nptr0; //nptr0->sc = STATIC; //nptr0->ty = t = type; - decl_data_field(type,nptr0,offset); + decl_data_field(type,v,offset); // do struct assignment before flushed assignment expression init_vars = list2( list4(STASS, @@ -1644,10 +1652,10 @@ list3(ADD,list3(IVAR,n->dsp,0),list2(CONST,offset)): list3(IVAR,n->dsp,0)): list3(LVAR,n->dsp+offset,0), - list3(RSTRUCT,list3(GVAR,0,(int)nptr0),sz),sz), + list3(RSTRUCT,v,sz),sz), init_vars); mode=STADECL; - flush_delayed_decl_data(nptr0); + flush_delayed_decl_data(v); mode = smode; } #endif @@ -1672,10 +1680,11 @@ */ static int -decl_data(int t, NMTBL *n,int offset,int skip) +decl_data(int t, int v,int offset,int skip) { int t0,t1=0,e,i,mode_save,lc=0; int offset0; + NMTBL *n = (NMTBL*)caddr(v); emit_init_vars(); conv->decl_data_(); @@ -1713,7 +1722,7 @@ // assign_data call rvalue, this is a duplicate rvalue call (correct?) if (!scalar(type) && (type>0 && (car(type)!=ADDRESS && car(type)!=ARRAY))) e = correct_type(rvalue(e),t0); - offset = assign_data(e,t,n,offset); + offset = assign_data(e,t,v,offset); type=t; return offset; } else if (t0==FLOAT||t0==DOUBLE||t0==LONGLONG||t0==ULONGLONG) { @@ -1727,7 +1736,7 @@ return offset; } e = correct_type(rvalue(e),t0); - offset = assign_data(e,t,n,offset); + offset = assign_data(e,t,v,offset); type=t; return offset; } else if (t0>0 && (t1 = car(t0)) && t1==ARRAY) { @@ -1742,12 +1751,12 @@ for(;;) { if (sym!=RC) { if (inmode) { - int e=decl_data(t1,n,0,0); + int e=decl_data(t1,v,0,0); offset1 = list4(DECL_DATA_ARRAY,offset1,e,t1); } else { offset0 = offset; /* array of some thing */ - offset=decl_data(t1,n,offset,0); + offset=decl_data(t1,v,offset,0); if (offset0!=offset) i++; } } @@ -1770,7 +1779,7 @@ // this check is sligtly odd (fix me) // error(INERR); } else if (!decl_str_init) - assign_data(list2(CONST,caddr(t)-i),EMPTY,n,offset); + assign_data(list2(CONST,caddr(t)-i),EMPTY,v,offset); } getsym(0); return offset; @@ -1786,7 +1795,7 @@ offset = list3(DECL_DATA,e,type); return offset; } - offset=assign_data(e,list3(ARRAY,CHAR,size(type)),n,offset); + offset=assign_data(e,list3(ARRAY,CHAR,size(type)),v,offset); if (caddr(t0)==0) { /* size not defined */ caddr(t0)=size(type); /* define array size */ } else if (caddr(t0)!=size(type)) { /* size match? */ @@ -1811,7 +1820,7 @@ type=t; return offset; } - offset = assign_data(e,t,n,offset); + offset = assign_data(e,t,v,offset); type=t; return offset; } else if (t1==STRUCT||t1==UNION) { @@ -1822,17 +1831,17 @@ #if LOCAL_STRUCT_INIT_STATIC if(mode==LDECL && !inmode) { if (offset) error(-1); - local_struct_static(n); + local_struct_static(v); // change mode to STADECL // decl_data_field(t,n,offset) is called inside; } else #endif if (inmode) { - int offset1=decl_data_field(t,n,0); + int offset1=decl_data_field(t,v,0); offset1 = reverse0(offset1); offset=list3(DECL_DATA,offset1,t); } else { - offset=decl_data_field(t,n,offset); + offset=decl_data_field(t,v,offset); } conv->decl_data_end_(); conv->rc_(); if (lc) { @@ -2015,7 +2024,7 @@ code_decl(NMTBL *n) { int t,arglist; - int sinmode; + int sinmode = 0; int arg_disp; if (parse_mode) { @@ -4115,7 +4124,8 @@ // initializer // q->lock = (spinlock_t) { }; conv->lc_(); - if (mode==GDECL) { + if (mode==GDECL||inmode) { + int e2,e3; smode = mode; type = t; nptr0=new_static_name("__lstruct",'_'); @@ -4123,14 +4133,17 @@ def(nptr0,0); e1 = size(type); mode = smode; - decl_data_field(type,nptr0,0); - e1 = list3(RSTRUCT,list3(GVAR,0,(int)nptr0),e1); - } else if (inmode) { - e1 = decl_data_field(t,&null_nptr,0); - e1 = reverse0(e1); - e1 = list3(DECL_DATA,e1,t); - e1 = list4(CAST,e1,t,t); // only for cast syntax + e2 = list3(GVAR,0,(int)nptr0); + e3 = decl_data_field(type,e2,0); + if (!inmode) { + e1 = list3(RSTRUCT,e2,e1); + } else { + e1 = reverse0(e3); + e1 = list3(DECL_DATA,e1,t); + e1 = list4(CAST,e1,t,t); // only for cast syntax + } } else { + int e2; nptr0 = get_nptr(); // should be freed in a scope? // in case of inline, we cannot nptr0->nm = ""; @@ -4139,13 +4152,13 @@ type = nptr0->ty = t; e1 = size(type); def(nptr0,0); + e2 = list3(nptr0->sc,nptr0->dsp,(int)nptr0); #if LOCAL_STRUCT_INIT_STATIC - local_struct_static(nptr0); + local_struct_static(e2); #else - decl_data_field(type,nptr0,0); + decl_data_field(type,e2,0); #endif - e1 = list3(RSTRUCT,list3( - nptr0->sc,nptr0->dsp,(int)nptr0),e1); + e1 = list3(RSTRUCT,e2,e1); } if (init_vars && mode!=LDECL) { emit_init_vars();