Commit 20c30ee2
20c30ee255bbMerged
Yusuke Endoh <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parser+codegen): support A, B, C = expr multi-write to constants
A, B, C = expr used to silently drop the targets — the parser
emitted ConstantTargetNode as UnknownNode_43, and the
constant-collection passes had no MultiWriteNode branch. Reads of
A/B/C then resolved to undefined identifiers.
This adds:
- A ConstantTargetNode case in the parser.
- An @multi_const_inits side table tracking the multi-write form (the LHS targets and the RHS expression node), populated during the constant-collection passes.
- Codegen that lowers literal-ArrayNode RHS into per-target writes and int_array-returning RHS into a temp + per-target sp_IntArray_get.
- Block-param locals scan that covers @multi_const_inits in
addition to @const_expr_ids — refactored as a shared
scan_const_init_localshelper. Without this, the block param inP, Q = [1, 2].map { |n| n * 10 }would emit as undeclared.
Test: test/multi_write_const.rb covers literal-ArrayNode RHS, a
prior-constant Array RHS, and a .map { |n| ... } RHS exercising
the block-param scan path.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 20c30ee255bb
- Parent
- 69db8f5aec04
spinel_codegen.rbmodifiedView file
@@ -4899,6 +4899,10 @@
48994899
if @nd_type[sid] == "ConstantWriteNode"49004900
collect_constant(sid)49014901
end4902+
# Top-level `A, B = expr` with constant targets.4903+
if @nd_type[sid] == "MultiWriteNode"4904+
collect_scoped_multi_const("", sid)4905+
end49024906
if @nd_type[sid] == "CallNode"49034907
if @nd_name[sid] == "define_method"49044908
collect_define_method(sid)@@ -5636,6 +5640,10 @@
56365640
if @nd_type[sid] == "ConstantWriteNode"56375641
collect_scoped_constant(cname, sid)56385642
end5643+
# Class-body `A, B = ...` multi-write to constants.5644+
if @nd_type[sid] == "MultiWriteNode"5645+
collect_scoped_multi_const(cname, sid)5646+
end56395647
if @nd_type[sid] == "CallNode"56405648
cn = @nd_name[sid]56415649
if cn != "include"@@ -6707,6 +6715,10 @@
67076715
if @nd_type[sid] == "ConstantWriteNode"67086716
collect_scoped_constant(mname, sid)67096717
end6718+
# Module-body `A, B = ...` multi-write to constants.6719+
if @nd_type[sid] == "MultiWriteNode"6720+
collect_scoped_multi_const(mname, sid)6721+
end67106722
# Collect module class methods (def self.xxx) as top-level functions67116723
if @nd_type[sid] == "DefNode"67126724
if @nd_receiver[sid] >= 0@@ -6774,6 +6786,56 @@
67746786
collect_scoped_constant("", nid)67756787
end67766788
6789+
# Multi-write to constants: `A, B, C = expr`. Each ConstantTargetNode6790+
# in the targets list gets registered as a separate constant whose6791+
# value is the i-th element of the RHS at emit time. Used both at6792+
# top level and inside class/module bodies (`scope_name`).6793+
def collect_scoped_multi_const(scope_name, nid)6794+
targets = parse_id_list(@nd_targets[nid])6795+
val_id = @nd_expression[nid]6796+
ti = 06797+
while ti < targets.length6798+
tid = targets[ti]6799+
if @nd_type[tid] == "ConstantTargetNode"6800+
cname = @nd_name[tid]6801+
if scope_name != ""6802+
cname = scope_name + "_" + cname6803+
end6804+
# Use the existing scope chain when inferring element types so6805+
# nested-array RHS (`A, B = (1..2).map {...}`) gets the elem6806+
# type of the array rather than int.6807+
old_scope = @current_lexical_scope6808+
@current_lexical_scope = scope_name6809+
ct = scan_ivars_multi_target_type(val_id, ti)6810+
@current_lexical_scope = old_scope6811+
ci = find_const_idx(cname)6812+
if ci >= 06813+
@const_types[ci] = ct6814+
@const_expr_ids[ci] = -16815+
@const_scope_names[ci] = scope_name6816+
else6817+
@const_names.push(cname)6818+
@const_types.push(ct)6819+
# Element-of-multi: emit_global_constants can't reduce these6820+
# at module-init time since the source expression is the6821+
# whole RHS, not a per-target one. Mark expr_id = -1 and6822+
# rely on @multi_const_inits below to drive the assignment.6823+
@const_expr_ids.push(-1)6824+
@const_scope_names.push(scope_name)6825+
end6826+
ti = ti + 16827+
else6828+
ti = ti + 16829+
end6830+
end6831+
# Record the multi-write as a single deferred init: the RHS is6832+
# evaluated once and each constant takes one element.6833+
if @multi_const_inits == nil6834+
@multi_const_inits = "".split(",")6835+
end6836+
@multi_const_inits.push(scope_name + "|" + nid.to_s)6837+
end6838+
67776839
def collect_struct_class(cname, call_nid)67786840
# Generate a synthetic class from Struct.new(:field1, :field2, ...)67796841
ci = @cls_names.length@@ -14988,6 +15050,34 @@
1498815050
11498915051
end1499015052
15053+
# Scan locals introduced by constant-initializer RHS expressions —15054+
# those run inside main() before the user stmts, so any block15055+
# params they introduce (`FRAME = [...].map { |n| ... }` and the15056+
# multi-write form `A, B = [...].map { |n| ... }`) need their15057+
# `lv_<bp>` decls in main's scope. Covers both `@const_expr_ids`15058+
# (single-const inits) and `@multi_const_inits` (the multi-write15059+
# form, where the RHS lives on a MultiWriteNode).15060+
def scan_const_init_locals(lnames, ltypes, empty_params)15061+
i = 015062+
while i < @const_expr_ids.length15063+
if @const_expr_ids[i] >= 015064+
scan_locals(@const_expr_ids[i], lnames, ltypes, empty_params)15065+
end15066+
i = i + 115067+
end15068+
if @multi_const_inits != nil15069+
j = 015070+
while j < @multi_const_inits.length15071+
mw_id = @multi_const_inits[j].split("|")[1].to_i15072+
rhs = @nd_expression[mw_id]15073+
if rhs >= 015074+
scan_locals(rhs, lnames, ltypes, empty_params)15075+
end15076+
j = j + 115077+
end15078+
end15079+
end15080+
1499115081
# ---- Main emission ----1499215082
def emit_main1499315083
stmts = get_body_stmts(@root_id)@@ -15038,15 +15128,10 @@
1503815128
# Constant initializer expressions get compiled into main() right1503915129
# before the user statements run, but the block params they1504015130
# introduce (e.g. \`FRAME = [...].map { |n| ... }\`) need15041-
# declarations in the same scope. Scan each const's RHS so the15042-
# block param names land in lnames alongside the user-stmt locals.15043-
ci_lc = 015044-
while ci_lc < @const_expr_ids.length15045-
if @const_expr_ids[ci_lc] >= 015046-
scan_locals(@const_expr_ids[ci_lc], lnames, ltypes, empty_params)15047-
end15048-
ci_lc = ci_lc + 115049-
end15131+
# declarations in the same scope. Scan each const init's RHS so15132+
# the block param names land in lnames alongside the user-stmt15133+
# locals. Covers both single-const and multi-write-const forms.15134+
scan_const_init_locals(lnames, ltypes, empty_params)1505015135
1505115136
# Declare vars for second pass to resolve dependent types1505215137
j = 0@@ -15204,19 +15289,78 @@
1520415289
# Constants (initialize global declarations)1520515290
i = 01520615291
while i < @const_names.length15207-
old_scope = @current_lexical_scope15208-
if i < @const_scope_names.length15209-
@current_lexical_scope = @const_scope_names[i]15210-
else15211-
@current_lexical_scope = ""15292+
# expr_id == -1 means this constant came from a `A, B, C = expr`15293+
# multi-write: its value is computed by the deferred init block15294+
# below, not from a single per-target expression.15295+
if i < @const_expr_ids.length && @const_expr_ids[i] >= 015296+
old_scope = @current_lexical_scope15297+
if i < @const_scope_names.length15298+
@current_lexical_scope = @const_scope_names[i]15299+
else15300+
@current_lexical_scope = ""15301+
end15302+
val = compile_expr(@const_expr_ids[i])15303+
@current_lexical_scope = old_scope15304+
emit(" cst_" + @const_names[i] + " = " + val + ";")15305+
if type_is_pointer(@const_types[i]) == 115306+
emit(" SP_GC_ROOT(cst_" + @const_names[i] + ");")15307+
end1521215308
end15213-
val = compile_expr(@const_expr_ids[i])15214-
@current_lexical_scope = old_scope15215-
emit(" cst_" + @const_names[i] + " = " + val + ";")15216-
if type_is_pointer(@const_types[i]) == 115217-
emit(" SP_GC_ROOT(cst_" + @const_names[i] + ");")15309+
i = i + 115310+
end15311+
15312+
# Multi-write constant initializers: `A, B, C = rhs` evaluates15313+
# rhs once and assigns each element. ArrayNode literals split15314+
# element-wise; otherwise we evaluate to a temp and index it15315+
# (currently only int_array supported, which covers the15316+
# optcarrot CLK_1..CLK_8 = (1..8).map {...} pattern).15317+
if @multi_const_inits != nil15318+
mci = 015319+
while mci < @multi_const_inits.length15320+
parts = @multi_const_inits[mci].split("|")15321+
scope_n = parts[0]15322+
mw_id = parts[1].to_i15323+
targets = parse_id_list(@nd_targets[mw_id])15324+
val_id = @nd_expression[mw_id]15325+
old_scope = @current_lexical_scope15326+
@current_lexical_scope = scope_n15327+
if val_id >= 0 && @nd_type[val_id] == "ArrayNode"15328+
elems = parse_id_list(@nd_elements[val_id])15329+
ti2 = 015330+
while ti2 < targets.length15331+
tid = targets[ti2]15332+
if @nd_type[tid] == "ConstantTargetNode" && ti2 < elems.length15333+
cn = @nd_name[tid]15334+
if scope_n != ""15335+
cn = scope_n + "_" + cn15336+
end15337+
ev = compile_expr(elems[ti2])15338+
emit(" cst_" + cn + " = " + ev + ";")15339+
end15340+
ti2 = ti2 + 115341+
end15342+
else15343+
rhs_t = infer_type(val_id)15344+
if rhs_t == "int_array"15345+
tmp_n = new_temp15346+
emit(" sp_IntArray *" + tmp_n + " = " + compile_expr(val_id) + ";")15347+
ti3 = 015348+
while ti3 < targets.length15349+
tid = targets[ti3]15350+
if @nd_type[tid] == "ConstantTargetNode"15351+
cn = @nd_name[tid]15352+
if scope_n != ""15353+
cn = scope_n + "_" + cn15354+
end15355+
emit(" cst_" + cn + " = sp_IntArray_get(" + tmp_n + ", " + ti3.to_s + ");")15356+
end15357+
ti3 = ti3 + 115358+
end15359+
end15360+
end15361+
@current_lexical_scope = old_scope15362+
mci = mci + 11521815363
end15219-
i = i + 11522015364
end1522115365
1522215366
emit_raw("")spinel_parse.cmodifiedView file
Loading diff…
test/multi_write_const.rbaddedView file
Loading diff…
test/multi_write_const.rb.expectedaddedView file
Loading diff…