Commit b2d417b1
b2d417b1d521Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support ClassVariableReadNode (@@var) + body init folding
Reads the per-(class,name) C global registered by the ClassVariableWriteNode commit. compile_expr arm:
if t == "ClassVariableReadNode"
return "cvar_" + cvar_qname(@current_class_idx, @nd_name[nid])
end
infer_type arm classifies as the registered cvar type (or "int" fallback for unregistered reads, mirroring the InstanceVariableReadNode fallback at line 1476).
Also lands two cvar correctness fixes the expanded test surfaced:
-
Expression-form ClassVariableWriteNode arm in compile_expr. Method bodies ending in
@@x = exprroute through compile_expr (the last-expression-as-return path), not compile_stmt. Without the arm,def tick; @@count = @@count + 1; endreturned 0 and the write was dropped. Adds the C-comma form(cvar_X = val)so the assignment fires AND the value is the result, matching InstanceVariableWriteNode at line 14780. -
Compile-time const folding of class-body literal initializers. Spinel doesn't run class-body statements at startup -- main()'s loop skips ClassNode -- so
class C; @@x = 42; endleft cvar_C_x at type-default (0) until the first runtime write. collect_cvars now calls try_fold_cvar_init when the RHS is a simple literal (Integer/Float/String/Symbol/True/False/Nil), capturing the value into @cvar_init_values. The static decl emit pass uses the folded value as the C initializer:static mrb_int cvar_Counter_count = 0; // before static mrb_int cvar_Other_count = 100; // after static mrb_bool cvar_Config_debug = TRUE; // after
Test fixture exercises five real-world cvar idioms:
- Singleton-counter (Counter.tick / .value / instance_value): three call paths into the same slot (class method write, class method read, instance method read).
- Per-class isolation (Counter vs Other with same @@count name): verifies cvar_qname's class-prefixing keeps the slots independent.
- Body-level literal init (Config.@@debug / @@max_retries): exercises the const-fold path -- without it both reads return 0/false.
- Many-call accumulator (Tally.add over multiple invocations): cvar state survives across class-method calls.
- Cross-method-kind mutation (Registry: instance method writes, class method reads): same @current_class_idx for both, so they agree on the slot.
Out of scope (documented inline): conditional class-body
rewrites (if X; @@y = ...; end -- needs a class-init-function);
hierarchy-shared cvars (Spinel intentionally doesn't walk the
inheritance chain); chained class-method .add(n).add(m) returning
self (Spinel's class methods are static C functions with no self).
Tests: 259 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b2d417b1d521
- Parent
- 40403f434501
spinel_codegen.rbmodifiedView file
# storage model better. Documented in the test fixtures. @cvar_names = "".split(",") @cvar_types = "".split(",") # Compile-time literal initializer per cvar, if the class-body # write was `@@x = <literal>`. "" means "use type-default". This # is necessary because Spinel doesn't run class-body statements # at startup, so any initializer that's not a fold-able literal # leaves the cvar at its type-default until first write. @cvar_init_values = "".split(",") @const_expr_ids = [] @const_scope_names = "".split(",") end # Register or update a cvar's inferred type. Called from # scan_class_vars during the pre-pass and (defensively) again from # collect_cvars during the pre-pass and (defensively) again from # compile_stmt when the write fires. def register_cvar(qname, t) ci = find_cvar_idx(qname) end @cvar_names.push(qname) @cvar_types.push(t) @cvar_init_values.push("") @cvar_names.length - 1 end # Try to compile-time fold a class-body cvar initializer. If the # value is a simple literal (Integer/Float/String/Symbol/True/ # False/Nil), capture it as the static decl's initializer so the # cvar enters the program with the source's value rather than the # type default. Spinel doesn't run class-body statements at # startup, so without this fold a `class C; @@x = 42; end` leaves # cvar_C_x at 0 until the first write fires. def try_fold_cvar_init(qname, value_id) if value_id < 0 return end vt = @nd_type[value_id] lit = "" if vt == "IntegerNode" lit = @nd_value[value_id].to_s end if vt == "FloatNode" lit = @nd_content[value_id] end if vt == "StringNode" lit = c_string_literal(@nd_content[value_id]) end if vt == "SymbolNode" lit = compile_symbol_literal(@nd_content[value_id]) end if vt == "TrueNode" lit = "TRUE" end if vt == "FalseNode" lit = "FALSE" end if vt == "NilNode" lit = "0" end if lit == "" return end ci = find_cvar_idx(qname) if ci >= 0 @cvar_init_values[ci] = lit end end # If the constant's initializer is a simple literal, return the # corresponding C expression. Otherwise return "" so callers fall # back to cst_<name> lookup. Enables propagation of: end return "int" end if t == "ClassVariableReadNode" qname = cvar_qname(@current_class_idx, @nd_name[nid]) ci = find_cvar_idx(qname) if ci >= 0 return @cvar_types[ci] end return "int" end if t == "ConstantReadNode" if @nd_name[nid] == "ARGV" return "argv" qname = cvar_qname(class_idx, @nd_name[nid]) val_t = infer_type(@nd_expression[nid]) register_cvar(qname, val_t) try_fold_cvar_init(qname, @nd_expression[nid]) end cs = [] push_child_ids(nid, cs) end # Emit file-scope class-variable declarations. One C global per # (class, name) pair, named `cvar_<ClassName>_<var>`; populated # by the ClassVariable*WriteNode codegen arms. # by the ClassVariable*WriteNode codegen arms. If the class-body # initializer was a simple literal, collect_cvars folded it into # @cvar_init_values; otherwise we fall back to the type default # and the cvar is undefined-acting until the first runtime write. i = 0 while i < @cvar_names.length ctp = c_type(@cvar_types[i]) emit_raw("static " + ctp + " cvar_" + @cvar_names[i] + " = " + c_default_val(@cvar_types[i]) + ";") init = @cvar_init_values[i] if init == "" init = c_default_val(@cvar_types[i]) end emit_raw("static " + ctp + " cvar_" + @cvar_names[i] + " = " + init + ";") i = i + 1 end if @cvar_names.length > 0 end return ivar_lhs(@nd_name[nid]) end if t == "ClassVariableReadNode" # `@@var` -- reads the per-(class,name) C global registered # by collect_cvars. @current_class_idx is set during class # body and class/instance method compilation, so the lookup # resolves to whichever class's body or method we're in. return "cvar_" + cvar_qname(@current_class_idx, @nd_name[nid]) end if t == "ClassVariableWriteNode" # Expression-form write: `def tick; @@x = @@x + 1; end` -- # the method body's last expression is the write, and # CRuby returns the assigned value. Emits the C-comma # form so the assignment fires AND the value is the # result, matching the InstanceVariableWriteNode pattern # at line 14780. qname = cvar_qname(@current_class_idx, @nd_name[nid]) val = compile_expr(@nd_expression[nid]) val_t = infer_type(@nd_expression[nid]) register_cvar(qname, val_t) return "(cvar_" + qname + " = " + val + ")" end if t == "InstanceVariableWriteNode" # Issue #130: same poly-slot boxing as the statement-form emit. # Expression form (`x = (@y = expr)`) is rarer but reaches thespinel_parse.cmodifiedView file
Loading diff…
test/class_var_read.rbaddedView file
Loading diff…
test/class_var_read.rb.expectedaddedView file
Loading diff…