Add vla support for arrays with no size

Support vla arrays like arr[][s]

- tcc.h: add nesting parameter
- tccgen.c: use nesting to test for illegal vla's
- test/tcctest.c: add test case
This commit is contained in:
herman ten brugge 2022-03-24 10:16:37 +01:00
parent 750f0a3e3f
commit 0244320b88
3 changed files with 22 additions and 11 deletions

1
tcc.h
View file

@ -645,6 +645,7 @@ typedef struct DLLReference {
#define TYPE_ABSTRACT 1 /* type without variable */ #define TYPE_ABSTRACT 1 /* type without variable */
#define TYPE_DIRECT 2 /* type with variable */ #define TYPE_DIRECT 2 /* type with variable */
#define TYPE_PARAM 4 /* type declares function parameter */ #define TYPE_PARAM 4 /* type declares function parameter */
#define TYPE_NEST 8 /* nested call to post_type */
#define IO_BUF_SIZE 8192 #define IO_BUF_SIZE 8192

View file

@ -5498,7 +5498,7 @@ check:
} }
skip(']'); skip(']');
/* parse next post type */ /* parse next post type */
post_type(type, ad, storage, td & ~(TYPE_DIRECT|TYPE_ABSTRACT)); post_type(type, ad, storage, (td & ~(TYPE_DIRECT|TYPE_ABSTRACT)) | TYPE_NEST);
if ((type->t & VT_BTYPE) == VT_FUNC) if ((type->t & VT_BTYPE) == VT_FUNC)
tcc_error("declaration of an array of functions"); tcc_error("declaration of an array of functions");
@ -5509,17 +5509,21 @@ check:
t1 |= type->t & VT_VLA; t1 |= type->t & VT_VLA;
if (t1 & VT_VLA) { if (t1 & VT_VLA) {
if (n < 0) if (n < 0) {
tcc_error("need explicit inner array size in VLAs"); if (td & TYPE_NEST)
loc -= type_size(&int_type, &align); tcc_error("need explicit inner array size in VLAs");
loc &= -align; }
n = loc; else {
loc -= type_size(&int_type, &align);
loc &= -align;
n = loc;
vpush_type_size(type, &align); vpush_type_size(type, &align);
gen_op('*'); gen_op('*');
vset(&int_type, VT_LOCAL|VT_LVAL, n); vset(&int_type, VT_LOCAL|VT_LVAL, n);
vswap(); vswap();
vstore(); vstore();
}
} }
if (n != -1) if (n != -1)
vpop(); vpop();

View file

@ -3017,6 +3017,11 @@ void c99_vla_test_3d(int s, int arr[2][3][s])
printf ("%d\n", arr[1][2][3]); printf ("%d\n", arr[1][2][3]);
} }
void c99_vla_test_3e(int s, int arr[][3][s])
{
printf ("%d\n", arr[1][2][3]);
}
void c99_vla_test_3(void) void c99_vla_test_3(void)
{ {
int a[2][3][4]; int a[2][3][4];
@ -3027,6 +3032,7 @@ void c99_vla_test_3(void)
c99_vla_test_3b(2, a); c99_vla_test_3b(2, a);
c99_vla_test_3c(3, a); c99_vla_test_3c(3, a);
c99_vla_test_3d(4, a); c99_vla_test_3d(4, a);
c99_vla_test_3e(4, a);
} }
void c99_vla_test(void) void c99_vla_test(void)