From 211af053819ccfabcf97b0e2a372e7e7deb8495d Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 6 Mar 2024 02:27:01 -0500 Subject: Fix up some array literal stuff with TCC --- builtins/array.h | 8 +++++++- compile.c | 16 +++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/builtins/array.h b/builtins/array.h index 4706c227..8b7b3b40 100644 --- a/builtins/array.h +++ b/builtins/array.h @@ -21,7 +21,13 @@ #define $TypedArray(t, ...) ({ t $items[] = {__VA_ARGS__}; \ (array_t){.length=sizeof($items)/sizeof($items[0]), \ .stride=(int64_t)&$items[1] - (int64_t)&$items[0], \ - .data=memcpy(GC_MALLOC(sizeof($items)), $items, sizeof($items)), \ + .data=memcpy(GC_MALLOC(sizeof($items)), $items, sizeof($items)), \ + .atomic=0, \ + .data_refcount=1}; }) +#define $TypedArrayN(t, N, ...) ({ t $items[N] = {__VA_ARGS__}; \ + (array_t){.length=N, \ + .stride=(int64_t)&$items[1] - (int64_t)&$items[0], \ + .data=memcpy(GC_MALLOC(sizeof($items)), $items, sizeof($items)), \ .atomic=0, \ .data_refcount=1}; }) #define $Array(x, ...) ({ __typeof(x) $items[] = {x, __VA_ARGS__}; \ diff --git a/compile.c b/compile.c index 0a878e3b..04397691 100644 --- a/compile.c +++ b/compile.c @@ -549,17 +549,19 @@ CORD compile(env_t *env, ast_t *ast) comparison, " ? $ternary$lhs : $ternary$rhs;\n" "})"); } - // Min, Max, case Array: { auto array = Match(ast, Array); if (!array->items) return "(array_t){.length=0}"; - - CORD code = "$Array("; - for (ast_list_t *item = array->items; item; item = item->next) { - code = CORD_cat(code, compile(env, item->ast)); - if (item->next) code = CORD_cat(code, ", "); - } + + int64_t n = 0; + for (ast_list_t *item = array->items; item; item = item->next) + ++n; + + type_t *item_type = Match(get_type(env, ast), ArrayType)->item_type; + CORD code = CORD_all("$TypedArrayN(", compile_type(item_type), CORD_asprintf(", %ld", n)); + for (ast_list_t *item = array->items; item; item = item->next) + code = CORD_all(code, ", ", compile(env, item->ast)); return CORD_cat(code, ")"); } case Table: { -- cgit v1.2.3