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
282 func_args : 5,282 func_args : 5,
283 mode : 4,283 mode : 4,
284 weak : 1,284 weak : 1,
285 fill : 11;285 resize : 1,
286 fill : 10;
286 struct Section *section;287 struct Section *section;
287} AttributeDef;288} AttributeDef;
288 289
@@ -293,6 +294,7 @@ typedef struct AttributeDef { · 4 unmodified lines
293#define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)294#define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
294#define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)295#define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
295#define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)296#define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
297#define ARRAY_RESIZE(r) (((AttributeDef*)&(r))->resize)
296#define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)298#define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
297#define INT_ATTR(ad) (*(int*)(ad))299#define INT_ATTR(ad) (*(int*)(ad))
298 300
tccgen.c+18−4
@@ -1944,7 +1944,7 @@ ST_FUNC int type_size(CType *type, int *a) · 1943 unmodified lines
1944 if (bt == VT_STRUCT) {1944 if (bt == VT_STRUCT) {
1945 /* struct/union */1945 /* struct/union */
1946 s = type->ref;1946 s = type->ref;
1947 *a = s->r;1947 *a = s->r & 0xffffff;
1948 return s->c;1948 return s->c;
1949 } else if (bt == VT_PTR) {1949 } else if (bt == VT_PTR) {
1950 if (type->t & VT_ARRAY) {1950 if (type->t & VT_ARRAY) {
@@ -2586,7 +2586,7 @@ static void parse_attribute(AttributeDef *ad) · 635 unmodified lines
2586static void struct_decl(CType *type, int u)2586static void struct_decl(CType *type, int u)
2587{2587{
2588 int a, v, size, align, maxalign, c, offset;2588 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;
2590 Sym *s, *ss, *ass, **ps;2590 Sym *s, *ss, *ass, **ps;
2591 AttributeDef ad;2591 AttributeDef ad;
2592 CType type1, btype;2592 CType type1, btype;
@@ -2647,6 +2647,7 @@ static void struct_decl(CType *type, int u) · 54 unmodified lines
2647 }2647 }
2648 skip('}');2648 skip('}');
2649 } else {2649 } else {
2650 resize = 0;
2650 maxalign = 1;2651 maxalign = 1;
2651 ps = &s->next;2652 ps = &s->next;
2652 prevbt = VT_INT;2653 prevbt = VT_INT;
@@ -2737,6 +2738,8 @@ static void struct_decl(CType *type, int u) · 84 unmodified lines
2737 offset = c;2738 offset = c;
2738 if (size > 0)2739 if (size > 0)
2739 c += size;2740 c += size;
2741 if (size < 0)
2742 resize = 1;
2740 } else {2743 } else {
2741 offset = 0;2744 offset = 0;
2742 if (size > c)2745 if (size > c)
@@ -2777,7 +2780,7 @@ static void struct_decl(CType *type, int u) · 34 unmodified lines
2777 skip('}');2780 skip('}');
2778 /* store size and alignment */2781 /* store size and alignment */
2779 s->c = (c + maxalign - 1) & -maxalign; 2782 s->c = (c + maxalign - 1) & -maxalign;
2780 s->r = maxalign;2783 s->r = maxalign | (resize ? (1 << 31) : 0);
2781 }2784 }
2782 }2785 }
2783}2786}
@@ -3113,6 +3116,8 @@ static void post_type(CType *type, AttributeDef *ad) · 329 unmodified lines
3113 /* we push a anonymous symbol which will contain the array3116 /* we push a anonymous symbol which will contain the array
3114 element type */3117 element type */
3115 s = sym_push(SYM_FIELD, type, 0, n);3118 s = sym_push(SYM_FIELD, type, 0, n);
3119 if (n < 0)
3120 ARRAY_RESIZE(s->r) = 1;
3116 type->t = t1 | VT_ARRAY | VT_PTR;3121 type->t = t1 | VT_ARRAY | VT_PTR;
3117 type->ref = s;3122 type->ref = s;
3118 }3123 }
@@ -4845,6 +4850,8 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 1726 unmodified lines
4845 }4850 }
4846 } else {4851 } else {
4847 index = 0;4852 index = 0;
4853 if (ARRAY_RESIZE(s->r))
4854 n = -1;
4848 while (tok != '}') {4855 while (tok != '}') {
4849 decl_designator(type, sec, c, &index, NULL, size_only);4856 decl_designator(type, sec, c, &index, NULL, size_only);
4850 if (n >= 0 && index >= n)4857 if (n >= 0 && index >= n)
@@ -4918,6 +4925,8 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 67 unmodified lines
4918 array_length = 0;4925 array_length = 0;
4919 index = 0;4926 index = 0;
4920 n = s->c;4927 n = s->c;
4928 if (s->r & (1<<31))
4929 n = -1;
4921 while (tok != '}') {4930 while (tok != '}') {
4922 decl_designator(type, sec, c, NULL, &f, size_only);4931 decl_designator(type, sec, c, NULL, &f, size_only);
4923 index = f->c;4932 index = f->c;
@@ -4954,7 +4963,7 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 30 unmodified lines
4954 skip(',');4963 skip(',');
4955 }4964 }
4956 /* put zeros at the end */4965 /* put zeros at the end */
4957 if (!size_only && array_length < n) {4966 if (!size_only && n >= 0 && array_length < n) {
4958 init_putz(type, sec, c + array_length, 4967 init_putz(type, sec, c + array_length,
4959 n - array_length);4968 n - array_length);
4960 }4969 }
@@ -4964,6 +4973,8 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 3 unmodified lines
4964 skip(')');4973 skip(')');
4965 par_count--;4974 par_count--;
4966 }4975 }
4976 if (n < 0)
4977 s->c = array_length;
4967 } else if (tok == '{') {4978 } else if (tok == '{') {
4968 next();4979 next();
4969 decl_initializer(type, sec, c, first, size_only);4980 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
5011 TokenString init_str;5022 TokenString init_str;
5012 Section *sec;5023 Section *sec;
5013 5024
5025 /* resize the struct */
5026 if ((type->t & VT_BTYPE) == VT_STRUCT && (type->ref->r & (1<<31)) != 0)
5027 type->ref->c = -1;
5014 size = type_size(type, &align);5028 size = type_size(type, &align);
5015 /* If unknown size, we must evaluate it before5029 /* If unknown size, we must evaluate it before
5016 evaluating initializers because5030 evaluating initializers because
tests/tcctest.c+43−26
@@ -827,27 +827,6 @@ struct aligntest4 { · 826 unmodified lines
827 double a[0];827 double a[0];
828};828};
829 829
830struct complexinit0 {
831 int a;
832 int b;
833};
834
835struct complexinit {
836 int a;
837 struct complexinit0 *b;
838};
839
840const 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
851void struct_test()830void struct_test()
852{831{
853 struct1 *s;832 struct1 *s;
@@ -877,11 +856,6 @@ void struct_test() · 23 unmodified lines
877 printf("st2: %d %d %d\n",856 printf("st2: %d %d %d\n",
878 s->f1, s->f2, s->f3);857 s->f1, s->f2, s->f3);
879 printf("str_addr=%x\n", (int)st1.str - (int)&st1.f1);858 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);
885 859
886 /* align / size tests */860 /* align / size tests */
887 printf("aligntest1 sizeof=%d alignof=%d\n",861 printf("aligntest1 sizeof=%d alignof=%d\n",
@@ -1288,6 +1262,42 @@ int sinit18[10] = { · 400 unmodified lines
1288 [8] = 10,1262 [8] = 10,
1289};1263};
1290 1264
1265struct complexinit0 {
1266 int a;
1267 int b;
1268};
1269
1270struct complexinit {
1271 int a;
1272 struct complexinit0 *b;
1273};
1274
1275const 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
1286struct complexinit2 {
1287 int a;
1288 int b[];
1289};
1290
1291struct complexinit2 cix21 = {
1292 .a = 3000,
1293 .b = { 3001, 3002, 3003 }
1294};
1295
1296struct complexinit2 cix22 = {
1297 .a = 4000,
1298 .b = { 4001, 4002, 4003, 4004, 4005, 4006 }
1299};
1300
1291void init_test(void)1301void init_test(void)
1292{1302{
1293 int linit1 = 2;1303 int linit1 = 2;
@@ -1380,6 +1390,13 @@ void init_test(void) · 86 unmodified lines
1380 for(i=0;i<10;i++)1390 for(i=0;i<10;i++)
1381 printf("%x ", sinit18[i]);1391 printf("%x ", sinit18[i]);
1382 printf("\n");1392 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]);
1383}1400}
1384 1401
1385 1402

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