fix(v4): the final review's six minors in the ROM boot

- Text after start is refused, as text.c refuses it: before it jumps, the
  ROM reads on to the end of the source; blanks, comments and the ends of
  blobs may follow, a token is ERR-AFTER (26), "text after start".
- A token too long where a name, a target or a value is wanted is "name
  too long", as text.c says: (MISSING) reports "missing" only when no error
  came first.  `", keeps "missing", which is text.c's word for both.
- text.c's "address out of range" names the address, not the value.
- The system's source files are one list, v4/sys_files.h: boot.c, the
  harness and the nucleus test are made from it.
- The ROM's error codes and words are one list, v4/rom_errors.h: boot.c's
  messages, host_map.h's ROM_ERR_* and mkmap's ERR-* are made from it.
- node.h says what v4_node_store does with a ROM address (nothing, as for
  any address outside RAM, D-14); test_host_rom_range pins it.

ROM: 2095 words, rom_hash 0x06d0dcb7a0cb41a1.  dict_hash unchanged,
0xe5a57f3eb72ff94a.  make -C v4 test test-32 hosted-check pass.  QEMU:
amd64, aarch64, riscv64 to ok>, POST 538/538, POST: PASSED.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rajames
2026-10-15 17:10:45 -04:00
co-authored by Claude Opus 5.5
parent ff75e69e91
commit a28b5f2230
18 changed files with 646 additions and 89 deletions
+4 -26
View File
@@ -12,6 +12,7 @@
#ifndef V4_TESTS_HOST_MAP_H
#define V4_TESTS_HOST_MAP_H
#include "v4/rom_errors.h"
#include "v4/blocks.h"
#include "v4/text.h"
#include <stdio.h>
@@ -197,32 +198,9 @@
#define ROM_KIND_CONST 2
#define ROM_KIND_MACRO 3
#define ROM_KIND_SET 4
/* the ROM's error codes */
#define ROM_ERR_TOO_LONG 1 /* a token longer than 63 characters: "name too long" */
#define ROM_ERR_COMMENT 2 /* a ( with no ) before the end: "comment not closed" */
#define ROM_ERR_BAD_NAME 3 /* an opcode, a reserved word, a label, a number or &...: "cannot be used as a name" */
#define ROM_ERR_DEFINED 4 /* "already defined" */
#define ROM_ERR_NAMES 5 /* the symbol table is full: "too many names" */
#define ROM_ERR_FIT 6 /* memory is full, or a branch cannot reach: "does not fit" */
#define ROM_ERR_OPCODE 7 /* not an opcode the packer takes there */
#define ROM_ERR_IN_MACRO 8 /* "not allowed in a macro" */
#define ROM_ERR_MACRO 9 /* "macro without endmacro", "endmacro without macro" */
#define ROM_ERR_LABEL_DUP 10 /* "label already defined in this word" */
#define ROM_ERR_LABELS 11 /* "too many labels in one word" */
#define ROM_ERR_FOR 12 /* FOR nested too deep, NEXT or UNEXT without FOR, FOR without NEXT */
#define ROM_ERR_UNEXT 13 /* "FOR ... UNEXT must fit one instruction word and hold no literal" */
#define ROM_ERR_TARGET 14 /* "not something to branch to" */
#define ROM_ERR_UNDEFINED 15 /* "never defined" */
#define ROM_ERR_BIG 16 /* "number does not fit a cell" */
#define ROM_ERR_AMP 17 /* "& needs a word that is already defined" */
#define ROM_ERR_FETCH_P 18 /* "write the number, not @p" */
#define ROM_ERR_MISSING 19 /* a name, a target, a value or a string missing at the end */
#define ROM_ERR_DEEP 20 /* "macros nested too deep" */
#define ROM_ERR_REFS 21 /* "too many unresolved branches" */
#define ROM_ERR_VALUE 22 /* "not a value", "not a number" */
#define ROM_ERR_NO_START 23 /* the input ended with no start */
#define ROM_ERR_START 24 /* start: not a word */
#define ROM_ERR_RANGE 25 /* "address out of range": store outside RAM */
/* the ROM's error codes: ROM_ERR_TOO_LONG, ROM_ERR_COMMENT ... (v4/rom_errors.h) */
#define ROM_ERR_ENUM_(name, code, text) ROM_ERR_##name = code,
enum { V4_ROM_ERRORS(ROM_ERR_ENUM_) ROM_ERR_END_ };
#define ROM_ADDR_TOP ((v4_cell)(V4_NODE_WORDS + V4_ROM_WORDS - 1u)) /* the top of the address space */
#define WORD_DEFINED (BUF0_W - 11) /* the xt of the word that is told of each new entry, or 0 */
#define WORD_FORGOTTEN (BUF0_W - 12) /* the xt of the word that is told when entries go, or 0 */
+8 -5
View File
@@ -12,6 +12,7 @@
#include "v4/testcode.h"
#include "host_map.h"
#include "v4/sys_files.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -105,13 +106,15 @@ static void rom_boot(v4_node *n)
}
/* THE SYSTEM, AS THE MACHINE BUILDS IT (MACHINE.md section 6, step 1). The
* system's source, capsules/v4/sys (V4_SYS_DIR): the map, the fourteen and
* system's source, capsules/v4/sys (V4_SYS_DIR): the map, v4/sys_files.h and
* the end, each followed by a new line and V4_ROM_BLOB_END, then EOT -- what v4/system/boot.c
* feeds the console. rom_system_text gives it whole, *len its length;
* rom_build_system has `n`, booted with its ROM, assemble it, and runs it to
* its start: 1 if it started, with *words the instruction words it took. */
static const char *const rom_system_files[] = { "core", "input", "dict", "codegen", "compile", "quit", "forth",
"numout", "words", "system", "qmath", "blocks", "log", "acl" };
#define ROM_SYS_FILE(file) #file,
static const char *const rom_system_files[] = { V4_SYS_FILES(ROM_SYS_FILE) };
#undef ROM_SYS_FILE
#define ROM_SYS_FILES ((unsigned)(sizeof rom_system_files / sizeof rom_system_files[0]))
static char *rom_system_text(size_t *len)
{
@@ -119,12 +122,12 @@ static char *rom_system_text(size_t *len)
char *text = malloc(cap);
unsigned i;
if (!text) return NULL;
for (i = 0; i < 16; i++) {
for (i = 0; i < ROM_SYS_FILES + 2u; i++) {
char path[512];
FILE *f;
long n;
if (i == 0) snprintf(path, sizeof path, "%s/map-%d.f18", V4_SYS_DIR, V4_CELL_BITS);
else if (i <= 14) snprintf(path, sizeof path, "%s/%s.f18", V4_SYS_DIR, rom_system_files[i - 1]);
else if (i <= ROM_SYS_FILES) snprintf(path, sizeof path, "%s/%s.f18", V4_SYS_DIR, rom_system_files[i - 1]);
else snprintf(path, sizeof path, "%s/end.f18", V4_SYS_DIR);
f = fopen(path, "rb");
if (!f) { printf(" cannot read %s\n", path); free(text); return NULL; }
+39 -1
View File
@@ -11,6 +11,7 @@
#include "rom_harness.h"
#include "v4/text.h"
#include "rom_asm_cases.h"
#include "v4/rom_errors.h"
static int failures = 0, checks = 0;
#define CHECK(c,...) do{checks++; if(!(c)){failures++; printf("FAIL %s:%d: ",__FILE__,__LINE__); printf(__VA_ARGS__); printf("\n");}}while(0)
@@ -38,7 +39,7 @@ static v4_cell code_of(const char *m)
{ "write the number", ROM_ERR_FETCH_P }, { "too many unresolved", ROM_ERR_REFS },
{ "not a value", ROM_ERR_VALUE }, { "not a number", ROM_ERR_VALUE },
{ "too many names", ROM_ERR_NAMES }, { "out of name space", ROM_ERR_NAMES },
{ "address out of range", ROM_ERR_RANGE }, { "without", ROM_ERR_MISSING },
{ "address out of range", ROM_ERR_RANGE }, { "text after start", ROM_ERR_AFTER }, { "without", ROM_ERR_MISSING },
};
unsigned i;
for (i = 0; i < sizeof map / sizeof map[0]; i++) if (strstr(m, map[i].s)) return map[i].c;
@@ -246,6 +247,43 @@ int main(void)
snprintf(t, sizeof t, "store %ld 5", (long)V4_NODE_WORDS - 1); same(t);
}
/* a token too long where a name or a value is wanted: "name too long", as
* text.c says, not "missing" -- the first error stands */
{
static const char *const forms[] = { "constant %s 5", "constant K %s", "set %s 5", "set S %s", ": %s",
"store %s 5", "store 300 %s", "#, %s", "jump %s", "if %s", "\", %s" };
static char t[160], long_tok[65];
memset(long_tok, 'X', 64); long_tok[64] = 0;
for (i = 0; i < sizeof forms / sizeof forms[0]; i++) { snprintf(t, sizeof t, forms[i], long_tok); same(t); }
}
/* text after start: refused, as text.c refuses it; blanks, comments and
* the blob's end after it are not text */
CHECK(rom_run(": Z ;\nstart Z\n1\n\x04") == 0 && b.mem[ROM_FAILED] == ROM_ERR_AFTER, "text after start: refused (code %ld)",
(long)b.mem[ROM_FAILED]);
CHECK(rom_run(": Z ;\nstart Z \\ a comment\n( and another )\n\x1c\x04") == 1, "comments and a blob's end after start: started (code %ld)",
(long)b.mem[ROM_FAILED]);
{
v4_node_reset(&a);
v4_text_begin(&tx, &a, ROM_ORIGIN);
CHECK(!v4_text_assemble(&tx, ": Z ;\nstart Z\n1\n") && code_of(v4_text_error(&tx)) == ROM_ERR_AFTER,
"text.c refuses text after start too: %s", v4_text_error(&tx));
}
/* the ROM's error messages, as the boot prints them: one list
* (v4/rom_errors.h), its codes 1, 2, 3 ... in order */
{
static const struct { int code; const char *text; } list[] = {
#define E(name, code, text) { code, text },
V4_ROM_ERRORS(E)
#undef E
};
for (i = 0; i < sizeof list / sizeof list[0]; i++) if (list[i].code != (int)i + 1) break;
CHECK(i == sizeof list / sizeof list[0], "the ROM's error codes run 1, 2, 3 ... : entry %u is %d", i, list[i].code);
CHECK(ROM_ERR_RANGE == 25 && strcmp(list[ROM_ERR_RANGE - 1].text, "address out of range") == 0, "ROM_ERR_RANGE's message");
CHECK(strcmp(list[ROM_ERR_AFTER - 1].text, "text after start") == 0, "ROM_ERR_AFTER's message");
}
printf("test_host_rom_asm: %d checks, %d failures\n", checks, failures);
return failures != 0;
}
+2 -2
View File
@@ -28,9 +28,9 @@ static int build_a(void)
unsigned i;
v4_node_reset(&a);
v4_text_begin(&tx, &a, ROM_ORIGIN);
for (i = 0; i < 16; i++) {
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 <= 14) snprintf(path, sizeof path, "%s/%s.f18", V4_SYS_DIR, rom_system_files[i - 1]);
else if (i <= ROM_SYS_FILES) snprintf(path, sizeof path, "%s/%s.f18", V4_SYS_DIR, rom_system_files[i - 1]);
else snprintf(path, sizeof path, "%s/end.f18", V4_SYS_DIR);
if (!v4_text_assemble_file(&tx, path)) return 0;
}
+2
View File
@@ -46,6 +46,8 @@ int main(void)
v4_node_rom_attach(&n, rom, 16);
CHECK(v4_node_load(&n, V4_ROM_BASE) == rom[0], "the ROM reads");
CHECK(v4_node_load(&n, V4_ROM_BASE + 16) == 0, "past the ROM's words: nothing");
v4_node_store(&n, V4_ROM_BASE, 12345);
CHECK(v4_node_load(&n, V4_ROM_BASE) == rom[0] && n.faults == 0, "C code's store into the ROM does nothing and counts no fault (node.h)");
/* RAM 16: call the ROM's word, then return */
v4_asm_begin(&as, &n, 16);
+1
View File
@@ -540,6 +540,7 @@ int main(void)
CHECK(ok(": T ; constant A 300 store A 5 store A+1 &T") && nb.mem[300] == 5 && nb.mem[301] == ORIGIN,
"store puts a VALUE anywhere in RAM");
CHECK(refused("store -1 5", "address out of range", 1), "store outside RAM is refused");
CHECK(refused("store 99999 5", "address out of range: 99999", 1), "store outside RAM names the address, not the value");
CHECK(refused("store 5", "store without a value", 1), "store needs a value");
CHECK(ok(": A ; : B ; start B") && v4_text_start(&tx) == ORIGIN + 1, "start names the word the system starts at");
CHECK(ok(": A ;") && v4_text_start(&tx) == -1, "no start: -1");