aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar zinc <zinc@downsta.ir> 2026-10-14 16:16:59 +0330
committerGravatar zinc <zinc@downsta.ir> 2026-10-14 16:16:59 +0330
commitb92c7fe87239279646fcbef493cf628d260f51ff (patch)
tree8603b1f43e1c8f6342787f0a3a38bc33db9033b9
parentzc0: x as T reads a whole type (x as int*, x as char**) (diff)
zc0: embed of an array type inside a function
-rw-r--r--src/tools/zc0.c6
-rw-r--r--tests/programs/embed_local_array.zc11
2 files changed, 15 insertions, 2 deletions
diff --git a/src/tools/zc0.c b/src/tools/zc0.c
index f28c655..51ea9f8 100644
--- a/src/tools/zc0.c
+++ b/src/tools/zc0.c
@@ -994,12 +994,13 @@ static void embeddecl(int local)
}
} else {
int count; w = embed_width(ty, star);
- if (local) die("zc0 embeds an array at the top level, not in a function");
if (!w) die("embed declares a bstr, a char*, a number or an array of numbers");
if (len % w) die("the embedded file's size is not a whole number of elements");
count = (int)(len / w);
if (arr >= 0 && count > arr) die("the embedded file is larger than the array it is declared as");
if (arr >= 0) count = arr;
+ /* in a function the elements sit in the code, jumped over, and the local holds their address */
+ if (local) { j = here; lit(0); op(O_JMP); }
a = here;
for (i = 0; i < count; i++) {
uint64_t u = 0; int k;
@@ -1009,7 +1010,8 @@ static void embeddecl(int local)
emit((int64_t)u);
}
if (!count) emit(0);
- add(n, K_GARR, a);
+ if (local) { setlit(j, here); l = newloc(n); lit(a); laddr(l->slot); op(O_ST); }
+ else add(n, K_GARR, a);
}
free(buf);
}
diff --git a/tests/programs/embed_local_array.zc b/tests/programs/embed_local_array.zc
new file mode 100644
index 0000000..0e2b7b8
--- /dev/null
+++ b/tests/programs/embed_local_array.zc
@@ -0,0 +1,11 @@
+include rt;
+// embed of an array type inside a function (zc0 used to embed arrays only at the top level)
+fn main() {
+ embed "tests/data/embed/bytes.bin" as u8[] bytes;
+ embed "tests/data/embed/bytes.bin" as i8[8] signed;
+ embed "tests/data/embed/shorts.bin" as u16[] shorts;
+ rt.put_n(bytes[4]); rt.put_c(32);
+ rt.put_n(signed[4]); rt.put_c(32); rt.put_n(signed[7]); rt.put_c(32);
+ rt.put_n(shorts[0] + shorts[1] * 10 + shorts[2] * 100); rt.put_c(10);
+ return 0;
+}