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
234static void decl(int l);234static void decl(int l);
235static int decl0(int l, int is_for_loop_init, Sym *);235static int decl0(int l, int is_for_loop_init, Sym *);
236static void expr_eq(void);236static void expr_eq(void);
237static void vla_runtime_type_size(CType *type, int *a);237static void vpush_type_size(CType *type, int *a);
238static int is_compatible_unqualified_types(CType *type1, CType *type2);238static int is_compatible_unqualified_types(CType *type1, CType *type2);
239static inline int64_t expr_const64(void);239static inline int64_t expr_const64(void);
240static void vpush64(int ty, unsigned long long v);240static 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
3173 goto no_var;3173 goto no_var;
3174 case VT_PTR:3174 case VT_PTR:
3175 s = type->ref;3175 s = type->ref;
3176 if (t & VT_ARRAY) {3176 if (t & (VT_ARRAY|VT_VLA)) {
3177 if (varstr && '*' == *varstr)3177 if (varstr && '*' == *varstr)
3178 snprintf(buf1, sizeof(buf1), "(%s)[%d]", varstr, s->c);3178 snprintf(buf1, sizeof(buf1), "(%s)[%d]", varstr, s->c);
3179 else3179 else
@@ -3220,12 +3220,6 @@ static int pointed_size(CType *type) · 40 unmodified lines
3220 return type_size(pointed_type(type), &align);3220 return type_size(pointed_type(type), &align);
3221}3221}
3222 3222
3223static void vla_runtime_pointed_size(CType *type)
3224{
3225 int align;
3226 vla_runtime_type_size(pointed_type(type), &align);
3227}
3228
3229static inline int is_null_pointer(SValue *p)3223static inline int is_null_pointer(SValue *p)
3230{3224{
3231 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)3225 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
3436/* generic gen_op: handles types problems */3430/* generic gen_op: handles types problems */
3437ST_FUNC void gen_op(int op)3431ST_FUNC void gen_op(int op)
3438{3432{
3439 int u, t1, t2, bt1, bt2, t;3433 int t1, t2, bt1, bt2, t;
3440 CType type1, combtype;3434 CType type1, combtype;
3441 3435
3442redo:3436redo:
@@ -3463,17 +3457,14 @@ redo: · 20 unmodified lines
3463 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {3457 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3464 /* at least one operand is a pointer */3458 /* at least one operand is a pointer */
3465 /* relational op: must be both pointers */3459 /* relational op: must be both pointers */
3460 int align;
3466 if (TOK_ISCOND(op))3461 if (TOK_ISCOND(op))
3467 goto std_op;3462 goto std_op;
3468 /* if both pointers, then it must be the '-' op */3463 /* if both pointers, then it must be the '-' op */
3469 if (bt1 == VT_PTR && bt2 == VT_PTR) {3464 if (bt1 == VT_PTR && bt2 == VT_PTR) {
3470 if (op != '-')3465 if (op != '-')
3471 tcc_error("cannot use pointers here");3466 tcc_error("cannot use pointers here");
3472 if (vtop[-1].type.t & VT_VLA) {3467 vpush_type_size(pointed_type(&vtop[-1].type), &align);
3473 vla_runtime_pointed_size(&vtop[-1].type);
3474 } else {
3475 vpushi(pointed_size(&vtop[-1].type));
3476 }
3477 vrott(3);3468 vrott(3);
3478 gen_opic(op);3469 gen_opic(op);
3479 vtop->type.t = VT_PTRDIFF_T;3470 vtop->type.t = VT_PTRDIFF_T;
@@ -3494,19 +3485,7 @@ redo: · 14 unmodified lines
3494 gen_cast_s(VT_INT);3485 gen_cast_s(VT_INT);
3495#endif3486#endif
3496 type1 = vtop[-1].type;3487 type1 = vtop[-1].type;
3497 if (vtop[-1].type.ref->type.t & VT_VLA)3488 vpush_type_size(pointed_type(&vtop[-1].type), &align);
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 }
3510 gen_op('*');3489 gen_op('*');
3511#ifdef CONFIG_TCC_BCHECK3490#ifdef CONFIG_TCC_BCHECK
3512 if (tcc_state->do_bounds_check && !const_wanted) {3491 if (tcc_state->do_bounds_check && !const_wanted) {
@@ -3523,7 +3502,7 @@ redo: · 10 unmodified lines
3523 {3502 {
3524 gen_opic(op);3503 gen_opic(op);
3525 }3504 }
3526 type1.t &= ~VT_ARRAY;3505 type1.t &= ~(VT_ARRAY|VT_VLA);
3527 /* put again type if gen_opic() swaped operands */3506 /* put again type if gen_opic() swaped operands */
3528 vtop->type = type1;3507 vtop->type = type1;
3529 }3508 }
@@ -3957,13 +3936,16 @@ ST_FUNC int type_size(CType *type, int *a) · 427 unmodified lines
3957 3936
3958/* push type size as known at runtime time on top of value stack. Put3937/* push type size as known at runtime time on top of value stack. Put
3959 alignment at 'a' */3938 alignment at 'a' */
3960ST_FUNC void vla_runtime_type_size(CType *type, int *a)3939static void vpush_type_size(CType *type, int *a)
3961{3940{
3962 if (type->t & VT_VLA) {3941 if (type->t & VT_VLA) {
3963 type_size(&type->ref->type, a);3942 type_size(&type->ref->type, a);
3964 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);3943 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
3965 } else {3944 } 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);
3967 }3949 }
3968}3950}
3969 3951
@@ -5501,7 +5483,7 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td) · 1531 unmodified lines
5501 loc &= -align;5483 loc &= -align;
5502 n = loc;5484 n = loc;
5503 5485
5504 vla_runtime_type_size(type, &align);5486 vpush_type_size(type, &align);
5505 gen_op('*');5487 gen_op('*');
5506 vset(&int_type, VT_LOCAL|VT_LVAL, n);5488 vset(&int_type, VT_LOCAL|VT_LVAL, n);
5507 vswap();5489 vswap();
@@ -6050,24 +6032,18 @@ ST_FUNC void unary(void) · 542 unmodified lines
6050 next();6032 next();
6051 in_sizeof++;6033 in_sizeof++;
6052 expr_type(&type, unary); /* Perform a in_sizeof = 0; */6034 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);
6059 if (t == TOK_SIZEOF) {6035 if (t == TOK_SIZEOF) {
6060 if (!(type.t & VT_VLA)) {6036 vpush_type_size(&type, &align);
6061 if (size < 0)6037 gen_cast_s(VT_SIZE_T);
6062 tcc_error("sizeof applied to an incomplete type");
6063 vpushs(size);
6064 } else {
6065 vla_runtime_type_size(&type, &align);
6066 }
6067 } else {6038 } 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);
6068 vpushs(align);6045 vpushs(align);
6069 }6046 }
6070 vtop->type.t |= VT_UNSIGNED;
6071 break;6047 break;
6072 6048
6073 case TOK_builtin_expect:6049 case TOK_builtin_expect:
@@ -8566,7 +8542,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 2492 unmodified lines
8566 }8542 }
8567 }8543 }
8568 8544
8569 vla_runtime_type_size(type, &a);8545 vpush_type_size(type, &a);
8570 gen_vla_alloc(type, a);8546 gen_vla_alloc(type, a);
8571#if defined TCC_TARGET_PE && defined TCC_TARGET_X86_648547#if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
8572 /* on _WIN64, because of the function args scratch area, the8548 /* 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
2980 for (z=0; z<d; z++) {2980 for (z=0; z<d; z++) {
2981 for (y=0; y<h; y++) {2981 for (y=0; y<h; y++) {
2982 for (x=0; x<w; x++) {2982 for (x=0; x<w; x++) {
2983 printf("% 4i", arr[z][y][x]);2983 printf(" %2d", arr[z][y][x]);
2984 }2984 }
2985 puts("");2985 puts("");
2986 }2986 }
2987 puts("");2987 puts("");
2988 }2988 }
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 );
2989 free (arr);2997 free (arr);
2990}2998}
2991 2999

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