diff options
Diffstat (limited to 'chapter33/src')
4 files changed, 48 insertions, 1 deletions
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java index 18bd98e3..37baf926 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java @@ -15,7 +15,7 @@ record FunctionSummary(TypeFunPtr sig, TypeTuple state, String name, int size, i ParmNode mem = new ParmNode("$mem",1,TypeMem.BOT,fun,cu._start.proj(1)).init(); body.addDef(mem); for( int i = 0; i<sig.nargs(); i++ ) - body.addDef(new ParmNode("arg"+i,i+2,sig.arg(i),fun,code.con(sig.arg(i))).init()); + body.addDef(new ParmNode("arg"+i,i+2,sig.arg(i),fun,code.con(fun.defaultArg(i))).init()); body.init(); Node ctrl = new CProjNode(body,0,"ctrl").init(); Node memory = new ProjNode(body,1,"mem").init(); diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/FunNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/FunNode.java index 8b6b347e..896dff05 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/FunNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/FunNode.java @@ -102,6 +102,10 @@ public class FunNode extends RegionNode { // uniform call syntax, including callbacks forwarded by another class. public Type defaultArg(int idx) { Type t = sig().arg(idx); + // Private construction memory starts without published identities. + // Keep the signature broad: real calls supply their actual memory. + if( t instanceof TypeMem mem && mem._one ) + return mem.makeFrom(TypeSet.EMPTY,TypeSet.EMPTY); if( t instanceof TypeMemPtr ptr && ptr._partial && !(idx==0 && sig().nargs()>1 && sig().arg(1) instanceof TypeMem) ) return ptr.partialBound(); if( idx!=0 || t!=TypePtr.PTR ) return t; diff --git a/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java b/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java index 4ae7f771..4cacd0e6 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java @@ -8,6 +8,7 @@ import com.seaofnodes.simple.type.TypeFunPtr; import com.seaofnodes.simple.type.Field; import com.seaofnodes.simple.type.TypeInteger; import com.seaofnodes.simple.type.TypeMem; +import com.seaofnodes.simple.type.TypeSet; import com.seaofnodes.simple.type.TypeTuple; import com.seaofnodes.simple.util.Ary; import java.nio.file.Files; @@ -549,6 +550,25 @@ public class ClassInitDependenciesTest { assertNull(Eval2.eval(code,0,10)); } + @Test public void testImportedConstructorPrivateMemoryDefault() throws Exception { + Path dir=tempDir("constructor-default-"); + object(dir,dir,"Library","struct Box_noInline { int n; new Box_noInline={int x -> n=x;}; };"); + CodeGen code=new CodeGen(null,dir.toString(),new Ary<>(new String[]{dir.toString()}), + "Main","return new Library.Box_noInline(42).n;",123,true,TypeInteger.BOT).parse(); + int helpers=0; + for( FunNode fun : code._linker ) + if( fun!=null && fun.summary()!=null && fun.sig().nargs()>1 && fun.sig().arg(1) instanceof TypeMem ) { + helpers++; + TypeMem initial=(TypeMem)fun.parm(3).in(1)._type; + assertTrue(initial._one); + assertSame(TypeSet.EMPTY,initial._escFs); + assertSame(TypeSet.EMPTY,initial._escAs); + } + assertTrue(helpers>0); + code.driver(CodeGen.Phase.TypeCheck); + assertEquals("42",Eval2.eval(code,0)); + } + @Test public void testImportedPrivateInitializerCycle() throws Exception { Path dir=tempDir("private-cycle-"), stubs=tempDir("private-stub-"); object(stubs,stubs,"Main","int value=arg;"); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java b/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java index 15495696..d9a9a6fe 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java @@ -7,6 +7,29 @@ import org.junit.Test; import static org.junit.Assert.*; public class EscapeFlowTest { + @Test public void testConstructorPrivateMemoryDefault() { + CodeGen code = new CodeGen(""" + struct Box { int n; new Box = { int x -> n=x; }; }; + return new Box(((arg<<32)>>32)).n; + """).parse(); + int helpers=0; + for( FunNode fun : code._linker ) + if( fun!=null && fun.sig().nargs()>1 && fun.sig().arg(1) instanceof TypeMem bound ) { + helpers++; + TypeMem initial=(TypeMem)fun.defaultArg(1); + assertTrue(initial._one); + assertSame(Type.BOTTOM,initial._t); + assertSame(TypeSet.EMPTY,initial._escFs); + assertSame(TypeSet.EMPTY,initial._escAs); + // This is a default, not a restriction on actual private memory. + assertSame(TypeSet.FULL,bound._escFs); + assertSame(TypeSet.FULL,bound._escAs); + } + assertTrue(helpers>0); + code.driver(CodeGen.Phase.TypeCheck); + assertEquals("42",Eval2.eval(code,42)); + } + @Test public void testSummaryCallDependencyDoesNotEscape() { CodeGen code = new CodeGen("return 0;").parse(); code._phase = CodeGen.Phase.Opto; |
