summaryrefslogtreecommitdiff
path: root/chapter30/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'chapter30/src/main/java')
-rw-r--r--chapter30/src/main/java/com/seaofnodes/simple/node/CallNode.java11
-rw-r--r--chapter30/src/main/java/com/seaofnodes/simple/node/PhiNode.java38
-rw-r--r--chapter30/src/main/java/com/seaofnodes/simple/node/RegionNode.java2
-rw-r--r--chapter30/src/main/java/com/seaofnodes/simple/node/SummaryNode.java11
-rw-r--r--chapter30/src/main/java/com/seaofnodes/simple/parser/Expressions.java3
-rw-r--r--chapter30/src/main/java/com/seaofnodes/simple/parser/Functions.java16
-rw-r--r--chapter30/src/main/java/com/seaofnodes/simple/parser/References.java60
7 files changed, 87 insertions, 54 deletions
diff --git a/chapter30/src/main/java/com/seaofnodes/simple/node/CallNode.java b/chapter30/src/main/java/com/seaofnodes/simple/node/CallNode.java
index ac5e4eee..be142889 100644
--- a/chapter30/src/main/java/com/seaofnodes/simple/node/CallNode.java
+++ b/chapter30/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,14 @@ 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( !external() ) return TypeMem.START;
Type input = dep.addDepForwards(mem())._type;
TypeMem escaped = input instanceof TypeMem m ? m : input.isHigh() ? TypeMem.TOP : TypeMem.BOT;
TypeMem outside = CodeGen.CODE._start.outside(dep);
- for( int i = 2; i<nIns()-1; i++ )
+ // A class receiver belongs to the callee; the caller does not expose its class.
+ for( int i = tfp.nargs()>0 && tfp.arg(0)==TypePtr.PTR ? 3 : 2; i<nIns()-1; i++ )
escaped = escaped.escapesFrom(dep.addDepForwards(arg(i))._type,outside);
return TypeMem.make(1,Type.BOTTOM,false,false,false,escaped._escFs,escaped._escAs);
}
@@ -179,7 +181,10 @@ 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);
+ // Match the callee's declared receiver view, including forward fields.
+ 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/chapter30/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter30/src/main/java/com/seaofnodes/simple/node/PhiNode.java
index f45c7325..af24a94b 100644
--- a/chapter30/src/main/java/com/seaofnodes/simple/node/PhiNode.java
+++ b/chapter30/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
@@ -158,10 +159,28 @@ public class PhiNode extends Node {
return tested!=null && (early==null || early.dominates(r,this)) ? n : null;
}
+ // View x<<1 as x+x only while matching mixed Phi arms. Do not
+ // expand the graph into Adds that would immediately strength-reduce again.
+ 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 in(i);
+ return op;
+ }
+
+ private boolean doubled(Node n) {
+ return n instanceof ShlNode &&
+ addDep(n.in(2))._type instanceof TypeInteger con && con.isConstant() && con.value()==1;
+ }
+
+ private static Node factoringInput(Node op, Node n, int j) {
+ return op instanceof AddNode && !(n 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.
@@ -179,7 +198,8 @@ public class PhiNode extends Node {
for( int i=1; i<nIns(); i++ ) {
Node n = in(i);
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;
+ !((op.getClass()==n.getClass() && op.eq(n)) || (op instanceof AddNode add && add._mode==1 && doubled(n))) ||
+ 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
@@ -197,21 +217,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/chapter30/src/main/java/com/seaofnodes/simple/node/RegionNode.java b/chapter30/src/main/java/com/seaofnodes/simple/node/RegionNode.java
index eee1c150..3ed63862 100644
--- a/chapter30/src/main/java/com/seaofnodes/simple/node/RegionNode.java
+++ b/chapter30/src/main/java/com/seaofnodes/simple/node/RegionNode.java
@@ -69,7 +69,7 @@ public class RegionNode extends CFGNode {
if( use instanceof PhiNode phi ) {
PhiNode phi2 = phi.in(i) instanceof PhiNode phi2x && phi2x.region()==region ? phi2x : null;
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/chapter30/src/main/java/com/seaofnodes/simple/node/SummaryNode.java b/chapter30/src/main/java/com/seaofnodes/simple/node/SummaryNode.java
index 483aab89..d0da3250 100644
--- a/chapter30/src/main/java/com/seaofnodes/simple/node/SummaryNode.java
+++ b/chapter30/src/main/java/com/seaofnodes/simple/node/SummaryNode.java
@@ -27,7 +27,7 @@ public class SummaryNode extends CFGNode implements MultiNode {
TypeMem mem = in(1)._type instanceof TypeMem m ? m : in(1)._type.isHigh() ? TypeMem.TOP : TypeMem.BOT;
mem = (TypeMem)mem.meet(_state.mem());
TypeMem outside = CodeGen.CODE._start.outside(this);
- for( int i = 2; i<nIns(); i++ ) mem = mem.escapesFrom(in(i)._type,outside);
+ for( int i = firstArgument(); i<nIns(); i++ ) mem = mem.escapesFrom(in(i)._type,outside);
return TypeTuple.make(_state.ctl(),mem,_state.ret());
}
@Override public Node idealize() { return null; }
@@ -36,9 +36,16 @@ public class SummaryNode extends CFGNode implements MultiNode {
if( dep.addDepForwards(in(0))._type.isHigh() ) return TypeMem.START;
TypeMem mem = (TypeMem)_state.mem();
TypeMem outside = CodeGen.CODE._start.outside(dep);
- for( int i = 2; i<nIns(); i++ ) mem = mem.escapesFrom(dep.addDepForwards(in(i))._type,outside);
+ for( int i = firstArgument(); i<nIns(); i++ ) mem = mem.escapesFrom(dep.addDepForwards(in(i))._type,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/chapter30/src/main/java/com/seaofnodes/simple/parser/Expressions.java b/chapter30/src/main/java/com/seaofnodes/simple/parser/Expressions.java
index 247d974e..e664f16c 100644
--- a/chapter30/src/main/java/com/seaofnodes/simple/parser/Expressions.java
+++ b/chapter30/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/chapter30/src/main/java/com/seaofnodes/simple/parser/Functions.java b/chapter30/src/main/java/com/seaofnodes/simple/parser/Functions.java
index 5175acc6..9a7ed396 100644
--- a/chapter30/src/main/java/com/seaofnodes/simple/parser/Functions.java
+++ b/chapter30/src/main/java/com/seaofnodes/simple/parser/Functions.java
@@ -30,7 +30,7 @@ final class Functions {
BitSet finals = new BitSet();
Type self = P.types.implicitSelfType();
ts.push(self);
- ids.push(self == TypePtr.PTR ? "#self" : "self");
+ ids.push("self");
// Parse other arguments
P._lexer.skipWhiteSpace();
@@ -85,6 +85,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);
@@ -300,8 +304,16 @@ final class Functions {
// All args, "as-if" called externally
for( int i = 0; i<ids.length; i++ ) {
Type t = sig.arg(i);
+ // 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
- Node parm = new ParmNode(ids[i],i+2,t,fun,ConstantNode.seed(fun.defaultArg(i)).peephole()).peephole();
+ Node parm = new ParmNode(ids[i],i+2,t,fun,ConstantNode.seed(i==0 && t instanceof TypeMemPtr ? t : fun.defaultArg(i)).peephole()).peephole();
if( t instanceof TypeMemPtr ptr && ptr._partial ) parm = new ConstructNode(parm).peephole();
P._scope.define(ids[i], t, (i==0 && ids[0]=="self") || (finals!=null && finals.get(i)), parm, loc);
}
diff --git a/chapter30/src/main/java/com/seaofnodes/simple/parser/References.java b/chapter30/src/main/java/com/seaofnodes/simple/parser/References.java
index 6335bad2..134fee90 100644
--- a/chapter30/src/main/java/com/seaofnodes/simple/parser/References.java
+++ b/chapter30/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;
@@ -142,19 +128,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
@@ -303,14 +276,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) {
- int kx = P._scope.kindx(var), fx = P._scope.enclosingFuncOrDecl();
+ // 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( kx==fx || kx+1==fx ) return defaultSelf();
- 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();
+ 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;
+ // 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) {