Commit acf7fbe6

acf7fbe6f958Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

Merge pull request #227 from mame/pr/multi-write-constants

feat(parser+codegen): support A, B, C = expr multi-write to constants

Details

Commit
acf7fbe6f958
Parent
69db8f5aec04
Merge parent
20c30ee255bb

spinel_codegen.rbmodifiedView file

@@ -4899,6 +4899,10 @@
48994899 if @nd_type[sid] == "ConstantWriteNode"
49004900 collect_constant(sid)
49014901 end
4902+ # Top-level `A, B = expr` with constant targets.
4903+ if @nd_type[sid] == "MultiWriteNode"
4904+ collect_scoped_multi_const("", sid)
4905+ end
49024906 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 end
5643+ # Class-body `A, B = ...` multi-write to constants.
5644+ if @nd_type[sid] == "MultiWriteNode"
5645+ collect_scoped_multi_const(cname, sid)
5646+ end
56395647 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 end
6718+ # Module-body `A, B = ...` multi-write to constants.
6719+ if @nd_type[sid] == "MultiWriteNode"
6720+ collect_scoped_multi_const(mname, sid)
6721+ end
67106722 # Collect module class methods (def self.xxx) as top-level functions
67116723 if @nd_type[sid] == "DefNode"
67126724 if @nd_receiver[sid] >= 0
@@ -6774,6 +6786,56 @@
67746786 collect_scoped_constant("", nid)
67756787 end
67766788
6789+ # Multi-write to constants: `A, B, C = expr`. Each ConstantTargetNode
6790+ # in the targets list gets registered as a separate constant whose
6791+ # value is the i-th element of the RHS at emit time. Used both at
6792+ # 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 = 0
6797+ while ti < targets.length
6798+ tid = targets[ti]
6799+ if @nd_type[tid] == "ConstantTargetNode"
6800+ cname = @nd_name[tid]
6801+ if scope_name != ""
6802+ cname = scope_name + "_" + cname
6803+ end
6804+ # Use the existing scope chain when inferring element types so
6805+ # nested-array RHS (`A, B = (1..2).map {...}`) gets the elem
6806+ # type of the array rather than int.
6807+ old_scope = @current_lexical_scope
6808+ @current_lexical_scope = scope_name
6809+ ct = scan_ivars_multi_target_type(val_id, ti)
6810+ @current_lexical_scope = old_scope
6811+ ci = find_const_idx(cname)
6812+ if ci >= 0
6813+ @const_types[ci] = ct
6814+ @const_expr_ids[ci] = -1
6815+ @const_scope_names[ci] = scope_name
6816+ else
6817+ @const_names.push(cname)
6818+ @const_types.push(ct)
6819+ # Element-of-multi: emit_global_constants can't reduce these
6820+ # at module-init time since the source expression is the
6821+ # whole RHS, not a per-target one. Mark expr_id = -1 and
6822+ # rely on @multi_const_inits below to drive the assignment.
6823+ @const_expr_ids.push(-1)
6824+ @const_scope_names.push(scope_name)
6825+ end
6826+ ti = ti + 1
6827+ else
6828+ ti = ti + 1
6829+ end
6830+ end
6831+ # Record the multi-write as a single deferred init: the RHS is
6832+ # evaluated once and each constant takes one element.
6833+ if @multi_const_inits == nil
6834+ @multi_const_inits = "".split(",")
6835+ end
6836+ @multi_const_inits.push(scope_name + "|" + nid.to_s)
6837+ end
6838+
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 1
1498915051 end
1499015052
15053+ # Scan locals introduced by constant-initializer RHS expressions —
15054+ # those run inside main() before the user stmts, so any block
15055+ # params they introduce (`FRAME = [...].map { |n| ... }` and the
15056+ # multi-write form `A, B = [...].map { |n| ... }`) need their
15057+ # `lv_<bp>` decls in main's scope. Covers both `@const_expr_ids`
15058+ # (single-const inits) and `@multi_const_inits` (the multi-write
15059+ # form, where the RHS lives on a MultiWriteNode).
15060+ def scan_const_init_locals(lnames, ltypes, empty_params)
15061+ i = 0
15062+ while i < @const_expr_ids.length
15063+ if @const_expr_ids[i] >= 0
15064+ scan_locals(@const_expr_ids[i], lnames, ltypes, empty_params)
15065+ end
15066+ i = i + 1
15067+ end
15068+ if @multi_const_inits != nil
15069+ j = 0
15070+ while j < @multi_const_inits.length
15071+ mw_id = @multi_const_inits[j].split("|")[1].to_i
15072+ rhs = @nd_expression[mw_id]
15073+ if rhs >= 0
15074+ scan_locals(rhs, lnames, ltypes, empty_params)
15075+ end
15076+ j = j + 1
15077+ end
15078+ end
15079+ end
15080+
1499115081 # ---- Main emission ----
1499215082 def emit_main
1499315083 stmts = get_body_stmts(@root_id)
@@ -15038,15 +15128,10 @@
1503815128 # Constant initializer expressions get compiled into main() right
1503915129 # before the user statements run, but the block params they
1504015130 # introduce (e.g. \`FRAME = [...].map { |n| ... }\`) need
15041- # declarations in the same scope. Scan each const's RHS so the
15042- # block param names land in lnames alongside the user-stmt locals.
15043- ci_lc = 0
15044- while ci_lc < @const_expr_ids.length
15045- if @const_expr_ids[ci_lc] >= 0
15046- scan_locals(@const_expr_ids[ci_lc], lnames, ltypes, empty_params)
15047- end
15048- ci_lc = ci_lc + 1
15049- end
15131+ # declarations in the same scope. Scan each const init's RHS so
15132+ # the block param names land in lnames alongside the user-stmt
15133+ # 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 types
1505215137 j = 0
@@ -15204,19 +15289,78 @@
1520415289 # Constants (initialize global declarations)
1520515290 i = 0
1520615291 while i < @const_names.length
15207- old_scope = @current_lexical_scope
15208- if i < @const_scope_names.length
15209- @current_lexical_scope = @const_scope_names[i]
15210- else
15211- @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 block
15294+ # below, not from a single per-target expression.
15295+ if i < @const_expr_ids.length && @const_expr_ids[i] >= 0
15296+ old_scope = @current_lexical_scope
15297+ if i < @const_scope_names.length
15298+ @current_lexical_scope = @const_scope_names[i]
15299+ else
15300+ @current_lexical_scope = ""
15301+ end
15302+ val = compile_expr(@const_expr_ids[i])
15303+ @current_lexical_scope = old_scope
15304+ emit(" cst_" + @const_names[i] + " = " + val + ";")
15305+ if type_is_pointer(@const_types[i]) == 1
15306+ emit(" SP_GC_ROOT(cst_" + @const_names[i] + ");")
15307+ end
1521215308 end
15213- val = compile_expr(@const_expr_ids[i])
15214- @current_lexical_scope = old_scope
15215- emit(" cst_" + @const_names[i] + " = " + val + ";")
15216- if type_is_pointer(@const_types[i]) == 1
15217- emit(" SP_GC_ROOT(cst_" + @const_names[i] + ");")
15309+ i = i + 1
15310+ end
15311+
15312+ # Multi-write constant initializers: `A, B, C = rhs` evaluates
15313+ # rhs once and assigns each element. ArrayNode literals split
15314+ # element-wise; otherwise we evaluate to a temp and index it
15315+ # (currently only int_array supported, which covers the
15316+ # optcarrot CLK_1..CLK_8 = (1..8).map {...} pattern).
15317+ if @multi_const_inits != nil
15318+ mci = 0
15319+ while mci < @multi_const_inits.length
15320+ parts = @multi_const_inits[mci].split("|")
15321+ scope_n = parts[0]
15322+ mw_id = parts[1].to_i
15323+ targets = parse_id_list(@nd_targets[mw_id])
15324+ val_id = @nd_expression[mw_id]
15325+ old_scope = @current_lexical_scope
15326+ @current_lexical_scope = scope_n
15327+ if val_id >= 0 && @nd_type[val_id] == "ArrayNode"
15328+ elems = parse_id_list(@nd_elements[val_id])
15329+ ti2 = 0
15330+ while ti2 < targets.length
15331+ tid = targets[ti2]
15332+ if @nd_type[tid] == "ConstantTargetNode" && ti2 < elems.length
15333+ cn = @nd_name[tid]
15334+ if scope_n != ""
15335+ cn = scope_n + "_" + cn
15336+ end
15337+ ev = compile_expr(elems[ti2])
15338+ emit(" cst_" + cn + " = " + ev + ";")
15339+ end
15340+ ti2 = ti2 + 1
15341+ end
15342+ else
15343+ rhs_t = infer_type(val_id)
15344+ if rhs_t == "int_array"
15345+ tmp_n = new_temp
15346+ emit(" sp_IntArray *" + tmp_n + " = " + compile_expr(val_id) + ";")
15347+ ti3 = 0
15348+ while ti3 < targets.length
15349+ tid = targets[ti3]
15350+ if @nd_type[tid] == "ConstantTargetNode"
15351+ cn = @nd_name[tid]
15352+ if scope_n != ""
15353+ cn = scope_n + "_" + cn
15354+ end
15355+ emit(" cst_" + cn + " = sp_IntArray_get(" + tmp_n + ", " + ti3.to_s + ");")
15356+ end
15357+ ti3 = ti3 + 1
15358+ end
15359+ end
15360+ end
15361+ @current_lexical_scope = old_scope
15362+ mci = mci + 1
1521815363 end
15219- i = i + 1
1522015364 end
1522115365
1522215366 emit_raw("")