From 19f9ea99114e9c1819239508f812d6521661f7bb Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Tue, 27 Oct 2026 19:06:47 -0700 Subject: Backport a bunch of fixes ARM encoding Extra fidx burned with no constructor Getting types during a parse to avoid a whole-program walk later --- .../src/main/java/com/seaofnodes/simple/Parser.java | 16 ++++++++++------ .../main/java/com/seaofnodes/simple/codegen/CodeGen.java | 8 -------- .../java/com/seaofnodes/simple/codegen/ParseAll.java | 10 +++------- .../com/seaofnodes/simple/node/cpus/arm/CallRRARM.java | 9 ++++++--- .../com/seaofnodes/simple/node/cpus/riscv/CallRRISC.java | 5 ++++- .../seaofnodes/simple/node/cpus/x86_64_v2/CallRX86.java | 7 +++++-- 6 files changed, 28 insertions(+), 27 deletions(-) (limited to 'chapter28a/src/main/java') diff --git a/chapter28a/src/main/java/com/seaofnodes/simple/Parser.java b/chapter28a/src/main/java/com/seaofnodes/simple/Parser.java index 5a9a5ca3..6bd02363 100644 --- a/chapter28a/src/main/java/com/seaofnodes/simple/Parser.java +++ b/chapter28a/src/main/java/com/seaofnodes/simple/Parser.java @@ -100,6 +100,9 @@ public class Parser { ScopeNode _continueScope; // Merge all the while-continues here ScopeNode _returnScope; // Merge all the function exits here Ary _frefs; // Forward refs created during this parse + // Layouts needed by field accesses in this source file. Resolve after + // parsing, since a later local struct declaration can satisfy the use. + public final LinkedHashSet _requiredTypes = new LinkedHashSet<>(); TypeStruct _ctorOpenStruct; // Open struct being initialized by an allocation constructor block. // Constructor exit types survive scope teardown until all fields are known. // A null entry denotes a constructor with no returning path. @@ -170,6 +173,7 @@ public class Parser { assert _frefs == null; _lexer = new Lexer(ref); _frefs = new Ary<>(FRefNode.class); + _requiredTypes.clear(); // Starting Scope has control, memory, initial arguments _scope = new ScopeNode(); _scope.define(ScopeNode.CTRL, Type.CONTROL , false, null, _lexer); @@ -1472,11 +1476,6 @@ public class Parser { return TypeFunPtr.make1((byte)2, true, targs, tret, _code.fidx(clzBare,order)); } - // A class type carries the constructor function as a required final field. - private TypeStruct classStruct( String typeName, TypeFunPtr init, boolean open ) { - return TypeStruct.make(addClzPrefix(typeName),open,Field.make(initFieldName(typeName),init,_code.alias(_ref._cname),true)); - } - private static String initFieldName( String typeName ) { if( startsClzPrefix(typeName) ) typeName = typeName.substring(clzPrefix.length()); @@ -1688,7 +1687,10 @@ public class Parser { private TypeMemPtr forwardRefType( String tname, TypeStruct tself ) { TYPES.put(tname,tself); String clzName = addClzPrefix( tname ); - TYPES.put(clzName, classStruct(tname,structInitSig(false,tself),true)); + // Only a struct declaration publishes an instance initializer. A + // forward type may name a file with just , so do not invent + // a constructor field (or overwrite an already parsed class). + TYPES.putIfAbsent(clzName,TypeStruct.make(clzName,true)); return TypeMemPtr.make(tself); } @@ -2741,6 +2743,8 @@ public class Parser { } // Field offset for an unknown type public Node fldoff( Node n, String name ) { + if( n._type instanceof TypeMemPtr ptr && ptr._obj._fref ) + _requiredTypes.add(ptr._obj._name); return new FldOffNode(n,name,loc()).peephole(); } public Node peep( Node n ) { diff --git a/chapter28a/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java b/chapter28a/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java index e6bace8c..8b3857a1 100644 --- a/chapter28a/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java +++ b/chapter28a/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java @@ -472,14 +472,6 @@ public class CodeGen { // Last check for bad programs public CodeGen typeCheck() { return phase(Phase.TypeCheck, _phase==Phase.FinalizeIR, () -> TypeCheck.run(this)); } - public static TypeStruct requiredStruct(Node n) { - // CNC: remove? cleanup? - if( n instanceof NewNode alloc ) return alloc._ts; - if( n instanceof ConFldOffNode off ) return off._ts; - if( n instanceof FldOffNode off && off.in(0)._type instanceof TypeMemPtr ptr ) return ptr._obj; - return null; - } - // --------------------------- // TypeCheck queued surviving CLInits during its existing error walk. public CodeGen lower() { diff --git a/chapter28a/src/main/java/com/seaofnodes/simple/codegen/ParseAll.java b/chapter28a/src/main/java/com/seaofnodes/simple/codegen/ParseAll.java index 171d9923..6ae4e4bb 100644 --- a/chapter28a/src/main/java/com/seaofnodes/simple/codegen/ParseAll.java +++ b/chapter28a/src/main/java/com/seaofnodes/simple/codegen/ParseAll.java @@ -172,13 +172,9 @@ public abstract class ParseAll { } private static void addRequiredTypes(CodeGen code, CompUnit cunit) { - LinkedHashSet required = new LinkedHashSet<>(); - code._stop.walk(n -> { - TypeStruct ts = CodeGen.requiredStruct(n); - if( ts!=null && ts._fref ) required.add(ts._name); - return null; - }); - for( String typeName : required ) { + for( String typeName : code.P._requiredTypes ) { + // A forward use can have been resolved later in this source file. + if( Parser.TYPES.get(typeName) instanceof TypeStruct ts && !ts._fref ) continue; try { CompUnit xcunit = findRequiredType(code,cunit,typeName); if( xcunit != null ) diff --git a/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/arm/CallRRARM.java b/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/arm/CallRRARM.java index 1949ae46..856af3b8 100644 --- a/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/arm/CallRRARM.java +++ b/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/arm/CallRRARM.java @@ -1,16 +1,19 @@ package com.seaofnodes.simple.node.cpus.arm; +import com.seaofnodes.simple.type.TypeFunPtr; import com.seaofnodes.isa.Arm64; import com.seaofnodes.simple.codegen.*; import com.seaofnodes.simple.node.*; import com.seaofnodes.simple.util.SB; public class CallRRARM extends CallNode implements MachNode { - CallRRARM(CallNode call) { super(call); } + final TypeFunPtr _tfp; + CallRRARM( CallNode call ) { super(call); _tfp = call.tfp(); } + @Override public TypeFunPtr tfp() { return _tfp; } @Override public String op() { return "callr"; } @Override public String label() { return op(); } @Override public RegMask regmap(int i) { - return i==_inputs._len + return i==_inputs._len-1 ? arm.RMASK // Function call target : arm.callInMask(tfp(),i,fun()._maxArgSlot); // Normal argument } @@ -19,7 +22,7 @@ public class CallRRARM extends CallNode implements MachNode { @Override public void encoding( Encoding enc ) { // Needs a register, typically a jump-and-link-register opcode // blr - short self = enc.reg(this); + short self = enc.reg(fptr()); enc.add4(Arm64.blr(Arm64.OP_CALLRARM, self)); } diff --git a/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/riscv/CallRRISC.java b/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/riscv/CallRRISC.java index cda4bc79..4ab69430 100644 --- a/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/riscv/CallRRISC.java +++ b/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/riscv/CallRRISC.java @@ -1,12 +1,15 @@ package com.seaofnodes.simple.node.cpus.riscv; +import com.seaofnodes.simple.type.TypeFunPtr; import com.seaofnodes.isa.RiscV; import com.seaofnodes.simple.codegen.*; import com.seaofnodes.simple.node.*; import com.seaofnodes.simple.util.SB; public class CallRRISC extends CallNode implements MachNode { - CallRRISC( CallNode call ) { super(call); } + final TypeFunPtr _tfp; + CallRRISC( CallNode call ) { super(call); _tfp = call.tfp(); } + @Override public TypeFunPtr tfp() { return _tfp; } @Override public String op() { return "callr"; } @Override public String label() { return op(); } @Override public RegMask regmap(int i) { diff --git a/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CallRX86.java b/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CallRX86.java index f97163eb..c204f25c 100644 --- a/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CallRX86.java +++ b/chapter28a/src/main/java/com/seaofnodes/simple/node/cpus/x86_64_v2/CallRX86.java @@ -1,5 +1,6 @@ package com.seaofnodes.simple.node.cpus.x86_64_v2; +import com.seaofnodes.simple.type.TypeFunPtr; import com.seaofnodes.isa.X86; import com.seaofnodes.simple.codegen.*; import com.seaofnodes.simple.node.CallNode; @@ -7,11 +8,13 @@ import com.seaofnodes.simple.node.MachNode; import com.seaofnodes.simple.util.SB; public class CallRX86 extends CallNode implements MachNode { - CallRX86( CallNode call ) { super(call); } + final TypeFunPtr _tfp; + CallRX86( CallNode call ) { super(call); _tfp = call.tfp(); } + @Override public TypeFunPtr tfp() { return _tfp; } @Override public String op() { return "callr"; } @Override public String label() { return op(); } @Override public RegMask regmap(int i) { - return i==_inputs._len + return i==_inputs._len-1 ? x86_64_v2.WMASK // Function call target : x86_64_v2.callInMask(tfp(),i,fun()._maxArgSlot); // Normal argument } -- cgit v1.2.3