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_DIRECT 2 /* type with variable */
#define TYPE_PARAM 4 /* type declares function parameter */
#define TYPE_NEST 8 /* nested call to post_type */
#define IO_BUF_SIZE 8192

View file

@ -5498,7 +5498,7 @@ check:
}
skip(']');
/* 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)
tcc_error("declaration of an array of functions");
@ -5509,8 +5509,11 @@ check:
t1 |= type->t & VT_VLA;
if (t1 & VT_VLA) {
if (n < 0)
if (n < 0) {
if (td & TYPE_NEST)
tcc_error("need explicit inner array size in VLAs");
}
else {
loc -= type_size(&int_type, &align);
loc &= -align;
n = loc;
@ -5521,6 +5524,7 @@ check:
vswap();
vstore();
}
}
if (n != -1)
vpop();
nocode_wanted = saved_nocode_wanted;

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