summaryrefslogtreecommitdiff
path: root/chapter31/src/test/java/com/seaofnodes
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-13 16:17:13 -0800
committerCliff Click <cliffc@acm.org>2026-11-13 16:17:13 -0800
commitafcf96444366fb4b7c83f938763013cbc360aade (patch)
tree67eb69ac6cc58322549020550d48aceb8f2f5ff8 /chapter31/src/test/java/com/seaofnodes
parentd2c59dbe9d6cc8dc4ff4d1e7949bfeddd8bef5d0 (diff)
downloadSimple-afcf96444366fb4b7c83f938763013cbc360aade.tar.gz
Simple-afcf96444366fb4b7c83f938763013cbc360aade.zip
Array init syntax
New array init syntax; used in a bunch of places. Allows e.g. read-only arrays with not-null entries. Fix bug x86 encoding of mem/cmp/fp compares
Diffstat (limited to 'chapter31/src/test/java/com/seaofnodes')
-rw-r--r--chapter31/src/test/java/com/seaofnodes/simple/ArrayInitTest.java175
-rw-r--r--chapter31/src/test/java/com/seaofnodes/simple/Chapter22Test.java4
-rw-r--r--chapter31/src/test/java/com/seaofnodes/simple/Chapter25Test.java2
-rw-r--r--chapter31/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java31
-rw-r--r--chapter31/src/test/java/com/seaofnodes/simple/Eval2.java18
-rw-r--r--chapter31/src/test/java/com/seaofnodes/simple/MemoryEncodingTest.java25
-rw-r--r--chapter31/src/test/java/com/seaofnodes/simple/ParserDiagnosticTest.java18
7 files changed, 265 insertions, 8 deletions
diff --git a/chapter31/src/test/java/com/seaofnodes/simple/ArrayInitTest.java b/chapter31/src/test/java/com/seaofnodes/simple/ArrayInitTest.java
new file mode 100644
index 00000000..6cba5ee3
--- /dev/null
+++ b/chapter31/src/test/java/com/seaofnodes/simple/ArrayInitTest.java
@@ -0,0 +1,175 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class ArrayInitTest {
+ private static void value(String src, long expected) {
+ CodeGen code=new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
+ assertEquals(Long.toString(expected),Eval2.eval(code,0,100000));
+ }
+
+ private static void bad(String src, String message) {
+ try {
+ new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
+ fail("Expected: "+message);
+ } catch( com.seaofnodes.simple.parser.Parser.ParseException e ) {
+ assertTrue(e.getMessage(),e.getMessage().contains(message));
+ }
+ }
+
+ @Test public void lists() {
+ value("val a=new u16[3]{025755,1,65538}; return a#*100000+a[0]+a[1]+a[2];",311248);
+ value("val a=new int[]{4,7,9,}; a[1]=2; return a#+a[0]+a[1]+a[2];",18);
+ value("val a=new int[]{}; return a#;",0);
+ bad("val a=new int[2]{1}; return a[0];","initializer has 1 elements");
+ }
+
+ @Test public void generators() {
+ value("int n=6; val a=new int[n]{int i -> i*i;}; return a[5]+a#;",31);
+ value("int base=7; val a=new int[3]{int i -> return base+i;}; return a[2];",9);
+ value("val a=new int[3]{int i -> if(i==1) return 9; i+1;}; return a[0]+a[1]+a[2];",13);
+ value("val a=new int[0]{int i -> 1/i;}; return a#;",0);
+ bad("val a=new int[]{int i -> i;}; return a#;","requires a length");
+ bad("val f={-> int x=1; val a=new int[2]{int i -> x++;}; return a[0];}; return f();","captured local");
+ }
+
+ @Test public void referencesAndReadonly() {
+ value("struct S { int x; new S={i64 v -> x=v;}; }; val a=new S[~3]{int i -> new S(i+4);}; return a[2].x;",6);
+ value("val a=new u8[~]{1,2,3}; return a[1];",2);
+ value("val names=new u8[~][~2]{\"Alice\",\"Bob\"}; return names[1][0];",66);
+ value("val a=new int[ ~ ]{4,5}; return a[1];",5);
+ value("val a=new {i64 -> i64}[]{ {i64 x -> x+1;}, {i64 x -> x*2;} }; return a[1](7);",14);
+ value("struct S { int x=1; }; val a=new !S[1]{new S}; a[0].x=8; return a[0].x;",8);
+ bad("struct S { int x=1; }; val a=new !S[~1]{new S}; a[0].x=8; return a[0].x;","final field");
+ value("struct S { int x; }; val a=new S?[~2]{u32 i -> null;}; return a[1]==null;",1);
+ bad("val a=new int[~2]{1,2}; a[0]=3; return a[0];","final field");
+ bad("struct S { int x; }; val a=new S[1]{null}; return a#;","declared type");
+ }
+
+ @Test public void nativeInitializers() throws Exception {
+ TestC.runSF("""
+ val main = { ->
+ int n=8;
+ val a=new u16[~n]{int i -> i*i+1;};
+ val b=new u8[]{257,2,3};
+ return a[7]==50 && b[0]==1 && a#==8 ? 0 : 1;
+ };
+ ""","arrayInitializers","",-1);
+ }
+
+ @Test public void nestedBodies() {
+ value("""
+ val f={ ->
+ int offset=5;
+ val a=new int[4]{int i ->
+ int sum=offset;
+ for(int j=0;j<i;j++) { if(j==1) continue; sum+=j; }
+ if(i==2) return 20;
+ return sum;
+ };
+ return a[0]+a[1]+a[2]+a[3];
+ };
+ return f();
+ """,37);
+ value("""
+ val a=new int[][3]{int i ->
+ new int[2]{int j -> i*10+j;};
+ };
+ return a[2][1]+a[0][0];
+ """,21);
+ value("""
+ val a=new int[2]{int i ->
+ val twice={i64 x -> return x*2;};
+ twice(i);
+ };
+ return a[1];
+ """,2);
+ }
+
+ @Test public void orderAndIdentity() throws Exception {
+ TestC.runSF("""
+ struct Counter {
+ int calls=0;
+ val next={->calls++;};
+ };
+ struct Item {
+ int x;
+ new Item={i64 v -> x=v;};
+ };
+ val build_noInline={u32 n, !Counter c ->
+ new Item[~n]{u32 i -> new Item(c.next());};
+ };
+ val main={->
+ val c=new Counter;
+ val empty=build_noInline(0,c);
+ if(empty#!=0 || c.calls!=0) return 1;
+ val a=build_noInline(4,c);
+ if(c.calls!=4 || a[0].x!=0 || a[3].x!=3 || a[0]==a[1]) return 2;
+ val b=new int[]{c.next(),c.next(),c.next()};
+ if(b[0]!=4 || b[2]!=6) return 3;
+ val d=new int[c.next()]{i64 i -> c.next();};
+ if(d#!=7 || d[0]!=8 || d[6]!=14 || c.calls!=15) return 4;
+ return 0;
+ };
+ ""","arrayInitOrder","",-1);
+ }
+
+ @Test public void diagnostics() {
+ bad("val f={u32 n -> new int[n]{1,2};}; return f(arg)#;","length must equal that constant");
+ bad("val a=new int[2]{u8 i -> i;}; return a[0];","generator index");
+ bad("val a=new int[2]{int return -> 0;}; return a[0];","index name");
+ bad("val a=new int[2]{int i -> i++;}; return a[0];","final 'i'");
+ bad("val f={->int x=0; return new int[2]{int i -> x=i;};}; return f()#;","captured local");
+ bad("val a=new int[2]{int i -> break;}; return a[0];","No active loop");
+ bad("val a=new int[-1]{}; return a#;","outside the u32 range");
+ bad("val a=new int[1.5]{int i -> i;}; return a#;","integer");
+ bad("val a=new int[~]{1,2}; val b=a; b[0]++; return b[0];","final field");
+ for( String init : new String[]{"self","int i -> self;"} )
+ bad("struct C { int x; new C={-> val a=new C[1]{"+init+"}; x=a#;}; }; return new C().x;",
+ "fully initialized elements");
+ }
+
+ @Test public void forwardReferencesAndLoopScopes() {
+ value("""
+ val f={ ->
+ int sum=0;
+ for(int k=0;k<3;k++) {
+ val a=new int[2]{int i ->
+ if(i) return later(i);
+ later(3);
+ };
+ if(k==1) continue;
+ sum+=a[0]+a[1];
+ }
+ return sum;
+ };
+ val later={i64 i -> i*2;};
+ return f();
+ """,16);
+ }
+
+ @Test public void optimizationOrder() {
+ String src="""
+ struct Box { int x; new Box={i64 v -> x=v;}; };
+ val f={u32 n ->
+ int base=7;
+ val a=new Box[~n]{u32 i ->
+ int x=base;
+ for(int j=0;j<i;j++) x+=j;
+ if(i==2) return new Box(20);
+ new Box(x);
+ };
+ return n==0 ? 0 : a[n-1].x;
+ };
+ return f(arg&7);
+ """;
+ for( long seed : new long[]{1,123,987654321} ) {
+ CodeGen code=new CodeGen(src,seed,true).driver(CodeGen.Phase.TypeCheck);
+ assertEquals("0",Eval2.eval(code,0));
+ assertEquals("20",Eval2.eval(code,3));
+ assertEquals("13",Eval2.eval(code,5));
+ }
+ }
+}
diff --git a/chapter31/src/test/java/com/seaofnodes/simple/Chapter22Test.java b/chapter31/src/test/java/com/seaofnodes/simple/Chapter22Test.java
index 9bebee23..c9791de4 100644
--- a/chapter31/src/test/java/com/seaofnodes/simple/Chapter22Test.java
+++ b/chapter31/src/test/java/com/seaofnodes/simple/Chapter22Test.java
@@ -332,9 +332,7 @@ return 0;
public void testFinalArray() {
String src = """
int N=4;
-i32[] !is = new i32[N];
-for( int i=0; i<N; i++ )
- is[i] = i*i;
+i32[~] !is = new i32[~N]{ int i -> i*i; };
val _sum = { i32[~] is -> // final array
int sum=0;
for( int i=0; i<is#; i++ )
diff --git a/chapter31/src/test/java/com/seaofnodes/simple/Chapter25Test.java b/chapter31/src/test/java/com/seaofnodes/simple/Chapter25Test.java
index 6b892c65..055ef518 100644
--- a/chapter31/src/test/java/com/seaofnodes/simple/Chapter25Test.java
+++ b/chapter31/src/test/java/com/seaofnodes/simple/Chapter25Test.java
@@ -477,7 +477,7 @@ public class Chapter25Test {
// Software drawing and the optional native graphics interface.
assertArrayEquals(new String[]{"sys/adt", "sys/ary",
- "sys/aryi64", "sys/aryu8", "sys/audio", "sys/char", "sys/draw", "sys/gfx", "sys/io", "sys/libc", "sys/rt", "sys/scan", "sys/text", "sys/time"},sys_elf._deps);
+ "sys/aryi64", "sys/aryu8", "sys/audio", "sys/char", "sys/draw", "sys/gfx", "sys/integer", "sys/io", "sys/libc", "sys/rt", "sys/scan", "sys/text", "sys/time"},sys_elf._deps);
assertArrayEquals(new String[]{"sys"},sys_elf._units);
assertSame("class:sys" ,sys_elf._clz._name);
}
diff --git a/chapter31/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java b/chapter31/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java
index 5a20388b..12634f24 100644
--- a/chapter31/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java
+++ b/chapter31/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java
@@ -7,6 +7,37 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class EscapeFlowTest {
+ @Test public void testArrayGeneratorDoesNotPublishUnrelatedFunctions() {
+ for( long seed : new long[]{1,123,987654321} )
+ for( String access : new String[]{"","~"} ) {
+ CodeGen code=new CodeGen("""
+ val _hidden_noInline={i64 x -> x+1;};
+ val data=new i32[%s(arg&7)]{int i -> i*i;};
+ return data#;
+ """.formatted(access),seed,true).parse();
+ int hidden=named(code,"_hidden_noInline").sig().fidx();
+ code.driver(CodeGen.Phase.TypeCheck);
+ TypeMem outside=((TypeTuple)code._start._type).mem();
+ assertFalse(XInt.bit(outside._escFs,hidden));
+ assertEquals("0",Eval2.eval(code,0));
+ assertEquals("4",Eval2.eval(code,4));
+ }
+ }
+
+ @Test public void testArrayGeneratorPublishesItsCallbacks() {
+ CodeGen code=new CodeGen("""
+ val _callback_noInline={i64 x -> x+1;};
+ val callbacks=new {i64 -> i64}[~3]{int i -> _callback_noInline;};
+ return callbacks;
+ """).parse();
+ FunNode callback=named(code,"_callback_noInline");
+ int fidx=callback.sig().fidx();
+ code.driver(CodeGen.Phase.TypeCheck);
+ assertTrue(XInt.bit(((TypeTuple)code._start._type).mem()._escFs,fidx));
+ assertFalse(callback.isDead());
+ assertTrue(callback.unknownCallers());
+ }
+
// A cofinite outside set must not revive private allocation helpers.
@Test public void testOpaqueRoundTrip() {
for( long seed : new long[]{1,123,987654321} ) {
diff --git a/chapter31/src/test/java/com/seaofnodes/simple/Eval2.java b/chapter31/src/test/java/com/seaofnodes/simple/Eval2.java
index 8d0e3d5b..b6228666 100644
--- a/chapter31/src/test/java/com/seaofnodes/simple/Eval2.java
+++ b/chapter31/src/test/java/com/seaofnodes/simple/Eval2.java
@@ -388,7 +388,7 @@ public abstract class Eval2 {
TypeMem mem = (TypeMem)con._type;
Object val = con(mem._t);
TypeMemPtr tmp = (TypeMemPtr)esc.self()._type;
- int idx = tmp._obj.findAlias(esc.fld()._alias);
+ int idx = layout(tmp._obj).findAlias(esc.fld()._alias);
Object[] fs = (Object[])val(esc.self());
if( tmp._obj.isAry() ) {
assert idx==0; // length field
@@ -443,10 +443,20 @@ public abstract class Eval2 {
private static Object[] classObject(TypeStruct type) {
Object[] ptr = CLASSES.get(type._name);
- if( ptr==null ) CLASSES.put(type._name,ptr=alloc(type));
+ if( ptr==null ) CLASSES.put(type._name,ptr=alloc(layout(type)));
return ptr;
}
+ // Imported pointer views may retain parsing-only forward-reference fields.
+ // Like native field offsets, evaluator slots use the module's final layout.
+ private static TypeStruct layout(TypeStruct type) {
+ if( TypeStruct.startsClzPrefix(type._name) ) {
+ CompUnit cu=CODE._compunits.get(type._name.substring(6).replace('.','/'));
+ if( cu!=null ) return cu._clz;
+ }
+ return type;
+ }
+
private static Object not( NotNode not ) {
Object n = val(not.in(1));
return ( n==null || (n instanceof Double D && D==0.0) || n instanceof Long L && L==0 )
@@ -465,7 +475,7 @@ public abstract class Eval2 {
TypeMemPtr tmp = (TypeMemPtr)ld.ptr()._type;
int idx = tmp._obj.isAry()
? offToIdx(x(ld.off()),tmp._obj)
- : tmp._obj.find(ld._name);
+ : layout(tmp._obj).find(ld._name);
return fs[idx];
}
@@ -476,7 +486,7 @@ public abstract class Eval2 {
Object[] fs = (Object[])val(st.ptr());
int idx = tmp._obj.isAry()
? offToIdx(x(st.off()),tmp._obj)
- : tmp._obj.find(st._name);
+ : layout(tmp._obj).find(st._name);
Object val = val(st.val());
// Idealization can remove conversions already performed by the store.
Type type = st.declaredType();
diff --git a/chapter31/src/test/java/com/seaofnodes/simple/MemoryEncodingTest.java b/chapter31/src/test/java/com/seaofnodes/simple/MemoryEncodingTest.java
index adeba136..6d81af85 100644
--- a/chapter31/src/test/java/com/seaofnodes/simple/MemoryEncodingTest.java
+++ b/chapter31/src/test/java/com/seaofnodes/simple/MemoryEncodingTest.java
@@ -8,6 +8,31 @@ import static org.junit.Assert.*;
// Execution regressions are separate from the fixed spill-measurement cohorts.
public class MemoryEncodingTest {
+ @Test public void testIntegerMemoryOperandsAcrossCalls() throws java.io.IOException {
+ TestC.runSF("""
+ val touch_noInline={i64[] a, i64 i -> a[i]+=1;};
+ val scan_noInline={i64[] a, i64 limit, i64 delta ->
+ int below=0, above=0;
+ for(int i=0;i<a#;i++) {
+ touch_noInline(a,i);
+ if(a[i]<limit) below++;
+ if(limit<a[i]) above++;
+ a[i]+=delta;
+ }
+ return below*10+above;
+ };
+ val main={->
+ val a=new i64[]{0x7ffffffffffffffe,-2,0};
+ if(scan_noInline(a,0x7fffffffffffffff,0)!=20) return 1;
+ if(a[0]!=0x7fffffffffffffff || a[1]!=-1 || a[2]!=1) return 2;
+ val b=new i64[]{-3,-2,0};
+ if(scan_noInline(b,-1,0x100000000)!=11) return 3;
+ if(b[0]!=0xfffffffe || b[1]!=0xffffffff || b[2]!=0x100000001) return 4;
+ return 0;
+ };
+ ""","integerMemoryAcrossCalls","",-1);
+ }
+
@Test public void testLoopLoadSearch() {
String[] sources = {
"""
diff --git a/chapter31/src/test/java/com/seaofnodes/simple/ParserDiagnosticTest.java b/chapter31/src/test/java/com/seaofnodes/simple/ParserDiagnosticTest.java
index ef26a943..a1253d05 100644
--- a/chapter31/src/test/java/com/seaofnodes/simple/ParserDiagnosticTest.java
+++ b/chapter31/src/test/java/com/seaofnodes/simple/ParserDiagnosticTest.java
@@ -134,6 +134,24 @@ public class ParserDiagnosticTest {
+"\n return x;\n ^",error.diagnostic());
}
+ @Test public void testJUnitReportsSourceDiagnostic() {
+ var error=rejected("val dist=new i64[3]{int i -> i;}\ndist[0]=0;");
+ String expected="Test.smp:2:1: error: Syntax error, expected `;` but found `dist`"
+ +"\n dist[0]=0;\n ^"+System.lineSeparator();
+ var test=org.junit.runner.Description.createTestDescription(getClass(),"missingSemicolon");
+ assertEquals(expected,new org.junit.runner.notification.Failure(test,error).getTrace());
+
+ var bytes=new java.io.ByteArrayOutputStream();
+ error.printStackTrace(new java.io.PrintStream(bytes,true,StandardCharsets.UTF_8));
+ assertEquals(expected,bytes.toString(StandardCharsets.UTF_8));
+ assertEquals("Syntax error, expected `;` but found `dist`",error.getMessage());
+
+ // Compiler implementation failures must still include Java frames.
+ String trace=new org.junit.runner.notification.Failure(test,new IllegalStateException("compiler bug")).getTrace();
+ assertTrue(trace,trace.contains("java.lang.IllegalStateException: compiler bug"));
+ assertTrue(trace,trace.contains("ParserDiagnosticTest.testJUnitReportsSourceDiagnostic"));
+ }
+
@Test public void testEOF() {
var error=rejected("return 1");
assertEquals("Test.smp:1:9: error: Syntax error, expected `;` but found `EOF`"