Mercurial > hg > CbC > old > device
comparison mc-parse.c @ 897:66f32d0af4d2
local struct initialization target management in decl
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 07 Apr 2014 10:27:12 +0900 |
parents | d712ee10feb7 |
children | c073495301ae |
comparison
equal
deleted
inserted
replaced
896:d712ee10feb7 | 897:66f32d0af4d2 |
---|---|
49 #endif | 49 #endif |
50 #if FLOAT_CODE | 50 #if FLOAT_CODE |
51 extern double strtod(const char *nptr, char **endptr); | 51 extern double strtod(const char *nptr, char **endptr); |
52 #endif | 52 #endif |
53 | 53 |
54 int parse_mode = 0; // generate parse tree for all code | 54 int parse_mode = 1; // generate parse tree for all code |
55 | 55 |
56 static int HEAP_REPORT = 0; | 56 static int HEAP_REPORT = 0; |
57 static int lfree_type_limit; // debugging purpose | 57 static int lfree_type_limit; // debugging purpose |
58 // list of local declared type have to be | 58 // list of local declared type have to be |
59 // keep | 59 // keep |
185 static void set_converter(char *s); | 185 static void set_converter(char *s); |
186 static int escape(void); | 186 static int escape(void); |
187 static void statement(int); | 187 static void statement(int); |
188 static int typename(void); | 188 static int typename(void); |
189 static int decl_data_field(int type,int v,int offset); | 189 static int decl_data_field(int type,int v,int offset); |
190 static int decl_data_def(NMTBL *n, int t, int v,int offset,int skip); | |
190 static int decl_data(int t, int v,int offset,int skip); | 191 static int decl_data(int t, int v,int offset,int skip); |
191 static int typeid(int s); | 192 static int typeid(int s); |
192 extern NMTBL * get_name_from_chptr(); | 193 extern NMTBL * get_name_from_chptr(); |
193 static NMTBL * hash_search(char *name,struct cheap *scheap,int len,unsigned int hash,int mode); | 194 static NMTBL * hash_search(char *name,struct cheap *scheap,int len,unsigned int hash,int mode); |
194 extern NMTBL * make_local_scope(NMTBL *nlist,NMTBL *nptr1,int sc); | 195 extern NMTBL * make_local_scope(NMTBL *nlist,NMTBL *nptr1,int sc); |
965 conv->return_type_(type,n,sd); | 966 conv->return_type_(type,n,sd); |
966 n = def(n,ctmode); | 967 n = def(n,ctmode); |
967 v = list3n(n->sc,n->dsp,n); | 968 v = list3n(n->sc,n->dsp,n); |
968 if (sym==ASS && n!=&null_nptr) { | 969 if (sym==ASS && n!=&null_nptr) { |
969 conv->op_(sym); | 970 conv->op_(sym); |
970 local_nptr = n; local_struct_offset = 0; | 971 init = decl_data_def(n, type,v,0,0); data_closing(v); |
971 init = decl_data(type,v,0,0); data_closing(v); | |
972 } | 972 } |
973 if (inmode && (mode==LDECL||mode==LLDECL||mode==STADECL)) { | 973 if (inmode && (mode==LDECL||mode==LLDECL||mode==STADECL)) { |
974 parse = list5n(ST_DECL,parse,list3(mode,stmode,ctmode),init,n); | 974 parse = list5n(ST_DECL,parse,list3(mode,stmode,ctmode),init,n); |
975 } | 975 } |
976 while(sym==COMMA) { | 976 while(sym==COMMA) { |
988 conv->return_type_(type,n,1); | 988 conv->return_type_(type,n,1); |
989 def(n,ctmode); | 989 def(n,ctmode); |
990 v = list3n(n->sc,n->dsp,n); | 990 v = list3n(n->sc,n->dsp,n); |
991 if (sym==ASS && n!=&null_nptr) { | 991 if (sym==ASS && n!=&null_nptr) { |
992 conv->op_(sym); | 992 conv->op_(sym); |
993 local_nptr = n; local_struct_offset = 0; | 993 init = decl_data_def(n, type,v,0,0);data_closing(v); |
994 init = decl_data(type,v,0,0);data_closing(v); | |
995 } | 994 } |
996 if (inmode && mode==LDECL) { | 995 if (inmode && mode==LDECL) { |
997 parse = list5n(ST_DECL,parse, | 996 parse = list5n(ST_DECL,parse, |
998 list3(mode,stmode,ctmode),init,n); | 997 list3(mode,stmode,ctmode),init,n); |
999 } | 998 } |
1724 data initializer (array or struct) // data type for field | 1723 data initializer (array or struct) // data type for field |
1725 list3(DECL_DATA,value,type); | 1724 list3(DECL_DATA,value,type); |
1726 | 1725 |
1727 cast があるかどうかの区別 | 1726 cast があるかどうかの区別 |
1728 */ | 1727 */ |
1728 | |
1729 static int | |
1730 decl_data_def(NMTBL *n, int t, int v,int offset,int skip) | |
1731 { | |
1732 NMTBL *slocal_nptr = local_nptr; | |
1733 local_nptr = n; | |
1734 int slocal_struct_offset = local_struct_offset ; | |
1735 int e = decl_data(t,v,offset,skip); | |
1736 local_struct_offset = slocal_struct_offset ; | |
1737 local_nptr = slocal_nptr; | |
1738 return e; | |
1739 } | |
1729 | 1740 |
1730 static int | 1741 static int |
1731 decl_data(int t, int v,int offset,int skip) | 1742 decl_data(int t, int v,int offset,int skip) |
1732 { | 1743 { |
1733 int t0,t1=0,e,i,mode_save,lc=0; | 1744 int t0,t1=0,e,i,mode_save,lc=0; |
4143 nptr0->attr = 0; | 4154 nptr0->attr = 0; |
4144 type = nptr0->ty = t; | 4155 type = nptr0->ty = t; |
4145 int local_offset; | 4156 int local_offset; |
4146 if (mode==GDECL||inmode) { | 4157 if (mode==GDECL||inmode) { |
4147 int e2,e3; | 4158 int e2,e3; |
4148 e1 = size(type); | 4159 int sz = size(type); |
4149 e2 = list3n(GVAR,0,nptr0); | 4160 e2 = list3n(GVAR,0,nptr0); |
4150 e3 = decl_data_field(type,e2,0); | 4161 e3 = decl_data_field(type,e2,0); |
4151 if (!inmode) { | 4162 if (!inmode) { |
4152 e1 = list3(RSTRUCT,e2,e1); | 4163 e1 = list3(RSTRUCT,e2,sz); |
4153 } else { | 4164 } else { |
4154 e1 = reverse0(e3); | 4165 e1 = reverse0(e3); |
4155 e1 = list3(DECL_DATA,e1,t); | 4166 e1 = list3(DECL_DATA,e1,t); |
4156 e1 = list4(CAST,e1,t,t); // only for cast syntax | 4167 e1 = list4(CAST,e1,t,t); // only for cast syntax |
4157 } | 4168 } |
4158 } else { | 4169 } else { |
4159 if (!local_nptr) { | 4170 if (!local_nptr) { |
4171 // struct initialization for this variable | |
4172 // (struct hoge) { .fuga = 5 } | |
4160 local_nptr = nptr0; | 4173 local_nptr = nptr0; |
4161 int smode = mode; mode = LDECL; | 4174 int smode = mode; mode = LDECL; |
4162 def(nptr0,0); | 4175 def(nptr0,0); |
4163 mode = smode; | 4176 mode = smode; |
4164 local_struct_offset = 0; | 4177 local_struct_offset = 0; |
4168 local_struct_static(e1); | 4181 local_struct_static(e1); |
4169 #else | 4182 #else |
4170 local_offset = local_struct_offset; | 4183 local_offset = local_struct_offset; |
4171 int offset = decl_data_field(type,e1,local_struct_offset); | 4184 int offset = decl_data_field(type,e1,local_struct_offset); |
4172 if (offset==local_struct_offset) { | 4185 if (offset==local_struct_offset) { |
4186 // empty case, let's zero clear now | |
4173 int sz = size(type); | 4187 int sz = size(type); |
4174 zfill(e1,offset,sz); | 4188 zfill(e1,offset,sz); |
4175 local_struct_offset = offset + sz; | 4189 local_struct_offset = offset + sz; |
4176 } | 4190 } else |
4191 local_struct_offset = offset; | |
4177 #endif | 4192 #endif |
4178 } | 4193 } |
4179 if (mode==STAT && decl_str_init) gen_delayed_decl_data(e1,local_offset); | 4194 if (mode==STAT && decl_str_init) gen_delayed_decl_data(e1,local_offset); |
4180 if (init_vars && mode!=LDECL) { | 4195 if (init_vars && mode!=LDECL) { |
4181 emit_init_vars(); | 4196 emit_init_vars(); |
4197 } | |
4198 if (nptr0==local_nptr) { | |
4199 // I have created this, sanitize | |
4200 local_nptr = 0; | |
4201 local_struct_offset = 0; | |
4182 } | 4202 } |
4183 conv->rc_(); | 4203 conv->rc_(); |
4184 checksym(RC); | 4204 checksym(RC); |
4185 type = t; | 4205 type = t; |
4186 return e1; | 4206 return e1; |