aboutsummaryrefslogtreecommitdiff
path: root/src/tools/mkgenome.c
diff options
context:
space:
mode:
authorGravatar zinc <zinc@downsta.ir> 2026-10-14 07:28:39 +0000
committerGravatar zinc <zinc@downsta.ir> 2026-10-14 07:28:39 +0000
commit7451eb090d890fb5ea5e4d40627a1b4df15a49de (patch)
tree357bdf4b528e860bffa688865306f656b788d2c2 /src/tools/mkgenome.c
parentanalyzer: close the ownership gaps that stopped the compiler compiling itself (diff)
build: embed images with #embed, keep everything generated in build/
the cores take their genome in with c23 #embed through src/core/genome.h, so images are plain little-endian word files (genome.bin) instead of generated c headers. zc0 writes that format, mkgenome checks an image against its map and shows a compiler's diagnostics when it gets them, and the javascript vm and the editor's vm host read the same files. src/tools/findcc.sh picks a compiler with #embed (clang 19 or gcc 15). zc gets an embed declaration, embed "file" as [type] name, for globals and locals: a byte string by default, char*, numbers read little-endian, and arrays of numbers. the compiler and language server carry their library and source texts through it, so embed.sh and the text it spliced into compiler.zc are gone. make writes executables to build/bin, the default map to build/core/map, the web page to build/web and the extension staging to build/vsix, and the genome now rebuilds when its sources change. the test suite lays out its own map and bootstrap and reads nothing from build/. also: rt.put_s of a byte string in zc0 images, retained function sources no longer count as live objects, a missing include no longer cascades into undefined-name errors, consecutive comment lines join into one hover paragraph, and stale test fixtures follow the include format.
Diffstat (limited to 'src/tools/mkgenome.c')
-rw-r--r--src/tools/mkgenome.c50
1 files changed, 33 insertions, 17 deletions
diff --git a/src/tools/mkgenome.c b/src/tools/mkgenome.c
index 3bf1533..9720760 100644
--- a/src/tools/mkgenome.c
+++ b/src/tools/mkgenome.c
@@ -1,7 +1,12 @@
-/* turns a genome image (little-endian 64-bit words on stdin, as written by
- src/tools/build.zc) into genome.h */
+/* checks a genome image (little-endian 64-bit words on stdin, as written by
+ zc0 or src/tools/build.zc) against the memory map it was built with, and
+ copies it to stdout unchanged. the cores #embed the result (src/core/genome.h),
+ so this is the gate that keeps a compiler's diagnostics out of a core: when
+ the input is not an image for this map, nothing is written and the input is
+ shown on stderr, since it is usually the compiler's error report. */
#include <stdio.h>
#include <stdint.h>
+#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <fcntl.h>
@@ -11,8 +16,9 @@
int main(int argc, char **argv)
{
if (argc == 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))) {
- fputs("usage: mkgenome [--help|-h] < image.bin > genome.h\n"
- "converts little-endian 64-bit image words into a c genome header.\n"
+ fputs("usage: mkgenome [--help|-h] < image.bin > genome.bin\n"
+ "checks little-endian 64-bit image words against this memory map and copies them\n"
+ "to stdout. input that is not an image is reported on stderr and nothing is written.\n"
"the image must match the memory layout selected when mkgenome is built.\n"
"options:\n --help, -h show this help and exit\n", stdout);
return 0;
@@ -22,19 +28,29 @@ int main(int argc, char **argv)
_setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY);
#endif
- unsigned char b[8]; int n = 0; size_t got;
- printf("#define GORG %d\nstatic const int64_t genome[] = {", GORG);
- while ((got = fread(b, 1, 8, stdin)) == 8) {
- uint64_t u = (uint64_t)b[0] | (uint64_t)b[1] << 8 | (uint64_t)b[2] << 16 | (uint64_t)b[3] << 24
- | (uint64_t)b[4] << 32 | (uint64_t)b[5] << 40 | (uint64_t)b[6] << 48 | (uint64_t)b[7] << 56;
- if ((n == 0 && u != (uint64_t)FTOP * 2 + 1) ||
- (n == 1 && u != (uint64_t)ZFP * 2 + 1) || (n == 2 && u != 4)) {
- fprintf(stderr, "mkgenome: input is not a genome for this map\n"); return 1;
+ size_t n = 0, cap = 1 << 20, got;
+ unsigned char *b = malloc(cap);
+ if (!b) { fputs("mkgenome: out of memory\n", stderr); return 1; }
+ while ((got = fread(b + n, 1, cap - n, stdin)) > 0) {
+ n += got;
+ if (n == cap) {
+ unsigned char *next = cap > ((size_t)1 << 30) ? 0 : realloc(b, cap * 2);
+ if (!next) { fputs("mkgenome: image too large\n", stderr); return 1; }
+ b = next; cap *= 2;
}
- printf("%s%lld", !n ? "\n" : n % 12 ? "," : ",\n", (long long)u);
- n++;
}
- if (got || ferror(stdin) || n < 7) { fprintf(stderr, "mkgenome: incomplete genome\n"); return 1; }
- printf("\n};\n#define GLEN %d\n", n);
- return n ? 0 : 1;
+ if (ferror(stdin)) { fputs("mkgenome: read failed\n", stderr); return 1; }
+ int ok = n >= 8 * 7 && n % 8 == 0;
+ for (int i = 0; ok && i < 3; i++) {
+ uint64_t u = 0;
+ for (int k = 0; k < 8; k++) u |= (uint64_t)b[i * 8 + k] << (k * 8);
+ if (u != (i == 0 ? (uint64_t)FTOP * 2 + 1 : i == 1 ? (uint64_t)ZFP * 2 + 1 : 4)) ok = 0;
+ }
+ if (!ok) {
+ if (n >= 4 && !memcmp(b, "zc: ", 4)) fwrite(b, 1, n, stderr);
+ fprintf(stderr, n % 8 || n < 8 * 7 ? "mkgenome: incomplete genome\n" : "mkgenome: input is not a genome for this map\n");
+ return 1;
+ }
+ if (fwrite(b, 1, n, stdout) != n || fflush(stdout)) { fputs("mkgenome: write failed\n", stderr); return 1; }
+ return 0;
}