From 0dcd3513a633479ce228f24826db4f09e4c0fc8b Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 16 Nov 2026 14:25:14 -0800 Subject: Phi fix Fix Phi/Add refactoring peephole to canonicalize regardless of peephole order. Treat class fcns as having `self` be the class pointer --- .../java/com/seaofnodes/simple/node/CallNode.java | 11 +++-- .../java/com/seaofnodes/simple/node/PhiNode.java | 38 +++++++++++---- .../com/seaofnodes/simple/node/RegionNode.java | 2 +- .../com/seaofnodes/simple/node/SummaryNode.java | 11 ++++- .../com/seaofnodes/simple/parser/Expressions.java | 3 ++ .../com/seaofnodes/simple/parser/Functions.java | 14 +++++- .../com/seaofnodes/simple/parser/References.java | 55 +++++++--------------- 7 files changed, 81 insertions(+), 53 deletions(-) (limited to 'chapter31/src/main/java/com') diff --git a/chapter31/src/main/java/com/seaofnodes/simple/node/CallNode.java b/chapter31/src/main/java/com/seaofnodes/simple/node/CallNode.java index 9e9c5224..9030d43d 100644 --- a/chapter31/src/main/java/com/seaofnodes/simple/node/CallNode.java +++ b/chapter31/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,14 +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; TypeMem outside = CodeGen.CODE._start.outside(dep); - for( int i = 2; i0 && tfp.arg(0)==TypePtr.PTR ? 3 : 2; i0 && sig.arg(0)==TypePtr.PTR ? 3 : 2; + } @Override boolean _upgradeType(HashMap types) { TypeTuple state = (TypeTuple)_state.upgradeType(types); if( state==_state ) return false; diff --git a/chapter31/src/main/java/com/seaofnodes/simple/parser/Expressions.java b/chapter31/src/main/java/com/seaofnodes/simple/parser/Expressions.java index ffc96f57..f791a34c 100644 --- a/chapter31/src/main/java/com/seaofnodes/simple/parser/Expressions.java +++ b/chapter31/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/chapter31/src/main/java/com/seaofnodes/simple/parser/Functions.java b/chapter31/src/main/java/com/seaofnodes/simple/parser/Functions.java index 84bd509e..a7726521 100644 --- a/chapter31/src/main/java/com/seaofnodes/simple/parser/Functions.java +++ b/chapter31/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/chapter31/src/main/java/com/seaofnodes/simple/parser/References.java b/chapter31/src/main/java/com/seaofnodes/simple/parser/References.java index 7724103e..91cf6859 100644 --- a/chapter31/src/main/java/com/seaofnodes/simple/parser/References.java +++ b/chapter31/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