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 +++++-------- .../java/com/seaofnodes/simple/Chapter05Test.java | 24 ++++++ .../java/com/seaofnodes/simple/Chapter20Test.java | 4 +- .../java/com/seaofnodes/simple/Chapter21Test.java | 4 +- .../java/com/seaofnodes/simple/Chapter22Test.java | 6 +- .../java/com/seaofnodes/simple/Chapter23Test.java | 2 +- .../seaofnodes/simple/GlobalAssignmentTest.java | 71 +++++++++++++++++ .../java/com/seaofnodes/simple/ReceiverTest.java | 93 ++++++++++++++++++++++ 14 files changed, 277 insertions(+), 61 deletions(-) create mode 100644 chapter31/src/test/java/com/seaofnodes/simple/GlobalAssignmentTest.java create mode 100644 chapter31/src/test/java/com/seaofnodes/simple/ReceiverTest.java (limited to 'chapter31/src') 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 N init(N n) { n._type=n.compute(); return n; } + + @Test public void testDoublingBeforeAndAfterStrengthReduction() { + for( boolean reduced : new boolean[]{false,true} ) + for( boolean reverse : new boolean[]{false,true} ) { + CodeGen code = new CodeGen(""); + Node ctrl = init(new CProjNode(code._start,0,"Ctrl")); + RegionNode region = init(new RegionNode(null,null,ctrl,ctrl)); + Node p = code.con(TypeInteger.BOT); + Node i = init(new MinusNode(p)); + Node twice = reduced ? init(new ShlNode(null,p,code.con(1))) : init(new AddNode(p,p,(byte)1)); + Node sum = init(new AddNode(i,p,(byte)1)); + PhiNode phi = init(new PhiNode("sum",region,reverse ? sum : twice,reverse ? twice : sum)); + phi.keep(); + Node factored = phi.idealize(); + assertTrue(factored instanceof AddNode); + assertSame(p,factored.in(2)); + assertTrue(factored.in(1) instanceof PhiNode); + assertSame(reverse ? i : p,factored.in(1).in(1)); + assertSame(reverse ? p : i,factored.in(1).in(2)); + phi.unkeep(); + } + } } diff --git a/chapter31/src/test/java/com/seaofnodes/simple/Chapter20Test.java b/chapter31/src/test/java/com/seaofnodes/simple/Chapter20Test.java index 273d76ba..226425ca 100644 --- a/chapter31/src/test/java/com/seaofnodes/simple/Chapter20Test.java +++ b/chapter31/src/test/java/com/seaofnodes/simple/Chapter20Test.java @@ -262,8 +262,8 @@ val hashCode = { !String self -> """; // Return the public hashCode wrapper; keep its helper private. testCPU(src,"x86_64_v2", "SystemV",28,null); - testCPU(src,"riscv" , "SystemV",8,null); - testCPU(src,"arm" , "SystemV",6,null); + testCPU(src,"riscv" , "SystemV",11,null); + testCPU(src,"arm" , "SystemV",11,null); } @Test diff --git a/chapter31/src/test/java/com/seaofnodes/simple/Chapter21Test.java b/chapter31/src/test/java/com/seaofnodes/simple/Chapter21Test.java index 347f2ce4..bd2a453a 100644 --- a/chapter31/src/test/java/com/seaofnodes/simple/Chapter21Test.java +++ b/chapter31/src/test/java/com/seaofnodes/simple/Chapter21Test.java @@ -386,8 +386,8 @@ val hashCode = { !String self -> """; // Return the public hashCode wrapper; keep its helper private. testCPU(src,"x86_64_v2", "SystemV",28,null); - testCPU(src,"riscv" , "SystemV",8,null); - testCPU(src,"arm" , "SystemV",6,null); + testCPU(src,"riscv" , "SystemV",11,null); + testCPU(src,"arm" , "SystemV",11,null); } @Test public void testStringExport() throws IOException { diff --git a/chapter31/src/test/java/com/seaofnodes/simple/Chapter22Test.java b/chapter31/src/test/java/com/seaofnodes/simple/Chapter22Test.java index c9791de4..f9146ae6 100644 --- a/chapter31/src/test/java/com/seaofnodes/simple/Chapter22Test.java +++ b/chapter31/src/test/java/com/seaofnodes/simple/Chapter22Test.java @@ -302,17 +302,17 @@ val p = { u8[~] str -> p("Hello, World!"); return 0; """; - TestC.runSF(src,"helloWorld", "Hello, World!",2); + TestC.runSF(src,"helloWorld", "Hello, World!",4); // Evaluate on RISC5 emulator - EvalRisc5 R5 = TestRisc5.build( src, "helloWorld", 0, 5, false); + EvalRisc5 R5 = TestRisc5.build( src, "helloWorld", 0, 8, false); int trap = R5.step(100); assertEquals(0,trap); assertEquals(0,R5.regs[riscv.A0]); assertEquals("Hello, World!",R5._stdout.toString()); // Evaluate on ARM emulator - EvalArm64 arm = TestArm64.build("helloWorld", src,0, 5, false); + EvalArm64 arm = TestArm64.build("helloWorld", src,0, 8, false); trap = arm.step(100); assertEquals(0,trap); assertEquals(0,arm.regs[0]); diff --git a/chapter31/src/test/java/com/seaofnodes/simple/Chapter23Test.java b/chapter31/src/test/java/com/seaofnodes/simple/Chapter23Test.java index 8667c0bd..f1ad4898 100644 --- a/chapter31/src/test/java/com/seaofnodes/simple/Chapter23Test.java +++ b/chapter31/src/test/java/com/seaofnodes/simple/Chapter23Test.java @@ -279,7 +279,7 @@ return _s.peek('q'); CodeGen code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck); assertEquals("return Phi(Region,0,1);", code.print()); assertEquals("1", Eval2.eval(code, 0)); - testCPU(src,"x86_64_v2", "win64",23,null); + testCPU(src,"x86_64_v2", "win64",31,null); }; @Test diff --git a/chapter31/src/test/java/com/seaofnodes/simple/GlobalAssignmentTest.java b/chapter31/src/test/java/com/seaofnodes/simple/GlobalAssignmentTest.java new file mode 100644 index 00000000..51ed10c6 --- /dev/null +++ b/chapter31/src/test/java/com/seaofnodes/simple/GlobalAssignmentTest.java @@ -0,0 +1,71 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.codegen.CodeGen; +import com.seaofnodes.simple.parser.Parser; +import org.junit.Test; +import static org.junit.Assert.*; + +public class GlobalAssignmentTest { + private static final String SOURCE = """ + int value=1; + int writes=0; + u8 small=0; + val set={int n -> value=n; return value;}; + val set_noInline={int n -> + if( n<0 ) value=3; + else value=n; + return value; + }; + val rhs_noInline={ -> writes=writes+1; return value+1;}; + val shadow={int value -> value=42; return value;}; + struct Writer { + int seen; + new Writer={int n -> value=n; seen=value;}; + }; + struct InitWriter { int seen=(value=17); }; + val main={ -> + if( set(5)!=5 || value!=5 ) return 1; + if( set_noInline(-1)!=3 || value!=3 ) return 2; + if( set_noInline(8)!=8 || value!=8 ) return 3; + int result=(value=rhs_noInline()); + if( result!=9 || value!=9 || writes!=1 ) return 4; + value=writes=11; + if( value!=11 || writes!=11 ) return 5; + if( shadow(0)!=42 || value!=11 ) return 6; + int wide=(small=257); + if( wide!=257 || small!=1 ) return 7; + val constructed=new Writer(13); + if( value!=13 || constructed.seen!=13 ) return 8; + val initialized=new InitWriter; + // Default expressions run at declaration time, not per instance. + if( value!=13 || initialized.seen!=17 ) return 9; + return 0; + }; + return main(); + """; + + @Test public void storesGlobalMemory() throws Exception { + assertEquals("0",Eval2.eval(new CodeGen(SOURCE).driver(CodeGen.Phase.TypeCheck),0)); + TestC.runSF(SOURCE,"global_assignment","",-1); + } + + @Test public void rejectsFinalGlobal() { + try { + new CodeGen("val value=1; val set={ -> value=2; }; return set();") + .driver(CodeGen.Phase.TypeCheck); + fail("Assigned a final global"); + } catch( Parser.ParseException e ) { + assertEquals("Cannot reassign final 'value'",e.getMessage()); + } + } + + @Test public void rejectsIncompatibleGlobalValue() { + try { + new CodeGen("struct S { int x; }; int value=1; val set={ -> value=new S; }; set(); return value;") + .driver(CodeGen.Phase.TypeCheck); + fail("Stored an object in an integer global"); + } catch( Parser.ParseException expected ) { + assertTrue(expected.getMessage(),expected.getMessage().contains("not of declared type")); + } + } +} diff --git a/chapter31/src/test/java/com/seaofnodes/simple/ReceiverTest.java b/chapter31/src/test/java/com/seaofnodes/simple/ReceiverTest.java new file mode 100644 index 00000000..83bf6768 --- /dev/null +++ b/chapter31/src/test/java/com/seaofnodes/simple/ReceiverTest.java @@ -0,0 +1,93 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.codegen.*; +import com.seaofnodes.simple.type.TypeInteger; +import com.seaofnodes.simple.util.Ary; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.Test; +import static org.junit.Assert.*; + +public class ReceiverTest { + @Test public void classAndInstanceFields() throws Exception { + String source=""" + int total=1; + val forward={ -> late(); }; + val early={ -> total; }; + val late={ -> early(); }; + val add_noInline={int n -> self.total=self.total+n; return total;}; + val read_noInline={ -> self.total; }; + val nested={ -> + val inc={ -> self.total++; }; + inc(); + return total; + }; + struct Counter { + int total=100; + val read=read_noInline; + }; + struct Writer { + int count=2; + val step={ -> self.count++; total=count; return total; }; + }; + val table=new u16[~2]{7,9}; + val lookup={u8 c -> c<2 ? table[c] : 0;}; + val main={ -> + total=1; + if( forward()!=1 ) return 4; + if( lookup(1)!=9 || lookup(2)!=0 ) return 5; + if( add_noInline(5)!=6 || nested()!=7 ) return 1; + val counter=new Counter; + if( counter.read()!=7 || counter.total!=100 ) return 2; + val a=new Writer, b=new Writer; + if( a.step()!=3 || total!=3 || a.count!=3 || b.count!=2 ) return 3; + return 0; + }; + return main(); + """; + assertEquals("0",Eval2.eval(new CodeGen(source).driver(CodeGen.Phase.TypeCheck),0)); + TestC.runSF(source,"class_instance_receivers","",-1); + } + + @Test public void importedCallbackKeepsItsClass() throws Exception { + Path build=Path.of("build/objs/receivers"); + Files.createDirectories(build); + Path lib=Files.createTempDirectory(build,"classes-"); + new CodeGen(null,lib.toString(),null,"Owner", + "int value=7; val get_noInline={ -> self.value; };",123,true,TypeInteger.BOT) + .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false); + new CodeGen(null,lib.toString(),null,"Forwarder", + "int value=99; val apply_noInline={ {int} action -> action()+self.value; };",123,true,TypeInteger.BOT) + .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false); + var paths=new Ary<>(new String[]{lib.toString()}); + String source="return Forwarder.apply_noInline(Owner.get_noInline)==106 ? 0 : 1;"; + CodeGen code=new CodeGen(null,null,paths,"ReceiverClient",source,123,true,TypeInteger.BOT) + .driver(CodeGen.Phase.TypeCheck); + assertEquals("0",Eval2.eval(code,0)); + TestC.run(source,"imported_receivers",paths,TestC.CALL_CONVENTION,null,null,"",-1); + } + + @Test public void classReceiverHasNoNativeArgument() throws Exception { + String source=""" + int base=10; + val calc={int a,int b,int c,int d,int e,int f,int g -> + self.base+a+2*b+3*c+4*d+5*e+6*f+7*g; + }; + val callback_noInline={int n -> self.base+n;}; + { {int -> i64} int -> i64 } invoke="C"; + val viaC={int n -> invoke(callback_noInline,n);}; + """; + Path build=Path.of("build/objs/receivers"); + Files.createDirectories(build); + Path driver=build.resolve("native_receiver.c"); + Files.writeString(driver,""" + typedef long long (__attribute__((CALL_CONV)) *action_t)(int); + __attribute__((CALL_CONV)) long long invoke(action_t action, int n) { return action(n); } + __attribute__((CALL_CONV)) extern long long calc(int,int,int,int,int,int,int); + __attribute__((CALL_CONV)) extern long long viaC(int); + int main(void) { return calc(1,2,3,4,5,6,7)!=150 || viaC(5)!=15; } + """); + TestC.run(source,"native_receiver",null,TestC.CALL_CONVENTION, + TestC.CALL_CONVENTION.equals("win64") ? "ms_abi" : "sysv_abi",driver.toString(),"",-1); + } +} -- cgit v1.2.3