From ddab8b84511ccab8ec04edc8f2f03174b081c926 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Thu, 3 Dec 2026 09:15:00 -0800 Subject: restructure parsing vs loading Get all the new FIDXs loaded up-front, so loading infinite-extent sets can exclude the known --- .../java/com/seaofnodes/simple/Chapter33Test.java | 88 ++++++++++++++++++++++ .../simple/codegen/ImportTestSupport.java | 74 ++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 chapter33/src/test/java/com/seaofnodes/simple/codegen/ImportTestSupport.java (limited to 'chapter33/src/test/java/com/seaofnodes') diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java index f28eed00..99470aa3 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java @@ -456,4 +456,92 @@ public class Chapter33Test { 123,true,TypeInteger.BOT).driver(CodeGen.Phase.Allocation2); assertTrue(code._allocation2.verify()); } + @Test public void testLateImportUniverses() { + ImportTestSupport.checkUniverses(); + } + + @Test public void testLateImportReturnedCallback() throws Exception { + Path lib=Files.createTempDirectory(Files.createDirectories(Path.of("build/objs")),"late-callback-"); + new CodeGen(null,lib.toString(),null,"Relay","val relay_noInline={{i64->i64} f -> f;}; return 0;", + 123,true,TypeInteger.BOT).driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false); + CodeGen code=new CodeGen(null,null,new Ary<>(new String[]{lib.toString()}),"Client",""" + val _sent={i64 x -> x+1;}; + val _private={i64 x -> x-1;}; + val main={->Relay.relay_noInline(_sent);}; + return 0; + """,123,true,TypeInteger.BOT).driver(CodeGen.Phase.Opto); + FunNode sent=null, hidden=null, relay=null; + for( FunNode fun : code._linker ) if( fun!=null ) { + if( "_sent".equals(fun._name) ) sent=fun; + if( "_private".equals(fun._name) ) hidden=fun; + if( "relay_noInline".equals(fun._name) ) relay=fun; + } + assertNotNull(sent); assertNotNull(hidden); assertNotNull(relay); + TypeFunPtr result=(TypeFunPtr)((TypeTuple)relay.summary()._type).ret(); + assertTrue(XInt.bit(result._fidxs,sent.sig().fidx())); + assertFalse(XInt.bit(result._fidxs,hidden.sig().fidx())); + assertFalse(hidden.unknownCallers()); + } + + @Test public void testDeferredConditionalZero() { + for( String source : new String[]{ + "val test={->arg;}; return arg && (test()==1);", + "val test={->arg;}; return arg && (test()==1 && test()==1);", + "val test={->arg;}; return 0 && (test()==1);" + } ) { + CodeGen code; + try { code=compile(source); } + catch( Exception e ) { throw new AssertionError(source,e); } + assertEquals("0",Eval2.eval(code,0)); + assertNull(code._start.walk(n -> n instanceof ZeroNode ? n : null)); + } + } + + @Test public void testLateImportPrivateFunctions() throws Exception { + Path root=Files.createTempDirectory(Files.createDirectories(Path.of("build/objs")),"late-import-"); + Path src=Files.createDirectory(root.resolve("src")); + Path obj=Files.createDirectory(root.resolve("obj")); + new CodeGen(null,obj.toString(),null,"Library", + "val id_noInline={i64 x -> x;}; return 0;",123,true,TypeInteger.BOT) + .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false); + Files.writeString(src.resolve("Helper.smp"),"val _hidden={i64 x -> x;}; val value_noInline={->2;}; return 0;"); + var paths=new Ary<>(new String[]{obj.toString()}); + String draw="val _draw={i64 x -> x+1;};"; + String main="val main={->Library.id_noInline(1)+Helper.value_noInline();};"; + for( boolean early : new boolean[]{true,false} ) { + CodeGen code=new CodeGen(src.toString(),obj.toString(),paths,"Client", + (early ? draw+main : main+draw)+"return 0;",123,true,TypeInteger.BOT).parse(); + FunNode privateFun=null, library=null; + for( FunNode fun : code._linker ) if( fun!=null ) { + if( "_draw".equals(fun._name) ) privateFun=fun; + if( "class:Library.".equals(fun._name) ) library=fun; + } + assertNotNull(privateFun); + assertNotNull(library); + assertNotNull("The later discovered source unit was parsed",code._compunits.get("Helper")._src); + TypeMem saved=(TypeMem)library.summary()._state.mem(); + for( FunNode fun : code._linker ) + if( fun!=null && fun._compunit._src!=null ) + assertFalse(fun._name,XInt.bit(saved._escFs,fun.sig().fidx())); + int fidx=privateFun.sig().fidx(); + code.driver(CodeGen.Phase.Opto); + assertFalse(privateFun.unknownCallers()); + assertFalse(XInt.bit(((TypeTuple)code._start._type).mem()._escFs,fidx)); + } + } + + @Test public void testDeferredConditionalZeroLoop() { + CodeGen code=compile(""" + int count=0; + for( int i=0; i<4; i++ ) { + var inside = i>=2; + var border = inside && (i==2 || i==3); + if( border ) count++; + } + return count; + """); + assertEquals("2",Eval2.eval(code,0)); + assertNull(code._start.walk(n -> n instanceof ZeroNode ? n : null)); + } + } diff --git a/chapter33/src/test/java/com/seaofnodes/simple/codegen/ImportTestSupport.java b/chapter33/src/test/java/com/seaofnodes/simple/codegen/ImportTestSupport.java new file mode 100644 index 00000000..fe61580a --- /dev/null +++ b/chapter33/src/test/java/com/seaofnodes/simple/codegen/ImportTestSupport.java @@ -0,0 +1,74 @@ +package com.seaofnodes.simple.codegen; + +import com.seaofnodes.simple.type.*; +import com.seaofnodes.simple.util.BAOS; +import java.util.HashMap; +import static org.junit.Assert.*; + +public class ImportTestSupport { + public static void checkUniverses() { + new CodeGen("return 0;"); + GlobalBits file = new GlobalBits(), local = new GlobalBits(); + int excluded = file.next("Library"), included = file.next("Library"); + int shared = file.next("Shared"); + // Cross word boundaries, including bit 31 and bit 63. + for( int i=0; i<65; i++ ) local.next("Client"); + int localShared = local.next("Shared"); + int[] cofinite = XInt.dual(XInt.make(excluded)); + TypeFunPtr callback = TypeFunPtr.make(false,TypeFunPtr.TEMPTY,TypeInteger.BOT); + TypeFunPtr value = TypeFunPtr.make((byte)2,true,new Type[]{callback},TypeInteger.BOT,cofinite); + TypeTuple root = TypeTuple.make(TypeMem.START.makeFrom(cofinite,cofinite),TypeMem.BOT, + value,TypeRPC.BOT,TypeRPC.constant(included)); + Type.ImportedTypes decoded = unpack(root,file,local); + TypeTuple copy = (TypeTuple)decoded.values()[0]; + TypeTuple bounds = (TypeTuple)decoded.bounds()[0]; + TypeMem mem = (TypeMem)copy._types[0]; + TypeMem all = (TypeMem)copy._types[1]; + TypeFunPtr fun = (TypeFunPtr)copy._types[2]; + TypeRPC rpc = (TypeRPC)copy._types[3]; + int mappedExcluded=local.map(file,excluded), mappedIncluded=local.map(file,included); + assertEquals(localShared,local.map(file,shared)); + for( int[] bits : new int[][]{mem._escFs,mem._escAs,all._escFs,all._escAs,fun._fidxs,rpc._rpcs} ) { + for( int i=2; i ids = new HashMap<>(); + root.gather(ids); + Type[] types = new Type[ids.size()]; + for( var entry : ids.entrySet() ) types[entry.getValue()] = entry.getKey(); + BAOS out = new BAOS(); + HashMap strings = new HashMap<>(); + for( Type t : types ) t.packed(out,strings); + for( Type t : types ) + for( int i=0; i