v4 mkf18a: import nucleus NAME -- hash 0, the effect from the nucleus (task 4, part 1)

mkf18a takes nucleus=SYSDIR, assembles the nucleus as the ROM does and
looks NAME up by its dictionary name; a word in line there, or one with
no fixed effect, is refused (plan section 12, rulings 3 and 4).
test_mkf18a: 5 new checks failed first, 93 pass at both widths. Its
build now has the host node's size, as the tool's has: the nucleus did
not fit a mesh node's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-20 03:53:57 +00:00
parent 7cfcbf2b68
commit 0684cd8362
8 changed files with 128 additions and 6 deletions
+4
View File
@@ -0,0 +1,4 @@
cc -std=c99 -Wall -Wextra -Wpedantic -Werror -O2 -g -I/home/user/LithosAnanke/v4/include -isystem /home/user/LithosAnanke/kernel/include -DV4_CELL_BITS=32 -DV4_NODE_WORDS=32768 -DV4_DATA_RING=1022 -DV4_RET_RING=31 -DV4_ROM_WORDS=4096 -DSCRATCH='"/home/user/LithosAnanke/v4/build/scratch-mkf18a-32"' -DV4_ROOT='"/home/user/LithosAnanke/v4"' -DV4_SYS_DIR='"/home/user/LithosAnanke/capsules/v4/sys"' /home/user/LithosAnanke/v4/tests/test_mkf18a.c /home/user/LithosAnanke/v4/tools/f18a.c /home/user/LithosAnanke/kernel/src/crypto/sha512.c /home/user/LithosAnanke/kernel/src/hash/xxhash64.c /home/user/LithosAnanke/v4/src/artifact.c /home/user/LithosAnanke/v4/src/asm.c /home/user/LithosAnanke/v4/src/capsule.c /home/user/LithosAnanke/v4/src/effect.c /home/user/LithosAnanke/v4/src/exec.c /home/user/LithosAnanke/v4/src/fabric.c /home/user/LithosAnanke/v4/src/guard.c /home/user/LithosAnanke/v4/src/heat.c /home/user/LithosAnanke/v4/src/image.c /home/user/LithosAnanke/v4/src/iword.c /home/user/LithosAnanke/v4/src/manage.c /home/user/LithosAnanke/v4/src/message.c /home/user/LithosAnanke/v4/src/node.c /home/user/LithosAnanke/v4/src/stack.c /home/user/LithosAnanke/v4/src/testcode.c /home/user/LithosAnanke/v4/src/text.c /home/user/LithosAnanke/v4/src/umul.c /home/user/LithosAnanke/v4/src/wire.c -o /home/user/LithosAnanke/v4/build/32-test_mkf18a
[V4_CELL_BITS=32 mkf18a]
v4 mkf18a: V4_CELL_BITS=32
93 checks, 0 failures
+4
View File
@@ -0,0 +1,4 @@
cc -std=c99 -Wall -Wextra -Wpedantic -Werror -O2 -g -I/home/user/LithosAnanke/v4/include -isystem /home/user/LithosAnanke/kernel/include -DV4_CELL_BITS=64 -DV4_NODE_WORDS=32768 -DV4_DATA_RING=1022 -DV4_RET_RING=31 -DV4_ROM_WORDS=4096 -DSCRATCH='"/home/user/LithosAnanke/v4/build/scratch-mkf18a-64"' -DV4_ROOT='"/home/user/LithosAnanke/v4"' -DV4_SYS_DIR='"/home/user/LithosAnanke/capsules/v4/sys"' /home/user/LithosAnanke/v4/tests/test_mkf18a.c /home/user/LithosAnanke/v4/tools/f18a.c /home/user/LithosAnanke/kernel/src/crypto/sha512.c /home/user/LithosAnanke/kernel/src/hash/xxhash64.c /home/user/LithosAnanke/v4/src/artifact.c /home/user/LithosAnanke/v4/src/asm.c /home/user/LithosAnanke/v4/src/capsule.c /home/user/LithosAnanke/v4/src/effect.c /home/user/LithosAnanke/v4/src/exec.c /home/user/LithosAnanke/v4/src/fabric.c /home/user/LithosAnanke/v4/src/guard.c /home/user/LithosAnanke/v4/src/heat.c /home/user/LithosAnanke/v4/src/image.c /home/user/LithosAnanke/v4/src/iword.c /home/user/LithosAnanke/v4/src/manage.c /home/user/LithosAnanke/v4/src/message.c /home/user/LithosAnanke/v4/src/node.c /home/user/LithosAnanke/v4/src/stack.c /home/user/LithosAnanke/v4/src/testcode.c /home/user/LithosAnanke/v4/src/text.c /home/user/LithosAnanke/v4/src/umul.c /home/user/LithosAnanke/v4/src/wire.c -o /home/user/LithosAnanke/v4/build/64-test_mkf18a
[V4_CELL_BITS=64 mkf18a]
v4 mkf18a: V4_CELL_BITS=64
93 checks, 0 failures
+12
View File
@@ -0,0 +1,12 @@
make: Entering directory '/home/user/LithosAnanke/v4'
cc -std=c99 -Wall -Wextra -Wpedantic -Werror -O2 -g -I/home/user/LithosAnanke/v4/include -isystem /home/user/LithosAnanke/kernel/include -DV4_CELL_BITS=64 -DSCRATCH='"/home/user/LithosAnanke/v4/build/scratch-mkf18a-64"' -DV4_ROOT='"/home/user/LithosAnanke/v4"' -DV4_SYS_DIR='"/home/user/LithosAnanke/capsules/v4/sys"' /home/user/LithosAnanke/v4/tests/test_mkf18a.c /home/user/LithosAnanke/v4/tools/f18a.c /home/user/LithosAnanke/kernel/src/crypto/sha512.c /home/user/LithosAnanke/kernel/src/hash/xxhash64.c /home/user/LithosAnanke/v4/src/artifact.c /home/user/LithosAnanke/v4/src/asm.c /home/user/LithosAnanke/v4/src/capsule.c /home/user/LithosAnanke/v4/src/effect.c /home/user/LithosAnanke/v4/src/exec.c /home/user/LithosAnanke/v4/src/fabric.c /home/user/LithosAnanke/v4/src/guard.c /home/user/LithosAnanke/v4/src/heat.c /home/user/LithosAnanke/v4/src/image.c /home/user/LithosAnanke/v4/src/iword.c /home/user/LithosAnanke/v4/src/manage.c /home/user/LithosAnanke/v4/src/message.c /home/user/LithosAnanke/v4/src/node.c /home/user/LithosAnanke/v4/src/stack.c /home/user/LithosAnanke/v4/src/testcode.c /home/user/LithosAnanke/v4/src/text.c /home/user/LithosAnanke/v4/src/umul.c /home/user/LithosAnanke/v4/src/wire.c -o /home/user/LithosAnanke/v4/build/64-test_mkf18a
[V4_CELL_BITS=64 mkf18a]
v4 mkf18a: V4_CELL_BITS=64
FAIL /home/user/LithosAnanke/v4/tests/test_mkf18a.c:337: an import from the nucleus builds: import of ABS from nucleus, a train not given
refused: import of SCR from nucleus, a train not given
FAIL /home/user/LithosAnanke/v4/tests/test_mkf18a.c:351: a word in line in the nucleus, which cannot be called
refused: import of UM* from nucleus, a train not given
FAIL /home/user/LithosAnanke/v4/tests/test_mkf18a.c:353: a nucleus word with no fixed effect
88 checks, 3 failures
make: *** [Makefile:462: mkf18a-64] Error 1
make: Leaving directory '/home/user/LithosAnanke/v4'
+2 -2
View File
@@ -440,13 +440,13 @@ $(BINDIR)/mkf18a-$(1): $(HERE)/tools/mkf18a.c $$(F18A_DEPS)
$(BINDIR)/$(1)-test_mkf18a: $(HERE)/tests/test_mkf18a.c $$(F18A_DEPS)
@mkdir -p $(BINDIR)
$$(CC) $$(CFLAGS) -I$(HERE)/include $(F18A_INC) -DV4_CELL_BITS=$(1) -DSCRATCH='"$(BINDIR)/scratch-mkf18a-$(1)"' -DV4_ROOT='"$(HERE)"' \
$$(CC) $$(CFLAGS) -I$(HERE)/include $(F18A_INC) -DV4_CELL_BITS=$(1) $(HOST_DEFS) -DSCRATCH='"$(BINDIR)/scratch-mkf18a-$(1)"' -DV4_ROOT='"$(HERE)"' -DV4_SYS_DIR='"$(SYS_DIR)"' \
$(HERE)/tests/test_mkf18a.c $(F18A_SRCS) $$(SRCS) -o $$@
$(BINDIR)/san-$(1)-test_mkf18a: $(HERE)/tests/test_mkf18a.c $$(F18A_DEPS)
@mkdir -p $(BINDIR)
$$(CC) $$(CSTD) $$(WARN) -O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined -fno-sanitize-recover=all \
-I$(HERE)/include $(F18A_INC) -DV4_CELL_BITS=$(1) -DSCRATCH='"$(BINDIR)/scratch-san-mkf18a-$(1)"' -DV4_ROOT='"$(HERE)"' \
-I$(HERE)/include $(F18A_INC) -DV4_CELL_BITS=$(1) $(HOST_DEFS) -DSCRATCH='"$(BINDIR)/scratch-san-mkf18a-$(1)"' -DV4_ROOT='"$(HERE)"' -DV4_SYS_DIR='"$(SYS_DIR)"' \
$(HERE)/tests/test_mkf18a.c $(F18A_SRCS) $$(SRCS) -o $$@
.PHONY: mkf18a-$(1) san-mkf18a-$(1)
+30
View File
@@ -61,6 +61,7 @@ static int save(const char *path, const unsigned char *a, size_t len)
}
/* the build of `dir`, with MAP, ROM and the trains given; NULL, err set, if it is refused */
static const char *nucleus; /* `import nucleus` builds against it when set */
static unsigned char *build(const char *dir, const f18a_train *trains, unsigned ntrains, size_t *len)
{
f18a_args a;
@@ -71,6 +72,7 @@ static unsigned char *build(const char *dir, const f18a_train *trains, unsigned
a.trains = trains;
a.ntrains = ntrains;
a.tool_root = V4_ROOT;
a.nucleus = nucleus;
err[0] = 0;
if (!f18a_build(&a, &out, len, err, sizeof err)) return NULL;
return out;
@@ -323,6 +325,34 @@ int main(void)
free(c);
}
/* imports from the nucleus (section 12, rulings 3 and 4): hash 0, the effect derived from the nucleus
* as the tool assembles it, by its dictionary name */
put(SCRATCH "/n1", "00.f18", "import nucleus ABS\n: DIST inv 1 + + ABS ;\n");
CHECK(refused(SCRATCH "/n1", NULL, 0, "nucleus"), "an import from the nucleus, with no nucleus given");
nucleus = V4_SYS_DIR;
{
size_t ln;
unsigned char *c = build(SCRATCH "/n1", NULL, 0, &ln);
v4_f18a_layout lc;
CHECK(c != NULL, "an import from the nucleus builds: %s", err);
if (c) {
CHECK(v4_f18a_check(c, ln, V4_CELL_BITS, &lc, &why) && lc.i == 1, "valid, with one import");
CHECK(U(c, lc.imports) == 0 && U(c, lc.imports + 1) == 0, "the nucleus's hash: 0");
CHECK(pool_is(c, &lc, U(c, lc.imports + 2), "ABS"), "the import's name, ABS");
CHECK(U(c, lc.imports + 3) == (1u | (1u << 16)) && U(c, lc.imports + 4) == 0, "ABS's effect from the nucleus: 1 -> 1");
x = export_of(c, &lc, "DIST");
CHECK(x && U(c, x + 3) == (2u | (1u << 16)), "DIST, calling it, 2 -> 1");
free(c);
}
}
put(SCRATCH "/n2", "00.f18", "import nucleus NOPE\n: W NOPE ;\n");
CHECK(refused(SCRATCH "/n2", NULL, 0, "NOPE"), "a name the nucleus does not have");
put(SCRATCH "/n3", "00.f18", "import nucleus SCR\n: W SCR ;\n");
CHECK(refused(SCRATCH "/n3", NULL, 0, "in line"), "a word in line in the nucleus, which cannot be called");
put(SCRATCH "/n4", "00.f18", "import nucleus UM*\n: W UM* ;\n");
CHECK(refused(SCRATCH "/n4", NULL, 0, "no fixed effect"), "a nucleus word with no fixed effect");
nucleus = NULL;
free(a);
done:
free(lib);
+66
View File
@@ -7,6 +7,7 @@
#include "v4/artifact.h"
#include "v4/effect.h"
#include "v4/text.h"
#include "v4/sys_files.h"
#include "starkernel/sha512.h"
#include "starkernel/xxhash64.h"
#include <dirent.h>
@@ -22,6 +23,15 @@
static v4_node n; /* large: not on the stack */
static v4_text tx;
/* The nucleus, for `import nucleus NAME` (plan section 12, rulings 3 and 4): assembled as the ROM assembles
* it, its map first, sys_files.h's order, end.f18 last, at the system's origin (tests/host_map.h's
* ROM_ORIGIN, as test_host_rom_nucleus assembles it), once per build that needs it. */
#define NUCLEUS_ORIGIN 16
#define NUCLEUS_FILE(x) #x,
static const char *const nucleus_files[] = { V4_SYS_FILES(NUCLEUS_FILE) };
static v4_node nn;
static v4_text ntx;
typedef struct { /* an import, resolved */
uint64_t id;
v4_effect fx;
@@ -165,6 +175,61 @@ static int import_effect(void *ctx, v4_cell addr, v4_effect *out)
return 0;
}
/* the nucleus assembled, and its effects: 1, or 0 having failed */
static v4_effects *nfx;
static int nucleus_ready(build_t *b, const char *dir)
{
char path[1024];
unsigned i;
if (nfx) return 1;
v4_node_reset(&nn);
v4_text_begin(&ntx, &nn, NUCLEUS_ORIGIN);
snprintf(path, sizeof path, "%s/map-%d.f18", dir, V4_CELL_BITS);
if (!v4_text_assemble_file(&ntx, path)) return fail(b, "the nucleus: %s", v4_text_error(&ntx));
for (i = 0; i < sizeof nucleus_files / sizeof nucleus_files[0]; i++) {
snprintf(path, sizeof path, "%s/%s.f18", dir, nucleus_files[i]);
if (!v4_text_assemble_file(&ntx, path)) return fail(b, "the nucleus: %s", v4_text_error(&ntx));
}
snprintf(path, sizeof path, "%s/end.f18", dir);
if (!v4_text_assemble_file(&ntx, path) || !v4_text_finish(&ntx)) return fail(b, "the nucleus: %s", v4_text_error(&ntx));
nfx = v4_effects_new(&nn, NUCLEUS_ORIGIN, v4_text_here(&ntx), NULL, NULL);
if (!nfx) return fail(b, "out of memory");
return 1;
}
/* the xt of the nucleus's entry named `name`, by its dictionary's links from the newest, or -1 */
static v4_cell nucleus_word(const char *name)
{
v4_cell link;
size_t len = strlen(name), i;
if (!v4_text_value(&ntx, "HDR-LINK", &link)) return -1;
for (; link > 4; link = nn.mem[link - 1]) {
v4_cell nfa = nn.mem[link - 2];
if ((nn.mem[nfa] & 255) != (v4_cell)len) continue;
for (i = 0; i < len; i++)
if (((nn.mem[nfa + (v4_cell)((i + 1) / 4)] >> (8 * ((i + 1) % 4))) & 255) != (unsigned char)name[i]) break;
if (i == len) return link;
}
return -1;
}
/* an import from the nucleus: hash 0, the effect derived from the nucleus; it declares no capabilities */
static int resolve_nucleus(build_t *b, const f18a_args *a, import_t *im, const char *name)
{
v4_cell xt;
if (!a->nucleus) return fail(b, "import of %s from the nucleus, which is not given", name);
if (!nucleus_ready(b, a->nucleus)) return 0;
xt = nucleus_word(name);
if (xt < 0) return fail(b, "the nucleus does not have %s", name);
if (nn.mem[xt - 3] & 8) return fail(b, "%s is in line in the nucleus: it cannot be called", name);
im->fx = v4_effect_of(nfx, xt, V4_EFFECT_CALLED);
if (!im->fx.fixed) return fail(b, "%s in the nucleus has no fixed effect: %s", name, im->fx.why);
im->id = 0;
im->caps = 0;
im->nreq = 0;
return 1;
}
static int resolve_import(build_t *b, const f18a_args *a, unsigned k)
{
const char *train, *name;
@@ -176,6 +241,7 @@ static int resolve_import(build_t *b, const f18a_args *a, unsigned k)
import_t *im = &b->imp[k];
v4_text_import_at(&tx, k, &train, &name);
if (strcmp(train, "nucleus") == 0) return resolve_nucleus(b, a, im, name);
for (i = 0; i < a->ntrains && strcmp(a->trains[i].name, train) != 0; i++) ;
if (i == a->ntrains) return fail(b, "import of %s from %s, a train not given", name, train);
art = (unsigned char *)read_file(a->trains[i].path, &len);
+4 -1
View File
@@ -9,7 +9,9 @@
* written after its name;
* - every `import TRAIN NAME` is an import of TRAIN's export NAME: TRAIN is given as an artifact, whose
* capsule ID (the XXHash64 of its bytes, as mkcapsule gives it) names it, and whose export gives the
* effects expected;
* effects expected. TRAIN `nucleus` is the nucleus (plan section 12): hash 0, NAME looked up by its
* dictionary name in the nucleus as the tool assembles it from its source, and its effect derived there;
* a word in line there, or one with no fixed effect, is refused. It declares no capabilities;
* - every address text.c records is a relocation;
* - the provenance's four SHA-512 hashes, each over files framed as: the file's name, a NUL, its length
* in 8 bytes low first, its bytes. The source: the train's .f18, in name order. The tool: its own
@@ -38,6 +40,7 @@ typedef struct {
const f18a_train *trains; /* the trains it may import from */
unsigned ntrains;
const char *tool_root; /* the v4 directory, for the tool's hash */
const char *nucleus; /* the nucleus's source directory, for `import nucleus NAME`, or NULL */
} f18a_args;
/* 1 and the artifact in *out (malloc'd, *len bytes), or 0 and why in err. */
+6 -3
View File
@@ -3,10 +3,11 @@
*/
/* mkf18a.c -- build a train's engine, its F18 artifact, off the node (f18a.h).
*
* mkf18a OUT.f18a MAP.f18 ROM DIR [TRAIN=PATH.f18a ...]
* mkf18a OUT.f18a MAP.f18 ROM DIR [TRAIN=PATH.f18a ...] [nucleus=SYSDIR]
*
* docs/v4.0.0/plans/2026-11-19-f18-artifact-build.md section 2. Built as mkrom is, one per cell width,
* with the node's size. DIR holds the train's .f18; each TRAIN=PATH is a train it may import from.
* with the node's size. DIR holds the train's .f18; each TRAIN=PATH is a train it may import from;
* nucleus=SYSDIR is the nucleus's source, for `import nucleus NAME` (plan section 12).
*/
#include "f18a.h"
#include <stdio.h>
@@ -27,15 +28,17 @@ int main(int argc, char **argv)
int i;
FILE *f;
if (argc < 5) { fprintf(stderr, "usage: mkf18a OUT.f18a MAP.f18 ROM DIR [TRAIN=PATH.f18a ...]\n"); return 2; }
if (argc < 5) { fprintf(stderr, "usage: mkf18a OUT.f18a MAP.f18 ROM DIR [TRAIN=PATH.f18a ...] [nucleus=SYSDIR]\n"); return 2; }
a.map = argv[2];
a.rom = argv[3];
a.dir = argv[4];
a.ntrains = 0;
a.nucleus = NULL;
for (i = 5; i < argc; i++) {
char *eq = strchr(argv[i], '=');
if (!eq || a.ntrains == 64) { fprintf(stderr, "mkf18a: not TRAIN=PATH: %s\n", argv[i]); return 2; }
*eq = 0;
if (strcmp(argv[i], "nucleus") == 0) { a.nucleus = eq + 1; continue; }
trains[a.ntrains].name = argv[i];
trains[a.ntrains].path = eq + 1;
a.ntrains++;