From 9fe7c6f9697b5bbf3041261fdc60040e75cc674f Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sun, 15 Nov 2026 16:11:39 -0800 Subject: Split off chapter 31 Ch 32 will be focused on compiler performance issues. --- .../com/seaofnodes/simple/parser/Allocations.java | 223 ++++++++++ .../simple/parser/ArrayInitializers.java | 187 ++++++++ .../com/seaofnodes/simple/parser/Construction.java | 131 ++++++ .../com/seaofnodes/simple/parser/ControlFlow.java | 98 ++++ .../com/seaofnodes/simple/parser/Conversions.java | 72 +++ .../com/seaofnodes/simple/parser/Declarations.java | 173 +++++++ .../com/seaofnodes/simple/parser/Expressions.java | 415 +++++++++++++++++ .../com/seaofnodes/simple/parser/Functions.java | 418 +++++++++++++++++ .../java/com/seaofnodes/simple/parser/Lexer.java | 495 +++++++++++++++++++++ .../com/seaofnodes/simple/parser/MethodAccess.java | 130 ++++++ .../java/com/seaofnodes/simple/parser/Parser.java | 300 +++++++++++++ .../java/com/seaofnodes/simple/parser/README.md | 45 ++ .../com/seaofnodes/simple/parser/References.java | 346 ++++++++++++++ .../com/seaofnodes/simple/parser/Statements.java | 284 ++++++++++++ .../java/com/seaofnodes/simple/parser/Strings.java | 59 +++ .../java/com/seaofnodes/simple/parser/Structs.java | 340 ++++++++++++++ .../java/com/seaofnodes/simple/parser/Types.java | 252 +++++++++++ .../java/com/seaofnodes/simple/parser/Var.java | 56 +++ .../com/seaofnodes/simple/parser/package-info.java | 14 + 19 files changed, 4038 insertions(+) create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Allocations.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/ArrayInitializers.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Construction.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/ControlFlow.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Conversions.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Declarations.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Expressions.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Functions.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Lexer.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/MethodAccess.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Parser.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/README.md create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/References.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Statements.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Strings.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Structs.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Types.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/Var.java create mode 100644 chapter32/src/main/java/com/seaofnodes/simple/parser/package-info.java (limited to 'chapter32/src/main/java/com/seaofnodes/simple/parser') diff --git a/chapter32/src/main/java/com/seaofnodes/simple/parser/Allocations.java b/chapter32/src/main/java/com/seaofnodes/simple/parser/Allocations.java new file mode 100644 index 00000000..64234045 --- /dev/null +++ b/chapter32/src/main/java/com/seaofnodes/simple/parser/Allocations.java @@ -0,0 +1,223 @@ +package com.seaofnodes.simple.parser; + +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.type.*; +import com.seaofnodes.simple.util.Ary; + +/** Allocation expressions and declaration-side allocation helper graphs. */ +final class Allocations { + private final Parser P; + + Allocations(Parser P) { this.P = P; } + + /** + Parse an allocation + */ + Node parseNew() { + P._lexer.skipWhiteSpace(); + Lexer site=P.loc(); + Type t = P.types.parseType(); + if( t==null ) { + String tname = P._lexer.matchId(); + if( tname==null || Parser.KEYWORDS.contains(tname) ) + throw Parser.error("Expected a type after 'new'",site); + t = P.types.forwardRefType(tname); + } + // Parse ary[ length_expr ] + if( P.match("[") ) { + boolean readonly = P.match("~"); + if( P.match("]") ) { + P.require("{"); + return P.arrays.parse(t,readonly,null,site); + } + Node len = P.expressions.parseAssignment().keep(); + P.require("]"); + if( P.match("{") ) return P.arrays.parse(t,readonly,len,site); + if( !t.makeZero().isa(t) ) + throw Parser.error("Cannot allocate a non-nullable, since arrays are always zero/null filled",site); + if( readonly ) return new ReadOnlyNode(allocArray(P.types.typeAry(t,true),len)).peephole(); + return allocArray(P.types.typeAry(t,false),len); + } else if( t instanceof TypeMemPtr ary && ary._obj.isAry() && P.match("{") ) { + return P.arrays.parse(ary._obj.field("[]")._t,ary._ro,null,site); + } else { + if( !(t instanceof TypeMemPtr tmp) ) + throw Parser.error("Cannot allocate a "+t.str(),site); + return allocStruct(tmp._obj,site); + } + } + + private Node allocStruct(TypeStruct ts, Lexer site) { + Ary ctorArgs = constructorArgs(); + // A field declaration can leave a short forward type name in TYPES. + // Allocation happens in lexical context, so resolve that short name + // to the completed nested type before selecting its class helper. + String typeName = ts._name.indexOf('.') == -1 ? P.types.fullTypeName(ts._name) : ts._name; + Type resolved = Parser.TYPES.get(typeName); + if( resolved instanceof TypeStruct rts ) ts = rts; + TypeStruct clz = (TypeStruct)Parser.TYPES.get(TypeStruct.addClzPrefix(typeName)); + if( clz==null ) + throw Parser.error("Unknown struct type '" + ts._name + "'",site); + Field afld = clz.field(""); + Node alloc; + if( afld != null ) { + TypeFunPtr tfp = (TypeFunPtr)afld._t; + FunNode fun = P._code.link(tfp); + alloc = fun==null + ? new FRefNode((TypeStruct.addClzPrefix(typeName)+".").intern(),P.loc()).init() + : new FunPtrNode(tfp,P._code._start,fun.ret()).peephole(); + } else { + if( !ts._fref ) + throw Parser.error("Constructor arguments for '" + ts._name + "' but no constructor is defined",site); + Type[] sig = new Type[ctorArgs._len]; + for( int i = 0; i").intern(),P.loc(), + TypeFunPtr.make(false,sig,TypeMemPtr.make(ts))).init(); + } + Ary args = new Ary<>(Node.class); + args.add(P.ctrl()); args.add(P.mem()); + for( Node arg : ctorArgs ) args.add(arg.unkeep()); + args.add(alloc); + Node result = P.functions.functionCall(args); + if( P.ctrl()._type==Type.XCONTROL ) return result; + return new ConstructNode(result,ctorArgs).peephole(); + } + + private Ary constructorArgs() { + Ary args = new Ary<>(Node.class); + if( !P.match("(") ) + return args; + P.functions.arguments(args); + P.require(")"); + return args; + } + + private Node allocArray(TypeStruct ts, Node len) { + Field lenFld = ts.field("#" ); + Field bodyFld = ts.field("[]"); + + Node base = ConFldOffNode.make(ts, "[]"); + int scale = ts.aryScale(); + Node size = P.peep(new AddNode(base,P.peep(new ShlNode(null,len,P.con(scale))))); + + // Build a NewNode; takes in and puts out all aliases. + NewNode nnn = new NewNode(ts, P.ctrl(), size ).init(); + ProjNode self = new ProjNode(nnn,0,ts.str()).init().keep(); + ProjNode smem = new ProjNode(nnn,1,"#selfMem").init().keep(); + + // Store length. Rest of array is zero'd via CALLOC during CodeGen. + // Length is casted to sanity. + // TODO: Needs runtime check. + Node st = P.peep(new StoreNode(null, "#", 1, TypeInteger.U32, null, smem, self, ConFldOffNode.make(ts,"#"), len.unkeep(), true)); + + Node prior = P.mem(); + Node esc = P.peep(new EscapeNode(lenFld,self,st,prior)); + P.mem(P.mergeAlias(prior,lenFld._alias,esc)); + prior = P.mem(); + esc = P.peep(new EscapeNode(bodyFld,self,smem,prior)); + P.mem(P.mergeAlias(prior,bodyFld._alias,esc)); + smem.unkeep(); + return self.unkeep(); + } + + Node newString(byte[] s) { + TypeStruct ts = P.types.typeAry(TypeInteger.U8,true); + int lenAlias = ts.field("#" )._alias; + int elemAlias = ts.field("[]")._alias; + TypeConAry body = TypeConAry.make(s); + TypeInteger slen = TypeInteger.constant(s.length); + // Make a TMP, not-null (byte)2, singleton (true) (requires text + // strings are hash-interned), with a TypeStruct having a constant + // array body. + TypeMemPtr str = TypeMemPtr.make((byte)2,TypeStruct.makeAry("[]u8", slen, lenAlias, body, elemAlias, true),true); + assert str.isConstant(); + return P.con(str.makeRO()); + } + + // Build the declaration-side allocation helper. Allocation sites only + // call this function; all layout, initialization and publication choices + // are made here, where the complete struct and constructor are known. + TypeFunPtr makeAllocator(TypeStruct ts, ReturnNode initRet, Field ctorFld) { + if( ctorFld==null ) { + Field legacy = ts.field(ts._name.substring(ts._name.lastIndexOf('.')+1).intern()); + if( legacy!=null && legacy._t instanceof TypeFunPtr sig && sig.nargs()>=2 && sig.arg(1) instanceof TypeMem ) + ctorFld = legacy; + } + TypeFunPtr ctor = ctorFld==null ? null : (TypeFunPtr)ctorFld._t; + int nargs = ctor==null ? 0 : ctor.nargs()-2; // Hide self and selfMem + Type[] atypes = new Type[nargs]; + for( int i = 0; i").intern(); + + int oldUID = P._code.UID(); + FunNode fun = (FunNode)new FunNode(P.loc(),sig,name,P._ref,null,P._ref._start).peephole(); + P._code.link(fun); + Node rpc = new ParmNode("$rpc",0,TypeRPC.BOT,fun,ConstantNode.seed(TypeRPC.BOT).peephole()).peephole(); + Node pub = new ParmNode(ScopeNode.MEM0,1,TypeMem.BOT,fun, + new ProjNode(P._ref._start,1,ScopeNode.MEM0).peephole()).peephole(); + Node[] parms = new Node[nargs]; + for( int i = 0; i