History

tccgen: multi-dimensional vla: bug fixes

fixes these cases: ptr - ptr (-> ptrdiff_t) ptr +/- num (-> ptr) sizeof (ptr+/-num) (-> size_t) Also some cleanups

grischka committed 4 years ago

Commit 0c6adcb1 parent ec5d9422 files · +31 −47Browse files

Unified · Split

2 files
  1. tccgen.c +22 −46
  2. tests/tcctest.c +9 −1
tccgen.c+22−46
@@ -234,7 +234,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has · 233 unmodified lines
234234 static void decl(int l);
235235 static int decl0(int l, int is_for_loop_init, Sym *);
236236 static void expr_eq(void);
237−static void vla_runtime_type_size(CType *type, int *a);
237+static void vpush_type_size(CType *type, int *a);
238238 static int is_compatible_unqualified_types(CType *type1, CType *type2);
239239 static inline int64_t expr_const64(void);
240240 static void vpush64(int ty, unsigned long long v);
@@ -3173,7 +3173,7 @@ static void type_to_str(char *buf, int buf_size, · 2932 unmodified lines
31733173 goto no_var;
31743174 case VT_PTR:
31753175 s = type->ref;
3176− if (t & VT_ARRAY) {
3176+ if (t & (VT_ARRAY|VT_VLA)) {
31773177 if (varstr && '*' == *varstr)
31783178 snprintf(buf1, sizeof(buf1), "(%s)[%d]", varstr, s->c);
31793179 else
@@ -3220,12 +3220,6 @@ static int pointed_size(CType *type) · 40 unmodified lines
32203220 return type_size(pointed_type(type), &align);
32213221 }
32223222
3223−static void vla_runtime_pointed_size(CType *type)
3224−{
3225− int align;
3226− vla_runtime_type_size(pointed_type(type), &align);
3227−}
3228−
32293223 static inline int is_null_pointer(SValue *p)
32303224 {
32313225 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
@@ -3436,7 +3430,7 @@ static int combine_types(CType *dest, SValue *op1, SValue *op2, int op) · 204 unmodified lines
34363430 /* generic gen_op: handles types problems */
34373431 ST_FUNC void gen_op(int op)
34383432 {
3439− int u, t1, t2, bt1, bt2, t;
3433+ int t1, t2, bt1, bt2, t;
34403434 CType type1, combtype;
34413435
34423436 redo:
@@ -3463,17 +3457,14 @@ redo: · 20 unmodified lines
34633457 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
34643458 /* at least one operand is a pointer */
34653459 /* relational op: must be both pointers */
3460+ int align;
34663461 if (TOK_ISCOND(op))
34673462 goto std_op;
34683463 /* if both pointers, then it must be the '-' op */
34693464 if (bt1 == VT_PTR && bt2 == VT_PTR) {
34703465 if (op != '-')
34713466 tcc_error("cannot use pointers here");
3472− if (vtop[-1].type.t & VT_VLA) {
3473− vla_runtime_pointed_size(&vtop[-1].type);
3474− } else {
3475− vpushi(pointed_size(&vtop[-1].type));
3476− }
3467+ vpush_type_size(pointed_type(&vtop[-1].type), &align);
34773468 vrott(3);
34783469 gen_opic(op);
34793470 vtop->type.t = VT_PTRDIFF_T;
@@ -3494,19 +3485,7 @@ redo: · 14 unmodified lines
34943485 gen_cast_s(VT_INT);
34953486 #endif
34963487 type1 = vtop[-1].type;
3497− if (vtop[-1].type.ref->type.t & VT_VLA)
3498− vla_runtime_pointed_size(&vtop[-1].type);
3499− else {
3500− u = pointed_size(&vtop[-1].type);
3501− if (u < 0)
3502− tcc_error("unknown array element size");
3503−#if PTR_SIZE == 8
3504− vpushll(u);
3505−#else
3506− /* XXX: cast to int ? (long long case) */
3507− vpushi(u);
3508−#endif
3509− }
3488+ vpush_type_size(pointed_type(&vtop[-1].type), &align);
35103489 gen_op('*');
35113490 #ifdef CONFIG_TCC_BCHECK
35123491 if (tcc_state->do_bounds_check && !const_wanted) {
@@ -3523,7 +3502,7 @@ redo: · 10 unmodified lines
35233502 {
35243503 gen_opic(op);
35253504 }
3526− type1.t &= ~VT_ARRAY;
3505+ type1.t &= ~(VT_ARRAY|VT_VLA);
35273506 /* put again type if gen_opic() swaped operands */
35283507 vtop->type = type1;
35293508 }
@@ -3957,13 +3936,16 @@ ST_FUNC int type_size(CType *type, int *a) · 427 unmodified lines
39573936
39583937 /* push type size as known at runtime time on top of value stack. Put
39593938 alignment at 'a' */
3960−ST_FUNC void vla_runtime_type_size(CType *type, int *a)
3939+static void vpush_type_size(CType *type, int *a)
39613940 {
39623941 if (type->t & VT_VLA) {
39633942 type_size(&type->ref->type, a);
39643943 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
39653944 } else {
3966− vpushi(type_size(type, a));
3945+ int size = type_size(type, a);
3946+ if (size < 0)
3947+ tcc_error("unknown type size");
3948+ vpushi(size);
39673949 }
39683950 }
39693951
@@ -5501,7 +5483,7 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td) · 1531 unmodified lines
55015483 loc &= -align;
55025484 n = loc;
55035485
5504− vla_runtime_type_size(type, &align);
5486+ vpush_type_size(type, &align);
55055487 gen_op('*');
55065488 vset(&int_type, VT_LOCAL|VT_LVAL, n);
55075489 vswap();
@@ -6050,24 +6032,18 @@ ST_FUNC void unary(void) · 542 unmodified lines
60506032 next();
60516033 in_sizeof++;
60526034 expr_type(&type, unary); /* Perform a in_sizeof = 0; */
6053− s = NULL;
6054− if (vtop[1].r & VT_SYM)
6055− s = vtop[1].sym; /* hack: accessing previous vtop */
6056− size = type_size(&type, &align);
6057− if (s && s->a.aligned)
6058− align = 1 << (s->a.aligned - 1);
60596035 if (t == TOK_SIZEOF) {
6060− if (!(type.t & VT_VLA)) {
6061− if (size < 0)
6062− tcc_error("sizeof applied to an incomplete type");
6063− vpushs(size);
6064− } else {
6065− vla_runtime_type_size(&type, &align);
6066− }
6036+ vpush_type_size(&type, &align);
6037+ gen_cast_s(VT_SIZE_T);
60676038 } else {
6039+ type_size(&type, &align);
6040+ s = NULL;
6041+ if (vtop[1].r & VT_SYM)
6042+ s = vtop[1].sym; /* hack: accessing previous vtop */
6043+ if (s && s->a.aligned)
6044+ align = 1 << (s->a.aligned - 1);
60686045 vpushs(align);
60696046 }
6070− vtop->type.t |= VT_UNSIGNED;
60716047 break;
60726048
60736049 case TOK_builtin_expect:
@@ -8566,7 +8542,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 2492 unmodified lines
85668542 }
85678543 }
85688544
8569− vla_runtime_type_size(type, &a);
8545+ vpush_type_size(type, &a);
85708546 gen_vla_alloc(type, a);
85718547 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
85728548 /* on _WIN64, because of the function args scratch area, the
tests/tcctest.c+9−1
@@ -2980,12 +2980,20 @@ void c99_vla_test_2(int d, int h, int w) · 2979 unmodified lines
29802980 for (z=0; z<d; z++) {
29812981 for (y=0; y<h; y++) {
29822982 for (x=0; x<w; x++) {
2983− printf("% 4i", arr[z][y][x]);
2983+ printf(" %2d", arr[z][y][x]);
29842984 }
29852985 puts("");
29862986 }
29872987 puts("");
29882988 }
2989+ printf(" sizes : %d %d %d\n"
2990+ " pdiff : %d %d\n"
2991+ " tests : %d %d\n",
2992+ sizeof (*arr), sizeof (*arr)[0], sizeof (*arr)[0][0],
2993+ arr + 2 - arr, *arr + 3 - *arr,
2994+ 0 == sizeof (*arr + 1) - sizeof arr,
2995+ 0 == sizeof sizeof *arr - sizeof arr
2996+ );
29892997 free (arr);
29902998 }
29912999

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