From 68cd11f31dd4ba7f089d1a15de5a36ec67043da6 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Wed, 13 Nov 2024 13:41:18 -0800 Subject: Final? pass of finals Add a least-upper-bound to set Phis to their respective Top while preserving final fields. Requires a ReadOnly node to "cast-away-writable". Series of isFinal/makeRO in all Types. Changes tests to match r/w intent. --- .../java/com/seaofnodes/simple/BrainFuckTest.java | 4 +- .../java/com/seaofnodes/simple/Chapter10Test.java | 32 ++--- .../java/com/seaofnodes/simple/Chapter11Test.java | 22 +-- .../java/com/seaofnodes/simple/Chapter13Test.java | 18 +-- .../java/com/seaofnodes/simple/Chapter14Test.java | 2 +- .../java/com/seaofnodes/simple/Chapter15Test.java | 36 +++-- .../java/com/seaofnodes/simple/Chapter16Test.java | 12 +- .../java/com/seaofnodes/simple/Chapter17Test.java | 160 ++++++++++++++++++--- .../java/com/seaofnodes/simple/SchedulerTest.java | 6 +- .../test/java/com/seaofnodes/simple/TypeTest.java | 4 +- .../com/seaofnodes/simple/evaluator/Evaluator.java | 1 + 11 files changed, 209 insertions(+), 88 deletions(-) (limited to 'chapter17/src/test/java/com/seaofnodes') diff --git a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java index 4833483b..88d79215 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/BrainFuckTest.java @@ -15,7 +15,7 @@ public class BrainFuckTest { @Test public void testBrainfuck() { var program = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.".getBytes(StandardCharsets.UTF_8); - var encoded = new StringBuilder("u8[] program = new u8[").append(program.length).append("];"); + var encoded = new StringBuilder("u8[] !program = new u8[").append(program.length).append("];"); for (int i = 0; i < program.length; i++) { int value = program[i] & 0xFF; encoded.append("program[").append(i).append("] = ").append(value).append(";"); @@ -25,7 +25,7 @@ public class BrainFuckTest { encoded + """ var d = 0; u8[] !output = new u8[0]; -val data = new u8[100]; +u8[] !data = new u8[100]; for( int pc = 0; pc < program#; pc++ ) { var command = program[pc]; diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter10Test.java index 44d71b1e..d41c83d8 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter10Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter10Test.java @@ -44,7 +44,7 @@ struct Foo { int x; }; Foo? foo = null; -Bar bar = new Bar; +Bar !bar = new Bar; bar.a = 1; bar.a = 2; return bar.a; @@ -57,7 +57,7 @@ return bar.a; public void testExample() { Parser parser = new Parser(""" struct Vector2D { int x; int y; }; -Vector2D v = new Vector2D; +Vector2D !v = new Vector2D; v.x = 1; if (arg) v.y = 2; @@ -96,7 +96,7 @@ arg=0+new s0.0; public void testLoop() { Parser parser = new Parser(""" struct Bar { int a; }; -Bar bar = new Bar; +Bar !bar = new Bar; while (arg) { bar.a = bar.a + 2; arg = arg + 1; @@ -111,7 +111,7 @@ return bar.a; public void testIf() { Parser parser = new Parser(""" struct Bar { int a; }; -Bar bar = new Bar; +Bar !bar = new Bar; if (arg) bar = null; bar.a = 1; return bar.a; @@ -124,7 +124,7 @@ return bar.a; public void testIf2() { Parser parser = new Parser(""" struct Bar { int a; }; -Bar? bar = null; +Bar? !bar = null; if (arg) bar = new Bar; bar.a = 1; return bar.a; @@ -150,7 +150,7 @@ return bar.a; public void testIfOrNull() { Parser parser = new Parser(""" struct Bar { int a; }; -Bar? bar = new Bar; +Bar? !bar = new Bar; if (arg) bar = null; if( bar ) bar.a = 1; return bar; @@ -164,7 +164,7 @@ return bar; Parser parser = new Parser( """ struct Bar { int a; }; -Bar? bar = new Bar; +Bar? !bar = new Bar; if (arg) bar = null; int rez = 3; if( !bar ) rez=4; @@ -179,7 +179,7 @@ return rez; public void testWhileWithNullInside() { Parser parser = new Parser(""" struct s0 {int v0;}; -s0? v0 = new s0; +s0? !v0 = new s0; int ret = 0; while(arg) { ret = v0.v0; @@ -216,7 +216,7 @@ struct Iter { int x; int len; }; -Iter i = new Iter; +Iter !i = new Iter; i.len = arg; int sum=0; while( i.x < i.len ) { @@ -234,9 +234,9 @@ return sum; public void test1() { Parser parser = new Parser(""" struct s0 {int v0;}; -s0 ret = new s0; +s0 !ret = new s0; while(arg) { - s0 v0 = new s0; + s0 !v0 = new s0; v0.v0 = arg; arg = arg-1; if (arg==5) ret=v0; @@ -252,8 +252,8 @@ return ret; public void test2() { Parser parser = new Parser(""" struct s0 {int v0;}; -s0 ret = new s0; -s0 v0 = new s0; +s0 !ret = new s0; +s0 !v0 = new s0; while(arg) { v0.v0 = arg; arg = arg-1; @@ -271,9 +271,9 @@ return ret; public void test3() { Parser parser = new Parser(""" struct s0 {int v0;}; -s0 ret = new s0; +s0 !ret = new s0; while(arg < 10) { - s0 v0 = new s0; + s0 !v0 = new s0; if (arg == 5) ret=v0; arg = arg + 1; } @@ -350,7 +350,7 @@ s0 v1 = v0; return v1; """); StopNode stop = parser.parse().iterate(); - assertEquals("return s0;", stop.toString()); + assertEquals("return (const)s0;", stop.toString()); } diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java index ddcc9e63..79c0622c 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter11Test.java @@ -338,7 +338,7 @@ return primeCount; Parser parser = new Parser( """ struct S { int f; }; -S v=new S; +S !v=new S; v.f = 2; int i=new S.f; i=v.f; @@ -356,9 +356,9 @@ return i; Parser parser = new Parser( """ struct S { int f; }; -S v = new S; +S !v = new S; v.f = arg; -S t = new S; +S !t = new S; int i = 0; if (arg) { if (arg+1) v = t; @@ -377,7 +377,7 @@ return i; Parser parser = new Parser( """ struct S { int f; }; -S v0 = new S; +S !v0 = new S; S? v1; if (arg) v1 = new S; if (v1) { @@ -397,7 +397,7 @@ return v0; Parser parser = new Parser( """ struct S { int f; }; -S v = new S; +S !v = new S; v.f = arg; S t = new S; int i = v.f; @@ -414,7 +414,7 @@ return i; Parser parser = new Parser( """ struct S { int f; }; -S v = new S; +S !v = new S; while(1) { while(arg+1) { arg=arg-1; } if (arg) break; @@ -431,7 +431,7 @@ return v; Parser parser = new Parser( """ struct s { int v; }; -s ptr=new s; +s !ptr=new s; while( -arg ) ptr = new s; while(1) @@ -446,7 +446,7 @@ while(1) Parser parser = new Parser( """ struct S { int f; }; -S v = new S; +S !v = new S; S t = new S; int i = v.f; while (arg) { @@ -464,7 +464,7 @@ return i; Parser parser = new Parser( """ struct S { int f; }; -S v = new S; +S !v = new S; S t = new S; while(arg) { arg=arg-1; @@ -484,7 +484,7 @@ return arg; Parser parser = new Parser( """ struct S { int f; }; -S v = new S; +S !v = new S; S t = new S; if (arg) { v.f=2; @@ -502,7 +502,7 @@ return v; Parser parser = new Parser( """ struct S { int f; }; -S v = new S; +S !v = new S; int i = arg; while (arg > 0) { int j = i/3; diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java index aa659147..46eac241 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter13Test.java @@ -23,9 +23,9 @@ return 3.14; Parser parser = new Parser( """ struct LLI { LLI? next; int i; }; -LLI? head = null; +LLI? !head = null; while( arg ) { - LLI x = new LLI; + LLI !x = new LLI; x.next = head; x.i = arg; head = x; @@ -42,9 +42,9 @@ return head.next.i; Parser parser = new Parser( """ struct LLI { LLI? next; int i; }; -LLI? head = null; +LLI? !head = null; while( arg ) { - LLI x = new LLI; + LLI !x = new LLI; x.next = head; x.i = arg; head = x; @@ -66,9 +66,9 @@ return next.i; """ struct int0 { int i; flt0? f; }; struct flt0 { flt f; int0? i; }; -int0 i0 = new int0; +int0 !i0 = new int0; i0.i = 17; -flt0 f0 = new flt0; +flt0 !f0 = new flt0; f0.f = 3.14; i0.f = f0; f0.i = i0; @@ -100,7 +100,7 @@ N n = new N { next = new M; }; return n.next; """); StopNode stop = parser.parse().iterate(); - assertEquals("return M;", stop.toString()); + assertEquals("return (const)M;", stop.toString()); } @Test @@ -121,7 +121,7 @@ return n.next; Parser parser = new Parser( """ struct N { N? next; int i; }; -N n = new N; +N !n = new N; n.i = 3.14; return n.i; """); @@ -169,7 +169,7 @@ return new S1.s=new S2; """ struct I {int i;}; struct P { I? pi; }; -P p1 = new P; +P !p1 = new P; P p2 = new P; p2.pi = new I; p2.pi.i = 2; diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter14Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter14Test.java index 01cecebb..23c82358 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter14Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter14Test.java @@ -94,7 +94,7 @@ return b; Parser parser = new Parser( """ struct Foo { u1 b; }; -Foo f = new Foo; +Foo !f = new Foo; f.b = 123; return f.b; """); diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java index 04958819..4e2682dc 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter15Test.java @@ -25,7 +25,7 @@ return 3.14; Parser parser = new Parser( """ struct C { C? l; }; -C c = new C; +C !c = new C; c.l = c; return c; """); @@ -39,7 +39,7 @@ return c; Parser parser = new Parser( """ u8[] old = new u8[0]; -u8[] output = new u8[1]; +u8[] !output = new u8[1]; int i = 0; while (i < old#) { output[i] = old[i]; @@ -82,7 +82,7 @@ return is[1]; public void testBasic3() { Parser parser = new Parser( """ -int[] a = new int[2]; +int[] !a = new int[2]; a[0] = 1; a[1] = 2; return a[0]; @@ -97,7 +97,7 @@ return a[0]; Parser parser = new Parser( """ struct A { int i; }; -A?[] a = new A?[2]; +A?[] !a = new A?[2]; return a; """); StopNode stop = parser.parse().iterate(); @@ -111,10 +111,10 @@ return a; """ struct S { int x; flt y; }; // A new S -S s = new S; s.x=99; s.y = 3.14; +S !s = new S; s.x=99; s.y = 3.14; // Double-d array of Ss. Fill in one row. -S?[]?[] iss = new S?[]?[2]; +S?[]?[] !iss = new S?[]?[2]; iss[0] = new S?[7]; iss[0][2] = s; @@ -135,14 +135,13 @@ return rez; assertEquals(1.2 , Evaluator.evaluate(stop, 1)); } - @Ignore @Test public void testBasic6() { Parser parser = new Parser( """ struct S { int x; flt y; }; // A new S -S s = new S; s.x=99; s.y = 3.14; +S !s = new S; s.x=99; s.y = 3.14; // Double-d array of Ss. Fill in one row. S?[]?[] iss = new S?[]?[2]; @@ -156,9 +155,8 @@ if( iss[arg] ) rez = iss[arg][2].y; return rez; """); - StopNode stop = parser.parse().iterate(); - assertEquals("return Phi(Region,Phi(Region,.y,1.2),1.2);", stop.toString()); - assertEquals(3.14, Evaluator.evaluate(stop, 0)); + try { parser.parse().iterate(); fail(); } + catch( Exception e ) { assertEquals("Might be null accessing 'y'",e.getMessage()); } } @Test @@ -167,8 +165,8 @@ return rez; """ // Can we define a forward-reference array? struct Tree { Tree?[]? _kids; }; -Tree root = new Tree; -root._kids = new Tree?[2]; +Tree !root = new Tree; +root._kids = new Tree?[2]; // NO BANG SO ARRAY IS OF IMMUTABLE TREES???? root._kids[0] = new Tree; return root; """); @@ -192,7 +190,7 @@ return 0; public void testRollingSum() { Parser parser = new Parser( """ -int[] ary = new int[arg]; +int[] !ary = new int[arg]; // Fill [0,1,2,3,4,...] int i=0; while( i < ary# ) { @@ -216,7 +214,7 @@ return ary[1] * 1000 + ary[3]; // 1 * 1000 + 6 public void sieveOEratosthenes() { Parser parser = new Parser( """ -int[] ary = new int[arg], primes = new int[arg]; +int[] !ary = new int[arg], !primes = new int[arg]; int nprimes = 0, p=2; // Find primes while p^2 < arg while( p*p < arg ) { @@ -240,7 +238,7 @@ while( p < arg ) { p = p + 1; } // Copy/shrink the result array -int[] rez = new int[nprimes]; +int[] !rez = new int[nprimes]; int j = 0; while( j < nprimes ) { rez[j] = primes[j]; @@ -251,7 +249,7 @@ return rez; StopNode stop = parser.parse().iterate(); assertEquals("return [int];", stop.toString()); Evaluator.Obj obj = (Evaluator.Obj)Evaluator.evaluate(stop, 20); - assertEquals("[int] { # :int; [] :int; }",obj.struct().toString()); + assertEquals("[int] {int #; int ![]; }",obj.struct().toString()); long nprimes = (Long)obj.fields()[0]; long[] primes = new long[]{2,3,5,7,11,13,17,19}; for( int i=0; i{x=3,y=4}", Evaluator.evaluate(stop, 0).toString()); } @@ -127,7 +127,7 @@ p.x++; return p; """); try { parser.parse().iterate(); fail(); } - catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); } + catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); } } @Test @@ -139,7 +139,7 @@ p.x++; return p; """); try { parser.parse().iterate(); fail(); } - catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); } + catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); } } @Test @@ -170,7 +170,7 @@ return p; public void testStructFinal5() { Parser parser = new Parser(""" struct Point { var x=3; var y=4; }; -Point p = new Point; +Point !p = new Point; p.x++; return p; """); @@ -186,7 +186,7 @@ return p; Parser parser = new Parser( """ struct LLI { LLI? next; int i; }; -LLI? head = null; +LLI? !head = null; while( arg ) { head = new LLI { next=head; i=arg; }; arg = arg-1; @@ -208,7 +208,7 @@ return next.i; Parser parser = new Parser( """ struct LLI { LLI? next; int i; }; -LLI? head = null; +LLI? !head = null; while( arg ) { head = new LLI { next=head; diff --git a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java index 3773f87f..7de7d514 100644 --- a/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java +++ b/chapter17/src/test/java/com/seaofnodes/simple/Chapter17Test.java @@ -12,13 +12,24 @@ public class Chapter17Test { @Test public void testJig() { Parser parser = new Parser(""" -return 3.14; +i32 v0=0; +if(1< ((Object[])val(cproj.ctrl()))[cproj._idx]; case ProjNode proj -> ((Object[])val( proj.in(0) ))[ proj._idx]; case ScopeMinNode mem -> null; + case ReadOnlyNode ro -> val(ro.in(1)); default -> throw new AssertionError("Unexpected node " + node); }; } -- cgit v1.2.3