Commit 56dc3d1c

56dc3d1c1e04Merged

Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat(codegen): support multi-argument yield + zero-unset block params fix

Two related codegen fixes for inlined yield paths.

Multi-argument yield (matz/spinel#98)

def f; yield 1, 2; end; f { |a, b| puts a + b } failed at C compile because the function-pointer signature for _block hardcoded a single mrb_int slot, while compile_yield_stmt emitted however many args the source carried.

Fix scans each yield-using method's body for the max yield arity and threads that count through yield_params_suffix (top-level) and yield_params_suffix_cls (instance methods) so the function-pointer signature gets N mrb_int slots, then through compile_yield_stmt so the call-site emit pads to N args with 0 for unused slots.

Zero unset block params in inlined mixed-arity yield

The inlined call-site path (compile_stmt_with_block's YieldNode branch) only assigned block-local C vars for the args that the yield actually passed. When a smaller-arity yield followed a larger one in the same method, the unset slots leaked the previous yield's values.

Reproducer pre-fix:

def f
  yield 100, 999
  yield 200
end
f { |a, b| puts a + b.to_i }

CRuby: 1099, 200. Spinel pre-fix: 1099, 1199 (b held 999 from the first yield). Spinel post-fix: 1099, 200.

Fix tracks how many block params received a value at each yield and emits lv_<bp> = 0; for the remaining ones.

Test plan

  • make bootstrap (gen2.c == gen3.c)
  • make test passes (182 tests; 1 new case)
  • test/multi_arg_yield.rb covers 2-arg yield, 3-arg yield, mixed-arity (smaller-after-larger), class-instance-method yield, and three-different-arity yields in one method.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
56dc3d1c1e04
Parent
04000f21a009

spinel_codegen.rbmodifiedView file

@@ -155,6 +155,7 @@
155155 @hoisted_strlen_var = ""
156156 @hoisted_strlen_recv = ""
157157 @in_yield_method = 0
158+ @current_method_yield_arity = 1
158159 @in_gc_scope = 0
159160
160161 # Yield/block tracking (parallel with meth_names / cls_meth_names)
@@ -5233,6 +5234,80 @@
52335234 0
52345235 end
52355236
5237+ # Walks `nid` for YieldNodes and returns max(`current`, max_args_of_yields).
5238+ # Mirrors body_has_yield's recursion shape. `current` carries the running
5239+ # max so callers can seed a floor (1, since every yield-using method needs
5240+ # at least one mrb_int slot in `_block`'s signature).
5241+ def body_max_yield_arity(nid, current)
5242+ if nid < 0
5243+ return current
5244+ end
5245+ if @nd_type[nid] == "YieldNode"
5246+ n = 0
5247+ if @nd_arguments[nid] >= 0
5248+ n = get_args(@nd_arguments[nid]).length
5249+ end
5250+ if n < 1
5251+ n = 1
5252+ end
5253+ if n > current
5254+ current = n
5255+ end
5256+ end
5257+ if @nd_type[nid] == "DefNode"
5258+ return current
5259+ end
5260+ if @nd_body[nid] >= 0
5261+ current = body_max_yield_arity(@nd_body[nid], current)
5262+ end
5263+ stmts = parse_id_list(@nd_stmts[nid])
5264+ k = 0
5265+ while k < stmts.length
5266+ current = body_max_yield_arity(stmts[k], current)
5267+ k = k + 1
5268+ end
5269+ if @nd_expression[nid] >= 0
5270+ current = body_max_yield_arity(@nd_expression[nid], current)
5271+ end
5272+ if @nd_predicate[nid] >= 0
5273+ current = body_max_yield_arity(@nd_predicate[nid], current)
5274+ end
5275+ if @nd_subsequent[nid] >= 0
5276+ current = body_max_yield_arity(@nd_subsequent[nid], current)
5277+ end
5278+ if @nd_else_clause[nid] >= 0
5279+ current = body_max_yield_arity(@nd_else_clause[nid], current)
5280+ end
5281+ if @nd_left[nid] >= 0
5282+ current = body_max_yield_arity(@nd_left[nid], current)
5283+ end
5284+ if @nd_right[nid] >= 0
5285+ current = body_max_yield_arity(@nd_right[nid], current)
5286+ end
5287+ if @nd_block[nid] >= 0
5288+ current = body_max_yield_arity(@nd_block[nid], current)
5289+ end
5290+ conds = parse_id_list(@nd_conditions[nid])
5291+ k = 0
5292+ while k < conds.length
5293+ current = body_max_yield_arity(conds[k], current)
5294+ k = k + 1
5295+ end
5296+ args = parse_id_list(@nd_args[nid])
5297+ k = 0
5298+ while k < args.length
5299+ current = body_max_yield_arity(args[k], current)
5300+ k = k + 1
5301+ end
5302+ if @nd_arguments[nid] >= 0
5303+ current = body_max_yield_arity(@nd_arguments[nid], current)
5304+ end
5305+ if @nd_receiver[nid] >= 0
5306+ current = body_max_yield_arity(@nd_receiver[nid], current)
5307+ end
5308+ current
5309+ end
5310+
52365311 # ---- Return type inference ----
52375312 def infer_constructor_types
52385313 # Scan AST for ClassName.new(args) calls and infer param types
@@ -10421,16 +10496,18 @@
1042110496 end
1042210497
1042310498 def yield_params_suffix(mi)
10499+ csig = block_params_csig(method_yield_arity(mi))
1042410500 pd = method_params_decl(mi)
1042510501 if pd == ""
10426- return "void (*_block)(mrb_int, void*), void *_benv"
10502+ return "void (*_block)(" + csig + ", void*), void *_benv"
1042710503 end
10428- return ", void (*_block)(mrb_int, void*), void *_benv"
10504+ return ", void (*_block)(" + csig + ", void*), void *_benv"
1042910505 end
1043010506
1043110507 def yield_params_suffix_cls(ci, midx)
1043210508 # For class instance methods (always have self first)
10433- return ", void (*_block)(mrb_int, void*), void *_benv"
10509+ csig = block_params_csig(cls_method_yield_arity(ci, midx))
10510+ return ", void (*_block)(" + csig + ", void*), void *_benv"
1043410511 end
1043510512
1043610513 def cls_method_has_yield(ci, midx)
@@ -10443,6 +10520,42 @@
1044310520 0
1044410521 end
1044510522
10523+ # Max number of args used in any `yield` inside the top-level method
10524+ # at @meth_body_ids[mi]. Floor of 1 — yield-using methods always have
10525+ # at least one mrb_int slot (the no-arg `yield` form is padded to 0).
10526+ def method_yield_arity(mi)
10527+ if mi < 0 || mi >= @meth_body_ids.length
10528+ return 1
10529+ end
10530+ body_max_yield_arity(@meth_body_ids[mi], 1)
10531+ end
10532+
10533+ # Same as method_yield_arity, but resolved through the class method
10534+ # body table @cls_meth_bodies (parallel to @cls_meth_has_yield).
10535+ def cls_method_yield_arity(ci, midx)
10536+ if ci < 0 || midx < 0
10537+ return 1
10538+ end
10539+ bodies = @cls_meth_bodies[ci].split(";")
10540+ if midx >= bodies.length
10541+ return 1
10542+ end
10543+ bid = bodies[midx].to_i
10544+ body_max_yield_arity(bid, 1)
10545+ end
10546+
10547+ # Comma-joined string of `arity` mrb_int slots — the variable-arity
10548+ # portion of the `_block` function-pointer signature.
10549+ def block_params_csig(arity)
10550+ csig = "mrb_int"
10551+ k = 1
10552+ while k < arity
10553+ csig = csig + ", mrb_int"
10554+ k = k + 1
10555+ end
10556+ csig
10557+ end
10558+
1044610559 # Returns 1 if the (ci, midx) method declares a `&block` parameter,
1044710560 # 0 otherwise. Ruby syntax requires `&block` to be the trailing
1044810561 # param, so we check only the last slot — a proc-typed slot in any
@@ -11004,6 +11117,7 @@
1100411117 if midx >= 0
1100511118 if cls_method_has_yield(ci, midx) == 1
1100611119 @in_yield_method = 1
11120+ @current_method_yield_arity = cls_method_yield_arity(ci, midx)
1100711121 else
1100811122 @in_yield_method = 0
1100911123 end
@@ -11061,6 +11175,7 @@
1106111175 @current_method_name = ""
1106211176 @current_method_block_param = ""
1106311177 @in_yield_method = 0
11178+ @current_method_yield_arity = 1
1106411179 @indent = 0
1106511180 emit_raw(" return " + c_return_default(rt) + ";")
1106611181 emit_raw("}")
@@ -11184,6 +11299,7 @@
1118411299
1118511300 if @meth_has_yield[mi] == 1
1118611301 @in_yield_method = 1
11302+ @current_method_yield_arity = method_yield_arity(mi)
1118711303 else
1118811304 @in_yield_method = 0
1118911305 end
@@ -11254,6 +11370,7 @@
1125411370 @current_method_name = ""
1125511371 @current_method_block_param = ""
1125611372 @in_yield_method = 0
11373+ @current_method_yield_arity = 1
1125711374 @indent = 0
1125811375 emit_raw(" return " + c_return_default(rt) + ";")
1125911376 emit_raw("}")
@@ -24147,23 +24264,21 @@
2414724264
2414824265 def compile_yield_stmt(nid)
2414924266 args_id = @nd_arguments[nid]
24150- arg_exprs = ""
24267+ emitted = "".split(",")
2415124268 if args_id >= 0
2415224269 aids = get_args(args_id)
2415324270 k = 0
2415424271 while k < aids.length
24155- if k > 0
24156- arg_exprs = arg_exprs + ", "
24157- end
24158- arg_exprs = arg_exprs + compile_expr(aids[k])
24272+ emitted.push(compile_expr(aids[k]))
2415924273 k = k + 1
2416024274 end
2416124275 end
24162- if arg_exprs != ""
24163- emit(" if (_block) _block(" + arg_exprs + ", _benv);")
24164- else
24165- emit(" if (_block) _block(0, _benv);")
24276+ # Pad to the enclosing method's max yield arity so the call matches
24277+ # the function-pointer signature emitted by yield_params_suffix.
24278+ while emitted.length < @current_method_yield_arity
24279+ emitted.push("0")
2416624280 end
24281+ emit(" if (_block) _block(" + emitted.join(", ") + ", _benv);")
2416724282 end
2416824283
2416924284 def compile_yield_call_stmt(nid, mi)
@@ -24258,16 +24373,26 @@
2425824373 if t == "YieldNode"
2425924374 # Replace yield with the block body
2426024375 args_id = @nd_arguments[nid]
24376+ assigned = 0
2426124377 if args_id >= 0
2426224378 aids = get_args(args_id)
2426324379 k = 0
2426424380 while k < aids.length
2426524381 if k < bp_names.length
2426624382 emit(" lv_" + bp_names[k] + " = " + compile_expr_remap(aids[k], map_from, map_to) + ";")
24383+ assigned = assigned + 1
2426724384 end
2426824385 k = k + 1
2426924386 end
2427024387 end
24388+ # Reset any block params that didn't receive a yield arg so a
24389+ # later smaller-arity yield in the same method doesn't leak the
24390+ # previous yield's values. Mirrors compile_yield_stmt's "0"
24391+ # padding on the function-pointer dispatch path.
24392+ while assigned < bp_names.length
24393+ emit(" lv_" + bp_names[assigned] + " = 0;")
24394+ assigned = assigned + 1
24395+ end
2427124396 body = @nd_body[blk]
2427224397 if body >= 0
2427324398 stmts = get_stmts(body)