v4 text.c: the address record (ADR step 3, task 1)
Build / Image RASPI (push) Successful in 4m13s
Build / Image PC (push) Successful in 3m51s
Build / Image MILKV (push) Successful in 4m30s
Build / v4 tests (32- and 64-bit cells) (push) Successful in 17m32s

Every cell laid down that holds an address of the assembly is recorded,
with its branch slot or 7 for the whole cell, as ARTIFACT.md's
relocations need. Test first: test_text 13 checks failed before.
test_host_rom_nucleus runs with the record on: 1825 branches, 664 whole
cells, the ROM still equal word for word. v4 test, test-32 and
ASan+UBSan pass.

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-19 17:51:54 +00:00
parent 29b99f90e6
commit 4d64e677f5
11 changed files with 1898 additions and 14 deletions
+67
View File
@@ -223,6 +223,30 @@ static int refused(const char *src, const char *what, unsigned line)
if (line && strncmp(v4_text_error(&tx), want, strlen(want)) != 0) { printf(" message was: %s\n", v4_text_error(&tx)); return 0; }
return 1;
}
/* The same with the address record on, `cap` entries. */
static v4_text_addr rec[64];
static int rec_ok(const char *src, unsigned cap)
{
v4_node_reset(&nb);
v4_text_begin(&tx, &nb, ORIGIN);
v4_text_constant(&tx, "VAR", VARS);
v4_text_record(&tx, rec, cap);
return v4_text_assemble(&tx, src) && v4_text_finish(&tx);
}
static int refused_rec(const char *src, unsigned cap, const char *what)
{
if (rec_ok(src, cap)) return 0;
if (strstr(v4_text_error(&tx), what) == NULL) { printf(" message was: %s\n", v4_text_error(&tx)); return 0; }
return 1;
}
/* 1 if the record holds (cell, slot) */
static int has(v4_cell cell, unsigned slot)
{
unsigned i;
for (i = 0; i < v4_text_addresses(&tx); i++) if (rec[i].cell == cell && rec[i].slot == slot) return 1;
for (i = 0; i < v4_text_addresses(&tx); i++) printf(" recorded: %ld slot %u\n", (long)rec[i].cell, rec[i].slot);
return 0;
}
/* Run the word NAME of the last assembly on up to two arguments; its one result. */
static v4_cell result(const char *name, unsigned argc, v4_cell a, v4_cell b)
{
@@ -568,6 +592,49 @@ int main(void)
"set, here, store and start are reserved");
}
/* THE ADDRESS RECORD (plans/2026-11-19-f18-artifact-build.md §2, §4 test 1): with it on, every cell
* laid down that holds an address of this assembly is recorded, as ARTIFACT.md §5's relocations
* need it: the cell, and the slot whose branch field holds it (0-3) or 7 for the whole cell. */
{
static v4_cell before[sizeof nb.mem / sizeof nb.mem[0]];
CHECK(rec_ok(": A ; : B A ;", 8) && v4_text_addresses(&tx) == 1 && has(ORIGIN + 1, 0),
"a call to a word defined already: its cell, slot 0");
CHECK(rec_ok(": B C ; : C ;", 8) && v4_text_addresses(&tx) == 1 && has(ORIGIN, 0),
"a call to a word defined later, once it is resolved");
CHECK(rec_ok(": A ; : B dup A ;", 8) && v4_text_addresses(&tx) == 1 && has(ORIGIN + 1, 1),
"a branch in slot 1: its slot");
CHECK(rec_ok(": W L: dup if L ;", 8) && v4_text_addresses(&tx) == 1 && has(ORIGIN, 1),
"a branch to a label of the word");
CHECK(rec_ok(": F 3 FOR dup drop NEXT ;", 8) && v4_text_addresses(&tx) == 1 && rec[0].slot < 4u,
"FOR ... NEXT: the next's branch");
CHECK(rec_ok(": A ; : X &A drop ;", 8) && v4_text_addresses(&tx) == 1 && has(ORIGIN + 2, V4_TEXT_WHOLE_CELL),
"&NAME in code: the literal's cell, whole");
CHECK(rec_ok(": A ; #, &A", 8) && v4_text_addresses(&tx) == 1 && has(ORIGIN + 1, V4_TEXT_WHOLE_CELL),
"#, &NAME: its cell, whole");
CHECK(rec_ok("set P here #, P", 8) && v4_text_addresses(&tx) == 1 && has(ORIGIN, V4_TEXT_WHOLE_CELL),
"a set variable that holds here is an address");
CHECK(rec_ok(": A ; set P &A : X P drop ;", 8) && v4_text_addresses(&tx) == 1 && has(ORIGIN + 2, V4_TEXT_WHOLE_CELL),
"a set variable that holds &NAME, as a literal in code");
CHECK(rec_ok("set P here set P 5 #, P", 8) && v4_text_addresses(&tx) == 0,
"a set variable set again to a number is not an address");
CHECK(rec_ok(": Y VAR drop ; #, VAR constant K 5 #, K+1 #, 7", 8) && v4_text_addresses(&tx) == 0,
"constants and numbers are not addresses of the assembly");
CHECK(rec_ok("header N1 : N1 ; header N2 : N2 ;", 8) && v4_text_addresses(&tx) == 3
&& has(ORIGIN + 3, V4_TEXT_WHOLE_CELL) && has(ORIGIN + 9, V4_TEXT_WHOLE_CELL) && has(ORIGIN + 10, V4_TEXT_WHOLE_CELL),
"a header: its name's cell; its link when there is an entry before it");
CHECK(rec_ok(": T ; store 300 &T store 301 5", 8) && v4_text_addresses(&tx) == 1 && has(300, V4_TEXT_WHOLE_CELL),
"store of an address: the cell stored");
CHECK(refused_rec(": A ; : B A A ;", 1, "too many addresses"), "a record that is full is an error");
CHECK(ok(": A ; : B A ;") && v4_text_addresses(&tx) == 0, "with the record off, nothing is recorded");
static char four[sizeof source + 32];
snprintf(four, sizeof four, "constant N-1 %d\n%s", V4_CELL_BITS - 1, source);
CHECK(ok(four), "the four definitions assemble: %s", v4_text_error(&tx));
memcpy(before, nb.mem, sizeof before);
CHECK(rec_ok(four, 64) && memcmp(before, nb.mem, sizeof before) == 0 && v4_text_addresses(&tx) > 0,
"the record changes no cell");
}
CHECK(v4_node_guards_intact(&na) && v4_node_guards_intact(&nb), "guards intact");
printf(" %d checks, %d failures\n", checks, failures);