From bbe824d4c5c998ad76ff685d9be67838c7ce86a8 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sat, 5 Dec 2026 09:08:46 -0800 Subject: Unify class and instance initialization through field memory Emit initializer stores in source order so helpers observe prior writes and their updates remain visible. Remove deferred class-field stores and preserve guards for reloaded fields. Fix exposed load, Phi, shift, and register-allocation cases. Preserve CallEnd memory bounds before finite targets link, without imposing peephole ordering. Use identity comparisons for interned class and field names. Add initializer and CallEnd regressions, keep historical SSA tests explicitly local, and document initialization and formatting rules. Full make -j8 -k tests passes, including native, fuzzer, and lattice checks. --- chapter33/docs/33-grammar.md | 13 ++++-- .../com/seaofnodes/simple/codegen/RegAlloc.java | 5 ++- .../com/seaofnodes/simple/codegen/StaticData.java | 2 +- .../com/seaofnodes/simple/node/CallEndNode.java | 5 ++- .../com/seaofnodes/simple/node/ConFldOffNode.java | 2 +- .../com/seaofnodes/simple/node/FldOffNode.java | 2 +- .../com/seaofnodes/simple/node/FormatNode.java | 2 +- .../java/com/seaofnodes/simple/node/LoadNode.java | 47 ++++++++++++++------- .../java/com/seaofnodes/simple/node/PhiNode.java | 4 ++ .../java/com/seaofnodes/simple/node/SarNode.java | 4 +- .../java/com/seaofnodes/simple/node/StoreNode.java | 6 ++- .../com/seaofnodes/simple/parser/ControlFlow.java | 9 +++- .../com/seaofnodes/simple/parser/Functions.java | 15 +++---- .../com/seaofnodes/simple/parser/References.java | 26 ++++++++---- .../com/seaofnodes/simple/parser/Statements.java | 4 +- .../java/com/seaofnodes/simple/parser/Structs.java | 48 ++++------------------ .../java/com/seaofnodes/simple/parser/Types.java | 2 +- .../java/com/seaofnodes/simple/type/Field.java | 2 +- .../com/seaofnodes/simple/type/TypeStruct.java | 2 +- .../java/com/seaofnodes/simple/CallEndTest.java | 25 +++++++++++ .../java/com/seaofnodes/simple/Chapter05Test.java | 14 ++++++- .../java/com/seaofnodes/simple/Chapter06Test.java | 2 + .../java/com/seaofnodes/simple/Chapter07Test.java | 6 +++ .../java/com/seaofnodes/simple/Chapter08Test.java | 4 +- .../java/com/seaofnodes/simple/Chapter10Test.java | 10 +++-- .../java/com/seaofnodes/simple/Chapter13Test.java | 6 ++- .../java/com/seaofnodes/simple/Chapter14Test.java | 2 + .../java/com/seaofnodes/simple/Chapter17bTest.java | 5 ++- .../java/com/seaofnodes/simple/Chapter19Test.java | 5 ++- .../java/com/seaofnodes/simple/Chapter20Test.java | 6 ++- .../java/com/seaofnodes/simple/Chapter21Test.java | 6 ++- .../java/com/seaofnodes/simple/Chapter23Test.java | 4 +- .../java/com/seaofnodes/simple/Chapter24Test.java | 2 + .../seaofnodes/simple/GlobalAssignmentTest.java | 29 +++++++++++-- .../seaofnodes/simple/SeparateCompilationTest.java | 15 +++++++ skills/chapter25-codex-notes.md | 6 ++- 36 files changed, 238 insertions(+), 109 deletions(-) diff --git a/chapter33/docs/33-grammar.md b/chapter33/docs/33-grammar.md index 7e9ef8cd..2de46bb9 100644 --- a/chapter33/docs/33-grammar.md +++ b/chapter33/docs/33-grammar.md @@ -256,8 +256,12 @@ one-pass IR builder: `Declarations` and `Types` use speculative parsing and cursor restore; qualified type lookup also stops before a suffix that belongs to an expression. - `References` resolves bare fields through their receiver, so assignments, - reads, and updates share the field-access path. Locals use SSA; class - initializer declarations retain their Scope values until initializer exit. + reads, and updates share the field-access path, including inside the owning + initializer. Both class and instance declarations emit Stores in source + order; helper calls see the preceding writes and subsequent reads see the + helpers' effects. Instance defaults run once per instance. Class fields use + public alias memory; an instance under construction uses its private memory. + Lexically local bindings use SSA; file-level bindings are class fields. Global functions receive their owning class singleton as `self`, including when invoked through a callback. Instance methods receive their object. - `for` updates are scanned first and parsed after the body, where their effects @@ -265,8 +269,9 @@ one-pass IR builder: discovery, so later fields have local slots. Both use `Lexer.skipTo`, which ignores delimiters inside comments and quoted text. - `Structs.parseInitializer` parses a file or struct body as a function while - discovering fields. `Functions.parseBody` still owns function scope/return - construction; moving it does not change constructor or memory semantics. + discovering fields. `declareField` emits both kinds of field initialization; + `finishInitializer` closes their types without emitting deferred Stores. + `Functions.parseBody` owns function scope/return construction. The syntax chart does not describe all name/type restrictions. In particular, prefix updates require an already-known non-pointer binding, and a private-class diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/RegAlloc.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/RegAlloc.java index c3be4b71..585cdbf1 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/RegAlloc.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/RegAlloc.java @@ -338,7 +338,10 @@ public class RegAlloc { if( splitEmptyMaskByUse(round,lrg) ) return true; } - if( lrg._mask.isEmpty() && lrg._multiDef && lrg._1regUseCnt > 1 && lrg._1regDefCnt <= 1 ) { + // Several call results can share this Phi web (all defined in RAX), + // while its arguments require different registers. Isolate those uses + // even when there is more than one constrained definition. + if( lrg._mask.isEmpty() && lrg._multiDef && lrg._1regUseCnt > 1 ) { if( splitEmptyMaskSingleUses(round,lrg) ) return true; } diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/StaticData.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/StaticData.java index 3d3a40dd..55abab52 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/StaticData.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/StaticData.java @@ -129,7 +129,7 @@ public final class StaticData { if( f._extern ) continue; int off=ts.offset(idx); while( bits.size()=CodeGen.Phase.IterPostOpto.ordinal() && classInitialized(mem,new BitSet()) ) @@ -207,7 +208,7 @@ public class LoadNode extends MemOpNode { // val = ptr.x; ptr.x = val; if( mem() instanceof PhiNode memphi && !(memphi instanceof ParmNode) && (memphi instanceof MemPhiNode || (memphi._type instanceof TypeMem tm && tm._one)) && - memphi.region()._type == Type.CONTROL && memphi.nIns()== 3 && + memphi.region()._type == Type.CONTROL && memphi.nIns()>2 && // Retain control on both copies only if it dominates both paths. // The Region itself, or a later range check, cannot move to its inputs. (in(0)==null || cfg0().domLCA(memphi.region().idom(this),this)==cfg0()) && @@ -220,8 +221,10 @@ public class LoadNode extends MemOpNode { // is also loop invariant. A pointer Phi may select another object. Node stop = memphi.region() instanceof LoopNode && !(ptr instanceof PhiNode p && p.region()==memphi.region()) ? memphi : null; - if( find(memphi.in(2),ptr,stop,new BitSet())!=null || - (!(memphi.region() instanceof LoopNode) && find(memphi.in(1),ptr,null,new BitSet())!=null) ) { + boolean folds = false; + for( int i=memphi.region() instanceof LoopNode ? 2 : 1; i and user constructors return their current private memory // Unstack the {break,continue,return} scopes. @@ -297,16 +297,14 @@ final class Functions { // Last expression is the return if( P.ctrl()._type==Type.CONTROL ) { // Constructors get special return types - if( fun.isClz() ) { - assert last!=null; - } else if( isCtor ) { + if( isCtor && !fun.isClz() ) { last = P.construction.selfMem(); } addReturn(last); // exit path is now dead (nothing live after a "return") } if( isInit ) - P.structs.finishInitializer(typeName,fun.isClz(),self); + P.structs.finishInitializer(typeName,self); // Build a return from the _returnScope. // Can be no returns for never-exit functions @@ -385,15 +383,12 @@ final class Functions { expr.keep(); // Force the pending lazy bulk-memory Phi. P.mem(); // Force a pending lazy bulk-memory Phi - // Class fields are still stored at exit. Instance fields already - // travel in expr's private memory; only their scope layout is retained. + // Both initializers already carry their field Stores in memory. + // Retain only the declaration layout across early-return scopes. int lexN = P._scope.enclosingFuncOrDecl(); Kind.Func k = (Kind.Func)P._scope._kinds.at(lexN); if( FunNode.isInit(k._name) ) { int nestedLexSize = lexN+1 < P._scope.depth() ? P._scope._kinds.at(lexN+1)._lexSize : P._scope.nIns(); - if( k._name.endsWith(".") ) - for( int i = k._lexSize; i receives the // broad allocation pointer, while private memory tracks initialized fields. - self._declaredType = self._type = isClz ? TypeMemPtr.make((byte)2,tself,true) : TypeMemPtr.make(constructorRecv(tself)).withPartial(true); FunNode fun = self.fun(); + self._declaredType = self._type = fun.isClz() ? TypeMemPtr.make((byte)2,tself,true) : TypeMemPtr.make(constructorRecv(tself)).withPartial(true); + P._code.addAll(self._outputs); + self.moveDepsToWorklist(); fun.setSig(fun.sig().makeFrom(self._type,0)); - - // When can _returnScope be null here? A never-exit constructor will - // not have any returns, and thus no need to gather values and store - // them into the (never) constructed object - if( P.functions._returnScope==null ) - return; - - // Class singleton fields retain their store-at-exit path. Instance - // initializers already returned their current private memory. - if( !isClz ) return; // Instance fields already have their Stores. - Node mmm = P.functions._returnScope.mem(1); - - // Store constructor results into fields - for( int i = 0; i type }` or `{ type }`. diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/Field.java b/chapter33/src/main/java/com/seaofnodes/simple/type/Field.java index beaa719c..5b661e73 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/Field.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/Field.java @@ -115,7 +115,7 @@ public class Field extends Type { int hash() { return _fname.hashCode() ^ _t.hashCode() ^ _alias ^ (_final ? 1024 : 0) ^ (_extern ? 2048 : 0); } private boolean static_eq( Field f ) { - return _fname.equals(f._fname) && _alias==f._alias && _final==f._final && _extern==f._extern && _t !=null && f._t !=null; + return _fname==f._fname && _alias==f._alias && _final==f._final && _extern==f._extern && _t !=null && f._t !=null; } @Override boolean eq( Type t ) { Field f = (Field)t; diff --git a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeStruct.java b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeStruct.java index 026a4018..86728184 100644 --- a/chapter33/src/main/java/com/seaofnodes/simple/type/TypeStruct.java +++ b/chapter33/src/main/java/com/seaofnodes/simple/type/TypeStruct.java @@ -338,7 +338,7 @@ public class TypeStruct extends Type { int len = Math.min(_fields.length,that._fields.length); for( int i=0; i>32))+1)<<32)>>32)+Sar);", code.print()); } @@ -73,13 +76,16 @@ return a+b;"""); public void testIfMerge2() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. int a=arg+1; int b=arg+2; if( arg==1 ) b=b+a; else a=b+1; -return a+b;"""); +return a+b; +} +"""); code.driver(CodeGen.Phase.FinalizeIR); assertEquals("return ((((Phi(Region,arg,(((arg+2)<<32)>>32))+1)<<32)>>32)+(((Phi(Region,(((arg+1)<<32)>>32),2)+Phi(Region,Sar,arg))<<32)>>32));", code.print()); } @@ -108,6 +114,7 @@ return a; public void testMerge4() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. int a=0; int b=0; if( arg ) @@ -115,6 +122,7 @@ if( arg ) if( arg==0 ) b=2; return arg+a+b; +} """); code.driver(CodeGen.Phase.FinalizeIR); String ir = code.print(); @@ -127,12 +135,14 @@ return arg+a+b; public void testMerge5() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. int a=arg==2; if( arg==1 ) { a=arg==3; } return a; +} """); code.driver(CodeGen.Phase.FinalizeIR); assertEquals("return (arg==Phi(Region,3,2));", code.print()); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter06Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter06Test.java index d1af5d14..39809ae0 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter06Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter06Test.java @@ -246,6 +246,7 @@ return a+b; @Test public void testDemo2NonConst() { CodeGen code = new CodeGen(""" +{ // Local SSA regression; file-level bindings are class fields. int a = 0; int b = 1; int c = 0; @@ -256,6 +257,7 @@ if( arg ) { else b = 3; } return a+b+c; +} """); code.driver(CodeGen.Phase.FinalizeIR); assertEquals("return Phi(Region,6,1,5);", code.print()); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter07Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter07Test.java index 20786571..6b53c394 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter07Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter07Test.java @@ -42,6 +42,7 @@ return a; public void testWhileNested() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. int sum = 0; int i = 0; while(i < arg) { @@ -53,6 +54,7 @@ while(i < arg) { } } return sum; +} """); code.driver(CodeGen.Phase.FinalizeIR); assertEquals("return Phi(Loop,0,Phi(Loop,Phi_sum,(((Phi_sum+Phi(Loop,0,(((Phi_j+1)<<32)>>32)))<<32)>>32)));", code.print()); @@ -62,6 +64,7 @@ return sum; public void testWhileScope() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. int a = 1; int b = 2; while(a < 10) { @@ -69,6 +72,7 @@ while(a < 10) { else b = 4; } return b; +} """); code.parse(); assertEquals("return Phi(Loop,2,Phi(Region,Phi_b,4));", code.print()); @@ -131,9 +135,11 @@ return a; public void testWhile2() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. int a = 1; while(arg) a = 2; return a; +} """); code.parse(); assertEquals("return Phi(Loop,1,2);", code.print()); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter08Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter08Test.java index 249beb11..d88016b9 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter08Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter08Test.java @@ -34,6 +34,7 @@ return arg; public void testEx5() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. int a = 1; while(arg < 10) { arg = arg + 1; @@ -44,7 +45,8 @@ while(arg < 10) { a = a + 1; } return a; - """); +} +"""); code.driver(CodeGen.Phase.FinalizeIR); assertEquals("return Phi(Loop,1,Phi(Region,Phi_a,(((Phi_a+1)<<32)>>32),Phi_a));", code.print()); assertTrue(code.ctrl() instanceof CProjNode); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter10Test.java index 13950789..8d9b5b85 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter10Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter10Test.java @@ -105,6 +105,7 @@ public class Chapter10Test { public void testFuzzer() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. int a = arg/3; int b = arg*5; int x = arg*7; @@ -121,6 +122,7 @@ if( (arg/13)==0 ) { } int r = g+h; return p-r; +} """); code.driver(CodeGen.Phase.FinalizeIR); assertEquals("return 0;", code.print()); @@ -386,16 +388,16 @@ return ret.v0; CodeGen code = new CodeGen( """ int cnt = 1; -// cnt is incremented exactly once, it is not part of s0 constructor -// hence s0.x is always 1, and cnt remains at 2 +// Each instance initializer increments the class field. struct s0 { int x = cnt++; }; s0 a = new s0(); s0 b = new s0(); -// a.x==1, b.x==1, cnt==2 +// a.x==1, b.x==2, cnt==3 return a.x * 100 + b.x * 10 + cnt; """); code.driver(CodeGen.Phase.FinalizeIR); - assertEquals("Stop[ return 112; return Test.s0; ]", code.print()); + assertEquals("Stop[ return 123; return Test.s0; ]", code.print()); + assertEquals("123",Eval2.eval(code,0)); } diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter13Test.java index b2d773ee..7f2fa6a1 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter13Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter13Test.java @@ -11,6 +11,7 @@ public class Chapter13Test { public void testPrimes() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. if( arg < 2 ) return 0; int primeCount = 1; int prime = 3; @@ -33,6 +34,7 @@ while( prime <= arg ) { prime = prime + 2; } return primeCount; +} """); code.driver(CodeGen.Phase.FinalizeIR); assertEquals("return Phi(Region,0,Phi(Loop,1,Phi(Region,Phi_primeCount,Phi_primeCount,(((Phi_primeCount+1)<<32)>>32))));", code.print()); @@ -143,6 +145,7 @@ return i; public void testAntiDeps5() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. struct _S { int f; }; !_S !v = new _S; while(1) { @@ -151,9 +154,10 @@ while(1) { v.f = 2; } return v.f; +} """); code.driver(CodeGen.Phase.FinalizeIR); - assertEquals("return ((Phi(Loop,0,2)<<32)>>32);", code.print()); + assertEquals("return Phi(Loop,0,2);", code.print()); for( int arg : new int[]{-1,0,1,3,100} ) assertEquals("0",Eval2.eval(code,arg)); } diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter14Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter14Test.java index bc2ad8d3..640416ff 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter14Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter14Test.java @@ -55,9 +55,11 @@ return b; public void testU8While() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. u8 b = 123; while( b ) b = b + 456;// Truncate return b; +} """); code.driver(CodeGen.Phase.FinalizeIR); assertEquals("return 0;", code.print()); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter17bTest.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter17bTest.java index 8a66dcc5..2cd7cbd8 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter17bTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter17bTest.java @@ -482,10 +482,12 @@ return sum; @Test public void testFor1() { CodeGen code = new CodeGen(""" +{ // Local SSA regression; file-level bindings are class fields. int sum=0, i=0; for( ; i>32)))<<32)>>32));", code.print()); @@ -588,7 +590,8 @@ for( ; ptr; ptr = ptr.next ) return sum; """); code.driver(CodeGen.Phase.FinalizeIR); - assertEquals("return Phi(Loop,0,(((Phi_sum+.i)<<32)>>32));", code.print()); + // Class fields can retain constructor bodies; inspect this entry's return. + assertEquals("return Phi(Loop,0,(((Phi_sum+.i)<<32)>>32));", code.link(code.compunit()).ret().toString()); assertEquals("45", Eval2.eval(code, 10)); } diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter19Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter19Test.java index 50fecdfd..0d97a91c 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter19Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter19Test.java @@ -294,13 +294,16 @@ return a;"""); public void testIfMerge2() { CodeGen code = new CodeGen( """ +{ // Local SSA regression; file-level bindings are class fields. int a=arg+1; int b=arg+2; if( arg==1 ) b=b+a; else a=b+1; -return a+b;"""); +return a+b; +} +"""); code.driver(Phase.LocalSched,"x86_64_v2", "SystemV"); assertEquals("return (add,(sari,(shli,(inc,Phi(Region,arg,(sari,(shli,(addi,arg))))))),(sari,(shli,(add,Phi(Region,(sari,(shli,(inc,arg))),2),Phi(Region,sari,arg)))));", code.print()); } diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter20Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter20Test.java index 008902c5..bf4ec581 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter20Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter20Test.java @@ -158,6 +158,7 @@ return _sqrt(x) + _sqrt(y); public void testNewtonFloat() { String src = """ +{ // Local SSA regression; file-level bindings are class fields. val _test_sqrt_noInline = { flt x -> flt epsilon = 1e-15; flt guess = x; @@ -169,6 +170,7 @@ val _test_sqrt_noInline = { flt x -> } }; flt farg = arg; return _test_sqrt_noInline(farg) + _test_sqrt_noInline(farg+2.0); +} """; testCPU(src,"x86_64_v2", "SystemV",40,null); testCPU(src,"riscv" , "SystemV",17,null); @@ -213,6 +215,7 @@ flt farg = arg; return _test_sqrt_noInline(farg) + _test_sqrt_noInline(farg+2.0) public void testArray1() { String src = """ +{ // Local SSA regression; file-level bindings are class fields. int[] !ary = new int[arg]; // Fill [0,1,2,3,4,...] for( int i=0; i @Test public void testLoop2() { String src = """ +{ // Local SSA regression; file-level bindings are class fields. int i = 0; while(true) { i += 1; @@ -439,6 +442,7 @@ while(true) { continue; } return i; +} """; testCPU(src,"x86_64_v2", "win64" ,0,"return (!=0)(sari,(shli,(inc,Phi(Loop,0,Phi(Region,(!=0),(!=0),0)))));"); testCPU(src,"riscv" , "SystemV",0,"return (!=0)( ( ( Phi(Loop,0,Phi(Region,(!=0),(!=0),0)) + #1 ) << #32 ) >> #1056 );"); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter23Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter23Test.java index 8fb781f9..8115747b 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter23Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter23Test.java @@ -226,6 +226,7 @@ return (x == 0 && y == 1 && z==-1) && switch && g @Test public void testFuncCall() throws IOException { String src = """ +{ // Local SSA regression; file-level bindings are class fields. int a = 1; int b = 1; var sq_noInline = { int x -> @@ -234,8 +235,9 @@ var sq_noInline = { int x -> return (a && sq_noInline(0)) ? 1 // Error : 0; // Expected +} """; - TestC.runSF( src, "and3", "", 2); + TestC.runSF( src, "and3", "", 0); // Evaluate on RISC5 emulator EvalRisc5 R5 = TestRisc5.build( src, "and3", 0, 2, false); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter24Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter24Test.java index f4c4f12f..93eb35ff 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/Chapter24Test.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter24Test.java @@ -99,6 +99,7 @@ return (10 < something_specific < 20) @Test public void testStack3() throws IOException { String src =""" +{ // Local SSA regression; file-level bindings are class fields. var sq_noInline = { int x -> x*x; }; @@ -106,6 +107,7 @@ int score = 4; return (sq_noInline(2) <= score < sq_noInline(3)) ? 0 // Expected : 1; // Error +} """; TestC.runSF( src, "stacked_r_3", "",0); diff --git a/chapter33/src/test/java/com/seaofnodes/simple/GlobalAssignmentTest.java b/chapter33/src/test/java/com/seaofnodes/simple/GlobalAssignmentTest.java index 51ed10c6..ba8c721e 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/GlobalAssignmentTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/GlobalAssignmentTest.java @@ -37,8 +37,9 @@ public class GlobalAssignmentTest { 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; + // Default expressions run in the instance initializer, including + // their writes to class fields. + if( value!=17 || initialized.seen!=17 ) return 9; return 0; }; return main(); @@ -46,7 +47,29 @@ public class GlobalAssignmentTest { @Test public void storesGlobalMemory() throws Exception { assertEquals("0",Eval2.eval(new CodeGen(SOURCE).driver(CodeGen.Phase.TypeCheck),0)); - TestC.runSF(SOURCE,"global_assignment","",-1); + // The native entry calls main itself, after initializing the class. + TestC.runSF(SOURCE.replace("return main();",""),"global_assignment","",-1); + } + + @Test public void initializerHelpersShareMemory() throws Exception { + String src=""" + int value=7; + val read_noInline={ -> value; }; + val write_noInline={ int n -> value=n; return value; }; + int first=read_noInline(); + value=9; + int second=read_noInline(); + int returned=write_noInline(11); + int third=value; + if( first!=7 || second!=9 || returned!=11 || third!=11 ) return 1; + for( int i=0; i<3; i++ ) { + value+=2; + if( read_noInline()!=13+2*i ) return 2; + } + return value!=17; + """; + assertEquals("0",Eval2.eval(new CodeGen(src).driver(CodeGen.Phase.TypeCheck),0)); + TestC.runSF(src,"initializer_helpers","",-1); } @Test public void rejectsFinalGlobal() { diff --git a/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java b/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java index be20b41d..f7dd0d0b 100644 --- a/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java +++ b/chapter33/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java @@ -10,6 +10,21 @@ import org.junit.Test; import static org.junit.Assert.*; public class SeparateCompilationTest { + @Test public void testGlobalReadAfterImportedInitializer() throws Exception { + Path lib=Files.createTempDirectory(Files.createDirectories(Path.of("build/objs/separate")),"global-read-"); + var paths=new Ary<>(new String[]{lib.toString()}); + new CodeGen(null,lib.toString(),paths,"Library", + "val number_noInline={ ->7; };",123,true,TypeInteger.BOT) + .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false); + CodeGen code=new CodeGen(null,lib.toString(),paths,"Client",""" + var value=Library.number_noInline(); + val read_noInline={ ->value; }; + value=4294967297; + return read_noInline(); + """,123,true,TypeInteger.BOT).driver(CodeGen.Phase.TypeCheck); + assertEquals("4294967297",Eval2.eval(code,0)); + } + @Test public void testInferredImportedFieldStoreWidth() throws Exception { Path lib=Files.createTempDirectory(Files.createDirectories(Path.of("build/objs/separate")),"inferred-store-"); new CodeGen(null,lib.toString(),null,"Library", diff --git a/skills/chapter25-codex-notes.md b/skills/chapter25-codex-notes.md index 08bfa885..62f9a4e7 100644 --- a/skills/chapter25-codex-notes.md +++ b/skills/chapter25-codex-notes.md @@ -115,7 +115,11 @@ Useful implementation facts: Keep arithmetic compact, but space assignments, compound assignments, and relational operators. Preserve deliberate column alignment above other spacing -rules. Time values use `_ms`/`_msec` suffixes; timer IDs have a smaller unsigned +rules. Align parallel statements into columns so their similarities and +differences stand out (Cliff's 2-D or rectangular coding). For example, pad +`trueExits` by one space to align its assignment and RHS with `falseExits`. +Always parenthesize groups when an expression mixes `&&` and `||`; never rely +on their implicit precedence. Time values use `_ms`/`_msec` suffixes; timer IDs have a smaller unsigned type. Prefer named bounds and products of time units to large magic numbers. Build and test from the chapter directory using the existing Java harness; -- cgit v1.2.3