v4: the host placer, and a small train placed on Hera at her HERE (ADR step 3, task 4)

place.c places a train's artifact as ABI.md section 5 has it, the
code split at each export with its entry before it. test_mesh_boot
places tests/train-place, built by mkf18a with an import from the
nucleus: its words run by name, the nucleus's ABS is reached, the
kernel records each export, and six refusals leave Hera unchanged.
mkf18a refuses a word that runs on into an export. Failed first;
gates pass, POST 990/990. Plan section 13.

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 12:44:50 +00:00
parent cf2a321807
commit f7769c0e93
16 changed files with 2246 additions and 10 deletions
+16 -9
View File
@@ -366,13 +366,20 @@ sanitize: $(foreach w,$(WIDTHS),$(foreach t,$(TESTS),san-$(w)-$(notdir $(t)))) s
# fabric, tested on the host with tests/test_mesh_boot.c as its owner. Linked as the hosted product
# is, with the registry (src/manage.c) and a mesh node's ROM; 64-bit cells, as the capsules are.
MESH_BOOT_SRCS := $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c
$(BINDIR)/test_mesh_boot: $(HERE)/tests/test_mesh_boot.c $(MESH_BOOT_SRCS) $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BLOCKS_SRC) $(V3_BLOCK_SRCS) $(wildcard $(HERE)/include/v4/*.h) $(HERE)/Makefile
$(CC) $(CFLAGS) -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/test_mesh_boot.o
# The small train it places (plan 2026-11-19-f18-artifact-build section 12, task 4): built by mkf18a itself,
# against the nucleus's map and the host ROM, importing from the nucleus; placed by the host placer.
PLACE_SRCS := $(HERE)/src/place.c $(HERE)/src/artifact.c $(HERE)/src/effect.c
TRAIN_PLACE := $(BINDIR)/train-place-64.f18a
$(TRAIN_PLACE): $(BINDIR)/mkf18a-64 $(wildcard $(HERE)/tests/train-place/*.f18) $(SYS_DIR)/map-64.f18 $(BINDIR)/rom-64.c $(CAPSULES)
$(BINDIR)/mkf18a-64 $@ $(SYS_DIR)/map-64.f18 $(BINDIR)/rom-64.c $(HERE)/tests/train-place nucleus=$(SYS_DIR)
MESH_PLACE := -DTRAIN_F18A='"$(TRAIN_PLACE)"'
$(BINDIR)/test_mesh_boot: $(HERE)/tests/test_mesh_boot.c $(MESH_BOOT_SRCS) $(PLACE_SRCS) $(TRAIN_PLACE) $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BLOCKS_SRC) $(V3_BLOCK_SRCS) $(wildcard $(HERE)/include/v4/*.h) $(HERE)/Makefile
$(CC) $(CFLAGS) -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) $(MESH_PLACE) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/test_mesh_boot.o
$(CC) $(SYSTEM_CFLAGS) -c $(HERE)/system/mesh.c -o $(BINDIR)/mesh-64.o
$(CC) $(SYSTEM_CFLAGS) -c $(HERE)/system/inference.c -o $(BINDIR)/inference-64.o
@rm -rf $(BINDIR)/v3o-mesh && mkdir -p $(BINDIR)/v3o-mesh
cd $(BINDIR)/v3o-mesh && $(CC) $(V3_CFLAGS) -I$(HERE)/include -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(V3_BLOCK_SRCS) $(BLOCKS_SRC)
$(CC) $(SYSTEM_CFLAGS) $(BINDIR)/test_mesh_boot.o $(BINDIR)/mesh-64.o $(BINDIR)/inference-64.o $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@
$(CC) $(SYSTEM_CFLAGS) $(BINDIR)/test_mesh_boot.o $(BINDIR)/mesh-64.o $(BINDIR)/inference-64.o $(HERE)/src/manage.c $(PLACE_SRCS) $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@
.PHONY: mesh-boot
mesh-boot: $(BINDIR)/test_mesh_boot
@@ -382,8 +389,8 @@ mesh-boot: $(BINDIR)/test_mesh_boot
# The same with pipelining and the hot-words cache on (#189 Phase 7; built, off as v3 by default).
MESH_ON := -DENABLE_PIPELINING=1 -DENABLE_HOTWORDS_CACHE=1
$(BINDIR)/test_mesh_boot_on: $(BINDIR)/test_mesh_boot
$(CC) $(CFLAGS) $(MESH_ON) -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/test_mesh_boot_on.o
$(CC) $(SYSTEM_CFLAGS) $(MESH_ON) $(BINDIR)/test_mesh_boot_on.o $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@
$(CC) $(CFLAGS) $(MESH_ON) -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) $(MESH_PLACE) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/test_mesh_boot_on.o
$(CC) $(SYSTEM_CFLAGS) $(MESH_ON) $(BINDIR)/test_mesh_boot_on.o $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(PLACE_SRCS) $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@
.PHONY: mesh-boot-on
mesh-boot-on: $(BINDIR)/test_mesh_boot_on
@@ -392,8 +399,8 @@ mesh-boot-on: $(BINDIR)/test_mesh_boot_on
# And without compudynamics on the opcodes: OPCODE_PHYSICS 0, the revert (#189 Phase 7, ruling 7).
$(BINDIR)/test_mesh_boot_noop: $(BINDIR)/test_mesh_boot
$(CC) $(CFLAGS) -DOPCODE_PHYSICS=0 -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/test_mesh_boot_noop.o
$(CC) $(SYSTEM_CFLAGS) -DOPCODE_PHYSICS=0 $(BINDIR)/test_mesh_boot_noop.o $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@
$(CC) $(CFLAGS) -DOPCODE_PHYSICS=0 -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) $(MESH_PLACE) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/test_mesh_boot_noop.o
$(CC) $(SYSTEM_CFLAGS) -DOPCODE_PHYSICS=0 $(BINDIR)/test_mesh_boot_noop.o $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(PLACE_SRCS) $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@
.PHONY: mesh-boot-noop
mesh-boot-noop: $(BINDIR)/test_mesh_boot_noop
@@ -403,10 +410,10 @@ mesh-boot-noop: $(BINDIR)/test_mesh_boot_noop
# The same, under ASan+UBSan (Captain Bob, 2026-11-13: "Yes, add it to v4-checks"): run by `sanitize`.
MESH_SAN := -O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined -fno-sanitize-recover=all
$(BINDIR)/san-test_mesh_boot: $(BINDIR)/test_mesh_boot
$(CC) $(CFLAGS) $(MESH_SAN) -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/san-test_mesh_boot.o
$(CC) $(CFLAGS) $(MESH_SAN) -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) $(MESH_PLACE) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/san-test_mesh_boot.o
@rm -rf $(BINDIR)/san-v3o-mesh && mkdir -p $(BINDIR)/san-v3o-mesh
cd $(BINDIR)/san-v3o-mesh && $(CC) $(V3_CFLAGS) $(MESH_SAN) -I$(HERE)/include -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(V3_BLOCK_SRCS) $(BLOCKS_SRC)
$(CC) $(SYSTEM_CFLAGS) $(MESH_SAN) $(BINDIR)/san-test_mesh_boot.o $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/san-v3o-mesh/*.o -o $@
$(CC) $(SYSTEM_CFLAGS) $(MESH_SAN) $(BINDIR)/san-test_mesh_boot.o $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(PLACE_SRCS) $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/san-v3o-mesh/*.o -o $@
.PHONY: san-mesh-boot
san-mesh-boot: $(BINDIR)/san-test_mesh_boot
+43
View File
@@ -0,0 +1,43 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* place.h -- place a train's F18 artifact on a node: resolution without a linker (ABI.md section 5).
*
* docs/v4.0.0/plans/2026-11-19-f18-artifact-build.md section 12, task 4. The host placer, in C: the
* oracle for the loader on the node (the ADR's step 6), not part of the engine. One pass, in order:
* 1. CHECK, before anything is written (fail closed): the artifact is valid (artifact.h), for this
* node's cell width and size; every import is from the nucleus (hash 0: section 10, ruling 4), its
* name is in the FORTH vocabulary, not in line, and the effect derived for it there (effect.c) is
* the one the artifact expects; the train and its entries fit below DLIMIT.
* 2. PLACE, at HERE, the base. The code is split at each export (ruling 2a): the code before the first
* export goes first, with no entry; then each export's entry, laid as (HEADER) lays one (dict.f18:
* the name, then the word ID, the flags, the name's word address and the link), then its code up to
* the next export's. So every cell of the code moves by the entries laid before it.
* 3. FIX each relocation: a branch field or a whole cell given the moved address of its target, or the
* import's xt. A branch whose target its slot cannot reach from its new place is refused, in step 1.
* 4. ENTER: each entry goes into the vocabulary whose head cell is given (CURRENT's), the next word ID
* is taken from (NEXT-ID), DP is moved past the train, and the kernel is told of each xt, as
* (WORD-DEFINED) is told of an entry (HEADER) makes.
*/
#ifndef V4_PLACE_H
#define V4_PLACE_H
#include "v4/node.h"
#include <stddef.h>
typedef struct {
v4_cell dp; /* word address of DP: the next free byte */
v4_cell dlimit; /* the byte address dictionary space ends at */
v4_cell head; /* the head cell of the vocabulary the entries go into */
v4_cell forth; /* FORTH's head cell, (LATEST): where an import's name is looked up */
v4_cell next_id; /* word address of (NEXT-ID) */
void (*defined)(void *ctx, v4_cell xt); /* the kernel told of an entry made, or NULL */
void *ctx;
} v4_train_env;
/* Place the artifact `a` of `len` bytes on node `n`. 1, with the exports' xts in xts[0 .. *count)
* in the artifact's order, or 0 with why in *why and the node unchanged. */
int v4_train_place(v4_node *n, const unsigned char *a, size_t len, const v4_train_env *env,
v4_cell *xts, unsigned max, unsigned *count, const char **why);
#endif /* V4_PLACE_H */
+190
View File
@@ -0,0 +1,190 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* place.c -- place a train's F18 artifact on a node (place.h). */
#include "v4/place.h"
#include "v4/artifact.h"
#include "v4/effect.h"
#include "v4/iword.h"
#include "v4/opcode.h"
#include <stdlib.h>
#include <string.h>
#define CELL_BYTES (V4_CELL_BITS / 8u)
typedef struct { /* an export, in entry order */
unsigned index; /* its place in the artifact */
uint32_t entry, name, flags; /* its entry's offset in the code; its name's pool offset; its flags */
unsigned cells; /* its entry's cells: the name's, then four */
} exp_t;
static int refuse(const char **why, const char *what) { *why = what; return 0; }
static v4_cell code_cell(const unsigned char *a, const v4_f18a_layout *l, uint32_t i)
{
const unsigned char *p = a + l->code * 4u + (size_t)i * CELL_BYTES;
v4_ucell v = 0;
unsigned k;
for (k = 0; k < CELL_BYTES; k++) v |= (v4_ucell)p[k] << (8u * k);
return (v4_cell)v;
}
/* the counted string at pool offset `off` */
static const unsigned char *pool_str(const unsigned char *a, const v4_f18a_layout *l, uint32_t off)
{
return a + (l->pool + off) * 4u;
}
/* the xt of the entry named by the counted string `s`, from the head cell `head`, skipping hidden ones; or -1 */
static v4_cell lookup(const v4_node *n, v4_cell head, const unsigned char *s)
{
v4_cell xt = n->mem[head];
unsigned k, guard = 0;
while (xt > 4 && xt < (v4_cell)V4_NODE_WORDS && guard++ < V4_NODE_WORDS) {
v4_cell nfa = n->mem[xt - 2];
if (!(n->mem[xt - 3] & 2) && nfa >= 0 && nfa < (v4_cell)V4_NODE_WORDS && (n->mem[nfa] & 255) == s[0]) {
for (k = 1; k <= s[0]; k++)
if (((n->mem[nfa + (v4_cell)(k / 4u)] >> (8u * (k % 4u))) & 255) != s[k]) break;
if (k > s[0]) return xt;
}
xt = n->mem[xt - 1];
}
return -1;
}
/* the moved address of code offset `off` (0 .. c): the base, the offset, and every entry laid at or before it */
static v4_cell moved(const exp_t *e, unsigned ne, v4_cell base, uint32_t off)
{
v4_cell shift = 0;
unsigned j;
for (j = 0; j < ne && e[j].entry <= off; j++) shift += (v4_cell)e[j].cells;
return base + (v4_cell)off + shift;
}
static int by_entry(const void *x, const void *y)
{
const exp_t *p = x, *q = y;
return p->entry < q->entry ? -1 : p->entry > q->entry;
}
int v4_train_place(v4_node *n, const unsigned char *a, size_t len, const v4_train_env *env,
v4_cell *xts, unsigned max, unsigned *count, const char **why)
{
v4_f18a_layout l;
exp_t *e = NULL;
v4_cell *code = NULL, *imp = NULL;
v4_cell base, end, here, head;
v4_effects *fx = NULL;
unsigned j, k;
int ok = 0;
*count = 0;
*why = NULL;
/* 1. CHECK: nothing is written until all of it has passed */
if (!v4_f18a_check(a, len, V4_CELL_BITS, &l, why)) return 0;
if (l.node_words != V4_NODE_WORDS) return refuse(why, "built for another node size");
if (l.e > max) return refuse(why, "more exports than the caller can be told of");
here = (n->mem[env->dp] + 3) / 4;
base = here;
e = calloc(l.e + 1u, sizeof *e);
code = calloc(l.c + 1u, sizeof *code);
imp = calloc(l.i + 1u, sizeof *imp);
if (!e || !code || !imp) { refuse(why, "out of memory"); goto out; }
/* the imports: each from the nucleus, by name in FORTH, called, with the effect expected */
for (k = 0; k < l.i; k++) {
size_t x = l.imports + (size_t)V4_F18A_IMPORT * k;
v4_effect ef;
v4_cell xt;
if (v4_f18a_unit(a, x) != 0 || v4_f18a_unit(a, x + 1) != 0) { refuse(why, "an import from a train, not the nucleus: not placed yet"); goto out; }
xt = lookup(n, env->forth, pool_str(a, &l, v4_f18a_unit(a, x + 2)));
if (xt < 0) {
static char msg[64];
const unsigned char *s = pool_str(a, &l, v4_f18a_unit(a, x + 2));
strcpy(msg, "the dictionary does not have ");
memcpy(msg + strlen(msg), s + 1, s[0]);
msg[29 + s[0]] = 0;
refuse(why, msg);
goto out;
}
if (n->mem[xt - 3] & 8) { refuse(why, "an import in line in the dictionary: it cannot be called"); goto out; }
if (!fx && !(fx = v4_effects_new(n, 0, here, NULL, NULL))) { refuse(why, "out of memory"); goto out; }
ef = v4_effect_of(fx, xt, V4_EFFECT_CALLED);
if (!ef.fixed || v4_f18a_unit(a, x + 3) != (ef.d_taken | (ef.d_left << 16))
|| v4_f18a_unit(a, x + 4) != (ef.r_taken | (ef.r_left << 16))) {
refuse(why, "an import's effect in the dictionary is not the one expected");
goto out;
}
imp[k] = xt;
}
/* the exports, in entry order, and the room they take */
for (j = 0; j < l.e; j++) {
size_t x = l.exports + (size_t)V4_F18A_EXPORT * j;
e[j].index = j;
e[j].name = v4_f18a_unit(a, x);
e[j].entry = v4_f18a_unit(a, x + 1);
e[j].flags = v4_f18a_unit(a, x + 2);
e[j].cells = (1u + pool_str(a, &l, e[j].name)[0] + 3u) / 4u + 4u;
}
qsort(e, l.e, sizeof *e, by_entry);
for (j = 1; j < l.e; j++) if (e[j].entry == e[j - 1].entry) { refuse(why, "two exports at one entry"); goto out; }
end = moved(e, l.e, base, l.c);
if (end * 4 > env->dlimit || end > (v4_cell)V4_NODE_WORDS) { refuse(why, "no room for the train below DLIMIT"); goto out; }
/* 3. FIX, into a copy: each relocation given its target's moved address, or the import's xt */
for (j = 0; j < l.c; j++) code[j] = code_cell(a, &l, j);
for (k = 0; k < l.r; k++) {
size_t x = l.relocs + (size_t)V4_F18A_RELOC * k;
uint32_t u0 = v4_f18a_unit(a, x), to = v4_f18a_unit(a, x + 1);
uint32_t cell = u0 & 0x1FFFFFFFu, slot = u0 >> 29;
v4_cell target, at = moved(e, l.e, base, cell);
if (slot == V4_F18A_WHOLE) {
if (to == V4_F18A_INTERNAL) {
if (code_cell(a, &l, cell) < 0 || code_cell(a, &l, cell) > (v4_cell)l.c) { refuse(why, "an address outside the code"); goto out; }
target = moved(e, l.e, base, (uint32_t)code_cell(a, &l, cell));
} else target = imp[to];
code[cell] = target;
} else {
v4_iword w = (v4_iword)((v4_ucell)code[cell] & 0xFFFFFFFFu);
uint32_t mask = v4_iword_slot_mask(slot);
if (to == V4_F18A_INTERNAL) {
v4_cell old = v4_iword_branch((v4_cell)cell + 1, (v4_iword)((v4_ucell)code_cell(a, &l, cell) & 0xFFFFFFFFu), slot);
if (old < 0 || old > (v4_cell)l.c) { refuse(why, "a branch outside the code"); goto out; }
target = moved(e, l.e, base, (uint32_t)old);
} else target = imp[to];
w = (v4_iword)((w & ~mask) | ((uint32_t)target & mask));
if (v4_iword_branch(at + 1, w, slot) != target) { refuse(why, "a branch whose slot cannot reach its target from where it is placed"); goto out; }
code[cell] = (v4_cell)(((v4_ucell)code[cell] & ~(v4_ucell)0xFFFFFFFFu) | (v4_ucell)w);
}
}
/* 2. PLACE and 4. ENTER: the code, split at each export, its entry before it */
for (j = 0; j < l.c; j++) n->mem[moved(e, l.e, base, j)] = code[j];
head = env->head;
for (j = 0; j < l.e; j++) {
const unsigned char *s = pool_str(a, &l, e[j].name);
v4_cell xt = moved(e, l.e, base, e[j].entry), nfa = xt - (v4_cell)e[j].cells;
unsigned c, ncells = e[j].cells - 4u;
for (c = 0; c < ncells; c++) n->mem[nfa + (v4_cell)c] = 0;
for (c = 0; c <= s[0]; c++) n->mem[nfa + (v4_cell)(c / 4u)] |= (v4_cell)s[c] << (8u * (c % 4u));
n->mem[xt - 4] = n->mem[env->next_id];
n->mem[env->next_id] = n->mem[env->next_id] + 1;
n->mem[xt - 3] = (v4_cell)e[j].flags;
n->mem[xt - 2] = nfa;
n->mem[xt - 1] = n->mem[head];
n->mem[head] = xt;
xts[e[j].index] = xt;
}
n->mem[env->dp] = end * 4;
*count = l.e;
for (j = 0; j < l.e; j++) if (env->defined) env->defined(env->ctx, moved(e, l.e, base, e[j].entry));
ok = 1;
out:
if (fx) v4_effects_free(fx);
free(e);
free(code);
free(imp);
return ok;
}
+116
View File
@@ -15,6 +15,8 @@
#include "v4/mesh.h"
#include "v4/message.h"
#include "v4/post.h"
#include "v4/place.h"
#include "v4/artifact.h"
#include "block_subsystem.h"
#include <stdio.h>
#include <string.h>
@@ -102,6 +104,50 @@ static const char *line(v4_cell node, const char *text, int *how)
/* the tick set for a check (v4_mesh_set_tick), and the heartbeat's clock with it, so no catch-up follows */
static void set_tick(uint64_t t) { v4_mesh_set_tick(&ms, t); heart = t; }
/* ---- the placed train (docs/v4.0.0/plans/2026-11-19-f18-artifact-build.md section 12, task 4) ---- */
static v4_cell told[16];
static unsigned ntold;
/* the kernel told of an entry, as (DEFINED) tells it (dict.f18): the word in (WORD-DEFINED) run with the xt */
static void tell(void *ctx, v4_cell xt)
{
char cmd[96];
int how_;
v4_cell w = v4_mesh_node(&ms, V4_MESH_HERA)->n.mem[im.word_defined];
(void)ctx;
if (ntold < 16u) told[ntold++] = xt;
if (w == 0) return;
snprintf(cmd, sizeof cmd, "%ld %ld EXECUTE", (long)xt, (long)w);
(void)line(V4_MESH_HERA, cmd, &how_);
}
static unsigned char *slurp(const char *path, size_t *len)
{
FILE *f = fopen(path, "rb");
unsigned char *b;
long n;
if (!f) return 0;
fseek(f, 0, SEEK_END); n = ftell(f); fseek(f, 0, SEEK_SET);
b = malloc(n > 0 ? (size_t)n : 1u);
if (b && fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); b = 0; }
fclose(f);
*len = (size_t)n;
return b;
}
static v4_cell mem_before[V4_NODE_WORDS];
/* a refusal leaves Hera as she was */
static int refused_unchanged(v4_node *hn, const unsigned char *a, size_t len, const v4_train_env *env, const char *what)
{
v4_cell xts[8];
unsigned cnt = 99;
const char *why = 0;
int ok;
memcpy(mem_before, hn->mem, sizeof mem_before);
ntold = 0;
ok = v4_train_place(hn, a, len, env, xts, 8, &cnt, &why);
if (ok) { printf(" placed, not refused\n"); return 0; }
if (!why || !strstr(why, what)) { printf(" refused: %s\n", why ? why : "(no reason)"); return 0; }
return memcmp(mem_before, hn->mem, sizeof mem_before) == 0 && ntold == 0 && cnt == 0;
}
int main(void)
{
int how;
@@ -650,6 +696,76 @@ int main(void)
}
#endif
/* A TRAIN PLACED (plan 2026-11-19-f18-artifact-build section 12, task 4; ABI.md section 5): the small
* train tests/train-place, built by mkf18a with one import from the nucleus, placed on Hera at her HERE
* by the host placer. Its words run by name, its calls into the nucleus reach the nucleus, and the
* kernel has a record of each. Then what placement refuses, each leaving Hera unchanged. */
{
size_t alen = 0;
unsigned char *art = slurp(TRAIN_F18A, &alen);
v4_fabric_node *hf = v4_mesh_node(&ms, V4_MESH_HERA);
v4_node *hn = &hf->n;
v4_train_env env;
v4_cell xts[8], here0, cur;
unsigned cnt = 0, k;
const char *why = 0;
char *end;
v4_f18a_layout lay;
CHECK(art != 0 && v4_f18a_check(art, alen, 64, &lay, &why), "the train's artifact, built by mkf18a: %s", why ? why : TRAIN_F18A);
if (art) {
r = line(V4_MESH_HERA, "CURRENT @ . HERE .", &how);
cur = (v4_cell)strtol(r, &end, 10);
here0 = (v4_cell)strtol(end, 0, 10);
env.dp = im.dp; env.dlimit = im.dict_hi * 4; env.head = cur; env.forth = im.latest; env.next_id = im.next_id;
env.defined = tell; env.ctx = 0;
/* what it refuses first, Hera unchanged by each */
{
unsigned char *c = malloc(alen);
size_t nameu = lay.pool + v4_f18a_unit(art, lay.imports + 2);
memcpy(c, art, alen); c[0] ^= 1;
CHECK(refused_unchanged(hn, c, alen, &env, "magic"), "an artifact the checker refuses");
memcpy(c, art, alen); c[3 * 4] ^= 1;
CHECK(refused_unchanged(hn, c, alen, &env, "node"), "an artifact for another node size");
memcpy(c, art, alen); c[nameu * 4 + 3] = 'X';
CHECK(refused_unchanged(hn, c, alen, &env, "ABX"), "an import the dictionary lacks: ABX");
memcpy(c, art, alen); c[(lay.imports + 3) * 4] = 2;
CHECK(refused_unchanged(hn, c, alen, &env, "effect"), "an import whose effect is not the one expected");
memcpy(c, art, alen); c[lay.imports * 4] = 1;
CHECK(refused_unchanged(hn, c, alen, &env, "train"), "an import from a train, not the nucleus: not placed yet");
{
v4_train_env small = env;
small.dlimit = (here0 + 4) * 4;
CHECK(refused_unchanged(hn, art, alen, &small, "room"), "no room for it below DLIMIT");
}
free(c);
}
ntold = 0;
CHECK(v4_train_place(hn, art, alen, &env, xts, 8, &cnt, &why), "placed on Hera: %s", why ? why : "");
CHECK(cnt == lay.e && cnt == 4 && ntold == cnt, "four exports entered, and the kernel told of each (%u, %u)", cnt, ntold);
r = line(V4_MESH_HERA, "HERE .", &how);
CHECK((v4_cell)strtol(r, 0, 10) > here0 && here0 != 0, "HERE moved past it, from %ld: \"%s\"", (long)here0, r);
r = line(V4_MESH_HERA, "' DIST .", &how);
CHECK((v4_cell)strtol(r, 0, 10) == xts[0] && xts[0] > here0, "DIST is found by its name, at %ld: \"%s\"", (long)xts[0], r);
for (k = 0; k < cnt && k < 8; k++)
CHECK(v4_mesh_account_at(&ms, 0, xts[k]) != 0, "the kernel keeps a record of export %u", k);
r = line(V4_MESH_HERA, "7 3 DIST . 3 7 DIST .", &how);
CHECK(strcmp(r, "4 4 ") == 0, "DIST, through the nucleus's ABS: \"%s\"", r);
r = line(V4_MESH_HERA, "3 7 DIST2 .", &how);
CHECK(strcmp(r, "8 ") == 0, "DIST2 calls DIST, moved: \"%s\"", r);
r = line(V4_MESH_HERA, "-5 CLAMP0 . 5 CLAMP0 .", &how);
CHECK(strcmp(r, "0 5 ") == 0, "CLAMP0's forward branch, moved: \"%s\"", r);
r = line(V4_MESH_HERA, "ADDR ' DIST = .", &how);
CHECK(strcmp(r, "-1 ") == 0, "ADDR's address literal is DIST's xt: \"%s\"", r);
r = line(V4_MESH_HERA, ": PLACED-USE 3 7 DIST2 ; PLACED-USE .", &how);
CHECK(strcmp(r, "8 ") == 0, "a word compiled at the prompt calls a placed one: \"%s\"", r);
r = line(V4_MESH_HERA, "FORGET PLACED-USE 3 4 + .", &how);
CHECK(strcmp(r, "7 ") == 0, "and the prompt goes on: \"%s\"", r);
}
free(art);
}
printf(" %d checks, %d failures\n", checks, failures);
return failures ? 1 : 0;
}
+12
View File
@@ -325,6 +325,18 @@ int main(void)
free(c);
}
/* a word that runs on into an export (section 12, 2a as corrected): placement splits the code at each
* export, so the tool refuses it; one that runs on into a word that is not an export is not split */
put(SCRATCH "/r11", "00.f18", ": A dup\n: B drop ;\n");
CHECK(refused(SCRATCH "/r11", NULL, 0, "runs on"), "a word that runs on into the next export");
put(SCRATCH "/r12", "00.f18", ": X dup\n: L FOR dup NEXT ;\n");
{
size_t l12;
unsigned char *c = build(SCRATCH "/r12", NULL, 0, &l12);
CHECK(c != NULL, "one that runs on into a word that is not exported builds: %s", err);
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");
+4
View File
@@ -0,0 +1,4 @@
\ the small train placed by test_mesh_boot (docs/v4.0.0/plans/2026-11-19-f18-artifact-build.md section 12)
\ ( a b -- |a-b| ), calling the nucleus's ABS
import nucleus ABS
: DIST inv 1 + + ABS ;
+4
View File
@@ -0,0 +1,4 @@
\ calls within the train, a forward branch, and an address literal
: DIST2 DIST dup + ;
: CLAMP0 -if K drop 0 K: ;
: ADDR &DIST ;
+30
View File
@@ -175,6 +175,16 @@ static int import_effect(void *ctx, v4_cell addr, v4_effect *out)
return 0;
}
/* For the run-on check: a word walked with its range ending at the next word, and every other address of
* the train known through the whole train's walk, as a call or a tail call. */
typedef struct { build_t *b; v4_effects *whole; v4_cell end; } run_on_t;
static int run_on_effect(void *ctx, v4_cell addr, v4_effect *out)
{
run_on_t *ro = ctx;
if (addr >= 0 && addr < ro->end) { *out = v4_effect_of(ro->whole, addr, V4_EFFECT_CALLED); return 1; }
return import_effect(ro->b, addr, out);
}
/* 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)
@@ -489,6 +499,26 @@ int f18a_build(const f18a_args *a, unsigned char **out, size_t *len, char *err,
e++;
}
/* a word that runs on into an export (plan section 12, 2a as corrected): placement splits the code at
* each export, putting its entry there, so the word before it must not reach it but by a call or a jump */
for (k = 0; k + 1u < nwords && !b.failed; k++) {
v4_text_word_info nx;
v4_effect ef;
v4_effects *one;
run_on_t ro;
v4_text_word_at(&tx, k, &wi);
v4_text_word_at(&tx, k + 1u, &nx);
if (nx.addr <= wi.addr) continue;
if (!v4_effect_of(fx, nx.addr, (nx.flags & 8) ? V4_EFFECT_INLINE : V4_EFFECT_CALLED).fixed) continue;
ro.b = &b; ro.whole = fx; ro.end = code_end;
one = v4_effects_new(&n, wi.addr, nx.addr, run_on_effect, &ro);
if (!one) { fail(&b, "out of memory"); break; }
ef = v4_effect_of(one, wi.addr, (wi.flags & 8) ? V4_EFFECT_INLINE : V4_EFFECT_CALLED);
v4_effects_free(one);
if (!ef.fixed && ef.why && strcmp(ef.why, "it runs outside the code") == 0 && ef.at == nx.addr)
fail(&b, "%s runs on into %s, an export: placement splits the code there", wi.name, nx.name);
}
/* the relocations: every branch can reach every word of a node */
for (k = 0; k < v4_text_addresses(&tx) && !b.failed; k++) {
const v4_text_addr *r = &b.rec[k];
+2 -1
View File
@@ -21,7 +21,8 @@
* V4_ROM_WORDS=..", framed as "config", then the map. The ROM: the ROM's file as given.
* It refuses what the ABI cannot carry: a header, a store or a start in the source; an export's name
* over 31 characters; a port or a request it can see that the word does not declare, or one declared by
* a word it calls.
* a word it calls; a word that runs on into the next export with no call or jump, since placement splits
* the code at each export (plan section 12, 2a).
*/
#ifndef V4_TOOLS_F18A_H
#define V4_TOOLS_F18A_H