v4 text.c: the address record (ADR step 3, task 1)
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:
@@ -24,13 +24,16 @@ static int failures = 0, checks = 0;
|
||||
static v4_node a, b;
|
||||
static v4_text tx;
|
||||
|
||||
/* node A: text.c assembles the system's source, map first and end last */
|
||||
/* node A: text.c assembles the system's source, map first and end last, with the address record on
|
||||
* (plans/2026-11-19-f18-artifact-build.md §4 test 4): the record must change no cell. */
|
||||
static v4_text_addr rec[1u << 15];
|
||||
static int build_a(void)
|
||||
{
|
||||
char path[512];
|
||||
unsigned i;
|
||||
v4_node_reset(&a);
|
||||
v4_text_begin(&tx, &a, ROM_ORIGIN);
|
||||
v4_text_record(&tx, rec, (unsigned)(sizeof rec / sizeof rec[0]));
|
||||
for (i = 0; i < ROM_SYS_FILES + 2u; i++) {
|
||||
if (i == 0) snprintf(path, sizeof path, "%s/map-%d.f18", V4_SYS_DIR, V4_CELL_BITS);
|
||||
else if (i <= ROM_SYS_FILES) snprintf(path, sizeof path, "%s/%s.f18", V4_SYS_DIR, rom_system_files[i - 1]);
|
||||
@@ -57,6 +60,20 @@ int main(void)
|
||||
CHECK(build_a(), "text.c assembles the system's source to its start: %s", v4_text_error(&tx));
|
||||
idle_a = v4_text_start(&tx);
|
||||
CHECK(idle_a == v4_text_word(&tx, "(IDLE)"), "the system starts at (IDLE)");
|
||||
{
|
||||
unsigned i, branches = 0, whole = 0, bad = 0;
|
||||
v4_cell end = v4_text_here(&tx);
|
||||
for (i = 0; i < v4_text_addresses(&tx); i++) {
|
||||
if (rec[i].slot == V4_TEXT_WHOLE_CELL) {
|
||||
whole++;
|
||||
if (a.mem[rec[i].cell] < (v4_cell)ROM_ORIGIN || a.mem[rec[i].cell] > end) bad++;
|
||||
} else if (rec[i].slot < 4u) branches++;
|
||||
else bad++;
|
||||
}
|
||||
printf(" address record: %u branches, %u whole cells\n", branches, whole);
|
||||
CHECK(branches > 0 && whole > 0, "the address record holds the system's branches and whole-cell addresses");
|
||||
CHECK(bad == 0, "every whole cell recorded holds an address of the assembly, every slot is 0-3 or 7: %u not", bad);
|
||||
}
|
||||
|
||||
t0 = clock();
|
||||
CHECK(rom_build_system(&b, &words), "the ROM started the system: P %ld, failed %ld at line %ld", (long)b.p,
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user