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:
+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)
|
||||
|
||||
Reference in New Issue
Block a user