/* SPDX-License-Identifier: LicenseRef-Starship-1.0 * Copyright (c) 2023-2026 Robert A. James */ /* test_dump.c -- the fault dump of a node that stopped (v4/dump.h; * docs/v4.0.0/plans/2026-11-17-emergency-diagnostic-build.md, ruled by Captain Bob 2026-11-17: * every build, on a stop only). * * A node is set by hand to a known state: registers, both stacks, a fault, and * a dictionary of two words laid out as dict.f18 lays an entry (the name's * word address at xt-2, the link at xt-1, the count byte first). The dump * must say all of it: the fault's kind, address and count; P, A, B; every * value on the data stack, T first; every return-stack entry, R first, each * named by the word it falls in (the backtrace), the ROM's as ROM, one below * every word as such; and the line that was running. * * Built and run at both V4_CELL_BITS=32 and 64; see v4/Makefile. */ #include "v4/dump.h" #include "v4/node.h" #include "v4/stack.h" #include "v4/image.h" #include #include static int failures = 0; static int checks = 0; #define CHECK(cond, ...) \ do { \ checks++; \ if (!(cond)) { \ failures++; \ printf(" FAIL %s:%d: ", __FILE__, __LINE__); \ printf(__VA_ARGS__); \ printf("\n"); \ } \ } while (0) static char out[8192]; static size_t used; static void capture(void *self, const char *s, unsigned n) { (void)self; if (used + n >= sizeof out) n = (unsigned)(sizeof out - 1 - used); memcpy(out + used, s, n); used += n; out[used] = 0; } static v4_node node; /* an entry as dict.f18 lays one: name at nfa (count byte first, four to a cell, low byte first) */ static void entry(v4_cell xt, v4_cell nfa, v4_cell link, const char *name) { unsigned len = (unsigned)strlen(name), i; for (i = 0; i <= len; i++) { unsigned b = i == 0 ? len : (unsigned char)name[i - 1]; node.mem[nfa + (v4_cell)(i / 4u)] |= (v4_cell)((v4_ucell)b << (8u * (i % 4u))); } node.mem[xt - 3] = 0; node.mem[xt - 2] = nfa; node.mem[xt - 1] = link; } static int has(const char *s) { return strstr(out, s) != 0; } int main(void) { v4_image im; memset(&im, 0, sizeof im); v4_node_reset(&node); im.latest = 10; entry(100, 90, 0, "ALPHA"); entry(200, 190, 100, "BETA"); node.mem[im.latest] = 200; node.p = 123; node.a = 7; node.b = 8; node.fault_kind = V4_FAULT_DATA_UNDER; node.fault_addr = 0; node.faults = 3; node.stopped = 1; v4_dstack_push(&node.ds, 11); v4_dstack_push(&node.ds, 22); v4_dstack_push(&node.ds, 33); v4_rstack_push(&node.rs, 50); /* below every word */ v4_rstack_push(&node.rs, (v4_cell)V4_ROM_BASE + 5); /* in the ROM */ v4_rstack_push(&node.rs, 205); /* in BETA */ v4_rstack_push(&node.rs, 150); /* in ALPHA: R */ used = 0; out[0] = 0; v4_dump_node(&node, &im, "3 4 SPIN", 8u, capture, 0); CHECK(has("data underflow"), "the fault's kind: %s", out); CHECK(has("faults=3"), "the fault count: %s", out); CHECK(has("stopped"), "it says the node stopped: %s", out); CHECK(has("P=123") && has("A=7") && has("B=8"), "P, A, B: %s", out); CHECK(has("data stack depth=3: 33 22 11"), "the data stack, T first: %s", out); CHECK(has("return stack depth=4"), "the return stack's depth: %s", out); CHECK(has("150 in ALPHA"), "R named by its word: %s", out); CHECK(has("205 in BETA"), "the next named: %s", out); CHECK(has("in ROM"), "a ROM address said so: %s", out); CHECK(has("50 below every word"), "an address below the dictionary said so: %s", out); CHECK(strstr(out, "150 in ALPHA") < strstr(out, "205 in BETA"), "R first, then down the stack: %s", out); CHECK(has("line: 3 4 SPIN"), "the running line: %s", out); /* an address fault says the address */ node.fault_kind = V4_FAULT_ADDRESS; node.fault_addr = 99999; used = 0; out[0] = 0; v4_dump_node(&node, &im, 0, 0u, capture, 0); CHECK(has("address fault at 99999"), "the faulting address: %s", out); CHECK(!has("line:"), "no line when none was running: %s", out); /* a dictionary whose link goes wild does not run away: the walk is bounded */ node.mem[100 - 1] = 200; /* ALPHA links back to BETA: a loop */ used = 0; out[0] = 0; v4_dump_node(&node, &im, 0, 0u, capture, 0); CHECK(has("return stack depth=4"), "the dump still ends: %s", out); printf("test_dump: %d checks, %d failures\n", checks, failures); return failures ? 1 : 0; }