summaryrefslogtreecommitdiff
path: root/chapter25/src/test/java/com/seaofnodes
diff options
context:
space:
mode:
Diffstat (limited to 'chapter25/src/test/java/com/seaofnodes')
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter11Test.java14
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter18Test.java28
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter19Test.java22
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter20Test.java5
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter23Test.java25
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter24Test.java20
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter25Test.java4
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/codegen/PrintRegTestSupport.java76
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java39
9 files changed, 232 insertions, 1 deletions
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter11Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter11Test.java
index ab730b45..60a31c40 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter11Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter11Test.java
@@ -1,10 +1,22 @@
package com.seaofnodes.simple;
-
import com.seaofnodes.simple.codegen.CodeGen;
+
+
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class Chapter11Test {
+ @Test public void testPrintingPreservesGraph() throws Exception {
+ var code = new CodeGen("while(arg < 10) arg = arg + 1; return arg;").parse().opto().typeCheck().GCM().localSched();
+ var stop = code._stop;
+ new com.seaofnodes.simple.node.ConstantNode(com.seaofnodes.simple.type.TypeInteger.constant(123456));
+ com.seaofnodes.simple.node.PrintTestSupport.unchanged(stop, () -> {
+ String first = stop.p(99);
+ assertEquals(first,stop.p(99));
+ stop.toString();
+ });
+ }
+
@Test
public void testPrimes() {
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter18Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter18Test.java
index dc29dafa..d7fd12ff 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter18Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter18Test.java
@@ -1,4 +1,7 @@
package com.seaofnodes.simple;
+import com.seaofnodes.simple.type.*;
+import com.seaofnodes.simple.node.*;
+
import com.seaofnodes.simple.codegen.CodeGen.Phase;
import com.seaofnodes.simple.codegen.CodeGen;
@@ -8,6 +11,31 @@ import static org.junit.Assert.*;
import static org.junit.Assert.fail;
public class Chapter18Test {
+ @Test public void testPrintingForwardReferenceScope() throws Exception {
+ new CodeGen("return 0;").parse();
+ var declared = TypeMemPtr.make(TypeStruct.make("PrintForward",true,true));
+ var scope = new ScopeNode();
+ scope.define("ptr",declared,false,new ConstantNode(TypeInteger.ZERO),null);
+ Parser.TYPES.put("PrintForward",TypeMemPtr.make(TypeStruct.make("PrintForward",false,false)));
+ var field = Var.class.getDeclaredField("_type");
+ field.setAccessible(true);
+ var v = scope.var(0);
+ org.junit.Assert.assertSame(declared,field.get(v));
+ scope.toString();
+ org.junit.Assert.assertSame(declared,field.get(v));
+ }
+
+ @Test public void testPrintingLazyMemory() throws Exception {
+ var code = new CodeGen("return 0;").parse();
+ var base = new ConstantNode(com.seaofnodes.simple.type.TypeMem.BOT);
+ var outer = new ScopeNode();
+ var inner = new ScopeNode();
+ outer.addDef(null); outer.addDef(base);
+ inner.addDef(null); inner.addDef(outer);
+ var mem = new MemMergeNode(null,base,inner);
+ PrintTestSupport.unchanged(mem, () -> org.junit.Assert.assertTrue(mem.toString().contains("Lazy_")));
+ }
+
@Test
public void testJig() {
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter19Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter19Test.java
index 10c4690e..5ab7ba97 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter19Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter19Test.java
@@ -1,4 +1,7 @@
package com.seaofnodes.simple;
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.*;
+
import com.seaofnodes.simple.codegen.CodeGen.Phase;
import com.seaofnodes.simple.codegen.CodeGen;
@@ -7,6 +10,25 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class Chapter19Test {
+ @Test public void testPrintingFunctionLookup() {
+ var code = new CodeGen("return 0;").parse();
+ var tfp = TypeFunPtr.make1((byte)2,false,new Type[]{TypeInteger.constant(987654)},TypeInteger.constant(123456),30);
+ var con = ConstantNode.raw(tfp);
+ var call = new CallNode(null,code._start,con,con);
+ // Diagnostic lookup may construct/intern the return-erased linker key.
+ con.toString();
+ org.junit.Assert.assertNull(call.name());
+
+ var fun = new FunNode(null,tfp,"printerTarget",null);
+ fun._name = "printerTarget";
+ fun.addDef(code._start);
+ code.link(fun);
+ var otherReturn = tfp.makeFrom(TypeInteger.constant(654321));
+ org.junit.Assert.assertSame(fun,code._link(otherReturn));
+ org.junit.Assert.assertTrue(con.toString().contains("printerTarget"));
+ assertEquals("printerTarget",call.name());
+ }
+
@Test
public void testJig() {
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter20Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter20Test.java
index e8a3a875..43112594 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter20Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter20Test.java
@@ -1,10 +1,15 @@
package com.seaofnodes.simple;
+
import com.seaofnodes.simple.codegen.CodeGen;
import org.junit.Test;
import static org.junit.Assert.*;
public class Chapter20Test {
+ @Test public void testPrintingRegisters() throws Exception {
+ com.seaofnodes.simple.codegen.PrintRegTestSupport.check();
+ }
+
@Test
public void testJig() {
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter23Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter23Test.java
index 3346aa51..0e6398ee 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter23Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter23Test.java
@@ -1,4 +1,7 @@
package com.seaofnodes.simple;
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.*;
+
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.node.cpus.riscv.riscv;
@@ -10,6 +13,28 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class Chapter23Test {
+ @Test public void testDiagnosticTypeAccessors() throws Exception {
+ new CodeGen("return 0;").parse();
+ var tfp = TypeFunPtr.TEST;
+ var con = ConstantNode.raw(tfp);
+ var call = new CallNode(null,con,con,con);
+ var field = Type.class.getDeclaredField("VISIT");
+ field.setAccessible(true);
+ var visit = (java.util.Map<Object,Type>)field.get(null);
+ var marker = new Object();
+ visit.put(marker,Type.BOTTOM);
+ try {
+ assertEquals(0,TypeInteger.ZERO.alignment());
+ assertEquals(0,TypeInteger.ZERO.value());
+ org.junit.Assert.assertTrue(tfp._isConstant());
+ con.toString();
+ call.name();
+ con.p(1);
+ org.junit.Assert.assertSame(Type.BOTTOM,visit.get(marker));
+ assertEquals(1,visit.size());
+ } finally { visit.clear(); }
+ }
+
@Test @Ignore
public void testJig() throws IOException {
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter24Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter24Test.java
index eb89dbb3..1307d2ce 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter24Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter24Test.java
@@ -1,4 +1,7 @@
package com.seaofnodes.simple;
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.*;
+
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.node.cpus.riscv.riscv;
@@ -7,6 +10,23 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class Chapter24Test {
+ @Test public void testPrintingDeadFunction() throws Exception {
+ var code = new CodeGen("return 0;").parse();
+ var tfp = TypeFunPtr.TEST;
+ var fun = new FunNode(null,tfp,"deadPrinterTarget",null);
+ org.junit.Assert.assertTrue(fun.isDead());
+ code.link(fun);
+ var con = ConstantNode.raw(tfp);
+ var call = new CallNode(null,con,con,con);
+ con.toString();
+ call.name();
+ code.funcName(tfp.fidx());
+ org.junit.Assert.assertSame(fun,code._link(tfp));
+ // Optimizer lookup still performs its usual lazy cleanup.
+ org.junit.Assert.assertNull(code.link(tfp));
+ org.junit.Assert.assertNull(code._link(tfp));
+ }
+
@Test
public void testJig() throws IOException {
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter25Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter25Test.java
index d2357ef1..b96ae836 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter25Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter25Test.java
@@ -20,6 +20,10 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class Chapter25Test {
+ @Test public void testPrintingConstantPool() throws Exception {
+ com.seaofnodes.simple.codegen.PrintRegTestSupport.checkConstantPool();
+ }
+
@Ignore
@Test
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/codegen/PrintRegTestSupport.java b/chapter25/src/test/java/com/seaofnodes/simple/codegen/PrintRegTestSupport.java
new file mode 100644
index 00000000..b255bcf7
--- /dev/null
+++ b/chapter25/src/test/java/com/seaofnodes/simple/codegen/PrintRegTestSupport.java
@@ -0,0 +1,76 @@
+package com.seaofnodes.simple.codegen;
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.TypeInteger;
+import static org.junit.Assert.*;
+public class PrintRegTestSupport {
+ public static void check() throws Exception {
+ var code = new CodeGen("return 0;").parse();
+ var alloc = new RegAlloc(code);
+ var n = new ConstantNode(TypeInteger.ZERO);
+ var a = new LRG((short)1,null);
+ var b = new LRG((short)2,null);
+ var c = new LRG((short)3,null);
+ a._reg = -1;
+ c._leader = b;
+ b._leader = a;
+ var field = RegAlloc.class.getDeclaredField("_lrgs");
+ field.setAccessible(true);
+ var map = (java.util.Map<Node,LRG>)field.get(alloc);
+ map.put(n,c);
+ assertEquals("V1",alloc.reg(n));
+ assertEquals(-1,alloc.regnum(n));
+ assertSame(c,map.get(n));
+ assertSame(b,c._leader);
+ assertSame(a,b._leader);
+ }
+ public static void checkConstantPool() throws Exception {
+ var code = new CodeGen("return 0;").parse();
+ code._phase = CodeGen.Phase.Encoding;
+ var enc = new Encoding(code);
+ var t = com.seaofnodes.simple.type.TypeStruct.make("PrintPool",false,
+ com.seaofnodes.simple.type.Field.make("x",TypeInteger.ZERO,2,true));
+ var n = new ConstantNode(TypeInteger.ZERO);
+ enc.largeConstant(n,t,0,2);
+ int size = t.size(); // Encoding lays out data before printing the pool.
+ for( int i=0; i<size; i++ ) enc._cpool.write(0);
+ // Layout queries are forbidden during printing, even if they would
+ // happen to return a cached answer without changing the type.
+ boolean[] printing = {false};
+ var scalar = new com.seaofnodes.simple.type.Type(com.seaofnodes.simple.type.Type.BOTTOM._type) {
+ @Override public int alignment() {
+ assertFalse("alignment queried by printer",printing[0]);
+ return 0;
+ }
+ @Override public int size() {
+ assertFalse("size queried by printer",printing[0]);
+ return 1;
+ }
+ @Override public String str() { return "LayoutProbe"; }
+ };
+ var scalarNode = new ConstantNode(TypeInteger.TRUE);
+ enc._bigCons.put(scalarNode,new Encoding.Relo(scalarNode,scalar,(byte)0,(byte)2));
+ enc._cpool.write(0);
+ printing[0] = true;
+ var offsets = com.seaofnodes.simple.type.TypeStruct.class.getDeclaredField("_offs");
+ offsets.setAccessible(true);
+ offsets.set(t,null); // Printer must not lazily rebuild the cached layout.
+ var field = com.seaofnodes.simple.type.Type.class.getDeclaredField("VISIT");
+ field.setAccessible(true);
+ var visit = (java.util.Map<Object,Object>)field.get(null);
+ var marker = new Object();
+ visit.put(marker,t);
+ var method = com.seaofnodes.simple.print.ASMPrinter.class.getDeclaredMethod(
+ "printConstantPool",int.class,com.seaofnodes.simple.util.SB.class,
+ com.seaofnodes.simple.util.BAOS.class,java.util.HashMap.class,boolean.class,String.class);
+ method.setAccessible(true);
+ try {
+ var sb = new com.seaofnodes.simple.util.SB();
+ method.invoke(null,0,sb,enc._cpool,enc._bigCons,true,"Constant Pool");
+ assertTrue(sb.toString().contains("PrintPool"));
+ assertTrue(sb.toString().contains("LayoutProbe"));
+ assertNull(offsets.get(t));
+ assertSame(t,visit.get(marker));
+ assertEquals(1,visit.size());
+ } finally { visit.clear(); }
+ }
+}
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java b/chapter25/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java
new file mode 100644
index 00000000..70d9071a
--- /dev/null
+++ b/chapter25/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java
@@ -0,0 +1,39 @@
+package com.seaofnodes.simple.node;
+import java.util.*;
+import static org.junit.Assert.*;
+
+// Inspect raw fields: the regression must not itself use lazy accessors.
+public class PrintTestSupport {
+ public static void unchanged(Node root, Runnable print) throws Exception {
+ var hash = Node.class.getDeclaredField("_hash");
+ hash.setAccessible(true);
+ var nodes = new ArrayList<Node>();
+ var seen = new IdentityHashMap<Node,Boolean>();
+ nodes.add(root);
+ seen.put(root,true);
+ for( int i=0; i<nodes.size(); i++ ) {
+ Node n = nodes.get(i);
+ for( Node def : n._inputs )
+ if( def!=null && seen.put(def,true)==null ) nodes.add(def);
+ for( Node use : n._outputs )
+ if( use!=null && seen.put(use,true)==null ) nodes.add(use);
+ }
+ var ins = new IdentityHashMap<Node,Node[]>();
+ var outs = new IdentityHashMap<Node,Node[]>();
+ var hashes = new IdentityHashMap<Node,Integer>();
+ var depths = new IdentityHashMap<Node,Integer>();
+ for( Node n : nodes ) {
+ ins.put(n,n._inputs.toArray(new Node[0]));
+ outs.put(n,n._outputs.toArray(new Node[0]));
+ hashes.put(n,hash.getInt(n));
+ }
+ print.run();
+ for( Node n : nodes ) {
+ assertEquals("hash of node "+n._nid,(int)hashes.get(n),hash.getInt(n));
+ assertEquals(ins.get(n).length,n.nIns());
+ assertEquals(outs.get(n).length,n.nOuts());
+ for( int i=0; i<n.nIns(); i++ ) assertSame(ins.get(n)[i],n.in(i));
+ for( int i=0; i<n.nOuts(); i++ ) assertSame(outs.get(n)[i],n._outputs.get(i));
+ }
+ }
+}