Commit b7c6eaa4
b7c6eaa4e0f5Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support AliasGlobalVariableNode (alias $copy $orig)
Compile-time only: $copy and $orig share storage by rewriting all references to $copy through $orig at three load-bearing call sites. Matches CRuby's "alias makes them aliases of the same slot" semantics for the static (toplevel) case.
Three pieces:
-
Parser case in spinel_parse.c -- emits the node with new_name and old_name as ref children (both are GlobalVariableReadNode).
-
New ivars + alloc_node fields + set_ref_field handlers for "new_name" / "old_name". Shared infrastructure for the upcoming AliasMethodNode commit.
-
@galias_new/@galias_old maps populated by collect_all Pass 2.1. resolve_gvar_alias(name) walks the map. Three call sites that need the rewrite:
- sanitize_gvar -- so the C identifier (
gv_<bare>) is the same for both names. - scan_features's GlobalVariableWriteNode/ReadNode arms -- so the gvar-type registry stores under one slot, avoiding a spurious type-mismatch error when $copy and $orig get different inferred types.
- infer_type's GlobalVariableReadNode arm -- so a $copy read returns $orig's registered type for printf format selection.
- sanitize_gvar -- so the C identifier (
compile_stmt arm is a no-op (the alias took effect at collect time).
Out of scope: dynamic alias from inside a method body (the alias map is built once at static collect time; method-scoped aliases would need a runtime registry).
Test exercises (test/alias_global.rb):
- Initial alias: $copy reads $orig's value.
- $orig mutation: $copy reflects the new value.
- $copy mutation: $orig reflects the new value (shared storage).
Tests: 275 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b7c6eaa4e0f5
- Parent
- f5fc728473ba
spinel_codegen.rbmodifiedView file
# (post-rest parameters aren't observed in test/), but the parser # emits the field so future tests get a proper AST. @nd_posts = "".split(",") # AliasMethodNode / AliasGlobalVariableNode -- parallel ref slots # for the new and old names (SymbolNode for methods, GlobalVariableReadNode # for globals). @nd_new_name = [] @nd_old_name = [] @nd_count = 0 @root_id = 0 @redo_label_stack = "".split(",") @redo_label_counter = 0 # `alias $copy $orig` -- maps new gvar name to its target. # Populated by collect_all from AliasGlobalVariableNode # statements; consulted by sanitize_gvar / scan_features / # infer_type so $copy and $orig share storage. @galias_new = "".split(",") @galias_old = "".split(",") # ---- Scope stack for local variables ---- @scope_names = "".split(",") @scope_types = "".split(",") @nd_targets.push("") @nd_rights.push("") @nd_posts.push("") @nd_new_name.push(-1) @nd_old_name.push(-1) @nd_count = @nd_count + 1 nid end if field == "call" @nd_receiver[nid] = ref_id end if field == "new_name" # AliasMethodNode / AliasGlobalVariableNode -- the new-name slot # (a SymbolNode for methods, GlobalVariableReadNode for globals). @nd_new_name[nid] = ref_id end if field == "old_name" @nd_old_name[nid] = ref_id end end def set_array_field(nid, field, ids_str) return "int" end if t == "GlobalVariableReadNode" gname = @nd_name[nid] # `alias $copy $orig` -- a $copy read must look up $orig's # registered type so the C codegen sees the correct format # specifier when interpolating or printing. gname = resolve_gvar_alias(@nd_name[nid]) gi = 0 while gi < @gvar_names.length if @gvar_names[gi] == gname end def sanitize_gvar(name) # `alias $copy $orig` -- transparently rewrite `$copy` to `$orig` # everywhere it appears so the C output uses one storage slot # for both names. Compile-time only; matches CRuby's # `$copy and $orig share storage` semantics for the static-alias # case. name = resolve_gvar_alias(name) # $last → gv_last, $1 → gv_1 if name.length > 0 && name[0] == "$" return "gv_" + name[1, name.length - 1] "gv_" + name end def resolve_gvar_alias(name) i = 0 while i < @galias_new.length if @galias_new[i] == name return @galias_old[i] end i = i + 1 end name end # ---- Array type helpers ---- def array_c_prefix(t) if t == "str_array" end } # Top-level `alias $copy $orig`. Records the rewrite in # @galias_* so sanitize_gvar / scan_features / infer_type # treat $copy and $orig as the same storage slot. stmts.each { |sid| if @nd_type[sid] == "AliasGlobalVariableNode" nn = @nd_name[@nd_new_name[sid]] on = @nd_name[@nd_old_name[sid]] if nn != "" && on != "" @galias_new.push(nn) @galias_old.push(on) end end } # Pass 2.6: hoist `recv.instance_eval do ... end` blocks into # file-scope static functions. Receiver-class flow analysis picks the # receiver's class, the block body is later compiled as a function end if t == "GlobalVariableWriteNode" gname = @nd_name[nid] # `alias $copy $orig` -- a $copy = ... write must register # the type under $orig's slot, not a separate $copy slot, # otherwise the type-consistency check below would later see # $orig and $copy as two slots that resolve to the same C # global and fire a spurious type-mismatch error. gname = resolve_gvar_alias(@nd_name[nid]) if gname != "$stderr" && gname != "$stdout" && gname != "$?" gt = infer_type(@nd_expression[nid]) if not_in(gname, @gvar_names) == 1 end end if t == "GlobalVariableReadNode" gname = @nd_name[nid] # Same alias resolution as the write side -- $copy reads must # land on $orig's slot. gname = resolve_gvar_alias(@nd_name[nid]) if gname != "$stderr" && gname != "$stdout" && gname != "$?" if not_in(gname, @gvar_names) == 1 @gvar_names.push(gname) emit(" goto " + lbl + ";") return end if t == "AliasGlobalVariableNode" # `alias $copy $orig` -- compile-time only. collect_all # already populated @galias_*; references through # sanitize_gvar / scan_features / infer_type pick up the # rewrite. Nothing to emit at runtime. return end if t == "LocalVariableWriteNode" lname = @nd_name[nid] # Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/alias_global.rbaddedView file
Loading diff…
test/alias_global.rb.expectedaddedView file
Loading diff…