From d85354f39d267e1840edeebf1e6e81b90985d3a8 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Tue, 29 Apr 2025 21:52:24 -0700 Subject: Drop a false start for adding clazz fields. GLB does not widen primitives in memory. Missing float-cmp-copy --- .../main/java/com/seaofnodes/simple/Parser.java | 133 ++++++++------------- .../src/main/java/com/seaofnodes/simple/Var.java | 17 +-- .../java/com/seaofnodes/simple/node/BoolNode.java | 21 +++- .../com/seaofnodes/simple/node/MemMergeNode.java | 2 +- .../java/com/seaofnodes/simple/node/MulNode.java | 4 +- .../java/com/seaofnodes/simple/node/PhiNode.java | 4 +- .../java/com/seaofnodes/simple/node/ScopeNode.java | 55 ++------- .../java/com/seaofnodes/simple/type/Field.java | 4 +- .../main/java/com/seaofnodes/simple/type/Type.java | 5 +- .../java/com/seaofnodes/simple/type/TypeFloat.java | 2 +- .../com/seaofnodes/simple/type/TypeFunPtr.java | 2 +- .../com/seaofnodes/simple/type/TypeInteger.java | 2 +- .../java/com/seaofnodes/simple/type/TypeMem.java | 2 +- .../com/seaofnodes/simple/type/TypeMemPtr.java | 2 +- .../java/com/seaofnodes/simple/type/TypeNil.java | 2 +- .../java/com/seaofnodes/simple/type/TypePtr.java | 2 +- .../com/seaofnodes/simple/type/TypeStruct.java | 10 +- .../java/com/seaofnodes/simple/type/TypeTuple.java | 5 +- 18 files changed, 107 insertions(+), 167 deletions(-) (limited to 'chapter22/src/main/java/com') diff --git a/chapter22/src/main/java/com/seaofnodes/simple/Parser.java b/chapter22/src/main/java/com/seaofnodes/simple/Parser.java index ebc7cdc5..bbf841c8 100644 --- a/chapter22/src/main/java/com/seaofnodes/simple/Parser.java +++ b/chapter22/src/main/java/com/seaofnodes/simple/Parser.java @@ -81,11 +81,31 @@ public class Parser { ScopeNode _breakScope; // Merge all the while-breaks here FunNode _fun; // Current function being parsed - // Mapping from a primitive name to a Type. The string name matches + // Mapping from a type name to a Type. The string name matches // `type.str()` call. No TypeMemPtrs are in here, because Simple does not // have C-style '*ptr' references. - public static final HashMap PRIMS = - new HashMap<>() {{ + public static HashMap TYPES = defaultTypes(); + + // Mapping from a type name to the constructor for a Type. + public final HashMap INITS; + + + public Parser(CodeGen code, TypeInteger arg) { + _code = code; + _scope = new ScopeNode(); + _continueScope = _breakScope = null; + ZERO = con(TypeInteger.ZERO).keep(); + NIL = con(Type.NIL).keep(); + XCTRL= new XCtrlNode().peephole().keep(); + TYPES = defaultTypes(); + INITS = new HashMap<>(); + } + + @Override + public String toString() { return _lexer.toString(); } + + public static HashMap defaultTypes() { + return new HashMap<>() {{ put("bool",TypeInteger.U1 ); put("byte",TypeInteger.U8 ); put("f32" ,TypeFloat .F32); @@ -103,25 +123,8 @@ public class Parser { put("val" ,Type.TOP); // Marker type, indicates type inference put("var" ,Type.BOTTOM); // Marker type, indicates type inference }}; - - - // Mapping from a type name to the constructor for a Type. - public final HashMap INITS; - - - public Parser(CodeGen code, TypeInteger arg) { - _code = code; - _scope = new ScopeNode(); - _continueScope = _breakScope = null; - ZERO = con(TypeInteger.ZERO).keep(); - NIL = con(Type.NIL).keep(); - XCTRL= new XCtrlNode().peephole().keep(); - INITS = new HashMap<>(); } - @Override - public String toString() { return _lexer.toString(); } - // Debugging utility to find a Node by index public Node f(int nid) { return _code.f(nid); } @@ -217,7 +220,7 @@ public class Parser { // Pre-call the function from Start, with worse-case arguments. This // represents all the future, yet-to-be-parsed functions calls and // external calls. - _scope.push(ScopeNode.Kind.Function,null); + _scope.push(ScopeNode.Kind.Function); ctrl(fun); // Scope control from function // Private mem alias tracking per function MemMergeNode mem = new MemMergeNode(true); @@ -276,7 +279,7 @@ public class Parser { */ private Node parseBlock(ScopeNode.Kind kind) { // Enter a new scope - _scope.push(kind,null); + _scope.push(kind); Node last = ZERO; while (!peek('}') && !_lexer.isEOF()) last = parseStatement(); @@ -343,7 +346,7 @@ public class Parser { // } // } require("("); - _scope.push(ScopeNode.Kind.Block,null); // Scope for the index variables + _scope.push(ScopeNode.Kind.Block); // Scope for the index variables if( !match(";") ) // Can be empty init "for(;test;next) body" parseDeclarationStatement(); // Non-empty init Node rez = parseLooping(true); @@ -677,17 +680,9 @@ public class Parser { * exprAsgn = var '=' exprAsgn | expr */ private Node parseDeclarationStatement() { - int old = pos(); Type t = type(); if( t == null ) return require(parseAsgn(),";"); - // Ambiguous leading type then doing a type/class static field lookup. - // "T var" - declare a variable of type T - // "T.fld" - lookup field "fld" in type T - if( peek('.') ) { - pos(old); - return require(parseAsgn(),";"); - } // now parse var['=' asgnexpr] in a loop Node n = parseDeclaration(t); @@ -727,7 +722,7 @@ public class Parser { if( expr._type==Type.NIL ) throw error("a not-null/non-zero expression"); t = expr._type; - if( !xfinal ) t = t.glb(); // Widen if not final + if( !xfinal ) t = t.glb(false); // Widen if not final } // expr is a constant function if( t instanceof TypeFunPtr && expr._type instanceof TypeFunPtr tfp && tfp.isConstant() ) { @@ -739,7 +734,7 @@ public class Parser { // Need an expression to infer the type. // Also, if not-null then need an initializing expression. // Allowed in a class def, because type/init will happen in the constructor. - if( (inferType || (t instanceof TypeNil tn && !tn.nullable() )) && !_scope.inDeclarationQ() ) + if( (inferType || (t instanceof TypeNil tn && !tn.nullable() )) && !_scope.inConstructor() ) throw errorSyntax("=expression"); // Initial value for uninitialized struct fields. expr = switch( t ) { @@ -784,7 +779,7 @@ public class Parser { String typeName = requireId(); // A Block scope parse, and inspect the scope afterward for fields. - _scope.push(ScopeNode.Kind.DeclarationQ,typeName); + _scope.push(ScopeNode.Kind.Constructor); require("{"); while (!peek('}') && !_lexer.isEOF()) parseStatement().isKill(); @@ -792,43 +787,24 @@ public class Parser { // Grab the declarations and build fields and a Struct int lexlen = _scope._lexSize.last(); int varlen = _scope._vars._len; - StructNode inst = new StructNode(); - StructNode clz = new StructNode(); - Ary fs = new Ary<>(Field.class); // Struct fields - Ary cs = new Ary<>(Field.class); // Class fields + StructNode s = new StructNode(); + Ary fs = new Ary<>(Field.class); for( int i=lexlen; i