summaryrefslogtreecommitdiff
path: root/chapter33/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'chapter33/src/test')
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/CallEndTest.java2
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/Chapter18Test.java48
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/Chapter20Test.java12
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/Chapter23Test.java5
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java16
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java65
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/MemMergeTest.java19
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/PhaseTest.java2
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/SelectiveLoadingTest.java68
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java60
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/StringFormatTest.java4
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java35
12 files changed, 324 insertions, 12 deletions
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/CallEndTest.java b/chapter33/src/test/java/com/seaofnodes/simple/CallEndTest.java
index 7983141d..26e479b2 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/CallEndTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/CallEndTest.java
@@ -78,7 +78,7 @@ public class CallEndTest {
// with its data parameter, not share it with the old body.
FldOffNode off = new FldOffNode(fun.parm(3),"<<",null).init();
fun.ret().setDef(2,off);
- assertSame(Type.BOTTOM,off._type);
+ assertSame(TypeInteger.BOT,off._type);
assertTrue("Unresolved field offset belongs to the function",fun.body().get(off._nid));
return;
}
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter18Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter18Test.java
index b1caae96..b6dc5138 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter18Test.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter18Test.java
@@ -13,6 +13,52 @@ import static org.junit.Assert.*;
import static org.junit.Assert.fail;
public class Chapter18Test {
+ @Test public void testLateInlineNonreturningLoop() {
+ CodeGen code = new CodeGen("val spin_noInline={ -> while(1) {} }; if(arg) spin_noInline(); return 7;")
+ .driver(Phase.TypeCheck);
+ FunNode spin = code._stop.walk(n -> n instanceof FunNode f && f._name.endsWith("spin_noInline") ? f : null);
+ assertNotNull(spin);
+ CallEndNode end = code._stop.walk(n -> n instanceof CallEndNode c && c.nIns()==2 && c.in(1)==spin.ret() ? c : null);
+ assertNotNull(end);
+ spin._name="spin";
+ code.add(end);
+ code._iter.iterate(code);
+ assertTrue(end.isDead());
+ assertEquals("7",Eval2.eval(code,0));
+ assertNull(Eval2.eval(code,1,10));
+ }
+
+ @Test public void testLateInlineLoopTree() {
+ CodeGen code = new CodeGen("""
+ val work_noInline = { i64 n -> i64 j=0; while(j<n) j++; return j; };
+ val keep_noInline = { i64 n -> i64 j=0; while(j<n) j+=2; return j; };
+ i64 i=0;
+ while(i<arg) { i=work_noInline(i)+1; }
+ return i+keep_noInline(arg);
+ """).driver(Phase.TypeCheck);
+ FunNode work = code._stop.walk(n -> n instanceof FunNode f && f._name.endsWith("work_noInline") ? f : null);
+ FunNode keep = code._stop.walk(n -> n instanceof FunNode f && f._name.endsWith("keep_noInline") ? f : null);
+ assertNotNull(work);
+ assertNotNull(keep);
+ LoopNode untouched = code._stop.walk(n -> n instanceof LoopNode l && !(l instanceof StartNode) && l.fun()==keep ? l : null);
+ assertNotNull(untouched);
+ CFGNode.LoopTree untouchedTree = untouched._ltree;
+ CallEndNode end = code._stop.walk(n -> n instanceof CallEndNode c && c.nIns()==2 && c.in(1)==work.ret() ? c : null);
+ assertNotNull(end);
+ LoopNode outer = end.call().loop();
+ CFGNode.LoopTree oldTree = outer._ltree;
+ // Permit this one call to inline after the initial normalization.
+ work._name="work";
+ code.add(end);
+ code._iter.iterate(code);
+ assertTrue("The late call was inlined",end.isDead());
+ assertNotSame("Rebuild the caller",oldTree,outer._ltree);
+ assertSame("Leave other functions alone",untouchedTree,untouched._ltree);
+ assertNotNull(code._stop.walk(n -> n instanceof LoopNode l && l!=outer &&
+ l._ltree!=null && l._ltree._par==outer._ltree ? l : null));
+ assertEquals("11",Eval2.eval(code,5));
+ }
+
@Test public void testNeverExitLoopDepth() {
String[] sources = {
"if(arg) while(1) {} return 7;",
@@ -21,7 +67,7 @@ public class Chapter18Test {
"int x=0; while(arg) { if(arg==2) while(1) { x++; } x++; arg--; } return x;"
};
for( String src : sources ) {
- CodeGen code = new CodeGen(src).driver(CodeGen.Phase.LoopTree);
+ CodeGen code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
int[] nevers = {0};
code._stop.walk(n -> {
if( n instanceof FunNode fun )
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter20Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter20Test.java
index 226425ca..008902c5 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter20Test.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter20Test.java
@@ -29,15 +29,19 @@ public class Chapter20Test {
};
for( String target : new String[]{"x86_64_v2","riscv","arm"} )
for( String src : sources ) {
- CodeGen code = new CodeGen(src).driver(CodeGen.Phase.RegAlloc,target,"SystemV");
+ CodeGen code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
+ assertNotNull(code._stop.walk(n -> n instanceof com.seaofnodes.simple.node.NeverNode ? n : null));
+ code.driver(CodeGen.Phase.RegAlloc,target,"SystemV");
com.seaofnodes.simple.codegen.RegAllocTestSupport.checkRegisters(code);
- assertNotNull(code._stop.walk(n -> {
+ // Early normalization allows peepholes to fold Phi(value,TOP).
+ // Any TOP that survives must still need neither a register nor a split.
+ code._stop.walk(n -> {
if( n._type!=com.seaofnodes.simple.type.Type.TOP ) return null;
assertEquals(-1,code._regAlloc.regnum(n));
for( var use : n.outs() )
assertFalse(use instanceof com.seaofnodes.simple.node.SplitNode);
- return n;
- }));
+ return null;
+ });
code.encode();
}
}
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter23Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter23Test.java
index 8667c0bd..8fb781f9 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter23Test.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter23Test.java
@@ -252,7 +252,7 @@ return (a && sq_noInline(0))
@Test
- public void testFRefFields() {
+ public void testFRefFields() throws IOException {
String src = """
// A hypothetical scanner class
struct _Scan {
@@ -279,7 +279,8 @@ return _s.peek('q');
CodeGen code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
assertEquals("return Phi(Region,0,1);", code.print());
assertEquals("1", Eval2.eval(code, 0));
- testCPU(src,"x86_64_v2", "win64",23,null);
+ testCPU(src,"x86_64_v2", "win64",31,null);
+ TestC.runSF(src.replace("return _s.peek('q');","return _s.peek('q') != 1;"),"frefFields","",-1);
};
@Test
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java
index b243beb2..87f9564d 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java
@@ -3,7 +3,8 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.util.WorkList;
-import com.seaofnodes.simple.type.TypeInteger;
+import com.seaofnodes.simple.type.*;
+import com.seaofnodes.simple.parser.Parser;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.Test;
@@ -11,6 +12,19 @@ import static org.junit.Assert.*;
/** Regressions for the independent compiler fixes introduced in this chapter. */
public class Chapter33Test {
+ @Test public void testBroadFieldStoreMatchesLayout() {
+ CodeGen code=new CodeGen("return 0;").driver(CodeGen.Phase.FinalizeIR);
+ int alias=code.alias("Test");
+ TypeStruct ts=TypeStruct.make("Test.Broad",false,Field.make("value",Type.BOTTOM,alias,false));
+ Parser.TYPES.put(ts._name,ts);
+ Node ptr=code.con(TypeMemPtr.make(ts));
+ StoreNode store=new StoreNode(null,"value",alias,Type.BOTTOM,null,
+ code.con(TypeMem.make(alias,Type.BOTTOM)),ptr,code.con(ts.offset(0)),code.con(4294967297L),false).init();
+ store.idealize();
+ assertEquals("A broad declaration still reserves a complete storage slot",
+ ts.offset(1)-ts.offset(0),store.storeSize());
+ }
+
@Test public void testWorkListRequeueAndTraversal() {
CodeGen code = new CodeGen("return arg;").parse();
Node arg = code.expr();
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java b/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java
index 4cacd0e6..3b52a5d2 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java
@@ -27,6 +27,53 @@ import org.junit.runners.model.Statement;
import static org.junit.Assert.*;
public class ClassInitDependenciesTest {
+ @Test public void testInitializedFlagAcrossCallers() throws Exception {
+ for( String name : new String[]{"_read_noInline","read_noInline"} )
+ for( boolean both : new boolean[]{true,false} ) {
+ CodeGen code=compile("A",Map.of("A","""
+ int state=0;
+ val _read_noInline={ -> B.value; };
+ int value=0;
+ if(arg) { state=1; value=B.value; value+=_read_noInline(); }
+ else { state=2; BEFORE value+=_read_noInline(); }
+ """.replace("BEFORE",both ? "value=B.value;" : "").replace("_read_noInline",name),"B","int value=arg;"),123);
+ CLInitNode touch=code._stop.walk(n -> n instanceof CLInitNode init &&
+ init.fun()._name.endsWith(name) ? init : null);
+ if( both && name.startsWith("_") ) assertNull("Every caller has already initialized B",touch);
+ else assertNotNull("An uninitialized or unknown caller still needs the check",touch);
+ }
+ }
+
+ @Test public void testLoweringPreservesLoopTree() throws Exception {
+ Path root=tempDir("lower-loops-");
+ Files.writeString(root.resolve("A.smp"),"int value=arg;");
+ CodeGen code=new CodeGen(root.toString(),root.resolve("obj").toString(),null,"Main",
+ "int i=0; while(i<arg) { i+=A.value+1; } return i;",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ var loops=new java.util.IdentityHashMap<LoopNode,CFGNode.LoopTree>();
+ int[] touches={0};
+ code._start.walk(n -> {
+ if( n instanceof LoopNode loop && !(loop instanceof StartNode) ) loops.put(loop,loop._ltree);
+ if( n instanceof CLInitNode init && init.loopDepth()>0 ) touches[0]++;
+ return null;
+ });
+ assertFalse(loops.isEmpty());
+ assertTrue("Exercise class initialization inside a loop",touches[0]>0);
+ CFGNode.LoopTree outer=code._start._ltree;
+ code.driver(CodeGen.Phase.Lower,"x86_64_v2","SystemV");
+ assertSame(outer,code._start._ltree);
+ for( var entry : loops.entrySet() )
+ if( !entry.getKey().isDead() ) assertSame(entry.getValue(),entry.getKey()._ltree);
+ code._start.walk(n -> {
+ if( n instanceof CFGNode cfg && !cfg.isDead() && !cfg._type.isHigh() &&
+ !(cfg instanceof StartNode) && !(cfg instanceof StopNode) &&
+ cfg.fun()!=null && cfg.fun().summary()==null )
+ assertNotNull("Missing containing loop for "+cfg.label(),cfg._ltree);
+ return null;
+ });
+ code.driver(CodeGen.Phase.Encoding,"x86_64_v2","SystemV");
+ }
+
@Test public void testPrivateInitializerCallEdges() throws Exception {
for( long seed : new long[]{1,123,987654321} ) {
Path root = tempDir("private-roots-");
@@ -429,7 +476,11 @@ public class ClassInitDependenciesTest {
if( n instanceof CLInitNode init && init.fun()==fun && init.cu()==b ) {
touches[0]++;
TypeTuple state=(TypeTuple)init._type;
- assertSame(TypeMem.BOT,state._types[1]);
+ // Initialization merges whole memory; its escape sets
+ // can be sharper than the all-escaped MemBot bound.
+ TypeMem memory=(TypeMem)state._types[1];
+ assertEquals(1,memory._alias);
+ assertSame(Type.BOTTOM,memory._t);
TypeMem flag=(TypeMem)state._types[2];
assertEquals(b._clz.field("$init")._alias,flag._alias);
assertSame(TypeInteger.TRUE,flag._t);
@@ -547,6 +598,18 @@ public class ClassInitDependenciesTest {
object(dir,dir,"Spin","val spin_noInline={ -> while(1) {} return 7; };");
CodeGen code=new CodeGen(null,dir.toString(),new Ary<>(new String[]{dir.toString()}),
"Main","Spin.spin_noInline(); return 7;",123,true,TypeInteger.BOT).driver(CodeGen.Phase.TypeCheck);
+ FunNode spin=code._stop.walk(n -> n instanceof FunNode f && f._name.endsWith("spin_noInline") ? f : null);
+ assertNotNull(spin);
+ assertNotNull(spin.summary());
+ assertSame(Type.XCONTROL,spin.returnState().ctl());
+ assertNull(Eval2.eval(code,0,10));
+ }
+
+ @Test public void testInlineNonreturningObject() throws Exception {
+ Path dir=tempDir("nonreturning-inline-");
+ object(dir,dir,"Spin","val spin={ -> while(1) {} return 7; };");
+ CodeGen code=new CodeGen(null,dir.toString(),new Ary<>(new String[]{dir.toString()}),
+ "Main","Spin.spin(); return 7;",123,true,TypeInteger.BOT).driver(CodeGen.Phase.TypeCheck);
assertNull(Eval2.eval(code,0,10));
}
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/MemMergeTest.java b/chapter33/src/test/java/com/seaofnodes/simple/MemMergeTest.java
index 253bce0b..6ab3ee67 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/MemMergeTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/MemMergeTest.java
@@ -2,11 +2,28 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.node.*;
-import com.seaofnodes.simple.type.TypeMem;
+import com.seaofnodes.simple.type.*;
import org.junit.Test;
import static org.junit.Assert.*;
public class MemMergeTest {
+ @Test public void testUnknownPrivateShapeBoundsStoredReferences() {
+ CodeGen code=new CodeGen("return 0;").parse();
+ TypeFunPtr callback=TypeFunPtr.make1((byte)2,true,TypeFunPtr.TEMPTY,TypeInteger.BOT,7);
+ TypeStruct shape=TypeStruct.make("PrivateCallback",false,Field.make("callback",callback,21,true));
+ Node base=ConstantNode.raw(TypeMem.makePrivate(Type.BOTTOM).makeFrom(TypeSet.EMPTY,TypeSet.EMPTY));
+ StoreNode store=new StoreNode(null,"callback",21,callback,code._start,base,
+ code.con(TypeMemPtr.make(shape)),code.con(TypeInteger.ZERO),code.con(callback),true).init();
+ MemMergeNode mem=new MemMergeNode(null,base);
+ mem.alias(21,store);
+ Type bound=mem.compute();
+ base._type=TypeMem.makePrivate(shape);
+ store._type=store.compute();
+ TypeMem precise=(TypeMem)mem.compute();
+ assertTrue(precise._escFs.contains(7));
+ assertTrue("Learning the private shape must refine the old result",precise.isa(bound));
+ }
+
@Test public void testDenseAliasesAndDefaults() {
CodeGen code = new CodeGen("");
Node defaults = code.con(TypeMem.START), value = code.con(TypeMem.BOT).keep();
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/PhaseTest.java b/chapter33/src/test/java/com/seaofnodes/simple/PhaseTest.java
index 6505009d..36ac1477 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/PhaseTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/PhaseTest.java
@@ -27,7 +27,7 @@ public class PhaseTest {
code.driver(Phase.TypeCheck); // A repeated request does not rerun anything.
assertEquals("45",Eval2.eval(code,10));
code.driver(Phase.LocalSched);
- assertEquals("Parse Iter Opto IterPostOpto FinalizeIR TypeCheck LoopTree Unlink Schedule LocalSched",
+ assertEquals("Parse Iter Opto IterPostOpto FinalizeIR LoopTree TypeCheck Unlink Schedule LocalSched",
String.join(" ",phases));
assertNull(code._serial);
assertNull(code._mach);
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/SelectiveLoadingTest.java b/chapter33/src/test/java/com/seaofnodes/simple/SelectiveLoadingTest.java
index 447025f6..8ea351d0 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/SelectiveLoadingTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/SelectiveLoadingTest.java
@@ -4,6 +4,8 @@ import com.seaofnodes.simple.parser.Parser;
import com.seaofnodes.simple.codegen.*;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
+import com.seaofnodes.simple.type.TypeTuple;
+import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.util.Ary;
import java.io.IOException;
import java.nio.file.*;
@@ -11,6 +13,72 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class SelectiveLoadingTest {
+ @Test public void testNonreturningSummaryRetainsCallees() throws Exception {
+ Path root=library("summary-nonreturning-");
+ Ary<String> paths=new Ary<>(new String[]{root.toString()});
+ object(root,paths,"Library","""
+ var _state=0;
+ val _helper_noInline={ -> _state=1; };
+ val spin_noInline={ -> _helper_noInline(); while(1) {} return 7; };
+ """);
+ CodeGen code=new CodeGen(null,root.toString(),paths,"Main",
+ "Library.spin_noInline(); return 7;",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.IterPostOpto);
+ assertSame(Type.XCONTROL,function(code,"spin_noInline").summary()._state.ctl());
+ FunNode helper=function(code,"_helper_noInline");
+ assertTrue(helper.hasSummaryCallers());
+ assertTrue("Calls before a nonreturning exit still execute",helper.unknownCallers());
+ }
+
+ @Test public void testUnreachableSummaryCycleIsNotARoot() throws Exception {
+ Path root=library("summary-cycle-");
+ Ary<String> paths=new Ary<>(new String[]{root.toString()});
+ object(root,paths,"Library","""
+ val _left_noInline={ i64 x -> x ? _right_noInline(x-1) : 0; };
+ val _right_noInline={ i64 x -> x ? _left_noInline(x-1) : 1; };
+ val entry_noInline={ i64 x -> _left_noInline(x); };
+ """);
+ for( long seed : new long[]{1,123,987654321} ) {
+ CodeGen code=new CodeGen(null,root.toString(),paths,"Main","""
+ val _false_noInline={ -> false; };
+ if(_false_noInline()) return Library.entry_noInline(((arg<<32)>>32));
+ return 7;
+ """,seed,true,TypeInteger.BOT).driver(CodeGen.Phase.TypeCheck);
+ CompUnit library=code._compunits.get("Library");
+ for( FunNode fun : code._linker )
+ assertTrue("A cycle of summary references must not root its functions",
+ fun==null || fun.isDead() || fun._compunit!=library);
+ assertEquals("7",Eval2.eval(code,0));
+ }
+ }
+
+ @Test public void testSummaryCalleeDefaultAndRelease() throws Exception {
+ Path root=library("summary-callees-");
+ Ary<String> paths=new Ary<>(new String[]{root.toString()});
+ object(root,paths,"Library","""
+ val _helper_noInline={ int x -> x+1; };
+ val entry_noInline={ int x -> _helper_noInline(x); };
+ """);
+ CodeGen code=new CodeGen(null,root.toString(),paths,"Main",
+ "return Library.entry_noInline(((arg<<32)>>32));",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.IterPostOpto);
+ FunNode helper=function(code,"_helper_noInline");
+ assertTrue("An unloaded caller retains the default",helper.unknownCallers());
+ assertTrue(helper.hasSummaryCallers());
+ assertFalse("A summary call does not publish its target",
+ ((TypeTuple)code._start._type).mem()._escFs.contains(helper.sig().fidx()));
+
+ code.loadForInline(code._compunits.get("Library"));
+ code._iter.iterate(code);
+ helper=function(code,"_helper_noInline");
+ assertFalse("Concrete calls replace the summary references",helper.hasSummaryCallers());
+ assertEquals("Only escapes can retain the default after loading",
+ ((TypeTuple)helper._compunit._start._type).mem()._escFs.contains(helper.sig().fidx()),helper.unknownCallers());
+ assertTrue(helper.nIns()>1);
+ code.driver(CodeGen.Phase.TypeCheck);
+ assertEquals("42",Eval2.eval(code,41));
+ }
+
@Test public void testSharedConstantsInSeparateObjects() throws Exception {
Path root = library("shared-constants-");
Path src = Files.createDirectory(root.resolve("src"));
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java b/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java
index 72e26a2e..be20b41d 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java
@@ -10,6 +10,66 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class SeparateCompilationTest {
+ @Test public void testInferredImportedFieldStoreWidth() throws Exception {
+ Path lib=Files.createTempDirectory(Files.createDirectories(Path.of("build/objs/separate")),"inferred-store-");
+ new CodeGen(null,lib.toString(),null,"Library",
+ "val number_noInline={ -> 7; };",123,true,TypeInteger.BOT)
+ .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false);
+ var paths=new Ary<>(new String[]{lib.toString()});
+ String src="""
+ struct Box { var value=Library.number_noInline(); };
+ val b=new Box;
+ b.value=4294967297;
+ return b.value;
+ """;
+ CodeGen code=new CodeGen(null,lib.toString(),paths,"Client",src,123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ assertEquals("4294967297",Eval2.eval(code,0));
+ TestC.run(src.replace("return b.value;","return b.value!=4294967297;"),
+ "inferred_import_store",paths,TestC.CALL_CONVENTION,null,null,"",-1);
+ }
+
+ @Test public void testImportedNestedTypeOnlyReference() throws Exception {
+ Path lib=Files.createTempDirectory(Files.createDirectories(Path.of("build/objs/separate")),"nested-type-");
+ new CodeGen(null,lib.toString(),null,"Library",
+ "struct Box { i64 x=42; };",123,true,TypeInteger.BOT)
+ .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false);
+ var paths=new Ary<>(new String[]{lib.toString()});
+ // No expression mentions Library: only this parameter requests its type table.
+ CodeGen code=new CodeGen(null,lib.toString(),paths,"Client",
+ "val read_noInline={ Library.Box b -> b.x; }; return 0;",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ assertEquals("0",Eval2.eval(code,0));
+ for( String name : new String[]{"Library.Missing"} ) {
+ try {
+ new CodeGen(null,lib.toString(),paths,"Client",
+ "val read_noInline={ "+name+" b -> b.x; }; return 0;",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ fail("Accepted nonexistent nested type "+name);
+ } catch( com.seaofnodes.simple.parser.Parser.ParseException expected ) {
+ assertTrue(expected.getMessage(),expected.getMessage().contains("Unknown type"));
+ }
+ }
+ }
+
+ @Test public void testNestedTypeInAnotherSourceUnit() throws Exception {
+ Path root=Files.createTempDirectory(Files.createDirectories(Path.of("build/objs/separate")),"nested-source-");
+ Files.createDirectories(root.resolve("Main"));
+ Files.writeString(root.resolve("Main/Library.smp"),"struct Box { i64 x=42; };");
+ CodeGen code=new CodeGen(root.toString(),root.resolve("obj").toString(),null,"Main",
+ "val read_noInline={ Library.Box b -> b.x; }; return 0;",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ assertEquals("0",Eval2.eval(code,0));
+ try {
+ new CodeGen(root.toString(),root.resolve("obj").toString(),null,"Main",
+ "val read_noInline={ Library.Missing b -> b.x; }; return 0;",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ fail("The owning file exists, but it does not define Missing");
+ } catch( com.seaofnodes.simple.parser.Parser.ParseException expected ) {
+ assertTrue(expected.getMessage(),expected.getMessage().contains("Unknown type"));
+ }
+ }
+
@Test public void testSparseWideMemoryImport() throws Exception {
Path build=Path.of("build/objs/separate");
Files.createDirectories(build);
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/StringFormatTest.java b/chapter33/src/test/java/com/seaofnodes/simple/StringFormatTest.java
index d6783380..0163de05 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/StringFormatTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/StringFormatTest.java
@@ -11,6 +11,10 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class StringFormatTest extends TestC {
+ @Test public void testSingleIntegerInterpolation() throws Exception {
+ runSYS("val main={ -> sys.io.p(\"%(7)\"); return 0; };","singleIntegerInterpolation","7",-1);
+ }
+
// Share a few native programs, retaining a separately reported assertion
// for each case. Blocks isolate locals; the delimiter preserves embedded
// NULs and newlines in the expected output.
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java b/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java
index 28b6b0fb..fb1834fa 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java
@@ -1,12 +1,47 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.type.*;
+import java.util.HashMap;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertSame;
public class TypeTest {
+ @Test public void testSingletonMemoryUpgradeEscapes() {
+ TypeStruct open = TypeStruct.open("class:UpgradeChild");
+ TypeMemPtr ptr = TypeMemPtr.make((byte)2,open,true);
+ TypeFunPtr callback = TypeFunPtr.make1((byte)2,true,TypeFunPtr.TEMPTY,TypeInteger.BOT,11);
+ TypeStruct child = open.add(Field.make("self",ptr,7,true))
+ .add(Field.make("callback",callback,8,true))
+ .add(Field.make("value",TypeInteger.BOT,9,false)).close();
+ HashMap<String,Type> types = new HashMap<>();
+ types.put(child._name,child);
+ TypeMem initial = TypeMem.make(6,ptr,true,true,true,TypeSet.FULL,TypeSet.FULL);
+ TypeMem upgraded = (TypeMem)initial.upgradeType(types);
+ assertSame(child,((TypeMemPtr)upgraded._t)._obj);
+ assertSame(TypeSet.of(11),upgraded._escFs);
+ assertSame(TypeSet.of(7).with(8).with(9),upgraded._escAs);
+ assertSame(upgraded,upgraded.upgradeType(types));
+
+ // Only a final singleton class slot can replace its old escape bound.
+ // Aggregate memory and mutable/non-class slots retain prior escapes.
+ for( TypeMem other : new TypeMem[]{
+ TypeMem.make(6,ptr,true,true,false,TypeSet.FULL,TypeSet.FULL),
+ TypeMem.make(6,ptr,true,false,true,TypeSet.FULL,TypeSet.FULL),
+ TypeMem.make(6,ptr,false,true,true,TypeSet.FULL,TypeSet.FULL),
+ TypeMem.make(1,ptr,true,true,true,TypeSet.FULL,TypeSet.FULL)} ) {
+ TypeMem kept = (TypeMem)other.upgradeType(types);
+ assertSame(TypeSet.FULL,kept._escFs);
+ assertSame(TypeSet.FULL,kept._escAs);
+ }
+ TypeStruct opaque = TypeStruct.forward("ExternalUpgradeChild");
+ types.put(opaque._name,opaque);
+ TypeMem external = TypeMem.make(6,TypeMemPtr.make((byte)2,opaque,true),
+ true,true,true,TypeSet.FULL,TypeSet.FULL);
+ assertSame(external,external.upgradeType(types));
+ }
+
@Test public void testFinalPointerCycles() {
TypeStruct cycle = TypeStruct.open("FinalPointerCycle");
Assert.assertFalse(cycle.isFinal());