From 0244320b8852e7401655daf4e7259579e9c03550 Mon Sep 17 00:00:00 2001 From: herman ten brugge Date: Thu, 24 Mar 2022 10:16:37 +0100 Subject: [PATCH] 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 --- tcc.h | 1 + tccgen.c | 26 +++++++++++++++----------- tests/tcctest.c | 6 ++++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/tcc.h b/tcc.h index 97248487..8a46210e 100644 --- a/tcc.h +++ b/tcc.h @@ -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 diff --git a/tccgen.c b/tccgen.c index 7d15dbea..185fdf27 100644 --- a/tccgen.c +++ b/tccgen.c @@ -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,17 +5509,21 @@ check: t1 |= type->t & VT_VLA; if (t1 & VT_VLA) { - if (n < 0) - tcc_error("need explicit inner array size in VLAs"); - loc -= type_size(&int_type, &align); - loc &= -align; - n = loc; + 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; - vpush_type_size(type, &align); - gen_op('*'); - vset(&int_type, VT_LOCAL|VT_LVAL, n); - vswap(); - vstore(); + vpush_type_size(type, &align); + gen_op('*'); + vset(&int_type, VT_LOCAL|VT_LVAL, n); + vswap(); + vstore(); + } } if (n != -1) vpop(); diff --git a/tests/tcctest.c b/tests/tcctest.c index dfa5f4f0..8db76d5d 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -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)