From 5ea32e69433d0c850d02bbc1e6c4a260bc095fec Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Thu, 1 Oct 2026 12:01:57 -0700 Subject: Move debug printing into shared space Same as the graph viewer, moves a lot of code out of all Nodes --- graph/README.md | 23 +++++------ graph/graph.iml | 1 + .../java/com/seaofnodes/graph/GraphAdapter.java | 46 ++++++++++++++++------ .../java/com/seaofnodes/graph/GraphCapture.java | 3 +- 4 files changed, 50 insertions(+), 23 deletions(-) (limited to 'graph') diff --git a/graph/README.md b/graph/README.md index 812a7318..a459898f 100644 --- a/graph/README.md +++ b/graph/README.md @@ -156,8 +156,9 @@ All chapters share the Java viewer plumbing as well as the browser: `GraphObserver`. Chapter subclasses compile source, attach/detach the observer, and supply roots, phase and parser position. Each compilation gets fresh capture state and a new compilation key, even when the previous compilation failed. -- `GraphAdapter` supplies node facts and indexed graph/dependency access. - Dependency access defaults to an empty set in chapters without those lists. +- `BaseNode` in `print/` supplies identity and indexed graph/dependency access. + Dependency access defaults to an empty set. `GraphAdapter` adds viewer roles + and grouping facts for the chapter. Shared Java has no dependency on chapter classes. The WebSocket listener and built-in asset server bind to 127.0.0.1. @@ -344,13 +345,12 @@ interactive viewer also loads the web assets from this checkout. The shared Java code is in `src/main/java/com/seaofnodes/graph/`: -- `GraphAdapter` is an abstract base with hooks for `id`, `desc`, and indexed - edge access (`nIns`/`in`, `nOuts`/`out`). Its final `snap` method walks definitions and - uses iteratively, handles cycles, and sorts the copied records by ID. -- `GraphSnapshot` is detached data: compilation key, step, roots, active scope, nodes and groups. - Each node has an ID, plain-text label/type, kind, edges to its defs, and optional - projection metadata. It contains no chapter classes or layout coordinates. - Node and edge lists are concrete `ArrayList`s, treated as read-only after capture. +- `GraphAdapter>` reads identity, edges, dependencies, + labels, and types directly from the shared node contract. It constructs node + and edge records centrally. Chapter hooks provide kinds, edge roles, scope + binding names, projection indices, call shortcuts, and folding state as needed. + Its final `snap` method walks definitions and uses iteratively, handles cycles, + and sorts the copied records by ID. Root IDs use `int[]`. Capture uses a node table indexed by ID for visitation and root deduplication; no boxed integer lists, sets or map keys are needed. @@ -358,8 +358,9 @@ Indexed edge access reads each chapter's native storage without copying it into a different collection type. Every chapter implements `com.seaofnodes.simple.print.SimpleGraphAdapter`, -extending `GraphAdapter`. The chapter owns classification and extraction; the base owns traversal -and snapshot assembly. No new Java interface is involved. +extending `GraphAdapter`. The chapter owns semantic classification; the +base owns edge extraction, traversal, and snapshot assembly. The common +`BaseNode` class lives in `print/`, which is also an IDEA dependency of `graph/`. For chapter 4: diff --git a/graph/graph.iml b/graph/graph.iml index 8e9a0e43..99b14f9d 100644 --- a/graph/graph.iml +++ b/graph/graph.iml @@ -11,5 +11,6 @@ + diff --git a/graph/src/main/java/com/seaofnodes/graph/GraphAdapter.java b/graph/src/main/java/com/seaofnodes/graph/GraphAdapter.java index eaa308f9..9014ce04 100644 --- a/graph/src/main/java/com/seaofnodes/graph/GraphAdapter.java +++ b/graph/src/main/java/com/seaofnodes/graph/GraphAdapter.java @@ -1,5 +1,7 @@ package com.seaofnodes.graph; +import com.seaofnodes.print.BaseNode; +import static com.seaofnodes.graph.GraphSnapshot.*; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -10,18 +12,40 @@ import java.util.Comparator; * Capture on the compiler thread, while the graph is not being modified. * Hooks must only read the IR: no computing types, peepholes or scheduling. */ -public abstract class GraphAdapter { - protected abstract int id(N node); - protected abstract int nIns(N node); - protected abstract N in(N node, int idx); - protected abstract int nOuts(N node); - protected abstract N out(N node, int idx); - protected abstract GraphSnapshot.Node desc(N node); - protected abstract boolean dead(N node); +public abstract class GraphAdapter> { + protected final int id(N n) { return n._nid; } + protected final int nIns(N n) { return n.nIns(); } + protected final N in(N n, int i) { return n.in(i); } + protected final int nOuts(N n) { return n.nOuts(); } + protected final N out(N n, int i) { return n.out(i); } + protected final boolean dead(N n) { return n.isDead(); } + protected final int nDeps(N n) { return n.nDeps(); } + protected final N dep(N n, int i) { return n.dep(i); } + + // Chapters supply only semantic distinctions that their IR has introduced. + protected Kind kind(N n) { return n.isCFG() ? Kind.CTRL : Kind.DATA; } + protected Role role(N n, int i) { return i==0 ? Role.CTRL : Role.DATA; } + protected String[] edgeNames(N n) { return null; } + protected String edgeName(N n, int i) { return null; } + protected int edgeJump(N n, int i) { return 0; } + protected int projectionIndex(N n) { return -1; } + protected boolean folding(N n) { return false; } // Omit cached helpers with no graph uses; still walk through them. - protected boolean show(N node) { return true; } - protected int nDeps(N node) { return 0; } - protected N dep(N node, int idx) { throw new IndexOutOfBoundsException(idx); } + protected boolean show(N n) { return true; } + + protected final GraphSnapshot.Node desc(N n) { + var edges=new ArrayList(); + String[] names=edgeNames(n); + for( int i=0; i extends GraphObserver { +public abstract class GraphCapture> extends GraphObserver { private final GraphAdapter _graph; private String _comp; -- cgit v1.2.3