summaryrefslogtreecommitdiff
path: root/chapter33/src/test/java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-12-04 14:16:29 -0800
committerCliff Click <cliffc@acm.org>2026-12-04 14:16:29 -0800
commit4c7e14994856e5d3218dd7d909d6a848be15753d (patch)
tree5d5b7944543177a83e08ed17bc1bc61cf111b9f5 /chapter33/src/test/java
parentfd21b1a62f0fb1a0efd503769b2e5f3a81747af0 (diff)
downloadSimple-4c7e14994856e5d3218dd7d909d6a848be15753d.tar.gz
Simple-4c7e14994856e5d3218dd7d909d6a848be15753d.zip
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).
Diffstat (limited to 'chapter33/src/test/java')
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java44
1 files changed, 44 insertions, 0 deletions
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<Node> 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));
+ }
+}