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:
@@ -111,9 +111,22 @@
|
||||
typedef struct {
|
||||
unsigned name; /* offset into pool */
|
||||
unsigned char kind;
|
||||
unsigned char addr; /* a set variable: its value is an address of the assembly */
|
||||
v4_cell value; /* address, constant, or macro text offset */
|
||||
} v4_text_symbol;
|
||||
|
||||
/* THE ADDRESS RECORD (docs/v4.0.0/plans/2026-11-19-f18-artifact-build.md §2). A cell laid down
|
||||
* that holds an address of this assembly -- a word's or a label's, `here`, or a set variable that
|
||||
* holds one -- as ARTIFACT.md §5's relocations need it: the cell, and the slot (0-3) whose branch
|
||||
* field holds the address, or V4_TEXT_WHOLE_CELL when the whole cell does (an &NAME literal, a
|
||||
* header's name and link, `#,`, `store`). The map's constants and numbers are not addresses of
|
||||
* the assembly, and are not recorded. */
|
||||
#define V4_TEXT_WHOLE_CELL 7u
|
||||
typedef struct {
|
||||
v4_cell cell;
|
||||
unsigned slot;
|
||||
} v4_text_addr;
|
||||
|
||||
typedef struct {
|
||||
unsigned name;
|
||||
v4_asm_ref ref;
|
||||
@@ -144,6 +157,9 @@ typedef struct {
|
||||
int header_pending; /* a header waits for its `:` */
|
||||
v4_cell next_id; /* the next word ID a header is given (#189, Phase 2) */
|
||||
|
||||
v4_text_addr *rec; /* the address record, or NULL when it is off */
|
||||
unsigned rec_cap, nrec;
|
||||
|
||||
unsigned line;
|
||||
int failed;
|
||||
char message[224];
|
||||
@@ -152,6 +168,13 @@ typedef struct {
|
||||
/* Start assembling at word address `origin` of node `n`. */
|
||||
void v4_text_begin(v4_text *t, v4_node *n, v4_cell origin);
|
||||
|
||||
/* Turn the address record on, into `buf`, which holds `cap`: every address laid down from now on
|
||||
* is recorded. More than `cap` is an error. v4_text_begin turns it off. It changes no cell. */
|
||||
void v4_text_record(v4_text *t, v4_text_addr *buf, unsigned cap);
|
||||
|
||||
/* How many addresses have been recorded. */
|
||||
unsigned v4_text_addresses(const v4_text *t);
|
||||
|
||||
/* Give NAME a value; written in the text it is a literal. */
|
||||
void v4_text_constant(v4_text *t, const char *name, v4_cell value);
|
||||
|
||||
|
||||
+62
-13
@@ -169,6 +169,26 @@ static int raw_token(v4_text *t, reader *r, char *tok)
|
||||
}
|
||||
|
||||
/* One cell appended at the next word address; nothing is being built. */
|
||||
/* The address record: `cell` holds an address of this assembly, in the branch field of `slot`, or
|
||||
* whole (V4_TEXT_WHOLE_CELL). Nothing when the record is off. */
|
||||
static void note(v4_text *t, v4_cell cell, unsigned slot)
|
||||
{
|
||||
if (!t->rec || t->failed) return;
|
||||
if (t->nrec == t->rec_cap) { fail(t, "too many addresses for the address record", NULL); return; }
|
||||
t->rec[t->nrec].cell = cell;
|
||||
t->rec[t->nrec].slot = slot;
|
||||
t->nrec++;
|
||||
}
|
||||
|
||||
/* `@p` and a literal that is an address of this assembly: the literal's cell follows the instruction
|
||||
* word, after the literals before it in that word. */
|
||||
static void lit_addr(v4_text *t, v4_cell value)
|
||||
{
|
||||
v4_cell cell = t->as.here + 1 + (v4_cell)t->as.nlit;
|
||||
v4_asm_lit(&t->as, value);
|
||||
if (!t->as.error) note(t, cell, V4_TEXT_WHOLE_CELL);
|
||||
}
|
||||
|
||||
static void put_cell(v4_text *t, v4_cell value)
|
||||
{
|
||||
v4_asm *as = &t->as;
|
||||
@@ -216,7 +236,9 @@ static void header(v4_text *t, reader *r)
|
||||
lay_name(t, name);
|
||||
put_cell(t, t->next_id++);
|
||||
put_cell(t, 0);
|
||||
note(t, t->as.here, V4_TEXT_WHOLE_CELL); /* the name's address */
|
||||
put_cell(t, nfa);
|
||||
if (t->latest) note(t, t->as.here, V4_TEXT_WHOLE_CELL); /* the link, when there is an entry before */
|
||||
put_cell(t, t->latest);
|
||||
check_asm(t, name);
|
||||
if (t->failed) return;
|
||||
@@ -242,9 +264,12 @@ static void check_asm(v4_text *t, const char *tok)
|
||||
|
||||
static void branch_to(v4_text *t, unsigned op, v4_cell target, const char *tok)
|
||||
{
|
||||
v4_asm_ref ref;
|
||||
place_branch(t);
|
||||
v4_asm_branch(&t->as, op, target);
|
||||
ref = v4_asm_branch_fwd(&t->as, op); /* v4_asm_branch, with its ref kept for the record */
|
||||
v4_asm_resolve(&t->as, ref, target);
|
||||
check_asm(t, tok);
|
||||
if (!t->failed) note(t, ref.addr, ref.slot);
|
||||
}
|
||||
|
||||
static void branch_later(v4_text *t, unsigned op, const char *name, int local)
|
||||
@@ -292,7 +317,7 @@ static void resolve(v4_text *t, unsigned i, v4_cell target)
|
||||
t->line = t->ref[i].line;
|
||||
fail(t, "branch cannot reach", t->pool + t->ref[i].name);
|
||||
t->line = line;
|
||||
}
|
||||
} else note(t, t->ref[i].ref.addr, t->ref[i].ref.slot);
|
||||
t->ref[i] = t->ref[--t->nref];
|
||||
}
|
||||
|
||||
@@ -324,6 +349,7 @@ static void define(v4_text *t, const char *name, unsigned char kind, v4_cell val
|
||||
t->sym[t->nsym].name = intern(t, name);
|
||||
t->sym[t->nsym].kind = kind;
|
||||
t->sym[t->nsym].value = value;
|
||||
t->sym[t->nsym].addr = 0;
|
||||
if (!t->failed) t->nsym++;
|
||||
}
|
||||
|
||||
@@ -375,11 +401,12 @@ static void constant_directive(v4_text *t, reader *r)
|
||||
/* A VALUE in a directive: a number, a constant, a set variable, CONST+N,
|
||||
* here, or &NAME of a word already defined. 1 and its value, or 0 having
|
||||
* failed. */
|
||||
static int value_of(v4_text *t, const char *tok, v4_cell *out)
|
||||
static int value_of(v4_text *t, const char *tok, v4_cell *out, int *addr)
|
||||
{
|
||||
const v4_text_symbol *s;
|
||||
int big;
|
||||
if (strcmp(tok, "here") == 0) { *out = v4_asm_label(&t->as); return 1; }
|
||||
*addr = 0;
|
||||
if (strcmp(tok, "here") == 0) { *out = v4_asm_label(&t->as); *addr = 1; return 1; }
|
||||
if (strcmp(tok, "#ids") == 0) { *out = t->next_id; return 1; } /* how many word IDs so far */
|
||||
if (number_of(tok, out, &big)) {
|
||||
if (big) { fail(t, "number does not fit a cell", tok); return 0; }
|
||||
@@ -387,12 +414,12 @@ static int value_of(v4_text *t, const char *tok, v4_cell *out)
|
||||
}
|
||||
if (tok[0] == '&' && tok[1] != 0) {
|
||||
s = find(t, tok + 1);
|
||||
if (s && s->kind == KIND_WORD) { *out = s->value; return 1; }
|
||||
if (s && s->kind == KIND_WORD) { *out = s->value; *addr = 1; return 1; }
|
||||
fail(t, "& needs a word that is already defined", tok + 1);
|
||||
return 0;
|
||||
}
|
||||
s = find(t, tok);
|
||||
if (s && (s->kind == KIND_CONST || s->kind == KIND_SET)) { *out = s->value; return 1; }
|
||||
if (s && (s->kind == KIND_CONST || s->kind == KIND_SET)) { *out = s->value; *addr = s->kind == KIND_SET && s->addr; return 1; }
|
||||
if (!s) {
|
||||
const char *plus = strrchr(tok, '+');
|
||||
if (plus && plus != tok && plus[1] != 0) {
|
||||
@@ -418,10 +445,12 @@ static void cell_directive(v4_text *t, reader *r)
|
||||
{
|
||||
char tok[V4_TEXT_MAX_TOKEN + 1u];
|
||||
v4_cell v;
|
||||
int addr;
|
||||
if (r->depth != 1) { fail(t, "not allowed in a macro", "#,"); return; }
|
||||
if (!next_token(t, r, tok)) { if (!t->failed) fail(t, "#, without a value", NULL); return; }
|
||||
if (!value_of(t, tok, &v)) return;
|
||||
if (!value_of(t, tok, &v, &addr)) return;
|
||||
(void)v4_asm_label(&t->as);
|
||||
if (addr) note(t, t->as.here, V4_TEXT_WHOLE_CELL);
|
||||
put_cell(t, v);
|
||||
check_asm(t, "#,");
|
||||
}
|
||||
@@ -451,15 +480,17 @@ static void set_directive(v4_text *t, reader *r)
|
||||
{
|
||||
char name[V4_TEXT_MAX_TOKEN + 1u], tok[V4_TEXT_MAX_TOKEN + 1u];
|
||||
v4_cell v;
|
||||
int addr;
|
||||
v4_text_symbol *s;
|
||||
|
||||
if (r->depth != 1) { fail(t, "not allowed in a macro", "set"); return; }
|
||||
if (!next_token(t, r, name)) { if (!t->failed) fail(t, "set without a name", NULL); return; }
|
||||
if (!next_token(t, r, tok)) { if (!t->failed) fail(t, "set without a value", name); return; }
|
||||
if (!value_of(t, tok, &v)) return;
|
||||
if (!value_of(t, tok, &v, &addr)) return;
|
||||
s = (v4_text_symbol *)find(t, name);
|
||||
if (s && s->kind == KIND_SET) { s->value = v; return; }
|
||||
if (s && s->kind == KIND_SET) { s->value = v; s->addr = (unsigned char)addr; return; }
|
||||
define(t, name, KIND_SET, v);
|
||||
if (!t->failed) t->sym[t->nsym - 1u].addr = (unsigned char)addr;
|
||||
}
|
||||
|
||||
/* `store ADDR VALUE`: a word of RAM is given VALUE. */
|
||||
@@ -467,14 +498,16 @@ static void store_directive(v4_text *t, reader *r)
|
||||
{
|
||||
char at[V4_TEXT_MAX_TOKEN + 1u], tok[V4_TEXT_MAX_TOKEN + 1u];
|
||||
v4_cell addr, v;
|
||||
int at_addr, v_addr;
|
||||
|
||||
if (r->depth != 1) { fail(t, "not allowed in a macro", "store"); return; }
|
||||
if (!next_token(t, r, at)) { if (!t->failed) fail(t, "store without an address", NULL); return; }
|
||||
if (!value_of(t, at, &addr)) return;
|
||||
if (!value_of(t, at, &addr, &at_addr)) return;
|
||||
if (!next_token(t, r, tok)) { if (!t->failed) fail(t, "store without a value", NULL); return; }
|
||||
if (!value_of(t, tok, &v)) return;
|
||||
if (!value_of(t, tok, &v, &v_addr)) return;
|
||||
if (addr < 0 || (v4_ucell)addr >= V4_NODE_WORDS) { fail(t, "address out of range", at); return; }
|
||||
v4_node_store(t->as.n, addr, v);
|
||||
if (v_addr) note(t, addr, V4_TEXT_WHOLE_CELL);
|
||||
}
|
||||
|
||||
/* `start NAME`: the text has ended; NAME is where the system starts. */
|
||||
@@ -589,7 +622,7 @@ static void one_token(v4_text *t, reader *r, const char *tok)
|
||||
if (tok[0] == '&' && tok[1] != 0) { /* the address of a word, as a literal */
|
||||
s = find(t, tok + 1);
|
||||
if (!s || s->kind != KIND_WORD) { fail(t, "& needs a word that is already defined", tok + 1); return; }
|
||||
v4_asm_lit(as, s->value);
|
||||
lit_addr(t, s->value);
|
||||
check_asm(t, tok);
|
||||
return;
|
||||
}
|
||||
@@ -607,7 +640,8 @@ static void one_token(v4_text *t, reader *r, const char *tok)
|
||||
return;
|
||||
}
|
||||
if (s && (s->kind == KIND_CONST || s->kind == KIND_SET)) {
|
||||
v4_asm_lit(as, s->value);
|
||||
if (s->kind == KIND_SET && s->addr) lit_addr(t, s->value);
|
||||
else v4_asm_lit(as, s->value);
|
||||
check_asm(t, tok);
|
||||
return;
|
||||
}
|
||||
@@ -645,6 +679,21 @@ void v4_text_begin(v4_text *t, v4_node *n, v4_cell origin)
|
||||
t->start = -1;
|
||||
t->header_pending = 0;
|
||||
t->next_id = 0;
|
||||
t->rec = NULL;
|
||||
t->rec_cap = 0;
|
||||
t->nrec = 0;
|
||||
}
|
||||
|
||||
void v4_text_record(v4_text *t, v4_text_addr *buf, unsigned cap)
|
||||
{
|
||||
t->rec = buf;
|
||||
t->rec_cap = cap;
|
||||
t->nrec = 0;
|
||||
}
|
||||
|
||||
unsigned v4_text_addresses(const v4_text *t)
|
||||
{
|
||||
return t->nrec;
|
||||
}
|
||||
|
||||
void v4_text_constant(v4_text *t, const char *name, v4_cell value)
|
||||
|
||||
@@ -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