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 --- chapter30/README.md | 3 + chapter30/docs/30-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/Chapter20Test.java | 4 +- .../java/com/seaofnodes/simple/Chapter21Test.java | 6 +- .../java/com/seaofnodes/simple/Chapter22Test.java | 6 +- .../seaofnodes/simple/GlobalAssignmentTest.java | 71 ++++++++++++++++ .../java/com/seaofnodes/simple/ReceiverTest.java | 94 ++++++++++++++++++++++ 15 files changed, 292 insertions(+), 66 deletions(-) create mode 100644 chapter30/src/test/java/com/seaofnodes/simple/GlobalAssignmentTest.java create mode 100644 chapter30/src/test/java/com/seaofnodes/simple/ReceiverTest.java (limited to 'chapter30') diff --git a/chapter30/README.md b/chapter30/README.md index 2e172e33..d8e32f80 100644 --- a/chapter30/README.md +++ b/chapter30/README.md @@ -12,6 +12,9 @@ squares are rejected, and play ends with a win or draw. Runnable programs, including sorting, file I/O, and shortest-path examples, live in [`docs/examples`](docs/examples/). +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. + ## Terminal input and diagnostics [`sys.io.getch()`](src/main/smp/sys/io.smp) reads one byte without waiting for diff --git a/chapter30/docs/30-grammar.md b/chapter30/docs/30-grammar.md index 189a7c04..7ff7b024 100644 --- a/chapter30/docs/30-grammar.md +++ b/chapter30/docs/30-grammar.md @@ -60,6 +60,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? memberName '=' functionExpression ';' ; memberName : IDENTIFIER | primitiveType ; @@ -235,10 +236,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/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; 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/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