diff --git a/logs/20261119-mkf18a/text-after.txt b/logs/20261119-mkf18a/text-after.txt new file mode 100644 index 00000000..bbb6bdb9 --- /dev/null +++ b/logs/20261119-mkf18a/text-after.txt @@ -0,0 +1,8 @@ +make: Entering directory '/home/user/LithosAnanke/v4' + [V4_CELL_BITS=64 run-64-test_text.c] +v4 text assembler tests: V4_CELL_BITS=64 + 274 checks, 0 failures + [V4_CELL_BITS=32 run-32-test_text.c] +v4 text assembler tests: V4_CELL_BITS=32 + 274 checks, 0 failures +make: Leaving directory '/home/user/LithosAnanke/v4' diff --git a/logs/20261119-mkf18a/text-before-64.txt b/logs/20261119-mkf18a/text-before-64.txt new file mode 100644 index 00000000..a6e2d71f --- /dev/null +++ b/logs/20261119-mkf18a/text-before-64.txt @@ -0,0 +1,18 @@ +make: Entering directory '/home/user/LithosAnanke/v4' + [V4_CELL_BITS=64 run-64-test_text.c] +v4 text assembler tests: V4_CELL_BITS=64 +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:501: a flag after the : name +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:644: inline after the name: flag 8, and only the word's ; laid down +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:646: immediate, compile-only, pinned and mode 3 +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:648: ports, and the requests it makes +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:650: a word with none +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:655: the new words are reserved +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:658: a header's flags stay the header's +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:666: a call to an import: recorded as import 0 +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:672: a call, an &NAME and a jump, each to its import +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:675: the import's placeholder is in the branch field + message was: line 1: never defined: import +FAIL /home/user/LithosAnanke/v4/tests/test_text.c:676: an import is named once + 274 checks, 11 failures +make: *** [Makefile:358: run-64-test_text.c] Error 1 +make: Leaving directory '/home/user/LithosAnanke/v4' diff --git a/v4/include/v4/text.h b/v4/include/v4/text.h index e69967b9..634100d2 100644 --- a/v4/include/v4/text.h +++ b/v4/include/v4/text.h @@ -112,7 +112,11 @@ typedef struct { unsigned name; /* offset into pool */ unsigned char kind; unsigned char addr; /* a set variable: its value is an address of the assembly */ + unsigned char ports; /* a word: it says it touches ports */ v4_cell value; /* address, constant, or macro text offset */ + v4_cell attr; /* a word: its flags, as v4_text_word_info's */ + unsigned req, nreq; /* a word: its requests, in reqs[]; an import: req is its index */ + unsigned train; /* an import: its train's name, offset into pool */ } v4_text_symbol; /* THE ADDRESS RECORD (docs/v4.0.0/plans/2026-11-19-f18-artifact-build.md §2). A cell laid down @@ -125,8 +129,30 @@ typedef struct { typedef struct { v4_cell cell; unsigned slot; + int import; /* -1: an address of this assembly; k: import k's (an import line) */ } v4_text_addr; +/* A word's attributes, written after its : name (plans/2026-11-19-f18-artifact-build.md §10.2, §10.5): + * : NAME [immediate] [data] [inline] [compile-only] [pinned] [mode N] [ports] [request N]... + * They are recorded for the artifact and lay nothing down. `flags` is as a dictionary entry's flags cell + * holds them (dict.f18): 1 immediate, 4 data, 8 in line, 16 compile-only, 64 pinned, the mode in bits + * 8-15. `ports`: it says it touches ports; `req`: the kernel requests it says it makes. */ +#define V4_TEXT_MAX_REQS 256u +typedef struct { + const char *name; + v4_cell addr; + v4_cell flags; + int ports; + const v4_cell *req; + unsigned nreq; +} v4_text_word_info; + +/* IMPORTS (§10.3). `import TRAIN NAME` declares NAME, a word of the train TRAIN, before it is used. A + * call, jump or &NAME of it is recorded as that import (v4_text_addr.import); its branch field or cell + * holds V4_TEXT_IMPORT_ADDR(k), an address above the code that placement replaces. */ +#define V4_TEXT_MAX_IMPORTS 256u +#define V4_TEXT_IMPORT_ADDR(k) ((v4_cell)(V4_NODE_WORDS - 1u - (unsigned)(k))) + typedef struct { unsigned name; v4_asm_ref ref; @@ -160,6 +186,12 @@ typedef struct { v4_text_addr *rec; /* the address record, or NULL when it is off */ unsigned rec_cap, nrec; + v4_cell reqs[V4_TEXT_MAX_REQS]; /* the words' requests */ + unsigned nreqs; + unsigned import_sym[V4_TEXT_MAX_IMPORTS]; /* each import's symbol */ + unsigned nimport; + int attr_open; /* the word whose attributes may follow, or -1 */ + unsigned line; int failed; char message[224]; @@ -175,6 +207,18 @@ 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); +/* The attributes of the word NAME: 1 and *out, or 0 if NAME is not a word. The strings and the request + * list stay the assembly's. */ +int v4_text_word_attrs(const v4_text *t, const char *name, v4_text_word_info *out); + +/* The words, in the order defined: how many, and the i-th's attributes. */ +unsigned v4_text_word_count(const v4_text *t); +int v4_text_word_at(const v4_text *t, unsigned i, v4_text_word_info *out); + +/* The imports, in the order declared: how many, and the k-th's train and name. */ +unsigned v4_text_import_count(const v4_text *t); +int v4_text_import_at(const v4_text *t, unsigned k, const char **train, const char **name); + /* 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); diff --git a/v4/src/text.c b/v4/src/text.c index 433e4091..d1d6bc3d 100644 --- a/v4/src/text.c +++ b/v4/src/text.c @@ -9,6 +9,7 @@ #include enum { KIND_WORD = 1, KIND_CONST, KIND_MACRO, KIND_SET }; /* the ROM's kinds: 1 word, 2 constant, 3 macro, 4 set variable */ +#define KIND_IMPORT 5 /* text.c's only: a word of another train (an import line) */ static const char *const mnemonic[V4_OPCODE_COUNT] = { ";", "ex", "jump", "call", "unext", "next", "if", "-if", @@ -71,7 +72,9 @@ static int is_reserved(const char *s) || strcmp(s, "header") == 0 || strcmp(s, "constant") == 0 || strcmp(s, "#,") == 0 || strcmp(s, "\",") == 0 || strcmp(s, "set") == 0 || strcmp(s, "here") == 0 || strcmp(s, "#id,") == 0 || strcmp(s, "#ids") == 0 - || strcmp(s, "store") == 0 || strcmp(s, "start") == 0 || flag_of(s) != 0; + || strcmp(s, "store") == 0 || strcmp(s, "start") == 0 || flag_of(s) != 0 + || strcmp(s, "pinned") == 0 || strcmp(s, "mode") == 0 || strcmp(s, "ports") == 0 + || strcmp(s, "request") == 0 || strcmp(s, "import") == 0; } static int is_label_def(const char *s) @@ -171,22 +174,33 @@ 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) +static void note_import(v4_text *t, v4_cell cell, unsigned slot, int import) { 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->rec[t->nrec].import = import; t->nrec++; } +static void note(v4_text *t, v4_cell cell, unsigned slot) +{ + note_import(t, cell, slot, -1); +} + /* `@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) +static void lit_addr_of(v4_text *t, v4_cell value, int import) { 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); + if (!t->as.error) note_import(t, cell, V4_TEXT_WHOLE_CELL, import); +} + +static void lit_addr(v4_text *t, v4_cell value) +{ + lit_addr_of(t, value, -1); } static void put_cell(v4_text *t, v4_cell value) @@ -262,14 +276,19 @@ static void check_asm(v4_text *t, const char *tok) if (t->as.error) fail(t, "does not fit: memory is full, or a branch cannot reach", tok); } -static void branch_to(v4_text *t, unsigned op, v4_cell target, const char *tok) +static void branch_to_of(v4_text *t, unsigned op, v4_cell target, const char *tok, int import) { v4_asm_ref ref; place_branch(t); 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); + if (!t->failed) note_import(t, ref.addr, ref.slot, import); +} + +static void branch_to(v4_text *t, unsigned op, v4_cell target, const char *tok) +{ + branch_to_of(t, op, target, tok, -1); } static void branch_later(v4_text *t, unsigned op, const char *name, int local) @@ -298,6 +317,7 @@ static void branch(v4_text *t, unsigned op, const char *name, int may_be_label) if (strcmp(t->pool + t->label[i].name, name) == 0) { branch_to(t, op, t->label[i].addr, name); return; } s = find(t, name); if (s && s->kind == KIND_WORD && !may_be_label) { branch_to(t, op, s->value, name); return; } + if (s && s->kind == KIND_IMPORT) { branch_to_of(t, op, s->value, name, (int)s->req); return; } if ((s && s->kind != KIND_WORD) || opcode_of(name) >= 0 || is_reserved(name) || is_label_def(name)) { fail(t, "not something to branch to", name); return; @@ -332,7 +352,10 @@ static void end_word(v4_text *t) t->ref[i].local = 0; s = find(t, t->pool + t->ref[i].name); if (s && s->kind == KIND_WORD) resolve(t, i, s->value); /* moves another into place i */ - else i++; + else if (s && s->kind == KIND_IMPORT) { + t->line = t->ref[i].line; + fail(t, "an import is declared before it is used", t->pool + t->ref[i].name); + } else i++; } t->nlabel = 0; } @@ -350,6 +373,10 @@ static void define(v4_text *t, const char *name, unsigned char kind, v4_cell val t->sym[t->nsym].kind = kind; t->sym[t->nsym].value = value; t->sym[t->nsym].addr = 0; + t->sym[t->nsym].ports = 0; + t->sym[t->nsym].attr = 0; + t->sym[t->nsym].req = t->sym[t->nsym].nreq = 0; + t->sym[t->nsym].train = 0; if (!t->failed) t->nsym++; } @@ -524,6 +551,48 @@ static void start_directive(v4_text *t, reader *r) t->start = s->value; } +/* After `: NAME`: one of its attributes (§10.2, §10.5). 1 if `tok` was one. */ +static int word_attr(v4_text *t, reader *r, const char *tok) +{ + v4_text_symbol *w = &t->sym[t->attr_open]; + char num[V4_TEXT_MAX_TOKEN + 1u]; + v4_cell f = flag_of(tok), v; + int big; + + if (f) { w->attr |= f; return 1; } + if (strcmp(tok, "pinned") == 0) { w->attr |= 64; return 1; } + if (strcmp(tok, "ports") == 0) { w->ports = 1; return 1; } + if (strcmp(tok, "mode") == 0 || strcmp(tok, "request") == 0) { + if (!next_token(t, r, num) || !number_of(num, &v, &big) || big) { fail(t, "mode and request need a number", tok); return 1; } + if (tok[0] == 'm') { + if (v < 0 || v > 255) { fail(t, "a mode is 0 to 255", num); return 1; } + w->attr = (v4_cell)(((v4_ucell)w->attr & ~(v4_ucell)0xFF00u) | ((v4_ucell)v << 8)); + return 1; + } + if (t->nreqs == V4_TEXT_MAX_REQS) { fail(t, "too many requests", num); return 1; } + if (w->nreq == 0) w->req = t->nreqs; + else if (w->req + w->nreq != t->nreqs) { fail(t, "a word's requests are written together", num); return 1; } + t->reqs[t->nreqs++] = v; + w->nreq++; + return 1; + } + return 0; +} + +/* `import TRAIN NAME`: NAME is a word of the train TRAIN (§10.3). */ +static void import_directive(v4_text *t, reader *r) +{ + char train[V4_TEXT_MAX_TOKEN + 1u], name[V4_TEXT_MAX_TOKEN + 1u]; + if (r->depth != 1) { fail(t, "not allowed in a macro", "import"); return; } + if (!next_token(t, r, train) || !next_token(t, r, name)) { if (!t->failed) fail(t, "import needs a train and a name", NULL); return; } + if (t->nimport == V4_TEXT_MAX_IMPORTS) { fail(t, "too many imports", name); return; } + define(t, name, KIND_IMPORT, V4_TEXT_IMPORT_ADDR(t->nimport)); + if (t->failed) return; + t->sym[t->nsym - 1u].req = t->nimport; + t->sym[t->nsym - 1u].train = intern(t, train); + t->import_sym[t->nimport++] = t->nsym - 1u; +} + static void one_token(v4_text *t, reader *r, const char *tok) { const v4_text_symbol *s; @@ -532,6 +601,13 @@ static void one_token(v4_text *t, reader *r, const char *tok) int op, big; if (t->start >= 0) { fail(t, "text after start", tok); return; } + if (t->attr_open >= 0 && word_attr(t, r, tok)) return; + t->attr_open = -1; + if (strcmp(tok, "pinned") == 0 || strcmp(tok, "mode") == 0 || strcmp(tok, "ports") == 0 || strcmp(tok, "request") == 0) { + fail(t, "a word's attribute must follow its : name", tok); + return; + } + if (strcmp(tok, "import") == 0) { import_directive(t, r); return; } if (strcmp(tok, "header") == 0) { header(t, r); return; } if (t->header_pending) { v4_cell f = flag_of(tok); @@ -544,6 +620,7 @@ static void one_token(v4_text *t, reader *r, const char *tok) if (!next_token(t, r, name)) { fail(t, ": without a name", NULL); return; } end_word(t); define(t, name, KIND_WORD, v4_asm_label(as)); + if (!t->failed && !t->header_pending) t->attr_open = (int)t->nsym - 1; t->header_pending = 0; return; } @@ -621,6 +698,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_IMPORT) { lit_addr_of(t, s->value, (int)s->req); check_asm(t, tok); return; } if (!s || s->kind != KIND_WORD) { fail(t, "& needs a word that is already defined", tok + 1); return; } lit_addr(t, s->value); check_asm(t, tok); @@ -682,6 +760,9 @@ void v4_text_begin(v4_text *t, v4_node *n, v4_cell origin) t->rec = NULL; t->rec_cap = 0; t->nrec = 0; + t->nreqs = 0; + t->nimport = 0; + t->attr_open = -1; } void v4_text_record(v4_text *t, v4_text_addr *buf, unsigned cap) @@ -696,6 +777,54 @@ unsigned v4_text_addresses(const v4_text *t) return t->nrec; } +static void info_of(const v4_text *t, const v4_text_symbol *s, v4_text_word_info *out) +{ + out->name = t->pool + s->name; + out->addr = s->value; + out->flags = s->attr; + out->ports = s->ports; + out->req = t->reqs + s->req; + out->nreq = s->nreq; +} + +int v4_text_word_attrs(const v4_text *t, const char *name, v4_text_word_info *out) +{ + const v4_text_symbol *s = find(t, name); + if (!s || s->kind != KIND_WORD) return 0; + info_of(t, s, out); + return 1; +} + +unsigned v4_text_word_count(const v4_text *t) +{ + unsigned i, c = 0; + for (i = 0; i < t->nsym; i++) if (t->sym[i].kind == KIND_WORD) c++; + return c; +} + +int v4_text_word_at(const v4_text *t, unsigned i, v4_text_word_info *out) +{ + unsigned j; + for (j = 0; j < t->nsym; j++) + if (t->sym[j].kind == KIND_WORD && i-- == 0) { info_of(t, &t->sym[j], out); return 1; } + return 0; +} + +unsigned v4_text_import_count(const v4_text *t) +{ + return t->nimport; +} + +int v4_text_import_at(const v4_text *t, unsigned k, const char **train, const char **name) +{ + const v4_text_symbol *s; + if (k >= t->nimport) return 0; + s = &t->sym[t->import_sym[k]]; + *train = t->pool + s->train; + *name = t->pool + s->name; + return 1; +} + void v4_text_constant(v4_text *t, const char *name, v4_cell value) { if (t->failed) return; @@ -761,6 +890,8 @@ int v4_text_finish(v4_text *t) resolve(t, 0, s->value); } if (!t->failed && !v4_asm_ok(&t->as)) fail(t, "does not fit: memory is full", NULL); + if (!t->failed && t->nimport && t->as.here > V4_TEXT_IMPORT_ADDR(t->nimport - 1u)) + fail(t, "the code reaches the imports' placeholders", NULL); return !t->failed; } diff --git a/v4/tests/test_text.c b/v4/tests/test_text.c index 5249fea8..54cc4486 100644 --- a/v4/tests/test_text.c +++ b/v4/tests/test_text.c @@ -496,7 +496,10 @@ int main(void) CHECK(refused("header X header Y : W ;", "header must be followed by :", 1), "so is another header"); CHECK(refused("header X", "header must be followed by :", 0), "and the end of the text"); CHECK(refused("header", "header without a name", 0), "header with no name is refused"); - CHECK(refused(": T immediate ;", "a flag with no header", 1), "a flag with no header is refused"); + /* Ruled 2026-11-19 (plans/2026-11-19-f18-artifact-build.md §10.2, "After the name, like header"): the + * flags may follow the : name. Until then this was refused, "a flag with no header". */ + CHECK(ok(": T immediate ;"), "a flag after the : name"); + CHECK(refused(": T 1 immediate ;", "a flag", 1), "a flag after the word's code is refused"); CHECK(refused("macro M header X endmacro", "", 0) || 1, "(header inside a macro body is only text until the macro is used)"); CHECK(refused("macro M header X : W ; endmacro", "not allowed in a macro", 1), "a word in a macro is still refused"); CHECK(refused(": immediate 1 ;", "cannot be used as a name", 1), "a flag's name cannot name a word"); @@ -635,6 +638,47 @@ int main(void) "the record changes no cell"); } + /* FLAGS AND CAPABILITIES AFTER THE : NAME (§10.2, §10.5): recorded for the artifact, nothing laid down. */ + { + v4_text_word_info wi; + CHECK(ok(": W inline ;") && v4_text_word_attrs(&tx, "W", &wi) && wi.flags == 8 && wi.addr == ORIGIN + && v4_text_here(&tx) == ORIGIN + 1, "inline after the name: flag 8, and only the word's ; laid down"); + CHECK(ok(": W immediate compile-only pinned mode 3 ;") && v4_text_word_attrs(&tx, "W", &wi) + && wi.flags == (1 | 16 | 64 | (3 << 8)), "immediate, compile-only, pinned and mode 3"); + CHECK(ok(": W ports request 7 request 9 ;") && v4_text_word_attrs(&tx, "W", &wi) && wi.ports == 1 + && wi.nreq == 2 && wi.req[0] == 7 && wi.req[1] == 9 && wi.flags == 0, "ports, and the requests it makes"); + CHECK(ok(": W ;") && v4_text_word_attrs(&tx, "W", &wi) && wi.flags == 0 && wi.ports == 0 && wi.nreq == 0, + "a word with none"); + CHECK(refused(": W mode 256 ;", "mode", 1), "a mode is 0 to 255"); + CHECK(refused(": W mode ;", "mode", 1), "mode needs a number"); + CHECK(refused(": W request ;", "request", 1), "request needs a number"); + CHECK(refused(": ports ;", "cannot be used as a name", 1) && refused(": import ;", "cannot be used as a name", 1) + && refused(": pinned ;", "cannot be used as a name", 1) && refused(": mode ;", "cannot be used as a name", 1) + && refused(": request ;", "cannot be used as a name", 1), "the new words are reserved"); + CHECK(ok("header N inline : N' ;") && v4_text_word_attrs(&tx, "N'", &wi) && wi.flags == 0, + "a header's flags stay the header's"); + } + + /* IMPORTS (§10.3): `import TRAIN NAME`. A call, a jump or an &NAME of it is recorded as that import; + * its field holds a placeholder outside the code, which placement replaces. */ + { + const char *train, *name; + CHECK(rec_ok("import LIB FOO : W FOO ;", 8) && v4_text_import_count(&tx) == 1 + && v4_text_import_at(&tx, 0, &train, &name) && strcmp(train, "LIB") == 0 && strcmp(name, "FOO") == 0 + && v4_text_addresses(&tx) == 1 && rec[0].cell == ORIGIN && rec[0].slot == 0 && rec[0].import == 0, + "a call to an import: recorded as import 0"); + CHECK(rec_ok(": A ; : B A ;", 8) && v4_text_addresses(&tx) == 1 && rec[0].import == -1, + "a call within the code is not an import"); + CHECK(rec_ok("import LIB FOO import LIB2 BAR : W BAR &FOO drop jump FOO", 8) && v4_text_import_count(&tx) == 2 + && v4_text_addresses(&tx) == 3 && rec[0].import == 1 && rec[2].import == 0 + && rec[1].slot == V4_TEXT_WHOLE_CELL && rec[1].import == 0, "a call, an &NAME and a jump, each to its import"); + CHECK(ok("import LIB FOO : W FOO ;") && nb.mem[ORIGIN] != 0, "the import's placeholder is in the branch field"); + CHECK(refused("import LIB FOO import LIB FOO", "already defined", 1), "an import is named once"); + CHECK(refused(": W FOO ; import LIB FOO", "never defined", 0) || refused(": W FOO ; import LIB FOO", "already", 0), + "an import is declared before it is used"); + CHECK(refused("import LIB", "import", 1), "import needs a train and a name"); + } + CHECK(v4_node_guards_intact(&na) && v4_node_guards_intact(&nb), "guards intact"); printf(" %d checks, %d failures\n", checks, failures);