diff options
| author | Cliff Click <cliffc@acm.org> | 2026-08-10 11:11:18 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-10-08 16:52:27 -0700 |
| commit | d32f97238c903765dec4e9a9f3cbfa5fd12cded6 (patch) | |
| tree | a5ac731801ef7066bab6359c4e85c2bdd20082a6 /src/test/java/com/seaofnodes/simple/Chapter16Test.java | |
| parent | b5ebb2032aa04cf4506d533127d373d4cf5a25c6 (diff) | |
| download | Simple-linear.tar.gz Simple-linear.zip | |
Chapter 25: Modules, Separate Compilation, and SSA Constructionlinear
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 <jun784@gmail.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'src/test/java/com/seaofnodes/simple/Chapter16Test.java')
| -rw-r--r-- | src/test/java/com/seaofnodes/simple/Chapter16Test.java | 78 |
1 files changed, 27 insertions, 51 deletions
diff --git a/src/test/java/com/seaofnodes/simple/Chapter16Test.java b/src/test/java/com/seaofnodes/simple/Chapter16Test.java index f73cbf57..91e5cd38 100644 --- a/src/test/java/com/seaofnodes/simple/Chapter16Test.java +++ b/src/test/java/com/seaofnodes/simple/Chapter16Test.java @@ -48,8 +48,8 @@ return x+y; @Test public void testConstruct0() { CodeGen code = new CodeGen(""" -struct X { int x=3; }; -X z = new X; +struct _X { int x=3; }; +_X z = new _X; return z.x; """); code.parse().opto(); @@ -60,8 +60,8 @@ return z.x; @Test public void testConstruct2() { CodeGen code = new CodeGen(""" -struct X { int x=3; }; -X z = new X { x = 4; }; +struct _X { int x=3; new _X = { int xx -> x=xx; }; }; +_X z = new _X(4); return z.x; """); code.parse().opto(); @@ -74,19 +74,19 @@ return z.x; public void testLinkedList1() { CodeGen code = new CodeGen( """ -struct LLI { !LLI? !next; int i; }; -!LLI? !head = null; +struct _LLI { !_LLI? !next; int i; new _LLI = { !_LLI? !n, int ii -> next=n; i=ii; }; }; +!_LLI? !head = null; while( arg ) { - head = new LLI { next=head; i=arg; }; + head = new _LLI(head,arg); arg = arg-1; } if( !head ) return 0; -LLI? next = head.next; +_LLI? next = head.next; if( !next ) return 1; return next.i; """); code.parse().opto(); - assertEquals("return Phi(Region,0,1,.i);", code.print()); + assertEquals("return Phi(Region,1,.i,0);", code.print()); assertEquals("0", Eval2.eval(code, 0)); assertEquals("1", Eval2.eval(code, 1)); assertEquals("2", Eval2.eval(code, 3)); @@ -96,28 +96,27 @@ return next.i; public void testLinkedList2() { CodeGen code = new CodeGen( """ -struct LLI { !LLI? !next; int i; }; -!LLI? !head = null; +struct _LLI { !_LLI? !next; int i; new _LLI = { !_LLI? !n, int a -> + next=n; + int !tmp=a; + while( a > 10 ) { + tmp = tmp + a; + a = a - 1; + } + i=tmp; +}; }; +!_LLI? !head = null; while( arg ) { - head = new LLI { - next=head; - // Any old code in the constructor - int !tmp=arg; - while( arg > 10 ) { - tmp = tmp + arg; - arg = arg - 1; - } - i=tmp; - }; + head = new _LLI(head,arg); arg = arg-1; } if( !head ) return 0; -LLI? next = head.next; +_LLI? next = head.next; if( !next ) return 1; return next.i; """); code.parse().opto(); - assertEquals("return Phi(Region,0,1,.i);", code.print()); + assertEquals("return Phi(Region,1,.i,0);", code.print()); assertEquals("0", Eval2.eval(code, 0)); assertEquals("1", Eval2.eval(code, 1)); assertEquals("2", Eval2.eval(code, 11)); @@ -127,7 +126,7 @@ return next.i; public void testSquare() { CodeGen code = new CodeGen( """ -struct Square { +struct _Square { flt !side = arg; // Newtons approximation to the square root, computed in a constructor. // The actual allocation will copy in this result as the initial @@ -139,34 +138,11 @@ struct Square { diag = next; } }; -return new Square; +return new _Square; """); code.parse().opto(); - assertEquals("return Square;", code.print()); - assertEquals("Square{side=3.0,diag=1.7320508075688772}", Eval2.eval(code, 3)); - assertEquals("Square{side=4.0,diag=2.0}", Eval2.eval(code, 4)); - } - static final String CONSTRUCTOR_MEMORY = """ - struct S { int x; int y; }; - struct T { int z=arg+40; }; - !T !t = new T; - !S !a = new S { x=11; y=7; }; - !S !b = new S { x=22; y=9; }; - !S !p=a; - if (arg) p=b; - int before=p.x; - !S !c = new S { - x=p.x+1; - { int i=0; while (i<2) { p.y=p.y+1; i=i+1; } } - y=p.y; - }; - p.x=33; - return before*10000+c.x*100+c.y+t.z; - """; - - @Test public void testConstructorMemory() { - var code = new CodeGen(CONSTRUCTOR_MEMORY).parse().opto(); - assertEquals("111249",Eval2.eval(code,0)); - assertEquals("222352",Eval2.eval(code,1)); + assertEquals("return Test._Square;", code.print()); + assertEquals("Test._Square{side=3.0,diag=1.7320508075688772}", Eval2.eval(code, 3)); + assertEquals("Test._Square{side=4.0,diag=2.0}", Eval2.eval(code, 4)); } } |
