History

fix another static struct init issue (arrays with unknown size at end)

Jaroslav Kysela committed 15 years ago

Commit ab73c9b1 parent dbefae53 files · +64 −31Browse files

Unified · Split

3 files
  1. tcc.h +3 −1
  2. tccgen.c +18 −4
  3. tests/tcctest.c +43 −26
tcc.h+3−1
@@ -282,7 +282,8 @@ typedef struct AttributeDef { · 281 unmodified lines
282282 func_args : 5,
283283 mode : 4,
284284 weak : 1,
285− fill : 11;
285+ resize : 1,
286+ fill : 10;
286287 struct Section *section;
287288 } AttributeDef;
288289
@@ -293,6 +294,7 @@ typedef struct AttributeDef { · 4 unmodified lines
293294 #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
294295 #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
295296 #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
297+#define ARRAY_RESIZE(r) (((AttributeDef*)&(r))->resize)
296298 #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
297299 #define INT_ATTR(ad) (*(int*)(ad))
298300
tccgen.c+18−4
@@ -1944,7 +1944,7 @@ ST_FUNC int type_size(CType *type, int *a) · 1943 unmodified lines
19441944 if (bt == VT_STRUCT) {
19451945 /* struct/union */
19461946 s = type->ref;
1947− *a = s->r;
1947+ *a = s->r & 0xffffff;
19481948 return s->c;
19491949 } else if (bt == VT_PTR) {
19501950 if (type->t & VT_ARRAY) {
@@ -2586,7 +2586,7 @@ static void parse_attribute(AttributeDef *ad) · 635 unmodified lines
25862586 static void struct_decl(CType *type, int u)
25872587 {
25882588 int a, v, size, align, maxalign, c, offset;
2589− int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2589+ int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt, resize;
25902590 Sym *s, *ss, *ass, **ps;
25912591 AttributeDef ad;
25922592 CType type1, btype;
@@ -2647,6 +2647,7 @@ static void struct_decl(CType *type, int u) · 54 unmodified lines
26472647 }
26482648 skip('}');
26492649 } else {
2650+ resize = 0;
26502651 maxalign = 1;
26512652 ps = &s->next;
26522653 prevbt = VT_INT;
@@ -2737,6 +2738,8 @@ static void struct_decl(CType *type, int u) · 84 unmodified lines
27372738 offset = c;
27382739 if (size > 0)
27392740 c += size;
2741+ if (size < 0)
2742+ resize = 1;
27402743 } else {
27412744 offset = 0;
27422745 if (size > c)
@@ -2777,7 +2780,7 @@ static void struct_decl(CType *type, int u) · 34 unmodified lines
27772780 skip('}');
27782781 /* store size and alignment */
27792782 s->c = (c + maxalign - 1) & -maxalign;
2780− s->r = maxalign;
2783+ s->r = maxalign | (resize ? (1 << 31) : 0);
27812784 }
27822785 }
27832786 }
@@ -3113,6 +3116,8 @@ static void post_type(CType *type, AttributeDef *ad) · 329 unmodified lines
31133116 /* we push a anonymous symbol which will contain the array
31143117 element type */
31153118 s = sym_push(SYM_FIELD, type, 0, n);
3119+ if (n < 0)
3120+ ARRAY_RESIZE(s->r) = 1;
31163121 type->t = t1 | VT_ARRAY | VT_PTR;
31173122 type->ref = s;
31183123 }
@@ -4845,6 +4850,8 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 1726 unmodified lines
48454850 }
48464851 } else {
48474852 index = 0;
4853+ if (ARRAY_RESIZE(s->r))
4854+ n = -1;
48484855 while (tok != '}') {
48494856 decl_designator(type, sec, c, &index, NULL, size_only);
48504857 if (n >= 0 && index >= n)
@@ -4918,6 +4925,8 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 67 unmodified lines
49184925 array_length = 0;
49194926 index = 0;
49204927 n = s->c;
4928+ if (s->r & (1<<31))
4929+ n = -1;
49214930 while (tok != '}') {
49224931 decl_designator(type, sec, c, NULL, &f, size_only);
49234932 index = f->c;
@@ -4954,7 +4963,7 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 30 unmodified lines
49544963 skip(',');
49554964 }
49564965 /* put zeros at the end */
4957− if (!size_only && array_length < n) {
4966+ if (!size_only && n >= 0 && array_length < n) {
49584967 init_putz(type, sec, c + array_length,
49594968 n - array_length);
49604969 }
@@ -4964,6 +4973,8 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 3 unmodified lines
49644973 skip(')');
49654974 par_count--;
49664975 }
4976+ if (n < 0)
4977+ s->c = array_length;
49674978 } else if (tok == '{') {
49684979 next();
49694980 decl_initializer(type, sec, c, first, size_only);
@@ -5011,6 +5022,9 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 41 unmodified lines
50115022 TokenString init_str;
50125023 Section *sec;
50135024
5025+ /* resize the struct */
5026+ if ((type->t & VT_BTYPE) == VT_STRUCT && (type->ref->r & (1<<31)) != 0)
5027+ type->ref->c = -1;
50145028 size = type_size(type, &align);
50155029 /* If unknown size, we must evaluate it before
50165030 evaluating initializers because
tests/tcctest.c+43−26
@@ -827,27 +827,6 @@ struct aligntest4 { · 826 unmodified lines
827827 double a[0];
828828 };
829829
830−struct complexinit0 {
831− int a;
832− int b;
833−};
834−
835−struct complexinit {
836− int a;
837− struct complexinit0 *b;
838−};
839−
840−const static struct complexinit cix[] = {
841− [0] = {
842− .a = 0xfefa,
843− .b = (const struct complexinit0[]) {
844− { 0x80, 0x81 },
845− { 0x82, 0x83 },
846− {}
847− }
848− }
849−};
850−
851830 void struct_test()
852831 {
853832 struct1 *s;
@@ -877,11 +856,6 @@ void struct_test() · 23 unmodified lines
877856 printf("st2: %d %d %d\n",
878857 s->f1, s->f2, s->f3);
879858 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);
880− printf("cix: %d %d %d %d %d %d %d\n",
881− cix[0].a,
882− cix[0].b[0].a, cix[0].b[0].b,
883− cix[0].b[1].a, cix[0].b[1].b,
884− cix[0].b[2].a, cix[0].b[2].b);
885859
886860 /* align / size tests */
887861 printf("aligntest1 sizeof=%d alignof=%d\n",
@@ -1288,6 +1262,42 @@ int sinit18[10] = { · 400 unmodified lines
12881262 [8] = 10,
12891263 };
12901264
1265+struct complexinit0 {
1266+ int a;
1267+ int b;
1268+};
1269+
1270+struct complexinit {
1271+ int a;
1272+ struct complexinit0 *b;
1273+};
1274+
1275+const static struct complexinit cix[] = {
1276+ [0] = {
1277+ .a = 2000,
1278+ .b = (const struct complexinit0[]) {
1279+ { 2001, 2002 },
1280+ { 2003, 2003 },
1281+ {}
1282+ }
1283+ }
1284+};
1285+
1286+struct complexinit2 {
1287+ int a;
1288+ int b[];
1289+};
1290+
1291+struct complexinit2 cix21 = {
1292+ .a = 3000,
1293+ .b = { 3001, 3002, 3003 }
1294+};
1295+
1296+struct complexinit2 cix22 = {
1297+ .a = 4000,
1298+ .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1299+};
1300+
12911301 void init_test(void)
12921302 {
12931303 int linit1 = 2;
@@ -1380,6 +1390,13 @@ void init_test(void) · 86 unmodified lines
13801390 for(i=0;i<10;i++)
13811391 printf("%x ", sinit18[i]);
13821392 printf("\n");
1393+ /* complex init check */
1394+ printf("cix: %d %d %d %d %d %d %d\n",
1395+ cix[0].a,
1396+ cix[0].b[0].a, cix[0].b[0].b,
1397+ cix[0].b[1].a, cix[0].b[1].b,
1398+ cix[0].b[2].a, cix[0].b[2].b);
1399+ printf("cix2: %d %d\n", cix21.b[2], cix22.b[5]);
13831400 }
13841401
13851402

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t