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
+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++;