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