diff options
Diffstat (limited to 'chapter33/src')
29 files changed, 272 insertions, 522 deletions
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/ClassInitDependencies.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/ClassInitDependencies.java index 9c954197..dd1f1454 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/ClassInitDependencies.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/ClassInitDependencies.java @@ -62,12 +62,12 @@ public class ClassInitDependencies { // Filter escapees by dead and not a class (class initializers are // reached through bodies, not directly callable function pointers). - int[] escaped = ((TypeTuple)code._stop._type).mem()._escFs; + TypeSet escaped = ((TypeTuple)code._stop._type).mem()._escFs; AryInt callees = new AryInt(); // An imported Stop can still carry FULL (only local bodies have edges) // so limit by existing FIDXs. int limit = code._linker._len; - for( int fidx = XInt.next(escaped,0,limit); fidx >= 0; fidx = XInt.next(escaped,fidx,limit) ) { + for( int fidx = escaped.nextKnown(0,limit); fidx >= 0; fidx = escaped.nextKnown(fidx,limit) ) { FunNode fun = code.link(fidx); // Discard dead bodies and external C functions. if( fun!=null && !fun.isClz() ) callees.push(fidx); } @@ -110,12 +110,12 @@ public class ClassInitDependencies { // Calls union all their fptr fidxs into the callee set if( cfg instanceof CallNode call && call.fptr()._type instanceof TypeFunPtr fp ) { - if( XInt.isHigh(fp.fidxs()) ) { + if( !fp.fidxs().isFinite() ) { iset.put(0,1); if( sites!=null && sites.atX(0)==null ) sites.setX(0,call._loc); } // Union call targets into the callee set - else for( int dstx = XInt.next(fp.fidxs(),0); dstx>=0; dstx=XInt.next(fp.fidxs(),dstx) ) { + else for( int dstx = fp.fidxs().next(0); dstx>=0; dstx=fp.fidxs().next(dstx) ) { iset.put(dstx,1); if( sites!=null && sites.atX(dstx)==null ) sites.setX(dstx,call._loc); } diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/GlobalBits.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/GlobalBits.java index acd2eee5..2606be08 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/GlobalBits.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/GlobalBits.java @@ -1,7 +1,7 @@ package com.seaofnodes.simple.codegen; import com.seaofnodes.simple.util.*; -import com.seaofnodes.simple.type.XInt; +import com.seaofnodes.simple.type.TypeSet; import java.util.HashMap; import java.util.Arrays; import java.util.Objects; @@ -30,15 +30,15 @@ public class GlobalBits { private int _local; // Identities not owned by this compilation. Reserved bits are not foreign. - public int[] foreign() { - int[] foreign = XInt.alloc((_local>>>5)+2); - Arrays.fill(foreign,1,foreign.length,-1); - for( int i = 0; i<_local; i++ ) { + public TypeSet foreign() { + // Preserve the existing cofinite set: known local identities are absent, + // known native identities and all not-yet-assigned identities are present. + int[] excluded = new int[_local]; + for( int i=0; i<_local; i++ ) { String owner = _local2Clz.at(i); - if( owner==null || !owner.startsWith("C:") ) - foreign[(i>>>5)+1] &= ~(1<<i); + if( owner==null || !owner.startsWith("C:") ) excluded[i] = i; } - return XInt.intern(foreign); + return TypeSet.make(true,true,excluded); } GlobalBits() { @@ -85,7 +85,7 @@ public class GlobalBits { // Bind a specific dense local index to {clz,order}. Used by both fresh // allocation and deserialization, where local indexes must not compact - // across holes because XInt bitsets are serialized against this space. + // across holes because TypeSet values are serialized against this space. private void bind( int local, String clz, int order ) { if( _local <= local ) _local = local+1; diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Iter.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Iter.java index 35bc62ac..72cca23b 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Iter.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Iter.java @@ -23,7 +23,7 @@ public abstract class Iter { if( t.isHigh() ) return t.dual(); if( t instanceof TypeMem mem ) - return TypeMem.make(mem._alias,iterImportReset(mem._t),mem._one,mem._clz,mem._final,XInt.FULL,XInt.FULL); + return TypeMem.make(mem._alias,iterImportReset(mem._t),mem._one,mem._clz,mem._final,TypeSet.FULL,TypeSet.FULL); if( t instanceof TypeMemPtr ) return t.makeStorage(); if( t instanceof TypeStruct ) diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Opto.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Opto.java index 4d6fb84d..63a39292 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Opto.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Opto.java @@ -26,8 +26,8 @@ abstract public class Opto { // get a fairly precise set of Call Graph edges between CallNodes and // FunNodes. public static void opto(CodeGen code) { - int[] foreign = code._fidxs.foreign(); - for( int fidx : code._externFunc.keySet() ) foreign = XInt.make(foreign,fidx); + TypeSet foreign = code._fidxs.foreign(); + for( int fidx : code._externFunc.keySet() ) foreign = foreign.with(fidx); code._start._foreign = TypeMem.START.makeFrom(foreign,code._aliases.foreign()); // Reset Types before running the worklist algorithm Ary<Type> oldTypes = new Ary<>(Type.class); @@ -160,12 +160,12 @@ abstract public class Opto { // into a Call, link the Call and Fun. private static void linkCG(CodeGen code, TypeFunPtr tfp, CallNode call) { if( tfp.nargs() != call.nargs() ) return; // Error calls hit this - int[] fidxs = tfp.fidxs(); + TypeSet fidxs = tfp.fidxs(); // An infinite target set uses the outside continuation. Escaped local // callbacks are called through Start; their effects feed that same // outside state. Do not attach unrelated same-arity function bodies. - if( XInt.isHigh(fidxs) ) return; - for( int fidx = XInt.next(fidxs,0); fidx >=0; fidx = XInt.next(fidxs,fidx) ) { + if( !fidxs.isFinite() ) return; + for( int fidx = fidxs.next(0); fidx >=0; fidx = fidxs.next(fidx) ) { // unlinkStart deliberately leaves uncalled functions in a // temporarily dead limbo. A newly discovered call revives them; // do not use the cleanup lookup which removes dead linker entries. @@ -184,16 +184,16 @@ abstract public class Opto { // and called by anybody. Add the CG edges. private static void linkStart( CodeGen code, TypeTuple tt ) { TypeMem tmem = (TypeMem)tt._types[1]; - int[] fidxs = tmem._escFs; - if( XInt.isHigh(fidxs) ) { + TypeSet fidxs = tmem._escFs; + if( !fidxs.isFinite() ) { // A cofinite set includes foreign identities but can exclude local // functions. Honor those exclusions just as in the finite case. for( FunNode fun : code._linker ) - if( fun != null && !fun.isDead() && XInt.bit(fidxs,fun.sig().fidx()) ) + if( fun != null && !fun.isDead() && fidxs.contains(fun.sig().fidx()) ) linkStart(code,fun); return; } - for( int fidx = XInt.next(fidxs,0); fidx >=0; fidx = XInt.next(fidxs,fidx) ) { + for( int fidx = fidxs.next(0); fidx >=0; fidx = fidxs.next(fidx) ) { FunNode fun = code._linker.atX(fidx); if( fun==null ) assert code._externFunc.containsKey(fidx); else linkStart(code,fun); diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Serialize.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Serialize.java index 9c43e029..ec135149 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/Serialize.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/Serialize.java @@ -104,9 +104,9 @@ public class Serialize { BAOS baos = new BAOS(); // A - Print the C0DE header; last byte is the version number. - // Format 2: gradual packed ints, UTF-8 strings, sparse MemMerge edges, - // and wider singleton function IDs. Reject old cached objects clearly. - baos.write('C').write('0').write('D').write('2'); + // Format 12: TypeSet defaults and finite exception words replace XInt. + // Earlier object formats must be rebuilt. + baos.write('C').write('0').write('D').write('C'); // Count unique Types var types = new HashMap<Type,Integer>(); @@ -131,7 +131,7 @@ public class Serialize { // Retention can happen on a path that never returns. The // unit's escape summary includes those native-call effects. TypeMem escaped = (TypeMem)((TypeTuple)fun._compunit._stop._type).mem(); - mem=mem.makeFrom(XInt.meet(mem._escFs,escaped._escFs),XInt.meet(mem._escAs,escaped._escAs)); + mem=mem.makeFrom(mem._escFs.meet(escaped._escFs),mem._escAs.meet(escaped._escAs)); state=state.makeFrom(1,mem); summaries.put(fun,state); state.gather(types); @@ -335,7 +335,7 @@ public class Serialize { Type.TAGOFFS(); // A - Read a header - if( bais.read()!='C' || bais.read()!='0' || bais.read()!='D' || bais.read()!='2' ) + if( bais.read()!='C' || bais.read()!='0' || bais.read()!='D' || bais.read()!='C' ) throw new IllegalArgumentException("Unsupported serialized IR format; rebuild the object file"); // B - Packed read of #strings, then strings @@ -368,7 +368,7 @@ public class Serialize { String[] strs = elf._strs; if( strs == null ) { // A - Read a header - if( bais.read() != 'C' || bais.read() != '0' || bais.read() != 'D' || bais.read() != '2' ) + if( bais.read() != 'C' || bais.read() != '0' || bais.read() != 'D' || bais.read() != 'C' ) throw new IllegalArgumentException( "Unsupported serialized IR format; rebuild the object file" ); // B - Packed read of #strings, then strings diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/BulkMemPhiNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/BulkMemPhiNode.java index f4d1e195..ca30d868 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/BulkMemPhiNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/BulkMemPhiNode.java @@ -4,7 +4,7 @@ import com.seaofnodes.simple.codegen.CodeGen; import com.seaofnodes.simple.codegen.GlobalBits; import com.seaofnodes.simple.type.Type; import com.seaofnodes.simple.type.TypeMem; -import com.seaofnodes.simple.type.XInt; +import com.seaofnodes.simple.type.TypeSet; import com.seaofnodes.simple.util.BAOS; import com.seaofnodes.simple.util.Utils; @@ -231,7 +231,7 @@ public class BulkMemPhiNode extends PhiNode { private boolean readySlice(Node precise) { TypeMem mem = (TypeMem)_type; TypeMem part = (TypeMem)precise._type; - if( !XInt.bit(mem._escAs,part._alias) || mem.meet(part).isa(mem) ) return true; + if( !mem._escAs.contains(part._alias) || mem.meet(part).isa(mem) ) return true; precise.setType(precise.compute()); if( mem.meet(precise._type).isa(mem) ) return true; addDep(precise); @@ -263,7 +263,7 @@ public class BulkMemPhiNode extends PhiNode { // An unescaped slice was excluded from that summary: include the // facts hidden in predecessor slices before wiring any cyclic Phis. TypeMem old = (TypeMem)_type; - if( !XInt.bit(old._escAs,alias) ) + if( !old._escAs.contains(alias) ) old = (TypeMem)old.meet(sliceBound(this,alias,new BitSet())); mphi.setType(TypeMem.make(alias,old._t,old._one,old._clz,false,old._escFs,old._escAs)); for( int i=1; i<nIns(); i++ ) diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/CallEndNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/CallEndNode.java index 108dd024..abe7953b 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/CallEndNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/CallEndNode.java @@ -90,7 +90,7 @@ public class CallEndNode extends CFGNode implements MultiNode { // The outside continuation reads Start directly: Call can remain // CONTROL while its possible returned identities change. - if( XInt.isHigh(tfp.fidxs()) ) { + if( !tfp.fidxs().isFinite() ) { TypeMem outside = CodeGen.CODE._start.outside(this).escapes(call.exposed(this)); TypeTuple state = TypeTuple.make(Type.CONTROL,outside,outside.outside(tfp.ret())); for( int i = 1; i<nIns(); i++ ) @@ -100,7 +100,7 @@ public class CallEndNode extends CFGNode implements MultiNode { // Check for making a "C" call int externs = 0; - for( int fidx = XInt.next(tfp.fidxs(),0); fidx >= 0; fidx = XInt.next(tfp.fidxs(),fidx) ) + for( int fidx = tfp.fidxs().next(0); fidx >= 0; fidx = tfp.fidxs().next(fidx) ) if( CodeGen.CODE.externFunc(fidx)!=null ) externs++; if( externs!=0 ) { // C calls have no Return edges. Simple calls have an extra self diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/CallNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/CallNode.java index 5506621b..a48f9115 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/CallNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/CallNode.java @@ -12,7 +12,7 @@ import com.seaofnodes.simple.type.TypeMemPtr; import com.seaofnodes.simple.type.TypeFunPtr; import com.seaofnodes.simple.type.TypeMem; import com.seaofnodes.simple.type.TypeRPC; -import com.seaofnodes.simple.type.XInt; +import com.seaofnodes.simple.type.TypeSet; import com.seaofnodes.simple.util.BAOS; import com.seaofnodes.simple.util.Utils; import java.util.HashMap; @@ -81,8 +81,8 @@ public class CallNode extends CFGNode { // Call is to an externally supplied code public boolean external() { if( !(fptr()._type instanceof TypeFunPtr tfp) ) return false; - if( XInt.isHigh(tfp.fidxs()) ) return true; - for( int fidx=XInt.next(tfp.fidxs(),-1); fidx>=0; fidx=XInt.next(tfp.fidxs(),fidx) ) + if( !tfp.fidxs().isFinite() ) return true; + for( int fidx=tfp.fidxs().next(-1); fidx>=0; fidx=tfp.fidxs().next(fidx) ) if( CodeGen.CODE.externFunc(fidx)!=null ) return true; return false; } @@ -155,11 +155,11 @@ public class CallNode extends CFGNode { // also the CallEnd points to the Return. Node progress = null; if( fptr()._type instanceof TypeFunPtr tfp && tfp.nargs() == nargs() ) { - // If fidxs is negative, then infinite unknown functions - int[] fidxs = tfp.fidxs(); - if( !XInt.isHigh(fidxs) ) { + // Only finite target sets can be enumerated to link known functions. + TypeSet fidxs = tfp.fidxs(); + if( fidxs.isFinite() ) { // Walk the bits and link - for( int fidx = XInt.next(fidxs,0); fidx > 0; fidx = XInt.next(fidxs,fidx) ) { + for( int fidx = fidxs.next(0); fidx > 0; fidx = fidxs.next(fidx) ) { FunNode fun = CodeGen.CODE.link(fidx); if( fun==null ) addDepForwards(CodeGen.CODE._start); if( fun!=null && !addDep(fun)._folding && !linked(fun) ) diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/EscapeNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/EscapeNode.java index a8a6151c..3c759c48 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/EscapeNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/EscapeNode.java @@ -93,8 +93,8 @@ public class EscapeNode extends TypeNode { // edge, not publication to unknown callers. Keep the public escape sets. if( _borrow ) return TypeMem.make(fld._alias,t,false,false,false,mpub._escFs,mpub._escAs); - int[] fidxes = XInt.meet(mpriv._escFs, mpub._escFs); - int[] aliases = XInt.meet(mpriv._escAs, mpub._escAs); + TypeSet fidxes = mpriv._escFs.meet(mpub._escFs); + TypeSet aliases = mpriv._escAs.meet(mpub._escAs); // Keep escapes from the actual private value before narrowing it to // the declaration. Selecting its Store from a MemMerge must preserve // this result, including Scalar.BOT's unknown pointer escapes. diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/GlobalEscapeNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/GlobalEscapeNode.java index 740b0895..4236cb6f 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/GlobalEscapeNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/GlobalEscapeNode.java @@ -28,7 +28,7 @@ public class GlobalEscapeNode extends Node { // private initializers. Its arguments are unavailable here. if( fun._type==Type.CONTROL ) for( int fidx : fun.summary()._classDependencyFIDXs ) - if( fidx!=0 ) xmem = xmem.makeFrom(XInt.meet(xmem._escFs,XInt.make(fidx)),xmem._escAs); + if( fidx!=0 ) xmem = xmem.makeFrom(xmem._escFs.meet(TypeSet.of(fidx)),xmem._escAs); } return xmem; } diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/IfNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/IfNode.java index ad8de653..c46d88a9 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/IfNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/IfNode.java @@ -44,7 +44,7 @@ public class IfNode extends CFGNode implements MultiNode { // If constant is 0 then false branch is reachable // Else true branch is reachable if( t.isConstant() ) - return (t==Type.NIL || t==TypeInteger.ZERO || (t instanceof TypeFunPtr tfp && tfp._fidxs== XInt.EMPTY) ) ? TypeTuple.IF_FALSE : TypeTuple.IF_TRUE; + return (t==Type.NIL || t==TypeInteger.ZERO || (t instanceof TypeFunPtr tfp && tfp._fidxs== TypeSet.EMPTY) ) ? TypeTuple.IF_FALSE : TypeTuple.IF_TRUE; // If adding a zero makes a difference, the predicate must not have a zero/null if( !t.makeZero().isa(t) ) return TypeTuple.IF_TRUE; diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/MemMergeNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/MemMergeNode.java index a077e14b..03cf4d36 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/MemMergeNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/MemMergeNode.java @@ -138,7 +138,7 @@ public class MemMergeNode extends Node { TypeMem mem = defmem; for( int i=2; i<nIns(); i++ ) { // Has this alias escaped? - if( XInt.bit(mem._escAs,aliasAt(i)) && + if( mem._escAs.contains(aliasAt(i)) && in(i) != null && !in(i)._type.isHigh() ) { mem = (TypeMem)mem.meet(in(i)._type); } diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/StartNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/StartNode.java index 36583884..ab2dd220 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/node/StartNode.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/node/StartNode.java @@ -62,7 +62,7 @@ public class StartNode extends LoopNode implements MultiNode { return !_type.isHigh(); // TOP never escapes, BOTTOM always escapes TypeMem tmem = (TypeMem)tt._types[1]; if( tmem == TypeMem.TOP ) return false; - return XInt.bit(tmem._escFs,fidx); + return tmem._escFs.contains(fidx); } @Override public TypeTuple compute() { diff --git a/chapter33/src/main/java/com/seaofnodes/simple/parser/MethodAccess.java b/chapter33/src/main/java/com/seaofnodes/simple/parser/MethodAccess.java index 099ba036..9577d691 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/parser/MethodAccess.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/parser/MethodAccess.java @@ -81,8 +81,8 @@ final class MethodAccess { if( !(type instanceof TypeFunPtr sig) || arg >= sig.nargs() ) { _writes = true; return; } if( arg == 0 && sig.arg(0) == TypePtr.PTR ) return; if( arg == 0 ) { - if( XInt.isHigh(sig.fidxs()) ) { _writes = true; return; } - for( int fidx = XInt.next(sig.fidxs(),-1); fidx >= 0; fidx = XInt.next(sig.fidxs(),fidx) ) { + if( !sig.fidxs().isFinite() ) { _writes = true; return; } + for( int fidx = sig.fidxs().next(-1); fidx >= 0; fidx = sig.fidxs().next(fidx) ) { MethodAccess local = methods.get(fidx); if( local != null ) _calls.add(local); else { diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/Type.java b/chapter33/src/main/java/com/seaofnodes/simple/type/Type.java index 0b8d1a34..0cf8fd09 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/Type.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/Type.java @@ -250,6 +250,7 @@ public class Type /*implements Cloneable*/ { VISIT.put(0,CONTROL); // Defeat assert for( Iterator<Type> it = INTERN.keySet().iterator(); it.hasNext(); ) { Type t = it.next(); + if( t instanceof TypeStruct ts ) ts.clearSummaryCaches(); if( !INTERN0.containsKey(t) ) { t._terned=false; t.free(t); } it.remove(); @@ -619,15 +620,15 @@ public class Type /*implements Cloneable*/ { return types; } - private static int[] remapBits(GlobalBits fileBits, GlobalBits bits, int[] fileLocals, int firstMapped ) { - if( fileLocals == XInt.EMPTY || fileLocals == XInt.FULL ) + private static TypeSet remapBits(GlobalBits fileBits, GlobalBits bits, TypeSet fileLocals, int firstMapped ) { + if( fileLocals == TypeSet.EMPTY || fileLocals == TypeSet.FULL ) return fileLocals; - boolean complement = XInt.isHigh(fileLocals); - if( complement ) fileLocals = XInt.dual(fileLocals); - int[] locals = XInt.EMPTY; - for( int fileLocal = XInt.next(fileLocals,-1); fileLocal >=0; fileLocal = XInt.next(fileLocals,fileLocal) ) - locals = XInt.make(locals,fileLocal < firstMapped ? fileLocal : bits.map(fileBits,fileLocal)); - return complement ? XInt.dual(locals) : locals; + boolean complement = !fileLocals.isFinite(); + if( complement ) fileLocals = fileLocals.dual(); + TypeSet locals = TypeSet.EMPTY; + for( int fileLocal = fileLocals.next(-1); fileLocal >=0; fileLocal = fileLocals.next(fileLocal) ) + locals = locals.with(fileLocal < firstMapped ? fileLocal : bits.map(fileBits,fileLocal)); + return complement ? locals.dual() : locals; } diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeFunPtr.java b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeFunPtr.java index c6f53022..1f033ec0 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeFunPtr.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeFunPtr.java @@ -20,23 +20,22 @@ public class TypeFunPtr extends TypeNil { public Type[] _sig; public Type _ret; boolean _open; // Extra args are true:scalar BOT, false:scalar TOP - // Set of functions. Bit zero is used for mutable/immutable safety. Sign - // bit repeats infinitely. - public int[] _fidxs; + // Interned set of possible functions, including finite or cofinite targets. + public TypeSet _fidxs; private static final Ary<TypeFunPtr> FREE = new Ary<>(TypeFunPtr.class); private TypeFunPtr(byte nil) { super(TFUNPTR,nil); } - private TypeFunPtr init(byte nil, boolean open, Type[] sig, Type ret, int[] fidxs ) { + private TypeFunPtr init(byte nil, boolean open, Type[] sig, Type ret, TypeSet fidxs ) { assert sig != null; _nil = nil; _open = open; _sig = sig; _ret = ret; assert _fidxs == null; - _fidxs= XInt.intern(fidxs); + _fidxs= fidxs; return this; } - static TypeFunPtr malloc( byte nil, boolean open, Type[] sig, Type ret, int[] fidxs ) { + static TypeFunPtr malloc( byte nil, boolean open, Type[] sig, Type ret, TypeSet fidxs ) { return (FREE.isEmpty() ? new TypeFunPtr(nil) : FREE.pop()).init(nil,open,sig,ret,fidxs); } @Override TypeFunPtr free(Type t) { @@ -53,14 +52,14 @@ public class TypeFunPtr extends TypeNil { @Override boolean isFree() { return _sig==null; } // All fields directly listed - public static TypeFunPtr make( byte nil, boolean open, Type[] sig, Type ret, int[] fidxs ) { + public static TypeFunPtr make( byte nil, boolean open, Type[] sig, Type ret, TypeSet fidxs ) { TypeFunPtr fun = malloc(nil,open,sig,ret,fidxs); TypeFunPtr f2 = fun.intern(); if( f2==fun ) return fun; return VISIT.isEmpty() ? f2.free(fun) : f2.delayFree(fun); } // Trim trailing args which match the default. - private static TypeFunPtr makeAdjusted( byte nil, boolean open, Type[] sig, Type ret, int[] fidxs ) { + private static TypeFunPtr makeAdjusted( byte nil, boolean open, Type[] sig, Type ret, TypeSet fidxs ) { Type def = open ? TypeScalar.BOT : TypeScalar.TOP; int nargs = sig.length; while( nargs > 0 && sig[nargs-1] == def ) @@ -69,9 +68,9 @@ public class TypeFunPtr extends TypeNil { sig = nargs == 0 ? ARG_EMPTY : Arrays.copyOf(sig,nargs); return make(nil,open,sig,ret,fidxs); } - public static TypeFunPtr make( boolean nil, Type[] sig, Type ret ) { return makeAdjusted((byte)(nil ? 3 : 2),true,sig,ret, XInt.FULL); } + public static TypeFunPtr make( boolean nil, Type[] sig, Type ret ) { return makeAdjusted((byte)(nil ? 3 : 2),true,sig,ret, TypeSet.FULL); } public static TypeFunPtr make1( byte nil, boolean open, Type[] sig, Type ret, int fidx ) { - return makeAdjusted(nil,open,sig,ret,XInt.make(fidx)); + return makeAdjusted(nil,open,sig,ret,TypeSet.of(fidx)); } @@ -79,8 +78,8 @@ public class TypeFunPtr extends TypeNil { @Override TypeFunPtr makeFrom( byte nil ) { return nil ==_nil ? this : make( nil,_open,_sig,_ret, _fidxs); } public TypeFunPtr makeFrom( Type ret ) { return ret ==_ret ? this : make( _nil,_open,_sig, ret, _fidxs); } public TypeFunPtr makeFrom( int fidx ) { return make1(_nil,_open,_sig,_ret,fidx); } - public TypeFunPtr restrictFIDXs(int[] fidxs) { - int[] ids = XInt.dual(XInt.meet(XInt.dual(_fidxs),XInt.dual(fidxs))); + public TypeFunPtr restrictFIDXs(TypeSet fidxs) { + TypeSet ids = _fidxs.join(fidxs); return ids==_fidxs ? this : make(_nil,_open,_sig,_ret,ids); } public TypeFunPtr makeFrom( Type arg, int idx ) { @@ -95,19 +94,19 @@ public class TypeFunPtr extends TypeNil { static final Type[] TINT = new Type[]{TypeInteger.BOT}; static final Type[] TINTMEM = new Type[]{TypeInteger.BOT,TypeFloat.F32}; static final Type[] TINTINT = new Type[]{TypeInteger.BOT,TypeInteger.BOT}; - public static TypeFunPtr BOT = make((byte)3,true,TEMPTY,Type.BOTTOM, XInt.FULL); - public static TypeFunPtr TEST = make((byte)2,true,TINTMEM,TypeInteger.BOT, XInt.SET1); - public static TypeFunPtr TEST0 = make((byte)3,true,TINTMEM,TypeInteger.BOT, XInt.SET3); - public static TypeFunPtr MAIN = make((byte)3,true,TINT ,Type.BOTTOM, XInt.FULL); // Main can return anything - public static TypeFunPtr CALLOC= make((byte)3,true,TINTINT,TypeMemPtr .BOT, XInt.FULL); + public static TypeFunPtr BOT = make((byte)3,true,TEMPTY,Type.BOTTOM, TypeSet.FULL); + public static TypeFunPtr TEST = make((byte)2,true,TINTMEM,TypeInteger.BOT, TypeSet.SET1); + public static TypeFunPtr TEST0 = make((byte)3,true,TINTMEM,TypeInteger.BOT, TypeSet.SET3); + public static TypeFunPtr MAIN = make((byte)3,true,TINT ,Type.BOTTOM, TypeSet.FULL); // Main can return anything + public static TypeFunPtr CALLOC= make((byte)3,true,TINTINT,TypeMemPtr .BOT, TypeSet.FULL); public static void gather(ArrayList<Type> ts) { ts.add(TEST); ts.add(TEST0); ts.add(BOT); ts.add(MAIN); ts.add(CALLOC); - ts.add(make((byte)2,false,TINT,TypeInteger.I32, XInt.SET1)); - ts.add(make((byte)2,true ,TINT,TypeInteger.BOT, XInt.SET3)); + ts.add(make((byte)2,false,TINT,TypeInteger.I32, TypeSet.SET1)); + ts.add(make((byte)2,true ,TINT,TypeInteger.BOT, TypeSet.SET3)); ts.add(make1((byte)2,true, new Type[]{TypeInteger.BOT}, TypeInteger.BOT, 65)); @@ -148,7 +147,7 @@ public class TypeFunPtr extends TypeNil { if( nargs != args.length ) args = nargs == 0 ? ARG_EMPTY : Arrays.copyOf(args,nargs); - return make(xmeet0(that), open, args, _ret.meet(that._ret), XInt.meet(_fidxs,that._fidxs)); + return make(xmeet0(that), open, args, _ret.meet(that._ret), _fidxs.meet(that._fidxs)); } static Type[] xmeet( Type[] ts0, Type[] ts1) { @@ -160,7 +159,7 @@ public class TypeFunPtr extends TypeNil { return ts; } - @Override TypeFunPtr xdual() { return malloc(dual0(), !_open, xdual(_sig), _ret.dual(), XInt.dual(_fidxs)); } + @Override TypeFunPtr xdual() { return malloc(dual0(), !_open, xdual(_sig), _ret.dual(), _fidxs.dual()); } static Type[] xdual(Type[] ts) { Type[] dual = new Type[ts.length]; for( int i=0; i<ts.length; i++ ) @@ -172,7 +171,7 @@ public class TypeFunPtr extends TypeNil { if( _dual!=null ) return dual(); assert !_terned; Type[] sigs = new Type[_sig.length]; - TypeFunPtr d = malloc(dual0(),!_open,sigs,null, XInt.dual(_fidxs)); + TypeFunPtr d = malloc(dual0(),!_open,sigs,null, _fidxs.dual()); (_dual = d)._dual = this; // Cross link duals // Return rdual d._ret = _ret._terned ? _ret.dual() : _ret.rdual(); @@ -190,9 +189,9 @@ public class TypeFunPtr extends TypeNil { return make((byte)3,_open,_sig,_ret,_fidxs); } - @Override public boolean isHigh() { return _nil <= 1 || (_nil==2 && _fidxs== XInt.EMPTY); } + @Override public boolean isHigh() { return _nil <= 1 || (_nil==2 && _fidxs== TypeSet.EMPTY); } - @Override public boolean _isConstant() { return (_nil==2 && XInt.isConstant(_fidxs)) || (_nil==3 && _fidxs== XInt.EMPTY); } + @Override public boolean _isConstant() { return (_nil==2 && _fidxs.isConstant()) || (_nil==3 && _fidxs== TypeSet.EMPTY); } @Override boolean _isFinal() { return true; } @Override TypeFunPtr _makeStorage() { return this; } @@ -221,13 +220,13 @@ public class TypeFunPtr extends TypeNil { @Override public int log_size() { return 2; } // (1<<2)==4-byte pointers public Type arg(int i) { return _sig[i]; } - public int[] fidxs() { return _fidxs; } + public TypeSet fidxs() { return _fidxs; } public Type ret() { return _ret; } public int nargs() { return _sig.length; } - public int fidx() { return XInt.onlyBit(_fidxs); } + public int fidx() { return _fidxs.onlyBit(); } public int nfcns() { - if( XInt.isHigh(_fidxs) ) return Integer.MAX_VALUE; // Infinite choices - return XInt.bitCount(_fidxs); + if( !_fidxs.isFinite() ) return Integer.MAX_VALUE; // Infinite choices + return _fidxs.bitCount(); } @@ -254,23 +253,23 @@ public class TypeFunPtr extends TypeNil { } else { baos.write(TAGOFFS[_type] + 6 + (_nil==2 ? 0 : 1)); baos.packed1(nargs()); - XInt.packed(baos,_fidxs); + _fidxs.packed(baos); } } static TypeFunPtr packed( int tag, BAOS bais ) { if( tag < 6 ) { int fidx = bais.packed2(); - return malloc((byte)2,true,new Type[tag],null, XInt.make(fidx)); + return malloc((byte)2,true,new Type[tag],null, TypeSet.of(fidx)); } byte nil = (byte)(tag==6 ? 2 : 3); int nargs = bais.packed1(); - int[] fidxs = XInt.packed(bais); + TypeSet fidxs = TypeSet.unpack(bais); return malloc(nil,true,new Type[nargs],null,fidxs); } @Override int hash() { - long hash = _ret.hashCode() ^ XInt.hash(_fidxs) ^ super.hash(); + long hash = _ret.hashCode() ^ _fidxs.hashCode() ^ super.hash(); for( Type arg : _sig ) hash = hash*19 ^ arg.hashCode(); return Utils.fold(hash); @@ -334,7 +333,7 @@ public class TypeFunPtr extends TypeNil { } String printFIDX() { String tilde = isHigh() ? "~" : ""; - String fidx = XInt.str(_fidxs); + String fidx = _fidxs.str(); return tilde+fidx; } diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeMem.java b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeMem.java index 1c83ddd7..a56aee69 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeMem.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeMem.java @@ -24,15 +24,15 @@ public class TypeMem extends Type { // Escaped aliases and functions - escaped into this memory. These are // reachable from some chain of indirections from `_t`. Bit zero is // used for mutable/immutable safety. Sign bit repeats infinitely. - public int[] _escFs; - public int[] _escAs; + public TypeSet _escFs; + public TypeSet _escAs; static public int CNT; private static final Ary<TypeMem> FREE = new Ary<>(TypeMem.class); private TypeMem() { super(TMEM); CNT++; } - private TypeMem init(int alias, Type t, boolean one, boolean clz, boolean xfinal, int[] fidxs, int[] aliases) { + private TypeMem init(int alias, Type t, boolean one, boolean clz, boolean xfinal, TypeSet fidxs, TypeSet aliases) { _alias = alias; _t = t; _one = one; @@ -43,8 +43,8 @@ public class TypeMem extends Type { _final = alias==1 ? one : xfinal; assert _escFs == null && _escAs == null; - _escFs = XInt.intern(fidxs); - _escAs = XInt.intern(aliases); + _escFs = fidxs; + _escAs = aliases; return this; } @Override TypeMem free(Type t) { @@ -63,12 +63,12 @@ public class TypeMem extends Type { } @Override boolean isFree() { return _alias == -99; } - public static TypeMem malloc(int alias, Type t, boolean one, boolean clz, boolean xfinal, int[] fidxs, int[] aliases) { + public static TypeMem malloc(int alias, Type t, boolean one, boolean clz, boolean xfinal, TypeSet fidxs, TypeSet aliases) { return (FREE.isEmpty() ? new TypeMem() : FREE.pop()).init(alias,t,one,clz,xfinal,fidxs,aliases); } - public static TypeMem make(int alias, Type t, boolean one, boolean clz, boolean xfinal, int[] fidxs, int[] aliases) { - if( fidxs ==null ) fidxs = XInt.EMPTY; - if( aliases==null ) aliases= XInt.EMPTY; + public static TypeMem make(int alias, Type t, boolean one, boolean clz, boolean xfinal, TypeSet fidxs, TypeSet aliases) { + if( fidxs ==null ) fidxs = TypeSet.EMPTY; + if( aliases==null ) aliases= TypeSet.EMPTY; TypeMem f = malloc(alias,t,one,clz,xfinal, fidxs, aliases); TypeMem f2 = f.intern(); if( f2==f ) return f; @@ -76,7 +76,7 @@ public class TypeMem extends Type { } public static TypeMem make(int alias, Type t) { return make(alias,t,false,false,false,null,null); } // Make a private memory - public static TypeMem makePrivate(Type t) { return make(1,t,true,false,false,escapeFIDX(XInt.EMPTY,t), escapeAlias(XInt.EMPTY,t)); } + public static TypeMem makePrivate(Type t) { return make(1,t,true,false,false,escapeFIDX(TypeSet.EMPTY,t), escapeAlias(TypeSet.EMPTY,t)); } public TypeMem makeFrom(int alias) { return make(alias,_t,_one,_clz,_final,_escFs,_escAs); } // Widen numeric fields at cyclic memory merges just as scalar Phis do. @@ -88,7 +88,7 @@ public class TypeMem extends Type { wide = wide.replace(fld.makeFrom(ti.same_but_slightly_wider_than())); return wide==ts ? this : make(_alias,wide,_one,_clz,_final,_escFs,_escAs); } - public TypeMem makeFrom(int[] fidxes, int[] aliases) { + public TypeMem makeFrom(TypeSet fidxes, TypeSet aliases) { return make(_alias,_t,_one,_clz,_final,fidxes,aliases); } // Existing escapes plus `t` escapes @@ -105,7 +105,7 @@ public class TypeMem extends Type { // types are resolved. Ordinary unknown values still require FULL. public TypeMem escapesFrom(Type t, TypeMem defaults) { if( opaque(t) ) - return makeFrom(XInt.meet(_escFs,defaults._escFs),XInt.meet(_escAs,defaults._escAs)); + return makeFrom(_escFs.meet(defaults._escFs),_escAs.meet(defaults._escAs)); return escapesFrom(t); } public static boolean opaque(Type t) { @@ -122,9 +122,9 @@ public class TypeMem extends Type { return declared instanceof TypeFunPtr fun ? fun.restrictFIDXs(_escFs) : declared; } - public static final TypeMem TOP = make(1, Type.TOP ,true ,true, true , XInt.EMPTY, XInt.EMPTY); - public static final TypeMem BOT = make(1, Type.BOTTOM,false,false,true , XInt.FULL , XInt.FULL ); - public static final TypeMem START= make(1,Type.BOTTOM,false,false,false, XInt.EMPTY, XInt.EMPTY); + public static final TypeMem TOP = make(1, Type.TOP ,true ,true, true , TypeSet.EMPTY, TypeSet.EMPTY); + public static final TypeMem BOT = make(1, Type.BOTTOM,false,false,true , TypeSet.FULL , TypeSet.FULL ); + public static final TypeMem START= make(1,Type.BOTTOM,false,false,false, TypeSet.EMPTY, TypeSet.EMPTY); public static final TypeMem SELF_MEM = make(1, TypeStruct.BOT); public static void gather(ArrayList<Type> ts) { @@ -133,8 +133,8 @@ public class TypeMem extends Type { ts.add(BOT); ts.add(SELF_MEM); ts.add(START); - ts.add(make(2,TypeInteger.ZERO,true,false,true,XInt.EMPTY,XInt.make(2))); - ts.add(make(2,TypeInteger.BOT,false,false,false,XInt.EMPTY,XInt.FULL)); + ts.add(make(2,TypeInteger.ZERO,true,false,true,TypeSet.EMPTY,TypeSet.of(2))); + ts.add(make(2,TypeInteger.BOT,false,false,false,TypeSet.EMPTY,TypeSet.FULL)); } @Override @@ -150,8 +150,8 @@ public class TypeMem extends Type { : that._alias==1 && that._t.isHigh() ? this._alias // that is high-1 alias, so take this : 1; Type mt = _t.meet(that._t); - int[] fidxs = XInt.meet( _escFs, that._escFs ); - int[] aliases= XInt.meet( _escAs, that._escAs ); + TypeSet fidxs = _escFs.meet(that._escFs); + TypeSet aliases= _escAs.meet(that._escAs); // Singleton and class facts are kept only when both sides agree. // An aggregate has no field-wide finality. If meeting with it selects // a precise alias, retain that slice's finality without mixing in the @@ -161,12 +161,12 @@ public class TypeMem extends Type { } @Override - Type xdual() { return malloc(_alias,_t.dual(),!_one,!_clz,!_final, XInt.dual(_escFs), XInt.dual(_escAs)); } + Type xdual() { return malloc(_alias,_t.dual(),!_one,!_clz,!_final, _escFs.dual(), _escAs.dual()); } @Override TypeMem rdual() { if( _dual!=null ) return dual(); assert !_terned; - TypeMem d = malloc(_alias,null,!_one,!_clz,!_final, XInt.dual(_escFs), XInt.dual(_escAs)); + TypeMem d = malloc(_alias,null,!_one,!_clz,!_final, _escFs.dual(), _escAs.dual()); (_dual = d)._dual = this; // Cross link duals d._t = _t._terned ? _t.dual() : _t.rdual(); return d; @@ -185,7 +185,7 @@ public class TypeMem extends Type { return make(_alias,_t._upgradeType(TYPES),_one,_clz,_final,_escFs,_escAs); } - @Override int hash() { return 9876543 + _alias + _t.hashCode() + (_one ? 8196 : 0) + (_clz ? 4098 : 0) + (_final ? 16392 : 0) + XInt.hash(_escFs) + XInt.hash(_escAs); } + @Override int hash() { return 9876543 + _alias + _t.hashCode() + (_one ? 8196 : 0) + (_clz ? 4098 : 0) + (_final ? 16392 : 0) + _escFs.hashCode() + _escAs.hashCode(); } @Override boolean eq(Type t) { TypeMem that = (TypeMem) t; // Invariant @@ -218,8 +218,8 @@ public class TypeMem extends Type { baos.packed1(_alias<=255 ? _alias : 0); if( _alias>255 ) baos.packed4(_alias); } - XInt.packed(baos,_escFs); - XInt.packed(baos,_escAs); + _escFs.packed(baos); + _escAs.packed(baos); } static TypeMem packed( int tag, BAOS bais ) { int alias = (tag&1)==0 ? 1 : bais.packed1(); @@ -227,8 +227,8 @@ public class TypeMem extends Type { boolean xfinal= (tag&2)==2; boolean one = (tag&12)!=0; boolean clz = (tag&4)==4; - int[] escFs = XInt.packed(bais); - int[] escAs = XInt.packed(bais); + TypeSet escFs = TypeSet.unpack(bais); + TypeSet escAs = TypeSet.unpack(bais); return malloc(alias,null,one,clz,xfinal,escFs,escAs); } @@ -241,8 +241,8 @@ public class TypeMem extends Type { if( _alias==0 ) return sb.p(_t._type==TTOP ? "TOP" : "BOT"); sb.p(_alias).p(":["); _t.print(sb,visit,html).p(","); - XInt.str(sb,_escFs).p(","); - return XInt.str(sb,_escAs).p("]"); + _escFs.str(sb).p(","); + return _escAs.str(sb).p("]"); } @Override public String str() { @@ -253,38 +253,38 @@ public class TypeMem extends Type { if( _one ) sb.p(_clz ? 'z' : '-'); if( _alias==0 ) return sb.p(_t._type==TTOP ? "TOP" : "BOT").toString(); sb.p(_alias).p(":[").p(_t.str()).p(","); - XInt.str(sb,_escFs).p(","); - return XInt.str(sb,_escAs).p("]").toString(); + _escFs.str(sb).p(","); + return _escAs.str(sb).p("]").toString(); } // Tracking bulk escaped aliases and fidxs // Escape all FIDXs from t - private static int[] escapeFIDX( int[] xs, Type t ) { + private static TypeSet escapeFIDX( TypeSet xs, Type t ) { return switch(t) { case TypeMemPtr tmp -> escapeFIDX(xs,tmp._obj); case TypeStruct ts -> ts._open && !ts.isAry() - ? XInt.FULL // Open structs can have any fields - : XInt.meet(xs,ts.fidxs()); - case TypeFunPtr tfp -> XInt.meet(xs,tfp.fidxs()); - case TypeMem mem -> XInt.meet(xs,mem._escFs); + ? TypeSet.FULL // Open structs can have any fields + : xs.meet(ts.fidxs()); + case TypeFunPtr tfp -> xs.meet(tfp.fidxs()); + case TypeMem mem -> xs.meet(mem._escFs); // An abstract pointer or scalar can refine to a concrete escaped // object or function. Until then it must allow all of their bits. - case TypePtr ptr -> ptr.isHigh() ? xs : XInt.FULL; - case Type tt -> tt == BOTTOM || tt == TypeScalar.BOT ? XInt.FULL : xs; + case TypePtr ptr -> ptr.isHigh() ? xs : TypeSet.FULL; + case Type tt -> tt == BOTTOM || tt == TypeScalar.BOT ? TypeSet.FULL : xs; }; } - private static int[] escapeAlias( int[] xs, Type t ) { + private static TypeSet escapeAlias( TypeSet xs, Type t ) { return switch(t) { case TypeMemPtr tmp -> escapeAlias(xs,tmp._obj); case TypeStruct ts -> ts._open && !ts.isAry() - ? XInt.FULL // Open structs can have any fields - : XInt.meet(xs,ts.aliases()); - case TypeMem mem -> XInt.meet(xs,mem._escAs); - case TypePtr ptr -> ptr.isHigh() ? xs : XInt.FULL; - case Type tt -> tt == BOTTOM || tt == TypeScalar.BOT ? XInt.FULL : xs; + ? TypeSet.FULL // Open structs can have any fields + : xs.meet(ts.aliases()); + case TypeMem mem -> xs.meet(mem._escAs); + case TypePtr ptr -> ptr.isHigh() ? xs : TypeSet.FULL; + case Type tt -> tt == BOTTOM || tt == TypeScalar.BOT ? TypeSet.FULL : xs; }; } } diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeRPC.java b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeRPC.java index 8a12f50a..9a28fdeb 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeRPC.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeRPC.java @@ -9,17 +9,17 @@ import java.util.*; */ public class TypeRPC extends Type { - // A set of CallEndNode IDs (or StopNode); commonly just one. High-bit - // XInt sets represent complements/infinite sets. - public int[] _rpcs; + // A set of CallEndNode IDs (or StopNode); commonly just one. + // TypeSet defaults represent complements/infinite sets. + public TypeSet _rpcs; private static final Ary<TypeRPC> FREE = new Ary<>(TypeRPC.class); - private TypeRPC init(int[] rpcs) { _rpcs = XInt.intern(rpcs); return this; } + private TypeRPC init(TypeSet rpcs) { _rpcs = rpcs; return this; } private TypeRPC() { super(TRPC); } - private static TypeRPC malloc(int[] rpcs) { + private static TypeRPC malloc(TypeSet rpcs) { return (FREE.isEmpty() ? new TypeRPC() : FREE.pop()).init(rpcs); } - private static TypeRPC make(int[] rpcs) { + private static TypeRPC make(TypeSet rpcs) { TypeRPC rpc = malloc(rpcs); TypeRPC t2 = rpc.intern(); return t2==rpc ? rpc : t2.free(rpc); @@ -35,18 +35,18 @@ public class TypeRPC extends Type { @Override boolean isFree() { return _rpcs==null; } public static TypeRPC constant(int cend) { - return make(XInt.make(cend)); + return make(TypeSet.of(cend)); } public int rpc() { - return XInt.onlyBit(_rpcs); + return _rpcs.onlyBit(); } - public static final TypeRPC BOT = make(XInt.FULL); + public static final TypeRPC BOT = make(TypeSet.FULL); private static final TypeRPC TEST2 = constant(2); private static final TypeRPC TEST3 = constant(2); - public static final TypeRPC EMPTY = make(XInt.EMPTY); + public static final TypeRPC EMPTY = make(TypeSet.EMPTY); public static void gather(ArrayList<Type> ts) { ts.add(BOT); ts.add(TEST2); ts.add(TEST3); } @@ -54,25 +54,25 @@ public class TypeRPC extends Type { @Override public String str() { - if( _rpcs == XInt.FULL ) return "$[ALL]"; - if( _rpcs == XInt.EMPTY ) return "$[]"; - return "$"+XInt.str(_rpcs); + if( _rpcs == TypeSet.FULL ) return "$[ALL]"; + if( _rpcs == TypeSet.EMPTY ) return "$[]"; + return "$"+_rpcs.str(); } @Override public TypeRPC xmeet(Type other) { TypeRPC rpc = (TypeRPC)other; - return make(XInt.meet(_rpcs,rpc._rpcs)); + return make(_rpcs.meet(rpc._rpcs)); } - @Override TypeRPC xdual() { return malloc(XInt.dual(_rpcs)); } + @Override TypeRPC xdual() { return malloc(_rpcs.dual()); } @Override Type rdual() { TypeRPC d = xdual(); (_dual = d)._dual = this; return d; } - @Override boolean _isConstant() { return XInt.isConstant(_rpcs); } + @Override boolean _isConstant() { return _rpcs.isConstant(); } @Override TypeRPC _makeStorage() { return this; } // Reserve tags for ALL, singleton, generic @@ -80,23 +80,23 @@ public class TypeRPC extends Type { @Override public void packed( BAOS baos, HashMap<String,Integer> strs ) { if( this==BOT ) { baos.write( TAGOFFS[_type] + 0 ); - } else if( XInt.isConstant(_rpcs) ) { // Singleton + } else if( _rpcs.isConstant() ) { // Singleton baos.write(TAGOFFS[_type] + 1); baos.packed2(rpc()); } else { // Generic - assert !XInt.isHigh(_rpcs); // Not serializing above-center RPC + assert _rpcs.isFinite(); // Not serializing above-center RPC baos.write(TAGOFFS[_type] + 2); - XInt.packed(baos,_rpcs); + _rpcs.packed(baos); } } static TypeRPC packed( int tag, BAOS bais ) { if( tag==0 ) return BOT; - if( tag==1 ) return malloc(XInt.make(bais.packed2())); - return malloc(XInt.packed(bais)); + if( tag==1 ) return malloc(TypeSet.of(bais.packed2())); + return malloc(TypeSet.unpack(bais)); } @Override - int hash() { return XInt.hash(_rpcs); } + int hash() { return _rpcs.hashCode(); } @Override public boolean eq( Type t ) { TypeRPC rpc = (TypeRPC)t; // Contract diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeSet.java b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeSet.java index d49c1e25..2413169f 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeSet.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeSet.java @@ -8,7 +8,7 @@ import com.seaofnodes.simple.util.BAOS; import com.seaofnodes.simple.util.SB; /** - * Interned co-finite set with independent local and external defaults and + * Interned set with independent local and external defaults and * finite XOR exceptions. Meet is union; dual complements both defaults, * and shares the bits. This is a lattice component, not a new runtime Type. * diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeStruct.java b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeStruct.java index d1586547..dc5d5f18 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeStruct.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeStruct.java @@ -87,38 +87,37 @@ public class TypeStruct extends Type { return VISIT.isEmpty() ? t2.free(ts) : ts.delayFree(ts); } + void clearSummaryCaches() { _aliases = _fidxs = null; } + // Summary aliases, after interning. Skips private // fields which need to be explicitly loaded. - private int[] _aliases; - public int[] aliases() { + private TypeSet _aliases; + public TypeSet aliases() { if( _aliases != null ) return _aliases; - if( _fields.length==0 ) return (_aliases=XInt.EMPTY); + if( _fields.length==0 ) return (_aliases=TypeSet.EMPTY); assert _terned; - int maxAlias = 0; - for( Field f : _fields ) - maxAlias = Math.max(maxAlias,f._alias); - int len = XInt.idx(maxAlias)+1; - if( (maxAlias&31)==31 ) len++; - int[] xs = XInt.alloc(len); + int[] aliases = new int[_fields.length]; + int count = 0; for( Field f : _fields ) - if( f._fname.charAt(0) != '_' && // Skip private fields. + if( f._alias>=0 && // Negative aliases are abstract lattice placeholders. + f._fname.charAt(0) != '_' && // Skip private fields. !hiddenInit(f) && // Skip private allocation helper. f._t != Type.TOP && f._t != TypeScalar.TOP ) // Skip unreachable values - xs[XInt.idx(f._alias)] |= XInt.mask(f._alias); - return (_aliases=XInt.intern(xs)); + aliases[count++] = f._alias; + return (_aliases=TypeSet.of(Arrays.copyOf(aliases,count))); } // Summary fidxs, after interning. Skips private // fields which need to be explicitly loaded. - private int[] _fidxs; - public int[] fidxs() { + private TypeSet _fidxs; + public TypeSet fidxs() { if( _fidxs != null ) return _fidxs; assert _terned; - int[] xs = XInt.EMPTY; + TypeSet xs = TypeSet.EMPTY; for( Field f : _fields ) if( f._fname.charAt(0) != '_' && !hiddenInit(f) ) { - if( f._t==TypeScalar.BOT || f._t==Type.BOTTOM ) return (_fidxs = XInt.FULL); - if( f._t instanceof TypeFunPtr tfp ) xs = XInt.meet(xs,tfp._fidxs); + if( f._t==TypeScalar.BOT || f._t==Type.BOTTOM ) return (_fidxs = TypeSet.FULL); + if( f._t instanceof TypeFunPtr tfp ) xs = xs.meet(tfp._fidxs); } return (_fidxs = xs); } diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/XInt.java b/chapter33/src/main/java/com/seaofnodes/simple/type/XInt.java deleted file mode 100644 index 06c9d466..00000000 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/XInt.java +++ /dev/null @@ -1,275 +0,0 @@ -package com.seaofnodes.simple.type; - -import com.seaofnodes.simple.util.*; -import java.lang.Integer; -import java.util.Arrays; - -// Stupidly overkill interned immutable int[]'s -public abstract class XInt { - private static final Ary<Ary<int[]>> FREES = new Ary(Ary.class); - private static final Ary<Ary<int[]>> INTERNS = new Ary(Ary.class); - private static int PROBES, HITS, NEWS; - - public static final int[] EMPTY = intern(new int[]{0, 0}); - public static final int[] FULL = intern(new int[]{0,-1}); - public static final int[] SET1 = make(1); - public static final int[] SET3 = make(SET1, 2); - - static int idx(int x) { return (x>>5)+1; } - static int mask(int x) { return 1<<(x&31); } - - // No hash set, still mutable - public static boolean isMutable(int[] xs) { return xs[0]==0; } - // Return hash code; must be set hence frozen - public static int hash(int[] xs) { return xs[0]; } - - // High bit set (infinite 1's extension) - public static boolean isHigh(int[] xs) { return xs[xs.length-1] < 0; } - - // Check for a bit - public static boolean bit(int[] xs, int x) { - int idx = idx(x); - if( idx >= xs.length ) - return isHigh(xs); - return (xs[idx] & mask(x))!=0; - } - - // Number of bits set - public static int bitCount(int[] xs) { - assert !isHigh(xs); // Dont ask if infinite - int cnt=0; - for( int i=1; i<xs.length; i++ ) - cnt += Integer.bitCount(xs[i]); - return cnt; - } - - // Largest bit - public static int max(int[] xs) { - assert !isHigh(xs); - return Integer.numberOfTrailingZeros(xs[xs.length-1])+((xs.length-1-1)<<5); - } - - // Has 1 bit set - public static boolean isConstant(int[] xs) { return !isHigh(xs) && bitCount(xs)==1; } - // Return the 1 set bit or assert - public static int onlyBit(int[] xs) { - assert isConstant(xs); - for( int i=1; i<xs.length; i++ ) - if( xs[i] != 0 ) - return Integer.numberOfTrailingZeros(xs[i]) + ((i-1)<<5); - throw Utils.TODO("Should not reach here"); - } - - // Subset check: ys is a subset of xs - public static boolean subset(int[] xs, int[] ys) { - assert !isMutable(ys); - if( xs==ys ) return true; - if( xs==FULL ) return true; - if( ys==EMPTY ) return true; - int min = Math.min(xs.length,ys.length); - for( int i=1; i<min; i++ ) - if( (xs[i] | ys[i])!=xs[i] ) - return false; - if( xs.length == ys.length ) - return true; - if( ys.length < xs.length ) - return !isHigh(ys); // If ys is infinite, it'll hit one of the xs extra zeros - return isHigh(xs); - } - - - // Construct a set with 1 bit set - public static int[] make(int x ) { return make(EMPTY,x); } - - // Add a bit - public static int[] make(int[] xs, int x ) { - assert !isMutable(xs); - int idx = idx(x); - if( idx >= xs.length && isHigh(xs) ) - return xs; // Set in the default infinite - if( idx < xs.length && (mask(x) & xs[idx])!=0 ) - return xs; // Already set - int len = Math.max(xs.length,idx+1); - if( (x&31)==31 && idx+1==len ) - len++; - int[] ys = alloc(len); - System.arraycopy(xs,1,ys,1,xs.length-1); - ys[idx] |= mask(x); - return intern(ys); - } - - // Interning, and life-cycle management - // Intern the array - public static int[] intern(int[] xs) { - if( !isMutable(xs) ) return xs; // Already interned - PROBES++; - - // Trim trailing 0s or -1s, use the infinite extend bit - int len = xs.length; - while( len > 2 && - ((xs[len-1]== 0 && xs[len-2]>=0) || - (xs[len-1]== -1 && xs[len-2]< 0) ) ) - len--; - // Replace with trimmed - if( len < xs.length ) { - var ys = alloc(len); - System.arraycopy(xs,1,ys,1,len-1); - xs = _free(xs,ys); - } - - // Compute lame hash code - int hash = 0; - for( int i=1; i<len; i++ ) - hash += xs[i]; - if( hash == 0 ) hash = 0xCAFEBABE; - xs[0] = hash; // Set lame hashcode, and mark immutable - - // Get length-specific INTERN table - var xss = INTERNS.atX(len); - if( xss==null ) - INTERNS.setX( xs.length, xss = new Ary<>(int[].class)); - - // Probe for a hit - for( int[] ys : xss ) - if( Arrays.equals(xs,ys) ) // Hit! - // Free new array and return old array - { HITS++; return _free(xs,ys); } - - // Intern new array and return - return xss.push(xs); - } - - // Return Ary of free int[]s of fixed size - private static Ary<int[]> _xss(int len) { - Ary<int[]> xss = FREES.atX(len); - if( xss==null ) - FREES.setX(len, xss = new Ary<>(int[].class)); - return xss; - } - // Allocate a zeroed mutable array, reusing the free list. - // Pass it to intern after filling; do not retain a mutable reference. - public static int[] alloc(int len) { - int[] xs = _xss(len).pop(); - if( xs==null ) - { NEWS++; xs = new int[len]; } - assert isMutable(xs); - return xs; - } - // Return ys. Put xs on free list - static int[] _free(int[] xs, int[] ys) { - Arrays.fill(xs,0); // Mutable again - _xss(xs.length).push(xs); - return ys; - } - - // Meet: Set union - public static int[] meet(int[] xs, int[] ys) { - if( xs==ys ) return xs; - boolean isX=true, isY=true; - int min = Math.min(xs.length,ys.length); - int max = Math.max(xs.length,ys.length); - for( int i=1; i<max; i++ ) { - int x = i < xs.length ? xs[i] : isHigh(xs) ? -1 : 0; - int y = i < ys.length ? ys[i] : isHigh(ys) ? -1 : 0; - if( (x | y)!=x ) isX=false; - if( (x | y)!=y ) isY=false; - } - if( isX ) return xs; - if( isY ) return ys; - // Get a free one - int[] ms = alloc(max); - for( int i=1; i<min; i++ ) - ms[i] = xs[i] | ys[i]; - for( int i=min; i<ms.length; i++ ) { - int x = i < xs.length ? xs[i] : isHigh(xs) ? -1 : 0; - int y = i < ys.length ? ys[i] : isHigh(ys) ? -1 : 0; - ms[i] = x | y; - } - return intern(ms); - } - - // Dual: flip all the bits - public static int[] dual(int[] xs) { - int[] ds = alloc(xs.length); - for( int i=1; i<xs.length; i++ ) - ds[i] = ~xs[i]; - return intern(ds); - } - - - public static void packed(BAOS baos, int[] xs) { - baos.packed1(xs.length); - for( int i=1; i<xs.length; i++ ) - baos.packed4(xs[i]); - } - public static int[] packed(BAOS bais) { - int len = bais.packed1(); - int[] xs = alloc(len); - for( int i=1; i<len; i++ ) - xs[i] = bais.packed4(); - return intern(xs); - } - - // Next set bit, or -1 - // for( int bit = XInt.next(bits,0); bit >=0; bit = XInt.next(bits,bit) ) {...bit...} - public static int next(int[] xs, int x) { - assert !isHigh(xs); // No iterating infinite bitsets - return next(xs,x,Integer.MAX_VALUE); - } - - // Next set bit below limit, or -1. Allows bounded walks of infinite sets. - public static int next(int[] xs, int x, int limit) { - while( ++x < limit ) { - if( bit(xs,x) ) return x; - if( idx(x)>=xs.length ) - return -1; - } - return -1; - } - - // Remap all bits - public static int[] remap( int[] xs, AryInt map ) { - if( xs==FULL ) return xs; - assert !isHigh(xs); - int[] ys = alloc(xs.length); - for( int bit = next(xs,0); bit >=0; bit = next(xs,bit) ) { - int bat = map.at(bit); - ys[idx(bat)] |= mask(bat); - } - return intern(ys); - } - - - - public static String str(int[] xs) { return str(new SB(),xs).toString(); } - public static SB str(SB sb, int[] xs) { - sb.p("[ "); - // Printing ranges; - // 1,2,3 prints 1-3 - // 1,2,4 prints 1,2,4 - // - int max = (xs.length-1)<<5; - int low = 0; // No low range - for( int i=1; i<max; i++ ) { - if( bit(xs,i) ) { // Set bit, start or extend range - if( low==0 ) low=i; // No range, start one - } else { // Break in sequence, ending a range - range(sb,low,i-1); // Print prior range, and start new one - low=0; // Reset range - } - } - // Print any partial range - range(sb,low,max-1).unchar(); - if( isHigh(xs) ) - sb.p(",..."); - return sb.p("]"); - } - - private static SB range( SB sb, int low, int mid ) { - if( low==0 ) ; - else if( low==mid ) sb.p(low).p(','); - else if( low==mid-1 ) sb.p(low).p(',').p(mid).p(','); - else sb.p(low).p('-').p(mid).p(','); - return sb; - } -} diff --git a/chapter33/src/test/java/com/seaofnodes/simple/BulkMemPhiTest.java b/chapter33/src/test/java/com/seaofnodes/simple/BulkMemPhiTest.java index fd816c68..cf57865e 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/BulkMemPhiTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/BulkMemPhiTest.java @@ -5,7 +5,7 @@ import com.seaofnodes.simple.node.*; import com.seaofnodes.simple.type.TypeInteger; import com.seaofnodes.simple.type.TypeMem; import com.seaofnodes.simple.type.Type; -import com.seaofnodes.simple.type.XInt; +import com.seaofnodes.simple.type.TypeSet; import org.junit.Test; import static org.junit.Assert.*; @@ -14,7 +14,7 @@ public class BulkMemPhiTest { CodeGen code = new CodeGen(""); code._phase = CodeGen.Phase.Lower; Node defaults = code.con(TypeMem.START); - Node method = code.con(TypeMem.make(55,Type.BOTTOM,false,false,false,XInt.make(4),XInt.EMPTY)); + Node method = code.con(TypeMem.make(55,Type.BOTTOM,false,false,false,TypeSet.of(4),TypeSet.EMPTY)); MemMergeNode fields = new MemMergeNode(null,defaults); fields.alias(55,method); fields.init(); @@ -30,7 +30,7 @@ public class BulkMemPhiTest { assertTrue("A new slice must not inherit an escape bound that excludes its method", precise.compute().isa(precise._type)); precise.peephole(); - assertTrue(XInt.bit(((TypeMem)precise._type)._escFs,4)); + assertTrue(((TypeMem)precise._type)._escFs.contains(4)); } // Reduced from Capitalize's Lower-phase failure. A precise MemMerge slot diff --git a/chapter33/src/test/java/com/seaofnodes/simple/CallEndTest.java b/chapter33/src/test/java/com/seaofnodes/simple/CallEndTest.java index bba3a2c8..7983141d 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/CallEndTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/CallEndTest.java @@ -56,10 +56,10 @@ public class CallEndTest { ret._type = TypeTuple.make(Type.CONTROL,TypeMem.START,broad); TypeTuple unknownStop = stop.compute(); TypeMem unknownGlobal = global.compute(); - assertSame(XInt.FULL,((TypeMem)unknownStop.mem())._escFs); - assertSame(XInt.FULL,((TypeMem)unknownStop.mem())._escAs); - assertSame(XInt.FULL,unknownGlobal._escFs); - assertSame(XInt.FULL,unknownGlobal._escAs); + assertSame(TypeSet.FULL,((TypeMem)unknownStop.mem())._escFs); + assertSame(TypeSet.FULL,((TypeMem)unknownStop.mem())._escAs); + assertSame(TypeSet.FULL,unknownGlobal._escFs); + assertSame(TypeSet.FULL,unknownGlobal._escAs); for( Type precise : new Type[]{object,callback} ) { assertTrue(precise.isa(broad)); value._type = precise; diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter25Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter25Test.java index 85f273a1..103461f0 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter25Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter25Test.java @@ -15,7 +15,7 @@ import com.seaofnodes.simple.node.LoopNode; import com.seaofnodes.simple.type.TypeInteger; import com.seaofnodes.simple.type.TypeMem; import com.seaofnodes.simple.type.TypeTuple; -import com.seaofnodes.simple.type.XInt; +import com.seaofnodes.simple.type.TypeSet; import com.seaofnodes.simple.util.Ary; import java.io.File; import java.io.FileWriter; @@ -76,7 +76,7 @@ public class Chapter25Test { for( long seed : new long[]{1,123,987654321} ) { CodeGen code = new CodeGen(src,seed,true).driver(CodeGen.Phase.Parse); FunNode visible = null; - int[] privateFidxs = XInt.EMPTY; + TypeSet privateFidxs = TypeSet.EMPTY; int checked = 0; for( FunNode fun : code._linker ) { if( fun==null || fun._name==null ) continue; @@ -85,16 +85,16 @@ public class Chapter25Test { checked++; } if( fun._name.equals("_hidden") || fun._name.equals("hiddenClassMethod") ) { - privateFidxs = XInt.make(privateFidxs,fun.sig().fidx()); + privateFidxs = privateFidxs.with(fun.sig().fidx()); checked++; } } assertEquals(3,checked); code.driver(CodeGen.Phase.TypeCheck); TypeMem outside = ((TypeTuple)code._start._type).mem(); - assertTrue(XInt.bit(outside._escFs,visible.sig().fidx())); - for( int fidx = XInt.next(privateFidxs,0); fidx>=0; fidx = XInt.next(privateFidxs,fidx) ) - assertFalse(XInt.bit(outside._escFs,fidx)); + assertTrue(outside._escFs.contains(visible.sig().fidx())); + for( int fidx = privateFidxs.next(0); fidx>=0; fidx = privateFidxs.next(fidx) ) + assertFalse(outside._escFs.contains(fidx)); assertTrue(visible.unknownCallers()); assertEquals(TypeInteger.U8,visible.parm(3)._type); assertEquals("0",Eval2.eval(code,0)); @@ -119,11 +119,11 @@ public class Chapter25Test { code.driver(CodeGen.Phase.TypeCheck); TypeMem outside = ((TypeTuple)code._start._type).mem(); // A public field can publish a privately named function. - assertTrue(XInt.bit(outside._escFs,published)); - assertFalse(XInt.bit(outside._escFs,unpublished)); - assertTrue(XInt.bit(outside._escAs,clz.field("visible")._alias)); - assertFalse(XInt.bit(outside._escAs,clz.field("_hidden")._alias)); - assertFalse(XInt.bit(outside._escAs,clz.field("_private")._alias)); + assertTrue(outside._escFs.contains(published)); + assertFalse(outside._escFs.contains(unpublished)); + assertTrue(outside._escAs.contains(clz.field("visible")._alias)); + assertFalse(outside._escAs.contains(clz.field("_hidden")._alias)); + assertFalse(outside._escAs.contains(clz.field("_private")._alias)); } @Test public void testImportedEscapeExclusions() throws IOException { @@ -150,8 +150,8 @@ public class Chapter25Test { int hidden=((com.seaofnodes.simple.type.TypeFunPtr)clz.field("_hidden_noInline")._t).fidx(); assertNotEquals(savedHidden,hidden); TypeMem outside=((TypeTuple)code._start._type).mem(); - assertTrue(XInt.isHigh(outside._escFs)); - assertFalse(XInt.bit(outside._escFs,hidden)); + assertTrue(!outside._escFs.isFinite()); + assertFalse(outside._escFs.contains(hidden)); assertEquals("42",Eval2.eval(code,41)); Files.delete(dir.resolve("Lib.o")); Files.delete(dir); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java b/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java index ee7406c6..84aa8a3f 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java @@ -18,7 +18,7 @@ public class EscapeFlowTest { 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)); + assertFalse(outside._escFs.contains(hidden)); assertEquals("0",Eval2.eval(code,0)); assertEquals("4",Eval2.eval(code,4)); } @@ -33,7 +33,7 @@ public class EscapeFlowTest { 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)); + assertTrue(((TypeTuple)code._start._type).mem()._escFs.contains(fidx)); assertFalse(callback.isDead()); assertTrue(callback.unknownCallers()); } @@ -47,8 +47,8 @@ public class EscapeFlowTest { return wrap; """,seed,true).driver(CodeGen.Phase.TypeCheck); TypeMem outside = ((TypeTuple)code._start._type).mem(); - assertTrue(XInt.isHigh(outside._escFs)); - assertNotSame(XInt.FULL,outside._escFs); + assertTrue(!outside._escFs.isFinite()); + assertNotSame(TypeSet.FULL,outside._escFs); for( FunNode fun : code._linker ) if( fun!=null && fun._name!=null && fun._name.contains("_A.") ) assertFalse(fun._name,fun.unknownCallers()); @@ -72,7 +72,7 @@ public class EscapeFlowTest { ParmNode callback = apply.parm(3); assertTrue(callback.in(1) instanceof ProjNode); TypeFunPtr type = (TypeFunPtr)callback._type; - assertTrue(XInt.isHigh(type.fidxs())); + assertTrue(!type.fidxs().isFinite()); assertSame(TypeInteger.I32,apply.ret().expr()._type); // Only <clinit> and apply escape; forwarding #self must not expose _hidden. assertEquals("Stop[ return { apply_noInline}; return #2; ]",code.print()); @@ -86,7 +86,7 @@ public class EscapeFlowTest { """).driver(CodeGen.Phase.TypeCheck); FunNode callback = named(code,"_callback_noInline"); assertTrue(callback.unknownCallers()); - assertTrue(XInt.bit(((TypeTuple)code._start._type).mem()._escFs,callback.sig().fidx())); + assertTrue(((TypeTuple)code._start._type).mem()._escFs.contains(callback.sig().fidx())); } // Call remains CONTROL. Changing only the outside set must wake CallEnd @@ -100,17 +100,17 @@ public class EscapeFlowTest { CallNode call = new CallNode(null,ConstantNode.raw(Type.CONTROL), ConstantNode.raw(TypeMem.START),ConstantNode.raw(factory)).init(); CallEndNode end = new CallEndNode(call,TypeRPC.BOT); - int[] foreign = XInt.dual(XInt.make(7)); - code._start._type = TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(foreign,XInt.EMPTY),TypeInteger.BOT); + TypeSet foreign = TypeSet.of(7).dual(); + code._start._type = TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(foreign,TypeSet.EMPTY),TypeInteger.BOT); TypeTuple before = (TypeTuple)end.compute(); - assertFalse(XInt.bit(((TypeFunPtr)before.ret()).fidxs(),7)); + assertFalse(((TypeFunPtr)before.ret()).fidxs().contains(7)); boolean depends = false; for( int i = 0; i<code._start.nDeps(); i++ ) depends |= code._start.dep(i)==end; assertTrue(depends); - code._start._type = TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(XInt.FULL,XInt.EMPTY),TypeInteger.BOT); + code._start._type = TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(TypeSet.FULL,TypeSet.EMPTY),TypeInteger.BOT); TypeTuple after = (TypeTuple)end.compute(); assertTrue(before.isa(after)); - assertTrue(XInt.bit(((TypeFunPtr)after.ret()).fidxs(),7)); + assertTrue(((TypeFunPtr)after.ret()).fidxs().contains(7)); } // Ordinary arguments do not read Start, but a later opaque argument must @@ -126,22 +126,22 @@ public class EscapeFlowTest { SummaryNode observer = new SummaryNode(state,new int[0],ctrl,mem).init(); summary.exposed(observer); code._iter._work.clear(); - code._start.setType(TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(XInt.make(XInt.make(7),8),XInt.EMPTY),TypeInteger.BOT)); + code._start.setType(TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(TypeSet.of(7).with(8),TypeSet.EMPTY),TypeInteger.BOT)); assertFalse(code._iter._work.on(summary)); assertFalse(code._iter._work.on(observer)); arg.setType(TypeMemPtr.makeNullable(TypeStruct.forward("OpaqueSummary"))); assertTrue(code._iter._work.on(summary)); assertTrue(code._iter._work.on(observer)); - assertTrue(XInt.bit(((TypeTuple)summary.compute()).mem()._escFs,7)); - assertTrue(XInt.bit(summary.exposed(observer)._escFs,7)); + assertTrue(((TypeTuple)summary.compute()).mem()._escFs.contains(7)); + assertTrue(summary.exposed(observer)._escFs.contains(7)); code._iter._work.clear(); - code._start.setType(TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(XInt.make(8),XInt.EMPTY),TypeInteger.BOT)); + code._start.setType(TypeTuple.make(Type.CONTROL,TypeMem.START.makeFrom(TypeSet.of(8),TypeSet.EMPTY),TypeInteger.BOT)); assertTrue(code._iter._work.on(summary)); assertTrue(code._iter._work.on(observer)); - assertTrue(XInt.bit(((TypeTuple)summary.compute()).mem()._escFs,8)); - assertTrue(XInt.bit(summary.exposed(observer)._escFs,8)); - assertFalse(XInt.bit(summary.exposed(observer)._escFs,7)); + assertTrue(((TypeTuple)summary.compute()).mem()._escFs.contains(8)); + assertTrue(summary.exposed(observer)._escFs.contains(8)); + assertFalse(summary.exposed(observer)._escFs.contains(7)); } // Public construction exposes the allocator; private-memory helpers @@ -152,17 +152,17 @@ public class EscapeFlowTest { return new Box(((arg<<32)>>32)).n; """).parse(); FunNode allocator = named(code,"Test.Box.<new>"); - int[] helpers = XInt.EMPTY; + TypeSet helpers = TypeSet.EMPTY; for( FunNode fun : code._linker ) if( fun!=null && fun.sig().ret() instanceof TypeMem ) - helpers = XInt.make(helpers,fun.sig().fidx()); - assertNotSame(XInt.EMPTY,helpers); + helpers = helpers.with(fun.sig().fidx()); + assertNotSame(TypeSet.EMPTY,helpers); code.driver(CodeGen.Phase.TypeCheck); TypeMem outside = ((TypeTuple)code._start._type).mem(); assertTrue(allocator.unknownCallers()); - assertTrue(XInt.bit(outside._escFs,allocator.sig().fidx())); - for( int fidx = XInt.next(helpers,0); fidx>=0; fidx = XInt.next(helpers,fidx) ) - assertFalse(XInt.bit(outside._escFs,fidx)); + assertTrue(outside._escFs.contains(allocator.sig().fidx())); + for( int fidx = helpers.next(0); fidx>=0; fidx = helpers.next(fidx) ) + assertFalse(outside._escFs.contains(fidx)); assertEquals("42",Eval2.eval(code,42)); } diff --git a/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java b/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java index cc4e552f..72e26a2e 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java @@ -36,7 +36,7 @@ public class SeparateCompilationTest { Path file=lib.resolve("Old.o"); ElfReader elf=ElfReader.load(file.toFile(),null); byte[] bytes=Files.readAllBytes(file); - bytes[elf._bais.size()+3]='E'; // Previous C0DE header, before format 2. + bytes[elf._bais.size()+3]='2'; // Previous format used packed XInt arrays. Files.write(file,bytes); try { ElfReader.load(file.toFile(),null).loadPublicTypes(new CodeGen("")); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/TypeSetTest.java b/chapter33/src/test/java/com/seaofnodes/simple/TypeSetTest.java index 5f25a901..43e35fc3 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/TypeSetTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/TypeSetTest.java @@ -2,6 +2,7 @@ package com.seaofnodes.simple; import com.seaofnodes.simple.type.Type; import com.seaofnodes.simple.type.TypeSet; +import com.seaofnodes.simple.util.BAOS; import java.util.ArrayList; import org.junit.Before; import org.junit.Test; @@ -10,6 +11,31 @@ import static org.junit.Assert.*; public class TypeSetTest { @Before public void resetTypes() { Type.reset(); } + @Test public void testPackedSetsAndUniformOperations() { + int[] ids = {0,1,30,31,32,63,64,95,127,1023}; + TypeSet finite = TypeSet.of(ids); + assertEquals(ids.length,finite.bitCount()); + int next = -1; + for( int id : ids ) { + next = finite.next(next); + assertEquals(id,next); + } + assertEquals(-1,finite.next(next)); + assertEquals(0,TypeSet.of(0).onlyBit()); + assertSame(TypeSet.FULL,finite.meet(finite.dual())); + assertSame(TypeSet.EMPTY,finite.join(finite.dual())); + assertSame(finite,finite.with(2048).without(2048)); + for( int defaults=0; defaults<4; defaults++ ) + for( int[] exceptions : new int[][]{new int[0],ids} ) { + TypeSet set = TypeSet.make((defaults&1)!=0,(defaults&2)!=0,exceptions); + BAOS out = new BAOS(); + set.packed(out); + BAOS in = new BAOS(out.toByteArray()); + assertSame(set,TypeSet.unpack(in)); + assertEquals(out.size(),in.size()); + } + } + @Test public void testTypeSetDiscoveryAndInterning() { int[] external = {0xAAAAAAAA,0xAAAAAAAA,2}; // Odd identities are external. int[] exceptions = {0,31,32,63,65}; diff --git a/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java b/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java index adcc1b3a..28b6b0fb 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/TypeTest.java @@ -48,9 +48,9 @@ public class TypeTest { // Conversely, a singleton pointer can refer to mutable contents. Assert.assertTrue(TypeMemPtr.make((byte)2,varying,true).isConstant()); - TypeMem memory = TypeMem.make(2,cycle,true,true,true,XInt.EMPTY,XInt.EMPTY); + TypeMem memory = TypeMem.make(2,cycle,true,true,true,TypeSet.EMPTY,TypeSet.EMPTY); Assert.assertTrue(memory.isConstant()); - Assert.assertFalse(TypeMem.make(2,cycle,true,false,true,XInt.EMPTY,XInt.EMPTY).isConstant()); + Assert.assertFalse(TypeMem.make(2,cycle,true,false,true,TypeSet.EMPTY,TypeSet.EMPTY).isConstant()); // Repeated aggregate types are sharing, not recursion. Neither query // should require or leave behind a cyclic-traversal visit marker. TypeTuple tuple = TypeTuple.make(cycle,memory,cycle); @@ -80,23 +80,23 @@ public class TypeTest { } @Test public void testOpaqueExternalEscapes() { - TypeMem defaults = TypeMem.START.makeFrom(XInt.make(2),XInt.make(3)); - TypeMem prior = TypeMem.START.makeFrom(XInt.make(5),XInt.make(7)); + TypeMem defaults = TypeMem.START.makeFrom(TypeSet.of(2),TypeSet.of(3)); + TypeMem prior = TypeMem.START.makeFrom(TypeSet.of(5),TypeSet.of(7)); TypeMemPtr opaque = TypeMemPtr.makeNullable(TypeStruct.forward("OpaqueExternal")); TypeMem escaped = prior.escapesFrom(opaque,defaults); - assertSame(XInt.meet(prior._escFs,defaults._escFs),escaped._escFs); - assertSame(XInt.meet(prior._escAs,defaults._escAs),escaped._escAs); + assertSame(prior._escFs.meet(defaults._escFs),escaped._escFs); + assertSame(prior._escAs.meet(defaults._escAs),escaped._escAs); // Before type closure the same name might still resolve locally. - assertSame(XInt.FULL,prior.escapesFrom(opaque,TypeMem.BOT)._escFs); + assertSame(TypeSet.FULL,prior.escapesFrom(opaque,TypeMem.BOT)._escFs); for( Type unknown : new Type[]{Type.BOTTOM,TypeScalar.BOT,TypePtr.PTR, TypeMemPtr.make(TypeStruct.open("LocalStillOpen"))} ) { TypeMem broad = prior.escapesFrom(unknown,defaults); - assertSame(XInt.FULL,broad._escFs); - assertSame(XInt.FULL,broad._escAs); + assertSame(TypeSet.FULL,broad._escFs); + assertSame(TypeSet.FULL,broad._escAs); } // Concrete private callbacks must not be restricted to public names. TypeFunPtr callback = TypeFunPtr.make1((byte)2,true,TypeFunPtr.TEMPTY,TypeInteger.BOT,11); - assertSame(XInt.make(11),TypeMem.START.escapesFrom(callback,defaults)._escFs); + assertSame(TypeSet.of(11),TypeMem.START.escapesFrom(callback,defaults)._escFs); } // Aggregate finality is carried by its fields. Canonicalizing the unused @@ -107,18 +107,18 @@ public class TypeTest { Field.make("y",TypeInteger.ZERO,3,true)); for( Type t : new Type[]{Type.BOTTOM,Type.TOP,TypeStruct.BOT,TypeStruct.TOP,obj,obj.dual()} ) for( boolean one : new boolean[]{false,true} ) { - TypeMem mem = TypeMem.make(1,t,one,false,false,XInt.EMPTY,XInt.EMPTY); - assertSame(mem,TypeMem.make(1,t,one,false,true,XInt.EMPTY,XInt.EMPTY)); + TypeMem mem = TypeMem.make(1,t,one,false,false,TypeSet.EMPTY,TypeSet.EMPTY); + assertSame(mem,TypeMem.make(1,t,one,false,true,TypeSet.EMPTY,TypeSet.EMPTY)); assertSame(mem,mem.dual().dual()); - assertSame(mem.dual(),TypeMem.make(1,t.dual(),!one,true,true,XInt.FULL,XInt.FULL)); + assertSame(mem.dual(),TypeMem.make(1,t.dual(),!one,true,true,TypeSet.FULL,TypeSet.FULL)); } // Precise field slices still distinguish final and mutable storage. - Assert.assertNotEquals(TypeMem.make(2,TypeInteger.BOT,false,false,false,XInt.EMPTY,XInt.EMPTY), - TypeMem.make(2,TypeInteger.BOT,false,false,true ,XInt.EMPTY,XInt.EMPTY)); + Assert.assertNotEquals(TypeMem.make(2,TypeInteger.BOT,false,false,false,TypeSet.EMPTY,TypeSet.EMPTY), + TypeMem.make(2,TypeInteger.BOT,false,false,true ,TypeSet.EMPTY,TypeSet.EMPTY)); for( boolean one : new boolean[]{false,true} ) for( boolean fin : new boolean[]{false,true} ) { - TypeMem bulk = TypeMem.make(1,Type.TOP,one,false,false,XInt.EMPTY,XInt.EMPTY); - TypeMem slice = TypeMem.make(2,TypeInteger.BOT,false,false,fin,XInt.EMPTY,XInt.EMPTY); + TypeMem bulk = TypeMem.make(1,Type.TOP,one,false,false,TypeSet.EMPTY,TypeSet.EMPTY); + TypeMem slice = TypeMem.make(2,TypeInteger.BOT,false,false,fin,TypeSet.EMPTY,TypeSet.EMPTY); assertSame(slice,bulk.meet(slice)); check_symmetric(bulk,slice); check_symmetric(bulk.dual(),slice.dual()); @@ -134,8 +134,8 @@ public class TypeTest { for( Type broad : new Type[]{Type.BOTTOM,TypeScalar.BOT,TypePtr.PTR,TypePtr.NPTR} ) { Assert.assertTrue(precise.isa(broad)); TypeMem unknown = TypeMem.START.escapes(broad); - assertSame(XInt.FULL,unknown._escFs); - assertSame(XInt.FULL,unknown._escAs); + assertSame(TypeSet.FULL,unknown._escFs); + assertSame(TypeSet.FULL,unknown._escAs); Assert.assertTrue(TypeMem.START.escapes(precise).isa(unknown)); } for( Type empty : new Type[]{Type.TOP,TypeScalar.TOP,TypePtr.XPTR,TypePtr.XNPTR,TypeNil.NIL} ) @@ -143,14 +143,14 @@ public class TypeTest { } @Test public void testBoundedBits() { - int[] sparse = XInt.make(XInt.make(2),65); - for( int[] bits : new int[][]{XInt.EMPTY,XInt.FULL,sparse,XInt.dual(sparse)} ) + TypeSet sparse = TypeSet.of(2).with(65); + for( TypeSet bits : new TypeSet[]{TypeSet.EMPTY,TypeSet.FULL,sparse,sparse.dual()} ) for( int limit : new int[]{0,1,32,65,66,100} ) { - int next = XInt.next(bits,-1,limit); + int next = bits.nextKnown(-1,limit); for( int bit=0; bit<limit; bit++ ) - if( XInt.bit(bits,bit) ) { + if( bits.contains(bit) ) { Assert.assertEquals(bit,next); - next = XInt.next(bits,next,limit); + next = bits.nextKnown(next,limit); } Assert.assertEquals(-1,next); } @@ -218,12 +218,12 @@ public class TypeTest { Assert.assertNotEquals(m3, m4); Assert.assertEquals(TypeStruct.BOT, s1.meet(s2)); - TypeMem scalarMemBot = TypeMem.make(1,TypeScalar.BOT,false,false,false,XInt.FULL,XInt.FULL); - Assert.assertEquals(scalarMemBot, ((TypeMem)m1.meet(m2)).makeFrom( XInt.FULL, XInt.FULL)); + TypeMem scalarMemBot = TypeMem.make(1,TypeScalar.BOT,false,false,false,TypeSet.FULL,TypeSet.FULL); + Assert.assertEquals(scalarMemBot, ((TypeMem)m1.meet(m2)).makeFrom( TypeSet.FULL, TypeSet.FULL)); Assert.assertEquals(TypeMem.make(1,TypeScalar.BOT), m1.meet(m3)); - Assert.assertEquals(scalarMemBot, ((TypeMem)m3.meet(m4)).makeFrom( XInt.FULL, XInt.FULL)); + Assert.assertEquals(scalarMemBot, ((TypeMem)m3.meet(m4)).makeFrom( TypeSet.FULL, TypeSet.FULL)); - Assert.assertEquals(TypeMem.make(2,Type.XNIL,true,true,true, XInt.FULL, XInt.FULL), m1.dual()); + Assert.assertEquals(TypeMem.make(2,Type.XNIL,true,true,true, TypeSet.FULL, TypeSet.FULL), m1.dual()); TypeMemPtr ptr1 = TypeMemPtr.make(s1); Assert.assertEquals(s1, ptr1._obj); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/type/PackedTypeTestSupport.java b/chapter33/src/test/java/com/seaofnodes/simple/type/PackedTypeTestSupport.java index c218e98b..39ea46b9 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/type/PackedTypeTestSupport.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/type/PackedTypeTestSupport.java @@ -7,7 +7,7 @@ import static org.junit.Assert.*; public class PackedTypeTestSupport { public static void singleton(int id) { Type.TAGOFFS(); - TypeFunPtr fun=TypeFunPtr.make((byte)2,true,new Type[0],TypeInteger.BOT,XInt.make(id)); + TypeFunPtr fun=TypeFunPtr.make((byte)2,true,new Type[0],TypeInteger.BOT,TypeSet.of(id)); BAOS out=new BAOS(); fun.packed(out,new HashMap<>()); assertEquals(id<=254 ? 2 : 4,out.size()); |
