/* SPDX-License-Identifier: LicenseRef-Starship-1.0 * Copyright (c) 2023-2026 Robert A. James */ /* dump.c -- what a node was doing when it stopped. See v4/dump.h. */ #include "v4/dump.h" #include "v4/stack.h" static void say(v4_dump_out out, void *self, const char *s) { unsigned n = 0; while (s[n]) n++; out(self, s, n); } static void say_num(v4_dump_out out, void *self, v4_cell v) { char buf[24]; unsigned i = sizeof buf; v4_ucell u = v < 0 ? (v4_ucell)0 - (v4_ucell)v : (v4_ucell)v; do { buf[--i] = (char)('0' + (int)(u % 10u)); u /= 10u; } while (u && i > 1); if (v < 0) buf[--i] = '-'; out(self, buf + i, (unsigned)(sizeof buf - i)); } static unsigned byte_at(const v4_node *n, v4_cell nfa, unsigned i) { v4_cell w = nfa + (v4_cell)(i / 4u); if (w < 0 || w >= (v4_cell)V4_NODE_WORDS) return 0; return (unsigned)(((v4_ucell)n->mem[w] >> (8u * (i % 4u))) & 0xFFu); } /* the newest word whose xt is at or below addr, or 0 */ static v4_cell word_of(const v4_node *n, const v4_image *im, v4_cell addr) { v4_cell xt; unsigned k; if (!im || im->latest <= 0 || im->latest >= (v4_cell)V4_NODE_WORDS) return 0; for (xt = n->mem[im->latest], k = 0; xt > 4 && xt < (v4_cell)V4_NODE_WORDS && k < V4_NODE_WORDS; xt = n->mem[xt - 1], k++) if (xt <= addr) return xt; return 0; } static void say_where(v4_dump_out out, void *self, const v4_node *n, const v4_image *im, v4_cell addr) { v4_cell xt, nfa; unsigned len, i; char c; say_num(out, self, addr); if (addr >= V4_ROM_BASE) { say(out, self, " in ROM"); return; } xt = addr >= 0 && addr < (v4_cell)V4_NODE_WORDS ? word_of(n, im, addr) : 0; if (xt == 0) { say(out, self, " below every word"); return; } nfa = n->mem[xt - 2]; if (nfa < 0 || nfa >= xt) { say(out, self, " in a word with no name"); return; } len = byte_at(n, nfa, 0); if (len > 31u) len = 31u; say(out, self, " in "); for (i = 1; i <= len; i++) { c = (char)byte_at(n, nfa, i); out(self, &c, 1); } } static const char *kind_name(unsigned k) { switch (k) { case V4_FAULT_ADDRESS: return "address fault"; case V4_FAULT_DATA_OVER: return "data overflow"; case V4_FAULT_DATA_UNDER: return "data underflow"; case V4_FAULT_RET_OVER: return "return overflow"; case V4_FAULT_RET_UNDER: return "return underflow"; case V4_FAULT_RAISED: return "error raised"; default: return "unknown fault"; } } void v4_dump_node(const v4_node *n, const v4_image *im, const char *line, unsigned len, v4_dump_out out, void *self) { unsigned i, h; say(out, self, "V4 DUMP: "); if (n->faults == 0) say(out, self, "no fault"); else { say(out, self, kind_name(n->fault_kind)); if (n->fault_kind == V4_FAULT_ADDRESS) { say(out, self, " at "); say_num(out, self, n->fault_addr); } } say(out, self, " faults="); say_num(out, self, (v4_cell)n->faults); say(out, self, n->stopped ? " stopped\n" : " running\n"); say(out, self, "V4 DUMP: P="); say_num(out, self, n->p); say(out, self, " A="); say_num(out, self, n->a); say(out, self, " B="); say_num(out, self, n->b); say(out, self, "\n"); say(out, self, "V4 DUMP: data stack depth="); say_num(out, self, (v4_cell)n->ds.depth); say(out, self, ":"); for (i = 0, h = n->ds.head; i < n->ds.depth; i++) { v4_cell v; if (i == 0) v = n->ds.t; else if (i == 1) v = n->ds.s; else { h = (h + V4_DATA_RING - 1u) % V4_DATA_RING; v = n->ds.ring[h]; } say(out, self, " "); say_num(out, self, v); } say(out, self, "\n"); say(out, self, "V4 DUMP: return stack depth="); say_num(out, self, (v4_cell)n->rs.depth); say(out, self, ", R first:\n"); for (i = 0, h = n->rs.head; i < n->rs.depth; i++) { v4_cell v; if (i == 0) v = n->rs.r; else { h = (h + V4_RET_RING - 1u) % V4_RET_RING; v = n->rs.ring[h]; } say(out, self, "V4 DUMP: "); say_where(out, self, n, im, v); say(out, self, "\n"); } if (line && len) { say(out, self, "V4 DUMP: line: "); out(self, line, len); say(out, self, "\n"); } }