From 4c7e14994856e5d3218dd7d909d6a848be15753d Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Fri, 4 Dec 2026 14:16:29 -0800 Subject: Extract the shared WorkList and accept dead-self peephole results Move the deduplicating node worklist into util, including traversal marks. Skip replacement processing when a peephole has already killed its returned self. Add regressions for traversal/requeue behavior and a rewrite that subsumes itself. Validation: the dead-self regression fails at the old assertion; make -k -j8 tests passes with both regressions (821 executions). --- .../java/com/seaofnodes/simple/Chapter33Test.java | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java (limited to 'chapter33/src/test/java') diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java new file mode 100644 index 00000000..99dc4452 --- /dev/null +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java @@ -0,0 +1,44 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.codegen.CodeGen; +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.util.WorkList; +import org.junit.Test; +import static org.junit.Assert.*; + +/** Regressions for the independent compiler fixes introduced in this chapter. */ +public class Chapter33Test { + @Test public void testWorkListRequeueAndTraversal() { + CodeGen code = new CodeGen("return arg;").parse(); + Node arg = code.expr(); + WorkList work = new WorkList<>(); + work.push(null); + work.push(arg); + work.push(arg); + assertSame(arg,work.pop()); + assertNull(work.pop()); + work.push(arg); + assertSame(arg,work.popOnce()); + work.push(arg); + assertNull("A traversal must not revisit a cycle",work.popOnce()); + work.clear(); + work.push(arg); + assertSame(arg,work.popOnce()); + } + + @Test public void testPeepholeMayReturnDeadSelf() { + CodeGen code = new CodeGen("return arg;").parse(); + Node replacement = code.expr(); + // A rewrite may replace its own uses and thereby kill itself before + // returning self to report progress to the iterative peephole driver. + Node rewritten = new AddNode(replacement,code.con(1)) { + @Override public Node idealize() { + subsume(in(1)); + return this; + } + }.init(); + code.driver(CodeGen.Phase.TypeCheck); + assertTrue(rewritten.isDead()); + assertEquals("42",Eval2.eval(code,42)); + } +} -- cgit v1.2.3