diff options
Diffstat (limited to 'chapter33/src/test/java/com/seaofnodes')
| -rw-r--r-- | chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java | 88 | ||||
| -rw-r--r-- | chapter33/src/test/java/com/seaofnodes/simple/codegen/ImportTestSupport.java | 74 |
2 files changed, 162 insertions, 0 deletions
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.<clinit>".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<localShared; i++ ) assertFalse("Local bit "+i,XInt.bit(bits,i)); + assertTrue(XInt.bit(bits,mappedIncluded)); + assertTrue(XInt.bit(bits,localShared)); + assertTrue("Unknown remote tail remains infinite",XInt.bit(bits,10000)); + } + assertFalse(XInt.bit(mem._escFs,mappedExcluded)); + assertFalse(XInt.bit(mem._escAs,mappedExcluded)); + assertFalse(XInt.bit(fun._fidxs,mappedExcluded)); + assertEquals(mappedIncluded,((TypeRPC)copy._types[4]).rpc()); + assertSame("Declarations remain universal",TypeMem.BOT,bounds._types[1]); + assertSame(TypeRPC.BOT,bounds._types[3]); + assertSame(XInt.FULL,((TypeFunPtr)fun.arg(0))._fidxs); + assertSame("Decoding ALL never mutates the canonical RPC",XInt.FULL,TypeRPC.BOT._rpcs); + for( int i=0; i<GlobalBits.RESERVED; i++ ) + assertEquals(XInt.bit(cofinite,i),XInt.bit(mem._escFs,i)); + + // A re-export must preserve previous exclusions and narrow its new + // receiving universe too, including generic cofinite RPC encoding. + GlobalBits next = new GlobalBits(); + int newer = next.next("NextClient"); + TypeTuple again = (TypeTuple)unpack(copy,local,next).values()[0]; + TypeRPC againRPC = (TypeRPC)again._types[3]; + assertFalse(XInt.bit(againRPC._rpcs,newer)); + assertFalse(XInt.bit(againRPC._rpcs,next.map(local,2))); + assertTrue(XInt.bit(againRPC._rpcs,next.map(local,mappedIncluded))); + assertSame(XInt.FULL,TypeRPC.BOT._rpcs); + } + + private static Type.ImportedTypes unpack(Type root, GlobalBits file, GlobalBits local) { + Type.TAGOFFS(); + HashMap<Type,Integer> 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<String,Integer> strings = new HashMap<>(); + for( Type t : types ) t.packed(out,strings); + for( Type t : types ) + for( int i=0; i<t.nkids(); i++ ) out.packed4(ids.get(t.at(i))); + return Type.packed(new BAOS(out.toByteArray()),new String[]{""},types.length, + file,local,file,local,file,local); + } +} |
