From d32f97238c903765dec4e9a9f3cbfa5fd12cded6 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 10 Aug 2026 11:11:18 -0700 Subject: Chapter 25: Modules, Separate Compilation, and SSA Construction Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: Symious <14933944+symious@users.noreply.github.com> Co-authored-by: Jun Kawasaki Co-authored-by: Claude Opus 5 --- .../java/com/seaofnodes/simple/Chapter13Test.java | 92 +++++++++++++--------- 1 file changed, 55 insertions(+), 37 deletions(-) (limited to 'src/test/java/com/seaofnodes/simple/Chapter13Test.java') diff --git a/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/src/test/java/com/seaofnodes/simple/Chapter13Test.java index 62bc1654..7cfa5817 100644 --- a/src/test/java/com/seaofnodes/simple/Chapter13Test.java +++ b/src/test/java/com/seaofnodes/simple/Chapter13Test.java @@ -1,6 +1,5 @@ package com.seaofnodes.simple; import com.seaofnodes.simple.codegen.CodeGen; -import com.seaofnodes.simple.print.IRPrinter; import org.junit.Test; @@ -45,14 +44,31 @@ return primeCount; assertEquals("4", Eval2.eval(code, 10)); // 2, 3, 5, 7 } + @Test + public void testClassField() { + CodeGen code = new CodeGen( +""" +if( arg < 2 ) return 0; +int primeCount = 1; +while( arg-- ) + primeCount++; +return primeCount; +"""); + code.driver(CodeGen.Phase.LocalSched); + assertEquals("0", Eval2.eval(code, 1)); // No primes 1 or below + assertEquals("3", Eval2.eval(code, 2)); // +} + + @Test public void testAntiDeps1() { CodeGen code = new CodeGen( """ -struct S { int f; }; -!S !v=new S; +struct _S { int f; }; +!_S !v=new _S; v.f = 2; -int i=new S.f; +_S q=new _S; +int i=q.f; i=v.f; if (arg) v.f=1; return i; @@ -67,10 +83,10 @@ return i; public void testAntiDeps2() { CodeGen code = new CodeGen( """ -struct S { int f; }; -!S !v = new S; +struct _S { int f; }; +!_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; @@ -88,19 +104,19 @@ return i; public void testAntiDeps3() { CodeGen code = new CodeGen( """ -struct S { int f; }; -!S !v0 = new S; -!S? !v1; -if (arg) v1 = new S; +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; +return v0.f; """); code.parse().opto(); - assertEquals("return S;", code.print()); + assertEquals("return Phi(Region,.f,2);", code.print()); } @@ -108,10 +124,10 @@ return v0; public void testAntiDeps4() { CodeGen code = new CodeGen( """ -struct S { int f; }; -!S !v = new S; +struct _S { int f; }; +!_S !v = new _S; v.f = arg; -S t = new S; +_S t = new _S; int i = v.f; if (arg+1) arg= 0; while (arg) v.f = 2; @@ -125,27 +141,29 @@ return i; public void testAntiDeps5() { CodeGen code = new CodeGen( """ -struct S { int f; }; -!S !v = new S; +struct _S { int f; }; +!_S !v = new _S; while(1) { while(arg+1) { arg=arg-1; } if (arg) break; v.f = 2; } -return v; +return v.f; """); code.parse().opto(); - assertEquals("return S;", code.print()); + assertEquals("return Phi(Loop,0,2);", code.print()); + for( int arg : new int[]{-1,0,1,3,100} ) + assertEquals("0",Eval2.eval(code,arg)); } @Test public void testAntiDeps6() { CodeGen code = new CodeGen( """ -struct s { int v; }; -!s !ptr=new s; +struct _S { int v; }; +!_S !ptr=new _S; while( -arg ) - ptr = new s; + ptr = new _S; while(1) arg = arg+ptr.v; """); @@ -157,9 +175,9 @@ while(1) public void testAntiDeps7() { CodeGen code = new CodeGen( """ -struct S { int f; }; -!S !v = new S; -S t = new S; +struct _S { int f; }; +!_S !v = new _S; +_S t = new _S; int i = v.f; while (arg) { v.f = arg; @@ -175,9 +193,9 @@ return i; public void testAntiDeps8() { CodeGen code = new CodeGen( """ -struct S { int f; }; -!S !v = new S; -S t = new S; +struct _S { int f; }; +!_S !v = new _S; +_S t = new _S; while(arg) { arg=arg-1; int f = v.f; @@ -195,26 +213,26 @@ return arg; public void testAntiDeps9() { CodeGen code = new CodeGen( """ -struct S { int f; }; -!S !v = new S; -S t = new 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; +return v.f; """); code.parse().opto(); - assertEquals("return S;", code.print()); + assertEquals("return 0;", code.print()); } @Test public void testExample2() { CodeGen code = new CodeGen( """ -struct S { int f; }; -!S !v = new S; +struct _S { int f; }; +!_S !v = new _S; int i = arg; while (arg > 0) { int j = i/3; @@ -225,7 +243,7 @@ while (arg > 0) { return v; """); code.parse().opto(); - //assertEquals("return new S;", code.print()); + //assertEquals("return new _S;", code.print()); } @Test -- cgit v1.2.3