summaryrefslogtreecommitdiff
path: root/chapter21/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'chapter21/src/test/java')
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/BrainFuckTest.java2
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/Chapter18Test.java38
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/Chapter20Test.java4
-rw-r--r--chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java4
4 files changed, 43 insertions, 5 deletions
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/chapter21/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
index 2733620e..1e1a1dc3 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
+++ b/chapter21/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
@@ -16,7 +16,7 @@ public class BrainFuckTest {
String brain_fuck = "Hello World!\n";
TestC.run("brain_fuck", brain_fuck, 40);
- EvalRisc5 R5 = TestRisc5.build("brain_fuck", 0, 44, false);
+ EvalRisc5 R5 = TestRisc5.build("brain_fuck", 0, 28, false);
int trap = R5.step(100000);
assertEquals(0,trap);
int ptr = (int)R5.regs[riscv.A0];
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter18Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter18Test.java
index d5d5c2ef..b3d81f0f 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter18Test.java
+++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter18Test.java
@@ -11,6 +11,44 @@ import static org.junit.Assert.*;
import static org.junit.Assert.fail;
public class Chapter18Test {
+ @Test public void testFunctionLocalConstantChains() {
+ String src = "val f = { int x -> x ? f(x-1)*305420988+x/305420988 : 1; }; "+
+ "val g = { int x -> x ? g(x-1)/305420988+x*305420988 : 1; }; "+
+ "return f(arg)+g(arg);";
+ CodeGen code = new CodeGen(src);
+ code.driver(CodeGen.Phase.Schedule,"riscv","SystemV");
+ var owners = new java.util.IdentityHashMap<CFGNode,Boolean>();
+ code._stop.walk(n -> {
+ if( !(n instanceof com.seaofnodes.simple.node.cpus.riscv.AddIRISC add) ||
+ !(add.in(1) instanceof com.seaofnodes.simple.node.cpus.riscv.LUI upper) ||
+ ((TypeInteger)upper._con).value()+((add._imm12<<20)>>20)!=305420988 )
+ return null;
+ CFGNode fun = n.cfg0();
+ while( fun!=null && !(fun instanceof FunNode) ) fun = fun.idom();
+ assertTrue("Constant must belong to a function",fun instanceof FunNode);
+ assertNull("Share one constant chain within each function",owners.put(fun,true));
+ // Follow all parts of a machine constant, including shared inputs.
+ var todo = new java.util.ArrayList<Node>();
+ var seen = new java.util.IdentityHashMap<Node,Boolean>();
+ todo.add(n);
+ for( int j=0; j<todo.size(); j++ ) {
+ Node part = todo.get(j);
+ if( seen.put(part,true)!=null ) continue;
+ CFGNode owner = part.cfg0();
+ while( owner!=null && !(owner instanceof FunNode) ) owner = owner.idom();
+ assertSame("Every constant-building operation is function-local",fun,owner);
+ for( int i=1; i<part.nIns(); i++ ) {
+ Node def = part.in(i);
+ if( def==null || def instanceof CFGNode ) continue;
+ assertTrue("Copies must register their data edges",def._outputs.find(part)!=-1);
+ if( def._type.isConstant() ) todo.add(def);
+ }
+ }
+ return null;
+ });
+ assertTrue("Exercise constants shared across functions",owners.size()>=2);
+ }
+
@Test public void testReachableMixedReturns() {
for( String[] test : new String[][] {
{"if(arg) return 7; else return 2.5;", "No common type amongst int and f64"},
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter20Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter20Test.java
index 21f096b9..d6bf1f4f 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter20Test.java
+++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter20Test.java
@@ -107,8 +107,8 @@ return sqrt(arg) + sqrt(arg+2);
public void testString() throws IOException {
String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/stringHash.smp"));
testCPU(src,"x86_64_v2", "SystemV", 9,null);
- testCPU(src,"riscv" , "SystemV", 5,null);
- testCPU(src,"arm" , "SystemV", 6,null);
+ testCPU(src,"riscv" , "SystemV", 3,null);
+ testCPU(src,"arm" , "SystemV", 3,null);
}
@Test
diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java
index a5d86fa5..fd91770a 100644
--- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java
+++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter21Test.java
@@ -93,8 +93,8 @@ public class Chapter21Test {
public void testString() throws IOException {
String src = Files.readString(Path.of("src/test/java/com/seaofnodes/simple/progs/stringHash.smp"));
testCPU(src,"x86_64_v2", "SystemV", 9,null);
- testCPU(src,"riscv" , "SystemV", 5,null);
- testCPU(src,"arm" , "SystemV", 6,null);
+ testCPU(src,"riscv" , "SystemV", 3,null);
+ testCPU(src,"arm" , "SystemV", 3,null);
}
@Test public void testStringExport() throws IOException {