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 --- chapter29/README.md | 3 + chapter29/docs/29-grammar.md | 9 ++- .../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 | 16 +++- .../com/seaofnodes/simple/parser/References.java | 60 ++++++-------- .../java/com/seaofnodes/simple/Chapter05Test.java | 24 ++++++ .../java/com/seaofnodes/simple/Chapter09Test.java | 2 +- .../java/com/seaofnodes/simple/Chapter20Test.java | 6 +- .../java/com/seaofnodes/simple/Chapter21Test.java | 8 +- .../java/com/seaofnodes/simple/Chapter22Test.java | 6 +- .../seaofnodes/simple/GlobalAssignmentTest.java | 71 ++++++++++++++++ .../java/com/seaofnodes/simple/ReceiverTest.java | 94 ++++++++++++++++++++++ 16 files changed, 295 insertions(+), 69 deletions(-) create mode 100644 chapter29/src/test/java/com/seaofnodes/simple/GlobalAssignmentTest.java create mode 100644 chapter29/src/test/java/com/seaofnodes/simple/ReceiverTest.java (limited to 'chapter29') diff --git a/chapter29/README.md b/chapter29/README.md index ed39de98..03c9a116 100644 --- a/chapter29/README.md +++ b/chapter29/README.md @@ -14,6 +14,9 @@ backporting the cleanups, in part to show the normal progression of large project development. +This snapshot retains [chapter 28a's shared class receiver](../chapter28a/README.md#globals-use-the-same-receiver-as-fields) +and chapter 28b's imported-callback handling. + ## Following the grammar The old `Parser.java` had grown beyond 3,000 lines, mixing recursive descent, diff --git a/chapter29/docs/29-grammar.md b/chapter29/docs/29-grammar.md index cb231ce0..d580a28b 100644 --- a/chapter29/docs/29-grammar.md +++ b/chapter29/docs/29-grammar.md @@ -57,6 +57,7 @@ constructorParameter : type '^'? bindingModifier? IDENTIFIER ; // A method is an ordinary fixed function binding in a struct definition. // The compiler supplies self; it is not written in parameterList. +// Global functions use their owning class singleton as self. // Function types and expressions both receive their implicit receiver here. methodDeclaration : 'val' bindingModifier? IDENTIFIER '=' functionExpression ';' ; @@ -220,10 +221,10 @@ one-pass IR builder: - A statement beginning with `{` might be a block or a function-type binding. `Declarations` and `Types` use speculative parsing and cursor restore; qualified type lookup also stops before a suffix that belongs to an expression. -- Bare assignment, field assignment, and compound updates share the grammar's - precedence but use different IR paths. `References` decides between an SSA - local, a constructor field's local value, and an actual memory access. A - function-valued field may also supply an implicit receiver to a call. +- Bare fields resolve through their receiver, so reads, assignments, and + updates share the field-access path. Locals use SSA; class declarations + retain Scope values until initializer exit. Global functions receive their + owning class singleton as `self`; instance methods receive their object. - `for` updates are scanned first and parsed after the body, where their effects belong. Explicit constructor bodies are scanned first and parsed after field discovery, so later fields have local slots. Both use `Lexer.skipTo`, which diff --git a/chapter29/src/main/java/com/seaofnodes/simple/node/CallNode.java b/chapter29/src/main/java/com/seaofnodes/simple/node/CallNode.java index 91d27330..0a48e7aa 100644 --- a/chapter29/src/main/java/com/seaofnodes/simple/node/CallNode.java +++ b/chapter29/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; 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/chapter29/src/main/java/com/seaofnodes/simple/parser/Expressions.java b/chapter29/src/main/java/com/seaofnodes/simple/parser/Expressions.java index 93c07d8a..806e5f7f 100644 --- a/chapter29/src/main/java/com/seaofnodes/simple/parser/Expressions.java +++ b/chapter29/src/main/java/com/seaofnodes/simple/parser/Expressions.java @@ -31,6 +31,9 @@ final class Expressions { { P.pos(old); return parseExpression(); } P.references.checkAssignment(def); + // 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/chapter29/src/main/java/com/seaofnodes/simple/parser/Functions.java b/chapter29/src/main/java/com/seaofnodes/simple/parser/Functions.java index 14dee33b..4d80b100 100644 --- a/chapter29/src/main/java/com/seaofnodes/simple/parser/Functions.java +++ b/chapter29/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(); @@ -81,6 +81,10 @@ final class Functions { } finals.set(ids.size(),P.declarations.bindingFinal(t)); 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"); ts .push(t ); // Push type/arg pairs ids.push(id); P.match(","); @@ -291,8 +295,16 @@ final class Functions { // All args, "as-if" called externally for( int i = 0; i