summaryrefslogtreecommitdiff
path: root/chapter16
diff options
context:
space:
mode:
Diffstat (limited to 'chapter16')
-rw-r--r--chapter16/README.md54
-rw-r--r--chapter16/src/main/java/com/seaofnodes/simple/Parser.java26
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java2
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter10bTest.java64
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java600
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter12Test.java195
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java564
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java83
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java58
9 files changed, 782 insertions, 864 deletions
diff --git a/chapter16/README.md b/chapter16/README.md
index 663f175a..c55de47f 100644
--- a/chapter16/README.md
+++ b/chapter16/README.md
@@ -1,25 +1,25 @@
-# Chapter 16: Constructors and Final fields
+# Chapter 16: Constructors
[Previous: Chapter 15](../chapter15/README.md) |
-[Next: Chapter 17](../chapter17/README.md)
+[Next: Chapter 17a](../chapter17a/README.md)
# Table of Contents
1. [Constructors](#constructors)
2. [Initialization code](#initialization-code)
-3. [Final fields](#final-fields)
-4. [Multiple Decls](#multiple-declarations-of-the-same-type)
-5. [Memory through constructors](#memory-through-constructors)
+3. [Multiple Decls](#multiple-declarations-of-the-same-type)
+4. [Memory through constructors](#memory-through-constructors)
-In this chapter, we add constructors and final fields.
+This chapter adds field defaults and constructor blocks. Chapter 17a will add
+fixed bindings and read-only access, using these constructors to initialize them.
You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linear-chapter16) in a linear Git revision history on the [linear](https://github.com/SeaOfNodes/Simple/tree/linear) branch and [compare](https://github.com/SeaOfNodes/Simple/compare/linear-chapter15...linear-chapter16) it to the previous chapter.
## Constructors
-A big problem with the [Chapter 13](../chapter13/README.md) refs is that
+A big problem with the [Chapter 12](../chapter12/README.md) refs is that
not-null fields always start out `null`. This is fixed in this chapter, where
-a *constructor* syntax is required to initialize `final` and not-null fields.
+a *constructor* syntax is required to initialize not-null fields.
Fields are initialized in three ways:
@@ -46,7 +46,7 @@ struct Point { int x=1; int y=1; }; // Point x and y will start as 1, not 0
return new Point { x=3; }.x; // Returns a 3
```
-Not-null and final fields *must* be initialized before first use and before the
+Not-null fields *must* be initialized before first use and before the
end of the allocation. They can be initialized in either the declaration or
allocation. They do not start with the default value, although the
initialization can be to the default.
@@ -81,6 +81,8 @@ struct Square {
return new Square;
```
+Another example:
+
```
struct Buffer {
if( arg < 0 || arg > 1000000 )
@@ -91,32 +93,6 @@ return new Buffer;
```
-## Final fields
-
-Final fields are declared with a `!` before the name.
-This decision is revisited in the next chapter.
-
-```
-int !x = 17; // Final field x
-x = 3; // ERROR: cannot re-assign a final field
-```
-
-```
-struct Person { u8[]? !name; };
-return new Person { name = null; }; // OK: final name is assigned
-```
-
-```
-struct Point { int !x; int !y; int !z; };
-return new Point { x=arg; y=z; z=x; }; // ERROR: 'Point' is not fully initialized, field 'y' needs to be set in a constructor
-```
-
-```
-struct Point { int x; int y; int z; }; // Fields not-final, default to 0
-return new Point { x=arg; y=z; z=x; }; // Ok: x=arg, y=0, z=arg
-```
-
-
### Multiple declarations of the same type
Also new this chapter is allowing multiple declarations with the same type, as
@@ -127,16 +103,12 @@ int x,y; // Two int variables declared
struct Point { int x,y,z; }; // Three fields declared
```
-```
-int !x=3,!y=5; // Two int variables declared, both are final and initialized
-```
## Memory through constructors
-The lazy memory partitioning from [Chapter 10b](../chapter10b/README.md) continues
+The lazy memory partitioning from [Chapter 11](../chapter11/README.md) continues
through constructors. The parser tracks one `$mem` variable alongside its scalar
variables. Branches and loops merge that binding with BulkMemPhi; field access
-splits out precise MemPhis when needed. The variable records for types and final
-fields live in ScopeNode. There is no separate parser table of memory aliases.
+splits out precise MemPhis when needed. The variable records for declared types live in ScopeNode. There is no separate parser table of memory aliases.
A constructor computes its field values before the allocation. Reads, writes,
and loops in its body update the same `$mem` binding as ordinary code. New then
diff --git a/chapter16/src/main/java/com/seaofnodes/simple/Parser.java b/chapter16/src/main/java/com/seaofnodes/simple/Parser.java
index a7d31584..97b29a37 100644
--- a/chapter16/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter16/src/main/java/com/seaofnodes/simple/Parser.java
@@ -402,7 +402,7 @@ public class Parser {
/** Parse: name [=expr]
*/
- private Node parseAsgn(Type t, boolean xfinal) {
+ private Node parseAsgn(Type t) {
boolean isDecl = t!=null; // Having a type is a declaration, missing one is updating a prior name
int old = _lexer._position;
String name = requireId();
@@ -411,7 +411,7 @@ public class Parser {
// Bare "name" is not allowed
if( t==null ) throw errorSyntax("expression");
// Does this declaration need an initializer and not getting one?
- Type init = xfinal || t instanceof TypeMemPtr tmp && !tmp._nil
+ Type init = t instanceof TypeMemPtr tmp && !tmp._nil
// Must be initialized in the "new" constructor
? Type.TOP
// Else takes the default
@@ -430,11 +430,6 @@ public class Parser {
ScopeNode.Var def = _scope.lookup(name);
if( def==null )
throw error("Undefined name '" + name + "'");
- // TOP fields are for late-initialized fields; these have never
- // been written to, and this must be the final write. Other writes
- // need to check the final bit.
- if( _scope.in(def._idx)._type!=Type.TOP && def._final )
- throw error("Cannot reassign final '"+name+"'");
t = def._type; // Declared field type
}
@@ -452,20 +447,13 @@ public class Parser {
throw error("Type " + e.str() + " is not of declared type " + t.str());
if( isDecl ) {
- if( !_scope.define(name,t,xfinal,expr) )
+ if( !_scope.define(name,t,false,expr) )
throw error("Redefining name '" + name + "'");
} else
_scope.update(name,expr);
return expr;
}
- /** Parse final: [!]asgn
- */
- private Node parseFinal(Type t) {
- boolean xfinal = t!=null && match("!");
- return parseAsgn(t,xfinal);
- }
-
/**
* Parses an expression statement or a declaration statement where type is a struct
*
@@ -482,13 +470,13 @@ public class Parser {
Node n;
Type t = type();
if( t != null ) {
- // now parse final [, final]*
- n = parseFinal(t);
+ // Parse comma-separated declarations
+ n = parseAsgn(t);
while( match(",") )
- n = parseFinal(t);
+ n = parseAsgn(t);
// Parse "asgn;" which is just "name = expr;"
- } else if( ( n = parseAsgn(null,false)) == null ) {
+ } else if( ( n = parseAsgn(null)) == null ) {
// Something else
n = parseExpression();
}
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java
index 75119a75..0b8be64b 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java
@@ -25,7 +25,7 @@ public class Chapter10Test {
// Issue #246: null-check guards start in Chapter 10; arrays arrive in Chapter 15.
private static final String NULLABLE_POINT_SOURCE = """
struct Point { int x; };
- Point?[] !points = new Point?[2];
+ Point?[] points = new Point?[2];
points[arg] = new Point { x = 42; };
Point? p = points[1];
""";
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter10bTest.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter10bTest.java
deleted file mode 100644
index 4281f0a1..00000000
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter10bTest.java
+++ /dev/null
@@ -1,64 +0,0 @@
-package com.seaofnodes.simple;
-
-import com.seaofnodes.simple.node.*;
-import com.seaofnodes.simple.evaluator.Evaluator;
-import org.junit.Test;
-import static org.junit.Assert.*;
-
-public class Chapter10bTest {
- @Test public void testIndependentField() {
- var stop = new Parser("""
- struct S { int x; int y; };
- S s = new S;
- s.x = 42;
- while (arg > 0) { s.y = s.y + arg; arg = arg - 1; }
- return s.x;
- """).parse().iterate();
- assertEquals("return 42;",stop.toString());
- assertEquals(42L,Evaluator.evaluate(stop,5));
- assertNull(stop.walk(n -> n instanceof LoadNode ? n : null));
- }
-
- @Test public void testParallelMemoryPhis() {
- var stop = new Parser("""
- struct S { int x; int y; int z; };
- S a = new S; S b = new S;
- S p = a; if (arg) p = b;
- while (arg > 0) {
- a.x = p.x + arg;
- a.y = p.y + 2;
- a.z = p.z + 3;
- arg = arg - 1;
- }
- return a;
- """).parse().iterate();
- int[] phis = {0};
- stop.walk(n -> {
- if (n instanceof MemPhiNode) phis[0]++;
- if (n instanceof MemMergeNode m && m.in(1) instanceof BulkMemPhiNode b)
- for(int a=b._aliases.nextSetBit(0); a>=0; a=b._aliases.nextSetBit(a+1))
- assertTrue(a<m.nIns() && m.in(a)!=null);
- assertFalse(n instanceof PhiNode && n.isMem() &&
- !(n instanceof MemPhiNode) && !(n instanceof BulkMemPhiNode));
- return null;
- });
- assertTrue(phis[0]>=3);
- assertArrayEquals(new Object[]{1L,2L,3L},((Evaluator.Obj)Evaluator.evaluate(stop,4)).fields());
- }
-
- @Test public void testLateSliceWorklist() throws ReflectiveOperationException {
- // Seed 97 exposed a store selecting a bulk predecessor without queuing
- // it. Keep both the worklist assertion and the final heap check.
- for (int seed : new int[]{0,97,123,456}) {
- var parser = new Parser(Chapter10Test.NESTED_MEMORY);
- var field = IterPeeps.class.getDeclaredField("WORK");
- field.setAccessible(true);
- Object work = field.get(null);
- field = work.getClass().getDeclaredField("_R");
- field.setAccessible(true);
- ((java.util.Random)field.get(work)).setSeed(seed);
- var stop = parser.parse().iterate();
- assertArrayEquals(new Object[]{9L,25L,17L},((Evaluator.Obj)Evaluator.evaluate(stop,4)).fields());
- }
- }
-}
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java
index 1183da5f..e009c6b6 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java
@@ -1,554 +1,64 @@
package com.seaofnodes.simple;
-
+import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.evaluator.Evaluator;
-import com.seaofnodes.simple.node.StopNode;
import org.junit.Test;
-
-
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
public class Chapter11Test {
-
- // A placeholder test used to rapidly rotate through fuzzer produced issues
- @Test
- public void testFuzzer() {
- Parser parser = new Parser(
-"""
-while(-----arg) {
- while(-arg) {
- while(-----arg*---0) {
- int N=--false==false<--arg;
- while(N<-4==N) {
- N=N+N;
- }
- }
- {
- int kzEl_r7EYz=3*----0!=-arg<(37*--97!=0/arg==arg)<=-(--arg!=0);
- while(kzEl_r7EYz<---1) {
- kzEl_r7EYz=kzEl_r7EYz+kzEl_r7EYz*arg>=24;
- if(7) continue;
- kzEl_r7EYz=false;
- if(-kzEl_r7EYz+arg<-3) continue;
- else {
- while(kzEl_r7EYz) continue;
- arg=----3;
- kzEl_r7EYz=-(-----arg*1>-kzEl_r7EYz);
- int v2uF_4ba=9;
- }
- }
- }
- int pjcp7rv=arg;
- {
- int m2ya3=--arg-pjcp7rv;
- while(m2ya3<-----pjcp7rv>=pjcp7rv+--pjcp7rv!=---pjcp7rv!=-arg<---(-m2ya3)>m2ya3<=-----true>14!=--m2ya3) {
- m2ya3=m2ya3+-(--0<=false<pjcp7rv/arg<=-true);
- while((--m2ya3)<--arg<-----arg*false!=arg<pjcp7rv*arg) {
- pjcp7rv=-3+-arg;
- break;
- }
- while(-3) {
- while(67+0-61) m2ya3=m2ya3;
- if(----pjcp7rv==---(---arg)) pjcp7rv=0!=-23;
- else pjcp7rv=----89;
- while(m2ya3!=m2ya3<pjcp7rv) m2ya3=1;
- while(--arg<=pjcp7rv) pjcp7rv=-32<=-m2ya3+62;
- }
- break;
- }
- }
- {
- int arg=-1<-9!=(-arg>=-pjcp7rv>=arg<=arg);
- while(arg<-arg) {
- arg=arg+--arg;
- {
- while((--93)--pjcp7rv*-pjcp7rv+arg) arg=0-(6>=(--(---41/(0))!=--pjcp7rv)>=---2*33);
- pjcp7rv=11;
- int c=pjcp7rv;
- if(--107) break;
- else break;
- }
- }
- }
- pjcp7rv=pjcp7rv;
- int aR0Wkua=-pjcp7rv==pjcp7rv;
- {
- if(-0!=-0) while(-true) break;
- else {
- while(aR0Wkua) pjcp7rv=-0/pjcp7rv;
- pjcp7rv=pjcp7rv;
- }
+ @Test public void testIndependentField() {
+ var stop = new Parser("""
+ struct S { int x; int y; };
+ S s = new S;
+ s.x = 42;
+ while (arg > 0) { s.y = s.y + arg; arg = arg - 1; }
+ return s.x;
+ """).parse().iterate();
+ assertEquals("return 42;",stop.toString());
+ assertEquals(42L,Evaluator.evaluate(stop,5));
+ assertNull(stop.walk(n -> n instanceof LoadNode ? n : null));
+ }
+
+ @Test public void testParallelMemoryPhis() {
+ var stop = new Parser("""
+ struct S { int x; int y; int z; };
+ S a = new S; S b = new S;
+ S p = a; if (arg) p = b;
+ while (arg > 0) {
+ a.x = p.x + arg;
+ a.y = p.y + 2;
+ a.z = p.z + 3;
+ arg = arg - 1;
+ }
+ return a;
+ """).parse().iterate();
+ int[] phis = {0};
+ stop.walk(n -> {
+ if (n instanceof MemPhiNode) phis[0]++;
+ if (n instanceof MemMergeNode m && m.in(1) instanceof BulkMemPhiNode b)
+ for(int a=b._aliases.nextSetBit(0); a>=0; a=b._aliases.nextSetBit(a+1))
+ assertTrue(a<m.nIns() && m.in(a)!=null);
+ assertFalse(n instanceof PhiNode && n.isMem() &&
+ !(n instanceof MemPhiNode) && !(n instanceof BulkMemPhiNode));
+ return null;
+ });
+ assertTrue(phis[0]>=3);
+ assertArrayEquals(new Object[]{1L,2L,3L},((Evaluator.Obj)Evaluator.evaluate(stop,4)).fields());
+ }
+
+ @Test public void testLateSliceWorklist() throws ReflectiveOperationException {
+ // Seed 97 exposed a store selecting a bulk predecessor without queuing
+ // it. Keep both the worklist assertion and the final heap check.
+ for (int seed : new int[]{0,97,123,456}) {
+ var parser = new Parser(Chapter10Test.NESTED_MEMORY);
+ var field = IterPeeps.class.getDeclaredField("WORK");
+ field.setAccessible(true);
+ Object work = field.get(null);
+ field = work.getClass().getDeclaredField("_R");
+ field.setAccessible(true);
+ ((java.util.Random)field.get(work)).setSeed(seed);
+ var stop = parser.parse().iterate();
+ assertArrayEquals(new Object[]{9L,25L,17L},((Evaluator.Obj)Evaluator.evaluate(stop,4)).fields());
}
}
- if(-arg) {
- {
- int Isdlt0fr91=0!=arg;
- while(Isdlt0fr91<-Isdlt0fr91==-Isdlt0fr91+true) {
- Isdlt0fr91=Isdlt0fr91+-1!=true;
- if(-arg) if(--arg<=Isdlt0fr91) Isdlt0fr91=-Isdlt0fr91;
- int i=---arg<---arg!=--40;
- {
- int jVX43Ln4Dm=-(6)==-Isdlt0fr91/----0;
- while(jVX43Ln4Dm<--(3-3)) {
- jVX43Ln4Dm=jVX43Ln4Dm+-0<25;
- i=24<--(jVX43Ln4Dm);
- }
- }
- i=(arg!=----false>=7);
- int Te=5>-arg;
- break;
- }
- }
- arg=arg!=--true/arg;
- {
- int UX=arg;
- while(UX<-3*arg) {
- UX=UX+arg/-1;
- arg=-3--(-UX--47<arg==--arg==arg)/arg--UX>=-arg;
- if(---12) while(-UX) break;
- break;
- }
- }
- continue;
- }
- if(arg) arg=--0;
- else if(1) continue;
- int DtTlhjYq=19==arg;
- {
- arg=--DtTlhjYq<=(DtTlhjYq/---DtTlhjYq<=-DtTlhjYq);
- if(---arg) continue;
- else {
- int Zsl=40;
- while(Zsl<--Zsl--false>((Zsl))==Zsl!=--DtTlhjYq-DtTlhjYq-DtTlhjYq) {
- Zsl=Zsl+arg;
- }
- }
- if((arg)) if(-DtTlhjYq) {
- }
- else while(((2)==3*arg/DtTlhjYq)<=0==---arg) return false;
- else {
- int lwbF=arg>=0;
- while(lwbF<57) {
- lwbF=lwbF+--(-arg+lwbF)>=-2>=arg!=arg;
- arg=----0+0;
- while(49) break;
- if(true) DtTlhjYq=-----arg;
- DtTlhjYq=0!=-----(0);
- }
- }
- arg=---arg!=DtTlhjYq/--0;
- DtTlhjYq=-DtTlhjYq/(--arg<arg);
- if(32) {
- int q=-DtTlhjYq;
- while(q<arg*(0*q)/0) {
- q=q+DtTlhjYq;
- DtTlhjYq=arg;
- }
- }
- else if(--arg) if(15) break;
- else arg=0==arg<=-55>DtTlhjYq;
- else {
- }
- DtTlhjYq=-0;
- {
- while((0)<-arg) DtTlhjYq=-DtTlhjYq;
- }
- if(30/--(arg)>=0) DtTlhjYq=-0>=arg;
- else {
- int Y2wcIQJ=--DtTlhjYq*---11;
- if(arg==arg) {
- Y2wcIQJ=-----Y2wcIQJ;
- Y2wcIQJ=Y2wcIQJ<7;
- if(DtTlhjYq) arg=arg;
- else break;
- while(-arg!=-31==(false<=-15>=-DtTlhjYq+--6*-----Y2wcIQJ<=---arg!=-Y2wcIQJ<=---0<-arg*--Y2wcIQJ)) DtTlhjYq=arg!=3;
- }
- continue;
- }
- arg=DtTlhjYq;
- arg=--3/(---arg)>=80>=-DtTlhjYq<=DtTlhjYq;
- arg=51+---(-true)!=arg!=----1==-DtTlhjYq;
- if(11<=arg) if((arg)+79<32+(-arg)) continue;
- else if(true==DtTlhjYq) break;
- break;
- }
-}
-if(arg!=62==arg) {
- while(---arg) {
- {
- int bhmDacPM=arg/arg;
- while(bhmDacPM<arg) {
- bhmDacPM=bhmDacPM+80*((-bhmDacPM)/---bhmDacPM>1)<bhmDacPM-2!=7;
- arg=-0;
- arg=bhmDacPM>=-arg+-arg;
- break;
- }
- }
- int arg=true;
- arg=arg<=arg;
- {
- int KlC=30;
- while(KlC<-2) {
- KlC=KlC+-1!=25<=-KlC>KlC>----0;
- int Ij=arg>=-arg<=---10;
- {
- int LAbgurVa2R=-arg!=Ij;
- while(LAbgurVa2R<-true) {
- LAbgurVa2R=LAbgurVa2R+Ij;
- KlC=-LAbgurVa2R;
- Ij=42*(-Ij>true);
- while(Ij) arg=KlC;
- KlC=-----7>-(--KlC<--7)==-LAbgurVa2R;
- }
- }
- int a=Ij<=-KlC;
- if(Ij) {
- int r1W=a!=-false;
- }
- else {
- arg=56>=Ij;
- while(54) break;
- arg=8>=-(a<--Ij)/a>=-a-KlC;
- }
- int Yt_T=KlC;
- arg=KlC;
- }
- }
- if(----false/--arg/12+-----0) if(-9+-arg) while(arg!=(arg)) arg=--1>=-71-arg!=---(--arg*11+102)/--arg;
- else if(----arg) continue;
- if(-arg-arg) {
- arg=-(-arg)*--(-3)<(-7==-arg----arg);
- {
- int arg=arg;
- while(arg<-0>arg==-arg-(-arg>1+11)) {
- arg=arg+6-arg!=--0>---30>=---arg<=arg/(3)+---arg;
- }
- }
- arg=-arg+arg;
- arg=--47+-arg<1>arg*(-arg==-arg--0>3);
- arg=(----arg)!=arg+--59;
- arg=--4+arg==-(arg)-arg;
- }
- else {
- while(--true-----true*-25+-18>arg>=-----(-arg)---13) {
- arg=-0+--true<false;
- continue;
- }
- arg=true<=arg;
- break;
- }
- arg=arg*22<--(0+-arg);
- {
- continue;
- }
- }
- arg=1*---((3<=true)==---2);
- arg=7+-(-1);
- return arg+--true;
-}
-else {
- int o3ypEsewxM=-(---arg);
- int vzXB=(-(---11<=--o3ypEsewxM>=--0))<=-0/-arg;
- int L=-arg;
- int B4mHcrf=---vzXB;
- int oZWGdzZl=-o3ypEsewxM;
- int K6tQqa1=11;
- while(0!=--L) {
- int sdZfTM=vzXB*true+K6tQqa1;
- while(sdZfTM<false) {
- sdZfTM=sdZfTM+arg;
- sdZfTM=oZWGdzZl;
- K6tQqa1=-K6tQqa1;
- arg=---K6tQqa1;
- while((3+-K6tQqa1)<oZWGdzZl) {
- if(-----(-1)) sdZfTM=-false;
- {}
- }
- K6tQqa1=vzXB!=-(B4mHcrf+-7>false);
- sdZfTM=vzXB;
- }
- }
- L=false;
- while(31) B4mHcrf=arg;
- oZWGdzZl=-0<--oZWGdzZl<=--2;
- if(K6tQqa1>=20<=46) {
- if(-1+-L) if(oZWGdzZl) {
- int wmhv=-----0;
- }
- int arg=0;
- o3ypEsewxM=---(oZWGdzZl);
- return -oZWGdzZl/0;
- }
- else if(arg) {
- int yKk5=B4mHcrf>=4>K6tQqa1<-B4mHcrf/o3ypEsewxM;
- return -K6tQqa1;
- }
- else {
- if(---0) {
- {}
- {}
- }
- return o3ypEsewxM;
- }
-}
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 9; return 0; return 0; ]", stop.toString());
- }
-
-
- @Test
- public void testPrimes() {
- Parser parser = new Parser(
-"""
-if( arg < 2 ) return 0;
-int primeCount = 1;
-int prime = 3;
-while( prime <= arg ) {
- int isPrime = 1;
- // Check for even case, so the next loop need only check odds
- if( (prime/2)*2 == prime )
- continue;
- // Check odds up to sqrt of prime
- int j = 3;
- while( j*j <= prime ) {
- if( (prime/j)*j == prime ) {
- isPrime = 0;
- break;
- }
- j = j + 2;
- }
- if( isPrime )
- primeCount = primeCount + 1;
- prime = prime + 2;
-}
-return primeCount;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 0; return Phi(Loop21,1,Phi(Region88,Phi_primeCount,Phi(Region82,(Phi_primeCount+1),Phi_primeCount))); ]", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 1)); // No primes 1 or below
- assertEquals(1L, Evaluator.evaluate(stop, 2)); // 2
- assertEquals(2L, Evaluator.evaluate(stop, 3)); // 2, 3
- assertEquals(2L, Evaluator.evaluate(stop, 4)); // 2, 3
- assertEquals(3L, Evaluator.evaluate(stop, 5)); // 2, 3, 5
- assertEquals(4L, Evaluator.evaluate(stop, 10)); // 2, 3, 5, 7
- }
-
- @Test
- public void testAntiDeps1() {
- Parser parser = new Parser(
-"""
-struct S { int f; };
-S v=new S;
-v.f = 2;
-int i=new S.f;
-i=v.f;
-if (arg) v.f=1;
-return i;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 2;", stop.toString());
- assertEquals(2L, Evaluator.evaluate(stop, 0));
- assertEquals(2L, Evaluator.evaluate(stop, 1));
- }
-
- @Test
- public void testAntiDeps2() {
- Parser parser = new Parser(
-"""
-struct S { int f; };
-S v = new S;
-v.f = arg;
-S t = new S;
-int i = 0;
-if (arg) {
- if (arg+1) v = t;
- i = v.f;
-} else {
- v.f = 2;
-}
-return i;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region48,.f,0);", stop.toString());
- }
-
- @Test
- public void testAntiDeps3() {
- Parser parser = new Parser(
-"""
-struct S { int f; };
-S v0 = new S;
-S? v1;
-if (arg) v1 = new S;
-if (v1) {
- v0.f = v1.f;
-} else {
- v0.f = 2;
-}
-return v0;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return S;", stop.toString());
- }
-
-
- @Test
- public void testAntiDeps4() {
- Parser parser = new Parser(
-"""
-struct S { int f; };
-S v = new S;
-v.f = arg;
-S t = new S;
-int i = v.f;
-if (arg+1) arg= 0;
-while (arg) v.f = 2;
-return i;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return arg;", stop.toString());
- }
-
- @Test
- public void testAntiDeps5() {
- Parser parser = new Parser(
-"""
-struct S { int f; };
-S v = new S;
-while(1) {
- while(arg+1) { arg=arg-1; }
- if (arg) break;
- v.f = 2;
-}
-return v;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return S;", stop.toString());
- }
-
- @Test
- public void testAntiDeps6() {
- Parser parser = new Parser(
-"""
-struct s { int v; };
-s ptr=new s;
-while( -arg )
- ptr = new s;
-while(1)
- arg = arg+ptr.v;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 0; return 0; ]", stop.toString());
- }
-
- @Test
- public void testAntiDeps7() {
- Parser parser = new Parser(
-"""
-struct S { int f; };
-S v = new S;
-S t = new S;
-int i = v.f;
-while (arg) {
- v.f = arg;
- arg = arg-1;
-}
-return i;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- }
-
- @Test
- public void testAntiDeps8() {
- Parser parser = new Parser(
-"""
-struct S { int f; };
-S v = new S;
-S t = new S;
-while(arg) {
- arg=arg-1;
- int f = v.f;
- v.f = 2;
- if (arg) arg = f;
- else v.f = 3;
-}
-return arg;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop23,arg,Phi(Region50,.f,0));", stop.toString());
- }
-
- @Test
- public void testAntiDeps9() {
- Parser parser = new Parser(
-"""
-struct S { int f; };
-S v = new S;
-S t = new S;
-if (arg) {
- v.f=2;
- int i=t.f;
- v.f=i;
-}
-return v;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return S;", stop.toString());
- }
-
- @Test
- public void testExample2() {
- Parser parser = new Parser(
-"""
-struct S { int f; };
-S v = new S;
-int i = arg;
-while (arg > 0) {
- int j = i/3;
- if (arg == 5)
- v.f = j;
- arg = arg - 1;
-}
-return v;
- """);
- StopNode stop = parser.parse().iterate();
- //assertEquals("return new S;", stop.toString());
- }
-
- @Test
- public void testScheduleUse() {
- Parser parser = new Parser(
-"""
-int v0=0;
-while(0>=0) {
- u1 v1=0;
- v1=v0;
- if(v1*0)
- v0=-v1;
-}
-return 0;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- }
-
- @Test
- public void testLoopCarriedDep() {
- Parser parser = new Parser(
-"""
-u32 v0=0;
-{
- int v1=0;
- while(v1) {
- v1=1>>>v0!=0;
- v0=v1/0;
- }
-}
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- }
-
}
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index 998053ec..22976058 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -2,82 +2,195 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.StopNode;
-import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class Chapter12Test {
- @Test public void testSubZeroFloat() {
- var stop = new Parser("flt x = arg; return 0-x;").parse().iterate();
- // Compare strings so +0.0 and -0.0 remain distinct.
- assertEquals("0.0", Evaluator.evaluate(stop,0).toString());
- assertEquals("-1.0", Evaluator.evaluate(stop,1).toString());
+ @Test
+ public void testJig() {
+ Parser parser = new Parser(
+"""
+return 3.14;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 3.14;", stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(stop, 0));
}
- @Test public void testDeadNumericReturns() {
- for( String[] test : new String[][] {
- {"if(1) return 7; else return 2.5;", "7"},
- {"if(0) return 7; else return 2.5;", "2.5"},
- {"return 7; return 2.5;", "7"},
- {"return 2.5; return 7;", "2.5"}
- } ) {
- String src = test[0];
- var stop = new Parser(src).parse().iterate();
- assertEquals(src,test[1],com.seaofnodes.simple.evaluator.Evaluator.evaluate(stop,0).toString());
- }
+ @Test
+ public void testLinkedList0() {
+ Parser parser = new Parser(
+"""
+struct LLI { LLI? next; int i; };
+LLI? head = null;
+while( arg ) {
+ LLI x = new LLI;
+ x.next = head;
+ x.i = arg;
+ head = x;
+ arg = arg-1;
+}
+return head.next.i;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
}
+ @Test
+ public void testLinkedList1() {
+ Parser parser = new Parser(
+"""
+struct LLI { LLI? next; int i; };
+LLI? head = null;
+while( arg ) {
+ LLI x = new LLI;
+ x.next = head;
+ x.i = arg;
+ head = x;
+ arg = arg-1;
+}
+if( !head ) return 0;
+LLI? next = head.next;
+if( next==null ) return 1;
+return next.i;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("Stop[ return 0; return 1; return .i; ]", stop.toString());
+ assertEquals(2L, Evaluator.evaluate(stop, 3));
+ }
@Test
- public void testJig() {
+ public void testCoRecur() {
Parser parser = new Parser(
"""
-return 3.14;
+struct int0 { int i; flt0? f; };
+struct flt0 { flt f; int0? i; };
+int0 i0 = new int0;
+i0.i = 17;
+flt0 f0 = new flt0;
+f0.f = 3.14;
+i0.f = f0;
+f0.i = i0;
+return f0.i.f.i.i;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ assertEquals("return 17;", stop.toString());
}
@Test
- public void testFloat() {
+ public void testNullRef0() {
Parser parser = new Parser(
"""
-return 3.14;
+struct N { N? next; int i; };
+N n = new N;
+return n.next;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ assertEquals("return null;", stop.toString());
}
@Test
- public void testSquareRoot() {
+ public void testNullRef1() {
Parser parser = new Parser(
"""
-flt guess = arg;
-while( 1 ) {
- flt next = (arg/guess + guess)/2;
- if( next == guess ) break;
- guess = next;
-}
-return guess;
+struct M { int m; };
+struct N { M next; int i; };
+N n = new N { next = new M; };
+return n.next;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return M;", stop.toString());
+ }
+
+ @Test
+ public void testNullRef2() {
+ Parser parser = new Parser(
+"""
+struct M { int m; };
+struct N { M next; int i; };
+N n = new N { next = null; }
+return n.next;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Type null is not of declared type *M",e.getMessage()); }
+ }
+
+ @Test
+ public void testNullRef3() {
+ Parser parser = new Parser(
+"""
+struct N { N? next; int i; };
+N n = new N;
+n.i = 3.14;
+return n.i;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot store 3.14 into field int i",e.getMessage()); }
+ }
+
+ @Test
+ public void testNullRef4() {
+ Parser parser = new Parser("-null-5/null-5");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Expected an identifier, found 'null'",e.getMessage()); }
+ }
+
+ @Test public void testNullRef5() {
+ Parser parser = new Parser("return null+42;");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
+ }
+
+ @Test
+ public void testEmpty() {
+ Parser parser = new Parser(
+"""
+struct S{};
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop10,(flt)arg,(((ToFloat/Phi_guess)+Phi_guess)/2.0));", stop.toString());
- assertEquals(3.0, Evaluator.evaluate(stop, 9));
- assertEquals(1.414213562373095, Evaluator.evaluate(stop, 2));
+ assertEquals("return 0;", stop.toString());
+ assertEquals(0L, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test
+ public void testForwardRef0() {
+ Parser parser = new Parser(
+"""
+struct S1 { S2 s; };
+return new S2;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Unknown struct type 'S2'",e.getMessage()); }
}
@Test
- public void testFPOps() {
+ public void testForwardRef1() {
Parser parser = new Parser(
"""
-flt x = arg;
-return x+1==x;
+struct S1 { S2? s; };
+struct S2 { int x; };
+return new S1.s=new S2;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return ((flt)arg==(ToFloat+1.0));", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 1));
+ assertEquals("return S2;", stop.toString());
+ }
+
+ @Test
+ public void testcheckNull() {
+ Parser parser = new Parser(
+"""
+struct I {int i;};
+struct P { I? pi; };
+P p1 = new P;
+P p2 = new P;
+p2.pi = new I;
+p2.pi.i = 2;
+if (arg) p1 = new P;
+return p1.pi.i + 1;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
}
+
}
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java
index aa496030..c749aba6 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java
@@ -1,196 +1,554 @@
package com.seaofnodes.simple;
+
import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.StopNode;
import org.junit.Test;
+
+
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
public class Chapter13Test {
+
+ // A placeholder test used to rapidly rotate through fuzzer produced issues
@Test
- public void testJig() {
+ public void testFuzzer() {
Parser parser = new Parser(
"""
-return 3.14;
+while(-----arg) {
+ while(-arg) {
+ while(-----arg*---0) {
+ int N=--false==false<--arg;
+ while(N<-4==N) {
+ N=N+N;
+ }
+ }
+ {
+ int kzEl_r7EYz=3*----0!=-arg<(37*--97!=0/arg==arg)<=-(--arg!=0);
+ while(kzEl_r7EYz<---1) {
+ kzEl_r7EYz=kzEl_r7EYz+kzEl_r7EYz*arg>=24;
+ if(7) continue;
+ kzEl_r7EYz=false;
+ if(-kzEl_r7EYz+arg<-3) continue;
+ else {
+ while(kzEl_r7EYz) continue;
+ arg=----3;
+ kzEl_r7EYz=-(-----arg*1>-kzEl_r7EYz);
+ int v2uF_4ba=9;
+ }
+ }
+ }
+ int pjcp7rv=arg;
+ {
+ int m2ya3=--arg-pjcp7rv;
+ while(m2ya3<-----pjcp7rv>=pjcp7rv+--pjcp7rv!=---pjcp7rv!=-arg<---(-m2ya3)>m2ya3<=-----true>14!=--m2ya3) {
+ m2ya3=m2ya3+-(--0<=false<pjcp7rv/arg<=-true);
+ while((--m2ya3)<--arg<-----arg*false!=arg<pjcp7rv*arg) {
+ pjcp7rv=-3+-arg;
+ break;
+ }
+ while(-3) {
+ while(67+0-61) m2ya3=m2ya3;
+ if(----pjcp7rv==---(---arg)) pjcp7rv=0!=-23;
+ else pjcp7rv=----89;
+ while(m2ya3!=m2ya3<pjcp7rv) m2ya3=1;
+ while(--arg<=pjcp7rv) pjcp7rv=-32<=-m2ya3+62;
+ }
+ break;
+ }
+ }
+ {
+ int arg=-1<-9!=(-arg>=-pjcp7rv>=arg<=arg);
+ while(arg<-arg) {
+ arg=arg+--arg;
+ {
+ while((--93)--pjcp7rv*-pjcp7rv+arg) arg=0-(6>=(--(---41/(0))!=--pjcp7rv)>=---2*33);
+ pjcp7rv=11;
+ int c=pjcp7rv;
+ if(--107) break;
+ else break;
+ }
+ }
+ }
+ pjcp7rv=pjcp7rv;
+ int aR0Wkua=-pjcp7rv==pjcp7rv;
+ {
+ if(-0!=-0) while(-true) break;
+ else {
+ while(aR0Wkua) pjcp7rv=-0/pjcp7rv;
+ pjcp7rv=pjcp7rv;
+ }
+ }
+ }
+ if(-arg) {
+ {
+ int Isdlt0fr91=0!=arg;
+ while(Isdlt0fr91<-Isdlt0fr91==-Isdlt0fr91+true) {
+ Isdlt0fr91=Isdlt0fr91+-1!=true;
+ if(-arg) if(--arg<=Isdlt0fr91) Isdlt0fr91=-Isdlt0fr91;
+ int i=---arg<---arg!=--40;
+ {
+ int jVX43Ln4Dm=-(6)==-Isdlt0fr91/----0;
+ while(jVX43Ln4Dm<--(3-3)) {
+ jVX43Ln4Dm=jVX43Ln4Dm+-0<25;
+ i=24<--(jVX43Ln4Dm);
+ }
+ }
+ i=(arg!=----false>=7);
+ int Te=5>-arg;
+ break;
+ }
+ }
+ arg=arg!=--true/arg;
+ {
+ int UX=arg;
+ while(UX<-3*arg) {
+ UX=UX+arg/-1;
+ arg=-3--(-UX--47<arg==--arg==arg)/arg--UX>=-arg;
+ if(---12) while(-UX) break;
+ break;
+ }
+ }
+ continue;
+ }
+ if(arg) arg=--0;
+ else if(1) continue;
+ int DtTlhjYq=19==arg;
+ {
+ arg=--DtTlhjYq<=(DtTlhjYq/---DtTlhjYq<=-DtTlhjYq);
+ if(---arg) continue;
+ else {
+ int Zsl=40;
+ while(Zsl<--Zsl--false>((Zsl))==Zsl!=--DtTlhjYq-DtTlhjYq-DtTlhjYq) {
+ Zsl=Zsl+arg;
+ }
+ }
+ if((arg)) if(-DtTlhjYq) {
+ }
+ else while(((2)==3*arg/DtTlhjYq)<=0==---arg) return false;
+ else {
+ int lwbF=arg>=0;
+ while(lwbF<57) {
+ lwbF=lwbF+--(-arg+lwbF)>=-2>=arg!=arg;
+ arg=----0+0;
+ while(49) break;
+ if(true) DtTlhjYq=-----arg;
+ DtTlhjYq=0!=-----(0);
+ }
+ }
+ arg=---arg!=DtTlhjYq/--0;
+ DtTlhjYq=-DtTlhjYq/(--arg<arg);
+ if(32) {
+ int q=-DtTlhjYq;
+ while(q<arg*(0*q)/0) {
+ q=q+DtTlhjYq;
+ DtTlhjYq=arg;
+ }
+ }
+ else if(--arg) if(15) break;
+ else arg=0==arg<=-55>DtTlhjYq;
+ else {
+ }
+ DtTlhjYq=-0;
+ {
+ while((0)<-arg) DtTlhjYq=-DtTlhjYq;
+ }
+ if(30/--(arg)>=0) DtTlhjYq=-0>=arg;
+ else {
+ int Y2wcIQJ=--DtTlhjYq*---11;
+ if(arg==arg) {
+ Y2wcIQJ=-----Y2wcIQJ;
+ Y2wcIQJ=Y2wcIQJ<7;
+ if(DtTlhjYq) arg=arg;
+ else break;
+ while(-arg!=-31==(false<=-15>=-DtTlhjYq+--6*-----Y2wcIQJ<=---arg!=-Y2wcIQJ<=---0<-arg*--Y2wcIQJ)) DtTlhjYq=arg!=3;
+ }
+ continue;
+ }
+ arg=DtTlhjYq;
+ arg=--3/(---arg)>=80>=-DtTlhjYq<=DtTlhjYq;
+ arg=51+---(-true)!=arg!=----1==-DtTlhjYq;
+ if(11<=arg) if((arg)+79<32+(-arg)) continue;
+ else if(true==DtTlhjYq) break;
+ break;
+ }
+}
+if(arg!=62==arg) {
+ while(---arg) {
+ {
+ int bhmDacPM=arg/arg;
+ while(bhmDacPM<arg) {
+ bhmDacPM=bhmDacPM+80*((-bhmDacPM)/---bhmDacPM>1)<bhmDacPM-2!=7;
+ arg=-0;
+ arg=bhmDacPM>=-arg+-arg;
+ break;
+ }
+ }
+ int arg=true;
+ arg=arg<=arg;
+ {
+ int KlC=30;
+ while(KlC<-2) {
+ KlC=KlC+-1!=25<=-KlC>KlC>----0;
+ int Ij=arg>=-arg<=---10;
+ {
+ int LAbgurVa2R=-arg!=Ij;
+ while(LAbgurVa2R<-true) {
+ LAbgurVa2R=LAbgurVa2R+Ij;
+ KlC=-LAbgurVa2R;
+ Ij=42*(-Ij>true);
+ while(Ij) arg=KlC;
+ KlC=-----7>-(--KlC<--7)==-LAbgurVa2R;
+ }
+ }
+ int a=Ij<=-KlC;
+ if(Ij) {
+ int r1W=a!=-false;
+ }
+ else {
+ arg=56>=Ij;
+ while(54) break;
+ arg=8>=-(a<--Ij)/a>=-a-KlC;
+ }
+ int Yt_T=KlC;
+ arg=KlC;
+ }
+ }
+ if(----false/--arg/12+-----0) if(-9+-arg) while(arg!=(arg)) arg=--1>=-71-arg!=---(--arg*11+102)/--arg;
+ else if(----arg) continue;
+ if(-arg-arg) {
+ arg=-(-arg)*--(-3)<(-7==-arg----arg);
+ {
+ int arg=arg;
+ while(arg<-0>arg==-arg-(-arg>1+11)) {
+ arg=arg+6-arg!=--0>---30>=---arg<=arg/(3)+---arg;
+ }
+ }
+ arg=-arg+arg;
+ arg=--47+-arg<1>arg*(-arg==-arg--0>3);
+ arg=(----arg)!=arg+--59;
+ arg=--4+arg==-(arg)-arg;
+ }
+ else {
+ while(--true-----true*-25+-18>arg>=-----(-arg)---13) {
+ arg=-0+--true<false;
+ continue;
+ }
+ arg=true<=arg;
+ break;
+ }
+ arg=arg*22<--(0+-arg);
+ {
+ continue;
+ }
+ }
+ arg=1*---((3<=true)==---2);
+ arg=7+-(-1);
+ return arg+--true;
+}
+else {
+ int o3ypEsewxM=-(---arg);
+ int vzXB=(-(---11<=--o3ypEsewxM>=--0))<=-0/-arg;
+ int L=-arg;
+ int B4mHcrf=---vzXB;
+ int oZWGdzZl=-o3ypEsewxM;
+ int K6tQqa1=11;
+ while(0!=--L) {
+ int sdZfTM=vzXB*true+K6tQqa1;
+ while(sdZfTM<false) {
+ sdZfTM=sdZfTM+arg;
+ sdZfTM=oZWGdzZl;
+ K6tQqa1=-K6tQqa1;
+ arg=---K6tQqa1;
+ while((3+-K6tQqa1)<oZWGdzZl) {
+ if(-----(-1)) sdZfTM=-false;
+ {}
+ }
+ K6tQqa1=vzXB!=-(B4mHcrf+-7>false);
+ sdZfTM=vzXB;
+ }
+ }
+ L=false;
+ while(31) B4mHcrf=arg;
+ oZWGdzZl=-0<--oZWGdzZl<=--2;
+ if(K6tQqa1>=20<=46) {
+ if(-1+-L) if(oZWGdzZl) {
+ int wmhv=-----0;
+ }
+ int arg=0;
+ o3ypEsewxM=---(oZWGdzZl);
+ return -oZWGdzZl/0;
+ }
+ else if(arg) {
+ int yKk5=B4mHcrf>=4>K6tQqa1<-B4mHcrf/o3ypEsewxM;
+ return -K6tQqa1;
+ }
+ else {
+ if(---0) {
+ {}
+ {}
+ }
+ return o3ypEsewxM;
+ }
+}
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ assertEquals("Stop[ return 9; return 0; return 0; ]", stop.toString());
}
+
@Test
- public void testLinkedList0() {
+ public void testPrimes() {
Parser parser = new Parser(
"""
-struct LLI { LLI? next; int i; };
-LLI? head = null;
-while( arg ) {
- LLI x = new LLI;
- x.next = head;
- x.i = arg;
- head = x;
- arg = arg-1;
+if( arg < 2 ) return 0;
+int primeCount = 1;
+int prime = 3;
+while( prime <= arg ) {
+ int isPrime = 1;
+ // Check for even case, so the next loop need only check odds
+ if( (prime/2)*2 == prime )
+ continue;
+ // Check odds up to sqrt of prime
+ int j = 3;
+ while( j*j <= prime ) {
+ if( (prime/j)*j == prime ) {
+ isPrime = 0;
+ break;
+ }
+ j = j + 2;
+ }
+ if( isPrime )
+ primeCount = primeCount + 1;
+ prime = prime + 2;
}
-return head.next.i;
+return primeCount;
""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
+ StopNode stop = parser.parse().iterate();
+ assertEquals("Stop[ return 0; return Phi(Loop21,1,Phi(Region88,Phi_primeCount,Phi(Region82,(Phi_primeCount+1),Phi_primeCount))); ]", stop.toString());
+ assertEquals(0L, Evaluator.evaluate(stop, 1)); // No primes 1 or below
+ assertEquals(1L, Evaluator.evaluate(stop, 2)); // 2
+ assertEquals(2L, Evaluator.evaluate(stop, 3)); // 2, 3
+ assertEquals(2L, Evaluator.evaluate(stop, 4)); // 2, 3
+ assertEquals(3L, Evaluator.evaluate(stop, 5)); // 2, 3, 5
+ assertEquals(4L, Evaluator.evaluate(stop, 10)); // 2, 3, 5, 7
}
@Test
- public void testLinkedList1() {
+ public void testAntiDeps1() {
Parser parser = new Parser(
"""
-struct LLI { LLI? next; int i; };
-LLI? head = null;
-while( arg ) {
- LLI x = new LLI;
- x.next = head;
- x.i = arg;
- head = x;
- arg = arg-1;
-}
-if( !head ) return 0;
-LLI? next = head.next;
-if( next==null ) return 1;
-return next.i;
+struct S { int f; };
+S v=new S;
+v.f = 2;
+int i=new S.f;
+i=v.f;
+if (arg) v.f=1;
+return i;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 0; return 1; return .i; ]", stop.toString());
- assertEquals(2L, Evaluator.evaluate(stop, 3));
+ assertEquals("return 2;", stop.toString());
+ assertEquals(2L, Evaluator.evaluate(stop, 0));
+ assertEquals(2L, Evaluator.evaluate(stop, 1));
}
@Test
- public void testCoRecur() {
+ public void testAntiDeps2() {
Parser parser = new Parser(
"""
-struct int0 { int i; flt0? f; };
-struct flt0 { flt f; int0? i; };
-int0 i0 = new int0;
-i0.i = 17;
-flt0 f0 = new flt0;
-f0.f = 3.14;
-i0.f = f0;
-f0.i = i0;
-return f0.i.f.i.i;
+struct S { int f; };
+S v = new S;
+v.f = arg;
+S t = new S;
+int i = 0;
+if (arg) {
+ if (arg+1) v = t;
+ i = v.f;
+} else {
+ v.f = 2;
+}
+return i;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 17;", stop.toString());
+ assertEquals("return Phi(Region48,.f,0);", stop.toString());
}
@Test
- public void testNullRef0() {
+ public void testAntiDeps3() {
Parser parser = new Parser(
"""
-struct N { N? next; int i; };
-N n = new N;
-return n.next;
+struct S { int f; };
+S v0 = new S;
+S? v1;
+if (arg) v1 = new S;
+if (v1) {
+ v0.f = v1.f;
+} else {
+ v0.f = 2;
+}
+return v0;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return null;", stop.toString());
+ assertEquals("return S;", stop.toString());
}
+
@Test
- public void testNullRef1() {
+ public void testAntiDeps4() {
Parser parser = new Parser(
"""
-struct M { int m; };
-struct N { M next; int i; };
-N n = new N { next = new M; };
-return n.next;
+struct S { int f; };
+S v = new S;
+v.f = arg;
+S t = new S;
+int i = v.f;
+if (arg+1) arg= 0;
+while (arg) v.f = 2;
+return i;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return M;", stop.toString());
+ assertEquals("return arg;", stop.toString());
}
@Test
- public void testNullRef2() {
+ public void testAntiDeps5() {
Parser parser = new Parser(
"""
-struct M { int m; };
-struct N { M next; int i; };
-N n = new N { next = null; }
-return n.next;
+struct S { int f; };
+S v = new S;
+while(1) {
+ while(arg+1) { arg=arg-1; }
+ if (arg) break;
+ v.f = 2;
+}
+return v;
""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Type null is not of declared type *M",e.getMessage()); }
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return S;", stop.toString());
}
@Test
- public void testNullRef3() {
+ public void testAntiDeps6() {
Parser parser = new Parser(
"""
-struct N { N? next; int i; };
-N n = new N;
-n.i = 3.14;
-return n.i;
+struct s { int v; };
+s ptr=new s;
+while( -arg )
+ ptr = new s;
+while(1)
+ arg = arg+ptr.v;
""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Cannot store 3.14 into field int i",e.getMessage()); }
+ StopNode stop = parser.parse().iterate();
+ assertEquals("Stop[ return 0; return 0; ]", stop.toString());
}
@Test
- public void testNullRef4() {
- Parser parser = new Parser("-null-5/null-5");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Expected an identifier, found 'null'",e.getMessage()); }
+ public void testAntiDeps7() {
+ Parser parser = new Parser(
+"""
+struct S { int f; };
+S v = new S;
+S t = new S;
+int i = v.f;
+while (arg) {
+ v.f = arg;
+ arg = arg-1;
+}
+return i;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 0;", stop.toString());
}
- @Test public void testNullRef5() {
- Parser parser = new Parser("return null+42;");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
+ @Test
+ public void testAntiDeps8() {
+ Parser parser = new Parser(
+"""
+struct S { int f; };
+S v = new S;
+S t = new S;
+while(arg) {
+ arg=arg-1;
+ int f = v.f;
+ v.f = 2;
+ if (arg) arg = f;
+ else v.f = 3;
+}
+return arg;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Loop23,arg,Phi(Region50,.f,0));", stop.toString());
}
@Test
- public void testEmpty() {
+ public void testAntiDeps9() {
Parser parser = new Parser(
"""
-struct S{};
+struct S { int f; };
+S v = new S;
+S t = new S;
+if (arg) {
+ v.f=2;
+ int i=t.f;
+ v.f=i;
+}
+return v;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ assertEquals("return S;", stop.toString());
}
@Test
- public void testForwardRef0() {
+ public void testExample2() {
Parser parser = new Parser(
"""
-struct S1 { S2 s; };
-return new S2;
-""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Unknown struct type 'S2'",e.getMessage()); }
+struct S { int f; };
+S v = new S;
+int i = arg;
+while (arg > 0) {
+ int j = i/3;
+ if (arg == 5)
+ v.f = j;
+ arg = arg - 1;
+}
+return v;
+ """);
+ StopNode stop = parser.parse().iterate();
+ //assertEquals("return new S;", stop.toString());
}
@Test
- public void testForwardRef1() {
+ public void testScheduleUse() {
Parser parser = new Parser(
"""
-struct S1 { S2? s; };
-struct S2 { int x; };
-return new S1.s=new S2;
+int v0=0;
+while(0>=0) {
+ u1 v1=0;
+ v1=v0;
+ if(v1*0)
+ v0=-v1;
+}
+return 0;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return S2;", stop.toString());
+ assertEquals("return 0;", stop.toString());
}
@Test
- public void testcheckNull() {
+ public void testLoopCarriedDep() {
Parser parser = new Parser(
"""
-struct I {int i;};
-struct P { I? pi; };
-P p1 = new P;
-P p2 = new P;
-p2.pi = new I;
-p2.pi.i = 2;
-if (arg) p1 = new P;
-return p1.pi.i + 1;
+u32 v0=0;
+{
+ int v1=0;
+ while(v1) {
+ v1=1>>>v0!=0;
+ v0=v1/0;
+ }
+}
""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 0;", stop.toString());
}
}
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java
new file mode 100644
index 00000000..f4323fe0
--- /dev/null
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java
@@ -0,0 +1,83 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.evaluator.Evaluator;
+import com.seaofnodes.simple.node.StopNode;
+import org.junit.Ignore;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+public class Chapter14FloatTest {
+ @Test public void testSubZeroFloat() {
+ var stop = new Parser("flt x = arg; return 0-x;").parse().iterate();
+ // Compare strings so +0.0 and -0.0 remain distinct.
+ assertEquals("0.0", Evaluator.evaluate(stop,0).toString());
+ assertEquals("-1.0", Evaluator.evaluate(stop,1).toString());
+ }
+
+ @Test public void testDeadNumericReturns() {
+ for( String[] test : new String[][] {
+ {"if(1) return 7; else return 2.5;", "7"},
+ {"if(0) return 7; else return 2.5;", "2.5"},
+ {"return 7; return 2.5;", "7"},
+ {"return 2.5; return 7;", "2.5"}
+ } ) {
+ String src = test[0];
+ var stop = new Parser(src).parse().iterate();
+ assertEquals(src,test[1],com.seaofnodes.simple.evaluator.Evaluator.evaluate(stop,0).toString());
+ }
+ }
+
+
+ @Test
+ public void testJig() {
+ Parser parser = new Parser(
+"""
+return 3.14;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 3.14;", stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test
+ public void testFloat() {
+ Parser parser = new Parser(
+"""
+return 3.14;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 3.14;", stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test
+ public void testSquareRoot() {
+ Parser parser = new Parser(
+"""
+flt guess = arg;
+while( 1 ) {
+ flt next = (arg/guess + guess)/2;
+ if( next == guess ) break;
+ guess = next;
+}
+return guess;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Loop10,(flt)arg,(((ToFloat/Phi_guess)+Phi_guess)/2.0));", stop.toString());
+ assertEquals(3.0, Evaluator.evaluate(stop, 9));
+ assertEquals(1.414213562373095, Evaluator.evaluate(stop, 2));
+ }
+
+ @Test
+ public void testFPOps() {
+ Parser parser = new Parser(
+"""
+flt x = arg;
+return x+1==x;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return ((flt)arg==(ToFloat+1.0));", stop.toString());
+ assertEquals(0L, Evaluator.evaluate(stop, 1));
+ }
+}
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java
index 76fdb31e..0f5a5a16 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java
@@ -9,9 +9,9 @@ import static org.junit.Assert.fail;
public class Chapter16Test {
@Test
- public void testStructFinal3() {
- for( String fields : new String[] { "int[] x;", "int[] !x;" } ) {
- String src = "struct Point { "+fields+" }; Point p = new Point; p.x++; return p;";
+ public void testRequiredReference() {
+ for( String fields : new String[] { "int[] x;" } ) {
+ String src = "struct Point { "+fields+" }; Point p = new Point; p.x=p.x+1; return p;";
try { new Parser(src).parse().iterate(); fail(fields); }
catch( Exception e ) { assertEquals("'Point' is not fully initialized, field 'x' needs to be set in a constructor",e.getMessage()); }
}
@@ -52,31 +52,9 @@ return x+y;
assertEquals(5L, Evaluator.evaluate(stop, 0));
}
- @Test
- public void testFinal0() {
- Parser parser = new Parser(
-"""
-int !x=2;
-x=3;
-return x;
-""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
- }
- @Test
- public void testFinal1() {
- Parser parser = new Parser(
-"""
-int !x=2, y=3;
-if( arg ) { int x = y; x = x*x; y=x; } // Shadow final x
-return y;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region19,9,3);", stop.toString());
- assertEquals(3L, Evaluator.evaluate(stop, 0));
- assertEquals(9L, Evaluator.evaluate(stop, 1));
- }
+
+
@Test
public void testConstruct0() {
@@ -90,17 +68,7 @@ return z.x;
assertEquals(3L, Evaluator.evaluate(stop, 0));
}
- @Test
- public void testConstruct1() {
- Parser parser = new Parser("""
-struct X { int !x; };
-X z = new X { x=3; };
-return z.x;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3;", stop.toString());
- assertEquals(3L, Evaluator.evaluate(stop, 0));
- }
+
@Test
public void testConstruct2() {
@@ -115,17 +83,7 @@ return z.x;
}
- @Test
- public void testStructFinal() {
- Parser parser = new Parser("""
-struct Point { int !x, !y; };
-Point p = new Point { x=3; y=4; };
-return p;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Point;", stop.toString());
- assertEquals("Obj<Point>{x=3,y=4}", Evaluator.evaluate(stop, 0).toString());
- }
+
// Same as the Chapter13 test with the same name, but using the new
// constructor syntax
@@ -187,7 +145,7 @@ return next.i;
Parser parser = new Parser(
"""
struct Square {
- flt !side = arg;
+ flt side = arg;
// Newtons approximation to the square root, computed in a constructor.
// The actual allocation will copy in this result as the initial
// value for 'diag'.