harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 1fcb36abc7cf070550e894ececea99dd0b2b4cb6
parent fa15827d851a56d6ce69476b4f0d717bd323a88c
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 30 Jan 2021 14:46:00 -0500

gen: fix array globals of aggregate types

Diffstat:
Msrc/eval.c | 6+++++-
Msrc/gen.c | 3+++
Mtests/11-globals.ha | 3++-
3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/eval.c b/src/eval.c @@ -248,7 +248,11 @@ eval_const(struct context *ctx, struct expression *in, struct expression *out) case TYPE_STORAGE_SLICE: assert(0); // TODO case TYPE_STORAGE_STRING: - out->constant.string.value = strdup(in->constant.string.value); + out->constant.string.len = in->constant.string.len; + out->constant.string.value = xcalloc(1, in->constant.string.len); + memcpy(out->constant.string.value, + in->constant.string.value, + in->constant.string.len); break; case TYPE_STORAGE_STRUCT: case TYPE_STORAGE_UNION: diff --git a/src/gen.c b/src/gen.c @@ -2222,6 +2222,9 @@ gen_data_item(struct gen_context *ctx, struct expression *expr, for (struct array_constant *c = constant->array; c && n; c = c->next ? c->next : c, --n) { gen_data_item(ctx, c->value, item); + while (item->next) { + item = item->next; + } if (n > 1 || c->next) { item->next = xcalloc(1, sizeof(struct qbe_data_item)); diff --git a/tests/11-globals.ha b/tests/11-globals.ha @@ -10,13 +10,14 @@ fn write() void = { let ar: [3]int = [1, 2, 3]; let sl: []int = [1, 2, 3]; +let st: str = "Hello!"; fn storage() void = { assert(len(ar) == 3z); assert(ar[0] == 1 && ar[1] == 2 && ar[2] == 3); - assert(len(sl) == 3z); assert(sl[0] == 1 && sl[1] == 2 && sl[2] == 3); + assert(len(st) == 6z); }; fn invariants() void = {