summaryrefslogtreecommitdiff
path: root/src/test/java/com/seaofnodes/simple/Chapter19Test.java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-08-10 11:11:18 -0700
committerCliff Click <cliffc@acm.org>2026-08-10 15:21:17 -0700
commit28209703c72fb7e9955961dbdb84e24a0f871acd (patch)
tree8e4df2c1a5e6811696b454bbc4d7cd1cc30e42e7 /src/test/java/com/seaofnodes/simple/Chapter19Test.java
parente57bdd315a7918a0d130e1c95648ca4a1b4f08ed (diff)
downloadSimple-linear-chapter25.tar.gz
Simple-linear-chapter25.zip
Chapter 25: Moduleslinear-chapter25
Diffstat (limited to 'src/test/java/com/seaofnodes/simple/Chapter19Test.java')
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter19Test.java124
1 files changed, 76 insertions, 48 deletions
diff --git a/src/test/java/com/seaofnodes/simple/Chapter19Test.java b/src/test/java/com/seaofnodes/simple/Chapter19Test.java
index 6d751946..eaf76041 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter19Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter19Test.java
@@ -3,8 +3,6 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen.Phase;
import com.seaofnodes.simple.codegen.CodeGen;
import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -16,18 +14,19 @@ public class Chapter19Test {
return 0;
""");
code.parse().opto().typeCheck();
- assertEquals("return 0;", code._stop.toString());
+ assertEquals("return 0;", code.print());
assertEquals("0", Eval2.eval(code, 2));
}
@Test
- public void testString() throws IOException {
+ public void testString() {
String src =
"""
struct String {
- u8[] cs;
+ u8[~] cs;
int _hashCode;
+ new String = { u8[~] s -> cs=s; };
};
// Compare two Strings
@@ -40,12 +39,6 @@ val equals = { String self, String s ->
return true;
};
-// Return the String hashCode (cached, and never 0)
-val hashCode = { String self ->
- self._hashCode
- ? self._hashCode
- : (self._hashCode = _hashCodeString(self));
-};
val _hashCodeString = { String self ->
int hash=0;
@@ -54,107 +47,141 @@ val _hashCodeString = { String self ->
if( !hash ) hash = 123456789;
return hash;
};
+
+// Return the String hashCode (cached, and never 0)
+val hashCode = { String self ->
+ self._hashCode
+ ? self._hashCode
+ : (self._hashCode = _hashCodeString(self));
+};
+
+hashCode(new String("Hello, World!"));
""";
- CodeGen code = new CodeGen(src).parse().opto().typeCheck().GCM().localSched();
- assertEquals("Stop[ return Phi(Region,1,0,0,1); return Phi(Region,._hashCode,Phi(Region,123456789,Phi(Loop,0,(.[]+((Phi_hash<<5)-Phi_hash))))); ]", code._stop.toString());
- //assertEquals("-4898613127354160978", Eval2.eval(code, 2));
+ CodeGen code = new CodeGen(src).driver(Phase.LocalSched);
+ assertEquals("Stop[ return Phi(Region,123456789,(!=0)Phi(Loop,0,(.[]+((Phi_hash<<5)-Phi_hash)))); return MEM[ 2:___ 3:___ 4:.cs=Parm_s(Test.String.String,*[]u8); 5:._hashCode=0;]; return MEM[ 2:___ 3:___ 4:___ 5:._hashCode=0;]; return Phi(Region,0,1,0,1); return Phi(Region,(!=0)._hashCode,Phi(Region,123456789,(!=0)Phi(Loop,0,(((Phi_hash<<5)-Phi_hash)+.[])))); ]", code.print());
+ assertEquals("4029215624828139541", Eval2.eval(code, 2));
+ }
+
+ @Test
+ public void testTernaryStoreCall() {
+ String src =
+"""
+struct String {
+ u8[~] cs;
+ int _hashCode;
+ new String = { u8[~] s -> cs=s; };
+};
+
+val f = { String self -> 7; };
+
+val hashCode = { String self ->
+ self._hashCode
+ ? 3
+ : (self._hashCode = f(self));
+};
+
+hashCode(new String("Hello"));
+""";
+ CodeGen code = new CodeGen(src).driver(Phase.LocalSched);
+ assertFalse(code.print().contains("return Top"));
+ assertEquals("7", Eval2.eval(code, 2));
}
@Test
public void testBasic0() {
CodeGen code = new CodeGen("return 0;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return 0;", code._stop.toString());
+ assertEquals("return 0;", code.print());
}
@Test
public void testBasic1() {
CodeGen code = new CodeGen("return arg+1;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (inc,arg);", code._stop.toString());
+ assertEquals("return (inc,arg);", code.print());
}
@Test
public void testBasic2() {
CodeGen code = new CodeGen("return -17;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return -17;", code._stop.toString());
+ assertEquals("return -17;", code.print());
}
@Test
public void testBasic3() {
CodeGen code = new CodeGen("return arg==1;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (set==,(cmp,arg));", code._stop.toString());
+ assertEquals("return (set==,(cmp,arg));", code.print());
}
@Test
public void testBasic4() {
CodeGen code = new CodeGen("return arg<<1;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (shli,arg);", code._stop.toString());
+ assertEquals("return (shli,arg);", code.print());
}
@Test
public void testBasic5() {
CodeGen code = new CodeGen("return arg >> 1;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (sari,arg);", code._stop.toString());
+ assertEquals("return (sari,arg);", code.print());
}
@Test
public void testBasic6() {
CodeGen code = new CodeGen("return arg >>> 1;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (shri,arg);", code._stop.toString());
+ assertEquals("return (shri,arg);", code.print());
}
@Test
public void testBasic7() {
CodeGen code = new CodeGen("return arg / 2;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (div,arg,2);", code._stop.toString());
+ assertEquals("return (div,arg,2);", code.print());
}
@Test
public void testBasic8() {
CodeGen code = new CodeGen("return arg * 6;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (muli,arg);", code._stop.toString());
+ assertEquals("return (muli,arg);", code.print());
}
@Test
public void testBasic9() {
CodeGen code = new CodeGen("return arg & 2;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (andi,arg);", code._stop.toString());
+ assertEquals("return (andi,arg);", code.print());
}
@Test
public void testBasic10() {
CodeGen code = new CodeGen("return arg | 2;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (ori,arg);", code._stop.toString());
+ assertEquals("return (ori,arg);", code.print());
}
@Test
public void testBasic11() {
CodeGen code = new CodeGen("return arg ^ 2;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (xori,arg);", code._stop.toString());
+ assertEquals("return (xori,arg);", code.print());
}
@Test
public void testBasic12() {
CodeGen code = new CodeGen("return arg + 2.0;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (addf,(cvtf,arg),2.0f);", code._stop.toString());
+ assertEquals("return (addf,(cvtf,arg),2.0f);", code.print());
}
@Test
public void testBasic13() {
CodeGen code = new CodeGen("return arg - 2.0;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (subf,(cvtf,arg),2.0f);", code._stop.toString());
+ assertEquals("return (subf,(cvtf,arg),2.0f);", code.print());
}
@Test
public void testBasic14() {
CodeGen code = new CodeGen("return arg * 2.0;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (mulf,(cvtf,arg),2.0f);", code._stop.toString());
+ assertEquals("return (mulf,(cvtf,arg),2.0f);", code.print());
}
@Test
public void testBasic15() {
CodeGen code = new CodeGen("return arg / 2.0;").driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (mulf,(cvtf,arg),0.5f);", code._stop.toString());
+ assertEquals("return (mulf,(cvtf,arg),0.5f);", code.print());
}
@Test
@@ -164,7 +191,7 @@ val _hashCodeString = { String self ->
int arg1 = arg + 1;
return arg1 / arg;""");
code.driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (div,(inc,arg),arg);", code._stop.toString());
+ assertEquals("return (div,(inc,arg),arg);", code.print());
}
@Test
@@ -175,7 +202,7 @@ int arg1 = arg + 1;
return arg1 * arg;
""");
code.driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (mul,(inc,arg),arg);", code._stop.toString());
+ assertEquals("return (mul,(inc,arg),arg);", code.print());
}
@Test
@@ -185,7 +212,7 @@ int a = arg;
return a + 2.0;
"""
).driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return (addf,(cvtf,arg),2.0f);", code._stop.toString());
+ assertEquals("return (addf,(cvtf,arg),2.0f);", code.print());
}
@Test
@@ -234,10 +261,10 @@ return sum;""");
public void testAlloc1() {
CodeGen code = new CodeGen(
"""
-struct S { int a; S? c; };
-return new S;""");
+struct _S { int a; _S? c; };
+return new _S;""");
code.driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return S;", code.print());
+ assertEquals("return Test._S;", code.print());
}
@Test
@@ -325,7 +352,7 @@ return A[1];
public void testNewton() throws IOException {
String src =
"""
-val test_sqrt = { flt x ->
+val _test_sqrt = { flt x ->
flt epsilon = 1e-15;
flt guess = x;
while( 1 ) {
@@ -335,15 +362,16 @@ val test_sqrt = { flt x ->
guess = next;
}
};
-flt farg = arg; return test_sqrt(farg);
+flt farg = arg;
+return _test_sqrt(farg);
""";
CodeGen code = new CodeGen(src).driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return Phi(Loop,(cvtf,arg),(mulf,(addf,(divf,cvtf,Phi_guess),Phi_guess),0.5f));", code.print());
+ assertEquals("return Phi(Loop,(cvtf,arg),(mulf,(addf,Phi_guess,(divf,cvtf,Phi_guess)),0.5f));", code.print());
};
@Test
- public void sieveOfEratosthenes() throws IOException {
+ public void sieveOfEratosthenes() {
String src =
"""
val sieve = { int N ->
@@ -378,8 +406,8 @@ val sieve = { int N ->
};
""";
CodeGen code = new CodeGen(src).driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("return []u32;", code.print());
- //assertEquals("u32[ 2,3,5,7,11,13,17,19]",Eval2.eval(code, 20));
+ assertEquals("Stop[ return { sieve}; return []u32; ]", code.print());
+ //assertEquals("{ ptr i64 -> *[]u32 #[ 3]}",Eval2.eval(code, 20));
}
@@ -387,21 +415,21 @@ val sieve = { int N ->
public void testFcn1() {
CodeGen code = new CodeGen(
"""
-val fcn = arg ? { int x -> x*x; } : { int x -> x+x; };
-return fcn(2)*10 + fcn(3);
+val _fcn = arg ? { int x -> x*x; } : { int x -> x+x; };
+return _fcn(2)*10 + _fcn(3);
""");
code.driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("Stop[ return (add,#2,(muli,#2)); return (mul,Parm_x($fun21,[2-3]),x); return (shli,Parm_x($fun22,[2-3])); ]", code.print());
+ assertEquals("Stop[ return (add,#2,(muli,#2)); return (mul,Parm_x($fun3,[2-3]),x); return (shli,Parm_x($fun4,[2-3])); ]", code.print());
}
@Test
public void testFcn2() {
CodeGen code = new CodeGen(
"""
-val sq = { int x -> x*x; };
-return sq(arg) + sq(3);
+val _sq = { int x -> x*x; };
+return _sq(arg) + _sq(3);
""");
code.driver(Phase.LocalSched,"x86_64_v2", "SystemV");
- assertEquals("Stop[ return (add,#2,#2); return (mul,Parm_x(sq,i64),x); ]", code.print());
+ assertEquals("return (addi,(mul,arg,arg));", code.print());
}
}