diff options
Diffstat (limited to 'chapter32/src/main/java/com')
7 files changed, 87 insertions, 53 deletions
diff --git a/chapter32/src/main/java/com/seaofnodes/simple/node/CallNode.java b/chapter32/src/main/java/com/seaofnodes/simple/node/CallNode.java index 3b2dc75b..5506621b 100644 --- a/chapter32/src/main/java/com/seaofnodes/simple/node/CallNode.java +++ b/chapter32/src/main/java/com/seaofnodes/simple/node/CallNode.java @@ -7,6 +7,7 @@ import com.seaofnodes.simple.parser.Parser; import com.seaofnodes.simple.codegen.CodeGen; import com.seaofnodes.simple.codegen.Iter; import com.seaofnodes.simple.type.Type; +import com.seaofnodes.simple.type.TypePtr; import com.seaofnodes.simple.type.TypeMemPtr; import com.seaofnodes.simple.type.TypeFunPtr; import com.seaofnodes.simple.type.TypeMem; @@ -95,13 +96,15 @@ public class CallNode extends CFGNode { // A broad scalar target can refine to a function pointer during Iter. // It cannot yet promise that the call exposes nothing. if( _type!=Type.CONTROL ) return TypeMem.BOT; - if( !(fptr()._type instanceof TypeFunPtr) ) + if( !(fptr()._type instanceof TypeFunPtr tfp) ) return fptr()._type.isHigh() ? TypeMem.START : TypeMem.BOT; if( Intrinsic.pure(this) ) return TypeMem.START; if( !external() ) return TypeMem.START; Type input = dep.addDepForwards(mem())._type; TypeMem escaped = input instanceof TypeMem m ? m : input.isHigh() ? TypeMem.TOP : TypeMem.BOT; - for( int i = 2; i<nIns()-1; i++ ) { + // The class-receiver slot is absent from the ABI. The callee supplies + // its own singleton, so passing a callback does not expose our class. + for( int i = tfp.nargs()>0 && tfp.arg(0)==TypePtr.PTR ? 3 : 2; i<nIns()-1; i++ ) { Type arg = dep.addDepForwards(arg(i))._type; TypeMem outside = TypeMem.opaque(arg) ? CodeGen.CODE._start.outside(dep) : TypeMem.BOT; escaped = escaped.escapesFrom(arg,outside); @@ -182,7 +185,13 @@ public class CallNode extends CFGNode { CodeGen.CODE.add(fun).addDef(this); for( Node use : fun._outputs ) if( use instanceof ParmNode parm ) { - Node def = parm._idx==0 ? ConstantNode.seed(cend()._rpc).peephole() : arg(parm._idx); + // A class function's receiver belongs to the callee, even + // when a different class or an instance forwards its callback. + // Match its declared view: before SCCP, forward fields can + // still differ from the completed class layout. + Node def = parm._idx==0 ? ConstantNode.seed(cend()._rpc).peephole() + : parm._idx==2 && fun.sig().arg(0)==TypePtr.PTR + ? ConstantNode.seed(parm._declaredType).peephole() : arg(parm._idx); CodeGen.CODE.add(parm).addDef(def); // Linking can happen during post-SCCP inlining. Keep an RPC // Parm's cached finite set synchronized with its newly added diff --git a/chapter32/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter32/src/main/java/com/seaofnodes/simple/node/PhiNode.java index 7544962f..94376086 100644 --- a/chapter32/src/main/java/com/seaofnodes/simple/node/PhiNode.java +++ b/chapter32/src/main/java/com/seaofnodes/simple/node/PhiNode.java @@ -122,7 +122,8 @@ public class PhiNode extends Node { // Generic "pull down op" Node progress; - if( same_op() && (progress = drop_same_op()) != null ) + Node op = factoringOp(); + if( same_op(op) && (progress = drop_same_op(op)) != null ) return progress; // If merging Phi(ZERO, guardNZ(N)) at the matching `if(N)` join, the @@ -161,10 +162,28 @@ public class PhiNode extends Node { return tested!=null && (early==null || early.dominates(r,this)) ? n : null; } + // View x<<1 as x+x when factoring mixed Add/Shl arms. Do not expand the + // graph: Add would immediately strength-reduce it again. If all arms are + // shifts, keep the ordinary shift factoring (including arbitrary counts). + private Node factoringOp() { + Node op = in(1); + if( doubled(op) ) + for( int i=2; i<nIns(); i++ ) + if( in(i) instanceof AddNode add && add._mode==1 ) return add; + return op; + } + + private boolean doubled(Node n) { + return n instanceof ShlNode && addDep(n.in(2))._type==TypeInteger.TRUE; + } + + private static Node factoringInput(Node op, Node n, int j) { + return n instanceof ShlNode && op instanceof AddNode ? n.in(1) : n.in(j); + } + // Same op on all Phi paths; all ops have only the Phi as a use. // None have a control input. - private boolean same_op() { - Node op = in(1); + private boolean same_op(Node op) { if( op instanceof EscapeNode || op instanceof CFGNode || op instanceof ConstantNode || op instanceof PhiNode || op instanceof ProjNode || op instanceof NewNode || op instanceof ScopeNode || op instanceof MemMergeNode ) return false; // A bulk Phi must split its aliases before factoring precise Stores. @@ -181,8 +200,10 @@ public class PhiNode extends Node { Node busy = null; for( int i=1; i<nIns(); i++ ) { Node n = in(i); + boolean same = op.getClass()==n.getClass() && op.eq(n); + if( !same && op instanceof AddNode add && add._mode==1 ) same = doubled(n); if( addDep(region().in(i))._type==Type.XCONTROL || - op.getClass()!=n.getClass() || n.nIns()!=op.nIns() || n.in(0)!=null || !op.eq(n) ) return false; + !same || n.nIns()!=op.nIns() || n.in(0)!=null ) return false; if( n instanceof MemOpNode mem && !mem.canDrop((MemOpNode)op,this) ) return false; // Moving a Store must remove the old effect, not duplicate it. // Allow one shared arm: the other arms disappear into the single @@ -200,21 +221,21 @@ public class PhiNode extends Node { return true; } - private Node drop_same_op() { - Node op = in(1), cp = op.copyEmpty(); + private Node drop_same_op(Node op) { + Node cp = op.copyEmpty(); cp._type = null; cp._inputs.add(null); for( int j=1; j<op.nIns(); j++ ) { - Node x = op.in(j); + Node x = factoringInput(op,in(1),j); boolean different = false; for( int i=2; i<nIns(); i++ ) - if( in(i).in(j)!=x ) different=true; + if( factoringInput(op,in(i),j)!=x ) different=true; if( different ) { Node[] ins = new Node[nIns()]; ins[0] = region(); Type t = Type.TOP; for( int i=1; i<nIns(); i++ ) { - ins[i] = in(i).in(j); + ins[i] = factoringInput(op,in(i),j); t = t.meet(ins[i]._type); } // Do not introduce an operand Phi mixing incompatible type families. diff --git a/chapter32/src/main/java/com/seaofnodes/simple/node/RegionNode.java b/chapter32/src/main/java/com/seaofnodes/simple/node/RegionNode.java index ca358b5a..17126f27 100644 --- a/chapter32/src/main/java/com/seaofnodes/simple/node/RegionNode.java +++ b/chapter32/src/main/java/com/seaofnodes/simple/node/RegionNode.java @@ -68,8 +68,9 @@ public class RegionNode extends CFGNode { for( Node use : _outputs ) { if( use instanceof PhiNode phi ) { PhiNode phi2 = phi.in(i) instanceof PhiNode phi2x && phi2x.region()==region ? phi2x : null; + // New users can require a bulk-memory input to split. for( int j=1; j<region.nIns(); j++ ) - phi.addDef(phi2==null ? phi.in(i) : phi2.in(j)); + phi.addDef(CodeGen.CODE.add(phi2==null ? phi.in(i) : phi2.in(j))); CodeGen.CODE.add(phi); } } diff --git a/chapter32/src/main/java/com/seaofnodes/simple/node/SummaryNode.java b/chapter32/src/main/java/com/seaofnodes/simple/node/SummaryNode.java index f73ba611..5b945457 100644 --- a/chapter32/src/main/java/com/seaofnodes/simple/node/SummaryNode.java +++ b/chapter32/src/main/java/com/seaofnodes/simple/node/SummaryNode.java @@ -26,7 +26,7 @@ public class SummaryNode extends CFGNode implements MultiNode { // A library summary does not forget escapes already present in the caller. TypeMem mem = in(1)._type instanceof TypeMem m ? m : in(1)._type.isHigh() ? TypeMem.TOP : TypeMem.BOT; mem = (TypeMem)mem.meet(_state.mem()); - for( int i = 2; i<nIns(); i++ ) { + for( int i = firstArgument(); i<nIns(); i++ ) { Type arg = in(i)._type; // Only opaque arguments can hide identities supplied by outside code. TypeMem outside = TypeMem.opaque(arg) ? CodeGen.CODE._start.outside(this) : TypeMem.BOT; @@ -39,13 +39,20 @@ public class SummaryNode extends CFGNode implements MultiNode { dep.addDepForwards(this); if( dep.addDepForwards(in(0))._type.isHigh() ) return TypeMem.START; TypeMem mem = (TypeMem)_state.mem(); - for( int i = 2; i<nIns(); i++ ) { + for( int i = firstArgument(); i<nIns(); i++ ) { Type arg = dep.addDepForwards(in(i))._type; TypeMem outside = TypeMem.opaque(arg) ? CodeGen.CODE._start.outside(dep) : TypeMem.BOT; mem = mem.escapesFrom(arg,outside); } return TypeMem.make(1,Type.BOTTOM,false,false,false,mem._escFs,mem._escAs); } + // A class receiver is supplied by the callee, not exposed by its caller. + // Actual uses which publish self are already represented in _state. + private int firstArgument() { + if( !(in(0) instanceof FunNode fun) ) return 2; + TypeFunPtr sig = fun.sig(); + return sig.nargs()>0 && sig.arg(0)==TypePtr.PTR ? 3 : 2; + } @Override boolean _upgradeType(HashMap<String,Type> types) { TypeTuple state = (TypeTuple)_state.upgradeType(types); if( state==_state ) return false; diff --git a/chapter32/src/main/java/com/seaofnodes/simple/parser/Expressions.java b/chapter32/src/main/java/com/seaofnodes/simple/parser/Expressions.java index ffc96f57..f791a34c 100644 --- a/chapter32/src/main/java/com/seaofnodes/simple/parser/Expressions.java +++ b/chapter32/src/main/java/com/seaofnodes/simple/parser/Expressions.java @@ -36,6 +36,9 @@ final class Expressions { { P.pos(old); return parseExpression(); } P.references.checkAssignment(def,site); + // All fields use the same receiver-based lvalue path. + if( P.references.fieldSelf(def)!=null ) + { P.pos(old); return parseExpression(); } return P.references.assign(def,parseAssignment()); } diff --git a/chapter32/src/main/java/com/seaofnodes/simple/parser/Functions.java b/chapter32/src/main/java/com/seaofnodes/simple/parser/Functions.java index 84bd509e..a7726521 100644 --- a/chapter32/src/main/java/com/seaofnodes/simple/parser/Functions.java +++ b/chapter32/src/main/java/com/seaofnodes/simple/parser/Functions.java @@ -36,7 +36,7 @@ final class Functions { Type self = P.types.implicitSelfType(); if( P.structs.inStructDeclaration() ) self = self.makeRO(); ts.push(self); - ids.push(self == TypePtr.PTR ? "#self" : "self"); + ids.push("self"); // Parse other arguments P._lexer.skipWhiteSpace(); @@ -130,6 +130,10 @@ final class Functions { P._lexer.skipWhiteSpace(); Lexer nameLoc=P.loc(); String id = P.requireId(); + // Older free functions may explicitly name a source argument + // "self". It shadows the implicit class receiver, not its ABI slot. + if( id=="self" && ts.at(0)==TypePtr.PTR && ids.at(0)=="self" ) + ids.set(0,"#self"); if( ids.find(id)!=-1 ) throw Parser.error("Duplicate parameter '"+id+"'",nameLoc); ts .push(t ); // Push type/arg pairs ids.push(id); @@ -359,6 +363,14 @@ final class Functions { if( i == 0 && ids[0].equals("self") && !FunNode.isInit(funName) && !(sig.ret() instanceof TypeMem) && t instanceof TypeMemPtr ptr ) t = ptr.withAccess(false); + // The body sees its owning class receiver. PTR remains in the + // signature as the ABI marker for a receiver with no machine slot. + if( i==0 && t==TypePtr.PTR ) { + TypeStruct clz = ((TypeMemPtr)fun.defaultArg(i))._obj; + // A file can add globals after its struct declaration closes. + // Keep this view open until SCCP installs the completed class. + t = TypeMemPtr.make((byte)2,TypeStruct.make(clz._name,true,clz._fields),true); + } // Args take a default input if called from Start/external-world Type seed = i == 0 && t instanceof TypeMemPtr ? t : fun.defaultArg(i); Node parm = new ParmNode(ids[i],i+2,t,fun,ConstantNode.seed(seed).peephole()).peephole(); diff --git a/chapter32/src/main/java/com/seaofnodes/simple/parser/References.java b/chapter32/src/main/java/com/seaofnodes/simple/parser/References.java index 7724103e..91cf6859 100644 --- a/chapter32/src/main/java/com/seaofnodes/simple/parser/References.java +++ b/chapter32/src/main/java/com/seaofnodes/simple/parser/References.java @@ -93,23 +93,9 @@ final class References { // Check for a function-escaping variable; these require true // closures. Final constants are OK; final vars require a hidden var // argument; not-final fields can just use an explicit struct arg. - if( kx < fx && !P.structs.inExplicitConstructor() && // Out of scope - !(var._final && rvalue._type.isConstant()) ) { // Not a final constant - if( kx > 0 ) // Global scope is OK, only ever one of these (not one per function invoke) - throw Parser.error("Variable '"+var._name+"' is out of function scope and must be a final constant",site); - - // var.idx==2 is self argument, 3 is args, 4 or more are now static globals - if( var._idx >= 4 ) { - // Read from global scope. - // Surely a nicer way to get the top-level scope compile name? - Var clzptr = P._scope.var( 2 ); - // Get the most recent sharpen global - TypeStruct clz = (TypeStruct) Parser.TYPES.get( ((TypeMemPtr) clzptr.type())._obj._name ); - TypeMemPtr clztmp = TypeMemPtr.make( (byte) 2, clz, true ); - return parseField( P.con( clztmp ), id,pre ); - //return parseField(_scope.in(2),id); - } - } + if( kx>0 && kx<fx && !P.structs.inExplicitConstructor() && + !(var._final && rvalue._type.isConstant()) ) + throw Parser.error("Variable '"+var._name+"' is out of function scope and must be a final constant",site); // Check for assign-update, x += e0; char ch = pre==0 ? P._lexer.matchOperAssign() : (char)pre; @@ -144,19 +130,6 @@ final class References { } Node assign(Var def, Node expr) { - Node self = fieldSelf(def); - if( self!=null ) { - TypeStruct ts = P.types.latestStruct(((TypeMemPtr)self._type)._obj); - Field fld = ts.field(def._name); - Node val = expr.keep(); - Node lift = P.conversions.convertExpr(val,def.type()); - Node mem = P.construction.storeMemory(self).keep(); - Node st = new StoreNode(P.loc(),fld._fname,fld._alias,fld._t,P.ctrl(),mem,self, - ConFldOffNode.make(ts,fld._fname),lift,P.construction.own(self)).peephole(); - P.construction.store(self,fld._alias,st,mem); - mem.unkeep(); - return val.unkeep(); - } String name = def._name; // Lift expression, based on type Type decl = def.type(); // Declared type @@ -305,19 +278,27 @@ final class References { return P._scope.in(P._scope.update(P._scope.var(selfx),null)); } - // Fields are lvalues in memory, including inside their initializer and - // explicit constructor. Scope slots retain declaration metadata only. - private Node fieldSelf(Var var) { + // Find the receiver for a field lvalue. Instance initializers use memory; + // class declarations retain their Scope values until initializer exit. + Node fieldSelf(Var var) { int kx = P._scope.kindx(var); ScopeNode.Kind kind = P._scope._kinds.at(kx); - if( !(kind instanceof Kind.Func inst) || !FunNode.isInstance(inst._name) || var._idx<inst._lexSize+2 ) return null; + if( !(kind instanceof Kind.Func owner) || !FunNode.isInit(owner._name) || var._idx<owner._lexSize+2 ) return null; + // Class declarations still accumulate their initial values in Scope. + // This also preserves declaration-time struct default expressions. + if( FunNode.isClz(owner._name) && P._scope.enclosingFunction()==kx ) return null; // Nested functions have their own receiver parameter. Resolve fields // through it even across intervening function and block scopes. Node self = defaultSelf(); - Type owner = P._scope.var(inst._lexSize).type(); - if( self._type instanceof TypeMemPtr ptr && owner instanceof TypeMemPtr declared && + Var receiver = P._scope.var(owner._lexSize); + if( self._type instanceof TypeMemPtr ptr && receiver.type() instanceof TypeMemPtr declared && ptr._obj._name.equals(declared._obj._name) ) return self; - return null; + // An instance method can also name fields of its enclosing class. + // Use that declaration's receiver; normal folding handles its identity. + if( !FunNode.isClz(owner._name) ) return null; + // The enclosing declaration may have discovered more fields since + // creating its receiver. View that same receiver with its current type. + return P.peep(new ConvertNode(receiver.type().upgradeType(Parser.TYPES),P._scope.in(receiver))); } private void checkSharedFunction(Var v) { |
